From 98f53abe7299f8b0258138ed522b4bc8b042265f Mon Sep 17 00:00:00 2001 From: Jenny Liu Date: Thu, 8 Nov 2018 07:55:13 -0500 Subject: [PATCH 1/2] add cumulus_context to task event message --- CHANGELOG.md | 8 +++- examples/messages/cumulus_context.input.json | 24 ++++++++++++ examples/messages/cumulus_context.output.json | 37 +++++++++++++++++++ message_adapter/message_adapter.py | 13 ++++--- message_adapter/version.py | 2 +- tests/test-module.py | 14 +++++++ 6 files changed, 91 insertions(+), 7 deletions(-) create mode 100644 examples/messages/cumulus_context.input.json create mode 100644 examples/messages/cumulus_context.output.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 4859097..5e27d52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +## [v1.0.13] - 2018-11-08 + +### Added +- Add `cumulus_context` property from `cumulus_meta[cumulus_context]` to task event message [CUMULUS-906] + ## [v1.0.12] - 2018-09-28 ### Fixed @@ -147,7 +152,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Added - Initial release -[Unreleased]: https://github.com/nasa/cumulus-message-adapter/compare/v1.0.5...HEAD +[Unreleased]: https://github.com/nasa/cumulus-message-adapter/compare/v1.0.13...HEAD +[v1.0.13]: https://github.com/nasa/cumulus-message-adapter/compare/v1.0.12...v1.0.13 [v1.0.5]: https://github.com/nasa/cumulus-message-adapter/compare/v1.0.4...v1.0.5 [v1.0.4]: https://github.com/nasa/cumulus-message-adapter/compare/v1.0.3...v1.0.4 [v1.0.3]: https://github.com/nasa/cumulus-message-adapter/compare/v1.0.2...v1.0.3 diff --git a/examples/messages/cumulus_context.input.json b/examples/messages/cumulus_context.input.json new file mode 100644 index 0000000..f0b0537 --- /dev/null +++ b/examples/messages/cumulus_context.input.json @@ -0,0 +1,24 @@ +{ + "workflow_config": { + "Example": { + "inlinestr": "prefix{meta.foo}suffix", + "array": "{[$.meta.foo]}", + "object": "{{$.meta}}" + } + }, + "cumulus_meta": { + "message_source": "sfn", + "state_machine": "arn:aws:states:us-east-1:1234:stateMachine:MySfn", + "execution_name": "MyExecution__id-1234", + "id": "id-1234", + "cumulus_context": { + "anycontext": "anycontextvalue" + } + }, + "meta": { + "foo": "bar" + }, + "payload": { + "anykey": "anyvalue" + } +} diff --git a/examples/messages/cumulus_context.output.json b/examples/messages/cumulus_context.output.json new file mode 100644 index 0000000..0ebb2f0 --- /dev/null +++ b/examples/messages/cumulus_context.output.json @@ -0,0 +1,37 @@ +{ + "workflow_config": { + "Example": { + "inlinestr": "prefix{meta.foo}suffix", + "array": "{[$.meta.foo]}", + "object": "{{$.meta}}" + } + }, + "cumulus_meta": { + "message_source": "sfn", + "state_machine": "arn:aws:states:us-east-1:1234:stateMachine:MySfn", + "execution_name": "MyExecution__id-1234", + "id": "id-1234", + "cumulus_context": { + "anycontext": "anycontextvalue" + } + }, + "meta": { + "foo": "bar" + }, + "payload": { + "input": { "anykey": "anyvalue" }, + "config": { + "inlinestr": "prefixbarsuffix", + "array": ["bar"], + "object": { "foo": "bar" } + }, + "cumulus_config": { + "state_machine": "arn:aws:states:us-east-1:1234:stateMachine:MySfn", + "execution_name": "MyExecution__id-1234" + }, + "cumulus_context": { + "anycontext": "anycontextvalue" + } + }, + "exception": "None" +} diff --git a/message_adapter/message_adapter.py b/message_adapter/message_adapter.py index d0650f1..a0e65c8 100644 --- a/message_adapter/message_adapter.py +++ b/message_adapter/message_adapter.py @@ -328,11 +328,14 @@ def loadNestedEvent(self, event, context): # add cumulus_config property, only selective attributes from event.cumulus_meta are added attributes = ['state_machine', 'execution_name'] - if ('cumulus_meta' in event - and all(attribute in event['cumulus_meta'] for attribute in attributes)): - response['cumulus_config'] = {} - for attribute in attributes: - response['cumulus_config'][attribute] = event['cumulus_meta'][attribute] + if 'cumulus_meta' in event: + if all(attribute in event['cumulus_meta'] for attribute in attributes): + response['cumulus_config'] = {} + for attribute in attributes: + response['cumulus_config'][attribute] = event['cumulus_meta'][attribute] + + if 'cumulus_context' in event['cumulus_meta']: + response['cumulus_context'] = event['cumulus_meta']['cumulus_context'] return response diff --git a/message_adapter/version.py b/message_adapter/version.py index 1fb88d0..efb1fc0 100644 --- a/message_adapter/version.py +++ b/message_adapter/version.py @@ -1 +1 @@ -__version__ = 'v1.0.12' +__version__ = 'v1.0.13' diff --git a/tests/test-module.py b/tests/test-module.py index 1ba7d79..cef5b50 100644 --- a/tests/test-module.py +++ b/tests/test-module.py @@ -345,6 +345,20 @@ def test_templates(self): result = self.cumulus_message_adapter.createNextEvent(msg, in_msg, messageConfig) assert result == out_msg + @patch.object(cumulus_message_adapter, '_message_adapter__getCurrentSfnTask', return_value="Example") + def test_cumulus_context(self, getCurrentSfnTask_function): + """ test storing cumulus_context metadata """ + inp = open(os.path.join(self.test_folder, 'cumulus_context.input.json')) + out = open(os.path.join(self.test_folder, 'cumulus_context.output.json')) + in_msg = json.loads(inp.read()) + out_msg = json.loads(out.read()) + + msg = self.cumulus_message_adapter.loadNestedEvent(in_msg, {}) + messageConfig = msg.get('messageConfig') + if 'messageConfig' in msg: del msg['messageConfig'] + result = self.cumulus_message_adapter.createNextEvent(msg, in_msg, messageConfig) + assert result == out_msg + def test_input_jsonschema(self): """ test a working input schema """ inp = open(os.path.join(self.test_folder, 'templates.input.json')) From e3093318432e5277626e0aaff2aa7fbf95c8c228 Mon Sep 17 00:00:00 2001 From: Jenny Liu Date: Thu, 8 Nov 2018 09:49:16 -0500 Subject: [PATCH 2/2] add cumulus_context to cumulus_config property --- CHANGELOG.md | 2 +- examples/messages/cumulus_context.output.json | 8 ++++---- message_adapter/message_adapter.py | 11 ++++++++--- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e27d52..ec269ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [v1.0.13] - 2018-11-08 ### Added -- Add `cumulus_context` property from `cumulus_meta[cumulus_context]` to task event message [CUMULUS-906] +- Add `cumulus_context` attribute from `cumulus_meta[cumulus_context]` to property `cumulus_config` of task event message [CUMULUS-906] ## [v1.0.12] - 2018-09-28 diff --git a/examples/messages/cumulus_context.output.json b/examples/messages/cumulus_context.output.json index 0ebb2f0..37a124e 100644 --- a/examples/messages/cumulus_context.output.json +++ b/examples/messages/cumulus_context.output.json @@ -27,10 +27,10 @@ }, "cumulus_config": { "state_machine": "arn:aws:states:us-east-1:1234:stateMachine:MySfn", - "execution_name": "MyExecution__id-1234" - }, - "cumulus_context": { - "anycontext": "anycontextvalue" + "execution_name": "MyExecution__id-1234", + "cumulus_context": { + "anycontext": "anycontextvalue" + } } }, "exception": "None" diff --git a/message_adapter/message_adapter.py b/message_adapter/message_adapter.py index a0e65c8..9bc1b98 100644 --- a/message_adapter/message_adapter.py +++ b/message_adapter/message_adapter.py @@ -327,15 +327,20 @@ def loadNestedEvent(self, event, context): response['messageConfig'] = config['cumulus_message'] # add cumulus_config property, only selective attributes from event.cumulus_meta are added - attributes = ['state_machine', 'execution_name'] if 'cumulus_meta' in event: + response['cumulus_config'] = {} + # add both attributes or none of them + attributes = ['state_machine', 'execution_name'] if all(attribute in event['cumulus_meta'] for attribute in attributes): - response['cumulus_config'] = {} for attribute in attributes: response['cumulus_config'][attribute] = event['cumulus_meta'][attribute] + # add attribute cumulus_context if 'cumulus_context' in event['cumulus_meta']: - response['cumulus_context'] = event['cumulus_meta']['cumulus_context'] + response['cumulus_config']['cumulus_context'] = event['cumulus_meta']['cumulus_context'] + + if not response['cumulus_config']: + del response['cumulus_config'] return response