Skip to content

Commit a957feb

Browse files
committed
feat/ssm
1 parent e2f172e commit a957feb

File tree

6 files changed

+252
-60
lines changed

6 files changed

+252
-60
lines changed

tencentcloud/data_source_tc_ssm_secret_versions.go

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
/*
22
Use this data source to query detailed information of SSM secret version
3+
34
Example Usage
5+
46
```hcl
7+
data "tencentcloud_ssm_secret_versions" "example" {
8+
secret_name = tencentcloud_ssm_secret_version.v1.secret_name
9+
version_id = tencentcloud_ssm_secret_version.v1.version_id
10+
}
11+
12+
resource "tencentcloud_ssm_secret" "example" {
13+
secret_name = "tf-example"
14+
description = "desc."
515
6-
data "tencentcloud_ssm_secret_versions" "foo" {
7-
secret_name = "test"
8-
version_id = "v1"
16+
tags = {
17+
createdBy = "terraform"
18+
}
19+
}
20+
21+
resource "tencentcloud_ssm_secret_version" "v1" {
22+
secret_name = tencentcloud_ssm_secret.example.secret_name
23+
version_id = "v1"
24+
secret_binary = "MTIzMTIzMTIzMTIzMTIzQQ=="
925
}
1026
```
1127
*/
@@ -72,22 +88,24 @@ func dataSourceTencentCloudSsmSecretVersions() *schema.Resource {
7288
func dataSourceTencentCloudSsmSecretVersionsRead(d *schema.ResourceData, meta interface{}) error {
7389
defer logElapsed("data_source.tencentcloud_ssm_secret_versions.read")()
7490

75-
logId := getLogId(contextNil)
76-
ctx := context.WithValue(context.TODO(), logIdKey, logId)
77-
ssmService := SsmService{
78-
client: meta.(*TencentCloudClient).apiV3Conn,
79-
}
91+
var (
92+
logId = getLogId(contextNil)
93+
ctx = context.WithValue(context.TODO(), logIdKey, logId)
94+
ssmService = SsmService{client: meta.(*TencentCloudClient).apiV3Conn}
95+
secretName = d.Get("secret_name").(string)
96+
outErr, inErr error
97+
secretInfo *SecretInfo
98+
)
8099

81-
secretName := d.Get("secret_name").(string)
82-
var outErr, inErr error
83-
var secretInfo *SecretInfo
84100
outErr = resource.Retry(readRetryTimeout, func() *resource.RetryError {
85101
secretInfo, inErr = ssmService.DescribeSecretByName(ctx, secretName)
86102
if inErr != nil {
87103
return retryError(inErr)
88104
}
105+
89106
return nil
90107
})
108+
91109
if outErr != nil {
92110
sdkErr, ok := outErr.(*sdkError.TencentCloudSDKError)
93111
if ok && sdkErr.Code == SSMResourceNotFound {
@@ -99,10 +117,12 @@ func dataSourceTencentCloudSsmSecretVersionsRead(d *schema.ResourceData, meta in
99117
log.Printf("[CRITAL]%s read SSM secret failed, reason:%+v", logId, outErr)
100118
return outErr
101119
}
120+
102121
if secretInfo.status != SSM_STATUS_ENABLED {
103122
log.Printf("[CRITAL]%s read SSM secret version failed, reason: secret status is not Enabled", logId)
104123
return nil
105124
}
125+
106126
var secretVersionInfos []*SecretVersionInfo
107127
var versionIds []string
108128
if v, ok := d.GetOk("version_id"); ok {
@@ -154,8 +174,10 @@ func dataSourceTencentCloudSsmSecretVersionsRead(d *schema.ResourceData, meta in
154174
log.Printf("[CRITAL]%s provider set SSM secret version list fail, reason:%+v", logId, e)
155175
return e
156176
}
177+
157178
if output, ok := d.GetOk("result_output_file"); ok && output.(string) != "" {
158179
return writeToFile(output.(string), secretVersionList)
159180
}
181+
160182
return nil
161183
}

tencentcloud/data_source_tc_ssm_secret_versions_test.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import (
66
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
77
)
88

9+
// go test -i; go test -test.run TestAccTencentCloudSsmSecretVersionsDataSource -v
910
func TestAccTencentCloudSsmSecretVersionsDataSource(t *testing.T) {
1011
t.Parallel()
11-
dataSourceName := "data.tencentcloud_ssm_secret_versions.secret_version"
12+
dataSourceName := "data.tencentcloud_ssm_secret_versions.example"
1213

1314
resource.Test(t, resource.TestCase{
1415
PreCheck: func() { testAccPreCheck(t) },
@@ -27,23 +28,23 @@ func TestAccTencentCloudSsmSecretVersionsDataSource(t *testing.T) {
2728
}
2829

2930
const TestAccTencentCloudSsmSecretVersionsDataSourceConfig = `
30-
resource "tencentcloud_ssm_secret" "secret" {
31-
secret_name = "unit-test-ver-data"
32-
description = "test secret"
31+
data "tencentcloud_ssm_secret_versions" "example" {
32+
secret_name = tencentcloud_ssm_secret_version.v1.secret_name
33+
version_id = tencentcloud_ssm_secret_version.v1.version_id
34+
}
35+
36+
resource "tencentcloud_ssm_secret" "example" {
37+
secret_name = "tf-example-ssm-unit-test"
38+
description = "desc."
3339
3440
tags = {
35-
test-tag = "test"
41+
createdBy = "terraform"
3642
}
3743
}
3844
3945
resource "tencentcloud_ssm_secret_version" "v1" {
40-
secret_name = tencentcloud_ssm_secret.secret.secret_name
41-
version_id = "v1"
46+
secret_name = tencentcloud_ssm_secret.example.secret_name
47+
version_id = "v1"
4248
secret_binary = "MTIzMTIzMTIzMTIzMTIzQQ=="
4349
}
44-
45-
data "tencentcloud_ssm_secret_versions" "secret_version" {
46-
secret_name = tencentcloud_ssm_secret_version.v1.secret_name
47-
version_id = tencentcloud_ssm_secret_version.v1.version_id
48-
}
4950
`

tencentcloud/data_source_tc_ssm_secrets.go

Lines changed: 138 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
11
/*
22
Use this data source to query detailed information of SSM secret
3+
34
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_ssm_secrets" "example" {
8+
secret_name = tencentcloud_ssm_secret.example.secret_name
9+
state = 1
10+
}
11+
12+
resource "tencentcloud_ssm_secret" "example" {
13+
secret_name = "tf_example"
14+
description = "desc."
15+
16+
tags = {
17+
createdBy = "terraform"
18+
}
19+
}
20+
```
21+
22+
OR you can filter by tags
23+
424
```hcl
25+
data "tencentcloud_ssm_secrets" "example" {
26+
secret_name = tencentcloud_ssm_secret.example.secret_name
27+
state = 1
528
6-
data "tencentcloud_ssm_secrets" "foo" {
7-
secret_name = "test"
8-
order_type = 1
9-
state = 1
29+
tags = {
30+
createdBy = "terraform"
31+
}
1032
}
1133
```
1234
*/
@@ -48,6 +70,17 @@ func dataSourceTencentCloudSsmSecrets() *schema.Resource {
4870
Optional: true,
4971
Description: "Tags to filter secret.",
5072
},
73+
"secret_type": {
74+
Type: schema.TypeInt,
75+
Optional: true,
76+
Default: 0,
77+
Description: "0- represents user-defined credentials, defaults to 0. 1- represents the user's cloud product credentials. 2- represents SSH key pair credentials. 3- represents cloud API key pair credentials.",
78+
},
79+
"product_name": {
80+
Type: schema.TypeString,
81+
Optional: true,
82+
Description: "This parameter only takes effect when the SecretType parameter value is 1. When the SecretType value is 1, if the Product Name value is empty, it means to query all types of cloud product credentials. If the Product Name value is MySQL, it means to query MySQL database credentials. If the Product Name value is Tdsql mysql, it means to query Tdsql (MySQL version) credentials.",
83+
},
5184
"result_output_file": {
5285
Type: schema.TypeString,
5386
Optional: true,
@@ -94,6 +127,67 @@ func dataSourceTencentCloudSsmSecrets() *schema.Resource {
94127
Computed: true,
95128
Description: "Create time of secret.",
96129
},
130+
"kms_key_type": {
131+
Type: schema.TypeString,
132+
Computed: true,
133+
Description: "KMS CMK type used to encrypt credentials, DEFAULT represents the default key created by SecretsManager, and CUSTOMER represents the user specified key.",
134+
},
135+
"rotation_status": {
136+
Type: schema.TypeInt,
137+
Computed: true,
138+
Description: "1: - Turn on the rotation; 0- No rotation Note: This field may return null, indicating that a valid value cannot be obtained.",
139+
},
140+
"next_rotation_time": {
141+
Type: schema.TypeInt,
142+
Computed: true,
143+
Description: "Next rotation start time, uinx timestamp.",
144+
},
145+
"secret_type": {
146+
Type: schema.TypeInt,
147+
Computed: true,
148+
Description: "0- User defined credentials; 1- Cloud product credentials; 2- SSH key pair credentials; 3- Cloud API key pair credentials.",
149+
},
150+
"product_name": {
151+
Type: schema.TypeString,
152+
Computed: true,
153+
Description: "Cloud product name, only effective when SecretType is 1, which means the credential type is cloud product credential.",
154+
},
155+
"resource_name": {
156+
Type: schema.TypeString,
157+
Computed: true,
158+
Description: "When the credential type is SSH key pair credential, this field is valid and is used to represent the name of the SSH key pair credential.",
159+
},
160+
"project_id": {
161+
Type: schema.TypeInt,
162+
Computed: true,
163+
Description: "When the credential type is SSH key pair credential, this field is valid and represents the item ID to which the SSH key pair belongs.",
164+
},
165+
"associated_instance_ids": {
166+
Type: schema.TypeList,
167+
Computed: true,
168+
Elem: &schema.Schema{Type: schema.TypeString},
169+
Description: "When the credential type is SSH key pair credential, this field is valid and is used to represent the CVM instance ID associated with the SSH key pair.",
170+
},
171+
"target_uin": {
172+
Type: schema.TypeInt,
173+
Computed: true,
174+
Description: "When the credential type is a cloud API key pair credential, this field is valid and is used to represent the user UIN to which the cloud API key pair belongs.",
175+
},
176+
"rotation_frequency": {
177+
Type: schema.TypeInt,
178+
Computed: true,
179+
Description: "The frequency of rotation, in days, takes effect when rotation is on.",
180+
},
181+
"resource_id": {
182+
Type: schema.TypeString,
183+
Computed: true,
184+
Description: "The cloud product instance ID number corresponding to the cloud product credentials.",
185+
},
186+
"rotation_begin_time": {
187+
Type: schema.TypeString,
188+
Computed: true,
189+
Description: "The user specified rotation start time.",
190+
},
97191
},
98192
},
99193
},
@@ -104,50 +198,76 @@ func dataSourceTencentCloudSsmSecrets() *schema.Resource {
104198
func dataSourceTencentCloudSsmSecretsRead(d *schema.ResourceData, meta interface{}) error {
105199
defer logElapsed("data_source.tencentcloud_ssm_secrets.read")()
106200

107-
logId := getLogId(contextNil)
108-
ctx := context.WithValue(context.TODO(), logIdKey, logId)
201+
var (
202+
logId = getLogId(contextNil)
203+
ctx = context.WithValue(context.TODO(), logIdKey, logId)
204+
ssmService = SsmService{client: meta.(*TencentCloudClient).apiV3Conn}
205+
secrets []*ssm.SecretMetadata
206+
)
109207

110208
param := make(map[string]interface{})
111209
if v, ok := d.GetOk("order_type"); ok {
112210
param["order_type"] = v.(int)
113211
}
212+
114213
if v, ok := d.GetOk("state"); ok {
115214
param["state"] = v.(int)
116215
}
216+
117217
if v, ok := d.GetOk("secret_name"); ok {
118218
param["secret_name"] = v.(string)
119219
}
220+
120221
if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
121222
param["tag_filter"] = tags
122223
}
123224

124-
ssmService := SsmService{
125-
client: meta.(*TencentCloudClient).apiV3Conn,
225+
if v, ok := d.GetOk("secret_type"); ok {
226+
param["secret_type"] = v.(string)
126227
}
127-
var secrets []*ssm.SecretMetadata
228+
229+
if v, ok := d.GetOk("product_name"); ok {
230+
param["product_name"] = v.(string)
231+
}
232+
128233
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
129234
results, e := ssmService.DescribeSecretsByFilter(ctx, param)
130235
if e != nil {
131236
return retryError(e)
132237
}
238+
133239
secrets = results
134240
return nil
135241
})
242+
136243
if err != nil {
137244
log.Printf("[CRITAL]%s read SSM secrets failed, reason:%+v", logId, err)
138245
return err
139246
}
247+
140248
secretList := make([]map[string]interface{}, 0, len(secrets))
141249
secretNames := make([]string, 0, len(secrets))
142250
for _, secret := range secrets {
143251
mapping := map[string]interface{}{
144-
"secret_name": secret.SecretName,
145-
"description": secret.Description,
146-
"kms_key_id": secret.KmsKeyId,
147-
"create_uin": secret.CreateUin,
148-
"status": secret.Status,
149-
"delete_time": secret.DeleteTime,
150-
"create_time": secret.CreateTime,
252+
"secret_name": secret.SecretName,
253+
"description": secret.Description,
254+
"kms_key_id": secret.KmsKeyId,
255+
"create_uin": secret.CreateUin,
256+
"status": secret.Status,
257+
"delete_time": secret.DeleteTime,
258+
"create_time": secret.CreateTime,
259+
"kms_key_type": secret.KmsKeyType,
260+
"rotation_status": secret.RotationStatus,
261+
"next_rotation_time": secret.NextRotationTime,
262+
"secret_type": secret.SecretType,
263+
"product_name": secret.ProductName,
264+
"resource_name": secret.ResourceName,
265+
"project_id": secret.ProjectID,
266+
"associated_instance_ids": secret.AssociatedInstanceIDs,
267+
"target_uin": secret.TargetUin,
268+
"rotation_frequency": secret.RotationFrequency,
269+
"resource_id": secret.ResourceID,
270+
"rotation_begin_time": secret.RotationBeginTime,
151271
}
152272

153273
secretList = append(secretList, mapping)
@@ -159,8 +279,10 @@ func dataSourceTencentCloudSsmSecretsRead(d *schema.ResourceData, meta interface
159279
log.Printf("[CRITAL]%s provider set SSM secret list fail, reason:%+v", logId, e)
160280
return e
161281
}
282+
162283
if output, ok := d.GetOk("result_output_file"); ok && output.(string) != "" {
163284
return writeToFile(output.(string), secretList)
164285
}
286+
165287
return nil
166288
}

0 commit comments

Comments
 (0)