From fb35966e46f705556821accfafbd48cb6b1af1e3 Mon Sep 17 00:00:00 2001 From: nleiva Date: Sun, 1 May 2022 00:07:35 -0400 Subject: [PATCH] Gracefully handle manually deleted resources --- GNUmakefile | 2 +- docs/index.md | 2 +- internal/provider/provider.go | 2 +- internal/provider/resource_manufacturer.go | 11 +++++++++++ 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/GNUmakefile b/GNUmakefile index 8b84f4a..eb69bd4 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -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) diff --git a/docs/index.md b/docs/index.md index 7a281fc..6cb273d 100644 --- a/docs/index.md +++ b/docs/index.md @@ -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/. diff --git a/internal/provider/provider.go b/internal/provider/provider.go index f80615e..4ef27db 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -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, diff --git a/internal/provider/resource_manufacturer.go b/internal/provider/resource_manufacturer.go index 9d02f72..221f081 100644 --- a/internal/provider/resource_manufacturer.go +++ b/internal/provider/resource_manufacturer.go @@ -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{})