From 953cd53f086653d9a3819116a96d9a242ab9e744 Mon Sep 17 00:00:00 2001 From: Fabian von Feilitzsch Date: Tue, 17 Jul 2018 14:35:21 -0400 Subject: [PATCH] Special case project creation so that it is possible (#42132) (cherry picked from commit 9eccc96586a605f01b491fe62a679c6797b40e5f) --- lib/ansible/module_utils/k8s/raw.py | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/ansible/module_utils/k8s/raw.py b/lib/ansible/module_utils/k8s/raw.py index 2ce90d649f4..2b8a8d10054 100644 --- a/lib/ansible/module_utils/k8s/raw.py +++ b/lib/ansible/module_utils/k8s/raw.py @@ -23,7 +23,7 @@ from ansible.module_utils.k8s.common import KubernetesAnsibleModule try: - from openshift.dynamic.exceptions import DynamicApiError, NotFoundError, ConflictError + from openshift.dynamic.exceptions import DynamicApiError, NotFoundError, ConflictError, ForbiddenError except ImportError: # Exception handled in common pass @@ -111,7 +111,7 @@ class KubernetesRawModule(KubernetesAnsibleModule): self.remove_aliases() - if definition['kind'].endswith('list'): + if definition['kind'].endswith('List'): result['result'] = resource.get(namespace=namespace).to_dict() result['changed'] = False result['method'] = 'get' @@ -121,6 +121,11 @@ class KubernetesRawModule(KubernetesAnsibleModule): existing = resource.get(name=name, namespace=namespace) except NotFoundError: pass + except ForbiddenError as exc: + if definition['kind'] in ['Project', 'ProjectRequest'] and state != 'absent': + return self.create_project_request(definition) + self.fail_json(msg='Failed to retrieve requested object: {0}'.format(exc.body), + error=exc.status, status=exc.status, reason=exc.reason) except DynamicApiError as exc: self.fail_json(msg='Failed to retrieve requested object: {0}'.format(exc.body), error=exc.status, status=exc.status, reason=exc.reason) @@ -153,7 +158,10 @@ class KubernetesRawModule(KubernetesAnsibleModule): self.warn("{0} was not found, but creating it returned a 409 Conflict error. This can happen \ if the resource you are creating does not directly create a resource of the same kind.".format(name)) return result - result['result'] = k8s_obj.to_dict() + except DynamicApiError as exc: + self.fail_json(msg="Failed to create object: {0}".format(exc.body), + error=exc.status, status=exc.status, reason=exc.reason) + result['result'] = k8s_obj.to_dict() result['changed'] = True result['method'] = 'create' return result @@ -188,3 +196,18 @@ class KubernetesRawModule(KubernetesAnsibleModule): result['method'] = 'patch' result['diff'] = diffs return result + + def create_project_request(self, definition): + definition['kind'] = 'ProjectRequest' + result = {'changed': False, 'result': {}} + resource = self.find_resource('ProjectRequest', definition['apiVersion'], fail=True) + if not self.check_mode: + try: + k8s_obj = resource.create(definition) + result['result'] = k8s_obj.to_dict() + except DynamicApiError as exc: + self.fail_json(msg="Failed to create object: {0}".format(exc.body), + error=exc.status, status=exc.status, reason=exc.reason) + result['changed'] = True + result['method'] = 'create' + return result