Skip to content

Commit 560b822

Browse files
l50052434liquid-liquid
authored andcommitted
add a datasource to query the tag list of KMS
1 parent 77620bd commit 560b822

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed

docs/data-sources/kms_key_tags.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
subcategory: "Data Encryption Workshop (DEW)"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_kms_key_tags"
5+
description: |-
6+
Use this data source to query the list of KMS key tags within HuaweiCloud.
7+
---
8+
9+
# huaweicloud_kms_key_tags
10+
11+
Use this data source to query the list of KMS key tags within HuaweiCloud.
12+
13+
## Example Usage
14+
15+
```hcl
16+
data "huaweicloud_kms_key_tags" "test" {}
17+
```
18+
19+
## Argument Reference
20+
21+
The following arguments are supported:
22+
23+
* `region` - (Optional, String) Specifies the region in which to query the resource.
24+
If omitted, the provider-level region will be used.
25+
26+
## Attribute Reference
27+
28+
In addition to all arguments above, the following attributes are exported:
29+
30+
* `id` - The data source ID.
31+
32+
* `tags` - the list of key tags.
33+
The [tags](#kms_project_tags) structure is documented below.
34+
35+
<a name="kms_project_tags"></a>
36+
The `tags` block supports:
37+
38+
* `key` - The tag key.
39+
40+
* `values` - The list of tag values.

huaweicloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,7 @@ func Provider() *schema.Provider {
11751175
"huaweicloud_kms_public_key": dew.DataSourceKmsPublicKey(),
11761176
"huaweicloud_kms_custom_keys_by_tags": dew.DataSourceKmsCustomKeysByTags(),
11771177
"huaweicloud_kms_parameters_for_import": dew.DataSourceKmsParametersForImport(),
1178+
"huaweicloud_kms_key_tags": dew.DataSourceKmsKeyTags(),
11781179
"huaweicloud_kps_failed_tasks": dew.DataSourceDewKpsFailedTasks(),
11791180
"huaweicloud_kps_running_tasks": dew.DataSourceDewKpsRunningTasks(),
11801181
"huaweicloud_kps_keypairs": dew.DataSourceKeypairs(),
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package dew
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
8+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
9+
)
10+
11+
func TestAccDataSourceKmsKeyTags_basic(t *testing.T) {
12+
var (
13+
dataSourcename = "data.huaweicloud_kms_key_tags.test"
14+
dc = acceptance.InitDataSourceCheck(dataSourcename)
15+
)
16+
17+
resource.ParallelTest(t, resource.TestCase{
18+
PreCheck: func() {
19+
acceptance.TestAccPreCheck(t)
20+
// Please prepare a import KMS key ID with tag and config it to the environment variable.
21+
acceptance.TestAccPreCheckKmsKeyID(t)
22+
},
23+
ProviderFactories: acceptance.TestAccProviderFactories,
24+
Steps: []resource.TestStep{
25+
{
26+
Config: testAccDataSourceKmsKeyTags_basic,
27+
Check: resource.ComposeTestCheckFunc(
28+
dc.CheckResourceExists(),
29+
resource.TestCheckResourceAttrSet(dataSourcename, "tags.#"),
30+
resource.TestCheckResourceAttrSet(dataSourcename, "tags.0.key"),
31+
resource.TestCheckResourceAttrSet(dataSourcename, "tags.0.values.#"),
32+
),
33+
},
34+
},
35+
})
36+
}
37+
38+
const testAccDataSourceKmsKeyTags_basic = `data "huaweicloud_kms_key_tags" "test" {}`
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package dew
2+
3+
import (
4+
"context"
5+
"strings"
6+
7+
"github.com/hashicorp/go-multierror"
8+
"github.com/hashicorp/go-uuid"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
12+
"github.com/chnsz/golangsdk"
13+
14+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
15+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
16+
)
17+
18+
// @API DEW GET /v1.0/{project_id}/kms/tags
19+
func DataSourceKmsKeyTags() *schema.Resource {
20+
return &schema.Resource{
21+
ReadContext: dataSourceKmsKeyTagsRead,
22+
23+
Schema: map[string]*schema.Schema{
24+
"region": {
25+
Type: schema.TypeString,
26+
Optional: true,
27+
Computed: true,
28+
Description: `Specifies the region in which to query the resource.`,
29+
},
30+
"tags": {
31+
Type: schema.TypeList,
32+
Computed: true,
33+
Description: `The list of the key tags.`,
34+
Elem: tagSchema(),
35+
},
36+
},
37+
}
38+
}
39+
40+
func tagSchema() *schema.Resource {
41+
return &schema.Resource{
42+
Schema: map[string]*schema.Schema{
43+
"key": {
44+
Type: schema.TypeString,
45+
Optional: true,
46+
Description: "Specifies the tag key.",
47+
},
48+
"values": {
49+
Type: schema.TypeList,
50+
Optional: true,
51+
Elem: &schema.Schema{Type: schema.TypeString},
52+
Description: "Specifies the tag value set.",
53+
},
54+
},
55+
}
56+
}
57+
58+
func dataSourceKmsKeyTagsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
59+
var (
60+
cfg = meta.(*config.Config)
61+
region = cfg.GetRegion(d)
62+
mErr *multierror.Error
63+
getHttpUrl = "v1.0/{project_id}/kms/tags"
64+
product = "kms"
65+
)
66+
67+
client, err := cfg.NewServiceClient(product, region)
68+
if err != nil {
69+
return diag.Errorf("error creating KMS client: %s", err)
70+
}
71+
72+
getPath := client.Endpoint + getHttpUrl
73+
getPath = strings.ReplaceAll(getPath, "{project_id}", client.ProjectID)
74+
getOpt := golangsdk.RequestOpts{
75+
KeepResponseBody: true,
76+
}
77+
78+
getResp, err := client.Request("GET", getPath, &getOpt)
79+
if err != nil {
80+
return diag.Errorf("error retrieving KMS key tags: %s", err)
81+
}
82+
83+
getRespBody, err := utils.FlattenResponse(getResp)
84+
if err != nil {
85+
return diag.FromErr(err)
86+
}
87+
88+
id, err := uuid.GenerateUUID()
89+
if err != nil {
90+
return diag.FromErr(err)
91+
}
92+
d.SetId(id)
93+
94+
tagsList := utils.PathSearch("tags", getRespBody, make([]interface{}, 0)).([]interface{})
95+
96+
mErr = multierror.Append(
97+
mErr,
98+
d.Set("region", region),
99+
d.Set("tags", flattenProjectTags(tagsList)),
100+
)
101+
102+
return diag.FromErr(mErr.ErrorOrNil())
103+
}
104+
105+
func flattenProjectTags(tags []interface{}) []map[string]interface{} {
106+
if len(tags) < 1 {
107+
return nil
108+
}
109+
110+
result := make([]map[string]interface{}, 0, len(tags))
111+
for _, tag := range tags {
112+
result = append(result, map[string]interface{}{
113+
"key": utils.PathSearch("key", tag, nil),
114+
"values": utils.PathSearch("values", tag, make([]interface{}, 0)),
115+
})
116+
}
117+
118+
return result
119+
}

0 commit comments

Comments
 (0)