import requests,json,time controller_ip=input("请输入访问openstack平台控制节点IP地址:(xx.xx.xx.xx)\n") try: url = f"http://{controller_ip}:5000/v3/auth/tokens" body = {"auth": {"identity": {"methods": ["password"], "password": { "user": {"domain": {"name": "demmo"}, "name": "admin", "password": "000000"}}}, "scope": {"project": {"domain": {"name": "demmo"}, "name": "admin"}}}} headers = { "Content-Type": "application/json", } Token = requests.post(url, data=json.dumps(body), headers=headers).headers['X-Subject-Token'] headers = { "X-Auth-Token": Token } except Exception as e: print(f"获取Token值失败,请检查访问云主机控制节点IP是否正确?输出错误信息如下:{str(e)}") exit(0) class user_api: def __init__(self, hands:dict, resurl:str): self.headers = hands self.resurl = resurl #获取用户列表 def user_list(): result = json.loads(requests.get(self.resurl, headers=self.headers).text) return result #根据用户名获得用户id def user_get_id(self, user_name:str): result = json.loads(requests.get(self.resurl, headers=self.headers).text) for item in result['users']: if(item['name']==user_name): return item['id'] #获取用户的详细信息 def user_detail(self, user_name:str): user_id='' result = json.loads(requests.get(self.resurl, headers=self.headers).text) for item in result['users']: if(item['name']==user_name): user_id=item['id'] break url = self.resurl+'/'+user_id result=json.loads(requests.get(url, headers=self.headers).text) return result #创建一个用户 def user_create(self, user_name, user_password): self.headers['Content-Type']="application/json" body={ "user": { "domain": {"name": "demmo"}, "project": {"domain": {"name": "demmo"}, "name": "admin"}, "name": user_name, "password":user_password } } status_code=requests.post(self.resurl, data=json.dumps(body), headers=self.headers).text print(status_code) user_api = user_api(headers,f"http://{controller_ip}:35357/v3/users") user_api.user_create(user_name='test1',user_password='111111') print(user_api.user_detail(user_name='test1'))
openstack学习之路-python运维(user)
最新推荐文章于 2023-09-13 15:05:45 发布