def list_user_tenants(self, user_id): rs = yield list_user_tenants_role(user_id, is_admin=False) result = [] for i in rs: tenant = yield get_tenant_by_id(i['tenant']) i['tenant'] = tenant result.append(i) self.response(Response(result=result, total=len(result)))
def add_users(self, tenant_id, body): tenant = yield identify.get_tenant_by_id(tenant_id) for user_name in body: user = yield user_mapping(user_name) yield identify.add_tenant_user(tenant_id, user.get("id")) optLog.write(self.request, Type.TENANT, tenant['name'], Operator.ADD_USER, user['displayname']) self.response(Response())
def delete_tenant_host(self, tenant_id, host_id): yield delete_tenant_host(tenant_id, int(host_id)) host_info = yield list_simple_hosts(host_id=host_id) for host in host_info: pass tenant = yield get_tenant_by_id(tenant_id) optLog.write(self.request, Type.TENANT, tenant['name'], Operator.CONFIG_TENANT_DELETE_HOST, host['name']) self.response(Response())
def set_tenant_subnet(self, tenant_id, body): subnet_ids = body['subnet_ids'] yield set_tenant_subnet(tenant_id, subnet_ids) tenant = yield get_tenant_by_id(tenant_id) for subnet_id in subnet_ids: subnet = yield get_subnet(subnet_id) optLog.write(self.request, Type.TENANT, tenant['name'], Operator.CONFIG_NETWORK, subnet['name']) self.response(Response())
def set_quota(self, tenant_id, body): tenant = yield identify.get_tenant_by_id(tenant_id) for quota in body: yield identify.update_quota_limit(tenant_id, name=quota['quota_name'], limit=quota['quota_limit']) optLog.write(self.request, Type.TENANT, tenant['name'], Operator.MODIFY_QUOTA, '') self.response(Response())
def delete_tenant(self, tenant_id, internal=False): tenant = yield identify.get_tenant_by_id(tenant_id) if not tenant: raise TenantNotExist if internal: try: for name in identify.get_quota_names(): q = yield identify.get_quota(tenant_id, name) if q and q.get("quota_used") != 0: region = yield rg.list_region( CONF.keystone.region_name) raise TenantDeleteFailed(region[0].get("displayname")) yield identify.delete_quotas(tenant_id) yield delete_tnr_from_ecloud(tenant_id) except Exception as e: LOG.error("delete tenant error:%s" % e) LOG.error(trace()) raise e else: regions = yield rg.list_region() for region in regions: if region["region"] == CONF.keystone.region_name: for name in identify.get_quota_names(): q = yield identify.get_quota(tenant_id, name) if q.get("quota_used") != 0: raise TenantDeleteFailed() continue servers_url = "%s/tenant/%s?internal=true" % \ (region['url'], tenant_id) try: res = yield async_request(url=servers_url, token=get_token(self.request), body=None, method="DELETE") if res and res.get("success") is False and res.get( "msg") == TenantDeleteFailed.msg: raise TenantDeleteFailed except Exception as e: LOG.error( "delete tenant quota from another region error:%s" % e) LOG.error(trace()) if e.message == TenantDeleteFailed.msg: raise TenantDeleteFailed(args=[region['displayname']]) raise rg.RegionException(args=[region['displayname']]) try: yield identify.delete_quotas(tenant_id) yield delete_tnr_from_ecloud(tenant_id) yield identify.delete_tenant(tenant_id) except Exception as e: LOG.error("delete tenant error:%s" % e) LOG.error(trace()) region = yield rg.list_region(CONF.keystone.region_name) raise TenantDeleteFailed(region[0].get("displayname")) optLog.write(self.request, Type.TENANT, tenant['name'], Operator.DELETE, '') self.response(Response())
def set_tenant_hosts(self, tenant_id, body): hosts = body.get("host_ids") if not tenant_id: raise InvalidateParam hosts_info = yield list_simple_hosts(host_id=hosts) tenant = yield get_tenant_by_id(tenant_id) yield set_tenant_hosts(tenant_id, hosts) for host in hosts_info: optLog.write(self.request, Type.TENANT, tenant['name'], Operator.CONFIG_TENANT_ADD_HOST, host['name']) self.response(Response())
def set_tenant_user_role(self, user_id, tenant_id, body): user = yield identify.get_user_by_id(user_id) yield identify.set_user_role(user_id, body['role'], tenant_id) t = yield get_tenant_by_id(tenant_id) if body['role'] == 'tenant_admin': log.write(self.request, log.Type.TENANT, t['name'], log.Operator.SET_TENANT_ROLE, user['displayname']) else: log.write(self.request, log.Type.TENANT, t['name'], log.Operator.CANCEL_TENANT_ROLE, user['displayname']) self.response(Response())
def set_tenant_subnet_ips(self, subnet_id, tenant_id, body): ips = body.get("ips") if not tenant_id: raise InvalidateParam subnet = yield get_subnet(subnet_id) if not subnet: raise SubNetNotExist tenant = yield get_tenant_by_id(tenant_id) yield set_tenant_subnet_ips(subnet_id, tenant_id, ips) optLog.write(self.request, Type.TENANT, tenant['name'], Operator.CONFIG_TENANT_IP, subnet['name']) self.response(Response())
def update_tenant(self, tenant_id, body): tenant = yield identify.get_tenant_by_id(tenant_id) if not tenant: raise TenantNotExist db = dbpools.get_keystone() cur = yield db.execute("select name from project where id = %s ", (tenant_id, )) old_tenant_name = cur.fetchone() t = yield identify.update_tenant(tenant_id, **body) optLog.write(self.request, Type.TENANT, old_tenant_name['name'], Operator.UPDATE, t['name']) self.response(Response(result=t))
def remove_users(self, tenant_id): users = self.get_argument('users').split(',') tenant = yield identify.get_tenant_by_id(tenant_id) usernames = [] for user_id in users: user = yield identify.get_user_by_id(user_id) role = yield identify.get_user_role(user_id, tenant_id) yield identify.remove_tenant_user(tenant_id, user_id, role['id']) usernames.append(user['displayname']) # clear volumes of this user yield clear_user_volumes(tenant_id, user_id) # clear vms of this user yield clear_vm_user(tenant_id, user_id) optLog.write(self.request, Type.TENANT, tenant['name'], Operator.REMOVE_USER, user['displayname']) self.response(Response())
def list_subnets(network_id=None, tenant_id=None): result = [] subnet_ids = [] if tenant_id: subnet_ids = yield query_tenant_subnets(tenant_id) if not subnet_ids: raise gen.Return(result) try: subnets_tenants = yield query_subnet_tenants(subnet_ids) vt_map = struct_convert(subnets_tenants, "subnet_id", "tenant_id") subnets = yield get_subnet_db(network_id=network_id, subnet_ids=subnet_ids) subnet_ids = [t_item['id'] for t_item in subnets] ips_counts = yield count_ip_total(subnet_ids) ips_used_counts = yield count_vlan_ip_used(subnet_ids) dhcp_counts = yield count_vlan_ip_used(subnet_ids, dhcp=True) dns_infos = yield list_dns(subnet_ids) dns_map = struct_convert(dns_infos, "subnet_id", "dns") for subnet_item in subnets: tenants = [] if vt_map and subnet_item['id'] in vt_map: tenant_ids = vt_map[subnet_item['id']] for id_item in tenant_ids: tenant_detail = yield get_tenant_by_id(id_item) if tenant_detail: tenants.append({"id": id_item, "name": tenant_detail["name"]}) result.append({ "id": subnet_item['id'], "network_id": subnet_item['network_id'], "network_name": subnet_item['network_name'], "name": subnet_item['name'], "cidr": subnet_item['cidr'], "gateway": subnet_item['gateway'], "dns": dns_map[subnet_item["id"]] if dns_map and subnet_item["id"] in dns_map else [], "ip_use": ips_used_counts[subnet_item['id']] if subnet_item['id'] in ips_used_counts else 0, "vm_counts": ips_used_counts.get(subnet_item['id'])-1 if dhcp_counts.get(subnet_item['id'], 0)else ips_used_counts.get(subnet_item['id'], 0), "ip_total": ips_counts[subnet_item['id']] if subnet_item['id'] in ips_counts else 0, "tenants": copy.deepcopy(tenants) }) except Exception, e: LOG.error("list subnet error: %s" % e) raise e
def get_tenant_ips(subnet_id, tenant_id): """ :param subnet_id: :param tenant_id: :return: { "name":"tenant_name", "id":"tenant_id" "ippools":[{"start":"","end":""} ....] "ipavailable":[{ }] "ipused":[{ "ip": "192.168.1.102", "used": true, "dhcp": true, "vm": "vm_name" "port": "port_id" }] } """ try: tenant_info = yield query_subnet_tenant_all(subnet_id=subnet_id, tenant_id=tenant_id) tenants = [] tenant_dict = {} for item in tenant_info: tenant_detail = yield get_tenant_by_id(item["tenant_id"]) if tenant_detail: tenant_dict = {"id": tenant_detail["id"], "name": tenant_detail["name"], "ippools": json.loads(item["ippools"]) if item["ippools"] else []} network_used_ips = yield query_subnet_ips_used_neutron(subnet_id, tenant_id) ips = [item["ip"] for item in network_used_ips] tenant_dict["ipavailable"] = yield _split_ippools(tenant_dict["ippools"][:], ips) tenants.append(tenant_dict) if tenant_id and tenant_dict: tenant_dict["ipused"] = yield _get_used_ip_info(subnet_id, tenant_id) tenants = tenant_dict except Exception, e: LOG.error("get ips of tenant error: %s" % e) raise e
def gen_out_volume(volume, metadata): """ :param volume: :param metadata: :return: """ tenant_id = volume["tenant_id"] new_tenant = yield get_tenant_by_id(tenant_id) user_id = metadata.get('user', None) new_user = {} if user_id: try: new_user = yield get_user_by_id(user_id) except Exception: metadata["user"] = "" attachments = [] if volume["attatchments_id"]: attach = { "id": volume["attatchments_id"], "vm_id": volume["vm_id"], "volume_id": volume["id"] } attachments.append(attach) volume_status = metadata["status"] if "status" in metadata and metadata[ "status"] else volume['status'] recover_status = metadata.get("recover_status", "") out_volume = { "id": volume['id'], "name": volume['name'], "size_gb": volume['size_gb'], "status": volume_status, "recover-status": recover_status, "location": volume['host'], "type": volume["type"], "attachments": attachments, "tenant": new_tenant, "user": new_user, "metadata": metadata, "created_at": datetimeUtils.time2epoch(volume['created_at']) } raise gen.Return(out_volume)
vm_control_state = vm_control_task_dict.get(server_id) meta_item = meta.get(server_id, {}) meta_status = meta_item.get("status", "") # 检查status状态为uploading时,查看镜像是否创建完成,如果完成删除云主机uploading状态。 if meta_status == VM_STATUS_UPLOAD: upt_image_id = meta_item["upt_image_id"] image_info = yield image_module.get_image(upt_image_id) if image_info and image_info["status"] == "active": yield del_server_meta(server_id, ["status", "upt_image_id"]) meta_status = None status = gen_server_status(server_item["vm_state"], server_item["task_state"], vm_control_state, meta_status) tenant = yield get_tenant_by_id(server_item["tenant_id"]) network = {} networks = [] try: flag_vlan = True flag_subnet = True networks = network_nics.get(server_id) for network_item in networks: if vlan_id and vlan_id == network_item['id']: flag_vlan = False if subnet_id and network_item["ip"] in vm_ips: flag_subnet = False if network_item["name"] not in network: network[network_item["name"]] = [] network[network_item["name"]].append(network_item["ip"]) except Exception:
def get_by_id(self, tenant_id): t = yield identify.get_tenant_by_id(tenant_id) self.response(Response(result=t))