def update(self, project_name, new_name, new_owner, quota_enable, quota_capacity): ''' Makes REST API call and updates project name and owner Parameters: project_name: name of project new_name: new name of the project Returns: List of project resources in response payload ''' project_uri = self.project_query(project_name) request = dict() if(new_name and len(new_name) > 0): request["name"] = new_name if(new_owner and len(new_owner) > 0): request["owner"] = new_owner body = json.dumps(request) (s, h) = common.service_json_request(self.__ipAddr, self.__port, "PUT", Project.URI_PROJECT.format(project_uri), body) # update quota if(quota_enable is not None or quota_capacity is not None): from quota import Quota quota_obj = Quota(self.__ipAddr, self.__port) quota_obj.update(quota_enable, quota_capacity, "project", project_uri)
def tenant_quota_update(self, tenant, quota_enable, quota_gb): if tenant: tenant_id = self.tenant_query(tenant) else: tenant_id = self.tenant_getid() # update quota if quota_enable is not None or quota_gb is not None: from quota import Quota quota_obj = Quota(self.__ipAddr, self.__port) quota_obj.update(quota_enable, quota_gb, "tenant", tenant_id)
def tenant_list(args): obj = Tenant(args.ip, args.port) from quota import Quota quota_obj = Quota(args.ip, args.port) try: uris = obj.tenant_list() output = [] myid = obj.tenant_getid() tenant_details = obj.tenant_show(myid) # append quota attributes quota_obj.append_quota_attributes("tenant", myid, tenant_details) output.append(tenant_details) for uri in uris: uri_details = obj.tenant_show(uri["id"]) if uri_details: # append quota attributes quota_obj.append_quota_attributes("tenant", uri["id"], uri_details) output.append(uri_details) if len(output) > 0: if args.verbose: return common.format_json_object(output) elif args.long: from common import TableGenerator TableGenerator( output, ["module/name", "quota_current_capacity", "quota_gb", "description"] ).printTable() else: from common import TableGenerator TableGenerator(output, ["module/name"]).printTable() except SOSError as e: if e.err_code == SOSError.NOT_FOUND_ERR: raise SOSError(SOSError.NOT_FOUND_ERR, "Tenant list failed: " + e.err_text) else: raise e
def project_list(args): obj = Project(args.ip, args.port) from quota import Quota quota_obj = Quota(args.ip, args.port) try: from common import TableGenerator projects = obj.project_list(args.tenantname) records = [] for project in projects: project_uri = project['id'] proj_detail = obj.project_show_by_uri(project_uri) if (proj_detail): # append quota attributes quota_obj.append_quota_attributes("project", project_uri, proj_detail) if ("tenant" in proj_detail and "name" in proj_detail["tenant"]): del proj_detail["tenant"]["name"] records.append(proj_detail) if (len(records) > 0): if (args.verbose == True): return common.format_json_object(records) elif (args.largetable == True): TableGenerator(records, [ 'name', 'owner', 'quota_current_capacity', 'quota_gb', 'tags' ]).printTable() else: TableGenerator(records, ['name']).printTable() else: return except SOSError as e: raise e
def project_list(args): obj = Project(args.ip, args.port) from quota import Quota quota_obj = Quota(args.ip, args.port) try: from common import TableGenerator projects = obj.project_list(args.tenantname) records = [] for project in projects: project_uri = project['id'] proj_detail = obj.project_show_by_uri(project_uri) if(proj_detail): # append quota attributes quota_obj.append_quota_attributes("project", project_uri, proj_detail) if("tenant" in proj_detail and "name" in proj_detail["tenant"]): del proj_detail["tenant"]["name"] records.append(proj_detail) if(len(records) > 0): if(args.verbose == True): return common.format_json_object(records) elif(args.largetable == True): TableGenerator(records, ['name', 'owner', 'quota_current_capacity', 'quota_gb', 'tags']).printTable() else: TableGenerator(records, ['name']).printTable() else: return except SOSError as e: raise e
def get_rse(self, **kwargs): """ This function, based on the option parameters, allow to choose whether to overwrite or to leave the current quota. Return a list of Quota objects. """ rses_list = [] if kwargs is not None: try: rse = self.get_rse_by_country(kwargs['institute'], kwargs['institute_country']) except RSENotFound: raise option = kwargs['option'] quota = None if rse is None: print('User ' + kwargs['username'] + 'no longer belong to CMS (institute is empty)\n') return print("Getting quota") if option == 'reset-all': quota = self.default_quota elif option == 'set-new-only': print('Set new only for', kwargs['username'], rse) quota = self.client.get_local_account_limit( account=kwargs['username'], rse=rse)[rse] print('Quota for %s at %s is %s', (kwargs['username'], quota, rse)) if quota is None: message = "[quota SET] User {0} has now {1} bytes at the {2} site\n".format( kwargs['username'], quota, rse) print(message) quota = self.default_quota elif quota != self.default_quota: message = "[quota ALREADY SET] User {0} has already {1} bytes at the {2} site and you can not " \ "overwrite default quota of {3} bytes. " \ "Use 'reset-all' option.\n".format(kwargs['username'], quota, rse, self.default_quota) print(message) raise Exception print("Making Quota object with %s and %s" % (rse, quota)) rse_quota = Quota(rse, quota) rses_list.append(rse_quota) return rses_list return
def quota(self): return Quota(self)
def post(self): try: tmpl_name = self.get_argument('template') self.sr_uuid = self.get_argument('storage') self.net_uuid = self.get_argument('network') self.vdi_size = self.get_argument('vdi_size') int(self.vdi_size) self.ram_size = self.get_argument('ram_size') int(self.ram_size) self.name_label = self.get_argument('name_label') except Exception as e: self.set_status(400) self.log.error(f"Exception: {e}") self.write({'status': 'error', 'message': 'bad request'}) self.finish() return with self.conn: db = r.db(opts.database) if not isinstance(self.user_authenticator, AdministratorAuthenticator): # Prevent provisioning if quotas are not met quota = Quota(self.user_authenticator) usage = quota.space_left_after_disk_creation(int(self.vdi_size)*1024*1024, f'users/{self.user_authenticator.get_id()}') if usage and usage < 0: self.write({'status': 'storage quota exceeded', 'message': f'storage limit will be exceeded by {-usage} bytes'}) self.finish() return tmpls = db.table('tmpls').run() self.template = None if not tmpl_name: self.set_status(400) self.write({'status': 'bad argument: template', 'message': 'bad request'}) self.log.error("Client supplied no template") self.finish() for tmpl in tmpls: if tmpl['uuid'] == tmpl_name or \ tmpl['name_label'] == tmpl_name: self.template = tmpl break if not self.template: self.set_status(400) self.write({'status': 'no such template', 'message': 'template ' + tmpl_name}) self.log.error("Client supplied wrong template name: {0}".format(tmpl_name)) self.finish() return self.override_pv_args = self.get_argument('override_pv_args', None) self.mode = 'hvm' if self.template['hvm'] else 'pv' self.os_kind = self.template['os_kind'] if 'os_kind' in self.template and self.template['os_kind'] else None self.log.info("Creating VM: name_label %s; os_kind: %s" % (self.name_label, self.os_kind)) if self.os_kind is None: self.iso = self.get_argument('iso') self.scenario_url = None self.mirror_url = None self.ip_tuple = None self.hostname = None self.username = None self.password = None self.fullname = None self.partition = None else: self.iso = None self.hostname = self.get_argument('hostname') self.mirror_url = self.get_argument('mirror_url', None) ip = self.get_argument('ip', '') gw = self.get_argument('gateway', '') netmask = self.get_argument('netmask', '') dns0 = self.get_argument('dns0', '') dns1 = self.get_argument('dns1', '') if not ip or not gw or not netmask: self.ip_tuple = None else: self.ip_tuple = [ip, gw, netmask] if dns0: self.ip_tuple.append(dns0) if dns1: self.ip_tuple.append(dns1) self.username = self.get_argument('username') self.password = self.get_argument('password') self.fullname = self.get_argument('fullname', default=None) self.partition = self.get_argument('partition', default='auto') try: self.vcpus = self.get_argument('vcpus', default=1) except ValueError: self.vcpus = 1 self.taskid = str(uuid.uuid4()) self._run_task = self.taskid # for on_finish tornado.ioloop.IOLoop.current().run_in_executor(self.executor, self.createvm) self.write(json.dumps({'task': self.taskid})) self.finish()