Skip to content

Commit a8e74c4

Browse files
modular-magicianYury Gridasovmelinath
authored
Artifact Registry: add version policy support for maven (#5294) (#4112)
* 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]>
1 parent 84e620b commit a8e74c4

File tree

4 files changed

+186
-0
lines changed

4 files changed

+186
-0
lines changed

.changelog/5294.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
artifactregistry: added maven config for `google_artifact_registry_repository`
3+
```

google-beta/resource_artifact_registry_repository.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,33 @@ and dashes.`,
9696
ForceNew: true,
9797
Description: `The name of the location this repository is located in.`,
9898
},
99+
"maven_config": {
100+
Type: schema.TypeList,
101+
Optional: true,
102+
Description: `MavenRepositoryConfig is maven related repository details.
103+
Provides additional configuration details for repositories of the maven
104+
format type.`,
105+
MaxItems: 1,
106+
Elem: &schema.Resource{
107+
Schema: map[string]*schema.Schema{
108+
"allow_snapshot_overwrites": {
109+
Type: schema.TypeBool,
110+
Optional: true,
111+
ForceNew: true,
112+
Description: `The repository with this flag will allow publishing the same
113+
snapshot versions.`,
114+
},
115+
"version_policy": {
116+
Type: schema.TypeString,
117+
Optional: true,
118+
ForceNew: true,
119+
ValidateFunc: validateEnum([]string{"VERSION_POLICY_UNSPECIFIED", "RELEASE", "SNAPSHOT", ""}),
120+
Description: `Version policy defines the versions that the registry will accept. Default value: "VERSION_POLICY_UNSPECIFIED" Possible values: ["VERSION_POLICY_UNSPECIFIED", "RELEASE", "SNAPSHOT"]`,
121+
Default: "VERSION_POLICY_UNSPECIFIED",
122+
},
123+
},
124+
},
125+
},
99126
"create_time": {
100127
Type: schema.TypeString,
101128
Computed: true,
@@ -155,6 +182,12 @@ func resourceArtifactRegistryRepositoryCreate(d *schema.ResourceData, meta inter
155182
} else if v, ok := d.GetOkExists("kms_key_name"); !isEmptyValue(reflect.ValueOf(kmsKeyNameProp)) && (ok || !reflect.DeepEqual(v, kmsKeyNameProp)) {
156183
obj["kmsKeyName"] = kmsKeyNameProp
157184
}
185+
mavenConfigProp, err := expandArtifactRegistryRepositoryMavenConfig(d.Get("maven_config"), d, config)
186+
if err != nil {
187+
return err
188+
} else if v, ok := d.GetOkExists("maven_config"); !isEmptyValue(reflect.ValueOf(mavenConfigProp)) && (ok || !reflect.DeepEqual(v, mavenConfigProp)) {
189+
obj["mavenConfig"] = mavenConfigProp
190+
}
158191

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

274310
return nil
275311
}
@@ -302,6 +338,12 @@ func resourceArtifactRegistryRepositoryUpdate(d *schema.ResourceData, meta inter
302338
} else if v, ok := d.GetOkExists("labels"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
303339
obj["labels"] = labelsProp
304340
}
341+
mavenConfigProp, err := expandArtifactRegistryRepositoryMavenConfig(d.Get("maven_config"), d, config)
342+
if err != nil {
343+
return err
344+
} else if v, ok := d.GetOkExists("maven_config"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, mavenConfigProp)) {
345+
obj["mavenConfig"] = mavenConfigProp
346+
}
305347

306348
url, err := replaceVars(d, config, "{{ArtifactRegistryBasePath}}projects/{{project}}/locations/{{location}}/repositories/{{repository_id}}")
307349
if err != nil {
@@ -318,6 +360,10 @@ func resourceArtifactRegistryRepositoryUpdate(d *schema.ResourceData, meta inter
318360
if d.HasChange("labels") {
319361
updateMask = append(updateMask, "labels")
320362
}
363+
364+
if d.HasChange("maven_config") {
365+
updateMask = append(updateMask, "mavenConfig")
366+
}
321367
// updateMask is a URL parameter but not present in the schema, so replaceVars
322368
// won't set it
323369
url, err = addQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")})
@@ -438,6 +484,29 @@ func flattenArtifactRegistryRepositoryUpdateTime(v interface{}, d *schema.Resour
438484
return v
439485
}
440486

487+
func flattenArtifactRegistryRepositoryMavenConfig(v interface{}, d *schema.ResourceData, config *Config) interface{} {
488+
if v == nil {
489+
return nil
490+
}
491+
original := v.(map[string]interface{})
492+
if len(original) == 0 {
493+
return nil
494+
}
495+
transformed := make(map[string]interface{})
496+
transformed["allow_snapshot_overwrites"] =
497+
flattenArtifactRegistryRepositoryMavenConfigAllowSnapshotOverwrites(original["allowSnapshotOverwrites"], d, config)
498+
transformed["version_policy"] =
499+
flattenArtifactRegistryRepositoryMavenConfigVersionPolicy(original["versionPolicy"], d, config)
500+
return []interface{}{transformed}
501+
}
502+
func flattenArtifactRegistryRepositoryMavenConfigAllowSnapshotOverwrites(v interface{}, d *schema.ResourceData, config *Config) interface{} {
503+
return v
504+
}
505+
506+
func flattenArtifactRegistryRepositoryMavenConfigVersionPolicy(v interface{}, d *schema.ResourceData, config *Config) interface{} {
507+
return v
508+
}
509+
441510
func expandArtifactRegistryRepositoryFormat(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
442511
return v, nil
443512
}
@@ -460,3 +529,37 @@ func expandArtifactRegistryRepositoryLabels(v interface{}, d TerraformResourceDa
460529
func expandArtifactRegistryRepositoryKmsKeyName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
461530
return v, nil
462531
}
532+
533+
func expandArtifactRegistryRepositoryMavenConfig(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
534+
l := v.([]interface{})
535+
if len(l) == 0 || l[0] == nil {
536+
return nil, nil
537+
}
538+
raw := l[0]
539+
original := raw.(map[string]interface{})
540+
transformed := make(map[string]interface{})
541+
542+
transformedAllowSnapshotOverwrites, err := expandArtifactRegistryRepositoryMavenConfigAllowSnapshotOverwrites(original["allow_snapshot_overwrites"], d, config)
543+
if err != nil {
544+
return nil, err
545+
} else if val := reflect.ValueOf(transformedAllowSnapshotOverwrites); val.IsValid() && !isEmptyValue(val) {
546+
transformed["allowSnapshotOverwrites"] = transformedAllowSnapshotOverwrites
547+
}
548+
549+
transformedVersionPolicy, err := expandArtifactRegistryRepositoryMavenConfigVersionPolicy(original["version_policy"], d, config)
550+
if err != nil {
551+
return nil, err
552+
} else if val := reflect.ValueOf(transformedVersionPolicy); val.IsValid() && !isEmptyValue(val) {
553+
transformed["versionPolicy"] = transformedVersionPolicy
554+
}
555+
556+
return transformed, nil
557+
}
558+
559+
func expandArtifactRegistryRepositoryMavenConfigAllowSnapshotOverwrites(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
560+
return v, nil
561+
}
562+
563+
func expandArtifactRegistryRepositoryMavenConfigVersionPolicy(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
564+
return v, nil
565+
}

google-beta/resource_artifact_registry_repository_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,50 @@ func TestAccArtifactRegistryRepository_update(t *testing.T) {
3737
})
3838
}
3939

