def post(self, **kw): context = t_context.extract_context_from_environ() if not context.is_admin: return utils.format_nova_error( 403, _("Policy doesn't allow os_compute_api:os-aggregates:" "index to be performed.")) try: with context.session.begin(): core.get_resource(context, models.Aggregate, self.aggregate_id) except t_exc.ResourceNotFound: return utils.format_nova_error( 404, _('Aggregate %s could not be found.') % self.aggregate_id) if 'add_host' in kw or 'remove_host' in kw: return utils.format_nova_error( 400, _('Add and remove host action not supported')) # TODO(zhiyuan) handle aggregate metadata updating try: aggregate = az_ag.get_one_ag(context, self.aggregate_id) return {'aggregate': aggregate} except Exception: return utils.format_nova_error( 500, _('Aggregate operation on %s failed') % self.aggregate_id)
def delete(self, _id): context = t_context.extract_context_from_environ() if not policy.enforce(context, policy.ADMIN_API_PODS_DELETE): pecan.abort(401, _('Unauthorized to delete pods')) return try: with context.session.begin(): pod = core.get_resource(context, models.Pod, _id) if pod is not None: ag_name = utils.get_ag_name(pod['pod_name']) ag = az_ag.get_ag_by_name(context, ag_name) if ag is not None: az_ag.delete_ag(context, ag['id']) core.delete_resource(context, models.Pod, _id) pecan.response.status = 200 except t_exc.ResourceNotFound: return Response(_('Pod not found'), 404) except Exception as e: LOG.exception( _LE('Failed to delete pod: %(pod_id)s,' '%(exception)s'), { 'pod_id': _id, 'exception': e }) return Response(_('Failed to delete pod'), 500)
def test_post_bottom_pod(self, mock_context): mock_context.return_value = self.context kw = {'pod': {'pod_name': 'BottomPod', 'az_name': 'TopAZ'}} pod_id = self.controller.post(**kw)['pod']['pod_id'] with self.context.session.begin(): pod = core.get_resource(self.context, models.Pod, pod_id) self.assertEqual(pod['pod_name'], 'BottomPod') self.assertEqual(pod['az_name'], 'TopAZ') pods = core.query_resource(self.context, models.Pod, [{'key': 'pod_name', 'comparator': 'eq', 'value': 'BottomPod'}], []) self.assertEqual(len(pods), 1) ag_name = utils.get_ag_name('BottomPod') aggregates = core.query_resource(self.context, models.Aggregate, [{'key': 'name', 'comparator': 'eq', 'value': ag_name}], []) self.assertEqual(len(aggregates), 1) metadatas = core.query_resource( self.context, models.AggregateMetadata, [{'key': 'key', 'comparator': 'eq', 'value': 'availability_zone'}, {'key': 'aggregate_id', 'comparator': 'eq', 'value': aggregates[0]['id']}], []) self.assertEqual(len(metadatas), 1) self.assertEqual(metadatas[0]['value'], 'TopAZ')
def get_bottom_mappings_by_top_id(context, top_id, resource_type): """Get resource id and pod name on bottom :param context: context object :param top_id: resource id on top :param resource_type: resource type :return: a list of tuple (pod dict, bottom_id) """ route_filters = [{ 'key': 'top_id', 'comparator': 'eq', 'value': top_id }, { 'key': 'resource_type', 'comparator': 'eq', 'value': resource_type }] mappings = [] with context.session.begin(): routes = core.query_resource(context, models.ResourceRouting, route_filters, []) for route in routes: if not route['bottom_id']: continue pod = core.get_resource(context, models.Pod, route['pod_id']) mappings.append((pod, route['bottom_id'])) return mappings
def delete(self, _id): context = t_context.extract_context_from_environ() if not t_context.is_admin_context(context): pecan.abort(400, _('Admin role required to delete pods')) return try: with context.session.begin(): pod = core.get_resource(context, models.Pod, _id) if pod is not None: ag_name = utils.get_ag_name(pod['pod_name']) ag = az_ag.get_ag_by_name(context, ag_name) if ag is not None: az_ag.delete_ag(context, ag['id']) core.delete_resource(context, models.Pod, _id) pecan.response.status = 200 return {} except t_exc.ResourceNotFound: return Response(_('Pod not found'), 404) except Exception as e: LOG.exception( _LE('Failed to delete pod: %(pod_id)s,' '%(exception)s'), { 'pod_id': _id, 'exception': e }) return Response(_('Failed to delete pod'), 500)
def post(self, **kw): context = t_context.extract_context_from_environ() if not t_context.is_admin_context(context): pecan.abort(400, _('Admin role required to create bindings')) return if 'pod_binding' not in kw: pecan.abort(400, _('Request body not found')) return pod_b = kw['pod_binding'] tenant_id = pod_b.get('tenant_id', '').strip() pod_id = pod_b.get('pod_id', '').strip() _uuid = uuidutils.generate_uuid() if tenant_id == '' or pod_id == '': return Response(_('Tenant_id and pod_id can not be empty'), 422) # the az_pod_map_id should be exist for in the pod map table try: with context.session.begin(): pod = core.get_resource(context, models.Pod, pod_id) if pod.get('az_name') == '': return Response(_('Top region can not be bound'), 422) except t_exc.ResourceNotFound: return Response(_('pod_id not found in pod'), 422) except Exception as e: LOG.exception( _LE('Failed to get_resource for pod_id: ' '%(pod_id)s ,' '%(exception)s '), { 'pod_id': pod_id, 'exception': e }) pecan.abort(500, _('Failed to create pod binding')) return try: with context.session.begin(): pod_binding = core.create_resource(context, models.PodBinding, { 'id': _uuid, 'tenant_id': tenant_id, 'pod_id': pod_id }) except db_exc.DBDuplicateEntry: return Response(_('Pod binding already exists'), 409) except db_exc.DBConstraintError: return Response(_('pod_id not exists in pod'), 422) except db_exc.DBReferenceError: return Response(_('DB reference not exists in pod'), 422) except Exception as e: LOG.exception(_LE('Failed to create pod binding: %(exception)s '), {'exception': e}) pecan.abort(500, _('Failed to create pod binding')) return return {'pod_binding': pod_binding}
def post(self, **kw): context = t_context.extract_context_from_environ() if not policy.enforce(context, policy.ADMIN_API_BINDINGS_CREATE): pecan.abort(401, _('Unauthorized to create bindings')) return if 'pod_binding' not in kw: pecan.abort(400, _('Request body not found')) return pod_b = kw['pod_binding'] tenant_id = pod_b.get('tenant_id', '').strip() pod_id = pod_b.get('pod_id', '').strip() if tenant_id == '' or pod_id == '': return Response(_('Tenant_id and pod_id can not be empty'), 422) # the az_pod_map_id should be exist for in the pod map table try: with context.session.begin(): pod = core.get_resource(context, models.Pod, pod_id) if pod.get('az_name') == '': return Response(_('Top region can not be bound'), 422) except t_exc.ResourceNotFound: return Response(_('pod_id not found in pod'), 422) except Exception as e: LOG.exception( _LE('Failed to get_resource for pod_id: ' '%(pod_id)s ,' '%(exception)s '), { 'pod_id': pod_id, 'exception': e }) pecan.abort(500, _('Failed to create pod binding')) return try: pod_binding = db_api.create_pod_binding(context, tenant_id, pod_id) except db_exc.DBDuplicateEntry: return Response(_('Pod binding already exists'), 409) except db_exc.DBConstraintError: return Response(_('pod_id not exists in pod'), 422) except db_exc.DBReferenceError: return Response(_('DB reference not exists in pod'), 422) except Exception as e: LOG.exception(_LE('Failed to create pod binding: %(exception)s '), {'exception': e}) pecan.abort(500, _('Failed to create pod binding')) return return {'pod_binding': pod_binding}
def test_post_top_pod(self, mock_context): mock_context.return_value = self.context kw = {'pod': {'pod_name': 'TopPod', 'az_name': ''}} pod_id = self.controller.post(**kw)['pod']['pod_id'] with self.context.session.begin(): pod = core.get_resource(self.context, models.Pod, pod_id) self.assertEqual(pod['pod_name'], 'TopPod') self.assertEqual(pod['az_name'], '') pods = core.query_resource(self.context, models.Pod, [{'key': 'pod_name', 'comparator': 'eq', 'value': 'TopPod'}], []) self.assertEqual(len(pods), 1)
def get_one(self, _id): context = t_context.extract_context_from_environ() if not policy.enforce(context, policy.ADMIN_API_BINDINGS_SHOW): pecan.abort(401, _('Unauthorized to show bindings')) return try: with context.session.begin(): pod_binding = core.get_resource(context, models.PodBinding, _id) return {'pod_binding': pod_binding} except t_exc.ResourceNotFound: pecan.abort(404, _('Tenant pod binding not found')) return
def get_one(self, _id): context = t_context.extract_context_from_environ() if not t_context.is_admin_context(context): pecan.abort(400, _('Admin role required to show bindings')) return try: with context.session.begin(): pod_binding = core.get_resource(context, models.PodBinding, _id) return {'pod_binding': pod_binding} except t_exc.ResourceNotFound: pecan.abort(404, _('Tenant pod binding not found')) return
def list_pods_by_tenant(context, tenant_id): pod_bindings = core.query_resource(context, models.PodBinding, [{ 'key': 'tenant_id', 'comparator': 'eq', 'value': tenant_id }], []) pods = [] if pod_bindings: for pod_b in pod_bindings: pod = core.get_resource(context, models.Pod, pod_b['pod_id']) pods.append(pod) return pods
def get_pod_by_az_tenant(context, az_name, tenant_id): pod_bindings = core.query_resource(context, models.PodBinding, [{ 'key': 'tenant_id', 'comparator': 'eq', 'value': tenant_id }], []) for pod_b in pod_bindings: pod = core.get_resource(context, models.Pod, pod_b['pod_id']) if az_name and pod['az_name'] == az_name: return pod, pod['pod_az_name'] elif az_name == '' and pod['az_name'] != '': # if the az_name is not specified, a defult bottom # pod will be selected return pod, pod['pod_az_name'] else: pass # TODO(joehuang): schedule one dynamically in the future if az_name != '': filters = [{'key': 'az_name', 'comparator': 'eq', 'value': az_name}] else: filters = None # if az_name is valid, select a pod under this az_name # if az_name is '', select the first valid bottom pod. # change to dynamic schedluing in the future pods = db_api.list_pods(context, filters=filters) for pod in pods: if pod['pod_name'] != '' and pod['az_name'] != '': try: with context.session.begin(): core.create_resource( context, models.PodBinding, { 'id': uuidutils.generate_uuid(), 'tenant_id': tenant_id, 'pod_id': pod['pod_id'], 'is_binding': True }) return pod, pod['pod_az_name'] except Exception as e: LOG.error(_LE('Fail to create pod binding: %(exception)s'), {'exception': e}) return None, None return None, None
def get_one_ag(context, aggregate_id): aggregate = core.get_resource(context, models.Aggregate, aggregate_id) metadatas = core.query_resource(context, models.AggregateMetadata, [{ 'key': 'key', 'comparator': 'eq', 'value': 'availability_zone' }, { 'key': 'aggregate_id', 'comparator': 'eq', 'value': aggregate['id'] }], []) if metadatas: aggregate['availability_zone'] = metadatas[0]['value'] aggregate['metadata'] = {'availability_zone': metadatas[0]['value']} else: aggregate['availability_zone'] = '' aggregate['metadata'] = {} return aggregate
def get_pod_service_configuration(context, config_id): with context.session.begin(): return core.get_resource(context, models.PodServiceConfiguration, config_id)
def get_pod(context, pod_id): with context.session.begin(): return core.get_resource(context, models.Pod, pod_id)