| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
from __future__ import absolute_import, division, print_function |
| 19 |
__metaclass__ = type |
| 20 |
|
| 21 |
|
| 22 |
DOCUMENTATION = ''' |
| 23 |
--- |
| 24 |
module: subscription_manifest |
| 25 |
version_added: 1.0.0 |
| 26 |
short_description: Manage Subscription Manifests |
| 27 |
description: |
| 28 |
- Upload, refresh and delete Subscription Manifests |
| 29 |
author: "Andrew Kofink (@akofink)" |
| 30 |
options: |
| 31 |
manifest_path: |
| 32 |
description: |
| 33 |
- Path to the manifest zip file |
| 34 |
- This parameter will be ignored if I(state=absent) or I(state=refreshed) |
| 35 |
type: path |
| 36 |
state: |
| 37 |
description: |
| 38 |
- The state of the manifest |
| 39 |
default: present |
| 40 |
choices: |
| 41 |
- absent |
| 42 |
- present |
| 43 |
- refreshed |
| 44 |
type: str |
| 45 |
repository_url: |
| 46 |
description: |
| 47 |
- URL to retrieve content from |
| 48 |
aliases: [ redhat_repository_url ] |
| 49 |
type: str |
| 50 |
attributes: |
| 51 |
check_mode: |
| 52 |
support: none |
| 53 |
diff_mode: |
| 54 |
support: partial |
| 55 |
extends_documentation_fragment: |
| 56 |
- theforeman.foreman.foreman |
| 57 |
- theforeman.foreman.foreman.organization |
| 58 |
''' |
| 59 |
|
| 60 |
EXAMPLES = ''' |
| 61 |
- name: "Upload the RHEL developer edition manifest" |
| 62 |
theforeman.foreman.subscription_manifest: |
| 63 |
username: "admin" |
| 64 |
password: "changeme" |
| 65 |
server_url: "https://foreman.example.com" |
| 66 |
organization: "Default Organization" |
| 67 |
state: present |
| 68 |
manifest_path: "/tmp/manifest.zip" |
| 69 |
''' |
| 70 |
|
| 71 |
RETURN = ''' # ''' |
| 72 |
|
| 73 |
from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import KatelloEntityAnsibleModule |
| 74 |
|
| 75 |
|
| 76 |
def main(): |
| 77 |
module = KatelloEntityAnsibleModule( |
| 78 |
argument_spec=dict( |
| 79 |
manifest_path=dict(type='path'), |
| 80 |
state=dict(default='present', choices=['absent', 'present', 'refreshed']), |
| 81 |
repository_url=dict(aliases=['redhat_repository_url']), |
| 82 |
), |
| 83 |
foreman_spec=dict( |
| 84 |
organization=dict(type='entity', required=True, thin=False), |
| 85 |
), |
| 86 |
required_if=[ |
| 87 |
['state', 'present', ['manifest_path']], |
| 88 |
], |
| 89 |
supports_check_mode=False, |
| 90 |
) |
| 91 |
|
| 92 |
module.task_timeout = 10 * 60 |
| 93 |
|
| 94 |
with module.api_connection(): |
| 95 |
organization = module.lookup_entity('organization') |
| 96 |
scope = module.scope_for('organization') |
| 97 |
|
| 98 |
try: |
| 99 |
existing_manifest = organization['owner_details']['upstreamConsumer'] |
| 100 |
except KeyError: |
| 101 |
existing_manifest = None |
| 102 |
|
| 103 |
if module.state == 'present': |
| 104 |
if 'repository_url' in module.foreman_params: |
| 105 |
payload = {'redhat_repository_url': module.foreman_params['repository_url']} |
| 106 |
org_spec = dict(id=dict(), redhat_repository_url=dict()) |
| 107 |
organization = module.ensure_entity('organizations', payload, organization, state='present', foreman_spec=org_spec) |
| 108 |
|
| 109 |
try: |
| 110 |
with open(module.foreman_params['manifest_path'], 'rb') as manifest_file: |
| 111 |
files = {'content': (module.foreman_params['manifest_path'], manifest_file, 'application/zip')} |
| 112 |
params = {} |
| 113 |
if 'repository_url' in module.foreman_params: |
| 114 |
params['repository_url'] = module.foreman_params['repository_url'] |
| 115 |
params.update(scope) |
| 116 |
result = module.resource_action('subscriptions', 'upload', params, files=files, record_change=False, ignore_task_errors=True) |
| 117 |
for error in result['humanized']['errors']: |
| 118 |
if "same as existing data" in error: |
| 119 |
|
| 120 |
break |
| 121 |
if "older than existing data" in error: |
| 122 |
module.fail_json(msg="Manifest is older than existing data.") |
| 123 |
else: |
| 124 |
module.fail_json(msg="Upload of the manifest failed: %s" % error) |
| 125 |
else: |
| 126 |
module.set_changed() |
| 127 |
except IOError as e: |
| 128 |
module.fail_json(msg="Unable to read the manifest file: %s" % e) |
| 129 |
elif module.desired_absent and existing_manifest: |
| 130 |
module.resource_action('subscriptions', 'delete_manifest', scope) |
| 131 |
elif module.state == 'refreshed': |
| 132 |
if existing_manifest: |
| 133 |
module.resource_action('subscriptions', 'refresh_manifest', scope) |
| 134 |
else: |
| 135 |
module.fail_json(msg="No manifest found to refresh.") |
| 136 |
|
| 137 |
|
| 138 |
if __name__ == '__main__': |
| 139 |
main() |