Skip to content

Commit 3344e6f

Browse files
author
l50052434
committed
add a datasource to query the tag list of KMS
1 parent 77620bd commit 3344e6f

File tree

4 files changed

+184
-0
lines changed

4 files changed

+184
-0
lines changed

docs/data-sources/kms_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_tags"
5+
description: |-
6+
Use this data source to query the tag list of KMS within HuaweiCloud.
7+
---
8+
9+
# huaweicloud_kms_tags
10+
11+
Use this data source to query the tag list of KMS within HuaweiCloud.
12+
13+
## Example Usage
14+
15+
```hcl
16+
data "huaweicloud_kms_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 the 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 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_tags": dew.DataSourceKmsTags(),
11781179
"huaweicloud_kps_failed_tasks": dew.DataSourceDewKpsFailedTasks(),
11791180
"huaweicloud_kps_running_tasks": dew.DataSourceDewKpsRunningTasks(),
11801181
"huaweicloud_kps_keypairs": dew.DataSourceKeypairs(),
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package dew
2+
3+
import (
4+
"regexp"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
9+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
10+
)
11+
12+
func TestAccDataSourceKmsTags_basic(t *testing.T) {
13+
var (
14+
all = "data.huaweicloud_kms_tags.test"
15+
dc = acceptance.InitDataSourceCheck(all)
16+
)
17+
18+
resource.ParallelTest(t, resource.TestCase{
19+
PreCheck: func() {
20+
acceptance.TestAccPreCheck(t)
21+
},
22+
ProviderFactories: acceptance.TestAccProviderFactories,
23+
Steps: []resource.TestStep{
24+
{
25+
Config: testAccDataSourceKmsTags_basic(),
26+
Check: resource.ComposeTestCheckFunc(
27+
dc.CheckResourceExists(),
28+
resource.TestMatchResourceAttr(all, "tags.#", regexp.MustCompile(`^[1-9]([0-9]*)?$`)),
29+
resource.TestCheckResourceAttrSet(all, "tags.0.key"),
30+
resource.TestMatchResourceAttr(all, "tags.0.values.#", regexp.MustCompile(`^[1-9]([0-9]*)?$`)),
31+
),
32+
},
33+
},
34+
})
35+
}
36+
37+
func testAccDataSourceKmsTags_basic() string {
38+
return (`
39+
data "huaweicloud_kms_tags" "test" {}
40+
`)
41+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 DataSourceKmsTags() *schema.Resource {
20+
return &schema.Resource{
21+
ReadContext: dataSourceKmsTagsRead,
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+
31+
"tags": {
32+
Type: schema.TypeList,
33+
Computed: true,
34+
Description: `The list of the tags.`,
35+
Elem: tagsSchema(),
36+
},
37+
},
38+
}
39+
}
40+
41+
func dataSourceKmsTagsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
42+
var (
43+
cfg = meta.(*config.Config)
44+
region = cfg.GetRegion(d)
45+
mErr *multierror.Error
46+
getHttpUrl = "v1.0/{project_id}/kms/tags"
47+
product = "kms"
48+
)
49+
50+
client, err := cfg.NewServiceClient(product, region)
51+
if err != nil {
52+
return diag.Errorf("error creating KMS client: %s", err)
53+
}
54+
55+
getPath := client.Endpoint + getHttpUrl
56+
getPath = strings.ReplaceAll(getPath, "{project_id}", client.ProjectID)
57+
getOpt := golangsdk.RequestOpts{
58+
KeepResponseBody: true,
59+
}
60+
61+
getResp, err := client.Request("GET", getPath, &getOpt)
62+
if err != nil {
63+
return diag.Errorf("error retrieving KMS tags: %s", err)
64+
}
65+
66+
getRespBody, err := utils.FlattenResponse(getResp)
67+
if err != nil {
68+
return diag.FromErr(err)
69+
}
70+
71+
id, err := uuid.GenerateUUID()
72+
if err != nil {
73+
return diag.FromErr(err)
74+
}
75+
d.SetId(id)
76+
77+
taglists := utils.PathSearch("tags", getRespBody, make([]interface{}, 0)).([]interface{})
78+
79+
mErr = multierror.Append(
80+
mErr,
81+
d.Set("region", region),
82+
d.Set("tags", flattenProjectTags(taglists)),
83+
)
84+
85+
return diag.FromErr(mErr.ErrorOrNil())
86+
}
87+
88+
func flattenProjectTags(tags []interface{}) []map[string]interface{} {
89+
if len(tags) < 1 {
90+
return nil
91+
}
92+
93+
result := make([]map[string]interface{}, 0, len(tags))
94+
for _, tag := range tags {
95+
result = append(result, map[string]interface{}{
96+
"key": utils.PathSearch("key", tag, nil),
97+
"values": utils.PathSearch("values", tag, make([]interface{}, 0)),
98+
})
99+
}
100+
101+
return result
102+
}

0 commit comments

Comments
 (0)