/// <summary> /// Waits for the task represented by the specified <see cref="VSphereManagedObject" /> to reach a completed state. /// </summary> /// <param name="taskObject">The <see cref="VSphereManagedObject" /> representing the task.</param> /// <param name="timeout">The length of time to wait for the task to complete.</param> /// <returns>A <see cref="VSphereTaskResult" /> representing the result of the task.</returns> /// <exception cref="ArgumentNullException"><paramref name="taskObject" /> is null.</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="timeout" /> is zero or negative.</exception> public VSphereTaskResult WaitForTaskCompletion(VSphereManagedObject taskObject, TimeSpan timeout) { if (taskObject == null) { throw new ArgumentNullException(nameof(taskObject)); } if (timeout <= TimeSpan.Zero) { throw new ArgumentOutOfRangeException(nameof(timeout), "Timeout must be greater than zero."); } VSpherePropertySpec getTaskState = new VSpherePropertySpec(VSphereManagedObjectType.Task, "info.state", "info.result", "info.error"); VSphereManagedObject task = RetrieveObject(taskObject, getTaskState); for (int i = 0; i < (int)timeout.TotalSeconds; i++) { TaskInfoState taskState = (TaskInfoState)task.Properties["info.state"]; if (taskState == TaskInfoState.success || taskState == TaskInfoState.error) { return(new VSphereTaskResult(task)); } else { Thread.Sleep(TimeSpan.FromSeconds(1)); task = RetrieveObject(taskObject, getTaskState); } } return(new VSphereTaskResult(task)); }
/// <summary> /// Retrieves a vSphere managed object with updated property values using the specified <see cref="VSpherePropertySpec" />. /// </summary> /// <param name="managedObject">The <see cref="VSphereManagedObject" /> to retrieve.</param> /// <param name="propertySpec">The <see cref="VSpherePropertySpec" /> that specifies which object properties will be retrieved.</param> /// <returns>A <see cref="VSphereManagedObject" />.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="managedObject" /> is null. /// <para>or</para> /// <paramref name="propertySpec" /> is null. /// </exception> public VSphereManagedObject RetrieveObject(VSphereManagedObject managedObject, VSpherePropertySpec propertySpec) { if (managedObject == null) { throw new ArgumentNullException(nameof(managedObject)); } if (propertySpec == null) { throw new ArgumentNullException(nameof(propertySpec)); } ObjectSpec objectSpec = new ObjectSpec { obj = managedObject.ManagedObjectReference }; PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec { objectSet = new[] { objectSpec }, propSet = new[] { propertySpec.PropertySpec } }; var result = _vimService.RetrieveProperties(_serviceContent.propertyCollector, new[] { propertyFilterSpec }); return(new VSphereManagedObject(result.Single())); }
/// <summary> /// Retrieves a set of vSphere managed objects using the specified <see cref="VSphereSelectionSpec" /> set and <see cref="VSpherePropertySpec" /> set. /// </summary> /// <param name="startingObject">The <see cref="VSphereManagedObject" /> from which the query should start.</param> /// <param name="selectionSpecs">The collection of <see cref="VSphereSelectionSpec" /> that specifies which objects will be queried.</param> /// <param name="propertySpecs">The collection of <see cref="VSpherePropertySpec" /> that specifies which object properties will be retrieved.</param> /// <returns>A collection of <see cref="VSphereManagedObject" />.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="startingObject" /> is null. /// <para>or</para> /// <paramref name="selectionSpecs" /> is null. /// <para>or</para> /// <paramref name="propertySpecs" /> is null. /// </exception> public IEnumerable <VSphereManagedObject> RetrieveObjects(VSphereManagedObject startingObject, IEnumerable <VSphereSelectionSpec> selectionSpecs, IEnumerable <VSpherePropertySpec> propertySpecs) { if (startingObject == null) { throw new ArgumentNullException(nameof(startingObject)); } if (selectionSpecs == null) { throw new ArgumentNullException(nameof(selectionSpecs)); } if (propertySpecs == null) { throw new ArgumentNullException(nameof(propertySpecs)); } ObjectSpec objectSpec = new ObjectSpec { obj = startingObject.ManagedObjectReference, selectSet = selectionSpecs.Select(n => n.SelectionSpec).ToArray() }; PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec { objectSet = new[] { objectSpec }, propSet = propertySpecs.Select(n => n.PropertySpec).ToArray() }; var result = _vimService.RetrieveProperties(_serviceContent.propertyCollector, new[] { propertyFilterSpec }); return(result.Select(n => new VSphereManagedObject(n))); }
/// <summary> /// Initializes a new instance of the <see cref="VSphereTaskResult"/> class. /// </summary> /// <param name="taskObject">The <see cref="VSphereManagedObject" /> representing the task object.</param> internal VSphereTaskResult(VSphereManagedObject taskObject) { Success = (TaskInfoState)taskObject.Properties["info.state"] == TaskInfoState.success; if (taskObject.Properties.ContainsKey("info.error")) { Error = ((LocalizedMethodFault)taskObject.Properties["info.error"]).localizedMessage; } }
private void WaitForTaskSuccess(VSphereManagedObject taskObject) { VSphereTaskResult taskResult = CallVSphere(n => n.WaitForTaskCompletion(taskObject, TimeSpan.FromMinutes(1))); if (!taskResult.Success) { throw new VirtualMachineOperationException(taskResult.Error ?? "The VSphere operation did not complete successfully."); } }
/// <summary> /// Reboots the specified virtual machine. /// </summary> /// <param name="vmObject">The <see cref="VSphereManagedObject" /> representing the virtual machine.</param> /// <exception cref="ArgumentNullException"><paramref name="vmObject" /> is null.</exception> public void RebootVirtualMachine(VSphereManagedObject vmObject) { if (vmObject == null) { throw new ArgumentNullException(nameof(vmObject)); } _vimService.RebootGuest(vmObject.ManagedObjectReference); }
/// <summary> /// Retrieves a set of vSphere managed objects using the specified <see cref="VSphereSelectionSpec" /> and <see cref="VSpherePropertySpec" /> set. /// </summary> /// <param name="startingObject">The <see cref="VSphereManagedObject" /> from which the query should start.</param> /// <param name="selectionSpec">The <see cref="VSphereSelectionSpec" /> that specifies which objects will be queried.</param> /// <param name="propertySpecs">The collection of <see cref="VSpherePropertySpec" /> that specifies which object properties will be retrieved.</param> /// <returns>A collection of <see cref="VSphereManagedObject" />.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="startingObject" /> is null. /// <para>or</para> /// <paramref name="selectionSpec" /> is null. /// <para>or</para> /// <paramref name="propertySpecs" /> is null. /// </exception> public IEnumerable <VSphereManagedObject> RetrieveObjects(VSphereManagedObject startingObject, VSphereSelectionSpec selectionSpec, IEnumerable <VSpherePropertySpec> propertySpecs) { if (selectionSpec == null) { throw new ArgumentNullException(nameof(selectionSpec)); } return(RetrieveObjects(startingObject, new[] { selectionSpec }, propertySpecs)); }
/// <summary> /// Updates the status of the specified <see cref="VSphereVirtualMachine" />. /// </summary> /// <param name="virtualMachine">The <see cref="VSphereVirtualMachine" /> to update.</param> /// <exception cref="ArgumentNullException"><paramref name="virtualMachine" /> is null.</exception> /// <exception cref="VirtualMachineOperationException">The vSphere service threw an error while processing the command.</exception> public void UpdateStatus(VSphereVirtualMachine virtualMachine) { if (virtualMachine == null) { throw new ArgumentNullException(nameof(virtualMachine)); } VSphereManagedObject result = CallVSphere(n => n.RetrieveObject(virtualMachine.ManagedObject, VSphereVirtualMachine.StandardProperties)); virtualMachine.UpdateStatus(result); }
/// <summary> /// Reverts the specified virtual machine to the most recent snapshot. /// </summary> /// <param name="vmObject">The <see cref="VSphereManagedObject" /> representing the virtual machine.</param> /// <returns>A <see cref="VSphereManagedObject" /> representing the task created by the power on operation.</returns> /// <exception cref="ArgumentNullException"><paramref name="vmObject" /> is null.</exception> public VSphereManagedObject RevertVirtualMachineToSnapshot(VSphereManagedObject vmObject) { if (vmObject == null) { throw new ArgumentNullException(nameof(vmObject)); } ManagedObjectReference task = _vimService.RevertToCurrentSnapshot_Task(vmObject.ManagedObjectReference, null, true, true); return(new VSphereManagedObject(task)); }
/// <summary> /// Powers off the specified virtual machine. /// </summary> /// <param name="vmObject">The <see cref="VSphereManagedObject" /> representing the virtual machine.</param> /// <returns>A <see cref="VSphereManagedObject" /> representing the task created by the power on operation.</returns> /// <exception cref="ArgumentNullException"><paramref name="vmObject" /> is null.</exception> public VSphereManagedObject PowerOffVirtualMachine(VSphereManagedObject vmObject) { if (vmObject == null) { throw new ArgumentNullException(nameof(vmObject)); } ManagedObjectReference task = _vimService.PowerOffVM_Task(vmObject.ManagedObjectReference); return(new VSphereManagedObject(task)); }
/// <summary> /// Reverts the specified virtual machine to the most recent snapshot. /// </summary> /// <param name="virtualMachine">The <see cref="VSphereVirtualMachine" /> to revert.</param> /// <exception cref="ArgumentNullException"><paramref name="virtualMachine" /> is null.</exception> /// <exception cref="VirtualMachineOperationException">The vSphere service threw an error while processing the command.</exception> public void RevertToSnapshot(VSphereVirtualMachine virtualMachine) { if (virtualMachine == null) { throw new ArgumentNullException(nameof(virtualMachine)); } LogInfo($"Reverting virtual machine {virtualMachine.HostName} to latest snapshot."); VSphereManagedObject task = CallVSphere(n => n.RevertVirtualMachineToSnapshot(virtualMachine.ManagedObject)); WaitForTaskSuccess(task); UpdateStatus(virtualMachine); }
/// <summary> /// Powers off the specified virtual machine. /// </summary> /// <param name="virtualMachine">The <see cref="VSphereVirtualMachine" /> to power off.</param> /// <exception cref="ArgumentNullException"><paramref name="virtualMachine" /> is null.</exception> /// <exception cref="VirtualMachineOperationException">The vSphere service threw an error while processing the command.</exception> public void PowerOff(VSphereVirtualMachine virtualMachine) { if (virtualMachine == null) { throw new ArgumentNullException(nameof(virtualMachine)); } LogInfo($"Powering off virtual machine {virtualMachine.HostName}."); VSphereManagedObject task = CallVSphere(n => n.PowerOffVirtualMachine(virtualMachine.ManagedObject)); WaitForTaskSuccess(task); UpdateStatus(virtualMachine); }
/// <summary> /// Runs a guest process in the specified virtual machine. /// </summary> /// <param name="vmObject">The <see cref="VSphereManagedObject" /> representing the virtual machine.</param> /// <param name="fileName">The file name of the application to run.</param> /// <param name="arguments">The command-line arguments to pass to the application when the process starts.</param> /// <param name="credential">The credential to run the process as.</param> /// <param name="waitForExit">if set to <c>true</c> wait for the process to exit before returning.</param> /// <returns>The ID of the guest process that was started.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="vmObject" /> is null. /// <para>or</para> /// <paramref name="credential" /> is null. /// </exception> public long RunVirtualMachineGuestProcess(VSphereManagedObject vmObject, string fileName, string arguments, NetworkCredential credential, bool waitForExit) { if (vmObject == null) { throw new ArgumentNullException(nameof(vmObject)); } if (credential == null) { throw new ArgumentNullException(nameof(credential)); } VSpherePropertySpec spec = new VSpherePropertySpec(VSphereManagedObjectType.GuestOperationsManager, "processManager"); VSphereManagedObject guestOperationsManager = RetrieveObject(new VSphereManagedObject(_serviceContent.guestOperationsManager), spec); var processManager = (ManagedObjectReference)guestOperationsManager.Properties["processManager"]; NamePasswordAuthentication auth = new NamePasswordAuthentication { username = BuildUserName(credential), password = credential.Password, interactiveSession = true }; GuestProgramSpec guestProgram = new GuestProgramSpec { programPath = fileName, arguments = arguments }; long processId = _vimService.StartProgramInGuest(processManager, vmObject.ManagedObjectReference, auth, guestProgram); if (waitForExit) { while (true) { var processInfo = _vimService.ListProcessesInGuest(processManager, vmObject.ManagedObjectReference, auth, new[] { processId }).FirstOrDefault(); if (processInfo?.exitCodeSpecified == true) { break; } Thread.Sleep(TimeSpan.FromSeconds(1)); } } return(processId); }