IBM Cloud提供了丰富的API接口让用户对IBM Cloud平台的各种服务进行日常的管理,查询和维护,可以极大的方便用户的IT管理,提高云平台运维的工作效率。
IBM Cloud提供了两种类型的API接口,第一类是针对传统经典网络架构的IaaS资源提供API接口,比如物理机,虚机,网络,存储等(以下我们称之为Softlayer API),还有一类针对VPC网络架构的IaaS资源,以及各种PaaS层的服务,例如云数据库服务,大数据分析,网络安全及高防服务,对象存储,容器,AI等服务。 下面我们主要对经典网络架构的IaaS资源如何使用Softlayer API来进行说明。
Softlayer API支持多种语言,例如Python,Java, Go, PHP, Perl, Ruby等。以下我们主要以Python为例子进行说明。在编写我们的API代码之前,需要先做一下初始化工作,以Python为例,我们需要运行以下命令:
$ pip install softlayer
关于API接口的使用文档,可以访问以下网页的Document链接:
在这个页面,我们还可以访问各种语言的SDK文档
在 https://sldn.softlayer.com/reference/softlayerapi/ 页面上能看到所有IaaS层服务所对应的API对象
其中我们最常用的有:
Account(SoftLayer_Account): 用来表示用户账户的API对象,包含了该账户下的用户信息,服务器信息(物理机,虚拟机),存储等信息,例如SoftLayer_Account::getHardware可以获得该账户下所有的物理机列表,并返回一个数组对象。SoftLayer_Account::getOpenTickets可以获得该账户下的所有正在处理的工单,并返回一个数组对象。SoftLayer_Account::getVirtualPrivateRack可以获得该账户下所有的流量池信息,并返回一个数组对象。
SoftLayer_Hardware_Server: 用来表示账户下的物理机对象,通过这个API对象里的各种方法可以获得物理机相关的信息,例如机器名,IP地址,费用信息,所在数据中心,所属的流量池等等。例如getCost()方法可以获得该物理机的费用, getDatacenterName()方法可以获得该物理机所在数据中心.
SoftLayer_Virtual_Guest: 用来表示账户下的虚拟机对象,通过这个API对象里的各种方法可以获得物理机相关的信息,例如机器名,IP地址,费用信息,出站流量大小,所在数据中心等等。
SoftLayer_Billing_Invoice: 用来表示账户下的账单信息,通过这个API对象可以获得账户下产生的任何账单信息,例如账单金额,账单里的详细条目,账单 pdf/excel文件等等。
SoftLayer_Network_Bandwidth_Version1_Allotment: 用来表示账户下的流量池信息,通过这个API对象可以获得账户下流量池的各种信息,例如流量池中的服务器列表,流量池已用流量,流量池可用流量等等。
SoftLayer_Network_Vlan: 用来表示账户的网络VLAN信息,通过这个API对象可以获得账户下VLAN有关的各种信息,例如该VLAN下的所有服务器,该VLAN下的Subnet等等。
SoftLayer_Ticket: 用来表示账户下的工单,通过这个API对象可以获得用户账户下的所有工单信息,例如创建新的工单,更新工单内容,添加附件到相关工单,查看工单回复等等。
上面只是介绍了一部分常用的API对象,仅做参考。 在使用API的时候,还有两个概念 Object Filters和Object masks我们需要先了解一下:
Object Filters用于对记录集进行过滤和筛选,这相当于我们使用EXCEL表格时候的针对记录的筛选功能。
Object masks是对已经筛选过的记录集要显示的字段进行选取,通过mask来显示我们需要的列信息,不显示我们不需要的列信息。
关于这两个对象的具体说明和用法,大家可以参考以下文章:https://blog.csdn.net/koolincn/article/details/107168973
以下我们通过一些例子来看一下如何使用IBM Cloud API实现各种需求(以Python为例), 例如我们想查看账号下所有物理机的账单费用
"""
This script retrive all baremetal's cost in billing under account.
"""
import SoftLayer
USERNAME = 'XXXXXXXX'
API_KEY = 'c8116985714c268395c434fd41dfdsfdsaf5493507a8972199a7bde984b6'
client = SoftLayer.Client(username=USERNAME, api_key=API_KEY)
"""
Declare these to keep line lengths minimal
"""
accountClient = client['SoftLayer_Account']
billingItemClient = client['SoftLayer_Billing_Item']
"""
Add an object mask to retrieve our billing items related to the servers
http://softlayer.github.io/reference/datatypes/SoftLayer_Hardware_Server
for a list of the relational properties you can retrieve along with hardware.
"""
objectMask = 'mask[fullyQualifiedDomainName,primaryIpAddress,billingItem]'
# Make the call to retrieve our all hardware records.
servers = accountClient.getHardware(mask=objectMask)
# We are looking for the server which has the desired IP to delete it.
for server in servers:
try:
print(server["fullyQualifiedDomainName"])
print(server["primaryIpAddress"])
billingId = server['billingItem']['id']
price = billingItemClient.getNextInvoiceTotalRecurringAmount(id=billingId)
print(price)
except SoftLayer.SoftLayerAPIError as e:
"""
If there was an error returned from the SoftLayer API then bomb out with the
error message.
"""
print("Unable to get server's cost information: \nfaultCode= %s, \nfaultString= %s" % (e.faultCode, e.faultString))
print("Done")
在上面的代码段里,我们首先用相应的用户初始化账号,并设置objectMask,然后获得账号下的所有物理机,依次显示每台物理机的主机名,外网IP和对应机器的账单费用。
再来看一个例子:
import SoftLayer
USERNAME = 'XXXXXX'
API_KEY = 'c8116985714c268395c434fd4493507a8972199a7bde984b6'
client = SoftLayer.Client(username=USERNAME, api_key=API_KEY)
accountClient = client['SoftLayer_Account']
#get bandwidthpool list
bandwidthpools = accountClient.getVirtualDedicatedRacks()
# We are looking for the bandwidth pool
for item in bandwidthpools:
try:
print(item["name"])
#流量池可用总流量
print(client['SoftLayer_Network_Bandwidth_Version1_Allotment'].getTotalBandwidthAllocated(id=item['id']))
#流量池已用公网出站流量
print(client['SoftLayer_Network_Bandwidth_Version1_Allotment'].getOutboundPublicBandwidthUsage(id=item['id']))
#流量池预估当前账期公网出站流量
print(client['SoftLayer_Network_Bandwidth_Version1_Allotment'].getProjectedPublicBandwidthUsage(id=item['id']))
#判断流量池内流量当前账期内是否会超
print(client['SoftLayer_Network_Bandwidth_Version1_Allotment'].getProjectedOverBandwidthAllocationFlag(id=item['id']))
except SoftLayer.SoftLayerAPIError as e:
"""
If there was an error returned from the SoftLayer API then bomb out with the
error message.
"""
print("Unable to get bandwidth pool: \nfaultCode= %s, \nfaultString= %s" % (e.faultCode, e.faultString))
print("Done")
在上面的代码段里,我们用相应的用户取出了该用户账号下的所有流量池,并显示该流量池的可用总流量,已用外网流量,预估当前账期所用流量,以及流量是否会超等信息。 可以帮助客户方便的了解流量池内流量的使用情况。
我们也可以通过API批量管理服务器,例如取消机器,如下面代码所示
"""
Cancel servers from a list of IPs
"""
import SoftLayer
USERNAME = 'xxxxxxxxxxx'
API_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
client = SoftLayer.Client(username=USERNAME, api_key=API_KEY)
# The list of IPs from the servers you wish cancel
ipsToCancel = ['10.215.34.xxx', '10.215.34.xxx', '10.215.33.xxx', '10.215.33.xxx', '10.215.33.xxx', '10.215.33.xxx', '10.215.33.xxx', '10.215.33.xxx', '10.215.34.xxx', '10.215.33.xxx']
"""
Declare these to keep line lengths minimal
"""
accountClient = client['SoftLayer_Account']
billingItemClient = client['SoftLayer_Billing_Item']
"""
Add an object mask to retrieve our billing items related to the servers
http://softlayer.github.io/reference/datatypes/SoftLayer_Hardware_Server
for a list of the relational properties you can retrieve along with hardware.
"""
objectMask = 'mask[billingItem]'
# Make the call to retrieve our hardware records along with their billing item.
servers = accountClient.getHardware(mask=objectMask)
# We are looking for the server which has the desired IP to delete it.
for server in servers:
if 'primaryBackendIpAddress' in server.keys():
if server['primaryBackendIpAddress'] in ipsToCancel:
if 'billingItem' in server.keys():
billingId = server['billingItem']['id']
try:
result = billingItemClient.cancelItem(id=billingId)
print(result)
except SoftLayer.SoftLayerAPIError as e:
"""
If there was an error returned from the SoftLayer API then bomb out with the
error message.
"""
print("Unable to cancel the server: \nfaultCode= %s, \nfaultString= %s" % (e.faultCode, e.faultString))
print("Done")
在上面的代码段中,会取消在ipsToCancel这个数组里的IP所对应的物理机,如果要取消虚拟机,只需要将
servers = accountClient.getHardware(mask=objectMask)
改成
servers = accountClient.getVirtualGuests(mask=objectMask)
即可,accountClient.getVirtualGuests()可获得用户账号下的所有虚拟机。
这里只针对IBM Cloud API做了一些简单的说明,大家可以在下面的网页看到更多的代码样例:
https://sldn.softlayer.com/python/
附录
经典网络架构下的IBM Cloud IaaS API: https://sldn.softlayer.com/
IBM Cloud API(包括VPC): https://cloud.ibm.com/docs?tab=api-docs&