Skip to content

Commit b370573

Browse files
Fix bug in handling admin state of device (#25)
* Fix bug in handling admin state of device * Update zedcloud-api module to v1.0.0
1 parent c49aa7d commit b370573

File tree

4 files changed

+76
-22
lines changed

4 files changed

+76
-22
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require (
1313
github.com/hashicorp/terraform-plugin-docs v0.5.1 // indirect
1414
github.com/hashicorp/terraform-plugin-sdk/v2 v2.7.0
1515
github.com/keybase/go-crypto v0.0.0-20161004153544-93f5b35093ba // indirect
16-
github.com/zededa/zedcloud-api v0.0.6-alpha
16+
github.com/zededa/zedcloud-api v1.0.0
1717
)
1818

1919
// replace github.com/zededa/zedcloud-api => ../zedcloud-api

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,8 @@ github.com/zededa/zedcloud-api v0.0.5-alpha h1:/CLwq5RPE2bYzpZ05ixG/N2k996g3SUiM
778778
github.com/zededa/zedcloud-api v0.0.5-alpha/go.mod h1:XxkHfpQWthD7+11O8GRijym56Jq30pF4VS76sNuU56M=
779779
github.com/zededa/zedcloud-api v0.0.6-alpha h1:kq+fjmaaAyKIpX7KEmVYOx0NhVVwtkGRHHg9c5kGo7A=
780780
github.com/zededa/zedcloud-api v0.0.6-alpha/go.mod h1:XxkHfpQWthD7+11O8GRijym56Jq30pF4VS76sNuU56M=
781+
github.com/zededa/zedcloud-api v1.0.0 h1:n8+3k5O//8oPUw8OtKHe1NPIbfCzXN2v6a430GiH5A4=
782+
github.com/zededa/zedcloud-api v1.0.0/go.mod h1:XxkHfpQWthD7+11O8GRijym56Jq30pF4VS76sNuU56M=
781783
go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs=
782784
go.etcd.io/etcd/client/pkg/v3 v3.5.0/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g=
783785
go.etcd.io/etcd/client/v2 v2.305.0/go.mod h1:h9puh54ZTgAKtEbut2oe9P4L/oqKCVB6xsXlzd7alYQ=

provider/resource_edgenode.go

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,28 @@ func adminStatePtr(strVal string) *swagger_models.AdminState {
7070
return &val
7171
}
7272

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+
7395
func edgeNodeUpdateBaseOs(client *zedcloudapi.Client, cfg *swagger_models.DeviceConfig,
7496
eve_image_version string) error {
7597
// 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
86108
// BaseOs update is Special case - Publish Config followed by ApplyConfig
87109
_, err := edgeNodeSendPutReq(client, cfg, "publish")
88110
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())
90113
}
91114
// We need to apply the configuration
92115
rsp, err := edgeNodeSendPutReq(client, cfg, "apply")
93116
// Ignore HTTP_STATUS_CONFLICT
94117
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())
96120
}
97121
return nil
98122
}
@@ -200,35 +224,49 @@ func setSystemInterface(cfg *swagger_models.DeviceConfig, d *schema.ResourceData
200224
return nil
201225
}
202226

203-
func setAdminState(cfg *swagger_models.DeviceConfig, d *schema.ResourceData, create bool) error {
227+
func checkAdminStateValue(d *schema.ResourceData) (string, error) {
204228
strVal := rdEntryStr(d, "adminstate_config")
205229
switch strVal {
206230
case "ADMIN_STATE_ACTIVE":
231+
return "activate", nil
207232
case "ADMIN_STATE_INACTIVE":
208-
break
233+
return "deactivate", nil
209234
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 " +
211236
"ADMIN_STATE_ACTIVE, ADMIN_STATE_INACTIVE")
212237
}
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" &&
217256
*cfg.AdminState == swagger_models.AdminStateADMINSTATEREGISTERED {
218-
return nil
257+
return "", nil
219258
}
220259
}
221-
adminstate := swagger_models.AdminState(strVal)
222260
cfg.AdminState = &adminstate
223-
return nil
261+
return action, nil
224262
}
225263

