# Copyright (c) 2019 OpenStack Foundation. # All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. import json import jsonschema from pecan import abort from pecan import expose from pecan import request from pecan import response from pecan import rest from oslo_log import log from oslo_serialization import jsonutils from fenix.api.v1 import maintenance from fenix.api.v1 import schema from fenix import policy LOG = log.getLogger(__name__) class ProjectController(rest.RestController): name = 'project' def __init__(self): self.engine_rpcapi = maintenance.EngineRPCAPI() # GET /v1/maintenance// @policy.authorize('maintenance:session:project', 'get') @expose(content_type='application/json') def get(self, session_id, project_id): if request.body: LOG.error("Unexpected data") abort(400) try: jsonschema.validate(session_id, schema.uid) jsonschema.validate(project_id, schema.uid) except jsonschema.exceptions.ValidationError as e: LOG.error(str(e.message)) abort(422) engine_data = self.engine_rpcapi.project_get_session(session_id, project_id) try: response.text = jsonutils.dumps(engine_data) except TypeError: response.body = jsonutils.dumps(engine_data) # PUT /v1/maintenance// @policy.authorize('maintenance:session:project', 'put') @expose(content_type='application/json') def put(self, session_id, project_id): data = json.loads(request.body.decode('utf8')) try: jsonschema.validate(session_id, schema.uid) jsonschema.validate(project_id, schema.uid) jsonschema.validate(data, schema.maintenance_session_project_put) except jsonschema.exceptions.ValidationError as e: LOG.error(str(e.message)) abort(422) engine_data = self.engine_rpcapi.project_update_session(session_id, project_id, data) try: response.text = jsonutils.dumps(engine_data) except TypeError: response.body = jsonutils.dumps(engine_data) class ProjectInstanceController(rest.RestController): name = 'project_instance' def __init__(self): self.engine_rpcapi = maintenance.EngineRPCAPI() # PUT /v1/maintenance/// @policy.authorize('maintenance:session:project:instance', 'put') @expose(content_type='application/json') def put(self, session_id, project_id, instance_id): data = json.loads(request.body.decode('utf8')) try: jsonschema.validate(session_id, schema.uid) jsonschema.validate(project_id, schema.uid) jsonschema.validate(instance_id, schema.uid) jsonschema.validate( data, schema.maintenance_session_project_instance_put) except jsonschema.exceptions.ValidationError as e: LOG.error(str(e.message)) abort(422) engine_data = ( self.engine_rpcapi.project_update_session_instance(session_id, project_id, instance_id, data)) try: response.text = jsonutils.dumps(engine_data) except TypeError: response.body = jsonutils.dumps(engine_data) class SessionController(rest.RestController): name = 'session' def __init__(self): self.engine_rpcapi = maintenance.EngineRPCAPI() # GET /v1/maintenance/ @policy.authorize('maintenance:session', 'get') @expose(content_type='application/json') def get(self, session_id): try: jsonschema.validate(session_id, schema.uid) except jsonschema.exceptions.ValidationError as e: LOG.error(str(e.message)) abort(422) if request.body: LOG.error("Unexpected data") abort(400) session = self.engine_rpcapi.admin_get_session(session_id) if session is None: LOG.error("Invalid session") abort(404) try: response.text = jsonutils.dumps(session) except TypeError: response.body = jsonutils.dumps(session) # PUT /v1/maintenance/ @policy.authorize('maintenance:session', 'put') @expose(content_type='application/json') def put(self, session_id): data = json.loads(request.body.decode('utf8')) try: jsonschema.validate(session_id, schema.uid) jsonschema.validate(data, schema.maintenance_session_put) except jsonschema.exceptions.ValidationError as e: LOG.error(str(e.message)) abort(422) engine_data = self.engine_rpcapi.admin_update_session(session_id, data) try: response.text = jsonutils.dumps(engine_data) except TypeError: response.body = jsonutils.dumps(engine_data) # DELETE /v1/maintenance/ @policy.authorize('maintenance:session', 'delete') @expose(content_type='application/json') def delete(self, session_id): try: jsonschema.validate(session_id, schema.uid) except jsonschema.exceptions.ValidationError as e: LOG.error(str(e.message)) abort(422) if request.body: LOG.error("Unexpected data") abort(400) engine_data = self.engine_rpcapi.admin_delete_session(session_id) try: response.text = jsonutils.dumps(engine_data) except TypeError: response.body = jsonutils.dumps(engine_data) class MaintenanceController(rest.RestController): name = 'maintenance' def __init__(self): self.engine_rpcapi = maintenance.EngineRPCAPI() # GET /v1/maintenance @policy.authorize('maintenance', 'get') @expose(content_type='application/json') def get(self): if request.body: LOG.error("Unexpected data") abort(400) sessions = self.engine_rpcapi.admin_get() try: response.text = jsonutils.dumps(sessions) except TypeError: response.body = jsonutils.dumps(sessions) # POST /v1/maintenance @policy.authorize('maintenance', 'post') @expose(content_type='application/json') def post(self): data = json.loads(request.body.decode('utf8')) try: jsonschema.validate(data, schema.maintenance_post) except jsonschema.exceptions.ValidationError as e: LOG.error(str(e.message)) abort(422) session = self.engine_rpcapi.admin_create_session(data) if session is None: LOG.error("Too many sessions") abort(509) try: response.text = jsonutils.dumps(session) except TypeError: response.body = jsonutils.dumps(session) class InstanceController(rest.RestController): name = 'instance' def __init__(self): self.engine_rpcapi = maintenance.EngineRPCAPI() # GET /v1/instance/ @policy.authorize('instance', 'get') @expose(content_type='application/json') def get(self, instance_id): try: jsonschema.validate(instance_id, schema.uid) except jsonschema.exceptions.ValidationError as e: LOG.error(str(e.message)) abort(422) if request.body: LOG.error("Unexpected data") abort(400) instance = self.engine_rpcapi.get_instance(instance_id) if instance is None: LOG.error("Invalid instance: %s" % instance_id) abort(404) try: response.text = jsonutils.dumps(instance) except TypeError: response.body = jsonutils.dumps(instance) # PUT /v1/instance/ @policy.authorize('instance', 'put') @expose(content_type='application/json') def put(self, instance_id): data = json.loads(request.body.decode('utf8')) try: jsonschema.validate(instance_id, schema.uid) jsonschema.validate(data, schema.instance_put) except jsonschema.exceptions.ValidationError as e: LOG.error(str(e.message)) abort(422) engine_data = self.engine_rpcapi.update_instance(instance_id, data) try: response.text = jsonutils.dumps(engine_data) except TypeError: response.body = jsonutils.dumps(engine_data) # DELETE /v1/instance/ @policy.authorize('instance', 'delete') @expose(content_type='application/json') def delete(self, instance_id): try: jsonschema.validate(instance_id, schema.uid) except jsonschema.exceptions.ValidationError as e: LOG.error(str(e.message)) abort(422) if request.body: LOG.error("Unexpected data") abort(400) engine_data = self.engine_rpcapi.delete_instance(instance_id) try: response.text = jsonutils.dumps(engine_data) except TypeError: response.body = jsonutils.dumps(engine_data) class InstanceGroupController(rest.RestController): name = 'instance_group' def __init__(self): self.engine_rpcapi = maintenance.EngineRPCAPI() # GET /v1/instance_group/ @policy.authorize('instance_group', 'get') @expose(content_type='application/json') def get(self, group_id): try: jsonschema.validate(group_id, schema.uid) except jsonschema.exceptions.ValidationError as e: LOG.error(str(e.message)) abort(422) if request.body: LOG.error("Unexpected data") abort(400) group = self.engine_rpcapi.get_instance_group(group_id) if group is None: LOG.error("Invalid instance_group: %s" % group_id) abort(404) try: response.text = jsonutils.dumps(group) except TypeError: response.body = jsonutils.dumps(group) # PUT /v1/instance_group/ @policy.authorize('instance_group', 'put') @expose(content_type='application/json') def put(self, group_id): data = json.loads(request.body.decode('utf8')) try: jsonschema.validate(group_id, schema.uid) jsonschema.validate(data, schema.instance_group_put) except jsonschema.exceptions.ValidationError as e: LOG.error(str(e.message)) abort(422) engine_data = ( self.engine_rpcapi.update_instance_group(group_id, data)) try: response.text = jsonutils.dumps(engine_data) except TypeError: response.body = jsonutils.dumps(engine_data) # DELETE /v1/instance_group/ @policy.authorize('instance_group', 'delete') @expose(content_type='application/json') def delete(self, group_id): try: jsonschema.validate(group_id, schema.uid) except jsonschema.exceptions.ValidationError as e: LOG.error(str(e.message)) abort(422) if request.body: LOG.error("Unexpected data") abort(400) engine_data = ( self.engine_rpcapi.delete_instance_group(group_id)) try: response.text = jsonutils.dumps(engine_data) except TypeError: response.body = jsonutils.dumps(engine_data)