40+
func TestAccArtifactRegistryRepository_create_mvn_snapshot(t *testing.T) {
41+
t.Parallel()
42+
43+
repositoryID := fmt.Sprintf("tf-test-%d", randInt(t))
44+
45+
vcrTest(t, resource.TestCase{
46+
PreCheck: func() { testAccPreCheck(t) },
47+
Providers: testAccProvidersOiCS,
48+
CheckDestroy: testAccCheckArtifactRegistryRepositoryDestroyProducer(t),
49+
Steps: []resource.TestStep{
50+
{
51+
Config: testAccArtifactRegistryRepository_create(repositoryID, "SNAPSHOT"),
52+
},
53+
{
54+
ResourceName: "google_artifact_registry_repository.test",
55+
ImportState: true,
56+
ImportStateVerify: true,
57+
},
58+
},
59+
})
60+
}
61+
62+
func TestAccArtifactRegistryRepository_create_mvn_release(t *testing.T) {
63+
t.Parallel()
64+
65+
repositoryID := fmt.Sprintf("tf-test-%d", randInt(t))
66+
67+
vcrTest(t, resource.TestCase{
68+
PreCheck: func() { testAccPreCheck(t) },
69+
Providers: testAccProvidersOiCS,
70+
CheckDestroy: testAccCheckArtifactRegistryRepositoryDestroyProducer(t),
71+
Steps: []resource.TestStep{
72+
{
73+
Config: testAccArtifactRegistryRepository_create(repositoryID, "RELEASE"),
74+
},
75+
{
76+
ResourceName: "google_artifact_registry_repository.test",
77+
ImportState: true,
78+
ImportStateVerify: true,
79+
},
80+
},
81+
})
82+
}
83+
4084
func testAccArtifactRegistryRepository_update(repositoryID string) string {
4185
return fmt.Sprintf(`
4286
resource "google_artifact_registry_repository" "test" {
@@ -72,3 +116,19 @@ resource "google_artifact_registry_repository" "test" {
72116
}
73117
`, repositoryID)
74118
}
119+
120+
func testAccArtifactRegistryRepository_create(repositoryID string, versionPolicy string) string {
121+
return fmt.Sprintf(`
122+
resource "google_artifact_registry_repository" "test" {
123+
provider = google-beta
124+
125+
repository_id = "%s"
126+
location = "us-central1"
127+
description = "post-update"
128+
format = "MAVEN"
129+
maven_config {
130+
version_policy = "%s"
131+
}
132+
}
133+
`, repositoryID, versionPolicy)
134+
}

website/docs/r/artifact_registry_repository.html.markdown

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,30 @@ The following arguments are supported:
154154
`projects/my-project/locations/my-region/keyRings/my-kr/cryptoKeys/my-key`.
155155
This value may not be changed after the Repository has been created.
156156

157+
* `maven_config` -
158+
(Optional)
159+
MavenRepositoryConfig is maven related repository details.
160+
Provides additional configuration details for repositories of the maven
161+
format type.
162+
Structure is [documented below](#nested_maven_config).
163+
157164
* `project` - (Optional) The ID of the project in which the resource belongs.
158165
If it is not provided, the provider project is used.
159166

160167

168+
<a name="nested_maven_config"></a>The `maven_config` block supports:
169+
170+
* `allow_snapshot_overwrites` -
171+
(Optional)
172+
The repository with this flag will allow publishing the same
173+
snapshot versions.
174+
175+
* `version_policy` -
176+
(Optional)
177+
Version policy defines the versions that the registry will accept.
178+
Default value is `VERSION_POLICY_UNSPECIFIED`.
179+
Possible values are `VERSION_POLICY_UNSPECIFIED`, `RELEASE`, and `SNAPSHOT`.
180+
161181
## Attributes Reference
162182

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

0 commit comments

Comments
 (0)