From 799d4b7a6dca3ce986a105d2bc099dfabff05632 Mon Sep 17 00:00:00 2001 From: Jeffrey van Pelt Date: Wed, 4 Jan 2023 10:55:17 +0100 Subject: [PATCH 01/10] Rebased --- plugins/modules/organization.py | 121 +++++++++++++++++++++++++++++++- 1 file changed, 120 insertions(+), 1 deletion(-) diff --git a/plugins/modules/organization.py b/plugins/modules/organization.py index c597c183b..d6efe120a 100644 --- a/plugins/modules/organization.py +++ b/plugins/modules/organization.py @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- # (c) 2016, Eric D Helms # (c) 2017, Matthias M Dellweg (ATIX AG) +# (c) 2022, Jeffrey van Pelt # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -30,6 +31,7 @@ author: - "Eric D Helms (@ehelms)" - "Matthias M Dellweg (@mdellweg) ATIX AG" + - "Jeffrey van Pelt (@Thulium-Drake)" options: name: description: @@ -54,6 +56,56 @@ aliases: - select_all_types version_added: 3.8.0 + upstream_type: + description: + - Type of upstream content source + required: false + type: str + choices: + - 'redhat_cdn' + - 'network_sync' + - 'export_sync' + upstream_url: + description: + - URL of the upstream resource + - Required when I(upstream_type) is 'redhat_cdn' or 'network_sync' + required: false + type: str + upstream_ca_cert: + description: + - SSL CA certificate used to validate I(upstream_url) + required: false + type: str + upstream_username: + description: + - Username to authenticate to the upstream Foreman server + - Required when I(upstream_type) is 'network_sync' + required: false + type: str + upstream_password: + description: + - Password to authenticate to the upstream Foreman server + - Required when I(upstream_type) is 'network_sync' + required: false + type: str + upstream_organization: + description: + - Organization in the upstream Foreman server to synchronize + - Required when I(upstream_type) is 'network_sync' + required: false + type: str + upstream_content_view: + description: + - Content View in the upstream Foreman server to synchronize + - Required when I(upstream_type) is 'network_sync' + required: false + type: str + upstream_lifecycle_environment: + description: + - Lifecycle Environment in the upstream Foreman server to synchronize + - Required when I(upstream_type) is 'network_sync' + required: false + type: str extends_documentation_fragment: - theforeman.foreman.foreman - theforeman.foreman.foreman.entity_state @@ -68,6 +120,38 @@ server_url: "https://foreman.example.com" name: "My Cool New Organization" state: present + +- name: "Configure Red Hat CDN on a different URL' + theforeman.foreman.organization: + username: "admin" + password: "changeme" + server_url: "https://foreman.example.com" + name: "My Cool New Organization" + upstream_type: "redhat_cdn" + upstream_url: "https://internal-cdn.example.com" + +- name: "Configure ISS Export Sync" + theforeman.foreman.organization: + username: "admin" + password: "changeme" + server_url: "https://foreman.example.com" + name: "My Cool New Organization" + upstream_type: "export_sync" + +- name: "Configure ISS Network Sync" + theforeman.foreman.organization: + username: "admin" + password: "changeme" + server_url: "https://foreman.example.com" + name: "My Cool New Organization" + upstream_type: "network_sync" + upstream_url: "https://upstream-foreman.example.com" + upstream_ca_cert: "Upstream Foreman" + upstream_username: sync_user + upstream_password: changeme2 + upstream_organization: "Default Organization" + upstream_lifecycle_environment: "Library" + upstream_content_view: "Foreman_Network_Sync_View" ''' RETURN = ''' @@ -98,6 +182,14 @@ def main(): label=dict(), ignore_types=dict(type='list', elements='str', required=False, aliases=['select_all_types']), select_all_types=dict(type='list', invisible=True, flat_name='ignore_types'), + upstream_type=dict(required=False, choices=['redhat_cdn', 'export_sync', 'network_sync']), + upstream_url=dict(required=False), + upstream_username=dict(required=False), + upstream_password=dict(required=False,no_log=True), + upstream_ca_cert=dict(required=False, type='entity', resource_type='content_credentials', scope=['organization'), + upstream_organization=dict(required=False), + upstream_lifecycle_environment=dict(required=False), + upstream_content_view=dict(required=False), ), ) @@ -109,7 +201,34 @@ def main(): if entity and 'select_all_types' in entity: entity['ignore_types'] = entity.pop('select_all_types') - module.run() + handle_cdn_configuration = 'upstream_type' in module.foreman_params + + organization = module.lookup_entity('entity') + new_organization = module.run() + + if handle_cdn_configuration: + payload = { + 'id': new_organization['id'], + 'type': module.foreman_params['upstream_type'], + } + + if module.foreman_params['upstream_type'] == 'redhat_cdn': + extra_payload = { + 'url': module.foreman_params['upstream_url'], + } + if module.foreman_params['upstream_type'] == 'network_sync': + extra_payload = { + 'url': module.foreman_params['upstream_url'], + 'ssl_ca_credential_id': module.foreman_params['upstream_ca_cert'], + 'username': module.foreman_params['upstream_username'], + 'password': module.foreman_params['upstream_password'], + 'upstream_organization_label': module.foreman_params['upstream_organization'], + 'upstream_lifecycle_environment_label': module.foreman_params['upstream_lifecycle_environment'], + 'upstream_content_view_label': module.foreman_params['upstream_content_view'], + } + + payload.update(extra_payload) + module.resource_action('organizations', 'cdn_configuration', payload) if __name__ == '__main__': From 72b168143d5abbaae3f0f9cf01bfc9e32a30b8a8 Mon Sep 17 00:00:00 2001 From: Jeffrey van Pelt Date: Fri, 18 Nov 2022 22:57:27 +0100 Subject: [PATCH 02/10] Fix missing () --- plugins/modules/organization.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/modules/organization.py b/plugins/modules/organization.py index d6efe120a..148fb850a 100644 --- a/plugins/modules/organization.py +++ b/plugins/modules/organization.py @@ -186,7 +186,8 @@ def main(): upstream_url=dict(required=False), upstream_username=dict(required=False), upstream_password=dict(required=False,no_log=True), - upstream_ca_cert=dict(required=False, type='entity', resource_type='content_credentials', scope=['organization'), + upstream_ca_cert=dict(required=False, type='entity', resource_type='content_credentials', scope=['organization']), + upstream_organization=dict(required=False), upstream_lifecycle_environment=dict(required=False), upstream_content_view=dict(required=False), From d28c9ad6d758051643b1ee969e165ca11be1daf6 Mon Sep 17 00:00:00 2001 From: Jeffrey van Pelt Date: Sat, 19 Nov 2022 02:15:13 +0100 Subject: [PATCH 03/10] Fixed some stuff with regards to handling payload and existing orgs --- plugins/modules/organization.py | 45 ++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/plugins/modules/organization.py b/plugins/modules/organization.py index 148fb850a..b56bef593 100644 --- a/plugins/modules/organization.py +++ b/plugins/modules/organization.py @@ -208,28 +208,31 @@ def main(): new_organization = module.run() if handle_cdn_configuration: - payload = { - 'id': new_organization['id'], - 'type': module.foreman_params['upstream_type'], - } - - if module.foreman_params['upstream_type'] == 'redhat_cdn': - extra_payload = { - 'url': module.foreman_params['upstream_url'], + if organization: + payload = { + 'id': new_organization['id'], + 'type': module.foreman_params['upstream_type'], } - if module.foreman_params['upstream_type'] == 'network_sync': - extra_payload = { - 'url': module.foreman_params['upstream_url'], - 'ssl_ca_credential_id': module.foreman_params['upstream_ca_cert'], - 'username': module.foreman_params['upstream_username'], - 'password': module.foreman_params['upstream_password'], - 'upstream_organization_label': module.foreman_params['upstream_organization'], - 'upstream_lifecycle_environment_label': module.foreman_params['upstream_lifecycle_environment'], - 'upstream_content_view_label': module.foreman_params['upstream_content_view'], - } - - payload.update(extra_payload) - module.resource_action('organizations', 'cdn_configuration', payload) + extra_payload = {} + + if module.foreman_params['upstream_type'] == 'redhat_cdn': + extra_payload = { + 'url': module.foreman_params['upstream_url'], + } + if module.foreman_params['upstream_type'] == 'network_sync': + extra_payload = { + 'url': module.foreman_params['upstream_url'], + 'ssl_ca_credential_id': module.foreman_params['upstream_ca_cert'], + 'username': module.foreman_params['upstream_username'], + 'password': module.foreman_params['upstream_password'], + 'upstream_organization_label': module.foreman_params['upstream_organization'], + 'upstream_lifecycle_environment_label': module.foreman_params['upstream_lifecycle_environment'], + 'upstream_content_view_label': module.foreman_params['upstream_content_view'], + } + + if extra_payload: + payload.update(extra_payload) + module.resource_action('organizations', 'cdn_configuration', payload) if __name__ == '__main__': From c660f7e809730359a6d58cb226f11a09798e931a Mon Sep 17 00:00:00 2001 From: Jeffrey van Pelt Date: Thu, 24 Nov 2022 15:18:54 +0100 Subject: [PATCH 04/10] Use Content Credential ID for now --- plugins/modules/organization.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/modules/organization.py b/plugins/modules/organization.py index b56bef593..c6786b985 100644 --- a/plugins/modules/organization.py +++ b/plugins/modules/organization.py @@ -186,8 +186,8 @@ def main(): upstream_url=dict(required=False), upstream_username=dict(required=False), upstream_password=dict(required=False,no_log=True), - upstream_ca_cert=dict(required=False, type='entity', resource_type='content_credentials', scope=['organization']), - + #upstream_ca_cert=dict(required=False, type='entity', resource_type='content_credentials', scope=['']), + upstream_ca_cert_id=dict(required=False), upstream_organization=dict(required=False), upstream_lifecycle_environment=dict(required=False), upstream_content_view=dict(required=False), @@ -222,7 +222,7 @@ def main(): if module.foreman_params['upstream_type'] == 'network_sync': extra_payload = { 'url': module.foreman_params['upstream_url'], - 'ssl_ca_credential_id': module.foreman_params['upstream_ca_cert'], + 'ssl_ca_credential_id': module.foreman_params['upstream_ca_cert_id'], 'username': module.foreman_params['upstream_username'], 'password': module.foreman_params['upstream_password'], 'upstream_organization_label': module.foreman_params['upstream_organization'], From de4955e140bd8074110b5dee8af3c7c6e7278555 Mon Sep 17 00:00:00 2001 From: Jeffrey van Pelt Date: Mon, 28 Nov 2022 00:24:47 +0100 Subject: [PATCH 05/10] Processed feedback --- plugins/modules/organization.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/modules/organization.py b/plugins/modules/organization.py index c6786b985..8069cda76 100644 --- a/plugins/modules/organization.py +++ b/plugins/modules/organization.py @@ -185,8 +185,8 @@ def main(): upstream_type=dict(required=False, choices=['redhat_cdn', 'export_sync', 'network_sync']), upstream_url=dict(required=False), upstream_username=dict(required=False), - upstream_password=dict(required=False,no_log=True), - #upstream_ca_cert=dict(required=False, type='entity', resource_type='content_credentials', scope=['']), + upstream_password=dict(required=False, no_log=True), + # upstream_ca_cert=dict(required=False, type='entity', resource_type='content_credentials', scope=['']), upstream_ca_cert_id=dict(required=False), upstream_organization=dict(required=False), upstream_lifecycle_environment=dict(required=False), From 0906f2b1f062f8a6f167979d9ec50b7391a499c4 Mon Sep 17 00:00:00 2001 From: Evgeni Golov Date: Wed, 30 Nov 2022 11:23:19 +0100 Subject: [PATCH 06/10] doc and examples formating fixes --- plugins/modules/organization.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/plugins/modules/organization.py b/plugins/modules/organization.py index 8069cda76..d0fe7f69d 100644 --- a/plugins/modules/organization.py +++ b/plugins/modules/organization.py @@ -62,13 +62,13 @@ required: false type: str choices: - - 'redhat_cdn' - - 'network_sync' - - 'export_sync' + - redhat_cdn + - network_sync + - export_sync upstream_url: description: - URL of the upstream resource - - Required when I(upstream_type) is 'redhat_cdn' or 'network_sync' + - Required when I(upstream_type) is C(redhat_cdn) or C(network_sync) required: false type: str upstream_ca_cert: @@ -79,31 +79,31 @@ upstream_username: description: - Username to authenticate to the upstream Foreman server - - Required when I(upstream_type) is 'network_sync' + - Required when I(upstream_type) is C(network_sync) required: false type: str upstream_password: description: - Password to authenticate to the upstream Foreman server - - Required when I(upstream_type) is 'network_sync' + - Required when I(upstream_type) is C(network_sync) required: false type: str upstream_organization: description: - Organization in the upstream Foreman server to synchronize - - Required when I(upstream_type) is 'network_sync' + - Required when I(upstream_type) is C(network_sync) required: false type: str upstream_content_view: description: - Content View in the upstream Foreman server to synchronize - - Required when I(upstream_type) is 'network_sync' + - Required when I(upstream_type) is C(network_sync) required: false type: str upstream_lifecycle_environment: description: - Lifecycle Environment in the upstream Foreman server to synchronize - - Required when I(upstream_type) is 'network_sync' + - Required when I(upstream_type) is C(network_sync) required: false type: str extends_documentation_fragment: @@ -121,7 +121,7 @@ name: "My Cool New Organization" state: present -- name: "Configure Red Hat CDN on a different URL' +- name: "Configure Red Hat CDN on a different URL" theforeman.foreman.organization: username: "admin" password: "changeme" From a7e5305afb97165192fc5b710ac8d8137aa8226a Mon Sep 17 00:00:00 2001 From: Jeffrey van Pelt Date: Wed, 30 Nov 2022 13:54:57 +0100 Subject: [PATCH 07/10] Made it idempotent --- plugins/modules/organization.py | 48 ++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/plugins/modules/organization.py b/plugins/modules/organization.py index d0fe7f69d..904dc49d9 100644 --- a/plugins/modules/organization.py +++ b/plugins/modules/organization.py @@ -207,29 +207,35 @@ def main(): organization = module.lookup_entity('entity') new_organization = module.run() - if handle_cdn_configuration: - if organization: - payload = { - 'id': new_organization['id'], - 'type': module.foreman_params['upstream_type'], + if handle_cdn_configuration and not module.desired_absent: + payload = { + 'id': organization['id'], + } + extra_payload = { + 'type': module.foreman_params['upstream_type'], + } + + if module.foreman_params['upstream_type'] == 'redhat_cdn': + cdn_config = { + 'url': module.foreman_params['upstream_url'], } - extra_payload = {} - - if module.foreman_params['upstream_type'] == 'redhat_cdn': - extra_payload = { - 'url': module.foreman_params['upstream_url'], - } - if module.foreman_params['upstream_type'] == 'network_sync': - extra_payload = { - 'url': module.foreman_params['upstream_url'], - 'ssl_ca_credential_id': module.foreman_params['upstream_ca_cert_id'], - 'username': module.foreman_params['upstream_username'], - 'password': module.foreman_params['upstream_password'], - 'upstream_organization_label': module.foreman_params['upstream_organization'], - 'upstream_lifecycle_environment_label': module.foreman_params['upstream_lifecycle_environment'], - 'upstream_content_view_label': module.foreman_params['upstream_content_view'], - } + extra_payload.update(cdn_config) + if module.foreman_params['upstream_type'] == 'network_sync': + cdn_config = { + 'url': module.foreman_params['upstream_url'], + 'ssl_ca_credential_id': module.foreman_params['upstream_ca_cert_id'], + 'username': module.foreman_params['upstream_username'], + 'password': module.foreman_params['upstream_password'], + 'upstream_organization_label': module.foreman_params['upstream_organization'], + 'upstream_lifecycle_environment_label': module.foreman_params['upstream_lifecycle_environment'], + 'upstream_content_view_label': module.foreman_params['upstream_content_view'], + } + extra_payload.update(cdn_config) + + current_cdn_config = {k: v for k, v in organization['cdn_configuration'].items() if v is not None} + del current_cdn_config['password_exists'] + if current_cdn_config != extra_payload: if extra_payload: payload.update(extra_payload) module.resource_action('organizations', 'cdn_configuration', payload) From 7fe76bae69a5b58fe3f6f90aed3c630214696f56 Mon Sep 17 00:00:00 2001 From: Jeffrey van Pelt Date: Wed, 30 Nov 2022 13:57:23 +0100 Subject: [PATCH 08/10] Fix mangled merge --- plugins/modules/organization.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugins/modules/organization.py b/plugins/modules/organization.py index 904dc49d9..abd960b00 100644 --- a/plugins/modules/organization.py +++ b/plugins/modules/organization.py @@ -204,8 +204,7 @@ def main(): handle_cdn_configuration = 'upstream_type' in module.foreman_params - organization = module.lookup_entity('entity') - new_organization = module.run() + organization = module.run() if handle_cdn_configuration and not module.desired_absent: payload = { From c5aa5006f6e8d6167e1a94f3033201f897917b3e Mon Sep 17 00:00:00 2001 From: Jeffrey van Pelt Date: Wed, 4 Jan 2023 11:11:40 +0100 Subject: [PATCH 09/10] Remove workaround with ID --- plugins/modules/organization.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/plugins/modules/organization.py b/plugins/modules/organization.py index abd960b00..28dae3f1e 100644 --- a/plugins/modules/organization.py +++ b/plugins/modules/organization.py @@ -186,8 +186,7 @@ def main(): upstream_url=dict(required=False), upstream_username=dict(required=False), upstream_password=dict(required=False, no_log=True), - # upstream_ca_cert=dict(required=False, type='entity', resource_type='content_credentials', scope=['']), - upstream_ca_cert_id=dict(required=False), + upstream_ca_cert=dict(required=False, type='entity', resource_type='content_credentials', scope=['organization']), upstream_organization=dict(required=False), upstream_lifecycle_environment=dict(required=False), upstream_content_view=dict(required=False), @@ -222,7 +221,7 @@ def main(): if module.foreman_params['upstream_type'] == 'network_sync': cdn_config = { 'url': module.foreman_params['upstream_url'], - 'ssl_ca_credential_id': module.foreman_params['upstream_ca_cert_id'], + 'ssl_ca_credential_id': module.foreman_params['upstream_ca_cert'], 'username': module.foreman_params['upstream_username'], 'password': module.foreman_params['upstream_password'], 'upstream_organization_label': module.foreman_params['upstream_organization'], From 458f2a89557452ec1db685c3770b01ec79253cef Mon Sep 17 00:00:00 2001 From: Evgeni Golov Date: Fri, 26 Jul 2024 11:05:50 +0200 Subject: [PATCH 10/10] add custom_cdn support --- plugins/modules/organization.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/plugins/modules/organization.py b/plugins/modules/organization.py index 28dae3f1e..c62986336 100644 --- a/plugins/modules/organization.py +++ b/plugins/modules/organization.py @@ -65,6 +65,7 @@ - redhat_cdn - network_sync - export_sync + - custom_cdn upstream_url: description: - URL of the upstream resource @@ -106,6 +107,11 @@ - Required when I(upstream_type) is C(network_sync) required: false type: str + upstream_custom_cdn_auth_enabled: + description: + - If product certificates should be used to authenticate to a custom CDN. + type: bool + required: false extends_documentation_fragment: - theforeman.foreman.foreman - theforeman.foreman.foreman.entity_state @@ -182,7 +188,7 @@ def main(): label=dict(), ignore_types=dict(type='list', elements='str', required=False, aliases=['select_all_types']), select_all_types=dict(type='list', invisible=True, flat_name='ignore_types'), - upstream_type=dict(required=False, choices=['redhat_cdn', 'export_sync', 'network_sync']), + upstream_type=dict(required=False, choices=['redhat_cdn', 'export_sync', 'network_sync', 'custom_cdn']), upstream_url=dict(required=False), upstream_username=dict(required=False), upstream_password=dict(required=False, no_log=True), @@ -190,6 +196,7 @@ def main(): upstream_organization=dict(required=False), upstream_lifecycle_environment=dict(required=False), upstream_content_view=dict(required=False), + upstream_custom_cdn_auth_enabled=dict(required=False, type='bool'), ), ) @@ -229,6 +236,18 @@ def main(): 'upstream_content_view_label': module.foreman_params['upstream_content_view'], } extra_payload.update(cdn_config) + if module.foreman_params['upstream_type'] == 'custom_cdn': + cdn_config = { + 'url': module.foreman_params['upstream_url'], + 'ssl_ca_credential_id': module.foreman_params['upstream_ca_cert'], + 'username': module.foreman_params['upstream_username'], + 'password': module.foreman_params['upstream_password'], + 'upstream_organization_label': module.foreman_params['upstream_organization'], + 'upstream_lifecycle_environment_label': module.foreman_params['upstream_lifecycle_environment'], + 'upstream_content_view_label': module.foreman_params['upstream_content_view'], + 'custom_cdn_auth_enabled': module.foreman_params['upstream_custom_cdn_auth_enabled'], + } + extra_payload.update(cdn_config) current_cdn_config = {k: v for k, v in organization['cdn_configuration'].items() if v is not None} del current_cdn_config['password_exists']