From 9eccc96586a605f01b491fe62a679c6797b40e5f 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) --- lib/ansible/module_utils/k8s/raw.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/ansible/module_utils/k8s/raw.py b/lib/ansible/module_utils/k8s/raw.py index 29d681f118..4bc2fa3e44 100644 --- a/lib/ansible/module_utils/k8s/raw.py +++ b/lib/ansible/module_utils/k8s/raw.py @@ -26,7 +26,7 @@ from ansible.module_utils.common.dict_transformations import dict_merge try: import yaml - from openshift.dynamic.exceptions import DynamicApiError, NotFoundError, ConflictError + from openshift.dynamic.exceptions import DynamicApiError, NotFoundError, ConflictError, ForbiddenError except ImportError: # Exceptions handled in common pass @@ -122,7 +122,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' @@ -132,6 +132,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) @@ -166,6 +171,9 @@ 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 + 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 result['changed'] = True result['method'] = 'create' @@ -205,3 +213,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