Skip to content

Commit

Permalink
Initial version of vsphere host resource
Browse files Browse the repository at this point in the history
This is a first draft of a resource that allows terraform users to add
new ESXi hosts to a vsphere cluster and manage things like:

[x] connection status
[x] maintenance status
[x] lockdown mode

Once we settle on what it should look like we can work on adding more
features.
  • Loading branch information
Kyriakos Oikonomakos committed Sep 24, 2019
1 parent 163f92b commit 8ae9ccf
Show file tree
Hide file tree
Showing 5 changed files with 1,393 additions and 3 deletions.
68 changes: 65 additions & 3 deletions vsphere/internal/helper/hostsystem/host_system_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func FromID(client *govmomi.Client, id string) (*object.HostSystem, error) {
defer cancel()
hs, err := finder.ObjectReference(ctx, ref)
if err != nil {
return nil, fmt.Errorf("could not find host system with id: %s: %s", id, err)
return nil, err
}
log.Printf("[DEBUG] Host system found: %s", hs.Reference().Value)
return hs.(*object.HostSystem), nil
Expand Down Expand Up @@ -98,6 +98,18 @@ func NameOrID(client *govmomi.Client, id string) string {
return name
}

// HostInMaintenance checks a HostSystem's maintenance mode and returns true if the
// the host is in maintenance mode.
func HostInMaintenance(host *object.HostSystem) (bool, error) {
hostObject, err := Properties(host)
if err != nil {
return false, err
}

return hostObject.Runtime.InMaintenanceMode, nil

}

// EnterMaintenanceMode puts a host into maintenance mode. If evacuate is set
// to true, all powered off VMs will be removed from the host, or the task will
// block until this is the case, depending on whether or not DRS is on or off
Expand All @@ -107,6 +119,12 @@ func EnterMaintenanceMode(host *object.HostSystem, timeout int, evacuate bool) e
evacuate = false
}

maintMode, err := HostInMaintenance(host)
if maintMode {
log.Printf("[DEBUG] Host %q is already in maintenance mode", host.Name())
return nil
}

log.Printf("[DEBUG] Host %q is entering maintenance mode (evacuate: %t)", host.Name(), evacuate)

ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(timeout))
Expand All @@ -116,11 +134,31 @@ func EnterMaintenanceMode(host *object.HostSystem, timeout int, evacuate bool) e
return err
}

return task.Wait(ctx)
err = task.Wait(ctx)
if err != nil {
return err
}
var to mo.Task
err = task.Properties(context.TODO(), task.Reference(), nil, &to)
if err != nil {
log.Printf("[DEBUG] Failed while getting task results: %s", err)
return err
}

if to.Info.State != "success" {
return fmt.Errorf("Error while putting host(%s) in maintenance mode: %s", host.Reference(), to.Info.Error)
}
return nil
}

// ExitMaintenanceMode takes a host out of maintenance mode.
func ExitMaintenanceMode(host *object.HostSystem, timeout int) error {
maintMode, err := HostInMaintenance(host)
if !maintMode {
log.Printf("[DEBUG] Host %q is already not in maintenance mode", host.Name())
return nil
}

log.Printf("[DEBUG] Host %q is exiting maintenance mode", host.Name())

// Add 5 minutes to timeout for the context timeout to allow for any issues
Expand All @@ -134,5 +172,29 @@ func ExitMaintenanceMode(host *object.HostSystem, timeout int) error {
return err
}

return task.Wait(ctx)
err = task.Wait(ctx)
if err != nil {
return err
}
var to mo.Task
err = task.Properties(context.TODO(), task.Reference(), nil, &to)
if err != nil {
log.Printf("[DEBUG] Failed while getting task results: %s", err)
return err
}

if to.Info.State != "success" {
return fmt.Errorf("Error while getting host(%s) out of maintenance mode: %s", host.Reference(), to.Info.Error)
}
return nil
}

// GetConnectionState returns the host's connection state (see vim.HostSystem.ConnectionState)
func GetConnectionState(host *object.HostSystem) (types.HostSystemConnectionState, error) {
hostProps, err := Properties(host)
if err != nil {
return "", err
}

return hostProps.Runtime.ConnectionState, nil
}
1 change: 1 addition & 0 deletions vsphere/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func Provider() terraform.ResourceProvider {
"vsphere_vapp_entity": resourceVSphereVAppEntity(),
"vsphere_vmfs_datastore": resourceVSphereVmfsDatastore(),
"vsphere_virtual_machine_snapshot": resourceVSphereVirtualMachineSnapshot(),
"vsphere_host": resourceVsphereHost(),
},

DataSourcesMap: map[string]*schema.Resource{
Expand Down
Loading

0 comments on commit 8ae9ccf

Please sign in to comment.