Skip to content

Commit dae9c80

Browse files
add primary key version (#9651) (#6782)
* add primary key version * Update mmv1/products/kms/CryptoKey.yaml * address PR comments * add resource attribute check * fix test * indexed value * fmt * 8th time's the charm --------- [upstream:79fbafca8d218b26313bd2bb4e353483a3bc5fe4] Signed-off-by: Modular Magician <[email protected]>
1 parent 944fc4c commit dae9c80

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

.changelog/9651.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
```release-note:enhancement
2+
Add output-only `primary` version attribute to `google_kms_crypto_key` resource
3+
4+
```

google-beta/services/kms/resource_kms_crypto_key.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,26 @@ See the [algorithm reference](https://cloud.google.com/kms/docs/reference/rest/v
160160
Description: `All of labels (key/value pairs) present on the resource in GCP, including the labels configured through Terraform, other clients and services.`,
161161
Elem: &schema.Schema{Type: schema.TypeString},
162162
},
163+
"primary": {
164+
Type: schema.TypeList,
165+
Computed: true,
166+
Description: `A copy of the primary CryptoKeyVersion that will be used by cryptoKeys.encrypt when this CryptoKey is given in EncryptRequest.name.
167+
Keys with purpose ENCRYPT_DECRYPT may have a primary. For other keys, this field will be unset.`,
168+
Elem: &schema.Resource{
169+
Schema: map[string]*schema.Schema{
170+
"name": {
171+
Type: schema.TypeString,
172+
Computed: true,
173+
Description: `The resource name for this CryptoKeyVersion.`,
174+
},
175+
"state": {
176+
Type: schema.TypeString,
177+
Computed: true,
178+
Description: `The current state of the CryptoKeyVersion.`,
179+
},
180+
},
181+
},
182+
},
163183
"terraform_labels": {
164184
Type: schema.TypeMap,
165185
Computed: true,
@@ -313,6 +333,9 @@ func resourceKMSCryptoKeyRead(d *schema.ResourceData, meta interface{}) error {
313333
if err := d.Set("labels", flattenKMSCryptoKeyLabels(res["labels"], d, config)); err != nil {
314334
return fmt.Errorf("Error reading CryptoKey: %s", err)
315335
}
336+
if err := d.Set("primary", flattenKMSCryptoKeyPrimary(res["primary"], d, config)); err != nil {
337+
return fmt.Errorf("Error reading CryptoKey: %s", err)
338+
}
316339
if err := d.Set("purpose", flattenKMSCryptoKeyPurpose(res["purpose"], d, config)); err != nil {
317340
return fmt.Errorf("Error reading CryptoKey: %s", err)
318341
}
@@ -508,6 +531,29 @@ func flattenKMSCryptoKeyLabels(v interface{}, d *schema.ResourceData, config *tr
508531
return transformed
509532
}
510533

534+
func flattenKMSCryptoKeyPrimary(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
535+
if v == nil {
536+
return nil
537+
}
538+
original := v.(map[string]interface{})
539+
if len(original) == 0 {
540+
return nil
541+
}
542+
transformed := make(map[string]interface{})
543+
transformed["name"] =
544+
flattenKMSCryptoKeyPrimaryName(original["name"], d, config)
545+
transformed["state"] =
546+
flattenKMSCryptoKeyPrimaryState(original["state"], d, config)
547+
return []interface{}{transformed}
548+
}
549+
func flattenKMSCryptoKeyPrimaryName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
550+
return v
551+
}
552+
553+
func flattenKMSCryptoKeyPrimaryState(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
554+
return v
555+
}
556+
511557
func flattenKMSCryptoKeyPurpose(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
512558
return v
513559
}

google-beta/services/kms/resource_kms_crypto_key_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ func TestAccKmsCryptoKey_basic(t *testing.T) {
152152
Steps: []resource.TestStep{
153153
{
154154
Config: testGoogleKmsCryptoKey_basic(projectId, projectOrg, projectBillingAccount, keyRingName, cryptoKeyName),
155+
Check: resource.ComposeTestCheckFunc(
156+
resource.TestCheckResourceAttrSet("google_kms_crypto_key.crypto_key", "primary.0.name"),
157+
),
155158
},
156159
{
157160
ResourceName: "google_kms_crypto_key.crypto_key",

website/docs/r/kms_crypto_key.html.markdown

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ In addition to the arguments listed above, the following computed attributes are
157157

158158
* `id` - an identifier for the resource with format `{{key_ring}}/cryptoKeys/{{name}}`
159159

160+
* `primary` -
161+
A copy of the primary CryptoKeyVersion that will be used by cryptoKeys.encrypt when this CryptoKey is given in EncryptRequest.name.
162+
Keys with purpose ENCRYPT_DECRYPT may have a primary. For other keys, this field will be unset.
163+
Structure is [documented below](#nested_primary).
164+
160165
* `terraform_labels` -
161166
The combination of labels configured directly on the resource
162167
and default labels configured on the provider.
@@ -165,6 +170,16 @@ In addition to the arguments listed above, the following computed attributes are
165170
All of labels (key/value pairs) present on the resource in GCP, including the labels configured through Terraform, other clients and services.
166171

167172

173+
<a name="nested_primary"></a>The `primary` block contains:
174+
175+
* `name` -
176+
(Output)
177+
The resource name for this CryptoKeyVersion.
178+
179+
* `state` -
180+
(Output)
181+
The current state of the CryptoKeyVersion.
182+
168183
## Timeouts
169184

170185
This resource provides the following

0 commit comments

Comments
 (0)