obj.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # pylint: skip-file
  2. # pylint: disable=too-many-branches
  3. def main():
  4. '''
  5. ansible oc module for services
  6. '''
  7. module = AnsibleModule(
  8. argument_spec=dict(
  9. kubeconfig=dict(default='/etc/origin/master/admin.kubeconfig', type='str'),
  10. state=dict(default='present', type='str',
  11. choices=['present', 'absent', 'list']),
  12. debug=dict(default=False, type='bool'),
  13. namespace=dict(default='default', type='str'),
  14. name=dict(default=None, type='str'),
  15. files=dict(default=None, type='list'),
  16. kind=dict(required=True,
  17. type='str',
  18. choices=['dc', 'deploymentconfig',
  19. 'svc', 'service',
  20. 'scc', 'securitycontextconstraints',
  21. 'ns', 'namespace', 'project', 'projects',
  22. 'is', 'imagestream',
  23. 'istag', 'imagestreamtag',
  24. 'bc', 'buildconfig',
  25. 'routes',
  26. 'node',
  27. 'secret',
  28. ]),
  29. delete_after=dict(default=False, type='bool'),
  30. content=dict(default=None, type='dict'),
  31. force=dict(default=False, type='bool'),
  32. ),
  33. mutually_exclusive=[["content", "files"]],
  34. supports_check_mode=True,
  35. )
  36. ocobj = OCObject(module.params['kind'],
  37. module.params['namespace'],
  38. module.params['name'],
  39. kubeconfig=module.params['kubeconfig'],
  40. verbose=module.params['debug'])
  41. state = module.params['state']
  42. api_rval = ocobj.get()
  43. #####
  44. # Get
  45. #####
  46. if state == 'list':
  47. module.exit_json(changed=False, results=api_rval['results'], state="list")
  48. if not module.params['name']:
  49. module.fail_json(msg='Please specify a name when state is absent|present.')
  50. ########
  51. # Delete
  52. ########
  53. if state == 'absent':
  54. if not Utils.exists(api_rval['results'], module.params['name']):
  55. module.exit_json(changed=False, state="absent")
  56. if module.check_mode:
  57. module.exit_json(change=False, msg='Would have performed a delete.')
  58. api_rval = ocobj.delete()
  59. module.exit_json(changed=True, results=api_rval, state="absent")
  60. if state == 'present':
  61. ########
  62. # Create
  63. ########
  64. if not Utils.exists(api_rval['results'], module.params['name']):
  65. if module.check_mode:
  66. module.exit_json(change=False, msg='Would have performed a create.')
  67. # Create it here
  68. api_rval = ocobj.create(module.params['files'], module.params['content'])
  69. if api_rval['returncode'] != 0:
  70. module.fail_json(msg=api_rval)
  71. # return the created object
  72. api_rval = ocobj.get()
  73. if api_rval['returncode'] != 0:
  74. module.fail_json(msg=api_rval)
  75. # Remove files
  76. if module.params['files'] and module.params['delete_after']:
  77. Utils.cleanup(module.params['files'])
  78. module.exit_json(changed=True, results=api_rval, state="present")
  79. ########
  80. # Update
  81. ########
  82. # if a file path is passed, use it.
  83. update = ocobj.needs_update(module.params['files'], module.params['content'])
  84. if not isinstance(update, bool):
  85. module.fail_json(msg=update)
  86. # No changes
  87. if not update:
  88. if module.params['files'] and module.params['delete_after']:
  89. Utils.cleanup(module.params['files'])
  90. module.exit_json(changed=False, results=api_rval['results'][0], state="present")
  91. if module.check_mode:
  92. module.exit_json(change=False, msg='Would have performed an update.')
  93. api_rval = ocobj.update(module.params['files'],
  94. module.params['content'],
  95. module.params['force'])
  96. if api_rval['returncode'] != 0:
  97. module.fail_json(msg=api_rval)
  98. # return the created object
  99. api_rval = ocobj.get()
  100. if api_rval['returncode'] != 0:
  101. module.fail_json(msg=api_rval)
  102. module.exit_json(changed=True, results=api_rval, state="present")
  103. module.exit_json(failed=True,
  104. changed=False,
  105. results='Unknown state passed. %s' % state,
  106. state="unknown")
  107. # pylint: disable=redefined-builtin, unused-wildcard-import, wildcard-import, locally-disabled
  108. # import module snippets. This are required
  109. from ansible.module_utils.basic import *
  110. main()