Debian Codegraph @

ansible_collections/theforeman/foreman/plugins/modules/smart_proxy.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 from __future__ import absolute_import, division, print_function
18 __metaclass__ = type
19
20
21 DOCUMENTATION = '''
22 ---
23 module: smart_proxy
24 version_added: 1.4.0
25 short_description: Manage Smart Proxies
26 description:
27 - Create, update and delete Smart Proxies
28 author:
29 - "James Stuart (@jstuart)"
30 - "Matthias M Dellweg (@mdellweg)"
31 - "Jeffrey van Pelt (@Thulium-Drake)"
32 options:
33 name:
34 description:
35 - Name of the Smart Proxy
36 required: true
37 type: str
38 lifecycle_environments:
39 description:
40 - Lifecycle Environments synced to the Smart Proxy.
41 - Only available for Katello installations.
42 required: false
43 elements: str
44 type: list
45 url:
46 description:
47 - URL of the Smart Proxy
48 required: true
49 type: str
50 download_policy:
51 description:
52 - The download policy for the Smart Proxy
53 - Only available for Katello installations.
54 - The download policy C(background) is deprecated and not available since Katello 4.3.
55 - The download policy C(streamed) is available since Katello 4.5.
56 choices:
57 - background
58 - immediate
59 - on_demand
60 - streamed
61 - inherit
62 required: false
63 type: str
64 notes:
65 - Even with I(state=present) this module does not install a new Smart Proxy.
66 - It can only associate an existing Smart Proxy listening at the specified I(url).
67 - Consider using I(foreman-installer) to create Smart Proxies.
68 extends_documentation_fragment:
69 - theforeman.foreman.foreman
70 - theforeman.foreman.foreman.entity_state
71 - theforeman.foreman.foreman.taxonomy
72 '''
73
74 EXAMPLES = '''
75 # Create a local Smart Proxy
76 - name: "Create Smart Proxy"
77 theforeman.foreman.smart_proxy:
78 username: "admin"
79 password: "changeme"
80 server_url: "https://{{ ansible_fqdn }}"
81 name: "{{ ansible_fqdn }}"
82 url: "https://{{ ansible_fqdn }}:9090"
83 download_policy: "immediate"
84 lifecycle_environments:
85 - "Development"
86 organizations:
87 - "Default Organization"
88 locations:
89 - "Default Location"
90 state: present
91 '''
92
93 RETURN = '''
94 entity:
95 description: Final state of the affected entities grouped by their type.
96 returned: success
97 type: dict
98 contains:
99 smart_proxies:
100 description: List of smart_proxies.
101 type: list
102 elements: dict
103 '''
104
105
106 from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import ForemanTaxonomicEntityAnsibleModule
107
108
109 class ForemanSmartProxyModule(ForemanTaxonomicEntityAnsibleModule):
110 pass
111
112
113 def main():
114 module = ForemanSmartProxyModule(
115 foreman_spec=dict(
116 name=dict(required=True),
117 url=dict(required=True),
118 lifecycle_environments=dict(required=False, type='entity_list'),
119 download_policy=dict(required=False, choices=['background', 'immediate', 'on_demand', 'streamed', 'inherit']),
120 ),
121 required_plugins=[('katello', ['lifecycle_environments', 'download_policy'])],
122 )
123
124 with module.api_connection():
125 handle_lifecycle_environments = not module.desired_absent and 'lifecycle_environments' in module.foreman_params
126 if handle_lifecycle_environments:
127 module.lookup_entity('lifecycle_environments')
128 lifecycle_environments = module.foreman_params.pop('lifecycle_environments', [])
129
130 smart_proxy = module.lookup_entity('entity')
131 new_smart_proxy = module.run()
132
133 if handle_lifecycle_environments:
134
135 if smart_proxy:
136 payload = {
137 'id': new_smart_proxy['id'],
138 }
139 current_lces = module.resource_action('capsule_content', 'lifecycle_environments', payload, ignore_check_mode=True, record_change=False)
140 else:
141 current_lces = {'results': []}
142
143 desired_environment_ids = set(lifecycle_environment['id'] for lifecycle_environment in lifecycle_environments)
144 current_environment_ids = set(lifecycle_environment['id'] for lifecycle_environment in current_lces['results']) if current_lces else set()
145
146 module.record_before('smart_proxy_content/lifecycle_environment_ids', current_environment_ids)
147 module.record_after('smart_proxy_content/lifecycle_environment_ids', desired_environment_ids)
148 module.record_after_full('smart_proxy_content/lifecycle_environment_ids', desired_environment_ids)
149
150 if desired_environment_ids != current_environment_ids:
151 environment_ids_to_add = desired_environment_ids - current_environment_ids
152 if environment_ids_to_add:
153 for environment_id_to_add in environment_ids_to_add:
154 payload = {
155 'id': new_smart_proxy['id'],
156 'environment_id': environment_id_to_add,
157 }
158 module.resource_action('capsule_content', 'add_lifecycle_environment', payload)
159 environment_ids_to_remove = current_environment_ids - desired_environment_ids
160 if environment_ids_to_remove:
161 for environment_id_to_remove in environment_ids_to_remove:
162 payload = {
163 'id': smart_proxy['id'],
164 'environment_id': environment_id_to_remove,
165 }
166 module.resource_action('capsule_content', 'remove_lifecycle_environment', payload)
167
168
169 if __name__ == '__main__':
170 main()