def post(self, **kw): context = t_context.extract_context_from_environ() if not policy.enforce(context, policy.ADMIN_API_ROUTINGS_CREATE): return utils.format_api_error( 403, _("Unauthorized to create resource routing")) if 'routing' not in kw: return utils.format_api_error( 400, _("Request body not found")) routing = kw['routing'] for field in ('top_id', 'bottom_id', 'pod_id', 'project_id', 'resource_type'): value = routing.get(field) if value is None or len(value.strip()) == 0: return utils.format_api_error( 400, _("Field %(field)s can not be empty") % { 'field': field}) # verify the integrity: the pod_id and the project_id should be bound pod_id = routing.get('pod_id').strip() project_id = routing.get('project_id').strip() bindings = db_api.list_pod_bindings(context, [{'key': 'pod_id', 'comparator': 'eq', 'value': pod_id }, {'key': 'tenant_id', 'comparator': 'eq', 'value': project_id} ], []) if len(bindings) == 0: return utils.format_api_error( 400, _('The pod_id and project_id have not been bound')) # the resource type should be properly provisioned. resource_type = routing.get('resource_type').strip() if not constants.is_valid_resource_type(resource_type): return utils.format_api_error( 400, _('There is no such resource type')) try: top_id = routing.get('top_id').strip() bottom_id = routing.get('bottom_id').strip() routing = db_api.create_resource_mapping(context, top_id, bottom_id, pod_id, project_id, resource_type) if not routing: return utils.format_api_error( 409, _('Resource routing already exists')) except Exception as e: LOG.exception(_LE('Failed to create resource routing: ' '%(exception)s '), {'exception': e}) return utils.format_api_error( 500, _('Failed to create resource routing')) return {'routing': routing}
def put(self, _id, **kw): context = t_context.extract_context_from_environ() if not policy.enforce(context, policy.ADMIN_API_ROUTINGS_PUT): return utils.format_api_error( 403, _('Unauthorized to update resource routing')) try: routing = db_api.get_resource_routing(context, _id) except t_exc.ResourceNotFound: return utils.format_api_error(404, _('Resource routing not found')) if 'routing' not in kw: return utils.format_api_error( 400, _('Request body not found')) update_dict = kw['routing'] # values to be updated should not be empty for field in update_dict: value = update_dict.get(field) if value is None or len(value.strip()) == 0: return utils.format_api_error( 400, _("Field %(field)s can not be empty") % { 'field': field}) # the resource type should be properly provisioned. if 'resource_type' in update_dict: if not constants.is_valid_resource_type( update_dict['resource_type']): return utils.format_api_error( 400, _('There is no such resource type')) # verify the integrity: the pod_id and project_id should be bound if 'pod_id' in update_dict or 'project_id' in update_dict: if 'pod_id' in update_dict: pod_id = update_dict['pod_id'] else: pod_id = routing['pod_id'] if 'project_id' in update_dict: project_id = update_dict['project_id'] else: project_id = routing['project_id'] bindings = db_api.list_pod_bindings(context, [{'key': 'pod_id', 'comparator': 'eq', 'value': pod_id }, {'key': 'tenant_id', 'comparator': 'eq', 'value': project_id} ], []) if len(bindings) == 0: return utils.format_api_error( 400, _('The pod_id and project_id have not been ' 'bound')) try: routing_updated = db_api.update_resource_routing( context, _id, update_dict) return {'routing': routing_updated} except Exception as e: LOG.exception(_LE('Failed to update resource routing: ' '%(exception)s '), {'exception': e}) return utils.format_api_error( 500, _('Failed to update resource routing'))