Skip to content

Commit

Permalink
Artifact Registry: add version policy support for maven (#5294) (#4112)
Browse files Browse the repository at this point in the history
* Artifact Registry: add version policy support for maven

* Artifact Registry: add version policy support for maven

* Fixing description

* tests

* Fixing camelcase to snake case

* Update mmv1/third_party/terraform/tests/resource_artifact_registry_repository_test.go.erb

Co-authored-by: Stephen Lewis (Burrows) <[email protected]>

* Update mmv1/products/artifactregistry/api.yaml

Co-authored-by: Stephen Lewis (Burrows) <[email protected]>

* Update mmv1/products/artifactregistry/api.yaml

Co-authored-by: Stephen Lewis (Burrows) <[email protected]>

* Update mmv1/products/artifactregistry/api.yaml

Co-authored-by: Stephen Lewis (Burrows) <[email protected]>

* Resolving comments

* Addressing comments

* Restricting modifications for allowSnapshotsOverwrite

* fixing test names

* Fixing tests

Co-authored-by: Yury Gridasov <[email protected]>
Co-authored-by: Stephen Lewis (Burrows) <[email protected]>
Signed-off-by: Modular Magician <[email protected]>

Co-authored-by: Yury Gridasov <[email protected]>
Co-authored-by: Stephen Lewis (Burrows) <[email protected]>
  • Loading branch information
3 people authored Mar 9, 2022
1 parent 84e620b commit a8e74c4
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/5294.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
artifactregistry: added maven config for `google_artifact_registry_repository`
```
103 changes: 103 additions & 0 deletions google-beta/resource_artifact_registry_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,33 @@ and dashes.`,
ForceNew: true,
Description: `The name of the location this repository is located in.`,
},
"maven_config": {
Type: schema.TypeList,
Optional: true,
Description: `MavenRepositoryConfig is maven related repository details.
Provides additional configuration details for repositories of the maven
format type.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"allow_snapshot_overwrites": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Description: `The repository with this flag will allow publishing the same
snapshot versions.`,
},
"version_policy": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validateEnum([]string{"VERSION_POLICY_UNSPECIFIED", "RELEASE", "SNAPSHOT", ""}),
Description: `Version policy defines the versions that the registry will accept. Default value: "VERSION_POLICY_UNSPECIFIED" Possible values: ["VERSION_POLICY_UNSPECIFIED", "RELEASE", "SNAPSHOT"]`,
Default: "VERSION_POLICY_UNSPECIFIED",
},
},
},
},
"create_time": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -155,6 +182,12 @@ func resourceArtifactRegistryRepositoryCreate(d *schema.ResourceData, meta inter
} else if v, ok := d.GetOkExists("kms_key_name"); !isEmptyValue(reflect.ValueOf(kmsKeyNameProp)) && (ok || !reflect.DeepEqual(v, kmsKeyNameProp)) {
obj["kmsKeyName"] = kmsKeyNameProp
}
mavenConfigProp, err := expandArtifactRegistryRepositoryMavenConfig(d.Get("maven_config"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("maven_config"); !isEmptyValue(reflect.ValueOf(mavenConfigProp)) && (ok || !reflect.DeepEqual(v, mavenConfigProp)) {
obj["mavenConfig"] = mavenConfigProp
}

url, err := replaceVars(d, config, "{{ArtifactRegistryBasePath}}projects/{{project}}/locations/{{location}}/repositories?repository_id={{repository_id}}")
if err != nil {
Expand Down Expand Up @@ -270,6 +303,9 @@ func resourceArtifactRegistryRepositoryRead(d *schema.ResourceData, meta interfa
if err := d.Set("update_time", flattenArtifactRegistryRepositoryUpdateTime(res["updateTime"], d, config)); err != nil {
return fmt.Errorf("Error reading Repository: %s", err)
}
if err := d.Set("maven_config", flattenArtifactRegistryRepositoryMavenConfig(res["mavenConfig"], d, config)); err != nil {
return fmt.Errorf("Error reading Repository: %s", err)
}

return nil
}
Expand Down Expand Up @@ -302,6 +338,12 @@ func resourceArtifactRegistryRepositoryUpdate(d *schema.ResourceData, meta inter
} else if v, ok := d.GetOkExists("labels"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}
mavenConfigProp, err := expandArtifactRegistryRepositoryMavenConfig(d.Get("maven_config"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("maven_config"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, mavenConfigProp)) {
obj["mavenConfig"] = mavenConfigProp
}

url, err := replaceVars(d, config, "{{ArtifactRegistryBasePath}}projects/{{project}}/locations/{{location}}/repositories/{{repository_id}}")
if err != nil {
Expand All @@ -318,6 +360,10 @@ func resourceArtifactRegistryRepositoryUpdate(d *schema.ResourceData, meta inter
if d.HasChange("labels") {
updateMask = append(updateMask, "labels")
}

if d.HasChange("maven_config") {
updateMask = append(updateMask, "mavenConfig")
}
// updateMask is a URL parameter but not present in the schema, so replaceVars
// won't set it
url, err = addQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")})
Expand Down Expand Up @@ -438,6 +484,29 @@ func flattenArtifactRegistryRepositoryUpdateTime(v interface{}, d *schema.Resour
return v
}

func flattenArtifactRegistryRepositoryMavenConfig(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["allow_snapshot_overwrites"] =
flattenArtifactRegistryRepositoryMavenConfigAllowSnapshotOverwrites(original["allowSnapshotOverwrites"], d, config)
transformed["version_policy"] =
flattenArtifactRegistryRepositoryMavenConfigVersionPolicy(original["versionPolicy"], d, config)
return []interface{}{transformed}
}
func flattenArtifactRegistryRepositoryMavenConfigAllowSnapshotOverwrites(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenArtifactRegistryRepositoryMavenConfigVersionPolicy(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func expandArtifactRegistryRepositoryFormat(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand All @@ -460,3 +529,37 @@ func expandArtifactRegistryRepositoryLabels(v interface{}, d TerraformResourceDa
func expandArtifactRegistryRepositoryKmsKeyName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandArtifactRegistryRepositoryMavenConfig(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedAllowSnapshotOverwrites, err := expandArtifactRegistryRepositoryMavenConfigAllowSnapshotOverwrites(original["allow_snapshot_overwrites"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedAllowSnapshotOverwrites); val.IsValid() && !isEmptyValue(val) {
transformed["allowSnapshotOverwrites"] = transformedAllowSnapshotOverwrites
}

transformedVersionPolicy, err := expandArtifactRegistryRepositoryMavenConfigVersionPolicy(original["version_policy"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedVersionPolicy); val.IsValid() && !isEmptyValue(val) {
transformed["versionPolicy"] = transformedVersionPolicy
}

return transformed, nil
}

func expandArtifactRegistryRepositoryMavenConfigAllowSnapshotOverwrites(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandArtifactRegistryRepositoryMavenConfigVersionPolicy(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
60 changes: 60 additions & 0 deletions google-beta/resource_artifact_registry_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,50 @@ func TestAccArtifactRegistryRepository_update(t *testing.T) {
})
}

func TestAccArtifactRegistryRepository_create_mvn_snapshot(t *testing.T) {
t.Parallel()

repositoryID := fmt.Sprintf("tf-test-%d", randInt(t))

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProvidersOiCS,
CheckDestroy: testAccCheckArtifactRegistryRepositoryDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccArtifactRegistryRepository_create(repositoryID, "SNAPSHOT"),
},
{
ResourceName: "google_artifact_registry_repository.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccArtifactRegistryRepository_create_mvn_release(t *testing.T) {
t.Parallel()

repositoryID := fmt.Sprintf("tf-test-%d", randInt(t))

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProvidersOiCS,
CheckDestroy: testAccCheckArtifactRegistryRepositoryDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccArtifactRegistryRepository_create(repositoryID, "RELEASE"),
},
{
ResourceName: "google_artifact_registry_repository.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccArtifactRegistryRepository_update(repositoryID string) string {
return fmt.Sprintf(`
resource "google_artifact_registry_repository" "test" {
Expand Down Expand Up @@ -72,3 +116,19 @@ resource "google_artifact_registry_repository" "test" {
}
`, repositoryID)
}

func testAccArtifactRegistryRepository_create(repositoryID string, versionPolicy string) string {
return fmt.Sprintf(`
resource "google_artifact_registry_repository" "test" {
provider = google-beta
repository_id = "%s"
location = "us-central1"
description = "post-update"
format = "MAVEN"
maven_config {
version_policy = "%s"
}
}
`, repositoryID, versionPolicy)
}
20 changes: 20 additions & 0 deletions website/docs/r/artifact_registry_repository.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,30 @@ The following arguments are supported:
`projects/my-project/locations/my-region/keyRings/my-kr/cryptoKeys/my-key`.
This value may not be changed after the Repository has been created.

* `maven_config` -
(Optional)
MavenRepositoryConfig is maven related repository details.
Provides additional configuration details for repositories of the maven
format type.
Structure is [documented below](#nested_maven_config).

* `project` - (Optional) The ID of the project in which the resource belongs.
If it is not provided, the provider project is used.


<a name="nested_maven_config"></a>The `maven_config` block supports:

* `allow_snapshot_overwrites` -
(Optional)
The repository with this flag will allow publishing the same
snapshot versions.

* `version_policy` -
(Optional)
Version policy defines the versions that the registry will accept.
Default value is `VERSION_POLICY_UNSPECIFIED`.
Possible values are `VERSION_POLICY_UNSPECIFIED`, `RELEASE`, and `SNAPSHOT`.

## Attributes Reference

In addition to the arguments listed above, the following computed attributes are exported:
Expand Down

0 comments on commit a8e74c4

Please sign in to comment.