I tried to cancel the instance of Bluemix IaaS (formerly SoftLayer) via API.
It is assumed that you understand the following articles. Learn more about the Bluemix Infrastructure (formerly SoftLayer) API.
ShinobiLayer: SoftLayer API Next Step: Data Type Object (1) --Qiita
I made a script that can get BillingItemId in the last column in the detailed list of devices, so please use it.
getDevicesTable-BillID.py
import SoftLayer
import dateutil.parser
from prettytable import PrettyTable
# account info
client = SoftLayer.create_client_from_env()
objectmask_vitualGuests = """
id,
virtualGuests[
id,
billingItem[
id,
cancellationDate
],
notes,
provisionDate,
fullyQualifiedDomainName,
primaryBackendIpAddress,
primaryIpAddress,
datacenter[
longName
],
operatingSystem[
softwareLicense[
softwareDescription[
name
]
],
passwords[
username,
password
]
]
]
"""
objectmask_hardware = """
id,
hardware[
id,
billingItem[
id,
cancellationDate
],
notes,
provisionDate,
fullyQualifiedDomainName,
primaryBackendIpAddress,
primaryIpAddress,
datacenter[
longName
],
operatingSystem[
softwareLicense[
softwareDescription[
name
]
],
passwords[
username,
password
]
]
]
"""
vg = client['Account'].getObject(mask=objectmask_vitualGuests)
hw = client['Account'].getObject(mask=objectmask_hardware)
virtualGuests=vg.keys()[0]
hardware=hw.keys()[0]
table = PrettyTable([
'ID',
'Device Name',
'Type',
'Location',
'Public IP',
'Private IP',
'Start Date',
'Cancel Date',
'OS Name',
'Username',
'Password',
'BillID'
])
for key in range(len(vg['virtualGuests'])):
for key2 in range(len(vg['virtualGuests'][key]['operatingSystem']['passwords'])):
table.add_row([
vg['virtualGuests'][key]['id'],
vg['virtualGuests'][key]['fullyQualifiedDomainName'],
"VSI",
vg['virtualGuests'][key]['datacenter']['longName'],
vg['virtualGuests'][key].get('primaryIpAddress'),
vg['virtualGuests'][key]['primaryBackendIpAddress'],
dateutil.parser.parse(vg['virtualGuests'][key]['provisionDate']).strftime('%Y/%m/%d'),
vg['virtualGuests'][key]['billingItem'].get('cancellationDate')[:16],
vg['virtualGuests'][key]['operatingSystem']['softwareLicense']['softwareDescription']['name'][:16],
vg['virtualGuests'][key]['operatingSystem']['passwords'][key2]['username'],
vg['virtualGuests'][key]['operatingSystem']['passwords'][key2].get('password'),
vg['virtualGuests'][key]['billingItem'].get('id')
])
for key in range(len(hw['hardware'])):
for key2 in range(len(hw['hardware'][key]['operatingSystem']['passwords'])):
table.add_row([
hw['hardware'][key]['id'],
hw['hardware'][key]['fullyQualifiedDomainName'],
"BMS",
hw['hardware'][key]['datacenter']['longName'],
hw['hardware'][key].get('primaryIpAddress'),
hw['hardware'][key]['primaryBackendIpAddress'],
dateutil.parser.parse(hw['hardware'][key]['provisionDate']).strftime('%Y/%m/%d'),
hw['hardware'][key]['billingItem'].get('cancellationDate')[:16],
hw['hardware'][key]['operatingSystem']['softwareLicense']['softwareDescription']['name'][:16],
hw['hardware'][key]['operatingSystem']['passwords'][key2]['username'],
hw['hardware'][key]['operatingSystem']['passwords'][key2]['password'],
hw['hardware'][key]['billingItem'].get('id')
])
print(table)
#Execution command
python ../Devices/getDevicesTable.py
#result
+----------+-----------------------------------------------+------+-------------+-----------------+----------------+------------+-------------+------------------+---------------+----------+-----------+
| ID | Device Name | Type | Location | Public IP | Private IP | Start Date | Cancel Date | OS Name | Username | Password | BillID |
+----------+-----------------------------------------------+------+-------------+-----------------+----------------+------------+-------------+------------------+---------------+----------+-----------+
| 22222222 | xxxxxxxxxxxxx.xxxxxxxxxxxxxxx01.vsphere.local | VSI | Tokyo 2 | xxx.xxx.xx.x | xx.xxx.xx.xx | 2016/xx/xx | | Windows 2012 R2 | xxxxxxxxxxxxx | xxxxxxxx | 111111111 |
I made a script that can cancel Immediately / ABD by specifying BillingItemId, so please use it.
cancelItem.py
import SoftLayer
import json
import sys
parm=sys.argv
itemId=parm[1]
# menu
print (30 * '-')
print (" M A I N - M E N U")
print (30 * '-')
print ("1. Immediate Cancel")
print ("2. ABD Cancel")
print (30 * '-')
# account info
client = SoftLayer.create_client_from_env()
Billing = client['SoftLayer_Billing_Item']
# define option
is_valid=0
while not is_valid :
try :
choice = int ( raw_input('Enter your choice [1-2] : ') )
is_valid = 1 ## set it to 1 to validate input and to terminate the while..not loop
except ValueError, e :
print ("'%s' is not a valid integer." % e.args[0].split(": ")[1])
### Take action as per selected menu-option ###
if choice == 1:
print ("Cancelling Immediately...")
cancel = Billing.cancelItem(True,False,"No longer needed",id=itemId)
print('Completed!')
elif choice == 2:
print ("Configuring ABD Cancellation...")
cancel = Billing.cancelItem(False,False,"No longer needed",id=itemId)
print('Completed!')
else:
print ("Invalid number. Try again...")
#Execution command
python cancelItem.py 111111111
#result
------------------------------
M A I N - M E N U
------------------------------
1. Immediate Cancel
2. ABD Cancel
------------------------------
Enter your choice [1-2] : 1
Cancelling Immediately...
Completed!
Recommended Posts