| 1 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 |
# not use this file except in compliance with the License. You may obtain |
| 3 |
# a copy of the License at |
| 4 |
# |
| 5 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
# |
| 7 |
# Unless required by applicable law or agreed to in writing, software |
| 8 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 |
# License for the specific language governing permissions and limitations |
| 11 |
# under the License. |
| 12 |
|
| 13 |
import warnings |
| 14 |
|
| 15 |
from mistral_lib import serialization |
| 16 |
|
| 17 |
|
| 18 |
class ActionContext(serialization.MistralSerializable): |
| 19 |
|
| 20 |
def __init__(self, security_ctx=None, execution_ctx=None): |
| 21 |
self.security = security_ctx |
| 22 |
self.execution = execution_ctx |
| 23 |
|
| 24 |
def _deprecation_warning(self, name): |
| 25 |
warnings.warn( |
| 26 |
"context.{0} is deprecated from the context passed to actions. " |
| 27 |
"Please use context.security.{0}. It will be removed in a future " |
| 28 |
"release.", DeprecationWarning |
| 29 |
) |
| 30 |
|
| 31 |
def __getattribute__(self, name): |
| 32 |
deprecated = [ |
| 33 |
"auth_cacert", "auth_token", "auth_uri", "expires_at", "insecure", |
| 34 |
"is_target", "is_trust_scoped", "project_id", "project_name", |
| 35 |
"redelivered", "region_name", "service_catalog", "trust_id", |
| 36 |
"user_name" |
| 37 |
] |
| 38 |
|
| 39 |
if name in deprecated: |
| 40 |
self._deprecation_warning(name) |
| 41 |
|
| 42 |
return getattr(self.security, name) |
| 43 |
|
| 44 |
return super(ActionContext, self).__getattribute__(name) |
| 45 |
|
| 46 |
|
| 47 |
class SecurityContext(object): |
| 48 |
def __init__(self, auth_uri=None, auth_cacert=None, insecure=None, |
| 49 |
service_catalog=None, region_name=None, is_trust_scoped=None, |
| 50 |
redelivered=None, expires_at=None, trust_id=None, |
| 51 |
is_target=None, project_id=None, project_name=None, |
| 52 |
user_name=None, auth_token=None): |
| 53 |
self.auth_uri = auth_uri |
| 54 |
self.auth_cacert = auth_cacert |
| 55 |
self.insecure = insecure |
| 56 |
self.service_catalog = service_catalog |
| 57 |
self.region_name = region_name |
| 58 |
self.is_trust_scoped = is_trust_scoped |
| 59 |
self.redelivered = redelivered |
| 60 |
self.expires_at = expires_at |
| 61 |
self.trust_id = trust_id |
| 62 |
self.is_target = is_target |
| 63 |
self.project_id = project_id |
| 64 |
self.project_name = project_name |
| 65 |
self.user_name = user_name |
| 66 |
self.auth_token = auth_token |
| 67 |
|
| 68 |
|
| 69 |
class ExecutionContext(object): |
| 70 |
def __init__(self, workflow_execution_id=None, task_execution_id=None, |
| 71 |
action_execution_id=None, workflow_name=None, |
| 72 |
callback_url=None, task_id=None, with_items_index=0, |
| 73 |
task_rerun_no=0, task_rerun_id=None, |
| 74 |
workflow_propagated_headers=None): |
| 75 |
self.workflow_execution_id = workflow_execution_id |
| 76 |
self.task_execution_id = task_execution_id |
| 77 |
self.action_execution_id = action_execution_id |
| 78 |
self.workflow_name = workflow_name |
| 79 |
self.callback_url = callback_url |
| 80 |
self.with_items_index = with_items_index |
| 81 |
self.task_rerun_no = task_rerun_no |
| 82 |
self.task_rerun_id = task_rerun_id |
| 83 |
self.workflow_propagated_headers = workflow_propagated_headers |
| 84 |
|
| 85 |
if task_id is not None: |
| 86 |
self.task_execution_id = task_id |
| 87 |
self._deprecate_task_id_warning() |
| 88 |
|
| 89 |
def _deprecate_task_id_warning(self): |
| 90 |
warnings.warn( |
| 91 |
"context.execution.task_id was deprecated in the Queens cycle. " |
| 92 |
"Please use context.execution.task_execution_id. It will be " |
| 93 |
"removed in a future release.", DeprecationWarning |
| 94 |
) |
| 95 |
|
| 96 |
@property |
| 97 |
def task_id(self): |
| 98 |
self._deprecate_task_id_warning() |
| 99 |
|
| 100 |
return self.task_execution_id |
| 101 |
|
| 102 |
|
| 103 |
class ActionContextSerializer(serialization.DictBasedSerializer): |
| 104 |
def serialize_to_dict(self, entity): |
| 105 |
return { |
| 106 |
'security': vars(entity.security), |
| 107 |
'execution': vars(entity.execution), |
| 108 |
} |
| 109 |
|
| 110 |
def deserialize_from_dict(self, entity_dict): |
| 111 |
return ActionContext( |
| 112 |
security_ctx=SecurityContext(**entity_dict['security']), |
| 113 |
execution_ctx=ExecutionContext(**entity_dict['execution']) |
| 114 |
) |
| 115 |
|
| 116 |
|
| 117 |
serialization.register_serializer(ActionContext, ActionContextSerializer()) |