Skip to content

Commit 817c5da

Browse files
MrKeiKunfbreckle
authored andcommitted
fix: replace unsafe type assertions in Read functions to prevent plugin crash
Fixes #806. Unchecked type assertions in 13 resource Read functions would panic if the API returned a non-HTTP error (e.g. network timeout, context cancellation), crashing the Terraform plugin process. Replaced with the safe two-value assertion form, consistent with the rest of the codebase.
1 parent 7bb7162 commit 817c5da

14 files changed

Lines changed: 84 additions & 70 deletions

netbox/resource_netbox_cable.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,12 @@ func resourceNetboxCableRead(d *schema.ResourceData, m interface{}) error {
140140
res, err := api.Dcim.DcimCablesRead(params, nil)
141141

142142
if err != nil {
143-
errorcode := err.(*dcim.DcimCablesReadDefault).Code()
144-
if errorcode == 404 {
145-
// If the ID is updated to blank, this tells Terraform the resource no longer exists (maybe it was destroyed out of band). Just like the destroy callback, the Read function should gracefully handle this case. https://www.terraform.io/docs/extend/writing-custom-providers.html
146-
d.SetId("")
147-
return nil
143+
if errresp, ok := err.(*dcim.DcimCablesReadDefault); ok {
144+
if errresp.Code() == 404 {
145+
// If the ID is updated to blank, this tells Terraform the resource no longer exists (maybe it was destroyed out of band). Just like the destroy callback, the Read function should gracefully handle this case. https://www.terraform.io/docs/extend/writing-custom-providers.html
146+
d.SetId("")
147+
return nil
148+
}
148149
}
149150
return err
150151
}

netbox/resource_netbox_config_context.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,12 @@ func resourceNetboxConfigContextRead(d *schema.ResourceData, m interface{}) erro
201201
res, err := api.Extras.ExtrasConfigContextsRead(params, nil)
202202

203203
if err != nil {
204-
errorcode := err.(*extras.ExtrasConfigContextsReadDefault).Code()
205-
if errorcode == 404 {
206-
// If the ID is updated to blank, this tells Terraform the resource no longer exists (maybe it was destroyed out of band). Just like the destroy callback, the Read function should gracefully handle this case. https://www.terraform.io/docs/extend/writing-custom-providers.html
207-
d.SetId("")
208-
return nil
204+
if errresp, ok := err.(*extras.ExtrasConfigContextsReadDefault); ok {
205+
if errresp.Code() == 404 {
206+
// If the ID is updated to blank, this tells Terraform the resource no longer exists (maybe it was destroyed out of band). Just like the destroy callback, the Read function should gracefully handle this case. https://www.terraform.io/docs/extend/writing-custom-providers.html
207+
d.SetId("")
208+
return nil
209+
}
209210
}
210211
return err
211212
}

netbox/resource_netbox_device_bay.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,12 @@ func resourceNetboxDeviceBayRead(d *schema.ResourceData, m interface{}) error {
9191
res, err := api.Dcim.DcimDeviceBaysRead(params, nil)
9292

9393
if err != nil {
94-
errorcode := err.(*dcim.DcimDeviceBaysReadDefault).Code()
95-
if errorcode == 404 {
96-
// If the ID is updated to blank, this tells Terraform the resource no longer exists (maybe it was destroyed out of band). Just like the destroy callback, the Read function should gracefully handle this case. https://www.terraform.io/docs/extend/writing-custom-providers.html
97-
d.SetId("")
98-
return nil
94+
if errresp, ok := err.(*dcim.DcimDeviceBaysReadDefault); ok {
95+
if errresp.Code() == 404 {
96+
// If the ID is updated to blank, this tells Terraform the resource no longer exists (maybe it was destroyed out of band). Just like the destroy callback, the Read function should gracefully handle this case. https://www.terraform.io/docs/extend/writing-custom-providers.html
97+
d.SetId("")
98+
return nil
99+
}
99100
}
100101
return err
101102
}

netbox/resource_netbox_device_front_port.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,12 @@ func resourceNetboxDeviceFrontPortRead(d *schema.ResourceData, m interface{}) er
118118
res, err := api.Dcim.DcimFrontPortsRead(params, nil)
119119

120120
if err != nil {
121-
errorcode := err.(*dcim.DcimFrontPortsReadDefault).Code()
122-
if errorcode == 404 {
123-
// If the ID is updated to blank, this tells Terraform the resource no longer exists (maybe it was destroyed out of band). Just like the destroy callback, the Read function should gracefully handle this case. https://www.terraform.io/docs/extend/writing-custom-providers.html
124-
d.SetId("")
125-
return nil
121+
if errresp, ok := err.(*dcim.DcimFrontPortsReadDefault); ok {
122+
if errresp.Code() == 404 {
123+
// If the ID is updated to blank, this tells Terraform the resource no longer exists (maybe it was destroyed out of band). Just like the destroy callback, the Read function should gracefully handle this case. https://www.terraform.io/docs/extend/writing-custom-providers.html
124+
d.SetId("")
125+
return nil
126+
}
126127
}
127128
return err
128129
}

netbox/resource_netbox_device_module_bay.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,12 @@ func resourceNetboxDeviceModuleBayRead(d *schema.ResourceData, m interface{}) er
9393
res, err := api.Dcim.DcimModuleBaysRead(params, nil)
9494

9595
if err != nil {
96-
errorcode := err.(*dcim.DcimModuleBaysReadDefault).Code()
97-
if errorcode == 404 {
98-
// If the ID is updated to blank, this tells Terraform the resource no longer exists (maybe it was destroyed out of band). Just like the destroy callback, the Read function should gracefully handle this case. https://www.terraform.io/docs/extend/writing-custom-providers.html
99-
d.SetId("")
100-
return nil
96+
if errresp, ok := err.(*dcim.DcimModuleBaysReadDefault); ok {
97+
if errresp.Code() == 404 {
98+
// If the ID is updated to blank, this tells Terraform the resource no longer exists (maybe it was destroyed out of band). Just like the destroy callback, the Read function should gracefully handle this case. https://www.terraform.io/docs/extend/writing-custom-providers.html
99+
d.SetId("")
100+
return nil
101+
}
101102
}
102103
return err
103104
}

netbox/resource_netbox_device_power_feed.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,12 @@ func resourceNetboxPowerFeedRead(d *schema.ResourceData, m interface{}) error {
142142
res, err := api.Dcim.DcimPowerFeedsRead(params, nil)
143143

144144
if err != nil {
145-
errorcode := err.(*dcim.DcimPowerFeedsReadDefault).Code()
146-
if errorcode == 404 {
147-
// If the ID is updated to blank, this tells Terraform the resource no longer exists (maybe it was destroyed out of band). Just like the destroy callback, the Read function should gracefully handle this case. https://www.terraform.io/docs/extend/writing-custom-providers.html
148-
d.SetId("")
149-
return nil
145+
if errresp, ok := err.(*dcim.DcimPowerFeedsReadDefault); ok {
146+
if errresp.Code() == 404 {
147+
// If the ID is updated to blank, this tells Terraform the resource no longer exists (maybe it was destroyed out of band). Just like the destroy callback, the Read function should gracefully handle this case. https://www.terraform.io/docs/extend/writing-custom-providers.html
148+
d.SetId("")
149+
return nil
150+
}
150151
}
151152
return err
152153
}

netbox/resource_netbox_device_power_outlet.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,12 @@ func resourceNetboxDevicePowerOutletRead(d *schema.ResourceData, m interface{})
118118
res, err := api.Dcim.DcimPowerOutletsRead(params, nil)
119119

120120
if err != nil {
121-
errorcode := err.(*dcim.DcimPowerOutletsReadDefault).Code()
122-
if errorcode == 404 {
123-
// If the ID is updated to blank, this tells Terraform the resource no longer exists (maybe it was destroyed out of band). Just like the destroy callback, the Read function should gracefully handle this case. https://www.terraform.io/docs/extend/writing-custom-providers.html
124-
d.SetId("")
125-
return nil
121+
if errresp, ok := err.(*dcim.DcimPowerOutletsReadDefault); ok {
122+
if errresp.Code() == 404 {
123+
// If the ID is updated to blank, this tells Terraform the resource no longer exists (maybe it was destroyed out of band). Just like the destroy callback, the Read function should gracefully handle this case. https://www.terraform.io/docs/extend/writing-custom-providers.html
124+
d.SetId("")
125+
return nil
126+
}
126127
}
127128
return err
128129
}

netbox/resource_netbox_device_power_port.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,12 @@ func resourceNetboxDevicePowerPortRead(d *schema.ResourceData, m interface{}) er
113113
res, err := api.Dcim.DcimPowerPortsRead(params, nil)
114114

115115
if err != nil {
116-
errorcode := err.(*dcim.DcimPowerPortsReadDefault).Code()
117-
if errorcode == 404 {
118-
// If the ID is updated to blank, this tells Terraform the resource no longer exists (maybe it was destroyed out of band). Just like the destroy callback, the Read function should gracefully handle this case. https://www.terraform.io/docs/extend/writing-custom-providers.html
119-
d.SetId("")
120-
return nil
116+
if errresp, ok := err.(*dcim.DcimPowerPortsReadDefault); ok {
117+
if errresp.Code() == 404 {
118+
// If the ID is updated to blank, this tells Terraform the resource no longer exists (maybe it was destroyed out of band). Just like the destroy callback, the Read function should gracefully handle this case. https://www.terraform.io/docs/extend/writing-custom-providers.html
119+
d.SetId("")
120+
return nil
121+
}
121122
}
122123
return err
123124
}

netbox/resource_netbox_device_rear_port.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,12 @@ func resourceNetboxDeviceRearPortRead(d *schema.ResourceData, m interface{}) err
113113
res, err := api.Dcim.DcimRearPortsRead(params, nil)
114114

115115
if err != nil {
116-
errorcode := err.(*dcim.DcimRearPortsReadDefault).Code()
117-
if errorcode == 404 {
118-
// If the ID is updated to blank, this tells Terraform the resource no longer exists (maybe it was destroyed out of band). Just like the destroy callback, the Read function should gracefully handle this case. https://www.terraform.io/docs/extend/writing-custom-providers.html
119-
d.SetId("")
120-
return nil
116+
if errresp, ok := err.(*dcim.DcimRearPortsReadDefault); ok {
117+
if errresp.Code() == 404 {
118+
// If the ID is updated to blank, this tells Terraform the resource no longer exists (maybe it was destroyed out of band). Just like the destroy callback, the Read function should gracefully handle this case. https://www.terraform.io/docs/extend/writing-custom-providers.html
119+
d.SetId("")
120+
return nil
121+
}
121122
}
122123
return err
123124
}

netbox/resource_netbox_inventory_item.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,12 @@ func resourceNetboxInventoryItemRead(d *schema.ResourceData, m interface{}) erro
148148
res, err := api.Dcim.DcimInventoryItemsRead(params, nil)
149149

150150
if err != nil {
151-
errorcode := err.(*dcim.DcimInventoryItemsReadDefault).Code()
152-
if errorcode == 404 {
153-
// If the ID is updated to blank, this tells Terraform the resource no longer exists (maybe it was destroyed out of band). Just like the destroy callback, the Read function should gracefully handle this case. https://www.terraform.io/docs/extend/writing-custom-providers.html
154-
d.SetId("")
155-
return nil
151+
if errresp, ok := err.(*dcim.DcimInventoryItemsReadDefault); ok {
152+
if errresp.Code() == 404 {
153+
// If the ID is updated to blank, this tells Terraform the resource no longer exists (maybe it was destroyed out of band). Just like the destroy callback, the Read function should gracefully handle this case. https://www.terraform.io/docs/extend/writing-custom-providers.html
154+
d.SetId("")
155+
return nil
156+
}
156157
}
157158
return err
158159
}

0 commit comments

Comments
 (0)