Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing tags and tag categories are not handled correctly #698

Closed
skywalk7 opened this issue Feb 1, 2019 · 1 comment
Closed

Missing tags and tag categories are not handled correctly #698

skywalk7 opened this issue Feb 1, 2019 · 1 comment
Labels
bug Type: Bug

Comments

@skywalk7
Copy link

skywalk7 commented Feb 1, 2019

When previously created tags and tag categories are missing in vCenter, provider doesn't remove them from state.

Terraform Version

Terraform v0.11.7

vSphere Provider Version

1.8.1

Affected Resource(s)

Please list the resources as a list, for example:

  • vsphere_tag
  • vsphere_tag_category

Terraform Configuration Files

variable "projects" {
  type        = "list"
  description = "List of supported projects"
}

resource "vsphere_tag_category" "project" {
  name        = "appinfra.project"
  cardinality = "SINGLE"
  description = "AppInfra Project tag category"

  associable_types = [
    "VirtualMachine",
  ]
}

resource "vsphere_tag" "tag" {
  count       = "${length(var.projects)}"
  category_id = "${vsphere_tag_category.project.id}"
  name        = "${element(var.projects, count.index)}"
  description = "AppInfra Project tag"
}

Debug Output

vsphere_tag_category.project: could not locate category with id "urn:vmomi:InventoryServiceCategory:5c1b281a-0850-4a14-a417-38620f8878ad:GLOBAL": Status code: 404, error: call failed: Error response from vCloud Suite API: {"type":"com.vmware.vapi.std.errors.not_found","value":{"messages":[]}}

Expected Behavior

Provider should mark resource as missing and plan to create it

Actual Behavior

Reading resource from provider fails

Steps to Reproduce

  1. Create tag or tag_category using terraform apply
  2. Delete tag or tag_category in vCenter
  3. Run terraform plan

Suggested patch

diff --git a/vsphere/resource_vsphere_tag.go b/vsphere/resource_vsphere_tag.go
index 6765234..0d5a35a 100644
--- a/vsphere/resource_vsphere_tag.go
+++ b/vsphere/resource_vsphere_tag.go
@@ -5,6 +5,8 @@ import (
 	"encoding/json"
 	"errors"
 	"fmt"
+	"log"
+	"strings"

 	"github.com/hashicorp/terraform/helper/schema"
 	"github.com/vmware/vic/pkg/vsphere/tags"
@@ -79,6 +81,11 @@ func resourceVSphereTagRead(d *schema.ResourceData, meta interface{}) error {
 	defer cancel()
 	tag, err := client.GetTag(ctx, id)
 	if err != nil {
+		if strings.Contains(err.Error(), "com.vmware.vapi.std.errors.not_found") {
+			log.Printf("[WARN] Tag with id %q is missing. Need to recreate it", id)
+			d.SetId("")
+			return nil
+		}
 		return fmt.Errorf("could not locate tag with id %q: %s", id, err)
 	}
 	d.Set("name", tag.Name)
diff --git a/vsphere/resource_vsphere_tag_category.go b/vsphere/resource_vsphere_tag_category.go
index 69703cb..8d0a664 100644
--- a/vsphere/resource_vsphere_tag_category.go
+++ b/vsphere/resource_vsphere_tag_category.go
@@ -4,6 +4,8 @@ import (
 	"context"
 	"errors"
 	"fmt"
+	"log"
+	"strings"

 	"github.com/hashicorp/terraform/helper/schema"
 	"github.com/hashicorp/terraform/helper/validation"
@@ -104,6 +106,11 @@ func resourceVSphereTagCategoryRead(d *schema.ResourceData, meta interface{}) er
 	defer cancel()
 	category, err := client.GetCategory(ctx, id)
 	if err != nil {
+		if strings.Contains(err.Error(), "com.vmware.vapi.std.errors.not_found") {
+			log.Printf("[WARN] Tag category with id %q is missing. Need to recreate it", id)
+			d.SetId("")
+			return nil
+		}
 		return fmt.Errorf("could not locate category with id %q: %s", id, err)
 	}
 	d.Set("name", category.Name)
@bill-rich
Copy link
Contributor

Thank you for getting this reported and providing a fix! I'll get the testing taken care of.

@bill-rich bill-rich added the bug Type: Bug label Feb 4, 2019
@bcornils bcornils added this to the v1.10.0 milestone Feb 6, 2019
@aareet aareet closed this as completed Oct 3, 2019
@aareet aareet removed this from the v1.10.0 milestone Oct 7, 2019
@ghost ghost locked and limited conversation to collaborators Apr 18, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Type: Bug
Projects
None yet
Development

No branches or pull requests

4 participants