从此可以使用"tmp_centos7"模板创建虚拟机了
API操作 #!/usr/bin/env python # -*- coding:utf-8 -*- # @Time : 2022/2/11 14:41 # @Author : XuLiLiang # @File : cloudstack-api.py import base64 import hashlib import hmac import json import requests from urllib import parse baseurl = 'http://10.10.10.5:8080/client/api?' api_key = 'g1Z0AutTl4Fbl3OeNX_rLJjaecOwMHkY3iUy6OiXlC6S3deP4s2NeLM2u8hGLqBHz509CHeC9eInPGvAqJ5KBg' secret_key = b'MiX6r_vBgO3c9ZUnadq8aOzHg18Y0FnHjP-NPLfCcS1wOsLYcoJdVXEU4aUK6reHjsB7Z4sP4pQbV8yLLmzIyA' class CloudApi: def __init__(self): self.baseurl = baseurl self.api_key = api_key self.secret_key = secret_key def GetSing(self, command,**kwargs): request = {} request['command'] = command request['response'] = 'json' request['apikey'] = self.api_key for k,v in kwargs.items(): request[k]=v request_str = '&'.join(['='.join([k, parse.quote_plus(request[k])]) for k in request.keys()]) sig_str = '&'.join(['='.join([k.lower(), parse.quote_plus(request[k].lower().replace('+', '%20'))]) for k in sorted(request.keys())]).encode("utf-8") sig = hmac.new(self.secret_key, sig_str, hashlib.sha1).digest() sig = base64.encodebytes(hmac.new(self.secret_key, sig_str, hashlib.sha1).digest()) sig = base64.encodebytes(hmac.new(self.secret_key, sig_str, hashlib.sha1).digest()).strip() sig = parse.quote_plus(base64.encodebytes(hmac.new(self.secret_key, sig_str, hashlib.sha1).digest()).strip()) req = self.baseurl + request_str + '&signature=' + sig return req def listClusters(self): req=self.GetSing('listClusters') s = requests.get(req) print(json.loads(s.content.decode("utf-8"))['listclustersresponse']['cluster']) def listVirtualMachines(self): req = self.GetSing('listVirtualMachines') s = requests.get(req) print(json.loads(s.content.decode("utf-8"))['listvirtualmachinesresponse']['virtualmachine']) def listTemplates(self): req = self.GetSing('listTemplates',templatefilter='all',aaa='123123') s = requests.get(req) response=json.loads(s.content.decode("utf-8"))['listtemplatesresponse']['template'] print(response) if __name__ == '__main__': api = CloudApi() api.listVirtualMachines() api.listClusters() api.listTemplates()cloudstack-4.1.5版本最全入门笔记【2022】 (5)
内容版权声明:除非注明,否则皆为本站原创文章。