2edcc0d41da184741d6ad1b38b00ca518687a46c
[dcaegen2/platform/plugins.git] / k8s / k8splugin / decorators.py
1 # ============LICENSE_START=======================================================
2 # org.onap.dcae
3 # ================================================================================
4 # Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 # ============LICENSE_END=========================================================
18 #
19 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
20
21 import copy
22 from cloudify import ctx
23 from cloudify.exceptions import NonRecoverableError, RecoverableError
24 from k8splugin import discovery as dis
25 from k8splugin.exceptions import DockerPluginDeploymentError, \
26     DockerPluginDependencyNotReadyError
27 from k8splugin import utils
28
29
30 def monkeypatch_loggers(task_func):
31     """Sets up the dependent loggers"""
32
33     def wrapper(**kwargs):
34         # Ouch! Monkeypatch loggers
35         dis.logger = ctx.logger
36
37         return task_func(**kwargs)
38
39     return wrapper
40
41
42 def wrap_error_handling_start(task_start_func):
43     """Wrap error handling for the start operations"""
44
45     def wrapper(**kwargs):
46         try:
47             return task_start_func(**kwargs)
48         except DockerPluginDependencyNotReadyError as e:
49             # You are here because things we need like a working docker host is not
50             # available yet so let Cloudify try again later.
51             raise RecoverableError(e)
52         except DockerPluginDeploymentError as e:
53             # Container failed to come up in the allotted time. This is deemed
54             # non-recoverable.
55             raise NonRecoverableError(e)
56         except Exception as e:
57             ctx.logger.error("Unexpected error while starting container: {0}"
58                     .format(str(e)))
59             raise NonRecoverableError(e)
60
61     return wrapper
62
63
64 def _wrapper_merge_inputs(task_func, properties, **kwargs):
65     """Merge Cloudify properties with input kwargs before calling task func"""
66     inputs = copy.deepcopy(properties)
67     # Recursively update
68     utils.update_dict(inputs, kwargs)
69
70     # Apparently kwargs contains "ctx" which is cloudify.context.CloudifyContext
71     # This has to be removed and not copied into runtime_properties else you get
72     # JSON serialization errors.
73     if "ctx" in inputs:
74         del inputs["ctx"]
75
76     return task_func(**inputs)
77
78 def merge_inputs_for_create(task_create_func):
79     """Merge all inputs for start operation into one dict"""
80
81     # Needed to wrap the wrapper because I was seeing issues with
82     # "RuntimeError: No context set in current execution thread"
83     def wrapper(**kwargs):
84         # NOTE: ctx.node.properties is an ImmutableProperties instance which is
85         # why it is passed into a mutable dict so that it can be deep copied
86         return _wrapper_merge_inputs(task_create_func,
87                 dict(ctx.node.properties), **kwargs)
88
89     return wrapper
90
91 def merge_inputs_for_start(task_start_func):
92     """Merge all inputs for start operation into one dict"""
93
94     # Needed to wrap the wrapper because I was seeing issues with
95     # "RuntimeError: No context set in current execution thread"
96     def wrapper(**kwargs):
97         return _wrapper_merge_inputs(task_start_func,
98                 ctx.instance.runtime_properties, **kwargs)
99
100     return wrapper