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

Add vSphere host resource #836

Merged
merged 1 commit into from
Sep 24, 2019
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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the change here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the error messages coming from the API can be a bit vague sometimes. Adding a little bit of text gives better context and we can also track the source of the error just by grepping for the error message if we have to :) .

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You went the other way here though, right? It looks like it originally included "could not find host system with..." and that part was removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm you're right.... I guess I read the diff the other way. This must have happened by accident. The good news is that I have another branch I'm working on where I'm revisiting all this code and adding some text instead of just returning the vsphere error, so this will be adressed there.

}
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" {
koikonom marked this conversation as resolved.
Show resolved Hide resolved
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