@@ -8,11 +8,6 @@ resource "tencentcloud_ssm_secret" "foo" {
88 recovery_window_in_days = 0
99 is_enabled = true
1010
11- init_secret {
12- version_id = "v1"
13- secret_string = "123456"
14- }
15-
1611 tags = {
1712 test-tag = "test"
1813 }
@@ -54,34 +49,6 @@ func resourceTencentCloudSsmSecret() *schema.Resource {
5449 Required : true ,
5550 Description : "Name of secret which cannot be repeated in the same region. The maximum length is 128 bytes. The name can only contain English letters, numbers, underscore and hyphen '-'. The first character must be a letter or number." ,
5651 },
57- "init_secret" : {
58- Type : schema .TypeList ,
59- Required : true ,
60- MinItems : 1 ,
61- MaxItems : 1 ,
62- Description : "The secret of initial version." ,
63- Elem : & schema.Resource {
64- Schema : map [string ]* schema.Schema {
65- "version_id" : {
66- Type : schema .TypeString ,
67- Required : true ,
68- Description : "Version of secret. The maximum length is 64 bytes. The version_id can only contain English letters, numbers, underscore and hyphen '-'. The first character must be a letter or number." ,
69- },
70- "secret_binary" : {
71- Type : schema .TypeString ,
72- Optional : true ,
73- ExactlyOneOf : []string {"init_secret.0.secret_string" },
74- Description : "The base64-encoded binary secret. secret_binary and secret_string must be set only one, and the maximum support is 4096 bytes. When secret status is `Disabled`, this field will not update anymore." ,
75- },
76- "secret_string" : {
77- Type : schema .TypeString ,
78- Optional : true ,
79- ExactlyOneOf : []string {"init_secret.0.secret_binary" },
80- Description : "The string text of secret. secret_binary and secret_string must be set only one, and the maximum support is 4096 bytes. When secret status is `Disabled`, this field will not update anymore." ,
81- },
82- },
83- },
84- },
8552 "is_enabled" : {
8653 Type : schema .TypeBool ,
8754 Optional : true ,
@@ -136,16 +103,10 @@ func resourceTencentCloudSsmSecretCreate(d *schema.ResourceData, meta interface{
136103 if v , ok := d .GetOk ("kms_key_id" ); ok {
137104 param ["kms_key_id" ] = v .(string )
138105 }
139-
140- initSecret := d .Get ("init_secret" ).([]interface {})
141- versionInfo := initSecret [0 ].(map [string ]interface {})
142- param ["version_id" ] = versionInfo ["version_id" ].(string )
143- if v , ok := versionInfo ["secret_binary" ]; ok {
144- param ["secret_binary" ] = v .(string )
145- }
146- if v , ok := versionInfo ["secret_string" ]; ok {
147- param ["secret_string" ] = v .(string )
148- }
106+ //use a default version info, after create secret will delete this version
107+ //because sdk do not support create secret without version
108+ param ["version_id" ] = "default"
109+ param ["secret_string" ] = "default"
149110
150111 var outErr , inErr error
151112 var secretName string
@@ -161,6 +122,18 @@ func resourceTencentCloudSsmSecretCreate(d *schema.ResourceData, meta interface{
161122 }
162123 d .SetId (secretName )
163124
125+ //delete default version info
126+ outErr = resource .Retry (writeRetryTimeout , func () * resource.RetryError {
127+ inErr = ssmService .DeleteSecretVersion (ctx , secretName , "default" )
128+ if inErr != nil {
129+ return retryError (inErr )
130+ }
131+ return nil
132+ })
133+ if outErr != nil {
134+ return outErr
135+ }
136+
164137 if isEnabled := d .Get ("is_enabled" ).(bool ); ! isEnabled {
165138 outErr = resource .Retry (writeRetryTimeout , func () * resource.RetryError {
166139 inErr = ssmService .DisableSecret (ctx , secretName )
@@ -231,55 +204,6 @@ func resourceTencentCloudSsmSecretRead(d *schema.ResourceData, meta interface{})
231204
232205 if secretInfo .status == SSM_STATUS_ENABLED {
233206 _ = d .Set ("is_enabled" , true )
234-
235- secret := d .Get ("init_secret" ).([]interface {})
236- var versionId string
237-
238- // import secret will import the first version as init_secret
239- if len (secret ) == 0 {
240- var versionIds []string
241- outErr = resource .Retry (readRetryTimeout , func () * resource.RetryError {
242- versionIds , inErr = ssmService .DescribeSecretVersionIdsByName (ctx , secretName )
243- if inErr != nil {
244- return retryError (inErr )
245- }
246- return nil
247- })
248- if outErr != nil {
249- log .Printf ("[CRITAL]%s read SSM secret versionId list failed, reason:%+v" , logId , outErr )
250- return outErr
251- }
252- if len (versionIds ) != 0 {
253- versionId = versionIds [0 ]
254- }
255- } else {
256- versionInfo := secret [0 ].(map [string ]interface {})
257- versionId = versionInfo ["version_id" ].(string )
258- }
259-
260- if versionId != "" {
261- var secretVersionInfo * SecretVersionInfo
262- outErr = resource .Retry (readRetryTimeout , func () * resource.RetryError {
263- secretVersionInfo , inErr = ssmService .DescribeSecretVersion (ctx , secretName , versionId )
264- if inErr != nil {
265- return retryError (inErr )
266- }
267- return nil
268- })
269- if outErr != nil {
270- return outErr
271- }
272-
273- initSecret := make (map [string ]interface {})
274- initSecret ["version_id" ] = secretVersionInfo .versionId
275- if secretVersionInfo .secretBinary != "" {
276- initSecret ["secret_binary" ] = secretVersionInfo .secretBinary
277- }
278- if secretVersionInfo .secretString != "" {
279- initSecret ["secret_string" ] = secretVersionInfo .secretString
280- }
281- _ = d .Set ("init_secret" , []map [string ]interface {}{initSecret })
282- }
283207 } else {
284208 _ = d .Set ("is_enabled" , false )
285209 }
@@ -331,30 +255,6 @@ func resourceTencentCloudSsmSecretUpdate(d *schema.ResourceData, meta interface{
331255 d .SetPartial ("is_enabled" )
332256 }
333257
334- var outErr , inErr error
335- var secretInfo * SecretInfo
336- outErr = resource .Retry (readRetryTimeout , func () * resource.RetryError {
337- secretInfo , inErr = ssmService .DescribeSecretByName (ctx , secretName )
338- if inErr != nil {
339- return retryError (inErr )
340- }
341- return nil
342- })
343- if outErr != nil {
344- return outErr
345- }
346-
347- if secretInfo .status == SSM_STATUS_ENABLED {
348- err := updateSecretVersionInfo (ctx , d , ssmService )
349- if err != nil {
350- log .Printf ("[CRITAL]%s modify SSM secret version failed, reason:%+v" , logId , err )
351- return err
352- }
353- d .SetPartial ("init_secret.0.version_id" )
354- d .SetPartial ("init_secret.0.secret_binary" )
355- d .SetPartial ("init_secret.0.secret_string" )
356- }
357-
358258 if d .HasChange ("tags" ) {
359259 tcClient := meta .(* TencentCloudClient ).apiV3Conn
360260 tagService := & TagService {client : tcClient }
@@ -451,63 +351,3 @@ func updateSecretIsEnabled(ctx context.Context, ssmService SsmService, secretNam
451351 }
452352 return err
453353}
454-
455- func updateSecretVersionInfo (ctx context.Context , d * schema.ResourceData , ssmService SsmService ) error {
456- logId := getLogId (ctx )
457-
458- param := make (map [string ]interface {})
459- param ["secret_name" ] = d .Get ("secret_name" ).(string )
460- param ["version_id" ] = d .Get ("init_secret.0.version_id" ).(string )
461- if v , ok := d .GetOk ("init_secret.0.secret_binary" ); ok {
462- param ["secret_binary" ] = v .(string )
463- }
464- if v , ok := d .GetOk ("init_secret.0.secret_string" ); ok {
465- param ["secret_string" ] = v .(string )
466- }
467- if d .HasChange ("init_secret.0.version_id" ) {
468- oldVersionId , newVersionId := d .GetChange ("init_secret.0.version_id" )
469- if oldVersionId .(string ) != "" {
470- err := resource .Retry (writeRetryTimeout , func () * resource.RetryError {
471- e := ssmService .DeleteSecretVersion (ctx , d .Get ("secret_name" ).(string ), oldVersionId .(string ))
472- if e != nil {
473- return retryError (e )
474- }
475- return nil
476- })
477- if err != nil {
478- log .Printf ("[CRITAL]%s delete SSM secret version failed, reason:%+v" , logId , err )
479- return err
480- }
481- }
482-
483- if newVersionId .(string ) != "" {
484- err := resource .Retry (writeRetryTimeout , func () * resource.RetryError {
485- _ , _ , e := ssmService .PutSecretValue (ctx , param )
486- if e != nil {
487- return retryError (e )
488- }
489- return nil
490- })
491- if err != nil {
492- log .Printf ("[CRITAL]%s add SSM secret version failed, reason:%+v" , logId , err )
493- return err
494- }
495- }
496- } else if d .HasChange ("init_secret.0.secret_binary" ) || d .HasChange ("init_secret.0.secret_string" ) {
497- versionId := d .Get ("init_secret.0.version_id" ).(string )
498- if versionId != "" {
499- err := resource .Retry (writeRetryTimeout , func () * resource.RetryError {
500- e := ssmService .UpdateSecret (ctx , param )
501- if e != nil {
502- return retryError (e )
503- }
504- return nil
505- })
506- if err != nil {
507- log .Printf ("[CRITAL]%s modify SSM secret content failed, reason:%+v" , logId , err )
508- return err
509- }
510- }
511- }
512- return nil
513- }
0 commit comments