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

tags: Ensure complete removal of tags is working #196

Merged
merged 1 commit into from
Oct 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions vsphere/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,27 @@ func testObjectHasTags(s *terraform.State, client *tags.RestClient, obj object.R
return nil
}

// testObjectHasNoTags checks to make sure that an object has no tags attached
// to it. The parameters are the same as testObjectHasTags, but no tag resource
// needs to be supplied.
func testObjectHasNoTags(s *terraform.State, client *tags.RestClient, obj object.Reference) error {
objID := obj.Reference().Value
objType, err := tagTypeForObject(obj)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout)
defer cancel()
actualIDs, err := client.ListAttachedTags(ctx, objID, objType)
if err != nil {
return err
}
if len(actualIDs) > 0 {
return fmt.Errorf("object %q still has tags (%#v)", obj.Reference().Value, actualIDs)
}
return nil
}

// testGetDatastore gets the datastore at the supplied full address. This
// function works for multiple datastore resources (example:
// vsphere_nas_datastore and vsphere_vmfs_datastore), hence the need for the
Expand Down
18 changes: 18 additions & 0 deletions vsphere/resource_vsphere_folder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,24 @@ func testAccResourceVSphereFolderCheckTags(tagResName string) resource.TestCheck
}
}

// testAccResourceVSphereFolderCheckNoTags is a check to ensure that a folder
// has no tags on it. This is used by the vsphere_tag tests specifically to
// test to make sure that complete tag removal is explicitly working without
// having to rely on the simple empty diff test after the final step.
func testAccResourceVSphereFolderCheckNoTags() resource.TestCheckFunc {
return func(s *terraform.State) error {
folder, err := testGetFolder(s, "folder")
if err != nil {
return err
}
tagsClient, err := testAccProvider.Meta().(*VSphereClient).TagsClient()
if err != nil {
return err
}
return testObjectHasNoTags(s, tagsClient, folder)
}
}

// testAccResourceVSphereFolderCreateOOB creates an out-of-band folder that is
// not tracked by TF. This is used in deletion checks to make sure we don't
// perform unsafe recursive deletions.
Expand Down
90 changes: 90 additions & 0 deletions vsphere/resource_vsphere_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -88,6 +89,31 @@ func TestAccResourceVSphereTag(t *testing.T) {
},
},
},
{
"detach all tags",
resource.TestCase{
PreCheck: func() {
testAccPreCheck(tp)
},
Providers: testAccProviders,
CheckDestroy: testAccResourceVSphereTagExists(false),
Steps: []resource.TestStep{
{
Config: testAccResourceVSphereTagConfigOnFolderAttached(),
Check: resource.ComposeTestCheckFunc(
testAccResourceVSphereTagExists(true),
),
},
{
Config: testAccResourceVSphereTagConfigOnFolderNotAttached(),
Check: resource.ComposeTestCheckFunc(
testAccResourceVSphereTagExists(true),
testAccResourceVSphereFolderCheckNoTags(),
),
},
},
},
},
{
"import",
resource.TestCase{
Expand Down Expand Up @@ -259,3 +285,67 @@ resource "vsphere_tag" "terraform-test-tag" {
category_id = "${vsphere_tag_category.terraform-test-category.id}"
}
`

func testAccResourceVSphereTagConfigOnFolderAttached() string {
return fmt.Sprintf(`
data "vsphere_datacenter" "dc" {
name = "%s"
}

resource "vsphere_tag_category" "terraform-test-category" {
name = "terraform-test-category"
cardinality = "SINGLE"

associable_types = [
"Folder",
]
}

resource "vsphere_tag" "terraform-test-tag" {
name = "terraform-test-tag"
description = "Managed by Terraform"
category_id = "${vsphere_tag_category.terraform-test-category.id}"
}

resource "vsphere_folder" "folder" {
path = "terraform-test-folder"
type = "vm"
datacenter_id = "${data.vsphere_datacenter.dc.id}"

tags = ["${vsphere_tag.terraform-test-tag.id}"]
}
`,
os.Getenv("VSPHERE_DATACENTER"),
)
}

func testAccResourceVSphereTagConfigOnFolderNotAttached() string {
return fmt.Sprintf(`
data "vsphere_datacenter" "dc" {
name = "%s"
}

resource "vsphere_tag_category" "terraform-test-category" {
name = "terraform-test-category"
cardinality = "SINGLE"

associable_types = [
"Folder",
]
}

resource "vsphere_tag" "terraform-test-tag" {
name = "terraform-test-tag"
description = "Managed by Terraform"
category_id = "${vsphere_tag_category.terraform-test-category.id}"
}

resource "vsphere_folder" "folder" {
path = "terraform-test-folder"
type = "vm"
datacenter_id = "${data.vsphere_datacenter.dc.id}"
}
`,
os.Getenv("VSPHERE_DATACENTER"),
)
}
3 changes: 2 additions & 1 deletion vsphere/tags_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ func (p *tagDiffProcessor) processDetachOperations() error {
// make sure it's worth proceeding with most of the operation. The returned
// client should be checked for nil before passing it to processTagDiff.
func tagsClientIfDefined(d *schema.ResourceData, meta interface{}) (*tags.RestClient, error) {
if _, ok := d.GetOk(vSphereTagAttributeKey); ok {
old, new := d.GetChange(vSphereTagAttributeKey)
if len(old.(*schema.Set).List()) > 0 || len(new.(*schema.Set).List()) > 0 {
client, err := meta.(*VSphereClient).TagsClient()
if err != nil {
return nil, err
Expand Down