def create_eip(self, regionId, bandwidthMbps, provider, count): ''' 创建多个弹性IP :param regionId: 区域ID :param bandwidthMbps: 弹性IP带宽 :param provider: bgp/no_bgp :param count: 弹性IP数量 :return: 弹性IP列表 ''' try: eipSpec = ElasticIpSpec(bandwidthMbps=bandwidthMbps, provider=provider) eipParam = CreateElasticIpsParameters(regionId=regionId, maxCount=count, elasticIpSpec=eipSpec) eipRequest = CreateElasticIpsRequest(parameters=eipParam) eipClient = VpcClient(self.credential) resp = eipClient.send(eipRequest) if resp.error is not None: logger.error(resp.error.message) # 从返回结果获取弹性IP eipList = [ eip.encode('utf-8') for eip in resp.result['elasticIpIds'] ] except Exception, e: logger.exception(e) sys.exit('error!创建弹性IP失败!')
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失败!')
def __init__(self, access_key, secret_key, regionId, bandwidthMbps, provider, elasticIpAddress=None, maxCount=1, chargeSpec=None): self.regionId = regionId self.bandwidthMbps = bandwidthMbps # bgp or no_bgp self.provider = provider # 指定弹性ip地址进行创建,当申请创建多个弹性ip时,必须为空 self.elasticIpAddress = elasticIpAddress # 购买弹性ip数量;取值范围:[1,100] self.maxCount = maxCount self.chargeSpec = chargeSpec self.credential = Credential(access_key, secret_key) self.client = VpcClient(self.credential) print 'Client created...'
from jdcloud_sdk.core.credential import Credential from jdcloud_sdk.services.vpc.client.VpcClient import VpcClient from jdcloud_sdk.services.charge.models.ChargeSpec import * from jdcloud_sdk.services.vpc.models.ElasticIpSpec import * from jdcloud_sdk.services.vpc.apis.CreateElasticIpsRequest import * access_key = 'ak' secret_key = 'sk' regionId = 'cn-north-1' bandwidthMbps = 2 provider = 'bgp' maxCount = 1 myCredential = Credential(access_key, secret_key) myClient = VpcClient(myCredential) myEipSpec = ElasticIpSpec(bandwidthMbps, provider) myEipParam = CreateElasticIpsParameters(regionId, maxCount, myEipSpec) myEipRequest = CreateElasticIpsRequest(myEipParam) resp = myClient.send(myEipRequest)
def getVpcClient(accessKey, secretKey): credential = Credential(accessKey, secretKey) logger = Logger(2) config = Config(timeout=1000) client = VpcClient(credential, logger=logger, config=config) return client
class NewElasticIPs(object): ''' Create 1 or more public IPs ''' def __init__(self, access_key, secret_key, regionId, bandwidthMbps, provider, elasticIpAddress=None, maxCount=1, chargeSpec=None): self.regionId = regionId self.bandwidthMbps = bandwidthMbps # bgp or no_bgp self.provider = provider # 指定弹性ip地址进行创建,当申请创建多个弹性ip时,必须为空 self.elasticIpAddress = elasticIpAddress # 购买弹性ip数量;取值范围:[1,100] self.maxCount = maxCount self.chargeSpec = chargeSpec self.credential = Credential(access_key, secret_key) self.client = VpcClient(self.credential) print 'Client created...' def setChargeSpec(self, chargeMode=None, chargeUnit=None, chargeDuration=None): # chargeMode: prepaid_by_duration:预付费,postpaid_by_usage:按用量后付费,postpaid_by_duration:按配置后付费,默认为postpaid_by_duration self.chargeSpec = ChargeSpec(chargeMode, chargeUnit, chargeDuration) print 'ChargeSpec created...' print 'ChargeMode: ' + self.chargeSpec.chargeMode def createElasticIps(self): if self.maxCount > 1 and self.elasticIpAddress is not None: print 'If you want to create more than 1 elastic IP, please make elasticIpAddress is None!' return 1 elasticIpSpec = ElasticIpSpec(bandwidthMbps=self.bandwidthMbps, provider=self.provider, chargeSpec=self.chargeSpec) elasticIpParam = CreateElasticIpsParameters(self.regionId, self.maxCount, elasticIpSpec) if self.elasticIpAddress is not None: elasticIpParam.setElasticIpAddress(self.elasticIpAddress) newRequest = CreateElasticIpsRequest(elasticIpParam) resp = self.client.send(newRequest) if resp.error is not None: print resp.error.code, resp.error.message my_resp = [] # from unicode to utf8 for i in resp.result['elasticIpIds']: my_resp.append(i.encode("utf-8")) return my_resp