Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2022, Håkon Lerring
4 # GNU General Public License v3.0+ (see COPYING 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: consul_role
11 short_description: Manipulate Consul roles
12 version_added: 7.5.0
13 description:
14 - Allows the addition, modification and deletion of roles in a Consul cluster using the agent. For more details on using
15 and configuring ACLs, see U(https://www.consul.io/docs/guides/acl.html).
16 author:
17 - Håkon Lerring (@Hakon)
18 extends_documentation_fragment:
19 - community.general._consul
20 - community.general._consul.token
21 - community.general._consul.actiongroup_consul
22 - community.general._attributes
23 attributes:
24 check_mode:
25 support: full
26 diff_mode:
27 support: partial
28 details:
29 - In check mode the diff misses operational attributes.
30 version_added: 8.3.0
31 action_group:
32 version_added: 8.3.0
33 options:
34 name:
35 description:
36 - A name used to identify the role.
37 required: true
38 type: str
39 state:
40 description:
41 - Whether the role should be present or absent.
42 choices: ['present', 'absent']
43 default: present
44 type: str
45 description:
46 description:
47 - Description of the role.
48 - If not specified, the assigned description is not changed.
49 type: str
50 policies:
51 type: list
52 elements: dict
53 description:
54 - List of policies to attach to the role. Each policy is a dict.
55 - If the parameter is left blank, any policies currently assigned are not changed.
56 - Any empty array (V([])) clears any policies previously set.
57 suboptions:
58 name:
59 description:
60 - The name of the policy to attach to this role; see M(community.general.consul_policy) for more info.
61 - Either this or O(policies[].id) must be specified.
62 type: str
63 id:
64 description:
65 - The ID of the policy to attach to this role; see M(community.general.consul_policy) for more info.
66 - Either this or O(policies[].name) must be specified.
67 type: str
68 templated_policies:
69 description:
70 - The list of templated policies that should be applied to the role.
71 type: list
72 elements: dict
73 version_added: 8.3.0
74 suboptions:
75 template_name:
76 description:
77 - The templated policy name.
78 type: str
79 required: true
80 template_variables:
81 description:
82 - The templated policy variables.
83 - Not all templated policies require variables.
84 type: dict
85 service_identities:
86 type: list
87 elements: dict
88 description:
89 - List of service identities to attach to the role.
90 - If not specified, any service identities currently assigned are not changed.
91 - If the parameter is an empty array (V([])), any node identities assigned are unassigned.
92 suboptions:
93 service_name:
94 description:
95 - The name of the node.
96 - Must not be longer than 256 characters, must start and end with a lowercase alphanumeric character.
97 - May only contain lowercase alphanumeric characters as well as V(-) and V(_).
98 - This suboption has been renamed from O(service_identities[].name) to O(service_identities[].service_name) in community.general
99 8.3.0. The old name can still be used.
100 type: str
101 required: true
102 aliases:
103 - name
104 datacenters:
105 description:
106 - The datacenters where the policies are effective.
107 - This results in effective policy only being valid in this datacenter.
108 - If an empty array (V([])) is specified, the policies are valid in all datacenters.
109 - Including those which do not yet exist but may in the future.
110 type: list
111 elements: str
112 node_identities:
113 type: list
114 elements: dict
115 description:
116 - List of node identities to attach to the role.
117 - If not specified, any node identities currently assigned are not changed.
118 - If the parameter is an empty array (V([])), any node identities assigned are unassigned.
119 suboptions:
120 node_name:
121 description:
122 - The name of the node.
123 - Must not be longer than 256 characters, must start and end with a lowercase alphanumeric character.
124 - May only contain lowercase alphanumeric characters as well as V(-) and V(_).
125 - This suboption has been renamed from O(node_identities[].name) to O(node_identities[].node_name) in community.general
126 8.3.0. The old name can still be used.
127 type: str
128 required: true
129 aliases:
130 - name
131 datacenter:
132 description:
133 - The nodes datacenter.
134 - This results in effective policy only being valid in this datacenter.
135 type: str
136 required: true
137 """
138
139 EXAMPLES = r"""
140 - name: Create a role with 2 policies
141 community.general.consul_role:
142 host: consul1.example.com
143 token: some_management_acl
144 name: foo-role
145 policies:
146 - id: 783beef3-783f-f41f-7422-7087dc272765
147 - name: "policy-1"
148
149 - name: Create a role with service identity
150 community.general.consul_role:
151 host: consul1.example.com
152 token: some_management_acl
153 name: foo-role-2
154 service_identities:
155 - name: web
156 datacenters:
157 - dc1
158
159 - name: Create a role with node identity
160 community.general.consul_role:
161 host: consul1.example.com
162 token: some_management_acl
163 name: foo-role-3
164 node_identities:
165 - name: node-1
166 datacenter: dc2
167
168 - name: Remove a role
169 community.general.consul_role:
170 host: consul1.example.com
171 token: some_management_acl
172 name: foo-role-3
173 state: absent
174 """
175
176 RETURN = r"""
177 role:
178 description: The role object.
179 returned: success
180 type: dict
181 sample:
182 {
183 "CreateIndex": 39,
184 "Description": "",
185 "Hash": "Trt0QJtxVEfvTTIcdTUbIJRr6Dsi6E4EcwSFxx9tCYM=",
186 "ID": "9a300b8d-48db-b720-8544-a37c0f5dafb5",
187 "ModifyIndex": 39,
188 "Name": "foo-role",
189 "Policies": [
190 {
191 "ID": "b1a00172-d7a1-0e66-a12e-7a4045c4b774",
192 "Name": "foo-access"
193 }
194 ]
195 }
196 operation:
197 description: The operation performed on the role.
198 returned: changed
199 type: str
200 sample: update
201 """
202
203 from ansible.module_utils.basic import AnsibleModule
204
205 from ansible_collections.community.general.plugins.module_utils._consul import (
206 AUTH_ARGUMENTS_SPEC,
207 OPERATION_READ,
208 _ConsulModule,
209 )
210
211
212 class ConsulRoleModule(_ConsulModule):
213 api_endpoint = "acl/role"
214 result_key = "role"
215 unique_identifiers = ["id"]
216
217 def endpoint_url(self, operation, identifier=None):
218 if operation == OPERATION_READ:
219 return [self.api_endpoint, "name", self.params["name"]]
220 return super().endpoint_url(operation, identifier)
221
222
223 NAME_ID_SPEC = dict(
224 name=dict(type="str"),
225 id=dict(type="str"),
226 )
227
228 NODE_ID_SPEC = dict(
229 node_name=dict(type="str", required=True, aliases=["name"]),
230 datacenter=dict(type="str", required=True),
231 )
232
233 SERVICE_ID_SPEC = dict(
234 service_name=dict(type="str", required=True, aliases=["name"]),
235 datacenters=dict(type="list", elements="str"),
236 )
237
238 TEMPLATE_POLICY_SPEC = dict(
239 template_name=dict(type="str", required=True),
240 template_variables=dict(type="dict"),
241 )
242
243 _ARGUMENT_SPEC = {
244 "name": dict(type="str", required=True),
245 "description": dict(type="str"),
246 "policies": dict(
247 type="list",
248 elements="dict",
249 options=NAME_ID_SPEC,
250 mutually_exclusive=[("name", "id")],
251 required_one_of=[("name", "id")],
252 ),
253 "templated_policies": dict(
254 type="list",
255 elements="dict",
256 options=TEMPLATE_POLICY_SPEC,
257 ),
258 "node_identities": dict(
259 type="list",
260 elements="dict",
261 options=NODE_ID_SPEC,
262 ),
263 "service_identities": dict(
264 type="list",
265 elements="dict",
266 options=SERVICE_ID_SPEC,
267 ),
268 "state": dict(default="present", choices=["present", "absent"]),
269 }
270 _ARGUMENT_SPEC.update(AUTH_ARGUMENTS_SPEC)
271
272
273 def main():
274 module = AnsibleModule(
275 _ARGUMENT_SPEC,
276 supports_check_mode=True,
277 )
278 consul_module = ConsulRoleModule(module)
279 consul_module.execute()
280
281
282 if __name__ == "__main__":
283 main()