def eip_delete(IPUID): ApiClient = UcloudApiClient(base_url, public_key, private_key) Parameters = {"Action": "ReleaseEIP", "EIPId": IPUID, "Region": "cn-bj2"} try: response = ApiClient.get("/", Parameters) except: log_write('', '\n', '', str(IPUID), 'EIP释放失败') sys.exit(0) response_data = json.loads(json.dumps(response)) if response_data["RetCode"] == 0: log_write('', '\n', '', str(IPUID), 'EIP释放成功') else: log_write('', '\n', '', str(IPUID), 'EIP释放失败') sys.exit(0)
def main(): while True: global ApiClient ApiClient = UcloudApiClient(base_url, public_key, private_key) if run_mode == 'manual': eipIdList = eipIdArray elif run_mode == 'auto': eipIdList = getAllEipId() # 获取所有EIPID elif run_mode == 'alarm': eipIdList = getAlarmEipId() # 获取告警的EIPID else: print 'You should choice one run_mode' adjustEip = list(set(eipIdList).difference( set(noAdjustEip))) # 剔除不参与的EIP。 for i in adjustEip: adjustBandwidth(i) time.sleep(durtime)
def getImage(ImageType="Base"): ApiClient = UcloudApiClient(base_url, public_key, private_key) Parameters = { "Action": "DescribeImage", "Region": region, "Zone":zone, "ImageType": ImageType, "OsType": 'Linux', "Offset": 0, "Limit": 1000, "ProjectId":project_id } response = ApiClient.get("/", Parameters) if 0 != response['RetCode']: print response exit(1) print response
def eip_list_relese(name): ApiClient = UcloudApiClient(base_url, public_key, private_key) Parameters = { "Action": "DescribeEIP", "ProjectId": "org-tgfjdn", "Region": "cn-bj2", "Limit": 300 } trynum = 0 success = False while trynum < 3 and not success: try: response = ApiClient.get("/", Parameters) success = True except: trynum += 1 if trynum != 3: time.sleep(5) else: log_write('\n', '\n', 'EIP获取列表失败时间:') sys.exit(0) response_data = json.loads(json.dumps(response)) for eip_data in response_data["EIPSet"]: if eip_data["Status"] == "free": a = name.decode('utf-8').encode('unicode_escape') b = eip_data["Name"].encode('unicode_escape') c = eip_data["Bandwidth"] d = eip_data["EIPId"] e = eip_data["EIPAddr"][0]["IP"] f = eip_data["CreateTime"] g = int(time.time() - 3600) if a == b and c == 1 and f < g: log_write('', ' ', str(e), str(d), '准备释放时间:') eip_delete(d) else: continue
def create(tag='', Bandwidth=2, ChargeType='Dynamic', PayMode='Traffic'): ApiClient = UcloudApiClient(base_url, public_key, private_key) Parameters = { "Action": "AllocateEIP", "Region": region, "OperatorName": 'Bgp', "Bandwidth": Bandwidth, "ChargeType": ChargeType, "PayMode": PayMode, "Tag": tag, "Remark": 'ip_Remark' } response = ApiClient.get("/", Parameters) #print response if 0 != response['RetCode']: print response exec(1) #print response if 'EIPSet' in response: return response['EIPSet'][0]['EIPAddr'][0]['IP'] raise TypeError("can not get host info")
def eip_delete(IPUID): ApiClient = UcloudApiClient(base_url, public_key, private_key) Parameters = {"Action": "ReleaseEIP", "EIPId": IPUID, "Region": "cn-bj2"} trynum = 0 success = False while trynum < 3 and not success: try: response = ApiClient.get("/", Parameters) success = True except: trynum += 1 if trynum != 3: time.sleep(5) else: log_write('\n', '\n', str(IPUID), '释放失败时间:') sys.exit(0) response_data = json.loads(json.dumps(response)) if response_data["RetCode"] == 0: log_write('', '\n', '释放成功时间:') else: log_write('', '\n', '释放失败时间:')
def eip_unbind(IPUID, UHOSTID): ApiClient = UcloudApiClient(base_url, public_key, private_key) Parameters = { "Action": "UnBindEIP", "EIPId": IPUID, "ResourceId": UHOSTID, "ResourceType": "uhost", "Region": "cn-bj2", "ProjectId": "org-tgfjdn" } try: response = ApiClient.get("/", Parameters) except: log_write('', '\n', '', str(IPUID), str(UHOSTID), 'EIP解绑失败') sys.exit(0) response_data = json.loads(json.dumps(response)) if response_data["RetCode"] == 0: log_write('', '\n', '', str(IPUID), str(UHOSTID), 'EIP解绑成功') else: log_write('', '\n', '', str(IPUID), str(UHOSTID), 'EIP解绑失败') sys.exit(0)
#!/usr/bin/env python # -*- coding: utf-8 -*- from sdk import UcloudApiClient from config import * import sys import json #实例化 API 句柄 if __name__ == '__main__': arg_length = len(sys.argv) ApiClient = UcloudApiClient(base_url, public_key, private_key) Parameters = { "Action": "CreateUHostInstance", "Region": "cn-north-03", "ImageId": "uimage-qiut5g", "LoginMode": "Password", "Password": "******" } response = ApiClient.get("/", Parameters) print json.dumps(response, sort_keys=True, indent=4, separators=(',', ': '))
def __init__(self): self.ApiClient = UcloudApiClient(base_url, public_key, private_key)
def __init__(self): self.api_client = UcloudApiClient(base_url, public_key, private_key, project_id)