Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2022 Mandar Kulkarni, < @mandar242 >
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_account_info
14
15 version_added: "1.14.0"
16
17 short_description: Get Azure Account facts (output of az account show)
18
19 description:
20 - Get facts for current logged in user.
21 - Output equivalent of `az account show` command.
22
23 extends_documentation_fragment:
24 - azure.azcollection.azure
25
26 author:
27 - Mandar Kulkarni (@mandar242)
28 '''
29
30 EXAMPLES = '''
31 - name: Get facts for current logged in user
32 azure.azcollection.azure_rm_account_info:
33 '''
34
35 RETURN = '''
36 account_info:
37 description:
38 - Facts for current logged in user, equivalent to `az account show`.
39 returned: always
40 type: dict
41 contains:
42 environmentName:
43 description: For cloud environments other than the US public cloud, the environment name.
44 returned: always
45 type: str
46 sample: AzureCloud
47 homeTenantId:
48 description: Subscription tenant id.
49 returned: always
50 type: str
51 sample: "00000000-0000-0000-0000-000000000000"
52 id:
53 description: Subscription id.
54 returned: always
55 type: str
56 sample: "00000000-0000-0000-0000-000000000000"
57 managedByTenants:
58 description: An array containing the tenants managing the subscription.
59 returned: always
60 type: list
61 elements: dict
62 contains:
63 tenantId:
64 description: Subscription tenant id
65 returned: always
66 type: str
67 sample: "00000000-0000-0000-0000-000000000000"
68 name:
69 description: The subscription display name.
70 returned: always
71 type: str
72 sample: "Pay-As-You-Go"
73 state:
74 description:
75 - The subscription state.
76 - Possible values include "Enabled", "Warned", "PastDue", "Disabled", "Deleted".
77 returned: always
78 type: str
79 sample: "Enabled"
80 tenant_id:
81 description: Subscription tenant id
82 returned: always
83 type: str
84 sample: "00000000-0000-0000-0000-000000000000"
85 user:
86 description: An dict containing the current user name and type.
87 returned: always
88 type: dict
89 elements: str
90 contains:
91 name:
92 description: The principal name of the active directory user.
93 returned: always
94 type: str
95 sample: "[email protected]"
96 type:
97 description: Active Directory user type.
98 returned: always
99 type: str
100 sample: "User"
101 '''
102
103
104 try:
105 import asyncio
106 from msgraph.generated.education.me.user.user_request_builder import UserRequestBuilder
107 except ImportError:
108 # This is handled in azure_rm_common
109 pass
110
111 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
112
113
114 class AzureRMAccountInfo(AzureRMModuleBase):
115
116 def __init__(self):
117
118 self.module_arg_spec = dict(
119 )
120
121 self.results = dict(
122 changed=False,
123 account_info=[]
124 )
125
126 # Different return info is gathered using 2 different clients
127 # 1. All except "user" section of the return value uses azure.mgmt.subsctiption.operations.subscriptionoperations
128 # 2. "user" section of the return value uses different client (GraphServiceClient)
129
130 super(AzureRMAccountInfo, self).__init__(derived_arg_spec=self.module_arg_spec,
131 supports_check_mode=True,
132 supports_tags=False,
133 is_ad_resource=True)
134
135 def exec_module(self, **kwargs):
136 self.results['account_info'] = self.list_items()
137 return self.results
138
139 def list_items(self):
140
141 results = {}
142
143 # Get
144 # "homeTenantId": "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx",
145 # "id": "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx",
146 # "isDefault": true, <- WIP on getting this param
147 # "managedByTenants": [
148 # {
149 # "tenantId": "64xxxxxx-xxxx-49fc-xxxx-ebxxxxxxxxxx"
150 # },
151 # {
152 # "tenantId": "2axxxxxx-xxxx-xxxx-a339-ebxxxxxxxxxx"
153 # },
154 # {
155 # "tenantId": "xxxxxxxx-xxxx-4e68-xxxx-ebxxxxxxxxxx"
156 # }
157 # ],
158 # "name": "Pay-As-You-Go",
159 # "state": "Enabled",
160 # "tenantId": "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx",
161
162 # Makes use of azure.mgmt.subsctiption.operations.subscriptionoperations
163 # https://docs.microsoft.com/en-us/python/api/azure-mgmt-subscription/azure.mgmt.subscription.operations.subscriptionsoperations?view=azure-python#methods
164
165 try:
166 subscription_list_response = list(self.subscription_client.subscriptions.list())
167 except Exception as exc:
168 self.fail("Failed to list all subscriptions - {0}".format(str(exc)))
169
170 results['id'] = subscription_list_response[0].subscription_id
171 results['tenantId'] = subscription_list_response[0].tenant_id
172 results['homeTenantId'] = subscription_list_response[0].tenant_id
173 results['name'] = subscription_list_response[0].display_name
174 results['state'] = subscription_list_response[0].state
175 results['managedByTenants'] = self.get_managed_by_tenants_list(subscription_list_response[0].managed_by_tenants)
176 results['environmentName'] = self.azure_auth._cloud_environment.name
177 results['user'] = self.get_aduser_info()
178
179 return results
180
181 def get_managed_by_tenants_list(self, object_list):
182
183 return [dict(tenantId=item.tenant_id) for item in object_list]
184
185 def get_aduser_info(self):
186
187 # Create GraphServiceClient for getting
188 # "user": {
189 # "name": "[email protected]",
190 # "type": "Member"
191 # }
192
193 # Makes use of azure MSGraph
194 # https://learn.microsoft.com/en-us/graph/api/user-get?view=graph-rest-1.0&tabs=http
195
196 user = {}
197
198 user_info = asyncio.get_event_loop().run_until_complete(self.getAccount())
199 user['name'] = user_info.user_principal_name
200 user['type'] = user_info.user_type
201 return user
202
203 async def getAccount(self):
204 return await self.get_msgraph_client().me.get(
205 request_configuration=UserRequestBuilder.UserRequestBuilderGetRequestConfiguration(
206 query_parameters=UserRequestBuilder.UserRequestBuilderGetQueryParameters(
207 select=["userType", "userPrincipalName", "postalCode", "identities"], ),
208 )
209 )
210
211
212 def main():
213 AzureRMAccountInfo()
214
215
216 if __name__ == '__main__':
217 main()