コード例 #1
0
ファイル: pod.py プロジェクト: paperandsoap/tricircle
    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)
コード例 #2
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')
コード例 #3
0
    def test_delete(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']
        self.controller.delete(pod_id)

        with self.context.session.begin():
            pods = core.query_resource(self.context, models.Pod,
                                       [{
                                           'key': 'pod_name',
                                           'comparator': 'eq',
                                           'value': 'BottomPod'
                                       }], [])
            self.assertEqual(len(pods), 0)
            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), 0)
            metadatas = core.query_resource(self.context,
                                            models.AggregateMetadata,
                                            [{
                                                'key': 'key',
                                                'comparator': 'eq',
                                                'value': 'availability_zone'
                                            }, {
                                                'key': 'value',
                                                'comparator': 'eq',
                                                'value': 'TopAZ'
                                            }], [])
            self.assertEqual(len(metadatas), 0)
コード例 #4
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)
コード例 #5
0
ファイル: root.py プロジェクト: hannibalhuang/tricircle
    def post(self, **kw):
        context = _extract_context_from_environ(_get_environment())
        if not context.is_admin:
            pecan.abort(400, 'Admin role required to create sites')
            return

        site_name = kw.get('name')
        is_top_site = kw.get('top', False)

        if not site_name:
            pecan.abort(400, 'Name of site required')
            return

        site_filters = [{'key': 'site_name', 'comparator': 'eq',
                         'value': site_name}]
        sites = models.list_sites(context, site_filters)
        if sites:
            pecan.abort(409, 'Site with name %s exists' % site_name)
            return

        ag_name = utils.get_ag_name(site_name)
        # top site doesn't need az
        az_name = utils.get_az_name(site_name) if not is_top_site else ''

        try:
            site_dict = {'site_id': str(uuid.uuid4()),
                         'site_name': site_name,
                         'az_id': az_name}
            site = models.create_site(context, site_dict)
        except Exception as e:
            LOG.debug(e.message)
            pecan.abort(500, 'Fail to create site')
            return

        # top site doesn't need aggregate
        if is_top_site:
            pecan.response.status = 201
            return {'site': site}
        else:
            try:
                top_client = client.Client()
                top_client.create_aggregates(context, ag_name, az_name)
                site_api = cascading_site_api.CascadingSiteNotifyAPI()
                site_api.create_site(context, site_name)
            except Exception as e:
                LOG.debug(e.message)
                # delete previously created site
                models.delete_site(context, site['site_id'])
                pecan.abort(500, 'Fail to create aggregate')
                return
            pecan.response.status = 201
            return {'site': site}
コード例 #6
0
    def create_site(self, context, site_name):
        """creates a fake node as nova-compute and add it to az"""

        # TODO(saggi): thread safety
        if site_name in self._sites:
            raise RuntimeError("Site already exists in site map")

        # TODO(zhiyuan): use DHT to judge whether host this site or not
        self._sites[site_name] = Site(site_name)
        self.compute_host_manager.create_host_adapter(site_name)

        ag_name = utils.get_ag_name(site_name)
        top_client = client.Client()
        aggregates = top_client.list_resources('aggregate', context)
        for aggregate in aggregates:
            if aggregate['name'] == ag_name:
                if site_name in aggregate['hosts']:
                    return
                else:
                    top_client.action_resources('aggregate', context,
                                                'add_host', aggregate['id'],
                                                site_name)
                    return
コード例 #7
0
ファイル: pod.py プロジェクト: Ronghui/tricircle
    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)
