@@ -57,43 +57,45 @@ func ResourceTencentCloudCkafkaUser() *schema.Resource {
57
57
func resourceTencentCloudCkafkaUserCreate (d * schema.ResourceData , meta interface {}) error {
58
58
defer tccommon .LogElapsed ("resource.tencentcloud_ckafka_user.create" )()
59
59
60
- logId := tccommon .GetLogId (tccommon .ContextNil )
61
- ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
60
+ var (
61
+ logId = tccommon .GetLogId (tccommon .ContextNil )
62
+ ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
63
+ ckafkaService = CkafkaService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
64
+ )
62
65
63
66
instanceId := d .Get ("instance_id" ).(string )
64
67
accountName := d .Get ("account_name" ).(string )
65
68
password := d .Get ("password" ).(string )
66
69
67
- ckafkaService := CkafkaService {
68
- client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn (),
69
- }
70
70
if err := ckafkaService .CreateUser (ctx , instanceId , accountName , password ); err != nil {
71
71
return fmt .Errorf ("[CRITAL]%s create ckafka user failed, reason:%+v" , logId , err )
72
72
}
73
- d .SetId (instanceId + tccommon .FILED_SP + accountName )
74
73
74
+ d .SetId (strings .Join ([]string {instanceId , accountName }, tccommon .FILED_SP ))
75
75
return resourceTencentCloudCkafkaUserRead (d , meta )
76
76
}
77
77
78
78
func resourceTencentCloudCkafkaUserRead (d * schema.ResourceData , meta interface {}) error {
79
79
defer tccommon .LogElapsed ("resource.tencentcloud_ckafka_user.read" )()
80
80
defer tccommon .InconsistentCheck (d , meta )()
81
81
82
- logId := tccommon .GetLogId (tccommon .ContextNil )
83
- ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
84
- ckafkaService := CkafkaService {
85
- client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn (),
86
- }
82
+ var (
83
+ logId = tccommon .GetLogId (tccommon .ContextNil )
84
+ ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
85
+ ckafkaService = CkafkaService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
86
+ id = d .Id ()
87
+ )
87
88
88
- id := d .Id ()
89
89
info , has , err := ckafkaService .DescribeUserByUserId (ctx , id )
90
90
if err != nil {
91
91
return err
92
92
}
93
+
93
94
if ! has {
94
95
d .SetId ("" )
95
96
return nil
96
97
}
98
+
97
99
items := strings .Split (id , tccommon .FILED_SP )
98
100
_ = d .Set ("instance_id" , items [0 ])
99
101
_ = d .Set ("account_name" , info .Name )
@@ -106,20 +108,23 @@ func resourceTencentCloudCkafkaUserRead(d *schema.ResourceData, meta interface{}
106
108
func resourceTencentCloudCkafkaUserUpdate (d * schema.ResourceData , meta interface {}) error {
107
109
defer tccommon .LogElapsed ("resource.tencentcloud_ckafka_user.update" )()
108
110
109
- logId := tccommon . GetLogId ( tccommon . ContextNil )
110
- ctx := context . WithValue ( context . TODO (), tccommon .LogIdKey , logId )
111
- ckafkaService := CkafkaService {
112
- client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn (),
113
- }
111
+ var (
112
+ logId = tccommon . GetLogId ( tccommon .ContextNil )
113
+ ctx = context . WithValue ( context . TODO (), tccommon . LogIdKey , logId )
114
+ ckafkaService = CkafkaService { client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
115
+ )
114
116
115
- instanceId := d .Get ("instance_id" ).(string )
116
- user := d .Get ("account_name" ).(string )
117
117
if d .HasChange ("password" ) {
118
+ idSplit := strings .Split (d .Id (), tccommon .FILED_SP )
119
+ if len (idSplit ) != 2 {
120
+ return fmt .Errorf ("id is broken, id is %s" , d .Id ())
121
+ }
122
+
123
+ instanceId , user := idSplit [0 ], idSplit [1 ]
118
124
old , new := d .GetChange ("password" )
119
125
if err := ckafkaService .ModifyPassword (ctx , instanceId , user , old .(string ), new .(string )); err != nil {
120
126
return err
121
127
}
122
-
123
128
}
124
129
125
130
return resourceTencentCloudCkafkaUserRead (d , meta )
@@ -128,14 +133,15 @@ func resourceTencentCloudCkafkaUserUpdate(d *schema.ResourceData, meta interface
128
133
func resourceTencentCloudCkafkaUserDelete (d * schema.ResourceData , meta interface {}) error {
129
134
defer tccommon .LogElapsed ("resource.tencentcloud_ckafka_user.delete" )()
130
135
131
- logId := tccommon . GetLogId ( tccommon . ContextNil )
132
- ctx := context . WithValue ( context . TODO (), tccommon .LogIdKey , logId )
133
- ckafkaService := CkafkaService {
134
- client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn (),
135
- }
136
+ var (
137
+ logId = tccommon . GetLogId ( tccommon .ContextNil )
138
+ ctx = context . WithValue ( context . TODO (), tccommon . LogIdKey , logId )
139
+ ckafkaService = CkafkaService { client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
140
+ )
136
141
137
142
if err := ckafkaService .DeleteUser (ctx , d .Id ()); err != nil {
138
143
return err
139
144
}
145
+
140
146
return nil
141
147
}
0 commit comments