@@ -22,20 +22,6 @@ import (
22
22
23
23
var importFlag1 = false
24
24
25
- var GlobalClusterId string
26
- var CreateClusterInstancesVpcId string
27
- var CreateClusterInstancesProjectId int64
28
- var WorkersInstanceIds []* string
29
- var WorkersNewWorkerInstancesList []map [string ]interface {}
30
- var WorkersLabelsMap map [string ]string
31
-
32
- func init () {
33
- // need to support append by multiple calls when the paging occurred
34
- WorkersNewWorkerInstancesList = make ([]map [string ]interface {}, 0 )
35
- WorkersLabelsMap = make (map [string ]string )
36
- WorkersInstanceIds = make ([]* string , 0 )
37
- }
38
-
39
25
func customScaleWorkerResourceImporter (ctx context.Context , d * schema.ResourceData , m interface {}) ([]* schema.ResourceData , error ) {
40
26
importFlag1 = true
41
27
err := resourceTencentCloudKubernetesScaleWorkerRead (d , m )
@@ -48,6 +34,7 @@ func customScaleWorkerResourceImporter(ctx context.Context, d *schema.ResourceDa
48
34
49
35
func resourceTencentCloudKubernetesScaleWorkerReadPostRequest1 (ctx context.Context , req * cvm.DescribeInstancesRequest , resp * cvm.DescribeInstancesResponse ) error {
50
36
d := tccommon .ResourceDataFromContext (ctx )
37
+ ctxData := tccommon .DataFromContext (ctx )
51
38
52
39
instances := make ([]* cvm.Instance , 0 )
53
40
instances = append (instances , resp .Response .InstanceSet ... )
@@ -117,85 +104,46 @@ func resourceTencentCloudKubernetesScaleWorkerReadPostRequest1(ctx context.Conte
117
104
_ = d .Set ("worker_config" , instanceList )
118
105
}
119
106
107
+ clusterId := ctxData .Get ("clusterId" ).(string )
108
+ newWorkerInstancesList := ctxData .Get ("newWorkerInstancesList" ).([]map [string ]interface {})
109
+ labelsMap := ctxData .Get ("labelsMap" ).(map [string ]string )
120
110
// The machines I generated was deleted by others.
121
- if len (WorkersNewWorkerInstancesList ) == 0 {
111
+ if len (newWorkerInstancesList ) == 0 {
122
112
d .SetId ("" )
123
113
return nil
124
114
}
125
115
126
- _ = d .Set ("cluster_id" , GlobalClusterId )
127
- _ = d .Set ("labels" , WorkersLabelsMap )
128
- _ = d .Set ("worker_instances_list" , WorkersNewWorkerInstancesList )
116
+ _ = d .Set ("cluster_id" , clusterId )
117
+ _ = d .Set ("labels" , labelsMap )
118
+ _ = d .Set ("worker_instances_list" , newWorkerInstancesList )
129
119
130
120
return nil
131
121
}
132
- func clusterInstanceParamHandle (ctx context.Context , req * tke. DescribeClusterInstancesRequest , resp * tke. DescribeClusterInstancesResponse ) error {
122
+ func clusterInstanceParamHandle (ctx context.Context , workers [] InstanceInfo ) error {
133
123
d := tccommon .ResourceDataFromContext (ctx )
134
- var has = map [string ]bool {}
135
-
136
- workerInstancesList := d .Get ("worker_instances_list" ).([]interface {})
137
- instanceMap := make (map [string ]bool )
138
- for _ , v := range workerInstancesList {
139
- infoMap , ok := v .(map [string ]interface {})
140
- if ! ok || infoMap ["instance_id" ] == nil {
141
- return fmt .Errorf ("worker_instances_list is broken." )
142
- }
143
-
144
- instanceId , ok := infoMap ["instance_id" ].(string )
145
- if ! ok || instanceId == "" {
146
- return fmt .Errorf ("worker_instances_list.instance_id is broken." )
147
- }
148
-
149
- if instanceMap [instanceId ] {
150
- log .Printf ("[WARN]The same instance id exists in the list" )
151
- }
152
-
153
- instanceMap [instanceId ] = true
154
- }
155
- workers := make ([]InstanceInfo , 0 , 100 )
156
- for _ , item := range resp .Response .InstanceSet {
157
- if has [* item .InstanceId ] {
158
- return fmt .Errorf ("get repeated instance_id[%s] when doing DescribeClusterInstances" , * item .InstanceId )
159
- }
160
- has [* item .InstanceId ] = true
161
- instanceInfo := InstanceInfo {
162
- InstanceId : * item .InstanceId ,
163
- InstanceRole : * item .InstanceRole ,
164
- InstanceState : * item .InstanceState ,
165
- FailedReason : * item .FailedReason ,
166
- InstanceAdvancedSettings : item .InstanceAdvancedSettings ,
167
- }
168
- if item .CreatedTime != nil {
169
- instanceInfo .CreatedTime = * item .CreatedTime
170
- }
171
- if item .NodePoolId != nil {
172
- instanceInfo .NodePoolId = * item .NodePoolId
173
- }
174
- if item .LanIP != nil {
175
- instanceInfo .LanIp = * item .LanIP
176
- }
177
- if instanceInfo .InstanceRole == TKE_ROLE_WORKER {
178
- workers = append (workers , instanceInfo )
179
- }
180
- }
124
+ ctxData := tccommon .DataFromContext (ctx )
181
125
126
+ newWorkerInstancesList := make ([]map [string ]interface {}, 0 , len (workers ))
127
+ labelsMap := make (map [string ]string )
128
+ instanceIds := make ([]* string , 0 )
129
+ instanceMap := ctxData .Get ("instanceMap" ).(map [string ]bool )
182
130
for sub , cvmInfo := range workers {
183
131
if _ , ok := instanceMap [cvmInfo .InstanceId ]; ! ok {
184
132
continue
185
133
}
186
- WorkersInstanceIds = append (WorkersInstanceIds , & workers [sub ].InstanceId )
134
+ instanceIds = append (instanceIds , & workers [sub ].InstanceId )
187
135
tempMap := make (map [string ]interface {})
188
136
tempMap ["instance_id" ] = cvmInfo .InstanceId
189
137
tempMap ["instance_role" ] = cvmInfo .InstanceRole
190
138
tempMap ["instance_state" ] = cvmInfo .InstanceState
191
139
tempMap ["failed_reason" ] = cvmInfo .FailedReason
192
140
tempMap ["lan_ip" ] = cvmInfo .LanIp
193
141
194
- WorkersNewWorkerInstancesList = append (WorkersNewWorkerInstancesList , tempMap )
142
+ newWorkerInstancesList = append (newWorkerInstancesList , tempMap )
195
143
if cvmInfo .InstanceAdvancedSettings != nil {
196
144
if cvmInfo .InstanceAdvancedSettings .Labels != nil {
197
145
for _ , v := range cvmInfo .InstanceAdvancedSettings .Labels {
198
- WorkersLabelsMap [helper .PString (v .Name )] = helper .PString (v .Value )
146
+ labelsMap [helper .PString (v .Name )] = helper .PString (v .Value )
199
147
}
200
148
}
201
149
@@ -278,6 +226,10 @@ func clusterInstanceParamHandle(ctx context.Context, req *tke.DescribeClusterIns
278
226
}
279
227
}
280
228
}
229
+
230
+ ctxData .Set ("newWorkerInstancesList" , newWorkerInstancesList )
231
+ ctxData .Set ("labelsMap" , labelsMap )
232
+ ctxData .Set ("instanceIds" , instanceIds )
281
233
return nil
282
234
}
283
235
@@ -290,12 +242,14 @@ func resourceTencentCloudKubernetesScaleWorkerDeletePostRequest0(ctx context.Con
290
242
291
243
func resourceTencentCloudKubernetesScaleWorkerReadPostFillRequest0 (ctx context.Context , req * tke.DescribeClustersRequest ) error {
292
244
d := tccommon .ResourceDataFromContext (ctx )
245
+ ctxData := tccommon .DataFromContext (ctx )
293
246
items := strings .Split (d .Id (), tccommon .FILED_SP )
294
247
295
248
instanceMap := make (map [string ]bool )
296
249
oldWorkerInstancesList := d .Get ("worker_instances_list" ).([]interface {})
250
+ clusterId := ""
297
251
if importFlag1 {
298
- GlobalClusterId = items [0 ]
252
+ clusterId = items [0 ]
299
253
if len (items [1 :]) >= 2 {
300
254
return fmt .Errorf ("only one additional configuration of virtual machines is now supported now, " +
301
255
"so should be 1" )
@@ -305,13 +259,15 @@ func resourceTencentCloudKubernetesScaleWorkerReadPostFillRequest0(ctx context.C
305
259
}
306
260
oldWorkerInstancesList = append (oldWorkerInstancesList , infoMap )
307
261
} else {
308
- GlobalClusterId = d .Get ("cluster_id" ).(string )
262
+ clusterId = d .Get ("cluster_id" ).(string )
309
263
}
310
264
311
- if GlobalClusterId == "" {
265
+ if clusterId == "" {
312
266
return fmt .Errorf ("tke.`cluster_id` is empty." )
313
267
}
314
268
269
+ ctxData .Set ("clusterId" , clusterId )
270
+
315
271
for _ , v := range oldWorkerInstancesList {
316
272
infoMap , ok := v .(map [string ]interface {})
317
273
if ! ok || infoMap ["instance_id" ] == nil {
@@ -327,6 +283,8 @@ func resourceTencentCloudKubernetesScaleWorkerReadPostFillRequest0(ctx context.C
327
283
instanceMap [instanceId ] = true
328
284
}
329
285
286
+ ctxData .Set ("instanceMap" , instanceMap )
287
+
330
288
return nil
331
289
}
332
290
@@ -338,7 +296,9 @@ func resourceTencentCloudKubernetesScaleWorkerReadPostRequest0(ctx context.Conte
338
296
}
339
297
340
298
func resourceTencentCloudKubernetesScaleWorkerReadPostFillRequest2 (ctx context.Context , req * cvm.DescribeInstancesRequest ) error {
341
- req .InstanceIds = WorkersInstanceIds
299
+ ctxData := tccommon .DataFromContext (ctx )
300
+ instanceIds := ctxData .Get ("instanceIds" ).([]* string )
301
+ req .InstanceIds = instanceIds
342
302
return nil
343
303
}
344
304
@@ -666,11 +626,6 @@ func resourceTencentCloudKubernetesScaleWorkerReadPostFillRequest1(ctx context.C
666
626
return err
667
627
}
668
628
log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request .GetAction (), request .ToJsonString (), response .ToJsonString ())
669
-
670
- if err := clusterInstanceParamHandle (ctx , request , response ); err != nil {
671
- return err
672
- }
673
-
674
629
if response == nil || len (response .Response .InstanceSet ) < 1 {
675
630
break
676
631
}
@@ -687,11 +642,45 @@ func resourceTencentCloudKubernetesScaleWorkerReadPostFillRequest1(ctx context.C
687
642
log .Printf ("[WARN]%s resource `kubernetes_scale_worker` [%s] not found, please check if it has been deleted.\n " , logId , d .Id ())
688
643
return nil
689
644
}
645
+
646
+ has := map [string ]bool {}
647
+ workers := make ([]InstanceInfo , 0 , len (instanceSet ))
648
+ for _ , item := range instanceSet {
649
+ if has [* item .InstanceId ] {
650
+ return fmt .Errorf ("get repeated instance_id[%s] when doing DescribeClusterInstances" , * item .InstanceId )
651
+ }
652
+ has [* item .InstanceId ] = true
653
+ instanceInfo := InstanceInfo {
654
+ InstanceId : * item .InstanceId ,
655
+ InstanceRole : * item .InstanceRole ,
656
+ InstanceState : * item .InstanceState ,
657
+ FailedReason : * item .FailedReason ,
658
+ InstanceAdvancedSettings : item .InstanceAdvancedSettings ,
659
+ }
660
+ if item .CreatedTime != nil {
661
+ instanceInfo .CreatedTime = * item .CreatedTime
662
+ }
663
+ if item .NodePoolId != nil {
664
+ instanceInfo .NodePoolId = * item .NodePoolId
665
+ }
666
+ if item .LanIP != nil {
667
+ instanceInfo .LanIp = * item .LanIP
668
+ }
669
+ if instanceInfo .InstanceRole == TKE_ROLE_WORKER {
670
+ workers = append (workers , instanceInfo )
671
+ }
672
+ }
673
+
674
+ if err := clusterInstanceParamHandle (ctx , workers ); err != nil {
675
+ return err
676
+ }
677
+
690
678
return nil
691
679
}
692
680
693
681
func resourceTencentCloudKubernetesScaleWorkerReadPreRequest1 (ctx context.Context , req * cvm.DescribeInstancesRequest ) error {
694
- req .InstanceIds = WorkersInstanceIds
695
-
682
+ ctxData := tccommon .DataFromContext (ctx )
683
+ instanceIds := ctxData .Get ("instanceIds" ).([]* string )
684
+ req .InstanceIds = instanceIds
696
685
return nil
697
686
}
0 commit comments