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

Optimize vm tag resources #261

Merged
merged 1 commit into from
Mar 11, 2020
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
2 changes: 1 addition & 1 deletion nsxt/resource_nsxt_policy_vm_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func resourceNsxtPolicyVMTags() *schema.Resource {
Description: "Instance id",
Required: true,
},
"tag": getTagsSchema(),
"tag": getRequiredTagsSchema(),
},
}
}
Expand Down
17 changes: 4 additions & 13 deletions nsxt/resource_nsxt_policy_vm_tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,7 @@ func TestAccResourceNsxtPolicyVMTags_basic(t *testing.T) {
),
},
{
Config: testAccNSXPolicyVMTagsRemoveTemplate(vmID),
Check: resource.ComposeTestCheckFunc(
testAccNSXPolicyVMTagsCheckExists(testResourceName),
resource.TestCheckResourceAttr(testResourceName, "tag.#", "0"),
resource.TestCheckResourceAttr(testResourceName, "instance_id", vmID),
),
Config: testAccNsxtPolicyEmptyTemplate(),
},
},
})
Expand All @@ -69,6 +64,9 @@ func TestAccResourceNsxtPolicyVMTags_import_basic(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"instance_id"},
},
{
Config: testAccNsxtPolicyEmptyTemplate(),
},
},
})
}
Expand Down Expand Up @@ -145,10 +143,3 @@ resource "nsxt_policy_vm_tags" "test" {
}
}`, instanceID)
}

func testAccNSXPolicyVMTagsRemoveTemplate(instanceID string) string {
return fmt.Sprintf(`
resource "nsxt_policy_vm_tags" "test" {
instance_id = "%s"
}`, instanceID)
}
39 changes: 25 additions & 14 deletions nsxt/resource_nsxt_vm_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,19 @@ func resourceNsxtVMTagsCreate(d *schema.ResourceData, m interface{}) error {
}

tags := getTagsFromSchema(d)
err = updateTags(nsxClient, vm.ExternalId, tags)
if err != nil {
return err
if len(tags) > 0 || d.HasChange("tag") {
err = updateTags(nsxClient, vm.ExternalId, tags)
if err != nil {
return err
}
}

portTags := getCustomizedTagsFromSchema(d, "logical_port_tag")
err = updatePortTags(nsxClient, vm.ExternalId, portTags)
if err != nil {
return err
if len(portTags) > 0 || d.HasChange("logical_port_tag") {
err = updatePortTags(nsxClient, vm.ExternalId, portTags)
if err != nil {
return err
}
}

d.SetId(vm.ExternalId)
Expand Down Expand Up @@ -288,16 +292,23 @@ func resourceNsxtVMTagsDelete(d *schema.ResourceData, m interface{}) error {
return fmt.Errorf("Error during VM retrieval: %v", err)
}

tags := make([]common.Tag, 0)
err = updateTags(nsxClient, vm.ExternalId, tags)
err2 := updatePortTags(nsxClient, vm.ExternalId, tags)

if err != nil {
return err
noTags := make([]common.Tag, 0)
vmTags := getTagsFromSchema(d)
if len(vmTags) > 0 {
// Update tags only if they were configured by the provider
err = updateTags(nsxClient, vm.ExternalId, noTags)
if err != nil {
return err
}
}

if err2 != nil {
return err2
portTags := getCustomizedTagsFromSchema(d, "logical_port_tag")
if len(portTags) > 0 {
// Update port tags only if they were configured by the provider
err := updatePortTags(nsxClient, vm.ExternalId, noTags)
if err != nil {
return err
}
}

return nil
Expand Down
13 changes: 9 additions & 4 deletions nsxt/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@ func getRevisionSchema() *schema.Schema {
}

// utilities to define & handle tags
func getTagsSchemaInternal(forceNew bool) *schema.Schema {
func getTagsSchemaInternal(required bool, forceNew bool) *schema.Schema {
return &schema.Schema{
Type: schema.TypeSet,
Description: "Set of opaque identifiers meaningful to the user",
Optional: true,
Optional: !required,
Required: required,
ForceNew: forceNew,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand All @@ -112,11 +113,15 @@ func getTagsSchemaInternal(forceNew bool) *schema.Schema {

// utilities to define & handle tags
func getTagsSchema() *schema.Schema {
return getTagsSchemaInternal(false)
return getTagsSchemaInternal(false, false)
}

func getRequiredTagsSchema() *schema.Schema {
return getTagsSchemaInternal(true, false)
}

func getTagsSchemaForceNew() *schema.Schema {
return getTagsSchemaInternal(true)
return getTagsSchemaInternal(false, true)
}

func getCustomizedTagsFromSchema(d *schema.ResourceData, schemaName string) []common.Tag {
Expand Down