Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_roledefinition_info.py

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2019 Yunge Zhu, (@yungezz)
4 #
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10
11 DOCUMENTATION = '''
12 ---
13 module: azure_rm_roledefinition_info
14 version_added: "0.1.2"
15 short_description: Get Azure Role Definition facts
16 description:
17 - Get facts of Azure Role Definition.
18
19 options:
20 scope:
21 description:
22 - The scope of role definition.
23 required: True
24 type: str
25 id:
26 description:
27 - Role definition id.
28 type: str
29 role_name:
30 description:
31 - Role name.
32 type: str
33 type:
34 description:
35 - Type of role.
36 type: str
37 choices:
38 - system
39 - custom
40
41 extends_documentation_fragment:
42 - azure.azcollection.azure
43
44 author:
45 - Yunge Zhu(@yungezz)
46
47 '''
48
49 EXAMPLES = '''
50 - name: List Role Definitions in scope
51 azure_rm_roledefinition_info:
52 scope: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup
53
54 - name: Get Role Definition by name
55 azure_rm_roledefinition_info:
56 scope: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup
57 role_name: myRoleDefinition
58 '''
59
60 RETURN = '''
61 roledefinitions:
62 description:
63 - A list of Role Definition facts.
64 returned: always
65 type: complex
66 contains:
67 id:
68 description:
69 - Role Definition ID.
70 returned: always
71 type: str
72 sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/providers/Microsoft.Authorization/roleDefinitions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
73 role_name:
74 description:
75 - Role name.
76 returned: always
77 type: str
78 sample: myCustomRoleDefinition
79 name:
80 description:
81 - System assigned role name.
82 returned: always
83 type: str
84 sample: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
85 assignable_scopes:
86 description:
87 - List of assignable scopes of this definition.
88 returned: always
89 type: list
90 sample: [ "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup" ]
91 permissions:
92 description:
93 - List of Role Definition permissions.
94 returned: always
95 type: complex
96 contains:
97 actions:
98 description:
99 - List of allowed actions.
100 returned: always
101 type: list
102 sample: [ 'Microsoft.Compute/virtualMachines/read' ]
103 not_actions:
104 description:
105 - List of denied actions.
106 returned: always
107 type: list
108 sample: [ 'Microsoft.Compute/virtualMachines/write' ]
109 data_actions:
110 description:
111 - List of allowed data actions.
112 returned: always
113 type: list
114 sample: [ 'Microsoft.Storage/storageAccounts/blobServices/containers/blobs/read' ]
115 not_data_actions:
116 description:
117 - List of denied data actions.
118 returned: always
119 type: list
120 sample: [ 'Microsoft.Storage/storageAccounts/blobServices/containers/blobs/write' ]
121 '''
122
123 try:
124 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
125 from azure.mgmt.authorization import AuthorizationManagementClient
126
127 except ImportError:
128 # This is handled in azure_rm_common
129 pass
130
131
132 def roledefinition_to_dict(role):
133 result = dict(
134 id=role.id,
135 name=role.name,
136 type=role.role_type,
137 assignable_scopes=role.assignable_scopes,
138 description=role.description,
139 role_name=role.role_name
140 )
141 if role.permissions:
142 result['permissions'] = [dict(
143 actions=p.actions,
144 not_actions=p.not_actions,
145 data_actions=p.data_actions,
146 not_data_actions=p.not_data_actions
147 ) for p in role.permissions]
148 return result
149
150
151 class AzureRMRoleDefinitionInfo(AzureRMModuleBase):
152 def __init__(self):
153 self.module_arg_spec = dict(
154 scope=dict(
155 type='str',
156 required=True
157 ),
158 role_name=dict(type='str'),
159 id=dict(type='str'),
160 type=dict(
161 type='str',
162 choices=['custom', 'system']
163 ),
164 )
165
166 self.role_name = None
167 self.scope = None
168 self.id = None
169 self.type = None
170
171 self.results = dict(
172 changed=False
173 )
174
175 self._client = None
176
177 super(AzureRMRoleDefinitionInfo, self).__init__(derived_arg_spec=self.module_arg_spec,
178 supports_check_mode=True,
179 supports_tags=False)
180
181 def exec_module(self, **kwargs):
182 """Main module execution method"""
183
184 for key in list(self.module_arg_spec.keys()):
185 if hasattr(self, key):
186 setattr(self, key, kwargs[key])
187
188 if self.type:
189 self.type = self.get_role_type(self.type)
190
191 # get management client
192 self._client = self.get_mgmt_svc_client(AuthorizationManagementClient,
193 base_url=self._cloud_environment.endpoints.resource_manager,
194 api_version="2022-04-01")
195
196 if self.id:
197 self.results['roledefinitions'] = self.get_by_id()
198 elif self.role_name:
199 self.results['roledefinitions'] = self.get_by_role_name()
200 else:
201 self.results['roledefinitions'] = self.list()
202
203 return self.results
204
205 def get_role_type(self, role_type):
206 if role_type:
207 if role_type == 'custom':
208 return 'CustomRole'
209 else:
210 return 'SystemRole'
211 return role_type
212
213 def list(self):
214 '''
215 List Role Definition in scope.
216
217 :return: deserialized Role Definition state dictionary
218 '''
219 self.log("List Role Definition in scope {0}".format(self.scope))
220
221 response = []
222
223 try:
224 response = list(self._client.role_definitions.list(scope=self.scope))
225
226 if len(response) > 0:
227 self.log("Response : {0}".format(response))
228 roles = []
229
230 if self.type:
231 roles = [r for r in response if r.role_type == self.type]
232 else:
233 roles = response
234
235 if len(roles) > 0:
236 return [roledefinition_to_dict(r) for r in roles]
237
238 except Exception as ex:
239 self.log("Didn't find role definition in scope {0}".format(self.scope))
240
241 return response
242
243 def get_by_id(self):
244 '''
245 Get Role Definition in scope by id.
246
247 :return: deserialized Role Definition state dictionary
248 '''
249 self.log("Get Role Definition by id {0}".format(self.id))
250
251 response = None
252
253 try:
254 response = self._client.role_definitions.get(scope=self.scope, role_definition_id=self.id)
255 if response:
256 response = roledefinition_to_dict(response)
257 if self.type:
258 if response.role_type == self.type:
259 return [response]
260 else:
261 return [response]
262
263 except Exception as ex:
264 self.log("Didn't find role definition by id {0}".format(self.id))
265
266 return []
267
268 def get_by_role_name(self):
269 '''
270 Get Role Definition in scope by role name.
271
272 :return: deserialized role definition state dictionary
273 '''
274 self.log("Get Role Definition by name {0}".format(self.role_name))
275
276 response = []
277
278 try:
279 response = self.list()
280
281 if len(response) > 0:
282 roles = []
283 for r in response:
284 if r['role_name'] == self.role_name:
285 roles.append(r)
286
287 if len(roles) == 1:
288 self.log("Role Definition : {0} found".format(self.role_name))
289 return roles
290 if len(roles) > 1:
291 self.fail("Found multiple Role Definitions with name: {0}".format(self.role_name))
292
293 except Exception as ex:
294 self.log("Didn't find Role Definition by name {0}".format(self.role_name))
295
296 return []
297
298
299 def main():
300 """Main execution"""
301 AzureRMRoleDefinitionInfo()
302
303
304 if __name__ == '__main__':
305 main()