import requests import json
def returnToken(app_key,app_secret):
api = 'https://open.c.163.com/api/v1/token'
payload = {"app_key":app_key,"app_secret":app_secret} headers = { 'content-type': "application/json", 'cache-control': "no-cache" }
response = requests.request("POST", api, data=json.dumps(payload), headers=headers) token = response.text tokenInfo = json.JSONDecoder().decode(token) return tokenInfo["token"]
def getpubimages(token):
api ='https://open.c.163.com/api/v1/vm/publicimages?pageSize=4&pageNum=1&keyword=os&Type=linux' headers = { 'content-type': "application/json", 'cache-control': "no-cache", 'Authorization':'Token '+token } response = requests.request("GET",api,headers=headers) tokenInfo = json.JSONDecoder().decode(response.text) imagesid = tokenInfo["images"][0]['imageId'] return imagesid
def createvm(tokenValue,instance_name,ssh_key_names,image_id,cpu_weight,memory_weight,ssd_weight):
api = 'https://open.c.163.com/api/v1/vm' payload = { "bill_info":"HOUR", "server_info":{ "instance_name":instance_name, "ssh_key_names":[ssh_key_names], "image_id":image_id, "cpu_weight":cpu_weight, "memory_weight":memory_weight, "ssd_weight":ssd_weight, } }
headers = { 'content-type': "application/json", 'cache-control': "no-cache", 'Authorization':'Token '+tokenValue } serveresponse = requests.request("POST", api, data=json.dumps(payload), headers=headers) serverid = json.JSONDecoder().decode(serveresponse.text) print serverid
def listvm():
api = 'https://open.c.163.com/api/v1/vm/allInstanceInfo?pageSize=4&pageNum=1' headers = { 'cache-control': "no-cache", 'Authorization': 'Token ' + tokenValue } listvm = requests.request("GET", api, headers=headers) return listvm.json()
def createsshkey(token,name): api = 'https://open.c.163.com/api/v1/secret-keys' headers = { 'content-type': "application/json", 'cache-control': "no-cache", 'Authorization': 'Token ' + token } payload = {"key_name": name} response = requests.request("POST", api, data=json.dumps(payload), headers=headers) sshkey = response.text sshKeyInfo = json.JSONDecoder().decode(sshkey) return sshKeyInfo
def getsshkey(token): api = 'https://open.c.163.com/api/v1/secret-keys' headers = { 'content-type': "application/json", 'cache-control': "no-cache", 'Authorization': 'Token ' + token } response = requests.request("GET", api, headers=headers)
sshkey = response.text sshKeyInfo = json.JSONDecoder().decode(sshkey) sshKeyInfo = str(sshKeyInfo[0]['name']) return sshKeyInfo
tokenValue = returnToken("","") print tokenValue instance_name = 'centos7' ssh_key_names = getsshkey(tokenValue) image_id =getpubimages(tokenValue) cpu_weight= 1 memory_weight = 2 ssd_weight = 20
listvm()
|