Example #1
0
    def put(self, _id, **kw):
        context = t_context.extract_context_from_environ()

        if not policy.enforce(context, policy.ADMIN_API_PODS_CREATE):
            pecan.abort(401, _('Unauthorized to put core_router'))
            return

        core_router = request.context['request_data']['core_router']
        try:
            with context.session.begin():
                core.get_resource(context, models.CoreRouter, _id)
                if 'routes' in core_router:
                    self._update_core_router_routes(context, _id, core_router)
                router_updated = core.update_resource(context,
                                                      models.CoreRouter, _id,
                                                      core_router)
            return_object = m.SuccessMessage(
                result={'core_router': router_updated})
            return return_object.to_dict()
        except t_exceptions.ResourceNotFound as e:
            LOG.exception(
                'Failed to update core_router : '
                'core_router_id %(core_router_id)s ,'
                '%(exception)s ', {
                    'core_router_id': _id,
                    'exception': e
                })
            return m.CoreRouterNotFound(core_router_id=_id).to_dict()
        except Exception as e:
            LOG.exception('Failed to update core_router : '
                          '%(exception)s ', {'exception': e})
            return_object = m.FailureMessage()
            return return_object.to_dict()
Example #2
0
 def remove_router_interface(self):
     context = t_context.extract_context_from_environ()
     body = {"core_router_interface": request.context['request_data']}
     attr_info = body_validation.RESOURCE_ATTRIBUTE_MAP.get(self.resource)
     body_validation.BodyValidationHook.check_request_body(
         body, True, "core_router_interface", attr_info)
     fabric = request.context['request_data']['fabric']
     try:
         with context.session.begin():
             core.get_resource(context, models.CoreRouter,
                               self.core_router_id)
             interface_filters = [{
                 'key': 'core_router_id',
                 'comparator': 'eq',
                 'value': self.core_router_id
             }, {
                 'key': 'fabric',
                 'comparator': 'eq',
                 'value': fabric
             }]
             interfaces = core.query_resource(context,
                                              models.CoreRouterInterface,
                                              interface_filters, [])
             if (not len(interfaces) > 0):
                 raise t_exceptions.CoreRouterInterfaceDeleteNotFound(
                     core_router_id=self.core_router_id, fabric=fabric)
             core.delete_resources(context, models.CoreRouterInterface,
                                   interface_filters)
             return_object = m.SuccessMessage(result={})
             return return_object.to_dict()
     except t_exceptions.ResourceNotFound as e:
         LOG.exception(
             'Failed to delete core_router_interface : '
             'core_router_id %(core_router_id)s ,'
             '%(exception)s ', {
                 'core_router_id': self.core_router_id,
                 'exception': e
             })
         return m.CoreRouterNotFound(
             core_router_id=self.core_router_id).to_dict()
     except t_exceptions.CoreRouterInterfaceDeleteNotFound:
         return m.CoreRouterInterfaceNotFound(
             core_router_id=self.core_router_id, fabric=fabric).to_dict()
     except Exception as e:
         LOG.exception(
             'Failed to delete core_router_interface :'
             'core_router_id: %(core_router_id)s,'
             'fabric: %(fabric)s,'
             '%(exception)s ', {
                 'core_router_id': self.core_router_id,
                 'fabric': fabric,
                 'exception': e
             })
         return_object = m.FailureMessage()
         return return_object.to_dict()
Example #3
0
    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')
Example #4
0
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 pod['az_name'] == az_name:
            return pod, pod['pod_az_name']

    # TODO(joehuang): schedule one dynamically in the future
    filters = [{'key': 'az_name', 'comparator': 'eq', 'value': az_name}]
    pods = db_api.list_pods(context, filters=filters)
    for pod in pods:
        if pod['pod_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']})
                    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
Example #5
0
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 pod['az_name'] == az_name:
            return pod, pod['pod_az_name']

    # TODO(joehuang): schedule one dynamically in the future
    filters = [{'key': 'az_name', 'comparator': 'eq', 'value': az_name}]
    pods = db_api.list_pods(context, filters=filters)
    for pod in pods:
        if pod['pod_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']
                        })
                    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
Example #6
0
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
Example #7
0
    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
                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)
Example #8
0
    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
        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)
Example #9
0
    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}
Example #10
0
 def post(self, **kw):
     context = t_context.extract_context_from_environ()
     if not context.is_admin:
         pecan.abort(400, 'Admin role required to operate aggregates')
         return
     try:
         with context.session.begin():
             core.get_resource(context, models.Aggregate, self.aggregate_id)
     except t_exc.ResourceNotFound:
         pecan.abort(400, 'Aggregate not found')
         return
     if 'add_host' in kw or 'remove_host' in kw:
         pecan.abort(400, 'Add and remove host action not supported')
         return
     # TODO(zhiyuan) handle aggregate metadata updating
     aggregate = az_ag.get_one_ag(context, self.aggregate_id)
     return {'aggregate': aggregate}
