Skip to content

Commit 9ca4299

Browse files
authored
adding datasource for autokeyconfig (#12611)
1 parent 44c6e75 commit 9ca4299

6 files changed

+134
-1
lines changed

mmv1/third_party/terraform/acctest/bootstrap_test_utils.go.tmpl

+4-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func BootstrapKMSKeyWithPurposeInLocation(t *testing.T, purpose, locationID stri
9393
{{ if ne $.TargetVersionName `ga` -}}
9494

9595
type BootstrappedKMSAutokey struct {
96+
*cloudkms.AutokeyConfig
9697
*cloudkms.KeyHandle
9798
}
9899

@@ -104,6 +105,7 @@ func BootstrapKMSAutokeyKeyHandleWithLocation(t *testing.T, locationID string) B
104105
config := BootstrapConfig(t)
105106
if config == nil {
106107
return BootstrappedKMSAutokey{
108+
&cloudkms.AutokeyConfig{},
107109
&cloudkms.KeyHandle{},
108110
}
109111
}
@@ -113,7 +115,7 @@ func BootstrapKMSAutokeyKeyHandleWithLocation(t *testing.T, locationID string) B
113115
// Enable autokey on autokey test folder
114116
kmsClient := config.NewKmsClient(config.UserAgent)
115117
autokeyConfigID := fmt.Sprintf("%s/autokeyConfig", autokeyFolder.Name)
116-
_, err := kmsClient.Folders.UpdateAutokeyConfig(autokeyConfigID, &cloudkms.AutokeyConfig{
118+
autokeyConfig, err := kmsClient.Folders.UpdateAutokeyConfig(autokeyConfigID, &cloudkms.AutokeyConfig{
117119
KeyProject: fmt.Sprintf("projects/%s", kmsProject.ProjectId),
118120
}).UpdateMask("keyProject").Do()
119121
if err != nil {
@@ -162,6 +164,7 @@ func BootstrapKMSAutokeyKeyHandleWithLocation(t *testing.T, locationID string) B
162164
}
163165

164166
return BootstrappedKMSAutokey{
167+
autokeyConfig,
165168
keyHandle,
166169
}
167170
}

mmv1/third_party/terraform/provider/provider_mmv1_resources.go.tmpl

+1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
149149
"google_kms_key_rings": kms.DataSourceGoogleKmsKeyRings(),
150150
{{- if ne $.TargetVersionName "ga" }}
151151
"google_kms_key_handle": kms.DataSourceGoogleKmsKeyHandle(),
152+
"google_kms_autokey_config": kms.DataSourceGoogleKmsAutokeyConfig(),
152153
{{- end }}
153154
"google_kms_secret": kms.DataSourceGoogleKmsSecret(),
154155
"google_kms_secret_ciphertext": kms.DataSourceGoogleKmsSecretCiphertext(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package kms
2+
3+
{{ if ne $.TargetVersionName `ga` -}}
4+
5+
import (
6+
"fmt"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
10+
)
11+
12+
func DataSourceGoogleKmsAutokeyConfig() *schema.Resource {
13+
dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourceKMSAutokeyConfig().Schema)
14+
tpgresource.AddRequiredFieldsToSchema(dsSchema, "folder")
15+
16+
return &schema.Resource{
17+
Read: dataSourceGoogleKmsAutokeyConfigRead,
18+
Schema: dsSchema,
19+
}
20+
21+
}
22+
23+
func dataSourceGoogleKmsAutokeyConfigRead(d *schema.ResourceData, meta interface{}) error {
24+
configId := KmsAutokeyConfigId{
25+
Folder: d.Get("folder").(string),
26+
}
27+
id := configId.AutokeyConfigId()
28+
d.SetId(id)
29+
err := resourceKMSAutokeyConfigRead(d, meta)
30+
if err != nil {
31+
return err
32+
}
33+
34+
if d.Id() == "" {
35+
return fmt.Errorf("%s not found", id)
36+
}
37+
return nil
38+
}
39+
40+
{{ end }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package kms_test
2+
3+
{{ if ne $.TargetVersionName `ga` -}}
4+
5+
import (
6+
"fmt"
7+
"regexp"
8+
"strings"
9+
"testing"
10+
11+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
12+
"github.com/hashicorp/terraform-provider-google/google/acctest"
13+
)
14+
15+
func TestAccDataSourceGoogleKmsAutokeyConfig_basic(t *testing.T) {
16+
kmsAutokey := acctest.BootstrapKMSAutokeyKeyHandle(t)
17+
folder := fmt.Sprintf("folders/%s", strings.Split(kmsAutokey.AutokeyConfig.Name, "/")[1])
18+
19+
acctest.VcrTest(t, resource.TestCase{
20+
PreCheck: func() { acctest.AccTestPreCheck(t) },
21+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
22+
Steps: []resource.TestStep{
23+
{
24+
Config: testAccDataSourceGoogleKmsAutokeyConfig_basic(folder),
25+
Check: resource.TestMatchResourceAttr("data.google_kms_autokey_config.kms_autokey_config", "id", regexp.MustCompile(kmsAutokey.AutokeyConfig.Name)),
26+
},
27+
},
28+
})
29+
}
30+
31+
func testAccDataSourceGoogleKmsAutokeyConfig_basic(folder string) string {
32+
33+
return fmt.Sprintf(`
34+
data "google_kms_autokey_config" "kms_autokey_config" {
35+
folder = "%s"
36+
}
37+
`, folder)
38+
}
39+
{{ end }}

mmv1/third_party/terraform/services/kms/kms_utils.go

+8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ type KmsKeyHandleId struct {
2626
Name string
2727
}
2828

29+
type KmsAutokeyConfigId struct {
30+
Folder string
31+
}
32+
33+
func (s *KmsAutokeyConfigId) AutokeyConfigId() string {
34+
return fmt.Sprintf("%s/autokeyConfig", s.Folder)
35+
}
36+
2937
func (s *KmsKeyHandleId) KeyHandleId() string {
3038
return fmt.Sprintf("projects/%s/locations/%s/keyHandles/%s", s.Project, s.Location, s.Name)
3139
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
subcategory: "Cloud Key Management Service"
3+
description: |-
4+
Provides access to KMS Autokey config data with Google Cloud KMS.
5+
---
6+
7+
# google_kms_key_handle
8+
9+
Provides access to Google Cloud Platform KMS AutokeyConfig. A AutokeyConfig is a Cloud KMS resource that helps you safely span the separation of duties to create new Cloud KMS keys for CMEK using Autokey.
10+
11+
~> **Warning:** This resource is in beta, and should be used with the terraform-provider-google-beta provider.
12+
See [Provider Versions](https://terraform.io/docs/providers/google/guides/provider_versions.html) for more details on beta resources.
13+
14+
15+
For more information see
16+
[the official documentation](https://cloud.google.com/kms/docs/reference/rest/v1/folders)
17+
and
18+
[API](https://cloud.google.com/kms/docs/reference/rest/v1/projects.locations.keyHandles).
19+
20+
## Example Usage
21+
22+
```hcl
23+
data "google_kms_autokey_config" "my_autokey_config" {
24+
folder = "folders/123"
25+
}
26+
```
27+
28+
## Argument Reference
29+
30+
The following arguments are supported:
31+
32+
* `folder` - The folder in which the AutokeyConfig is configured. If it
33+
is not provided, the provider folder is used.
34+
35+
## Attributes Reference
36+
37+
In addition to the arguments listed above, the following computed attributes are
38+
exported:
39+
40+
* `id` - The identifier of the AutokeyConfig. Its format is `folders/{folderId}/autokeyConfig`.
41+
42+
* `key_project` - The identifier of the project hosting KMS KeyRings and Keys generated by Autokey. Its format is `projects/{projectId}`.

0 commit comments

Comments
 (0)