Refactors common code for new K8s and OpenShift modules (#33646)
* Refactors common code for new k8s and openshift modules * Move Ansible module helper code from OpenShift client
This commit is contained in:
parent
92e52ef515
commit
d629a5ece2
2 changed files with 771 additions and 160 deletions
File diff suppressed because it is too large
Load diff
|
@ -1,5 +1,5 @@
|
|||
#
|
||||
# Copyright 2017 Red Hat | Ansible
|
||||
# Copyright 2018 Red Hat | Ansible
|
||||
#
|
||||
# This file is part of Ansible
|
||||
#
|
||||
|
@ -16,41 +16,50 @@
|
|||
# You should have received a copy of the GNU General Public License
|
||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from ansible.module_utils.k8s_common import KubernetesAnsibleException, KubernetesAnsibleModule
|
||||
import copy
|
||||
|
||||
from ansible.module_utils.k8s_common import KubernetesAnsibleModule, AnsibleMixin, ARG_SPEC
|
||||
|
||||
try:
|
||||
from openshift.helper.ansible import OpenShiftAnsibleModuleHelper, ARG_ATTRIBUTES_BLACKLIST
|
||||
from openshift.helper.exceptions import KubernetesException, OpenShiftException
|
||||
from openshift.helper.openshift import OpenShiftObjectHelper
|
||||
from openshift.helper.exceptions import KubernetesException
|
||||
HAS_OPENSHIFT_HELPER = True
|
||||
except ImportError as exc:
|
||||
class OpenShiftObjectHelper(object):
|
||||
pass
|
||||
HAS_OPENSHIFT_HELPER = False
|
||||
|
||||
|
||||
class OpenShiftAnsibleException(KubernetesAnsibleException):
|
||||
class OpenShiftAnsibleModuleHelper(AnsibleMixin, OpenShiftObjectHelper):
|
||||
pass
|
||||
|
||||
|
||||
class OpenShiftAnsibleModule(KubernetesAnsibleModule):
|
||||
def __init__(self, kind, api_version):
|
||||
def __init__(self):
|
||||
|
||||
if not HAS_OPENSHIFT_HELPER:
|
||||
raise OpenShiftAnsibleException(
|
||||
raise Exception(
|
||||
"This module requires the OpenShift Python client. Try `pip install openshift`"
|
||||
)
|
||||
|
||||
try:
|
||||
super(OpenShiftAnsibleModule, self).__init__(kind, api_version)
|
||||
except KubernetesAnsibleException as exc:
|
||||
raise OpenShiftAnsibleException(exc.args)
|
||||
super(OpenShiftAnsibleModule, self).__init__()
|
||||
|
||||
@staticmethod
|
||||
def get_helper(api_version, kind):
|
||||
return OpenShiftAnsibleModuleHelper(api_version, kind)
|
||||
@property
|
||||
def _argspec(self):
|
||||
return copy.deepcopy(ARG_SPEC)
|
||||
|
||||
def _get_helper(self, api_version, kind):
|
||||
try:
|
||||
helper = OpenShiftAnsibleModuleHelper(api_version=api_version, kind=kind, debug=False)
|
||||
helper.get_model(api_version, kind)
|
||||
return helper
|
||||
except KubernetesException as exc:
|
||||
self.exit_json(msg="Error initializing module helper {}".format(exc.message))
|
||||
|
||||
def _create(self, namespace):
|
||||
if self.kind.lower() == 'project':
|
||||
return self._create_project()
|
||||
else:
|
||||
return super(OpenShiftAnsibleModule, self)._create(namespace)
|
||||
return super(OpenShiftAnsibleModule, self)._create(namespace)
|
||||
|
||||
def _create_project(self):
|
||||
new_obj = None
|
||||
|
|
Loading…
Reference in a new issue