Skip to content

Commit 03d2ddc

Browse files
adding datasource for autokeyconfig (#12611) (#8986)
[upstream:9ca4299ff15c8f81506bbed2696bd30919c440db] Signed-off-by: Modular Magician <[email protected]>
1 parent cf20722 commit 03d2ddc

File tree

7 files changed

+134
-1
lines changed

7 files changed

+134
-1
lines changed

.changelog/12611.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-datasource
2+
`google_kms_autokey_config`
3+
```

google-beta/acctest/bootstrap_test_utils.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func BootstrapKMSKeyWithPurposeInLocation(t *testing.T, purpose, locationID stri
8585
}
8686

8787
type BootstrappedKMSAutokey struct {
88+
*cloudkms.AutokeyConfig
8889
*cloudkms.KeyHandle
8990
}
9091

@@ -96,6 +97,7 @@ func BootstrapKMSAutokeyKeyHandleWithLocation(t *testing.T, locationID string) B
9697
config := BootstrapConfig(t)
9798
if config == nil {
9899
return BootstrappedKMSAutokey{
100+
&cloudkms.AutokeyConfig{},
99101
&cloudkms.KeyHandle{},
100102
}
101103
}
@@ -105,7 +107,7 @@ func BootstrapKMSAutokeyKeyHandleWithLocation(t *testing.T, locationID string) B
105107
// Enable autokey on autokey test folder
106108
kmsClient := config.NewKmsClient(config.UserAgent)
107109
autokeyConfigID := fmt.Sprintf("%s/autokeyConfig", autokeyFolder.Name)
108-
_, err := kmsClient.Folders.UpdateAutokeyConfig(autokeyConfigID, &cloudkms.AutokeyConfig{
110+
autokeyConfig, err := kmsClient.Folders.UpdateAutokeyConfig(autokeyConfigID, &cloudkms.AutokeyConfig{
109111
KeyProject: fmt.Sprintf("projects/%s", kmsProject.ProjectId),
110112
}).UpdateMask("keyProject").Do()
111113
if err != nil {
@@ -154,6 +156,7 @@ func BootstrapKMSAutokeyKeyHandleWithLocation(t *testing.T, locationID string) B
154156
}
155157

156158
return BootstrappedKMSAutokey{
159+
autokeyConfig,
157160
keyHandle,
158161
}
159162
}

google-beta/provider/provider_mmv1_resources.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
284284
"google_kms_key_ring": kms.DataSourceGoogleKmsKeyRing(),
285285
"google_kms_key_rings": kms.DataSourceGoogleKmsKeyRings(),
286286
"google_kms_key_handle": kms.DataSourceGoogleKmsKeyHandle(),
287+
"google_kms_autokey_config": kms.DataSourceGoogleKmsAutokeyConfig(),
287288
"google_kms_secret": kms.DataSourceGoogleKmsSecret(),
288289
"google_kms_secret_ciphertext": kms.DataSourceGoogleKmsSecretCiphertext(),
289290
"google_kms_secret_asymmetric": kms.DataSourceGoogleKmsSecretAsymmetric(),
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package kms
4+
5+
import (
6+
"fmt"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/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+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package kms_test
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-beta/google-beta/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+
}

google-beta/services/kms/kms_utils.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ type KmsKeyHandleId struct {
2828
Name string
2929
}
3030

31+
type KmsAutokeyConfigId struct {
32+
Folder string
33+
}
34+
35+
func (s *KmsAutokeyConfigId) AutokeyConfigId() string {
36+
return fmt.Sprintf("%s/autokeyConfig", s.Folder)
37+
}
38+
3139
func (s *KmsKeyHandleId) KeyHandleId() string {
3240
return fmt.Sprintf("projects/%s/locations/%s/keyHandles/%s", s.Project, s.Location, s.Name)
3341
}
Lines changed: 42 additions & 0 deletions
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)