Skip to content

Commit b90ed2c

Browse files
authored
feat(dew): add a data source to get csms secrets by tags. (#6859)
1 parent eebb2dd commit b90ed2c

File tree

4 files changed

+785
-0
lines changed

4 files changed

+785
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
---
2+
subcategory: "Data Encryption Workshop (DEW)"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_csms_secrets_by_tags"
5+
description: |-
6+
Use this data source to get a list of the secrets by tags.
7+
---
8+
9+
# huaweicloud_csms_secret_versions
10+
11+
Use this data source to get a list of the secrets by tags.
12+
13+
## Example Usage
14+
15+
```hcl
16+
variable "resource_instances" {}
17+
variable "action" {}
18+
19+
data "huaweicloud_csms_secrets_by_tags" "test" {
20+
resource_instances = var.resource_instances
21+
action = var.action
22+
}
23+
```
24+
25+
## Argument Reference
26+
27+
The following arguments are supported:
28+
29+
* `region` - (Optional, String) Specifies the region in which to query the resource.
30+
If omitted, the provider-level region will be used.
31+
32+
* `resource_instances` - (Required, String) Specifies the resource instances, the valid value is **resource_instances**.
33+
34+
* `action` - (Required, String) Specifies the operation type. The valid values are as follows:
35+
+ **filter**: Indicates filtering secrets.
36+
+ **count**: Indicates the total number of secrets.
37+
38+
* `tags` - (Optional, List) Specifies the list of tags, the maximum of tags is `10`.
39+
The [tags](#tags_struct) structure is documented below.
40+
41+
* `matches` - (Optional, List) Specifies the key-value pair to be matched.
42+
The [matches](#matches_struct) structure is documented below.
43+
44+
* `sequence` - (Optional, String) Specifies the `36` byte sequence number of a request message.
45+
46+
<a name="tags_struct"></a>
47+
The `tags` block supports:
48+
49+
* `key` - (Optional, String) Specifies the tag key.
50+
51+
* `values` - (Optional, List) Specifies the set of tag values, the maximum of values is `10`.
52+
If the tag list is empty, any value can be matched.
53+
A search result matches only one value.
54+
55+
<a name="matches_struct"></a>
56+
The `matches` block supports:
57+
58+
* `key` - (Optional, String) Specifies the search field, the valid value is **resource_name**.
59+
60+
* `value` - (Optional, String) Specifies the field for fuzzy match, maximum of `255` characters are allowed.
61+
If it is left blank, a null value is returned.
62+
63+
## Attribute Reference
64+
65+
In addition to all arguments above, the following attributes are exported:
66+
67+
* `id` - The data source ID.
68+
69+
* `resources` - The list of the filtered secrets.
70+
71+
The [resources](#resources_struct) structure is documented below.
72+
73+
* `total_count` - The total number of the filtered secrets.
74+
75+
<a name="resources_struct"></a>
76+
The `resources` block supports:
77+
78+
* `resource_id` - The secret ID.
79+
80+
* `resource_name` - The secret name.
81+
82+
* `resource_detail` - The secret detail.
83+
84+
The [resource_detail](#resource_detail_struct) structure is documented below.
85+
86+
* `tags` - The tag list.
87+
88+
The [tags](#tags_item_struct) structure is documented below.
89+
90+
* `sys_tags` - The system tag list.
91+
92+
The [sys_tags](#sys_tags_struct) structure is documented below.
93+
94+
<a name="resource_detail_struct"></a>
95+
The `resource_detail` block supports:
96+
97+
* `id` - The ID of the secret.
98+
99+
* `name` - The secret name.
100+
101+
* `state` - The secret status. The valid values are as follows:
102+
+ **ENABLED**: Indicates enabled status.
103+
+ **DISABLED**: Indicates disabled status.
104+
+ **PENDING_DELETE**: Indicates pending deletion status.
105+
+ **FROZEN**: Indicates frozen state.
106+
107+
* `kms_key_id` - The ID of KMS key used to encrypt secret.
108+
109+
* `description` - The description of the secret.
110+
111+
* `create_time` - The creation time of the secret, the value is a timestamp.
112+
113+
* `update_time` - The update time of the secret, the value is a timestamp.
114+
115+
* `scheduled_delete_time` - The time of the secret to be scheduled deleted, the value is a timestamp.
116+
117+
* `secret_type` - The secret type. The valid values are as follows:
118+
+ **COMMON**: shared secret (default), which is used to store sensitive information in an application system.
119+
+ **RDS**: RDS secret, which is used to store RDS account information. (no longer supported, replaced by RDS-FG).
120+
+ **RDS-FG**: RDS secret, which is used to store RDS account information.
121+
+ **GaussDB-FG**: TaurusDB secret, which is used to store TaurusDB account information.
122+
123+
* `auto_rotation` - Automatic rotation. The valid values are as follows:
124+
+ **true**: Enabled.
125+
+ **false**: Disabled.
126+
127+
* `rotation_period` - The secret rotation period. Valid when `auto_rotation` is **true**.
128+
129+
* `rotation_config` - The secret rotation config. Valid when `auto_rotation` is **true**.
130+
131+
* `rotation_time` - The rotation time of the secret, the value is a timestamp.
132+
133+
* `next_rotation_time` - The next rotation time of the secret, the value is a timestamp.
134+
135+
* `event_subscriptions` - The list of events subscribed to by secret.
136+
137+
* `enterprise_project_id` - The enterprise project ID.
138+
139+
<a name="tags_item_struct"></a>
140+
The `tags` block supports:
141+
142+
* `key` - The tag key.
143+
144+
* `value` - The tag value.
145+
146+
<a name="sys_tags_struct"></a>
147+
The `sys_tags` block supports:
148+
149+
* `key` - The system tag key.
150+
151+
* `value` - The system tag value.

huaweicloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,7 @@ func Provider() *schema.Provider {
715715
"huaweicloud_csms_secrets": dew.DataSourceDewCsmsSecrets(),
716716
"huaweicloud_csms_secret_version": dew.DataSourceDewCsmsSecret(),
717717
"huaweicloud_csms_secret_versions": dew.DataSourceDewCsmsSecretVersions(),
718+
"huaweicloud_csms_secrets_by_tags": dew.DataSourceCSMSSecretsByTags(),
718719
"huaweicloud_css_flavors": css.DataSourceCssFlavors(),
719720
"huaweicloud_css_clusters": css.DataSourceCssClusters(),
720721
"huaweicloud_css_logstash_pipelines": css.DataSourceCssLogstashPipelines(),
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
package dew
2+
3+
import (
4+
"fmt"
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 TestAccCSMSSecretsByTagDataSource_basic(t *testing.T) {
13+
var (
14+
name = acceptance.RandomAccResourceName()
15+
dataSourceName = "data.huaweicloud_csms_secrets_by_tags.test"
16+
dc = acceptance.InitDataSourceCheck(dataSourceName)
17+
)
18+
19+
resource.ParallelTest(t, resource.TestCase{
20+
PreCheck: func() {
21+
acceptance.TestAccPreCheck(t)
22+
},
23+
ProviderFactories: acceptance.TestAccProviderFactories,
24+
Steps: []resource.TestStep{
25+
{
26+
Config: testCSMSSecretsByTagDataSource_basic(name),
27+
Check: resource.ComposeTestCheckFunc(
28+
dc.CheckResourceExists(),
29+
resource.TestCheckResourceAttrSet(dataSourceName, "resources.#"),
30+
resource.TestCheckResourceAttrSet(dataSourceName, "resources.0.resource_id"),
31+
resource.TestCheckResourceAttrSet(dataSourceName, "resources.0.resource_name"),
32+
resource.TestCheckResourceAttrSet(dataSourceName, "resources.0.tags.0.key"),
33+
resource.TestCheckResourceAttrSet(dataSourceName, "resources.0.tags.0.value"),
34+
resource.TestCheckResourceAttrSet(dataSourceName, "resources.0.resource_detail.0.auto_rotation"),
35+
resource.TestCheckResourceAttrSet(dataSourceName, "resources.0.resource_detail.0.create_time"),
36+
resource.TestCheckResourceAttrSet(dataSourceName, "resources.0.resource_detail.0.description"),
37+
resource.TestCheckResourceAttrSet(dataSourceName, "resources.0.resource_detail.0.enterprise_project_id"),
38+
resource.TestCheckResourceAttrSet(dataSourceName, "resources.0.resource_detail.0.id"),
39+
resource.TestCheckResourceAttrSet(dataSourceName, "resources.0.resource_detail.0.kms_key_id"),
40+
resource.TestCheckResourceAttrSet(dataSourceName, "resources.0.resource_detail.0.name"),
41+
resource.TestCheckResourceAttrSet(dataSourceName, "resources.0.resource_detail.0.description"),
42+
resource.TestCheckResourceAttrSet(dataSourceName, "resources.0.resource_detail.0.secret_type"),
43+
resource.TestCheckResourceAttrSet(dataSourceName, "resources.0.resource_detail.0.state"),
44+
resource.TestCheckResourceAttrSet(dataSourceName, "resources.0.resource_detail.0.update_time"),
45+
resource.TestCheckOutput("tags_single_filter_is_useful", "true"),
46+
resource.TestCheckOutput("tags_double_filter_is_useful", "true"),
47+
resource.TestCheckOutput("matches_single_filter_is_useful", "true"),
48+
resource.TestCheckOutput("matches_double_filter_is_useful", "true"),
49+
resource.TestCheckOutput("count_is_useful", "true"),
50+
),
51+
},
52+
},
53+
})
54+
}
55+
56+
func testCSMSSecretsByTagDataSource_basic(name string) string {
57+
return fmt.Sprintf(`
58+
resource "huaweicloud_csms_secret" "test" {
59+
name = "%s_1"
60+
secret_text = "this is a password"
61+
description = "csms secret test"
62+
secret_type = "COMMON"
63+
enterprise_project_id = "0"
64+
65+
tags = {
66+
test_tag_1 = "test_value_1"
67+
}
68+
}
69+
70+
resource "huaweicloud_csms_secret" "test2" {
71+
name = "%s_2"
72+
secret_text = "this is a password"
73+
description = "csms secret test"
74+
secret_type = "COMMON"
75+
enterprise_project_id = "0"
76+
77+
tags = {
78+
test_tag_1 = "test_value_2"
79+
}
80+
}
81+
82+
data "huaweicloud_csms_secrets_by_tags" "test" {
83+
resource_instances = "resource_instances"
84+
action = "filter"
85+
sequence = "test_sequence"
86+
87+
depends_on = [huaweicloud_csms_secret.test, huaweicloud_csms_secret.test2]
88+
}
89+
90+
locals {
91+
secret1_key = keys(huaweicloud_csms_secret.test.tags)[0]
92+
secret1_value = huaweicloud_csms_secret.test.tags[keys(huaweicloud_csms_secret.test.tags)[0]]
93+
secret2_key = keys(huaweicloud_csms_secret.test2.tags)[0]
94+
secret2_value = huaweicloud_csms_secret.test2.tags[keys(huaweicloud_csms_secret.test.tags)[0]]
95+
secret1_name = huaweicloud_csms_secret.test.name
96+
secret2_name = huaweicloud_csms_secret.test2.name
97+
}
98+
99+
data "huaweicloud_csms_secrets_by_tags" "tags_single_filter" {
100+
resource_instances = "resource_instances"
101+
action = "filter"
102+
103+
tags {
104+
key = local.secret1_key
105+
values = [local.secret1_value]
106+
}
107+
108+
depends_on = [huaweicloud_csms_secret.test, huaweicloud_csms_secret.test2]
109+
}
110+
111+
data "huaweicloud_csms_secrets_by_tags" "tags_double_filter" {
112+
resource_instances = "resource_instances"
113+
action = "filter"
114+
115+
tags {
116+
key = local.secret1_key
117+
values = [local.secret1_value,local.secret2_value]
118+
}
119+
120+
depends_on = [huaweicloud_csms_secret.test, huaweicloud_csms_secret.test2]
121+
}
122+
123+
data "huaweicloud_csms_secrets_by_tags" "matches_single_filter" {
124+
resource_instances = "resource_instances"
125+
action = "filter"
126+
127+
matches {
128+
key = "resource_name"
129+
value = local.secret1_name
130+
}
131+
132+
depends_on = [huaweicloud_csms_secret.test, huaweicloud_csms_secret.test2]
133+
}
134+
135+
data "huaweicloud_csms_secrets_by_tags" "matches_double_filter" {
136+
resource_instances = "resource_instances"
137+
action = "filter"
138+
139+
matches {
140+
key = "resource_name"
141+
value = local.secret1_name
142+
}
143+
144+
matches {
145+
key = "resource_name"
146+
value = local.secret2_name
147+
}
148+
149+
depends_on = [huaweicloud_csms_secret.test, huaweicloud_csms_secret.test2]
150+
}
151+
152+
data "huaweicloud_csms_secrets_by_tags" "count" {
153+
resource_instances = "resource_instances"
154+
action = "count"
155+
156+
depends_on = [huaweicloud_csms_secret.test, huaweicloud_csms_secret.test2]
157+
}
158+
159+
output "tags_single_filter_is_useful" {
160+
value = alltrue([
161+
length(data.huaweicloud_csms_secrets_by_tags.tags_single_filter.resources) == 1,
162+
data.huaweicloud_csms_secrets_by_tags.tags_single_filter.resources[0].tags[0].key == local.secret1_key,
163+
data.huaweicloud_csms_secrets_by_tags.tags_single_filter.resources[0].tags[0].value == local.secret1_value
164+
])
165+
}
166+
167+
output "tags_double_filter_is_useful" {
168+
value = alltrue([
169+
length(data.huaweicloud_csms_secrets_by_tags.tags_double_filter.resources) == 2,
170+
data.huaweicloud_csms_secrets_by_tags.tags_double_filter.resources[0].tags[0].key == local.secret1_key,
171+
data.huaweicloud_csms_secrets_by_tags.tags_double_filter.resources[0].tags[0].value == local.secret1_value,
172+
data.huaweicloud_csms_secrets_by_tags.tags_double_filter.resources[1].tags[0].key == local.secret1_key,
173+
data.huaweicloud_csms_secrets_by_tags.tags_double_filter.resources[1].tags[0].value == local.secret2_value,
174+
])
175+
}
176+
177+
output "matches_single_filter_is_useful" {
178+
value = alltrue([
179+
length(data.huaweicloud_csms_secrets_by_tags.matches_single_filter.resources) == 1,
180+
data.huaweicloud_csms_secrets_by_tags.tags_double_filter.resources[0].resource_name == local.secret1_name,
181+
])
182+
}
183+
184+
output "matches_double_filter_is_useful" {
185+
value = alltrue([
186+
length(data.huaweicloud_csms_secrets_by_tags.matches_double_filter.resources) == 0,
187+
])
188+
}
189+
190+
output "count_is_useful" {
191+
value = alltrue([
192+
data.huaweicloud_csms_secrets_by_tags.count.total_count == 2,
193+
])
194+
}
195+
`, name, name)
196+
}

0 commit comments

Comments
 (0)