Power off and Delete VM
In this article, we’ll learn how to implement the PowerOff and DeleteInstance commands, which shut down and delete the VM from the cloud provider, respectively.
PowerOff method
The PowerOff method shuts down (or powers off) the VM instance. It is run automatically as part of the sandbox’s teardown, and can also be run manually by the sandbox end-user from the deployed App’s commands pane. When PowerOff completes, the green ‘online’ live status icon is replaced with a grey one on the App resource, indicating it is offline.
Note: CloudShell sets the resource state to ‘offline’ if PowerOff completed successfully.
Signature
Inputs
context: context is a ResourceRemoteCommandContext object that contains:
- connectivity - CloudShell server connectivity data for authentication with CloudShell Automation API
- resource - resource configuration settings entered by the user when creating the Cloud Provider resource in the Inventory dashboard
- remote_reservation – reservation details
- remote_endpoints - will contain a single ResourceContextDetails object which provides data for the operation.
Ports
Legacy argument. Obsolete for custom cloud providers.
Error handling
If an error occurs during the PowerOff operation, the command should raise an exception.
PowerOff method implementation
The PowerOff method should perform the following steps:
- Retrieve the cloud provider resource’s connection credentials
- Convert context deployed_app_json string to object
- Power off the deployed App resource
PowerOff implementation example
Return value
None
DeleteInstance method
The DeleteInstance method powers off the VM, deletes the VM from the cloud provider and removes the App from the sandbox. It is run when removing the deployed App from the sandbox or during the sandbox’s teardown.
Signature
def DeleteInstance(self, context, ports)
Inputs
context: context is a ResourceRemoteCommandContext object that contains:
- connectivity - CloudShell server connectivity data for authentication with CloudShell Automation API
- resource - resource configuration settings entered by the user when creating the Cloud Provider resource in the Inventory dashboard
- remote_reservation – reservation details
- remote_endpoints- will contain a single ResourceContextDetails object which provides data for the operation.
Ports
Legacy argument. Obsolete for custom cloud providers.
DeleteInstance method implementation
The DeleteInstance method should perform the following steps:
- Retrieve the cloud provider resource’s connection credentials.
- Convert the deployed_app_json context string to object.
- Delete the VM instance from the cloud provider.
DeleteInstance implementation example
Return value
None