Example #11
0
 def post(self, **kw):
     context = t_context.extract_context_from_environ()
     if not context.is_admin:
         pecan.abort(400, 'Admin role required to operate aggregates')
         return
     try:
         with context.session.begin():
             core.get_resource(context, models.Aggregate, self.aggregate_id)
     except t_exc.ResourceNotFound:
         pecan.abort(400, 'Aggregate not found')
         return
     if 'add_host' in kw or 'remove_host' in kw:
         pecan.abort(400, 'Add and remove host action not supported')
         return
     # TODO(zhiyuan) handle aggregate metadata updating
     aggregate = az_ag.get_one_ag(context, self.aggregate_id)
     return {'aggregate': aggregate}
Example #12
0
    def post(self, **kw):
        context = t_context.extract_context_from_environ()

        if 'fabric' not in kw:
            pecan.abort(400, _('Request body fabric not found'))
            return
        print 'MaXiao kw ' + str(request.context['request_data'])
        fabric = request.context['request_data']['fabric']
        fabric_name = fabric.get('fabric_name', '').strip()
        dc_id = fabric.get('dc_id', '').strip()
        _uuid = uuidutils.generate_uuid()
        try:
            with context.session.begin():
                core.get_resource(context, models.DC, dc_id)
                new_fabric = core.create_resource(context, models.Fabric, {
                    'id': _uuid,
                    'fabric_name': fabric_name,
                    'dc_id': dc_id
                })
        except t_exceptions.ResourceNotFound:
            return Response(_('Specified dc not found'), 404)
        except db_exc.DBDuplicateEntry as e1:
            LOG.exception(
                'Record already exists on %(fabric_name)s,'
                'dc_id: %(dc_id)s,'
                '%(exception)s', {
                    'fabric_name': fabric_name,
                    'dc_id': dc_id,
                    'exception': e1
                })
            return Response(_('Record already exists'), 409)
        except Exception as e2:
            LOG.exception(
                'Failed to create fabric: %(fabric_name)s,'
                'dc_id: %(dc_id)s,'
                '%(exception)s ', {
                    'fabric_name': fabric_name,
                    'dc_id': dc_id,
                    'exception': e2
                })
            return Response(_('Failed to create fabric'), 500)

        return {'fabric': new_fabric}
Example #13
0
    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.error(_LE('Fail to create pod binding: %(exception)s'),
                      {'exception': e})
            pecan.abort(500, _('Fail 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.error(_LE('Fail to create pod binding: %(exception)s'),
                      {'exception': e})
            pecan.abort(500, _('Fail to create pod binding'))
            return

        return {'pod_binding': pod_binding}
Example #14
0
    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}
Example #15
0
    def put(self, _id, **kw):
        context = t_context.extract_context_from_environ()

        tricircle_resource = request.context['request_data']['tricircle_resource']
        try:
            with context.session.begin():
                core.get_resource(context, models.TricircleResource, _id)
        except t_exceptions.ResourceNotFound:
            pecan.abort(404, _('Tricircle resource not found'))
            return
        try:
           tricircle_resource_updated= db_api.update_tricircle_resource(
                    context, _id, tricircle_resource)
           return {'tricircle_resource': tricircle_resource_updated}
        except t_exceptions.ResourceNotFound:
            pecan.abort(404, _('Tricircle resource not found'))
            return
        except Exception as e:
            LOG.exception('Failed to update tricircle resource : '
                          '%(exception)s ', {'exception': e})
            return utils.format_api_error(
                500, _('Failed to update tricircle resource '))
Example #16
0
 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)
Example #17
0
    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 put(self, _id, **kw):
        context = t_context.extract_context_from_environ()

        print 'MaXiao put '+str(request.context['request_data'])
        dynamic_peering_connection = request.context['request_data']['dynamic_peering_connection']
        try:
            with context.session.begin():
                core.get_resource(context, models.DynamicPeeringConnection, _id)
        except t_exceptions.ResourceNotFound:
            pecan.abort(404, _('Core Router not found'))
            return
        try:
           connection_updated= db_api.update_dynamic_peering_connection(
                    context, _id, dynamic_peering_connection)
           return {'dynamic_peering_connection': connection_updated}
        except t_exceptions.ResourceNotFound:
            pecan.abort(404, _('Dynamic Peering Connection not found'))
            return
        except Exception as e:
            LOG.exception('Failed to update dynamic peering connection : '
                          '%(exception)s ', {'exception': e})
            return utils.format_api_error(
                500, _('Failed to update dynamic peering connection '))
