def post(self, baymodel): """Create a new baymodel. :param baymodel: a baymodel within the request body. """ baymodel_dict = baymodel.as_dict() context = pecan.request.context self.check_keypair_exists(context, baymodel_dict['keypair_id']) baymodel_dict['project_id'] = context.project_id baymodel_dict['user_id'] = context.user_id image_data = self._get_image_data(context, baymodel_dict['image_id']) if image_data.get('os_distro'): baymodel_dict['cluster_distro'] = image_data['os_distro'] else: raise exception.OSDistroFieldNotFound( image_id=baymodel_dict['image_id']) # check permissions for making baymodel public if baymodel_dict['public']: if not policy.enforce( context, "baymodel:publish", None, do_raise=False): raise exception.BaymodelPublishDenied() new_baymodel = objects.BayModel(context, **baymodel_dict) new_baymodel.create() # Set the HTTP Location Header pecan.response.location = link.build_url('baymodels', new_baymodel.uuid) return BayModel.convert_with_links(new_baymodel)
def test_create_baymodel_with_no_os_distro_image(self, mock_image_data): mock_image_data.side_effect = exception.OSDistroFieldNotFound('img') bdict = apiutils.baymodel_post_data() del bdict['uuid'] response = self.post_json('/baymodels', bdict, expect_errors=True) self.assertEqual(400, response.status_int)
def test_create_bay_with_on_os_distro_image(self, mock_valid_os_res): bdict = apiutils.bay_post_data() mock_valid_os_res.side_effect = exception.OSDistroFieldNotFound('img') response = self.post_json('/bays', bdict, expect_errors=True) self.assertEqual('application/json', response.content_type) self.assertTrue(mock_valid_os_res.called) self.assertEqual(400, response.status_int)
def test_create_baymodel_with_no_os_distro_image(self): image_exce = exception.OSDistroFieldNotFound('img') self.mock_valid_os_res.side_effect = image_exce response = self.patch_json('/baymodels/%s' % self.baymodel.uuid, [{'path': '/image_id', 'value': 'img', 'op': 'replace'}], expect_errors=True) self.assertEqual('application/json', response.content_type) self.assertEqual(400, response.status_code) self.assertTrue(response.json['errors'])
def validate_image(cli, image): """Validate image""" try: image_found = api_utils.get_openstack_resource(cli.glance().images, image, 'images') except (glance_exception.NotFound, exception.ResourceNotFound): raise exception.ImageNotFound(image_id=image) except glance_exception.HTTPForbidden: raise exception.ImageNotAuthorized(image_id=image) if not image_found.get('os_distro'): raise exception.OSDistroFieldNotFound(image_id=image) return image_found
def post(self, baymodel): """Create a new baymodel. :param baymodel: a baymodel within the request body. """ baymodel_dict = baymodel.as_dict() context = pecan.request.context baymodel_dict['project_id'] = context.project_id baymodel_dict['user_id'] = context.user_id image_data = self._get_image_data(context, baymodel_dict['image_id']) if image_data.get('os_distro'): baymodel_dict['cluster_distro'] = image_data['os_distro'] else: raise exception.OSDistroFieldNotFound( image_id=baymodel_dict['image_id']) new_baymodel = objects.BayModel(context, **baymodel_dict) new_baymodel.create() # Set the HTTP Location Header pecan.response.location = link.build_url('baymodels', new_baymodel.uuid) return BayModel.convert_with_links(new_baymodel)