Skip to content

Commit

Permalink
Merge pull request #450 from terraform-providers/f-storage-drs-vm-config
Browse files Browse the repository at this point in the history
New resource: vsphere_storage_drs_vm_override
  • Loading branch information
vancluever authored Apr 5, 2018
2 parents 42fce95 + 2738d84 commit d98e5ab
Show file tree
Hide file tree
Showing 7 changed files with 1,103 additions and 0 deletions.
31 changes: 31 additions & 0 deletions vsphere/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package vsphere

import (
"context"
"errors"
"fmt"
"os"
"path"
Expand Down Expand Up @@ -684,3 +685,33 @@ func testGetDatastoreClusterProperties(s *terraform.State, resourceName string)
}
return storagepod.Properties(pod)
}

// testGetDatastoreClusterSDRSVMConfig is a convenience method to fetch a VM's
// SDRS override in a datastore cluster.
func testGetDatastoreClusterSDRSVMConfig(s *terraform.State, resourceName string) (*types.StorageDrsVmConfigInfo, error) {
vars, err := testClientVariablesForResource(s, fmt.Sprintf("%s.%s", resourceVSphereStorageDrsVMOverrideName, resourceName))
if err != nil {
return nil, err
}

if vars.resourceID == "" {
return nil, errors.New("resource ID is empty")
}

podID, vmID, err := resourceVSphereStorageDrsVMOverrideParseID(vars.resourceID)
if err != nil {
return nil, err
}

pod, err := storagepod.FromID(vars.client, podID)
if err != nil {
return nil, err
}

vm, err := virtualmachine.FromUUID(vars.client, vmID)
if err != nil {
return nil, err
}

return resourceVSphereStorageDrsVMOverrideFindEntry(pod, vm)
}
79 changes: 79 additions & 0 deletions vsphere/internal/helper/structure/structure_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"math"
"reflect"
"strconv"

"github.com/hashicorp/terraform/helper/schema"
"github.com/vmware/govmomi/vim25/types"
Expand Down Expand Up @@ -124,6 +125,84 @@ func SetBoolPtr(d *schema.ResourceData, key string, val *bool) error {
return err
}

// GetBoolStringPtr reads a ResourceData *string* field. This field is handled
// in the following way:
//
// * If it's empty, nil is returned.
// * The string is then sent through ParseBool. This will return a valid value
// for anything ParseBool returns a value for.
// * If it's anything else, an error is returned.
//
// This is designed to address the current lack of HCL and Terraform to be able
// to distinguish between nil states and zero values properly. This is a
// systemic issue that affects reading, writing, and diffing of these values.
// These issues will eventually be addressed in HCL2.
func GetBoolStringPtr(d *schema.ResourceData, key string) (*bool, error) {
v, ok := d.GetOk(key)
if !ok {
return nil, nil
}
b, err := strconv.ParseBool(v.(string))
if err != nil {
return nil, err
}
return &b, nil
}

// SetBoolStringPtr sets a stringified ResoruceData bool field. This is a field
// that is supposed to behave like a bool (true/false), but needs to be a
// string to represent a nil state as well.
//
// This is designed to address the current lack of HCL and Terraform to be able
// to distinguish between nil states and zero values properly. This is a
// systemic issue that affects reading, writing, and diffing of these values.
// These issues will eventually be addressed in HCL2.
func SetBoolStringPtr(d *schema.ResourceData, key string, val *bool) error {
var s string
if val != nil {
s = strconv.FormatBool(*val)
}
return d.Set(key, s)
}

// BoolStringPtrState is a state normalization function for stringified 3-state
// bool pointers.
//
// The function silently drops any result that can't be parsed with ParseBool,
// and will return an empty string for these cases.
//
// This is designed to address the current lack of HCL and Terraform to be able
// to distinguish between nil states and zero values properly. This is a
// systemic issue that affects reading, writing, and diffing of these values.
// These issues will eventually be addressed in HCL2.
func BoolStringPtrState(v interface{}) string {
b, err := strconv.ParseBool(v.(string))
if err != nil {
return ""
}
return strconv.FormatBool(b)
}

// ValidateBoolStringPtr validates that the input value can be parsed by
// ParseBool. It also succeeds on empty strings.
//
// This is designed to address the current lack of HCL and Terraform to be able
// to distinguish between nil states and zero values properly. This is a
// systemic issue that affects reading, writing, and diffing of these values.
// These issues will eventually be addressed in HCL2.
func ValidateBoolStringPtr() schema.SchemaValidateFunc {
return func(i interface{}, k string) (s []string, es []error) {
v := i.(string)
if v == "" {
return
}
if _, err := strconv.ParseBool(v); err != nil {
es = append(es, err)
}
return
}
}

// Int64Ptr makes an *int64 out of the value passed in through v.
func Int64Ptr(v int64) *int64 {
return &v
Expand Down
1 change: 1 addition & 0 deletions vsphere/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func Provider() terraform.ResourceProvider {
"vsphere_virtual_disk": resourceVSphereVirtualDisk(),
"vsphere_virtual_machine": resourceVSphereVirtualMachine(),
"vsphere_nas_datastore": resourceVSphereNasDatastore(),
"vsphere_storage_drs_vm_override": resourceVSphereStorageDrsVMOverride(),
"vsphere_vmfs_datastore": resourceVSphereVmfsDatastore(),
"vsphere_virtual_machine_snapshot": resourceVSphereVirtualMachineSnapshot(),
},
Expand Down
Loading

0 comments on commit d98e5ab

Please sign in to comment.