Example #19
0
    def test_post_bottom_pod(self, mock_context):
        mock_context.return_value = self.context
        kw = {'pod': {'region_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('BottomPod', pod['region_name'])
            self.assertEqual('TopAZ', pod['az_name'])
            pods = core.query_resource(self.context, models.Pod,
                                       [{'key': 'region_name',
                                         'comparator': 'eq',
                                         'value': 'BottomPod'}], [])
            self.assertEqual(1, len(pods))
Example #20
0
    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
Example #21
0
    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
Example #22
0
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
Example #23
0
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
Example #24
0
    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
Example #25
0
    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)
Example #26
0
    def test_post_bottom_pod(self, mock_context):
        mock_context.return_value = self.context
        kw = {'pod': {'region_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('BottomPod', pod['region_name'])
            self.assertEqual('TopAZ', pod['az_name'])
            pods = core.query_resource(self.context, models.Pod,
                                       [{
                                           'key': 'region_name',
                                           'comparator': 'eq',
                                           'value': 'BottomPod'
                                       }], [])
            self.assertEqual(1, len(pods))
Example #27
0
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
Example #28
0
def get_pod_by_top_id(context, _id):
    """Get pod resource from pod table by top id of resource

    :param context: context object
    :param _id: the top id of resource
    :returns: pod resource
    """
    route_filters = [{'key': 'top_id', 'comparator': 'eq', 'value': _id}]
    with context.session.begin():
        routes = core.query_resource(
            context, models.ResourceRouting, route_filters, [])
        if not routes or len(routes) != 1:
            return None
        route = routes[0]
        if not route['bottom_id']:
            return None
        return core.get_resource(context, models.Pod, route['pod_id'])
Example #29
0
def get_pod_by_top_id(context, _id):
    """Get pod resource from pod table by top id of resource

    :param context: context object
    :param _id: the top id of resource
    :returns: pod resource
    """
    route_filters = [{'key': 'top_id', 'comparator': 'eq', 'value': _id}]
    with context.session.begin():
        routes = core.query_resource(context, models.ResourceRouting,
                                     route_filters, [])
        if not routes or len(routes) != 1:
            return None
        route = routes[0]
        if not route['bottom_id']:
            return None
        return core.get_resource(context, models.Pod, route['pod_id'])
Example #30
0
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
Example #31
0
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
Example #32
0
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
Example #33
0
    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
        except t_exc.ResourceNotFound:
            return Response(_('Pod not found'), 404)
        except Exception as e:
            LOG.error(_LE('Fail to delete pod: %(exception)s'),
                      {'exception': e})
            return Response(_('Fail to delete pod'), 500)
Example #34
0
    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')
Example #35
0
def get_firewall_gateway(context, core_router_id):
    with context.session.begin():
        return core.get_resource(context, models.FirewallGateway,
                                 core_router_id)
Example #36
0
def get_dynamic_peering_connection(context, core_router_id):
    with context.session.begin():
        return core.get_resource(context, models.DynamicPeeringConnection,
                                 core_router_id)
Example #37
0
def get_core_router(context, core_router_id):
    with context.session.begin():
        return core.get_resource(context, models.CoreRouter, core_router_id)
Example #38
0
def get_cached_endpoints(context, config_id):
    with context.session.begin():
        return core.get_resource(context, models.CachedEndpoint, config_id)
Example #39
0
def get_resource_routing(context, id):
    with context.session.begin():
        return core.get_resource(context, models.ResourceRouting, id)
Example #40
0
def get_fabric(context, fabric_id):
    with context.session.begin():
        return core.get_resource(context, models.Fabric, fabric_id)
Example #41
0
def get_region(context, region_id):
    with context.session.begin():
        return core.get_resource(context, models.Region, region_id)
Example #42
0
def get_resource_routing(context, id):
    with context.session.begin():
        return core.get_resource(context, models.ResourceRouting, id)
Example #43
0
def get_cached_endpoints(context, config_id):
    with context.session.begin():
        return core.get_resource(context, models.CachedEndpoint,
                                 config_id)
Example #44
0
def get_job_from_log(context, job_id):
    with context.session.begin():
        return core.get_resource(context, models.AsyncJobLog, job_id)
Example #45
0
def get_site(context, site_id):
    with context.session.begin():
        return core.get_resource(context, Site, site_id)
Example #46
0
def get_firewall_bypass(context, core_router_id):
    with context.session.begin():
        return core.get_resource(context, models.FirewallBypass,
                                 core_router_id)
Example #47
0
def get_tricircle_resource(context, core_router_id):
    with context.session.begin():
        return core.get_resource(context, models.TricircleResource,
                                 core_router_id)
Example #48
0
def get_pod_service_configuration(context, config_id):
    with context.session.begin():
        return core.get_resource(context, models.PodServiceConfiguration,
                                 config_id)
Example #49
0
def get_dc(context, dc_id):
    with context.session.begin():
        return core.get_resource(context, models.DC, dc_id)
Example #50
0
def get_job_from_log(context, job_id):
    with context.session.begin():
        return core.get_resource(context, models.AsyncJobLog, job_id)
Example #51
0
def get_pod(context, pod_id):
    with context.session.begin():
        return core.get_resource(context, models.Pod, pod_id)
Example #52
0
def get_pod(context, pod_id):
    with context.session.begin():
        return core.get_resource(context, models.Pod, pod_id)