Coverage for k8splugin/decorators.py : 49%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# ============LICENSE_START=======================================================
2# org.onap.dcae
3# ================================================================================
4# Copyright (c) 2017-2020 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#
20import copy
22from cloudify import ctx
23from cloudify.exceptions import NonRecoverableError, RecoverableError
25from k8splugin import discovery as dis
26from k8splugin import utils
27from k8splugin.exceptions import (DockerPluginDependencyNotReadyError,
28 DockerPluginDeploymentError)
31def monkeypatch_loggers(task_func):
32 """Sets up the dependent loggers"""
34 def wrapper(**kwargs):
35 # Ouch! Monkeypatch loggers
36 dis.logger = ctx.logger
38 return task_func(**kwargs)
40 return wrapper
43def wrap_error_handling_start(task_start_func):
44 """Wrap error handling for the start operations"""
46 def wrapper(**kwargs):
47 try:
48 return task_start_func(**kwargs)
49 except DockerPluginDependencyNotReadyError as e:
50 # You are here because things we need like a working docker host is not
51 # available yet so let Cloudify try again later.
52 raise RecoverableError(e)
53 except DockerPluginDeploymentError as e:
54 # Container failed to come up in the allotted time. This is deemed
55 # non-recoverable.
56 raise NonRecoverableError(e)
57 except Exception as e:
58 ctx.logger.error("Unexpected error while starting container: {0}"
59 .format(str(e)))
60 raise NonRecoverableError(e)
62 return wrapper
65def _wrapper_merge_inputs(task_func, properties, **kwargs):
66 """Merge Cloudify properties with input kwargs before calling task func"""
67 inputs = copy.deepcopy(properties)
68 # Recursively update
69 utils.update_dict(inputs, kwargs)
71 # Apparently kwargs contains "ctx" which is cloudify.context.CloudifyContext
72 # This has to be removed and not copied into runtime_properties else you get
73 # JSON serialization errors.
74 if "ctx" in inputs: 74 ↛ 75line 74 didn't jump to line 75, because the condition on line 74 was never true
75 del inputs["ctx"]
77 return task_func(**inputs)
79def merge_inputs_for_create(task_create_func):
80 """Merge all inputs for start operation into one dict"""
82 # Needed to wrap the wrapper because I was seeing issues with
83 # "RuntimeError: No context set in current execution thread"
84 def wrapper(**kwargs):
85 # NOTE: ctx.node.properties is an ImmutableProperties instance which is
86 # why it is passed into a mutable dict so that it can be deep copied
87 return _wrapper_merge_inputs(task_create_func,
88 dict(ctx.node.properties), **kwargs)
90 return wrapper
92def merge_inputs_for_start(task_start_func):
93 """Merge all inputs for start operation into one dict"""
95 # Needed to wrap the wrapper because I was seeing issues with
96 # "RuntimeError: No context set in current execution thread"
97 def wrapper(**kwargs):
98 return _wrapper_merge_inputs(task_start_func,
99 ctx.instance.runtime_properties, **kwargs)
101 return wrapper
103def wrap_error_handling_update(update_func):
104 """ Wrap error handling for update operations (scale and upgrade) """
106 def wrapper(**kwargs):
107 try:
108 return update_func(**kwargs)
109 except DockerPluginDeploymentError:
110 raise NonRecoverableError ("Update operation did not complete successfully in the alloted time")
111 except Exception as e:
112 ctx.logger.error ("Unexpected error during update operation: {0}".format(str(e)))
113 raise NonRecoverableError(e)
115 return wrapper