226264
func rdDeviceConfig(cfg *swagger_models.DeviceConfig, d *schema.ResourceData, create bool) error {
227265
var err error
228266
cfg.Description = rdEntryStr(d, "description")
229267
cfg.Title = rdEntryStrPtrOrNil(d, "title")
230268

231-
err = setAdminState(cfg, d, create)
269+
_, err = checkAdminStateValue(d)
232270
if err != nil {
233271
return err
234272
}
@@ -266,7 +304,7 @@ func rdDeviceConfig(cfg *swagger_models.DeviceConfig, d *schema.ResourceData, cr
266304
}
267305
cfg.ProjectID = projectIdPtr
268306
} else {
269-
if cfg.ProjectID != nil && *cfg.ProjectID == *projectIdPtr {
307+
if cfg.ProjectID != nil && *cfg.ProjectID != *projectIdPtr {
270308
// Update. Project cannot be changed
271309
return fmt.Errorf("project_id cannot be changed after EdgeNode is "+
272310
"created. Current: %s, New: %s", *cfg.ProjectID, *projectIdPtr)
@@ -295,7 +333,8 @@ func createEdgeNodeResource(ctx context.Context, d *schema.ResourceData, meta in
295333
if err != nil {
296334
return diag.Errorf("%s Error: %s", errMsgPrefix, err.Error())
297335
}
298-
log.Printf("[INFO] Creating EdgeNode: %s", name)
336+
337+
log.Printf("[INFO] Creating EdgeNode: %s", name)
299338
client.XRequestIdPrefix = "TF-edgenode-create"
300339
rspData := &swagger_models.ZsrvResponse{}
301340
_, err = client.SendReq("POST", deviceUrlExtension, cfg, rspData)
@@ -317,6 +356,13 @@ func createEdgeNodeResource(ctx context.Context, d *schema.ResourceData, meta in
317356
return diag.Errorf("%s %s", errMsgPrefix, err.Error())
318357
}
319358

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+
320366
// Get Edge node config and publish the latest version. This is mainly to
321367
// published the computed fields. Object rev. changes for every update
322368
err = getEdgeNodeAndPublishData(client, d, name, id, true)
@@ -365,6 +411,10 @@ func updateEdgeNodeResource(ctx context.Context, d *schema.ResourceData, meta in
365411
if err != nil {
366412
return diag.Errorf("%s %s", errMsgPrefix, err.Error())
367413
}
414+
err = edgeNodeUpdateAdminState(client, d, id)
415+
if err != nil {
416+
return diag.Errorf("%s %s", errMsgPrefix, err.Error())
417+
}
368418
err = getEdgeNodeAndPublishData(client, d, name, id, true)
369419
if err != nil {
370420
return diag.Errorf("%s", err.Error())

provider/resource_edgenode_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ var rdEdgeNodeFullCfg = map[string]interface{}{
4444
"id": "SAMPLE-EdgeNode-ID",
4545
"description": "SAMPLE EdgeNode DESCRIPTION",
4646
"title": "sample-title 2",
47+
"adminstate": "",
4748
"adminstate_config": "ADMIN_STATE_ACTIVE",
4849
"asset_id": "Sample Asset ID",
4950
"client_ip": "10.10.1.1",
@@ -116,12 +117,13 @@ var rdEdgeNodeFullCfg = map[string]interface{}{
116117

117118
// efo - Expected Flattened Output
118119
var efoEdgeNodeFullCfg = map[string]interface{}{
119-
"name": rdEdgeNodeFullCfg["name"],
120-
"id": rdEdgeNodeFullCfg["id"],
121-
"description": rdEdgeNodeFullCfg["description"],
122-
"title": rdEdgeNodeFullCfg["title"],
123-
"adminstate": rdEdgeNodeFullCfg["adminstate_config"],
124-
"adminstate_config": rdEdgeNodeFullCfg["adminstate_config"],
120+
"name": rdEdgeNodeFullCfg["name"],
121+
"id": rdEdgeNodeFullCfg["id"],
122+
"description": rdEdgeNodeFullCfg["description"],
123+
"title": rdEdgeNodeFullCfg["title"],
124+
"adminstate": "",
125+
// adminstate config is not set in rdDeviceConfig - so ignoring this field.
126+
"adminstate_config": "",
125127
"asset_id": rdEdgeNodeFullCfg["asset_id"],
126128
"client_ip": rdEdgeNodeFullCfg["client_ip"],
127129
"cluster_id": rdEdgeNodeFullCfg["cluster_id"],

0 commit comments

Comments
 (0)