def put(self, class_name): """Update a class.""" context = restcomm.extract_context_from_environ() LOG.info("Update quota class [class_name=%s]" % class_name) if not context.is_admin: pecan.abort(403, _('Admin required')) if not request.body: pecan.abort(400, _('Body required')) quota_class_set = eval(request.body).get('quota_class_set') if not quota_class_set: pecan.abort(400, _('Missing quota_class_set in the body')) utils.validate_quota_limits(quota_class_set) for key, value in six.iteritems(quota_class_set): try: db_api.quota_class_update(context, class_name, key, value) except exceptions.QuotaClassNotFound: db_api.quota_class_create(context, class_name, key, value) values = db_api.quota_class_get_all_by_name(context, class_name) return self._format_quota_set(class_name, values)
def get(self, project_id, action=None): """Get quota of a tenant. :param project_id: It's UUID of the project. Only specified quota details can be viewed using this param. :param action: Optional. If provided, it can be 'defaults' or 'detail' defaults - returns the quotas limits from the conf file. detail - returns the current quota usages of the tenant """ context = restcomm.extract_context_from_environ() quota = collections.defaultdict(dict) result = collections.defaultdict(dict) if not uuidutils.is_uuid_like(project_id): pecan.abort(400) enforce = enf.enforce('kingbird:get_all_quota', context) try: if enforce or (project_id == context.project)\ or (action == 'defaults'): result = self.get_quota(context, project_id, action) quota['quota_set'] = result return quota else: pecan.abort(403, _('Admin required ')) except exceptions.InternalError: pecan.abort(400, _('Error while requesting usage'))
def put(self, project_id, target_project_id, action=None): """Update quota limits for a project. If it fails, Then creates a new entry in DB for that project. :param project_id: It's UUID of the project. :param target_project_id: It's UUID of the project. Note: In v1.0 it can be defaults sometimes. Only specified tenant quota is retrieved from database using this param. :param action: Optional. If provided, it can be 'detail' detail - Gets detail quota usage for the specified tenant. """ context = restcomm.extract_context_from_environ() valid_project_id = uuidutils.is_uuid_like(project_id) if not context.is_admin: pecan.abort(403, _('Admin required')) if not valid_project_id: pecan.abort(400, _('Invalid request URL')) if project_id != context.project and not context.is_admin: pecan.abort(400, _('Invalid request URL')) if not uuidutils.is_uuid_like(target_project_id): pecan.abort(400, _('Invalid request URL')) if action and action != 'sync': pecan.abort(404, 'Invalid action, only sync is allowed') elif action == 'sync': return self.sync(context, target_project_id) quota = self.update_quota(context, request, target_project_id) return quota
def delete(self, project_id, target_project_id): """Delete quota for a specified tenant. Resources for a specific tenant can also be deleted. :param project_id: It's UUID of the project. :param target_project_id: It's UUID of the project. Note: In v1.0 it can be defaults sometimes. Only specified tenant quota is retrieved from database using this param. #NOTE: Support to delete quota for a specific resource is through CURL request in V1.0. """ context = restcomm.extract_context_from_environ() valid_project_id = uuidutils.is_uuid_like(project_id) if not valid_project_id: pecan.abort(400, _('Invalid request URL')) if project_id != context.project and not context.is_admin: pecan.abort(400, _('Invalid request URL')) if not uuidutils.is_uuid_like(target_project_id): pecan.abort(400, _('Invalid request URL')) if not context.is_admin: pecan.abort(403, _('Admin required')) if request.body: payload = eval(request.body).get('quota_set') if not payload: pecan.abort(400, _('quota_set in body required')) self.delete_quota_resources(context, target_project_id, payload) return {'Deleted quota limits': payload} else: self.delete_quota(context, target_project_id) return "Deleted all quota limits for the given project"
def put(self, project_id, class_name): """Update a class.""" context = restcomm.extract_context_from_environ() valid_project_id = uuidutils.is_uuid_like(project_id) if not valid_project_id: pecan.abort(400, _('Invalid request URL')) if project_id != context.project and not context.is_admin: pecan.abort(400, _('Invalid request URL')) LOG.info("Update quota class [class_name=%s]" % class_name) if not context.is_admin: pecan.abort(403, _('Admin required')) if not request.body: pecan.abort(400, _('Body required')) quota_class_set = eval(request.body).get('quota_class_set') if not quota_class_set: pecan.abort(400, _('Missing quota_class_set in the body')) utils.validate_quota_limits(quota_class_set) for key, value in quota_class_set.items(): try: db_api.quota_class_update(context, class_name, key, value) except exceptions.QuotaClassNotFound: db_api.quota_class_create(context, class_name, key, value) values = db_api.quota_class_get_all_by_name(context, class_name) return self._format_quota_set(context, class_name, values)
def get(self, project_id, target_project_id=None, action=None): """Get quota for a specified tenant. :param project_id: It's UUID of the project. :param target_project_id: It's UUID of the project. Note: In v1.0 it can be defaults sometimes. Only specified tenant quota is retrieved from database using this param. :param action: Optional. If provided, it can be 'detail' detail - Gets detail quota usage for the specified tenant. """ context = restcomm.extract_context_from_environ() valid_project_id = uuidutils.is_uuid_like(project_id) if not valid_project_id: pecan.abort(400, _('Invalid request URL')) if project_id != context.project and not context.is_admin: pecan.abort(400, _('Invalid request URL')) if not uuidutils.is_uuid_like(target_project_id)\ and target_project_id != 'defaults': pecan.abort(400, _('Invalid request URL')) quota = collections.defaultdict(dict) try: if context.is_admin or (project_id == target_project_id)\ or (target_project_id == 'defaults'): result = self.get_quota(context, target_project_id, action) quota['quota_set'] = result return quota else: pecan.abort(403, _('Admin required ')) except exceptions.InternalError: pecan.abort(400, _('Error while requesting usage'))
def delete(self, project, job_id): """Delete the database entries of a given job_id. :param project: It's UUID of the project. :param job_id: ID of the job for which the database entries have to be deleted. """ context = restcomm.extract_context_from_environ() if not uuidutils.is_uuid_like(project) or project != context.project: pecan.abort(400, _('Invalid request URL')) if uuidutils.is_uuid_like(job_id): try: status = db_api.sync_job_status(context, job_id) except exceptions.JobNotFound: pecan.abort(404, _('Job not found')) if status == consts.JOB_PROGRESS: pecan.abort( 406, _('action not supported' ' while sync is in progress')) try: db_api.sync_job_delete(context, job_id) except exceptions.JobNotFound: pecan.abort(404, _('Job not found')) return 'job %s deleted from the database.' % job_id else: pecan.abort(400, _('Bad request'))
def get(self, class_name): context = restcomm.extract_context_from_environ() LOG.info("Fetch quotas for [class_name=%s]" % class_name) values = db_api.quota_class_get_all_by_name(context, class_name) return self._format_quota_set(class_name, values)
def delete(self, class_name): context = restcomm.extract_context_from_environ() if not context.is_admin: pecan.abort(403, _('Admin required')) LOG.info("Delete quota class [class_name=%s]" % class_name) try: db_api.quota_class_destroy_all(context, class_name) except exceptions.QuotaClassNotFound: pecan.abort(404, _('Quota class not found'))
def post(self, project): """Sync resources present in one region to another region. :param project: It's UUID of the project. """ context = restcomm.extract_context_from_environ() if not uuidutils.is_uuid_like(project) or project != context.project: pecan.abort(400, _('Invalid request URL')) payload = eval(request.body) if not payload: pecan.abort(400, _('Body required')) payload = payload.get('resource_set') if not payload: pecan.abort(400, _('resource_set required')) resource_type = payload.get('resource_type') target_regions = payload.get('target') if not target_regions or not isinstance(target_regions, list): pecan.abort(400, _('Target regions required')) source_region = payload.get('source') if not source_region or not isinstance(source_region, str): pecan.abort(400, _('Source region required')) source_resources = payload.get('resources') if not source_resources: pecan.abort(400, _('Source resources required')) job_id = uuidutils.generate_uuid() if resource_type == consts.KEYPAIR: session = EndpointCache().get_session_from_token( context.auth_token, context.project) # Create Source Region object source_nova_client = NovaClient(source_region, session) # Check for keypairs in Source Region for source_keypair in source_resources: source_keypair = source_nova_client.\ get_keypairs(source_keypair) if not source_keypair: pecan.abort(404) result = self._entries_to_database(context, target_regions, source_region, source_resources, resource_type, job_id) return self._keypair_sync(job_id, payload, context, result) elif resource_type == consts.IMAGE: # Create Source Region glance_object glance_driver = GlanceClient(source_region, context) # Check for images in Source Region for image in source_resources: source_image = glance_driver.check_image(image) if image != source_image: pecan.abort(404) result = self._entries_to_database(context, target_regions, source_region, source_resources, resource_type, job_id) return self._image_sync(job_id, payload, context, result) else: pecan.abort(400, _('Bad resource_type'))
def get(self, project_id, class_name): context = restcomm.extract_context_from_environ() valid_project_id = uuidutils.is_uuid_like(project_id) if not valid_project_id: pecan.abort(400, _('Invalid request URL')) if project_id != context.project and not context.is_admin: pecan.abort(400, _('Invalid request URL')) LOG.info("Fetch quotas for [class_name=%s]" % class_name) values = db_api.quota_class_get_all_by_name(context, class_name) return self._format_quota_set(context, class_name, values)
def delete(self, project_id, class_name): context = restcomm.extract_context_from_environ() valid_project_id = uuidutils.is_uuid_like(project_id) if not valid_project_id: pecan.abort(400, _('Invalid request URL')) if project_id != context.project and not context.is_admin: pecan.abort(400, _('Invalid request URL')) if not context.is_admin: pecan.abort(403, _('Admin required')) LOG.info("Delete quota class [class_name=%s]" % class_name) try: db_api.quota_class_destroy_all(context, class_name) except exceptions.QuotaClassNotFound: pecan.abort(404, _('Quota class not found'))
def delete(self, project_id, **args): """Delete quota of a tenant. :param project_id: It's UUID of the project. Only specified tenant quota is deleted using this param. """ context = restcomm.extract_context_from_environ() if not uuidutils.is_uuid_like(project_id): pecan.abort(400) enforce = enf.enforce('kingbird:delete_quota', context) if not enforce: pecan.abort(403, _('Admin required')) if args: payload = args.keys() if not payload: pecan.abort(400, _('quota_set in body required')) self.delete_quota_resources(context, project_id, payload) return {'Deleted quota limits': payload} else: # Delete all quota limits for the project self.delete_quota(context, project_id) return "Deleted all quota limits for the given project"
def get(self, project, action=None): """Get details about Sync Job. :param project: It's UUID of the project. :param action: Optional. If provided, it can be 'active' to get the list of active jobs. 'job-id' to get the details of a job. """ context = restcomm.extract_context_from_environ() if not uuidutils.is_uuid_like(project) or project != context.project: pecan.abort(400, _('Invalid request URL')) result = collections.defaultdict(dict) if not action or action == 'active': result['job_set'] = db_api.sync_job_list(context, action) elif uuidutils.is_uuid_like(action): try: result['job_set'] = db_api.resource_sync_list_by_job( context, action) except exceptions.JobNotFound: pecan.abort(404, _('Job not found')) else: pecan.abort(400, _('Invalid request URL')) return result
def put(self, project_id, action=None): """Update quota of a tenant. :param project_id: It's UUID of the project. Only specified tenant quota is updated using this param. :param action: Optional. If provided, it can be 'sync' action - syncs quota for the specified tenant based on the kingbird magic. """ context = restcomm.extract_context_from_environ() quota = collections.defaultdict(dict) quota[project_id] = collections.defaultdict(dict) if not uuidutils.is_uuid_like(project_id): pecan.abort(400) enforce = enf.enforce('kingbird:update_quota', context) if not enforce: pecan.abort(403, _('Admin required')) if action not in ('sync', None): pecan.abort(404, 'Invalid action, only sync is allowed') elif action == 'sync': return self.sync(context, project_id) quota = self.update_quota(context, request, project_id) return quota