k8s/openshift module_utils split (#23115)

pull/23151/head
Jason DeTiberus 8 years ago committed by Chris Houseknecht
parent a0a26700c9
commit 3642de9ecc

@ -23,10 +23,11 @@ import os
from ansible.module_utils.basic import AnsibleModule
try:
from openshift.helper.ansible import AnsibleModuleHelper, OpenShiftException, ARG_ATTRIBUTES_BLACKLIST
HAS_OPENSHIFT = True
from openshift.helper.ansible import KubernetesAnsibleModuleHelper, ARG_ATTRIBUTES_BLACKLIST
from openshift.helper.exceptions import KubernetesException
HAS_K8S_MODULE_HELPER = True
except ImportError as exc:
HAS_OPENSHIFT = False
HAS_K8S_MODULE_HELPER = False
try:
import yaml
@ -35,31 +36,34 @@ except ImportError:
HAS_YAML = False
class OpenShiftAnsibleException(Exception):
class KubernetesAnsibleException(Exception):
pass
class OpenShiftAnsibleModule(AnsibleModule):
class KubernetesAnsibleModule(AnsibleModule):
@staticmethod
def get_helper(api_version, kind):
return KubernetesAnsibleModuleHelper(api_version, kind)
def __init__(self, kind, api_version):
self.api_version = api_version
self.kind = kind
self.argspec_cache = None
if not HAS_OPENSHIFT:
raise OpenShiftAnsibleException(
if not HAS_K8S_MODULE_HELPER:
raise KubernetesAnsibleException(
"This module requires the OpenShift Python client. Try `pip install openshift`"
)
if not HAS_YAML:
raise OpenShiftAnsibleException(
raise KubernetesAnsibleException(
"This module requires PyYAML. Try `pip install PyYAML`"
)
try:
self.helper = AnsibleModuleHelper(api_version, kind)
self.helper = self.get_helper(api_version, kind)
except Exception as exc:
raise OpenShiftAnsibleException(
raise KubernetesAnsibleException(
"Error initializing AnsibleModuleHelper: {}".format(exc)
)
@ -134,7 +138,7 @@ class OpenShiftAnsibleModule(AnsibleModule):
if value.get('auth_option') and self.params.get(key) is not None:
auth_options[key] = self.params[key]
self.helper.set_client_config(**auth_options)
except OpenShiftException as e:
except KubernetesException as e:
self.fail_json(msg='Error loading config', error=str(e))
if state is None:
@ -156,7 +160,7 @@ class OpenShiftAnsibleModule(AnsibleModule):
# CRUD modules
try:
existing = self.helper.get_object(name, namespace)
except OpenShiftException as exc:
except KubernetesException as exc:
self.fail_json(msg='Failed to retrieve requested object: {}'.format(exc.message),
error=exc.value.get('status'))
@ -169,17 +173,13 @@ class OpenShiftAnsibleModule(AnsibleModule):
if not self.check_mode:
try:
self.helper.delete_object(name, namespace)
except OpenShiftException as exc:
except KubernetesException as exc:
self.fail_json(msg="Failed to delete object: {}".format(exc.message),
error=exc.value.get('status'))
return_attributes['changed'] = True
self.exit_json(**return_attributes)
else:
if not existing:
# create new object
if self.kind.lower() == 'project':
k8s_obj = self._create_project()
else:
k8s_obj = self._create(namespace)
return_attributes[self.kind] = k8s_obj.to_dict()
return_attributes['changed'] = True
@ -191,7 +191,7 @@ class OpenShiftAnsibleModule(AnsibleModule):
if not self.check_mode:
try:
k8s_obj = self.helper.replace_object(name, namespace, body=request_body)
except OpenShiftException as exc:
except KubernetesException as exc:
self.fail_json(msg="Failed to replace object: {}".format(exc.message),
error=exc.value.get('status'))
return_attributes[self.kind] = k8s_obj.to_dict()
@ -202,7 +202,7 @@ class OpenShiftAnsibleModule(AnsibleModule):
k8s_obj = copy.deepcopy(existing)
try:
self.helper.object_from_params(self.params, obj=k8s_obj)
except OpenShiftException as exc:
except KubernetesException as exc:
self.fail_json(msg="Failed to patch object: {}".format(exc.message))
match, diff = self.helper.objects_match(existing, k8s_obj)
if match:
@ -217,7 +217,7 @@ class OpenShiftAnsibleModule(AnsibleModule):
if not self.check_mode:
try:
k8s_obj = self.helper.patch_object(name, namespace, k8s_obj)
except OpenShiftException as exc:
except KubernetesException as exc:
self.fail_json(msg="Failed to patch object: {}".format(exc.message))
return_attributes[self.kind] = k8s_obj.to_dict()
return_attributes['changed'] = True
@ -228,37 +228,21 @@ class OpenShiftAnsibleModule(AnsibleModule):
k8s_obj = None
try:
request_body = self.helper.request_body_from_params(self.params)
except OpenShiftException as exc:
except KubernetesException as exc:
self.fail_json(msg="Failed to create object: {}".format(exc.message))
if not self.check_mode:
try:
k8s_obj = self.helper.create_object(namespace, body=request_body)
except OpenShiftException as exc:
except KubernetesException as exc:
self.fail_json(msg="Failed to create object: {}".format(exc.message),
error=exc.value.get('status'))
return k8s_obj
def _create_project(self):
new_obj = None
k8s_obj = None
try:
new_obj = self.helper.object_from_params(self.params)
except OpenShiftException as exc:
self.fail_json(msg="Failed to create object: {}".format(exc.message))
try:
k8s_obj = self.helper.create_project(metadata=new_obj.metadata,
display_name=self.params.get('display_name'),
description=self.params.get('description'))
except OpenShiftException as exc:
self.fail_json(msg='Failed to retrieve requested object',
error=exc.value.get('status'))
return k8s_obj
def _read(self, name, namespace):
k8s_obj = None
try:
k8s_obj = self.helper.get_object(name, namespace)
except OpenShiftException as exc:
except KubernetesException as exc:
self.fail_json(msg='Failed to retrieve requested object',
error=exc.value.get('status'))
return k8s_obj

@ -0,0 +1,69 @@
#
# Copyright 2017 Red Hat | Ansible
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# 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
try:
from openshift.helper.ansible import OpenShiftAnsibleModuleHelper, ARG_ATTRIBUTES_BLACKLIST
from openshift.helper.exceptions import KubernetesException, OpenShiftException
HAS_OPENSHIFT_HELPER = True
except ImportError as exc:
HAS_OPENSHIFT_HELPER = False
class OpenShiftAnsibleException(KubernetesAnsibleException):
pass
class OpenShiftAnsibleModule(KubernetesAnsibleModule):
def __init__(self, kind, api_version):
if not HAS_OPENSHIFT_HELPER:
raise OpenShiftAnsibleException(
"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)
@staticmethod
def get_helper(api_version, kind):
return OpenShiftAnsibleModuleHelper(api_version, kind)
def _create(self, namespace):
if self.kind.lower() == 'project':
return self._create_project()
else:
return super(OpenShiftAnsibleModule, self)._create(namespace)
def _create_project(self):
new_obj = None
k8s_obj = None
try:
new_obj = self.helper.object_from_params(self.params)
except KubernetesException as exc:
self.fail_json(msg="Failed to create object: {}".format(exc.message))
try:
k8s_obj = self.helper.create_project(metadata=new_obj.metadata,
display_name=self.params.get('display_name'),
description=self.params.get('description'))
except KubernetesException as exc:
self.fail_json(msg='Failed to retrieve requested object',
error=exc.value.get('status'))
return k8s_obj
Loading…
Cancel
Save