def check_instanceTypes(self, regionId, az, instanceTypes=None): ''' 查看实例库存 :param regionId: 地域 :param az: 可用区 :param instanceTypes: 实例规格列表 :return: 库存dict ''' # 过滤条件:指定规格类型列表+可用区 myFilter = [] filter_1 = Filter(name='instanceTypes', operator='eq', values=instanceTypes) myFilter.append(filter_1) filter_2 = Filter(name='az', operator='eq', values=az) myFilter.append(filter_2) try: # 指定参数 myParam = DescribeInstanceTypesParameters(regionId) myParam.setFilters(myFilter) myRequest = DescribeInstanceTypesRequest(myParam) myClient = VmClient(self.credential) # 发送请求 resp = myClient.send(myRequest) if resp.error is not None: logger.error(resp.error.message) except Exception, e: logger.exception(e) sys.exit('error!获取云主机库存失败!')
def check_instance_status(self, regionId, instanceIds): ''' 获取云主机状态信息 :param regionId: 区域 :param instanceIds: 云主机列表 :return: 云主机状态dict ''' # 云主机ID作为过滤条件 myFilter = [ Filter(name='instanceId', operator='eq', values=instanceIds) ] try: myParam = DescribeInstancesParameters(regionId) myParam.setFilters(myFilter) myRequest = DescribeInstancesRequest(myParam) myClient = VmClient(self.credential) # 发送请求 resp = myClient.send(myRequest) if resp.error is not None: logger.error(resp.error.message) # 从返回结果获取云主机状态信息 instanceStatus = {} for ins in resp.result['instances']: instanceStatus[ins['instanceId']] = ins['status'] except Exception, e: logger.exception(e) sys.exit('error!获取云主机状态失败!')
def getIpFromEipId(self, regionId, eipList): ''' 从弹性IP的id获得IP地址 :param regionId: 地域 :param eipList: 弹性IP列表 :return: IP地址列表 ''' try: # 过滤条件 myFilter = Filter('elasticIpIds', eipList) myParam = DescribeElasticIpsParameters(regionId) myParam.setFilters([myFilter]) myRequest = DescribeElasticIpsRequest(myParam) # 发送请求 eipClient = VpcClient(self.credential) resp = eipClient.send(myRequest) if resp.error is not None: logger.error(resp.error.message) ipList = [ ip['elasticIpAddress'].encode('utf-8') for ip in resp.result['elasticIps'] ] except Exception, e: logger.exception(e) sys.exit('error!获取云主机状态失败!')
def getEipIdFromIp(self, regionId, ipAddress): ''' 从公网IP地址获得弹性IP的id :param regionId: 区域 :param ipAddress: 公网ip地址 :return: 弹性IP的id ''' try: ipList = [ ipAddress ] # 以IP地址作为过滤条件 newFilter = Filter(name='elasticIpAddress', values=ipList) myParam = DescribeElasticIpsParameters(regionId) myParam.setFilters([newFilter]) myRequest = DescribeElasticIpsRequest(myParam) myClient = VpcClient(self.credential) # 发送请求 resp = myClient.send(myRequest) if resp.error is not None: logger.error(resp.error.message) eipId = resp.result['elasticIps'][0]['elasticIpId'] except Exception, e: logger.exception(e) sys.exit('error!获取弹性IP的id失败!')
from jdcloud_sdk.services.vpc.client.VpcClient import * from jdcloud_sdk.services.common.models.Filter import * from jdcloud_sdk.services.vpc.apis.DescribeElasticIpsRequest import * import json access_key = 'ak' secret_key = 'sk' regionId = 'cn-north-1' myCredential = Credential(access_key, secret_key) myClient = VpcClient(myCredential) # List of all the EIP IDs eipList = ['fip-tj98zpg0ym', 'fip-0rsd6husd2', 'fip-7quu2vwtmu'] # Filter by EIP ID myFilter = Filter('elasticIpIds', eipList) myParam = DescribeElasticIpsParameters(regionId) myParam.setFilters([myFilter]) myRequest = DescribeElasticIpsRequest(myParam) resp = myClient.send(myRequest) # output all the info of EIPs print json.dumps(resp.result, indent=2) eipID = [] # get the public IPs by order for elasticIp in resp.result['elasticIps']: eipID.append(elasticIp['elasticIpAddress'].encode('utf-8')) print eipID
# -*- coding: utf-8 -*- from jdcloud_sdk.core.credential import * from jdcloud_sdk.services.common.models.Filter import * from jdcloud_sdk.services.vm.client.VmClient import * from jdcloud_sdk.services.vm.apis.DescribeInstanceTypesRequest import * import json access_key = 'ak' secret_key = 'sk' regionId = 'cn-east-2' azs = ['cn-east-2a', 'cn-east-2b'] instanceTypes = ['g.s1.small', 'g.s1.micro'] myFilter = [] filter_1 = Filter(name='instanceTypes', operator='eq', values=instanceTypes) myFilter.append(filter_1) filter_2 = Filter(name='az', operator='eq', values=azs) myFilter.append(filter_2) myParam = DescribeInstanceTypesParameters(regionId=regionId) myParam.setFilters(myFilter) myRequest = DescribeInstanceTypesRequest(parameters=myParam) myCredential = Credential(access_key, secret_key) myClient = VmClient(myCredential) print 'Sending request...' resp = myClient.send(myRequest)
# -*- coding: utf-8 -*- from jdcloud_sdk.core.credential import Credential from jdcloud_sdk.services.vpc.client.VpcClient import * from jdcloud_sdk.services.common.models.Filter import * from jdcloud_sdk.services.vpc.apis.DescribeElasticIpsRequest import * import json access_key = 'ak' secret_key = 'sk' regionId = 'cn-north-1' myCredential = Credential(access_key, secret_key) myClient = VpcClient(myCredential) # ip address in a list, Only 1 IP address can be accepted once ipList = ['116.196.108.44'] # filter by EIP address newFilter = Filter(name='elasticIpAddress', values=ipList) myParam = DescribeElasticIpsParameters(regionId) myParam.setFilters([newFilter]) myRequest = DescribeElasticIpsRequest(myParam) resp = myClient.send(myRequest) print json.dumps(resp.result, indent=2) # get the EIP id from the response: fip-tj98zpg0ym myEipID = resp.result['elasticIps'][0]['elasticIpId']