コード例 #8
0
ファイル: test_pod.py プロジェクト: Ronghui/tricircle
    def test_get_delete_one(self, mock_context):

        mock_context.return_value = self.context

        pods = [

            {
                "pod":
                {
                    "pod_name": "Pod1",
                    "pod_az_name": "az1",
                    "dc_name": "dc2",
                    "az_name": "AZ1"
                },
                "expected_error": 200,
            },

            {
                "pod":
                {
                    "pod_name": "Pod2",
                    "pod_az_name": "az1",
                    "dc_name": "dc2",
                    "az_name": "AZ1"
                },
                "expected_error": 200,
            },

            {
                "pod":
                {
                    "pod_name": "Pod3",
                    "pod_az_name": "az1",
                    "dc_name": "dc2",
                    "az_name": "AZ2"
                },
                "expected_error": 200,
            },

            ]

        self._test_and_check(pods)

        response = self.app.get('/v1.0/pods')
        self.assertEqual(response.status_int, 200)

        return_pods = response.json

        for ret_pod in return_pods['pods']:

            _id = ret_pod['pod_id']
            single_ret = self.app.get('/v1.0/pods/' + str(_id))

            self.assertEqual(single_ret.status_int, 200)

            one_pod_ret = single_ret.json
            get_one_pod = one_pod_ret['pod']

            self.assertEqual(get_one_pod['pod_id'],
                             ret_pod['pod_id'])

            self.assertEqual(get_one_pod['pod_name'],
                             ret_pod['pod_name'])

            self.assertEqual(get_one_pod['pod_az_name'],
                             ret_pod['pod_az_name'])

            self.assertEqual(get_one_pod['dc_name'],
                             ret_pod['dc_name'])

            self.assertEqual(get_one_pod['az_name'],
                             ret_pod['az_name'])

            _id = ret_pod['pod_id']

            # check ag and az automaticly added
            ag_name = utils.get_ag_name(ret_pod['pod_name'])
            ag = az_ag.get_ag_by_name(self.context, ag_name)
            self.assertIsNotNone(ag)
            self.assertEqual(ag['name'],
                             utils.get_ag_name(ret_pod['pod_name']))
            self.assertEqual(ag['availability_zone'], ret_pod['az_name'])

            single_ret = self.app.delete('/v1.0/pods/' + str(_id))
            self.assertEqual(single_ret.status_int, 200)

            # make sure ag is deleted
            ag = az_ag.get_ag_by_name(self.context, ag_name)
            self.assertIsNone(ag)
コード例 #9
0
ファイル: pod.py プロジェクト: paperandsoap/tricircle
    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 pods'))
            return

        if 'pod' not in kw:
            pecan.abort(400, _('Request body pod not found'))
            return

        pod = kw['pod']

        # if az_name is null, and there is already one in db
        pod_name = pod.get('pod_name', '').strip()
        pod_az_name = pod.get('pod_az_name', '').strip()
        dc_name = pod.get('dc_name', '').strip()
        az_name = pod.get('az_name', '').strip()
        _uuid = uuidutils.generate_uuid()

        if az_name == '' and pod_name == '':
            return Response(_('Valid pod_name is required for top region'),
                            422)

        if az_name != '' and pod_name == '':
            return Response(_('Valid pod_name is required for pod'), 422)

        if pod.get('az_name') is None:
            if self._get_top_region(context) != '':
                return Response(_('Top region already exists'), 409)

        # if az_name is not null, then the pod region name should not
        # be same as that the top region
        if az_name != '':
            if self._get_top_region(context) == pod_name and pod_name != '':
                return Response(
                    _('Pod region name duplicated with the top region name'),
                    409)

        # to create the top region, make the pod_az_name to null value
        if az_name == '':
            pod_az_name = ''

        try:
            with context.session.begin():
                # if not top region,
                # then add corresponding ag and az for the pod
                if az_name != '':
                    ag_name = utils.get_ag_name(pod_name)
                    aggregate = az_ag.create_ag_az(context,
                                                   ag_name=ag_name,
                                                   az_name=az_name)
                    if aggregate is None:
                        return Response(_('Ag creation failure'), 400)

                new_pod = core.create_resource(
                    context, models.Pod, {
                        'pod_id': _uuid,
                        'pod_name': pod_name,
                        'pod_az_name': pod_az_name,
                        'dc_name': dc_name,
                        'az_name': az_name
                    })
        except db_exc.DBDuplicateEntry as e1:
            LOG.exception(
                _LE('Record already exists on %(pod_name)s: '
                    '%(exception)s'), {
                        'pod_name': pod_name,
                        'exception': e1
                    })
            return Response(_('Record already exists'), 409)
        except Exception as e2:
            LOG.exception(
                _LE('Failed to create pod: %(pod_name)s,'
                    'pod_az_name: %(pod_az_name)s,'
                    'dc_name: %(dc_name)s,'
                    'az_name: %(az_name)s'
                    '%(exception)s '), {
                        'pod_name': pod_name,
                        'pod_az_name': pod_az_name,
                        'dc_name': dc_name,
                        'az_name': az_name,
                        'exception': e2
                    })
            return Response(_('Failed to create pod'), 500)

        return {'pod': new_pod}
