lippman lippman - 1 month ago 11
Python Question

Softlayer api: How to check that a VSI has been created an cancelation request on "Anniversary Date"?

For a newly created VSI, we can cancel it on "Anniversary Date". The API billing_item.cancelItem can helps to accomplish it. Then in the website/devicelist of softlayer, the CancelDevice button will be unable. enter image description here

My question is how to check that a VSI has been created an cancelation request on "Anniversary Date" or not by api ? Ohter word, I want the api to get the status of a VSI has been submit the cancelation request or not.

Answer

You just need to see the "cancelationDate" property of the vsi's billing item if the value of the property is "null" it means that the VSI has not been cancelled. In case that the VSI has been cancelled in "Anniversary Date" the value of the property will be equals to the date when the machine is going to get canceled

see the example below to get the "cancelationDate" property of a particular VSI:

import SoftLayer

USERNAME = 'set me'
API_KEY = 'set me'

vsiId = 123

client = SoftLayer.Client(username=USERNAME, api_key=API_KEY)
vsiService = client['SoftLayer_Virtual_Guest']

objectMask = "mask[id, cancellationDate]"

try:
    result = vsiService.getBillingItem(mask=objectMask, id=vsiId)
    print(result)
except SoftLayer.SoftLayerAPIError as e:
    print("Unable to retrieve the VSI's billing item. " % (e.faultCode, e.faultString))
    exit(1)

list all the VSIs and their billingItems

import SoftLayer

USERNAME = 'set me'
API_KEY = 'set me'

client = SoftLayer.Client(username=USERNAME, api_key=API_KEY)
vsiService = client['SoftLayer_Account']

objectMask = "mask[id,hostname, billingItem[cancellationDate]]"

try:
    result = vsiService.getVirtualGuests(mask=objectMask)
    print(result)
except SoftLayer.SoftLayerAPIError as e:
    print("Unable to retrieve the VSI's billing item. " % (e.faultCode, e.faultString))
    exit(1)