Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Added CDN configuration to organizations module #1523

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
148 changes: 147 additions & 1 deletion plugins/modules/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
# (c) 2016, Eric D Helms <[email protected]>
# (c) 2017, Matthias M Dellweg <[email protected]> (ATIX AG)
# (c) 2022, Jeffrey van Pelt <[email protected]>
#
# 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
Expand Down Expand Up @@ -30,6 +31,7 @@
author:
- "Eric D Helms (@ehelms)"
- "Matthias M Dellweg (@mdellweg) ATIX AG"
- "Jeffrey van Pelt (@Thulium-Drake)"
options:
name:
description:
Expand All @@ -54,6 +56,62 @@
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
- custom_cdn
upstream_url:
description:
- URL of the upstream resource
- Required when I(upstream_type) is C(redhat_cdn) or C(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 C(network_sync)
required: false
type: str
upstream_password:
description:
- Password to authenticate to the upstream Foreman server
- 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 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 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 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
Expand All @@ -68,6 +126,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 = '''
Expand Down Expand Up @@ -98,6 +188,15 @@ 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', 'custom_cdn']),
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),
upstream_custom_cdn_auth_enabled=dict(required=False, type='bool'),
),
)

Expand All @@ -109,7 +208,54 @@ 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.run()

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.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'],
'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'] == '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']

if current_cdn_config != extra_payload:
if extra_payload:
payload.update(extra_payload)
module.resource_action('organizations', 'cdn_configuration', payload)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I read it right, this essentially always returns changed=True now, as there is no way to "diff" the cdn config?!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The data is shown in the API reply to /katello/api/organizations/:id, so we could diff it… What a mess

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried poking around in the API, but I can't find it.

When I go to https://sat.rh.lab/api/organizations/1/cdn_configuration I get a 404. And it's not listed in the 'overview' at the organizations/<org_id> endpoint either.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attention attention! katello overrides the org controller!

you need to go to /katello/api/organizations/:id, not to /api/organizations/:id, the apipie library will do the right thing for us.

and yes, the cdn_configuration part is only defined for PUT (update) operations, you can't GET it, but the data is part of the normal org show view, if you look at the right controller

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel my Python-Fu is lacking a bit, but I made an attempt to compare some things, and it seems to work well :-)

This will never be idempotent for network_sync as the current password cannot be compared with the provided password.



if __name__ == '__main__':
Expand Down
Loading