| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = ''' |
| 11 |
--- |
| 12 |
module: azure_rm_batchaccountapplication |
| 13 |
version_added: "3.0.0" |
| 14 |
short_description: Manages a Batch Account Application on Azure |
| 15 |
description: |
| 16 |
- Create, update and delete instance of Azure Batch Account Application. |
| 17 |
|
| 18 |
options: |
| 19 |
resource_group: |
| 20 |
description: |
| 21 |
- The name of the resource group in which to create the Batch Account Application. |
| 22 |
required: true |
| 23 |
type: str |
| 24 |
batch_account_name: |
| 25 |
description: |
| 26 |
- The name of the Batch Account. |
| 27 |
required: true |
| 28 |
type: str |
| 29 |
name: |
| 30 |
description: |
| 31 |
- The name of the Batch Account Application. |
| 32 |
required: true |
| 33 |
type: str |
| 34 |
display_name: |
| 35 |
description: |
| 36 |
- The display name for the application. |
| 37 |
type: str |
| 38 |
allow_updates: |
| 39 |
description: |
| 40 |
- A value indicating whether packages within the application may be overwritten using the same version string. |
| 41 |
type: bool |
| 42 |
default_version: |
| 43 |
description: |
| 44 |
- The package to use if a client requests the application but does not specify a version. |
| 45 |
- This property can only be set to the name of an existing package. |
| 46 |
type: str |
| 47 |
state: |
| 48 |
description: |
| 49 |
- Assert the state of the Batch Account Application. |
| 50 |
- Use C(present) to create or update a Batch Account Application and C(absent) to delete it. |
| 51 |
default: present |
| 52 |
type: str |
| 53 |
choices: |
| 54 |
- present |
| 55 |
- absent |
| 56 |
|
| 57 |
extends_documentation_fragment: |
| 58 |
- azure.azcollection.azure |
| 59 |
|
| 60 |
author: |
| 61 |
- xuzhang3 (@xuzhang3) |
| 62 |
- Fred Sun (@Fred-sun) |
| 63 |
''' |
| 64 |
|
| 65 |
EXAMPLES = ''' |
| 66 |
- name: Create Batch Account Application |
| 67 |
azure_rm_batchaccountapplication: |
| 68 |
resource_group: MyResGroup |
| 69 |
name: mybatchaccountapplication |
| 70 |
batch_account_name: mybatchaccount |
| 71 |
allow_updates: true |
| 72 |
display_name: fredtest |
| 73 |
|
| 74 |
- name: Delete Batch Account Application |
| 75 |
azure_rm_batchaccountapplication: |
| 76 |
resource_group: MyResGroup |
| 77 |
name: mybatchaccountapplication |
| 78 |
batch_account_name: mybatchaccount |
| 79 |
state: absent |
| 80 |
''' |
| 81 |
|
| 82 |
RETURN = ''' |
| 83 |
batch_account_application: |
| 84 |
description: |
| 85 |
- Contains information about an application in a Batch account. |
| 86 |
type: complex |
| 87 |
returned: always |
| 88 |
contains: |
| 89 |
id: |
| 90 |
description: |
| 91 |
- The ID of the batch account application. |
| 92 |
type: str |
| 93 |
returned: always |
| 94 |
sample: "/subscriptions/xxx-xxx/resourceGroups/testRG/providers/Microsoft.Batch/batchAccounts/batch01/applications/app01" |
| 95 |
resource_group: |
| 96 |
description: |
| 97 |
- The resource group name. |
| 98 |
type: str |
| 99 |
returned: always |
| 100 |
sample: testRG |
| 101 |
batch_account_name: |
| 102 |
description: |
| 103 |
- The name of the batch account. |
| 104 |
type: str |
| 105 |
returned: always |
| 106 |
sample: batch01 |
| 107 |
name: |
| 108 |
description: |
| 109 |
- The name of the application. |
| 110 |
type: str |
| 111 |
returned: always |
| 112 |
sample: app01 |
| 113 |
allow_updates: |
| 114 |
description: |
| 115 |
- A value indicating whether packages within the application may be overwritten using the same version string. |
| 116 |
type: str |
| 117 |
returned: always |
| 118 |
sample: true |
| 119 |
default_version: |
| 120 |
description: |
| 121 |
- The package to use if a client requests the application but does not specify a version. |
| 122 |
- This property can only be set to the name of an existing package. |
| 123 |
type: str |
| 124 |
returned: always |
| 125 |
sample: null |
| 126 |
display_name: |
| 127 |
description: |
| 128 |
- The display name for the application. |
| 129 |
type: str |
| 130 |
returned: always |
| 131 |
sample: testbatch |
| 132 |
type: |
| 133 |
description: |
| 134 |
- The type of the resource. |
| 135 |
type: str |
| 136 |
returned: always |
| 137 |
sample: Microsoft.Batch/batchAccounts/applications |
| 138 |
''' |
| 139 |
|
| 140 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBase |
| 141 |
|
| 142 |
try: |
| 143 |
from azure.core.polling import LROPoller |
| 144 |
from azure.core.exceptions import ResourceNotFoundError |
| 145 |
except ImportError: |
| 146 |
|
| 147 |
pass |
| 148 |
|
| 149 |
|
| 150 |
class AzureRMBatchAccountApplication(AzureRMModuleBase): |
| 151 |
"""Configuration class for an Azure RM Batch Account Application resource""" |
| 152 |
|
| 153 |
def __init__(self): |
| 154 |
self.module_arg_spec = dict( |
| 155 |
resource_group=dict( |
| 156 |
required=True, |
| 157 |
type='str' |
| 158 |
), |
| 159 |
batch_account_name=dict( |
| 160 |
type='str', |
| 161 |
required=True, |
| 162 |
), |
| 163 |
name=dict( |
| 164 |
required=True, |
| 165 |
type='str' |
| 166 |
), |
| 167 |
display_name=dict( |
| 168 |
type='str' |
| 169 |
), |
| 170 |
allow_updates=dict( |
| 171 |
type='bool' |
| 172 |
), |
| 173 |
default_version=dict( |
| 174 |
type='str' |
| 175 |
), |
| 176 |
state=dict( |
| 177 |
type='str', |
| 178 |
default='present', |
| 179 |
choices=['present', 'absent'] |
| 180 |
) |
| 181 |
) |
| 182 |
|
| 183 |
self.resource_group = None |
| 184 |
self.batch_account_name = None |
| 185 |
self.name = None |
| 186 |
self.results = dict(changed=False) |
| 187 |
self.state = None |
| 188 |
self.body = dict() |
| 189 |
|
| 190 |
super(AzureRMBatchAccountApplication, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 191 |
supports_check_mode=True, |
| 192 |
supports_tags=False) |
| 193 |
|
| 194 |
def exec_module(self, **kwargs): |
| 195 |
"""Main module execution method""" |
| 196 |
|
| 197 |
for key in list(self.module_arg_spec.keys()): |
| 198 |
if hasattr(self, key): |
| 199 |
setattr(self, key, kwargs[key]) |
| 200 |
elif kwargs[key] is not None: |
| 201 |
self.body[key] = kwargs[key] |
| 202 |
|
| 203 |
response = None |
| 204 |
changed = False |
| 205 |
|
| 206 |
old_response = self.get_batchaccount_application() |
| 207 |
|
| 208 |
if not old_response: |
| 209 |
self.log("Batch Account Application instance doesn't exist") |
| 210 |
if self.state == 'absent': |
| 211 |
self.log("Old instance didn't exist") |
| 212 |
else: |
| 213 |
changed = True |
| 214 |
if not self.check_mode: |
| 215 |
response = self.create_batchaccount_application() |
| 216 |
else: |
| 217 |
self.log("Batch Account Application instance already exists") |
| 218 |
if self.state == 'absent': |
| 219 |
if not self.check_mode: |
| 220 |
changed = True |
| 221 |
response = self.delete_batchaccount_application() |
| 222 |
else: |
| 223 |
if self.body.get('default_version') is not None and self.body['default_version'] != old_response['default_version']: |
| 224 |
changed = True |
| 225 |
else: |
| 226 |
self.body['default_version'] = old_response['default_version'] |
| 227 |
if self.body.get('display_name') and self.body['display_name'] != old_response['display_name']: |
| 228 |
changed = True |
| 229 |
else: |
| 230 |
self.body['display_name'] = old_response['display_name'] |
| 231 |
if self.body.get('allow_updates') is not None and bool(self.body['allow_updates']) != bool(old_response['allow_updates']): |
| 232 |
changed = True |
| 233 |
else: |
| 234 |
self.body['allow_updates'] = old_response['allow_updates'] |
| 235 |
if not self.check_mode and changed: |
| 236 |
response = self.update_batchaccount_application() |
| 237 |
|
| 238 |
self.results = dict( |
| 239 |
changed=changed, |
| 240 |
state=response, |
| 241 |
) |
| 242 |
return self.results |
| 243 |
|
| 244 |
def create_batchaccount_application(self): |
| 245 |
''' |
| 246 |
Creates Batch Account Application with the specified configuration. |
| 247 |
''' |
| 248 |
self.log("Creating the Batch Account Application instance {0}".format(self.name)) |
| 249 |
|
| 250 |
try: |
| 251 |
response = self.batch_account_client.application.create(resource_group_name=self.resource_group, |
| 252 |
account_name=self.batch_account_name, |
| 253 |
application_name=self.name, |
| 254 |
parameters=self.body) |
| 255 |
if isinstance(response, LROPoller): |
| 256 |
response = self.get_poller_result(response) |
| 257 |
except Exception as exc: |
| 258 |
self.log('Error attempting to create the Batch Account Application instance.') |
| 259 |
self.fail("Error creating the Batch Account Application instance: {0}".format(str(exc))) |
| 260 |
return response.as_dict() |
| 261 |
|
| 262 |
def update_batchaccount_application(self): |
| 263 |
''' |
| 264 |
Update Batch Account Application with the specified configuration. |
| 265 |
''' |
| 266 |
self.log("Updating the Batch Account Application instance {0}".format(self.name)) |
| 267 |
|
| 268 |
try: |
| 269 |
response = self.batch_account_client.application.update(resource_group_name=self.resource_group, |
| 270 |
account_name=self.batch_account_name, |
| 271 |
application_name=self.name, |
| 272 |
parameters=self.body) |
| 273 |
if isinstance(response, LROPoller): |
| 274 |
response = self.get_poller_result(response) |
| 275 |
except Exception as exc: |
| 276 |
self.log('Error attempting to update the Batch Account Application instance.') |
| 277 |
self.fail("Error updating the Batch Account Application instance: {0}".format(str(exc))) |
| 278 |
return response.as_dict() |
| 279 |
|
| 280 |
def delete_batchaccount_application(self): |
| 281 |
''' |
| 282 |
Deletes specified Batch Account Application instance in the specified subscription and resource group. |
| 283 |
:return: True |
| 284 |
''' |
| 285 |
self.log("Deleting the Batch Account Application instance {0}".format(self.name)) |
| 286 |
try: |
| 287 |
self.batch_account_client.application.delete(resource_group_name=self.resource_group, |
| 288 |
account_name=self.batch_account_name, |
| 289 |
application_name=self.name) |
| 290 |
except Exception as e: |
| 291 |
self.log('Error attempting to delete the Batch Account Application instance.') |
| 292 |
self.fail("Error deleting the Batch Account Application instance: {0}".format(str(e))) |
| 293 |
|
| 294 |
return True |
| 295 |
|
| 296 |
def get_batchaccount_application(self): |
| 297 |
''' |
| 298 |
Gets the properties of the specified Batch Account Application |
| 299 |
:return: deserialized Batch Account Application instance state dictionary |
| 300 |
''' |
| 301 |
self.log("Checking if the Batch Account Application instance {0} is present".format(self.name)) |
| 302 |
found = False |
| 303 |
try: |
| 304 |
response = self.batch_account_client.application.get(resource_group_name=self.resource_group, |
| 305 |
account_name=self.batch_account_name, |
| 306 |
application_name=self.name) |
| 307 |
found = True |
| 308 |
self.log("Response : {0}".format(response)) |
| 309 |
self.log("Batch Account Application instance : {0} found".format(response.name)) |
| 310 |
except ResourceNotFoundError as e: |
| 311 |
self.log('Did not find the Batch Account Application instance. Exception as {0}'.format(e)) |
| 312 |
if found is True: |
| 313 |
return self.format_item(response.as_dict()) |
| 314 |
return False |
| 315 |
|
| 316 |
def format_item(self, item): |
| 317 |
result = { |
| 318 |
'resource_group': self.resource_group, |
| 319 |
'batch_account_name': self.batch_account_name, |
| 320 |
'id': item['id'], |
| 321 |
'name': item['name'], |
| 322 |
'type': item['type'], |
| 323 |
'display_name': item['display_name'], |
| 324 |
'allow_updates': item['allow_updates'], |
| 325 |
'default_version': item['default_version'] if item.get('default_version') is not None else None |
| 326 |
} |
| 327 |
return result |
| 328 |
|
| 329 |
|
| 330 |
def main(): |
| 331 |
"""Main execution""" |
| 332 |
AzureRMBatchAccountApplication() |
| 333 |
|
| 334 |
|
| 335 |
if __name__ == '__main__': |
| 336 |
main() |