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 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 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 post(self, **kw): context = t_context.extract_context_from_environ() if not policy.enforce(context, policy.ADMIN_API_PODS_CREATE): pecan.abort(401, _('Unauthorized 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}
def test_get_delete_one(self): 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)