Debian Codegraph @

ansible_collections/community/general/plugins/modules/manageiq_policies.py

raw
1 #!/usr/bin/python
2 # Copyright (c) 2017, Daniel Korn <[email protected]>
3 # Copyright (c) 2017, Yaacov Zamir <[email protected]>
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: manageiq_policies
11
12 short_description: Management of resource policy_profiles in ManageIQ
13 extends_documentation_fragment:
14 - community.general._manageiq
15 - community.general._attributes
16
17 author: Daniel Korn (@dkorn)
18 description:
19 - The manageiq_policies module supports adding and deleting policy_profiles in ManageIQ.
20 attributes:
21 check_mode:
22 support: none
23 diff_mode:
24 support: none
25
26 options:
27 state:
28 type: str
29 description:
30 - V(absent) - policy_profiles should not exist,
31 - V(present) - policy_profiles should exist.
32 choices: ['absent', 'present']
33 default: 'present'
34 policy_profiles:
35 type: list
36 elements: dict
37 description:
38 - List of dictionaries, each includes the policy_profile V(name) key.
39 - Required if O(state) is V(present) or V(absent).
40 resource_type:
41 type: str
42 description:
43 - The type of the resource to which the profile should be [un]assigned.
44 required: true
45 choices:
46 - provider
47 - host
48 - vm
49 - blueprint
50 - category
51 - cluster
52 - data store
53 - group
54 - resource pool
55 - service
56 - service template
57 - template
58 - tenant
59 - user
60 resource_name:
61 type: str
62 description:
63 - The name of the resource to which the profile should be [un]assigned.
64 - Must be specified if O(resource_id) is not set. Both options are mutually exclusive.
65 resource_id:
66 type: int
67 description:
68 - The ID of the resource to which the profile should be [un]assigned.
69 - Must be specified if O(resource_name) is not set. Both options are mutually exclusive.
70 version_added: 2.2.0
71 """
72
73 EXAMPLES = r"""
74 - name: Assign new policy_profile for a provider in ManageIQ
75 community.general.manageiq_policies:
76 resource_name: 'EngLab'
77 resource_type: 'provider'
78 policy_profiles:
79 - name: openscap profile
80 manageiq_connection:
81 url: 'http://127.0.0.1:3000'
82 username: 'admin'
83 password: 'smartvm'
84 validate_certs: false # only do this when you trust the network!
85
86 - name: Unassign a policy_profile for a provider in ManageIQ
87 community.general.manageiq_policies:
88 state: absent
89 resource_name: 'EngLab'
90 resource_type: 'provider'
91 policy_profiles:
92 - name: openscap profile
93 manageiq_connection:
94 url: 'http://127.0.0.1:3000'
95 username: 'admin'
96 password: 'smartvm'
97 validate_certs: false # only do this when you trust the network!
98 """
99
100 RETURN = r"""
101 manageiq_policies:
102 description:
103 - List current policy_profile and policies for a provider in ManageIQ.
104 returned: always
105 type: dict
106 sample:
107 {
108 "changed": false,
109 "profiles": [
110 {
111 "policies": [
112 {
113 "active": true,
114 "description": "OpenSCAP",
115 "name": "openscap policy"
116 },
117 {
118 "active": true,
119 "description": "Analyse incoming container images",
120 "name": "analyse incoming container images"
121 },
122 {
123 "active": true,
124 "description": "Schedule compliance after smart state analysis",
125 "name": "schedule compliance after smart state analysis"
126 }
127 ],
128 "profile_description": "OpenSCAP profile",
129 "profile_name": "openscap profile"
130 }
131 ]
132 }
133 """
134
135 from ansible.module_utils.basic import AnsibleModule
136
137 from ansible_collections.community.general.plugins.module_utils._manageiq import (
138 ManageIQ,
139 manageiq_argument_spec,
140 manageiq_entities,
141 )
142
143
144 def main():
145 actions = {"present": "assign", "absent": "unassign"}
146 argument_spec = dict(
147 policy_profiles=dict(type="list", elements="dict"),
148 resource_id=dict(type="int"),
149 resource_name=dict(type="str"),
150 resource_type=dict(required=True, type="str", choices=list(manageiq_entities().keys())),
151 state=dict(type="str", choices=["present", "absent"], default="present"),
152 )
153 # add the manageiq connection arguments to the arguments
154 argument_spec.update(manageiq_argument_spec())
155
156 module = AnsibleModule(
157 argument_spec=argument_spec,
158 mutually_exclusive=[["resource_id", "resource_name"]],
159 required_one_of=[["resource_id", "resource_name"]],
160 required_if=[("state", "present", ["policy_profiles"]), ("state", "absent", ["policy_profiles"])],
161 )
162
163 policy_profiles = module.params["policy_profiles"]
164 resource_id = module.params["resource_id"]
165 resource_type_key = module.params["resource_type"]
166 resource_name = module.params["resource_name"]
167 state = module.params["state"]
168
169 # get the action and resource type
170 action = actions[state]
171 resource_type = manageiq_entities()[resource_type_key]
172
173 manageiq = ManageIQ(module)
174 manageiq_policies = manageiq.policies(resource_id, resource_type, resource_name)
175
176 # assign or unassign the profiles
177 res_args = manageiq_policies.assign_or_unassign_profiles(policy_profiles, action)
178
179 module.exit_json(**res_args)
180
181
182 if __name__ == "__main__":
183 main()