Skip to content

Commit

Permalink
Adding vm_storage_policy_profile resource for tag based placement pol…
Browse files Browse the repository at this point in the history
…icy management (#1094)

* Adding vm_storage_policy_profile resource for tag based placement policies management

* Fixing descriptions and punctuation for storage policy.
  • Loading branch information
sumitAgrawal007 authored Jun 7, 2020
1 parent 396d3ed commit 4971a0d
Show file tree
Hide file tree
Showing 11 changed files with 539 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ require (
github.com/terraform-providers/terraform-provider-null v1.0.1-0.20191204185112-e5c592237f62
github.com/terraform-providers/terraform-provider-random v1.3.2-0.20190925210718-83518d96ae4f
github.com/terraform-providers/terraform-provider-template v1.0.0 // indirect
github.com/vmware/govmomi v0.22.2-0.20200420222347-5fceac570f29
github.com/vmware/govmomi v0.22.2-0.20200523220130-61b30e20be49
google.golang.org/genproto v0.0.0-20200122232147-0452cf42e150 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ github.com/vmihailenco/msgpack v4.0.1+incompatible h1:RMF1enSPeKTlXrXdOcqjFUElyw
github.com/vmihailenco/msgpack v4.0.1+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/vmware/govmomi v0.22.2-0.20200420222347-5fceac570f29 h1:8S2J/LGp5U19G4tia+aq+KvlNPuynIeui7W8xc8PGqU=
github.com/vmware/govmomi v0.22.2-0.20200420222347-5fceac570f29/go.mod h1:Y+Wq4lst78L85Ge/F8+ORXIWiKYqaro1vhAulACy9Lc=
github.com/vmware/govmomi v0.22.2-0.20200523220130-61b30e20be49 h1:bBp4s6KFH+6pwN2SjZPLC4W+SSEld0q1pycrcPTVyj4=
github.com/vmware/govmomi v0.22.2-0.20200523220130-61b30e20be49/go.mod h1:Y+Wq4lst78L85Ge/F8+ORXIWiKYqaro1vhAulACy9Lc=
github.com/vmware/vmw-guestinfo v0.0.0-20170707015358-25eff159a728/go.mod h1:x9oS4Wk2s2u4tS29nEaDLdzvuHdB19CvSGJjPgkZJNk=
github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4=
github.com/xiang90/probing v0.0.0-20160813154853-07dd2e8dfe18/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
Expand Down
15 changes: 15 additions & 0 deletions vsphere/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"github.com/terraform-providers/terraform-provider-vsphere/vsphere/internal/helper/contentlibrary"
"github.com/terraform-providers/terraform-provider-vsphere/vsphere/internal/helper/spbm"
"github.com/vmware/govmomi/vapi/library"
"github.com/vmware/govmomi/vapi/rest"
"os"
Expand Down Expand Up @@ -1128,3 +1129,17 @@ func testGetDatastoreClusterVMAntiAffinityRule(s *terraform.State, resourceName

return resourceVSphereDatastoreClusterVMAntiAffinityRuleFindEntry(pod, key)
}

func testGetVmStoragePolicy(s *terraform.State, resourceName string) (string, error) {

tVars, err := testClientVariablesForResource(s, fmt.Sprintf("vsphere_vm_storage_policy.%s", resourceName))
if err != nil {
return "", err
}
policyId, ok := tVars.resourceAttributes["id"]
if !ok {
return "", fmt.Errorf("resource %q has no id", resourceName)
}

return spbm.PolicyNameByID(tVars.client, policyId)
}
2 changes: 1 addition & 1 deletion vsphere/internal/helper/spbm/spbm_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func PolicyIDByName(client *govmomi.Client, name string) (string, error) {
}

// policyNameByID returns storage policy name by its ID.
func policyNameByID(client *govmomi.Client, id string) (string, error) {
func PolicyNameByID(client *govmomi.Client, id string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout)
defer cancel()
pc, err := pbmClientFromGovmomiClient(ctx, client)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package virtualdevice

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func VirtualMachineTagRulesSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"tag_category": {
Type: schema.TypeString,
Required: true,
Description: "The tag category to select the tags from.",
},
"tags": {
Type: schema.TypeList,
Required: true,
MinItems: 1,
Description: "The tags to use for creating a tag-based vm placement rule.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"include_datastores_with_tags": {
Type: schema.TypeBool,
Optional: true,
Description: "Whether to include or exclude datastores tagged with the provided tags",
Default: true,
},
}
}
1 change: 1 addition & 0 deletions vsphere/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func Provider() terraform.ResourceProvider {
"vsphere_virtual_machine_snapshot": resourceVSphereVirtualMachineSnapshot(),
"vsphere_host": resourceVsphereHost(),
"vsphere_vnic": resourceVsphereNic(),
"vsphere_vm_storage_policy": resourceVmStoragePolicy(),
},

DataSourcesMap: map[string]*schema.Resource{
Expand Down
19 changes: 17 additions & 2 deletions vsphere/resource_vsphere_tag_category.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
// vSphereTagCategoryCardinalityMultiple defines the API type for multiple
// cardinality.
vSphereTagCategoryCardinalityMultiple = "MULTIPLE"
vim25Prefix = "urn:vim25:"
)

func resourceVSphereTagCategory() *schema.Resource {
Expand Down Expand Up @@ -72,9 +73,11 @@ func resourceVSphereTagCategoryCreate(d *schema.ResourceData, meta interface{})
if err != nil {
return err
}
associableTypesRaw := structure.SliceInterfacesToStrings(d.Get("associable_types").(*schema.Set).List())
associableTypes := appendPrefix(associableTypesRaw)

spec := &tags.Category{
AssociableTypes: structure.SliceInterfacesToStrings(d.Get("associable_types").(*schema.Set).List()),
AssociableTypes: associableTypes,
Cardinality: d.Get("cardinality").(string),
Description: d.Get("description").(string),
Name: d.Get("name").(string),
Expand Down Expand Up @@ -143,9 +146,12 @@ func resourceVSphereTagCategoryUpdate(d *schema.ResourceData, meta interface{})
}

id := d.Id()
associableTypesRaw := structure.SliceInterfacesToStrings(d.Get("associable_types").(*schema.Set).List())
associableTypes := appendPrefix(associableTypesRaw)

spec := &tags.Category{
ID: id,
AssociableTypes: structure.SliceInterfacesToStrings(d.Get("associable_types").(*schema.Set).List()),
AssociableTypes: associableTypes,
Cardinality: d.Get("cardinality").(string),
Description: d.Get("description").(string),
Name: d.Get("name").(string),
Expand Down Expand Up @@ -192,3 +198,12 @@ func resourceVSphereTagCategoryImport(d *schema.ResourceData, meta interface{})
d.SetId(id)
return []*schema.ResourceData{d}, nil
}

func appendPrefix(associableTypes []string) []string {

var appendedTypes []string
for _, associableType := range associableTypes {
appendedTypes = append(appendedTypes, vim25Prefix+associableType)
}
return appendedTypes
}
Loading

0 comments on commit 4971a0d

Please # to comment.