Microsoft azure platform provides a lot of exciting features and is very much as par with competition from AWS and google. However, there remain a few questions around the documentation and examples offered by the platform for developers – or maybe just for JAVA developers in particular who use the Azure java SDK.
In particular, I found some difficulty dealing with async VM launches and determining VM states accurately. The SDK team has been very helpful in providing examples and getting in touch for answering questions regarding the SDK. I am sharing here some code snippet which may be helpful to other developers treading the same path.
The method given below shows how you can read and categorize a VM’s state to a concrete tangible machine state.
{
PowerState instanceState = instance.powerState();
String instanceProvisionState = instance.provisioningState();
if(instanceProvisionState.equalsIgnoreCase(ProvisioningState.CREATING.name()))
{
// Instance is in pending state and is starting up
}
else if(instanceProvisionState.equalsIgnoreCase(ProvisioningState.SUCCEEDED.name()) && instanceState.toString().equalsIgnoreCase(PowerState.RUNNING.toString()))
{
// Instance is up and running
}
else if(instanceProvisionState.equalsIgnoreCase(ProvisioningState.DELETING.name()))
{
// Instance is terminating
}
else if(instanceProvisionState.equalsIgnoreCase("Updating") && instanceState.toString().equalsIgnoreCase("PowerState/stopping"))
{
// Instance is stopping
}
else
{
// unknown state
}
}
Note how an azure instance state is a combination of PowerState and ProvisingState. You need to read both values and then determine its tangible state using the combination of the two values. For my use case, I have evaluated the combination into four states which mimic the AWS instance states. You can always imply more instance states as per your needs.