@@ -70,6 +70,28 @@ func adminStatePtr(strVal string) *swagger_models.AdminState {
70
70
return & val
71
71
}
72
72
73
+ func edgeNodeUpdateAdminState (client * zedcloudapi.Client , d * schema.ResourceData ,
74
+ id string ) error {
75
+ cfg , err := getEdgeNodeConfig (client , "" , id )
76
+ if err != nil {
77
+ return fmt .Errorf ("Failed to find Edge Node to set Admin state. err: %s" ,
78
+ err .Error ())
79
+ }
80
+ var action string
81
+ action , err = setAdminState (cfg , d )
82
+ if err != nil {
83
+ return fmt .Errorf ("Failed to set Admin state in Config. err: %s" , err .Error ())
84
+ }
85
+ if action == "" {
86
+ return nil
87
+ }
88
+ _ , err = edgeNodeSendPutReq (client , cfg , action )
89
+ if err != nil {
90
+ return fmt .Errorf ("Failed to set Admin state. Err: %s" , err .Error ())
91
+ }
92
+ return nil
93
+ }
94
+
73
95
func edgeNodeUpdateBaseOs (client * zedcloudapi.Client , cfg * swagger_models.DeviceConfig ,
74
96
eve_image_version string ) error {
75
97
// BaseImage is supposed to have only one entry. If there are multiple,
@@ -86,13 +108,15 @@ func edgeNodeUpdateBaseOs(client *zedcloudapi.Client, cfg *swagger_models.Device
86
108
// BaseOs update is Special case - Publish Config followed by ApplyConfig
87
109
_ , err := edgeNodeSendPutReq (client , cfg , "publish" )
88
110
if err != nil {
89
- return fmt .Errorf ("BaseOsImage Publish failed. Err: %s" , err .Error ())
111
+ return fmt .Errorf ("BaseOsImage Publish failed. eve_image_version: %s, Err: %s" ,
112
+ eve_image_version , err .Error ())
90
113
}
91
114
// We need to apply the configuration
92
115
rsp , err := edgeNodeSendPutReq (client , cfg , "apply" )
93
116
// Ignore HTTP_STATUS_CONFLICT
94
117
if err != nil && rsp .StatusCode != http .StatusConflict {
95
- return fmt .Errorf ("BaseOsUpdate Apply request failed. Err: %s" , err .Error ())
118
+ return fmt .Errorf ("BaseOsUpdate Apply request failed. eve_image_version: %s, " +
119
+ "Err: %s." , eve_image_version , err .Error ())
96
120
}
97
121
return nil
98
122
}
@@ -200,35 +224,49 @@ func setSystemInterface(cfg *swagger_models.DeviceConfig, d *schema.ResourceData
200
224
return nil
201
225
}
202
226
203
- func setAdminState ( cfg * swagger_models. DeviceConfig , d * schema.ResourceData , create bool ) error {
227
+ func checkAdminStateValue ( d * schema.ResourceData ) ( string , error ) {
204
228
strVal := rdEntryStr (d , "adminstate_config" )
205
229
switch strVal {
206
230
case "ADMIN_STATE_ACTIVE" :
231
+ return "activate" , nil
207
232
case "ADMIN_STATE_INACTIVE" :
208
- break
233
+ return "deactivate" , nil
209
234
default :
210
- return fmt .Errorf ("adminstate_config must be specified and be one of " +
235
+ return "" , fmt .Errorf ("adminstate_config must be specified and be one of " +
211
236
"ADMIN_STATE_ACTIVE, ADMIN_STATE_INACTIVE" )
212
237
}
213
- if ! create {
214
- // Device Update. If adminstate_config is ACTIVE and the device is
215
- // already in Registered state, do not change Admin State
216
- if strVal == "ADMIN_STATE_ACTIVE" && cfg .AdminState != nil &&
238
+ }
239
+
240
+ func setAdminState (cfg * swagger_models.DeviceConfig ,
241
+ d * schema.ResourceData ) (string , error ) {
242
+ action , err := checkAdminStateValue (d )
243
+ if err != nil {
244
+ return "" , err
245
+ }
246
+ strVal := rdEntryStr (d , "adminstate_config" )
247
+ adminstate := swagger_models .AdminState (strVal )
248
+ if cfg .AdminState != nil {
249
+ if adminstate == * cfg .AdminState {
250
+ // Admin State same as configured value - no action needed.
251
+ return "" , nil
252
+ }
253
+ // If adminstate_config is ACTIVE and the device is already in
254
+ // Registered state. No action needed.
255
+ if strVal == "ADMIN_STATE_ACTIVE" &&
217
256
* cfg .AdminState == swagger_models .AdminStateADMINSTATEREGISTERED {
218
- return nil
257
+ return "" , nil
219
258
}
220
259
}
221
- adminstate := swagger_models .AdminState (strVal )
222
260
cfg .AdminState = & adminstate
223
- return nil
261
+ return action , nil
224
262
}
225
263
226
264
func rdDeviceConfig (cfg * swagger_models.DeviceConfig , d * schema.ResourceData , create bool ) error {
227
265
var err error
228
266
cfg .Description = rdEntryStr (d , "description" )
229
267
cfg .Title = rdEntryStrPtrOrNil (d , "title" )
230
268
231
- err = setAdminState ( cfg , d , create )
269
+ _ , err = checkAdminStateValue ( d )
232
270
if err != nil {
233
271
return err
234
272
}
@@ -266,7 +304,7 @@ func rdDeviceConfig(cfg *swagger_models.DeviceConfig, d *schema.ResourceData, cr
266
304
}
267
305
cfg .ProjectID = projectIdPtr
268
306
} else {
269
- if cfg .ProjectID != nil && * cfg .ProjectID = = * projectIdPtr {
307
+ if cfg .ProjectID != nil && * cfg .ProjectID ! = * projectIdPtr {
270
308
// Update. Project cannot be changed
271
309
return fmt .Errorf ("project_id cannot be changed after EdgeNode is " +
272
310
"created. Current: %s, New: %s" , * cfg .ProjectID , * projectIdPtr )
@@ -295,7 +333,8 @@ func createEdgeNodeResource(ctx context.Context, d *schema.ResourceData, meta in
295
333
if err != nil {
296
334
return diag .Errorf ("%s Error: %s" , errMsgPrefix , err .Error ())
297
335
}
298
- log .Printf ("[INFO] Creating EdgeNode: %s" , name )
336
+
337
+ log .Printf ("[INFO] Creating EdgeNode: %s" , name )
299
338
client .XRequestIdPrefix = "TF-edgenode-create"
300
339
rspData := & swagger_models.ZsrvResponse {}
301
340
_ , err = client .SendReq ("POST" , deviceUrlExtension , cfg , rspData )
@@ -317,6 +356,13 @@ func createEdgeNodeResource(ctx context.Context, d *schema.ResourceData, meta in
317
356
return diag .Errorf ("%s %s" , errMsgPrefix , err .Error ())
318
357
}
319
358
359
+ // Activating / De-Activating the device requires activate / deactivate call
360
+ err = edgeNodeUpdateAdminState (client , d , id )
361
+ if err != nil {
362
+ return diag .Errorf ("%s Failed to update Admin state. Err: %s" ,
363
+ errMsgPrefix , err .Error ())
364
+ }
365
+
320
366
// Get Edge node config and publish the latest version. This is mainly to
321
367
// published the computed fields. Object rev. changes for every update
322
368
err = getEdgeNodeAndPublishData (client , d , name , id , true )
@@ -365,6 +411,10 @@ func updateEdgeNodeResource(ctx context.Context, d *schema.ResourceData, meta in
365
411
if err != nil {
366
412
return diag .Errorf ("%s %s" , errMsgPrefix , err .Error ())
367
413
}
414
+ err = edgeNodeUpdateAdminState (client , d , id )
415
+ if err != nil {
416
+ return diag .Errorf ("%s %s" , errMsgPrefix , err .Error ())
417
+ }
368
418
err = getEdgeNodeAndPublishData (client , d , name , id , true )
369
419
if err != nil {
370
420
return diag .Errorf ("%s" , err .Error ())
0 commit comments