Gracefully handle manually deleted resources

main
nleiva 2022-05-01 00:07:35 -04:00
parent ea28860388
commit fb35966e46
4 changed files with 14 additions and 3 deletions

View File

@ -3,7 +3,7 @@ HOSTNAME=github.com
NAMESPACE=nleiva
NAME=nautobot
BINARY=terraform-provider-${NAME}
VERSION=0.2.3
VERSION=0.2.4
OS_ARCH=$(shell go env GOOS)_$(shell go env GOARCH)

View File

@ -25,4 +25,4 @@ provider "nautobot" {
### Required
- `token` (String, Sensitive) Customer/user-specific authorization API token for Nautobot.
- `url` (String) URL for the Nautobot server. It should be of the form https:///server.example.org/api/.
- `url` (String) URL for the Nautobot API platform. It should be of the form https:///server.example.org/api/.

View File

@ -35,7 +35,7 @@ func New(version string) func() *schema.Provider {
Required: true,
DefaultFunc: schema.EnvDefaultFunc("NAUTOBOT_URL", nil),
ValidateFunc: validation.IsURLWithHTTPorHTTPS,
Description: "URL for the Nautobot server. It should be of the form https:///server.example.org/api/.",
Description: "URL for the Nautobot API platform. It should be of the form https:///server.example.org/api/.",
},
"token": {
Type: schema.TypeString,

View File

@ -176,7 +176,18 @@ func resourceManufacturerRead(ctx context.Context, d *schema.ResourceData, meta
return diag.Errorf("failed to get manufacturer %s from %s: %s", name, s, err.Error())
}
// If the Manufacturer is in the state file, but it is not in the Nautobot platform
// the response we get from DcimManufacturersListWithResponse is: {"count":0,"next":null,"previous":null,"results":[]}
// When you create something in Terraform but delete it manually, Terraform should gracefully handle it.
// We should set the ID to an empty string so Terraform "destroys" the resource in state.
count := gjson.Get(string(rsp.Body), "count")
if count.String() == "0" {
d.SetId("")
return diags
}
results := gjson.Get(string(rsp.Body), "results.0")
resultsReader := strings.NewReader(results.String())
item := make(map[string]interface{})