コード例 #10
0
    def test_get_delete_one(self, mock_context):

        mock_context.return_value = self.context

        pods = [
            {
                "pod": {
                    "pod_name": "Pod1",
                    "pod_az_name": "az1",
                    "dc_name": "dc2",
                    "az_name": "AZ1"
                },
                "expected_error": 200,
            },
            {
                "pod": {
                    "pod_name": "Pod2",
                    "pod_az_name": "az1",
                    "dc_name": "dc2",
                    "az_name": "AZ1"
                },
                "expected_error": 200,
            },
            {
                "pod": {
                    "pod_name": "Pod3",
                    "pod_az_name": "az1",
                    "dc_name": "dc2",
                    "az_name": "AZ2"
                },
                "expected_error": 200,
            },
        ]

        self._test_and_check(pods)

        response = self.app.get('/v1.0/pods')
        self.assertEqual(response.status_int, 200)

        return_pods = response.json

        for ret_pod in return_pods['pods']:

            _id = ret_pod['pod_id']
            single_ret = self.app.get('/v1.0/pods/' + str(_id))

            self.assertEqual(single_ret.status_int, 200)

            one_pod_ret = single_ret.json
            get_one_pod = one_pod_ret['pod']

            self.assertEqual(get_one_pod['pod_id'], ret_pod['pod_id'])

            self.assertEqual(get_one_pod['pod_name'], ret_pod['pod_name'])

            self.assertEqual(get_one_pod['pod_az_name'],
                             ret_pod['pod_az_name'])

            self.assertEqual(get_one_pod['dc_name'], ret_pod['dc_name'])

            self.assertEqual(get_one_pod['az_name'], ret_pod['az_name'])

            _id = ret_pod['pod_id']

            # check ag and az automaticly added
            ag_name = utils.get_ag_name(ret_pod['pod_name'])
            ag = az_ag.get_ag_by_name(self.context, ag_name)
            self.assertIsNotNone(ag)
            self.assertEqual(ag['name'],
                             utils.get_ag_name(ret_pod['pod_name']))
            self.assertEqual(ag['availability_zone'], ret_pod['az_name'])

            single_ret = self.app.delete('/v1.0/pods/' + str(_id))
            self.assertEqual(single_ret.status_int, 200)

            # make sure ag is deleted
            ag = az_ag.get_ag_by_name(self.context, ag_name)
            self.assertIsNone(ag)
コード例 #11
0
ファイル: pod.py プロジェクト: Ronghui/tricircle
    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 pods'))
            return

        if 'pod' not in kw:
            pecan.abort(400, _('Request body pod not found'))
            return

        pod = kw['pod']

        # if az_name is null, and there is already one in db
        pod_name = pod.get('pod_name', '').strip()
        pod_az_name = pod.get('pod_az_name', '').strip()
        dc_name = pod.get('dc_name', '').strip()
        az_name = pod.get('az_name', '').strip()
        _uuid = uuidutils.generate_uuid()

        if az_name == '' and pod_name == '':
            return Response(_('Valid pod_name is required for top region'),
                            422)

        if az_name != '' and pod_name == '':
            return Response(_('Valid pod_name is required for pod'), 422)

        if pod.get('az_name') is None:
            if self._get_top_region(context) != '':
                return Response(_('Top region already exists'), 409)

        # if az_name is not null, then the pod region name should not
        # be same as that the top region
        if az_name != '':
            if self._get_top_region(context) == pod_name and pod_name != '':
                return Response(
                    _('Pod region name duplicated with the top region name'),
                    409)

        # to create the top region, make the pod_az_name to null value
        if az_name == '':
            pod_az_name = ''

        try:
            with context.session.begin():
                # if not top region,
                # then add corresponding ag and az for the pod
                if az_name != '':
                    ag_name = utils.get_ag_name(pod_name)
                    aggregate = az_ag.create_ag_az(context,
                                                   ag_name=ag_name,
                                                   az_name=az_name)
                    if aggregate is None:
                        return Response(_('Ag creation failure'), 400)

                new_pod = core.create_resource(
                    context, models.Pod,
                    {'pod_id': _uuid,
                     'pod_name': pod_name,
                     'pod_az_name': pod_az_name,
                     'dc_name': dc_name,
                     'az_name': az_name})
        except db_exc.DBDuplicateEntry as e1:
            LOG.error(_LE('Record already exists: %(exception)s'),
                      {'exception': e1})
            return Response(_('Record already exists'), 409)
        except Exception as e2:
            LOG.error(_LE('Fail to create pod: %(exception)s'),
                      {'exception': e2})
            return Response(_('Fail to create pod'), 500)

        return {'pod': new_pod}