Skip to content

Commit 28d4d01

Browse files
Add kms key field to Artifact Registry repository. (#3658) (#2254)
Co-authored-by: Dana Hoffman <[email protected]> Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Dana Hoffman <[email protected]>
1 parent ce0799a commit 28d4d01

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

.changelog/3658.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 field `kms_key_name` to `google_artifact_registry_repository`
3+
```

google-beta/resource_artifact_registry_repository.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ func resourceArtifactRegistryRepository() *schema.Resource {
6161
Optional: true,
6262
Description: `The user-provided description of the repository.`,
6363
},
64+
"kms_key_name": {
65+
Type: schema.TypeString,
66+
Optional: true,
67+
ForceNew: true,
68+
Description: `The Cloud KMS resource name of the customer managed encryption key that’s
69+
used to encrypt the contents of the Repository. Has the form:
70+
'projects/my-project/locations/my-region/keyRings/my-kr/cryptoKeys/my-key'.
71+
This value may not be changed after the Repository has been created.`,
72+
},
6473
"labels": {
6574
Type: schema.TypeMap,
6675
Optional: true,
@@ -126,6 +135,12 @@ func resourceArtifactRegistryRepositoryCreate(d *schema.ResourceData, meta inter
126135
} else if v, ok := d.GetOkExists("labels"); !isEmptyValue(reflect.ValueOf(labelsProp)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
127136
obj["labels"] = labelsProp
128137
}
138+
kmsKeyNameProp, err := expandArtifactRegistryRepositoryKmsKeyName(d.Get("kms_key_name"), d, config)
139+
if err != nil {
140+
return err
141+
} else if v, ok := d.GetOkExists("kms_key_name"); !isEmptyValue(reflect.ValueOf(kmsKeyNameProp)) && (ok || !reflect.DeepEqual(v, kmsKeyNameProp)) {
142+
obj["kmsKeyName"] = kmsKeyNameProp
143+
}
129144

130145
url, err := replaceVars(d, config, "{{ArtifactRegistryBasePath}}projects/{{project}}/locations/{{location}}/repositories?repository_id={{repository_id}}")
131146
if err != nil {
@@ -210,6 +225,9 @@ func resourceArtifactRegistryRepositoryRead(d *schema.ResourceData, meta interfa
210225
if err := d.Set("labels", flattenArtifactRegistryRepositoryLabels(res["labels"], d, config)); err != nil {
211226
return fmt.Errorf("Error reading Repository: %s", err)
212227
}
228+
if err := d.Set("kms_key_name", flattenArtifactRegistryRepositoryKmsKeyName(res["kmsKeyName"], d, config)); err != nil {
229+
return fmt.Errorf("Error reading Repository: %s", err)
230+
}
213231
if err := d.Set("create_time", flattenArtifactRegistryRepositoryCreateTime(res["createTime"], d, config)); err != nil {
214232
return fmt.Errorf("Error reading Repository: %s", err)
215233
}
@@ -247,6 +265,12 @@ func resourceArtifactRegistryRepositoryUpdate(d *schema.ResourceData, meta inter
247265
} else if v, ok := d.GetOkExists("labels"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
248266
obj["labels"] = labelsProp
249267
}
268+
kmsKeyNameProp, err := expandArtifactRegistryRepositoryKmsKeyName(d.Get("kms_key_name"), d, config)
269+
if err != nil {
270+
return err
271+
} else if v, ok := d.GetOkExists("kms_key_name"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, kmsKeyNameProp)) {
272+
obj["kmsKeyName"] = kmsKeyNameProp
273+
}
250274

251275
url, err := replaceVars(d, config, "{{ArtifactRegistryBasePath}}projects/{{project}}/locations/{{location}}/repositories/{{name}}")
252276
if err != nil {
@@ -343,6 +367,10 @@ func flattenArtifactRegistryRepositoryLabels(v interface{}, d *schema.ResourceDa
343367
return v
344368
}
345369

370+
func flattenArtifactRegistryRepositoryKmsKeyName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
371+
return v
372+
}
373+
346374
func flattenArtifactRegistryRepositoryCreateTime(v interface{}, d *schema.ResourceData, config *Config) interface{} {
347375
return v
348376
}
@@ -369,3 +397,7 @@ func expandArtifactRegistryRepositoryLabels(v interface{}, d TerraformResourceDa
369397
}
370398
return m, nil
371399
}
400+
401+
func expandArtifactRegistryRepositoryKmsKeyName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
402+
return v, nil
403+
}

google-beta/resource_artifact_registry_repository_generated_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,40 @@ resource "google_artifact_registry_repository" "my-repo" {
5555
`, context)
5656
}
5757

58+
func TestAccArtifactRegistryRepository_artifactRegistryRepositoryCmekExample(t *testing.T) {
59+
t.Parallel()
60+
61+
context := map[string]interface{}{
62+
"kms_key_name": BootstrapKMSKeyInLocation(t, "us-central1").CryptoKey.Name,
63+
"random_suffix": randString(t, 10),
64+
}
65+
66+
vcrTest(t, resource.TestCase{
67+
PreCheck: func() { testAccPreCheck(t) },
68+
Providers: testAccProvidersOiCS,
69+
CheckDestroy: testAccCheckArtifactRegistryRepositoryDestroyProducer(t),
70+
Steps: []resource.TestStep{
71+
{
72+
Config: testAccArtifactRegistryRepository_artifactRegistryRepositoryCmekExample(context),
73+
},
74+
},
75+
})
76+
}
77+
78+
func testAccArtifactRegistryRepository_artifactRegistryRepositoryCmekExample(context map[string]interface{}) string {
79+
return Nprintf(`
80+
resource "google_artifact_registry_repository" "my-repo" {
81+
provider = google-beta
82+
83+
location = "us-central1"
84+
repository_id = "tf-test-my-repository%{random_suffix}"
85+
description = "example docker repository with cmek"
86+
format = "DOCKER"
87+
kms_key_name = "%{kms_key_name}"
88+
}
89+
`, context)
90+
}
91+
5892
func TestAccArtifactRegistryRepository_artifactRegistryRepositoryIamExample(t *testing.T) {
5993
t.Parallel()
6094

website/docs/r/artifact_registry_repository.html.markdown

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,25 @@ resource "google_artifact_registry_repository" "my-repo" {
5151
format = "DOCKER"
5252
}
5353
```
54+
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
55+
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=artifact_registry_repository_cmek&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
56+
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
57+
</a>
58+
</div>
59+
## Example Usage - Artifact Registry Repository Cmek
60+
61+
62+
```hcl
63+
resource "google_artifact_registry_repository" "my-repo" {
64+
provider = google-beta
65+
66+
location = "us-central1"
67+
repository_id = "my-repository"
68+
description = "example docker repository with cmek"
69+
format = "DOCKER"
70+
kms_key_name = "kms-key"
71+
}
72+
```
5473
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
5574
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=artifact_registry_repository_iam&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
5675
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
@@ -123,6 +142,13 @@ The following arguments are supported:
123142
and may only contain lowercase letters, numeric characters, underscores,
124143
and dashes.
125144

145+
* `kms_key_name` -
146+
(Optional)
147+
The Cloud KMS resource name of the customer managed encryption key that’s
148+
used to encrypt the contents of the Repository. Has the form:
149+
`projects/my-project/locations/my-region/keyRings/my-kr/cryptoKeys/my-key`.
150+
This value may not be changed after the Repository has been created.
151+
126152
* `project` - (Optional) The ID of the project in which the resource belongs.
127153
If it is not provided, the provider project is used.
128154

0 commit comments

Comments
 (0)