Special case project creation so that it is possible (#42132)

(cherry picked from commit 9eccc96586)
pull/43346/head
Fabian von Feilitzsch 6 years ago committed by Matt Clay
parent 134ed24474
commit 953cd53f08

@ -23,7 +23,7 @@ from ansible.module_utils.k8s.common import KubernetesAnsibleModule
try: try:
from openshift.dynamic.exceptions import DynamicApiError, NotFoundError, ConflictError from openshift.dynamic.exceptions import DynamicApiError, NotFoundError, ConflictError, ForbiddenError
except ImportError: except ImportError:
# Exception handled in common # Exception handled in common
pass pass
@ -111,7 +111,7 @@ class KubernetesRawModule(KubernetesAnsibleModule):
self.remove_aliases() self.remove_aliases()
if definition['kind'].endswith('list'): if definition['kind'].endswith('List'):
result['result'] = resource.get(namespace=namespace).to_dict() result['result'] = resource.get(namespace=namespace).to_dict()
result['changed'] = False result['changed'] = False
result['method'] = 'get' result['method'] = 'get'
@ -121,6 +121,11 @@ class KubernetesRawModule(KubernetesAnsibleModule):
existing = resource.get(name=name, namespace=namespace) existing = resource.get(name=name, namespace=namespace)
except NotFoundError: except NotFoundError:
pass 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: except DynamicApiError as exc:
self.fail_json(msg='Failed to retrieve requested object: {0}'.format(exc.body), self.fail_json(msg='Failed to retrieve requested object: {0}'.format(exc.body),
error=exc.status, status=exc.status, reason=exc.reason) 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 \ 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)) if the resource you are creating does not directly create a resource of the same kind.".format(name))
return result 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['changed'] = True
result['method'] = 'create' result['method'] = 'create'
return result return result
@ -188,3 +196,18 @@ class KubernetesRawModule(KubernetesAnsibleModule):
result['method'] = 'patch' result['method'] = 'patch'
result['diff'] = diffs result['diff'] = diffs
return result 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

Loading…
Cancel
Save