Skip to content

Commit

Permalink
Special case project creation so that it is possible (ansible#42132)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianvf authored and maxamillion committed Jul 17, 2018
1 parent 5bcdf4f commit 9eccc96
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions lib/ansible/module_utils/k8s/raw.py
Expand Up @@ -26,7 +26,7 @@

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
Expand Down Expand Up @@ -122,7 +122,7 @@ def perform_action(self, resource, definition):

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'
Expand All @@ -132,6 +132,11 @@ def perform_action(self, resource, definition):
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)
Expand Down Expand Up @@ -166,6 +171,9 @@ def perform_action(self, resource, definition):
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'
Expand Down Expand Up @@ -205,3 +213,18 @@ def perform_action(self, resource, definition):
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

0 comments on commit 9eccc96

Please sign in to comment.