Debian Codegraph @

ansible_collections/community/okd/plugins/module_utils/openshift_common.py

raw
1 #!/usr/bin/env python
2
3 from __future__ import absolute_import, division, print_function
4
5 __metaclass__ = type
6
7 import traceback
8 from abc import abstractmethod
9
10 from ansible.module_utils._text import to_native
11
12 try:
13 from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import (
14 get_api_client,
15 )
16 from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import (
17 AnsibleK8SModule,
18 )
19 from ansible_collections.kubernetes.core.plugins.module_utils.k8s.service import (
20 K8sService,
21 diff_objects,
22 )
23 from ansible_collections.kubernetes.core.plugins.module_utils.k8s.runner import (
24 perform_action,
25 validate,
26 )
27 from ansible_collections.kubernetes.core.plugins.module_utils.k8s.resource import (
28 create_definitions,
29 merge_params,
30 flatten_list_kind,
31 )
32 from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import (
33 CoreException,
34 )
35
36 HAS_KUBERNETES_COLLECTION = True
37 k8s_collection_import_exception = None
38 K8S_COLLECTION_ERROR = None
39 except ImportError as e:
40 HAS_KUBERNETES_COLLECTION = False
41 k8s_collection_import_exception = e
42 K8S_COLLECTION_ERROR = traceback.format_exc()
43
44
45 class AnsibleOpenshiftModule(AnsibleK8SModule):
46 def __init__(self, **kwargs):
47 super(AnsibleOpenshiftModule, self).__init__(**kwargs)
48
49 self.client = get_api_client(module=self)
50 self.fail = self.fail_json
51
52 self.svc = K8sService(self.client, self._module)
53 self.find_resource = self.svc.find_resource
54 self.kubernetes_facts = self.svc.find
55
56 if not HAS_KUBERNETES_COLLECTION:
57 self.fail_json(
58 msg="The kubernetes.core collection must be installed",
59 exception=K8S_COLLECTION_ERROR,
60 error=to_native(k8s_collection_import_exception),
61 )
62
63 @property
64 def module(self):
65 return self._module
66
67 @abstractmethod
68 def execute_module(self):
69 pass
70
71 def request(self, *args, **kwargs):
72 return self.client.client.request(*args, **kwargs)
73
74 def set_resource_definitions(self):
75 self.resource_definitions = create_definitions(self.params)
76
77 def perform_action(self, definition, params):
78 return perform_action(self.svc, definition, params)
79
80 def validate(self, definition):
81 validate(self.client, self, definition)
82
83 @staticmethod
84 def merge_params(definition, params):
85 return merge_params(definition, params)
86
87 @staticmethod
88 def flatten_list_kind(definition, params):
89 return flatten_list_kind(definition, params)
90
91 @staticmethod
92 def diff_objects(existing, new):
93 return diff_objects(existing, new)
94
95 def run_module(self):
96 try:
97 self.execute_module()
98 except CoreException as e:
99 self.fail_from_exception(e)