├── README.md └── token.py /README.md: -------------------------------------------------------------------------------- 1 | # netcore-api 2 | 3 | 练习项目 4 | 5 | 参考网易蜂巢文档调用创建 删除 获取镜像等操作 6 | 7 | 8 | -------------------------------------------------------------------------------- /token.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | #-*-coding:utf-8 3 | 4 | import requests 5 | import json 6 | 7 | 8 | # 获取token 9 | def returnToken(app_key,app_secret): 10 | 11 | api = 'https://open.c.163.com/api/v1/token' 12 | 13 | payload = {"app_key":app_key,"app_secret":app_secret} 14 | headers = { 15 | 'content-type': "application/json", 16 | 'cache-control': "no-cache" 17 | } 18 | 19 | response = requests.request("POST", api, data=json.dumps(payload), headers=headers) 20 | token = response.text # 输出的是 21 | tokenInfo = json.JSONDecoder().decode(token) # 转换成json格式 22 | return tokenInfo["token"] 23 | 24 | #获取镜像 25 | def getpubimages(token): 26 | 27 | api ='https://open.c.163.com/api/v1/vm/publicimages?pageSize=4&pageNum=1&keyword=os&Type=linux' 28 | headers = { 29 | 'content-type': "application/json", 30 | 'cache-control': "no-cache", 31 | 'Authorization':'Token '+token 32 | } 33 | response = requests.request("GET",api,headers=headers) 34 | tokenInfo = json.JSONDecoder().decode(response.text) 35 | imagesid = tokenInfo["images"][0]['imageId'] 36 | return imagesid 37 | 38 | 39 | # 创建虚拟机 40 | def createvm(tokenValue,instance_name,ssh_key_names,image_id,cpu_weight,memory_weight,ssd_weight): 41 | 42 | api = 'https://open.c.163.com/api/v1/vm' 43 | payload = { 44 | "bill_info":"HOUR", 45 | "server_info":{ 46 | "instance_name":instance_name, 47 | "ssh_key_names":[ssh_key_names], 48 | "image_id":image_id, 49 | "cpu_weight":cpu_weight, 50 | "memory_weight":memory_weight, 51 | "ssd_weight":ssd_weight, 52 | } 53 | } 54 | 55 | headers = { 56 | 'content-type': "application/json", 57 | 'cache-control': "no-cache", 58 | 'Authorization':'Token '+tokenValue 59 | } 60 | serveresponse = requests.request("POST", api, data=json.dumps(payload), headers=headers) 61 | serverid = json.JSONDecoder().decode(serveresponse.text) # 转换成json格式 62 | print serverid 63 | 64 | def listvm(): 65 | 66 | api = 'https://open.c.163.com/api/v1/vm/allInstanceInfo?pageSize=4&pageNum=1' 67 | headers = { 68 | 'cache-control': "no-cache", 69 | 'Authorization': 'Token ' + tokenValue 70 | } 71 | listvm = requests.request("GET", api, headers=headers) 72 | return listvm.json() 73 | 74 | def createsshkey(token,name): 75 | api = 'https://open.c.163.com/api/v1/secret-keys' 76 | headers = { 77 | 'content-type': "application/json", 78 | 'cache-control': "no-cache", 79 | 'Authorization': 'Token ' + token 80 | } 81 | payload = {"key_name": name} 82 | response = requests.request("POST", api, data=json.dumps(payload), headers=headers) 83 | sshkey = response.text # 输出的是 84 | sshKeyInfo = json.JSONDecoder().decode(sshkey) # 转换成json格式 85 | return sshKeyInfo 86 | 87 | def getsshkey(token): 88 | api = 'https://open.c.163.com/api/v1/secret-keys' 89 | headers = { 90 | 'content-type': "application/json", 91 | 'cache-control': "no-cache", 92 | 'Authorization': 'Token ' + token 93 | } 94 | response = requests.request("GET", api, headers=headers) 95 | 96 | sshkey = response.text # 输出的是 97 | sshKeyInfo = json.JSONDecoder().decode(sshkey) # 转换成json格式 98 | sshKeyInfo = str(sshKeyInfo[0]['name']) 99 | return sshKeyInfo 100 | 101 | 102 | 103 | 104 | 105 | tokenValue = returnToken(" "," ") 106 | print tokenValue 107 | instance_name = 'centos7' 108 | ssh_key_names = getsshkey(tokenValue) 109 | image_id =getpubimages(tokenValue) 110 | cpu_weight= 1 111 | memory_weight = 2 112 | ssd_weight = 20 113 | 114 | # createvm(tokenValue,instance_name,ssh_key_names,image_id,cpu_weight,memory_weight,ssd_weight) 115 | listvm() --------------------------------------------------------------------------------