def update(self, req, body, package_id): """List of allowed changes: { "op": "add", "path": "/tags", "value": [ "foo", "bar" ] } { "op": "add", "path": "/categories", "value": [ "foo", "bar" ] } { "op": "remove", "path": "/tags" } { "op": "remove", "path": "/categories" } { "op": "replace", "path": "/tags", "value": ["foo", "bar"] } { "op": "replace", "path": "/is_public", "value": true } { "op": "replace", "path": "/description", "value":"New description" } { "op": "replace", "path": "/name", "value": "New name" } """ policy.check("modify_package", req.context, {'package_id': package_id}) pkg_to_update = db_api.package_get(package_id, req.context) if pkg_to_update.is_public: policy.check("manage_public_package", req.context) _check_content_type(req, 'application/murano-packages-json-patch') if not isinstance(body, list): msg = _('Request body must be a JSON array of operation objects.') LOG.error(msg) raise exc.HTTPBadRequest(explanation=msg) for change in body: if 'is_public' in change['path']: if change['value'] is True and not pkg_to_update.is_public: policy.check('publicize_package', req.context) break package = db_api.package_update(package_id, body, req.context) return package.to_dict()
def update(self, req, body, package_id): """List of allowed changes: { "op": "add", "path": "/tags", "value": [ "foo", "bar" ] } { "op": "add", "path": "/categories", "value": [ "foo", "bar" ] } { "op": "remove", "path": "/tags" } { "op": "remove", "path": "/categories" } { "op": "replace", "path": "/tags", "value": ["foo", "bar"] } { "op": "replace", "path": "/is_public", "value": true } { "op": "replace", "path": "/description", "value":"New description" } { "op": "replace", "path": "/name", "value": "New name" } """ policy.check("modify_package", req.context, {'package_id': package_id}) pkg_to_update = db_api.package_get(package_id, req.context) if pkg_to_update.is_public: policy.check("manage_public_package", req.context) _check_content_type(req, 'application/murano-packages-json-patch') if not isinstance(body, list): msg = _('Request body must be a JSON array of operation objects.') LOG.error(msg) raise exc.HTTPBadRequest(explanation=msg) for change in body: if 'is_public' in change['path']: if change['value'] is True and not pkg_to_update.is_public: policy.check('publicize_package', req.context) if 'name' in change['path']: if len(change['value']) > 80: msg = _('Package name should be 80 characters maximum') LOG.error(msg) raise exc.HTTPBadRequest(explanation=msg) package = db_api.package_update(package_id, body, req.context) return package.to_dict()
def delete(self, req, package_id): target = {'package_id': package_id} policy.check("delete_package", req.context, target) package = db_api.package_get(package_id, req.context) if package.is_public: policy.check("manage_public_package", req.context, target) db_api.package_delete(package_id, req.context)
def get_ui(self, req, package_id): if CONF.engine.packages_service == 'murano': target = {'package_id': package_id} policy.check("get_package", req.context, target) package = db_api.package_get(package_id, req.context) return package.ui_definition else: g_client = self._get_glare_client(req) blob_data = g_client.artifacts.download_blob(package_id, 'archive') with tempfile.NamedTemporaryFile() as tempf: for chunk in blob_data: tempf.write(chunk) with load_utils.load_from_file(tempf.name, target_dir=None, drop_dir=True) as pkg: return pkg.ui
def get(self, req, package_id): policy.check("get_package", req.context, {'package_id': package_id}) package = db_api.package_get(package_id, req.context) return package.to_dict()
def get_logo(self, req, package_id): target = {'package_id': package_id} policy.check("get_package", req.context, target) package = db_api.package_get(package_id, req.context) return package.logo
def test_package_make_public(self): id = api.package_upload(self._stub_package(), self.tenant_id).id patch = self.get_change('replace', ['is_public'], True) api.package_update(id, [patch], self.context) package = api.package_get(id, self.context) self.assertTrue(package.is_public)
def provision(self, req, body, instance_id): """Here is the example of request body given us from Cloud Foundry: { "service_id": "service-guid-here", "plan_id": "plan-guid-here", "organization_guid": "org-guid-here", "space_guid": "space-guid-here", "parameters": {"param1": "value1", "param2": "value2"} } """ data = json.loads(req.body) space_guid = data['space_guid'] org_guid = data['organization_guid'] plan_id = data['plan_id'] service_id = data['service_id'] parameters = data['parameters'] self.current_session = None # Here we'll take an entry for CF org and space from db. If we # don't have any entries we will create it from scratch. try: tenant = db_cf.get_tenant_for_org(org_guid) except AttributeError: tenant = req.headers['X-Project-Id'] db_cf.set_tenant_for_org(org_guid, tenant) LOG.info( _LI("Cloud Foundry {org_id} mapped to tenant " "{tenant_name}").format(org_id=org_guid, tenant_name=tenant)) token = req.headers['X-Auth-Token'] m_cli = muranoclient(token) try: environment_id = db_cf.get_environment_for_space(space_guid) except AttributeError: body = {'name': 'my_{uuid}'.format(uuid=uuid.uuid4().hex)} env = m_cli.environments.create(body) environment_id = env.id db_cf.set_environment_for_space(space_guid, environment_id) LOG.info( _LI("Cloud Foundry {space_id} mapped to {environment_id}"). format(space_id=space_guid, environment_id=environment_id)) package = db_api.package_get(service_id, req.context) LOG.debug('Adding service {name}'.format(name=package.name)) service = self._make_service(space_guid, package, plan_id) db_cf.set_instance_for_service(instance_id, service['?']['id'], environment_id, tenant) # NOTE(Kezar): Here we are going through JSON and add ids where # it's necessary. Before that we need to drop '?' key from parameters # dictionary as far it contains murano package related info which is # necessary in our scenario if '?' in parameters.keys(): parameters.pop('?', None) LOG.warning( _LW("Incorrect input parameters. Package related " "parameters shouldn't be passed through Cloud " "Foundry")) params = [parameters] while params: a = params.pop() for k, v in six.iteritems(a): if isinstance(v, dict): params.append(v) if k == '?': v['id'] = uuid.uuid4().hex service.update(parameters) # Now we need to obtain session to modify the env session_id = create_session(m_cli, environment_id) m_cli.services.post(environment_id, path='/', data=service, session_id=session_id) m_cli.sessions.deploy(environment_id, session_id) self.current_session = session_id return response.Response(status=202, json_body={})
def get_ui(self, req, package_id): target = {'package_id': package_id} policy.check("get_package_ui", req.context, target) package = db_api.package_get(package_id, req.context) return package.ui_definition
def provision(self, req, body, instance_id): """Here is the example of request body given us from Cloud Foundry: { "service_id": "service-guid-here", "plan_id": "plan-guid-here", "organization_guid": "org-guid-here", "space_guid": "space-guid-here", "parameters": {"param1": "value1", "param2": "value2"} } """ data = json.loads(req.body) space_guid = data['space_guid'] org_guid = data['organization_guid'] plan_id = data['plan_id'] service_id = data['service_id'] parameters = data['parameters'] self.current_session = None # Here we'll take an entry for CF org and space from db. If we # don't have any entries we will create it from scratch. try: tenant = db_cf.get_tenant_for_org(org_guid) except AttributeError: tenant = req.headers['X-Project-Id'] db_cf.set_tenant_for_org(org_guid, tenant) LOG.info(_LI("Cloud Foundry {org_id} mapped to tenant " "{tenant_name}").format(org_id=org_guid, tenant_name=tenant)) token = req.headers['X-Auth-Token'] m_cli = muranoclient(token) try: environment_id = db_cf.get_environment_for_space(space_guid) except AttributeError: body = {'name': 'my_{uuid}'.format(uuid=uuid.uuid4().hex)} env = m_cli.environments.create(body) environment_id = env.id db_cf.set_environment_for_space(space_guid, environment_id) LOG.info(_LI("Cloud Foundry {space_id} mapped to {environment_id}") .format(space_id=space_guid, environment_id=environment_id)) package = db_api.package_get(service_id, req.context) LOG.debug('Adding service {name}'.format(name=package.name)) service = self._make_service(space_guid, package, plan_id) db_cf.set_instance_for_service(instance_id, service['?']['id'], environment_id, tenant) # NOTE(Kezar): Here we are going through JSON and add ids where # it's necessary. Before that we need to drop '?' key from parameters # dictionary as far it contains murano package related info which is # necessary in our scenario if '?' in parameters.keys(): parameters.pop('?', None) LOG.warning(_LW("Incorrect input parameters. Package related " "parameters shouldn't be passed through Cloud " "Foundry")) params = [parameters] while params: a = params.pop() for k, v in six.iteritems(a): if isinstance(v, dict): params.append(v) if k == '?': v['id'] = uuid.uuid4().hex service.update(parameters) # Now we need to obtain session to modify the env session_id = create_session(m_cli, environment_id) m_cli.services.post(environment_id, path='/', data=service, session_id=session_id) m_cli.sessions.deploy(environment_id, session_id) self.current_session = session_id return response.Response(status=202, json_body={})
def test_package_make_public(self): id = api.package_upload(self._stub_package(), self.tenant_id).id patch = self.get_change("replace", ["is_public"], True) api.package_update(id, [patch], self.context) package = api.package_get(id, self.context) self.assertEqual(True, package.is_public)
def download(self, req, package_id): target = {'package_id': package_id} policy.check("download_package", req.context, target) package = db_api.package_get(package_id, req.context) return package.archive
def provision(self, req, body, instance_id): """Here is the example of request body given us from Cloud Foundry: { "service_id": "service-guid-here", "plan_id": "plan-guid-here", "organization_guid": "org-guid-here", "space_guid": "space-guid-here", "parameters": {"param1": "value1", "param2": "value2"} } """ data = json.loads(req.body) space_guid = data['space_guid'] org_guid = data['organization_guid'] plan_id = data['plan_id'] service_id = data['service_id'] parameters = data['parameters'] self.current_session = None # Here we'll take an entry for CF org and space from db. If we # don't have any entries we will create it from scratch. try: tenant = db_cf.get_tenant_for_org(org_guid) except AttributeError: # FIXME(Kezar): need to find better way to get tenant tenant = CONF.cfapi.tenant db_cf.set_tenant_for_org(org_guid, tenant) LOG.info(_LI("Cloud Foundry {org_id} mapped to tenant " "{tenant_name}").format(org_id=org_guid, tenant_name=tenant)) # Now as we have all parameters we can try to auth user in actual # tenant user, _, keystone = self._check_auth(req, tenant) # Once we get here we were authorized by keystone token = keystone.auth_token m_cli = muranoclient(token) try: environment_id = db_cf.get_environment_for_space(space_guid) except AttributeError: body = {'name': 'my_{uuid}'.format(uuid=uuid.uuid4().hex)} env = m_cli.environments.create(body) environment_id = env.id db_cf.set_environment_for_space(space_guid, environment_id) LOG.info(_LI("Cloud Foundry {space_id} mapped to {environment_id}") .format(space_id=space_guid, environment_id=environment_id)) LOG.debug('Keystone endpoint: {0}'.format(keystone.auth_ref)) tenant_id = keystone.project_id ctx = context.RequestContext(user=user, tenant=tenant_id) package = db_api.package_get(service_id, ctx) LOG.debug('Adding service {name}'.format(name=package.name)) service = self._make_service(space_guid, package, plan_id) db_cf.set_instance_for_service(instance_id, service['?']['id'], environment_id, tenant) # NOTE(Kezar): Here we are going through JSON and add ids where # it's necessary params = [parameters] while params: a = params.pop() for k, v in a.iteritems(): if isinstance(v, dict): params.append(v) if k == '?': v['id'] = uuid.uuid4().hex service.update(parameters) # Now we need to obtain session to modify the env session_id = create_session(m_cli, environment_id) m_cli.services.post(environment_id, path='/', data=service, session_id=session_id) m_cli.sessions.deploy(environment_id, session_id) self.current_session = session_id return response.Response(status=202, json_body={})
def get_supplier_logo(self, req, package_id): package = db_api.package_get(package_id, req.context) return package.supplier_logo
def provision(self, req, body, instance_id): """Here is the example of request body given us from Cloud Foundry: { "service_id": "service-guid-here", "plan_id": "plan-guid-here", "organization_guid": "org-guid-here", "space_guid": "space-guid-here", "parameters": {"param1": "value1", "param2": "value2"} } """ data = json.loads(req.body) space_guid = data['space_guid'] org_guid = data['organization_guid'] plan_id = data['plan_id'] service_id = data['service_id'] parameters = data['parameters'] self.current_session = None # Here we'll take an entry for CF org and space from db. If we # don't have any entries we will create it from scratch. try: tenant = db_cf.get_tenant_for_org(org_guid) except AttributeError: # FIXME(Kezar): need to find better way to get tenant tenant = CONF.cfapi.tenant db_cf.set_tenant_for_org(org_guid, tenant) LOG.info( _LI("Cloud Foundry {org_id} mapped to tenant " "{tenant_name}").format(org_id=org_guid, tenant_name=tenant)) # Now as we have all parameters we can try to auth user in actual # tenant user, _, keystone = self._check_auth(req, tenant) # Once we get here we were authorized by keystone token = keystone.auth_token m_cli = muranoclient(token) try: environment_id = db_cf.get_environment_for_space(space_guid) except AttributeError: body = {'name': 'my_{uuid}'.format(uuid=uuid.uuid4().hex)} env = m_cli.environments.create(body) environment_id = env.id db_cf.set_environment_for_space(space_guid, environment_id) LOG.info( _LI("Cloud Foundry {space_id} mapped to {environment_id}"). format(space_id=space_guid, environment_id=environment_id)) LOG.debug('Keystone endpoint: {0}'.format(keystone.auth_ref)) tenant_id = keystone.project_id ctx = context.RequestContext(user=user, tenant=tenant_id) package = db_api.package_get(service_id, ctx) LOG.debug('Adding service {name}'.format(name=package.name)) service = self._make_service(space_guid, package, plan_id) db_cf.set_instance_for_service(instance_id, service['?']['id'], environment_id, tenant) # NOTE(Kezar): Here we are going through JSON and add ids where # it's necessary params = [parameters] while params: a = params.pop() for k, v in a.iteritems(): if isinstance(v, dict): params.append(v) if k == '?': v['id'] = uuid.uuid4().hex service.update(parameters) # Now we need to obtain session to modify the env session_id = create_session(m_cli, environment_id) m_cli.services.post(environment_id, path='/', data=service, session_id=session_id) m_cli.sessions.deploy(environment_id, session_id) self.current_session = session_id return {}