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

New resource: vsphere_compute_cluster_vm_group #506

Merged
merged 8 commits into from
May 10, 2018
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 .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ script:
- make test
- make vendor-status
- make vet
- make website-test
# - make website-test

branches:
only:
Expand Down
25 changes: 25 additions & 0 deletions vsphere/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,3 +869,28 @@ func testGetHostFromDataSource(s *terraform.State, resourceName string) (*object
}
return hostsystem.FromID(vars.client, vars.resourceID)
}

// testGetComputeClusterVMGroup is a convenience method to fetch a virtual
// machine group override in a (compute) cluster.
func testGetComputeClusterVMGroup(s *terraform.State, resourceName string) (*types.ClusterVmGroup, error) {
vars, err := testClientVariablesForResource(s, fmt.Sprintf("%s.%s", resourceVSphereComputeClusterVMGroupName, resourceName))
if err != nil {
return nil, err
}

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

clusterID, name, err := resourceVSphereComputeClusterVMGroupParseID(vars.resourceID)
if err != nil {
return nil, err
}

cluster, err := clustercomputeresource.FromID(vars.client, clusterID)
if err != nil {
return nil, err
}

return resourceVSphereComputeClusterVMGroupFindEntry(cluster, name)
}
18 changes: 18 additions & 0 deletions vsphere/internal/helper/structure/structure_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,3 +570,21 @@ func SetBatch(d *schema.ResourceData, attrs map[string]interface{}) error {

return nil
}

// MoRefSorter is a sorting wrapper for a slice of MangedObjectReference.
type MoRefSorter []types.ManagedObjectReference

// Len implements sort.Interface for MoRefSorter.
func (s MoRefSorter) Len() int {
return len(s)
}

// Less helps implement sort.Interface for MoRefSorter.
func (s MoRefSorter) Less(i, j int) bool {
return s[i].Value < s[j].Value
}

// Swap helps implement sort.Interface for MoRefSorter.
func (s MoRefSorter) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
120 changes: 120 additions & 0 deletions vsphere/internal/helper/virtualmachine/virtual_machine_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,3 +529,123 @@ func Destroy(vm *object.VirtualMachine) error {
defer tcancel()
return task.Wait(tctx)
}

// MOIDForUUIDResult is a struct that holds a virtual machine UUID -> MOID
// association, designed to be used as a helper for mass returning the results
// of translating multiple UUIDs to managed object IDs for various virtual
// machine operations.
type MOIDForUUIDResult struct {
// The UUID of a virtual machine.
UUID string

// The matching managed object reference ID for the virtual machine at the ID
// referenced by UUID.
MOID string
}

// MOIDForUUIDResults is a slice that holds multiple MOIDForUUIDResult structs.
type MOIDForUUIDResults []MOIDForUUIDResult

// MOIDForUUID returns the managed object reference ID for a specific virtual
// machine UUID and returns a MOIDForUUIDResult with the appropriate
// association.
func MOIDForUUID(client *govmomi.Client, uuid string) (MOIDForUUIDResult, error) {
vm, err := FromUUID(client, uuid)
if err != nil {
return MOIDForUUIDResult{}, err
}
return MOIDForUUIDResult{
UUID: uuid,
MOID: vm.Reference().Value,
}, nil
}

// UUIDForMOID returns the managed object reference ID for a specific virtual
// machine MOID and returns a MOIDForUUIDResult with the appropriate
// association.
func UUIDForMOID(client *govmomi.Client, moid string) (MOIDForUUIDResult, error) {
vm, err := FromMOID(client, moid)
if err != nil {
return MOIDForUUIDResult{}, err
}
props, err := Properties(vm)
if err != nil {
return MOIDForUUIDResult{}, err
}
return MOIDForUUIDResult{
UUID: props.Config.Uuid,
MOID: vm.Reference().Value,
}, nil
}

// MOIDsForUUIDs returns a MOIDForUUIDResults for a list of UUIDs. If one UUID
// cannot be found, an error is returned. There are no partial results
// returned.
func MOIDsForUUIDs(client *govmomi.Client, uuids []string) (MOIDForUUIDResults, error) {
var results MOIDForUUIDResults
for _, uuid := range uuids {
result, err := MOIDForUUID(client, uuid)
if err != nil {
return nil, err
}
results = append(results, result)
}
return results, nil
}

// UUIDsForMOIDs returns a MOIDForUUIDResults for a list of MOIDs. If one MOID
// cannot be found, an error is returned. There are no partial results
// returned.
func UUIDsForMOIDs(client *govmomi.Client, moids []string) (MOIDForUUIDResults, error) {
var results MOIDForUUIDResults
for _, uuid := range moids {
result, err := UUIDForMOID(client, uuid)
if err != nil {
return nil, err
}
results = append(results, result)
}
return results, nil
}

// UUIDsForManagedObjectReferences returns a MOIDForUUIDResults for a list of
// ManagedObjectReferences. If one cannot be found, an error is returned. There
// are no partial results returned.
func UUIDsForManagedObjectReferences(client *govmomi.Client, refs []types.ManagedObjectReference) (MOIDForUUIDResults, error) {
var moids []string
for _, ref := range refs {
moids = append(moids, ref.Value)
}
return UUIDsForMOIDs(client, moids)
}

// MOIDs returns all MOIDs in a MOIDForUUIDResults.
func (r MOIDForUUIDResults) MOIDs() []string {
var moids []string
for _, result := range r {
moids = append(moids, result.MOID)
}
return moids
}

// ManagedObjectReferences returns all MOIDs in a MOIDForUUIDResults, as
// ManagedObjectReferences as type VirtualMachine.
func (r MOIDForUUIDResults) ManagedObjectReferences() []types.ManagedObjectReference {
var refs []types.ManagedObjectReference
for _, result := range r {
refs = append(refs, types.ManagedObjectReference{
Type: "VirtualMachine",
Value: result.MOID,
})
}
return refs
}

// UUIDs returns all UUIDs in a MOIDForUUIDResults.
func (r MOIDForUUIDResults) UUIDs() []string {
var uuids []string
for _, result := range r {
uuids = append(uuids, result.UUID)
}
return uuids
}
1 change: 1 addition & 0 deletions vsphere/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func Provider() terraform.ResourceProvider {

ResourcesMap: map[string]*schema.Resource{
"vsphere_compute_cluster": resourceVSphereComputeCluster(),
"vsphere_compute_cluster_vm_group": resourceVSphereComputeClusterVMGroup(),
"vsphere_custom_attribute": resourceVSphereCustomAttribute(),
"vsphere_datacenter": resourceVSphereDatacenter(),
"vsphere_datastore_cluster": resourceVSphereDatastoreCluster(),
Expand Down
Loading