diff options
author | Lars Wirzenius <liw@liw.fi> | 2017-11-05 16:15:19 +0100 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2017-11-05 16:15:19 +0100 |
commit | eb17e187d100b500bd2575e362ea72f657699aa8 (patch) | |
tree | 7ff62a49484cc6625d872a94dcfb3db6d46b8cdc | |
parent | 9f64f6d8d229fff98e51532a1a8ee1032416af10 (diff) | |
download | ick2-eb17e187d100b500bd2575e362ea72f657699aa8.tar.gz |
Add: PUT to update pipeline state
-rw-r--r-- | ick2/controllerapi.py | 44 | ||||
-rw-r--r-- | ick2/controllerapi_tests.py | 56 |
2 files changed, 99 insertions, 1 deletions
diff --git a/ick2/controllerapi.py b/ick2/controllerapi.py index 4f2872b..91368c1 100644 --- a/ick2/controllerapi.py +++ b/ick2/controllerapi.py @@ -113,7 +113,16 @@ class APIbase: # pragma: no cover content_type=content_type, body=body) if 'raw_uri_path' in kwargs: del kwargs['raw_uri_path'] - body = callback(body, **kwargs) + try: + body = callback(body, **kwargs) + except ick2.NotFound as e: + return not_found(e) + except ick2.WrongPipelineStatus as e: + ick2.log.log( + 'error', + msg_text='Wrong state for pipeline', + exception=str(e)) + return bad_request(e) ick2.log.log('trace', msg_text='returned body', body=repr(body)) return OK(body) return wrapper @@ -239,6 +248,11 @@ class ProjectAPI(ResourceApiBase): 'callback': self.GET(self.get_pipeline), }, { + 'method': 'PUT', + 'path': pipeline_path, + 'callback': self.PUT(self.set_pipeline_callback), + }, + { 'method': 'GET', 'path': builds_path, 'callback': self.GET(self.get_builds), @@ -254,6 +268,27 @@ class ProjectAPI(ResourceApiBase): } raise ick2.NotFound() + def set_pipeline_callback( + self, body, project, pipeline): # pragma: no cover + return self.set_pipeline(body['status'], project, pipeline) + + def set_pipeline(self, state, project, pipeline): + allowed_changes = { + 'idle': 'triggered', + 'triggered': 'building', + 'building': 'idle', + } + p = self._state.get_resource(self._type_name, project) + for pl in p['pipelines']: + if pl['name'] == pipeline: + old_state = pl.get('status', 'idle') + if allowed_changes[old_state] != state: + raise ick2.WrongPipelineStatus(state) + pl['status'] = state + self._state.update_resource(self._type_name, project, p) + return {'status': state} + raise ick2.NotFound() + def get_builds(self, project): p = self._state.get_resource(self._type_name, project) return { @@ -285,6 +320,13 @@ def not_found(error): # pragma: no cover return response(apifw.HTTP_NOT_FOUND, str(error), headers) +def bad_request(error): # pragma: no cover + headers = { + 'Content-Type': 'text/plain', + } + return response(apifw.HTTP_BAD_REQUEST, str(error), headers) + + def created(body): # pragma: no cover headers = { 'Content-Type': 'application/json', diff --git a/ick2/controllerapi_tests.py b/ick2/controllerapi_tests.py index e46137c..c39e76f 100644 --- a/ick2/controllerapi_tests.py +++ b/ick2/controllerapi_tests.py @@ -174,3 +174,59 @@ class ProjectAPITests(unittest.TestCase): api = self.create_api() with self.assertRaises(ick2.NotFound): api.delete('foo') + + def test_updates_pipeline_status(self): + project = { + 'project': 'foo', + 'pipelines': [ + { + 'name': 'build', + 'actions': [ + { + 'shell': 'step-1', + }, + ], + }, + ], + } + api = self.create_api() + api.create(project) + self.assertEqual(api.get_pipeline('foo', 'build'), {'status': 'idle'}) + + print() + with self.assertRaises(ick2.WrongPipelineStatus): + api.set_pipeline('building', 'foo', 'build') + + api.set_pipeline('triggered', 'foo', 'build') + self.assertEqual( + api.get_pipeline('foo', 'build'), + {'status': 'triggered'} + ) + + with self.assertRaises(ick2.WrongPipelineStatus): + api.set_pipeline('idle', 'foo', 'build') + + api.set_pipeline('building', 'foo', 'build') + self.assertEqual( + api.get_pipeline('foo', 'build'), + {'status': 'building'} + ) + + with self.assertRaises(ick2.WrongPipelineStatus): + api.set_pipeline('triggered', 'foo', 'build') + + api.set_pipeline('idle', 'foo', 'build') + self.assertEqual( + api.get_pipeline('foo', 'build'), + {'status': 'idle'} + ) + + def test_raises_error_updating_status_of_missing_pipeline(self): + project = { + 'project': 'foo', + 'pipelines': [], + } + api = self.create_api() + api.create(project) + with self.assertRaises(ick2.NotFound): + api.set_pipeline('idle', 'foo', 'build') |