# Copyright (C) 2017-2018 Lars Wirzenius # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . import ick2 class ProjectAPI(ick2.ResourceApiBase): def __init__(self, state): super().__init__('projects', state) self._ps = ick2.ProjectStatus(self.get_state()) def mangle_new_resource(self, resource): new = dict(resource) if 'next_build_id' not in new: new['next_build_id'] = None return new def mangle_updated_resource(self, old, new): new = dict(new) new['next_build_id'] = old.get('next_build_id') return new def get_resource_name(self, resource): return resource['project'] def get_routes(self, path): # pragma: no cover return super().get_routes(path) + self.get_status_routes(path) def get_status_routes(self, path): # pragma: no cover all_statuses_path = '/status' status_path = '{}//status'.format(path) trigger_path = '{}//+trigger'.format(path) return [ { 'method': 'GET', 'path': all_statuses_path, 'callback': self.GET(self.get_all_statuses), }, { 'method': 'GET', 'path': status_path, 'callback': self.GET(self.get_status), }, { 'method': 'PUT', 'path': status_path, 'callback': self.PUT(self.set_status_callback), }, { 'needs-authorization': False, 'method': 'GET', 'path': trigger_path, 'callback': self.GET(self.trigger_project), }, ] def get_all_statuses(self, **kwargs): # pragma: no cover all_projects = self.list() projects = all_projects['projects'] ick2.log.log( 'trace', msg_text='get_all_statuses', projects=projects) return { project['project']: self.get_status(project['project']) for project in projects } def get_status(self, project, **kwargs): ick2.log.log('trace', msg_text='get_status', project=project) _ = self._state.get_resource(self._type_name, project) ps = self._ps.get_instance(project) return { 'status': ps.get('status', 'idle'), } def set_status_callback(self, body, project, **kwargs): # pragma: no cover return self.set_status(project, body['status']) def set_status(self, project, status): ick2.log.log( 'trace', msg_text='Setting project status', project=project, status=status) allowed_changes = { 'idle': 'triggered', 'triggered': 'building', 'building': 'idle', } p = self._state.get_resource(self._type_name, project) ick2.log.log('trace', msg_text='Found project', project=p) ps = self._ps.get_instance(project) old_status = ps.get('status', 'idle') if allowed_changes[old_status] != status: raise ick2.WrongProjectStatus(status) ps['status'] = status self._ps.update_instance(project, ps) return {'status': status} # This needs to go away as it is not protected. Once an IDP is # added. def trigger_project(self, project, **kwargs): # pragma: no cover return self.set_status(project, 'triggered')