Skip to content

Commit

Permalink
Move to runtime validation
Browse files Browse the repository at this point in the history
  • Loading branch information
appilon committed Feb 24, 2022
1 parent 0c0cca5 commit e1a956b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 22 deletions.
47 changes: 25 additions & 22 deletions vsphere/resource_vsphere_tag_category.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,6 @@ func resourceVSphereTagCategory() *schema.Resource {
Description: "Object types to which this category's tags can be attached. Valid types include: Folder, ClusterComputeResource, Datacenter, Datastore, StoragePod, DistributedVirtualPortgroup, DistributedVirtualSwitch, VmwareDistributedVirtualSwitch, HostSystem, com.vmware.content.Library, com.vmware.content.library.Item, HostNetwork, Network, OpaqueNetwork, ResourcePool, VirtualApp, VirtualMachine.",
Elem: &schema.Schema{Type: schema.TypeString},
Required: true,
ValidateFunc: validation.StringInSlice(
[]string{
"Folder",
"ClusterComputeResource",
"Datacenter",
"Datastore",
"StoragePod",
"DistributedVirtualPortgroup",
"DistributedVirtualSwitch",
"VmwareDistributedVirtualSwitch",
"HostSystem",
"com.vmware.content.Library",
"com.vmware.content.library.Item",
"HostNetwork",
"Network",
"OpaqueNetwork",
"ResourcePool",
"VirtualApp",
"VirtualMachine",
},
false,
),
},
},
}
Expand All @@ -96,6 +74,9 @@ func resourceVSphereTagCategoryCreate(d *schema.ResourceData, meta interface{})
return err
}
associableTypesRaw := structure.SliceInterfacesToStrings(d.Get("associable_types").(*schema.Set).List())
if err := validateAssociableTypes(associableTypesRaw); err != nil {
return err
}
associableTypes := appendPrefix(associableTypesRaw)

spec := &tags.Category{
Expand Down Expand Up @@ -169,6 +150,9 @@ func resourceVSphereTagCategoryUpdate(d *schema.ResourceData, meta interface{})

id := d.Id()
associableTypesRaw := structure.SliceInterfacesToStrings(d.Get("associable_types").(*schema.Set).List())
if err := validateAssociableTypes(associableTypesRaw); err != nil {
return err
}
associableTypes := appendPrefix(associableTypesRaw)

spec := &tags.Category{
Expand Down Expand Up @@ -228,3 +212,22 @@ func appendPrefix(associableTypes []string) []string {
}
return appendedTypes
}

func validateAssociableTypes(types []string) error {

mapOf := func(s []string) map[string]struct{} {
m := make(map[string]struct{}, len(s))
for _, k := range s {
m[k] = struct{}{}
}
return m
}

validTypesMap := mapOf(vSphereTagTypes)
for _, t := range types {
if _, exists := validTypesMap[t]; !exists {
return fmt.Errorf("%s is not a valid associable_type, valid types include: %v", t, vSphereTagTypes)
}
}
return nil
}
29 changes: 29 additions & 0 deletions vsphere/resource_vsphere_tag_category_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,23 @@ func TestAccResourceVSphereTagCategory_removeTypeShouldError(t *testing.T) {
})
}

func TestAccResourceVSphereTagCategory_invalidTypeShouldError(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
RunSweepers()
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccResourceVSphereTagCategoryExists(false),
Steps: []resource.TestStep{
{
Config: testAccResourceVSphereTagCategoryConfigInvalid,
ExpectError: regexp.MustCompile("is not a valid associable_type"),
},
},
})
}

func TestAccResourceVSphereTagCategory_rename(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
Expand Down Expand Up @@ -262,6 +279,18 @@ resource "vsphere_tag_category" "testacc-category" {
}
`

const testAccResourceVSphereTagCategoryConfigInvalid = `
resource "vsphere_tag_category" "testacc-category" {
name = "testacc-category"
description = "Managed by Terraform"
cardinality = "SINGLE"
associable_types = [
"invalid",
]
}
`

const testAccResourceVSphereTagCategoryConfigMultiCardinality = `
resource "vsphere_tag_category" "testacc-category" {
name = "testacc-category"
Expand Down
20 changes: 20 additions & 0 deletions vsphere/tags_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ const (
vSphereTagTypeVirtualMachine = "VirtualMachine"
)

var vSphereTagTypes = []string{
"Folder",
"ClusterComputeResource",
"Datacenter",
vSphereTagTypeDatastore,
"StoragePod",
"DistributedVirtualPortgroup",
"DistributedVirtualSwitch",
"VmwareDistributedVirtualSwitch",
"HostSystem",
"com.vmware.content.Library",
"com.vmware.content.library.Item",
"HostNetwork",
"Network",
"OpaqueNetwork",
"ResourcePool",
"VirtualApp",
vSphereTagTypeVirtualMachine,
}

// vSphereTagCategorySearchErrMultiple is an error message format for a tag
// category search that returned multiple results. This is a bug and needs to
// be reported so we can adjust the API.
Expand Down

0 comments on commit e1a956b

Please # to comment.