Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2023 xuzhang3 (@xuzhang3), Fred-sun (@Fred-sun)
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 DOCUMENTATION = '''
11 ---
12 module: azure_rm_batchaccount_info
13 version_added: "0.1.2"
14 short_description: Get the Batch Account on Azure facts
15 description:
16 - Get the Batch Account on Azure facts.
17
18 options:
19 resource_group:
20 description:
21 - The name of the resource group in which to create the Batch Account.
22 type: str
23 name:
24 description:
25 - The name of the Batch Account.
26 type: str
27 tags:
28 description:
29 - Limit results by providing a list of tags. Format tags as 'key' or 'key:value'.
30 type: list
31 elements: str
32
33 extends_documentation_fragment:
34 - azure.azcollection.azure
35
36 author:
37 - xuzhang3 (@xuzhang3)
38 - Fred Sun (@Fred-sun)
39 '''
40
41 EXAMPLES = '''
42 - name: Get the Batch Account by name
43 azure_rm_batchaccount_info:
44 resource_group: MyResGroup
45 name: mybatchaccount
46
47 - name: List the Batch Account by subscription
48 azure_rm_batchaccount_info:
49 tags:
50 - key1
51 '''
52
53 RETURN = '''
54 id:
55 description:
56 - The ID of the Batch account.
57 returned: always
58 type: str
59 sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Batch/batchAccounts/sampleacct"
60 account_endpoint:
61 description:
62 - The account endpoint used to interact with the Batch service.
63 returned: always
64 type: str
65 sample: sampleacct.westus.batch.azure.com
66 '''
67
68 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt
69
70 try:
71 from azure.core.exceptions import ResourceNotFoundError
72 except ImportError:
73 # This is handled in azure_rm_common
74 pass
75
76
77 class AzureRMBatchAccountInfo(AzureRMModuleBaseExt):
78 """Configuration class for an Azure RM Batch Account resource"""
79
80 def __init__(self):
81 self.module_arg_spec = dict(
82 resource_group=dict(
83 type='str',
84 ),
85 name=dict(
86 type='str',
87 ),
88 tags=dict(
89 type='list',
90 elements='str'
91 )
92 )
93
94 self.resource_group = None
95 self.name = None
96 self.tags = None
97
98 self.results = dict(changed=False)
99
100 super(AzureRMBatchAccountInfo, self).__init__(derived_arg_spec=self.module_arg_spec,
101 supports_check_mode=True,
102 supports_tags=False)
103
104 def exec_module(self, **kwargs):
105 """Main module execution method"""
106
107 for key in list(self.module_arg_spec.keys()) + ['tags']:
108 setattr(self, key, kwargs[key])
109
110 response = []
111
112 if self.resource_group is not None and self.name is not None:
113 response = [self.get_batchaccount()]
114 elif self.resource_group is not None:
115 response = self.list_by_resourcegroup()
116 else:
117 response = self.list_all()
118
119 self.results['batch_account'] = [self.format_item(item) for item in response if item and self.has_tags(item.get('tags'), self.tags)]
120
121 return self.results
122
123 def list_by_resourcegroup(self):
124 self.log("List all Batch Account in the rsource group {0}".format(self.resource_group))
125 result = []
126 response = []
127 try:
128 response = self.batch_account_client.batch_account.list_by_resource_group(resource_group_name=self.resource_group)
129 self.log("Response : {0}".format(response))
130 except Exception as e:
131 self.log('Did not find the Batch Account instance. Exception as {0}'.format(e))
132 for item in response:
133 result.append(item.as_dict())
134 return result
135
136 def list_all(self):
137 self.log("List all Batch Account in the same subscritpion")
138 result = []
139 response = []
140 try:
141 response = self.batch_account_client.batch_account.list()
142 self.log("Response : {0}".format(response))
143 except Exception as e:
144 self.log('Did not find the Batch Account instance.')
145 for item in response:
146 result.append(item.as_dict())
147 return result
148
149 def get_batchaccount(self):
150 '''
151 Gets the properties of the specified Batch Account
152 :return: deserialized Batch Account instance state dictionary
153 '''
154 self.log("Checking if the Batch Account instance {0} is present".format(self.name))
155 try:
156 response = self.batch_account_client.batch_account.get(resource_group_name=self.resource_group,
157 account_name=self.name)
158 self.log("Response : {0}".format(response))
159 self.log("Batch Account instance : {0} found".format(response.name))
160 except ResourceNotFoundError as e:
161 self.log('Did not find the Batch Account instance.')
162 return
163 return response.as_dict()
164
165 def format_item(self, item):
166 result = {
167 'id': item['id'],
168 'name': item['name'],
169 'type': item['type'],
170 'location': item['location'],
171 'account_endpoint': item['account_endpoint'],
172 'provisioning_state': item['provisioning_state'],
173 'pool_allocation_mode': item['pool_allocation_mode'],
174 'auto_storage': item['auto_storage'],
175 'dedicated_core_quota': item['dedicated_core_quota'],
176 'low_priority_core_quota': item['low_priority_core_quota'],
177 'pool_quota': item['pool_quota'],
178 'active_job_and_job_schedule_quota': item['active_job_and_job_schedule_quota'],
179 'tags': item.get('tags'),
180 'identity': item.get('identity')
181 }
182 return result
183
184
185 def main():
186 """Main execution"""
187 AzureRMBatchAccountInfo()
188
189
190 if __name__ == '__main__':
191 main()