Skip to content

Commit

Permalink
Add support to managed batch account relate resource (ansible-collect…
Browse files Browse the repository at this point in the history
…ions#1753)

* Add support to managed batchaccount application modules

* Support to managed batch account certificate

* The certificate has deprecate from 2024/2/29

* Add batch account pool

* Delete duplicate key

* Modify azure_rm_batchaccountpool.py

* fix sanity fail

* Fix sanity error 02

* Fix sanity error 03

* Fix sanity error 04

* Fix sanity error 05

* Fixed sandity error 06

* Fix sanity error 07

* Add test case

* last change

* last change02

* last change 03
  • Loading branch information
Fred-sun authored Nov 21, 2024
1 parent 29900d4 commit 6a444bb
Show file tree
Hide file tree
Showing 12 changed files with 4,000 additions and 26 deletions.
7 changes: 6 additions & 1 deletion meta/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,9 @@ action_groups:
- azure.azcollection.azure_rm_capacityreservationgroup
- azure.azcollection.azure_rm_capacityreservationgroup_info
- azure.azcollection.azure_rm_virtualnetworkgatewayconnection
- azure.azcollection.azure_rm_virtualnetworkgatewayconnection_info
- azure.azcollection.azure_rm_batchaccountapplication
- azure.azcollection.azure_rm_batchaccountapplication_info
- azure.azcollection.azure_rm_batchaccountpool
- azure.azcollection.azure_rm_batchaccountpool_info
- azure.azcollection.azure_rm_batchaccountapplicationpackage
- azure.azcollection.azure_rm_batchaccountapplicationpackage_info
15 changes: 15 additions & 0 deletions plugins/module_utils/azure_rm_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ def default_api_version(self):
from azure.identity._credentials import client_secret, user_password, certificate, managed_identity
from azure.identity import AzureCliCredential
from msgraph import GraphServiceClient
from azure.mgmt.batch import BatchManagementClient
from azure.mgmt.batch import models as BatchManagementModel

except ImportError as exc:
Authentication = object
Expand Down Expand Up @@ -462,6 +464,7 @@ def __init__(self, derived_arg_spec, bypass_checks=False, no_log=False,
self._datafactory_client = None
self._notification_hub_client = None
self._event_hub_client = None
self._batch_account_client = None

self.check_mode = self.module.check_mode
self.api_profile = self.module.params.get('api_profile')
Expand Down Expand Up @@ -1461,6 +1464,18 @@ def datafactory_client(self):
def datafactory_model(self):
return DataFactoryModel

@property
def batch_account_client(self):
self.log('Getting batch account client...')
if not self._batch_account_client:
self._batch_account_client = self.get_mgmt_svc_client(BatchManagementClient,
base_url=self._cloud_environment.endpoints.resource_manager)
return self._batch_account_client

@property
def batch_account_model(self):
return BatchManagementModel


class AzureRMAuthException(Exception):
pass
Expand Down
29 changes: 12 additions & 17 deletions plugins/modules/azure_rm_batchaccount.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@

try:
from azure.core.polling import LROPoller
from azure.mgmt.batch import BatchManagementClient
from azure.core.exceptions import ResourceNotFoundError
from azure.mgmt.batch.models import BatchAccountIdentity, UserAssignedIdentities
except ImportError:
Expand Down Expand Up @@ -172,7 +171,6 @@ def __init__(self):
self.batch_account = dict()

self.results = dict(changed=False)
self.mgmt_client = None
self.state = None
self.to_do = Actions.NoAction
self._managed_identity = None
Expand Down Expand Up @@ -222,9 +220,6 @@ def exec_module(self, **kwargs):

response = None

self.mgmt_client = self.get_mgmt_svc_client(BatchManagementClient,
base_url=self._cloud_environment.endpoints.resource_manager)

old_response = self.get_batchaccount()
curr_identity = old_response["identity"] if old_response else None

Expand Down Expand Up @@ -296,15 +291,15 @@ def create_update_batchaccount(self):

try:
if self.to_do == Actions.Create:
response = self.mgmt_client.batch_account.begin_create(resource_group_name=self.resource_group,
account_name=self.name,
parameters=self.batch_account)
response = self.batch_account_client.batch_account.begin_create(resource_group_name=self.resource_group,
account_name=self.name,
parameters=self.batch_account)
else:
response = self.mgmt_client.batch_account.update(resource_group_name=self.resource_group,
account_name=self.name,
parameters=dict(tags=self.tags,
auto_storage=self.batch_account.get('auto_storage'),
identity=self.batch_account.get('identity')))
response = self.batch_account_client.batch_account.update(resource_group_name=self.resource_group,
account_name=self.name,
parameters=dict(tags=self.tags,
auto_storage=self.batch_account.get('auto_storage'),
identity=self.batch_account.get('identity')))
if isinstance(response, LROPoller):
response = self.get_poller_result(response)
except Exception as exc:
Expand All @@ -320,8 +315,8 @@ def delete_batchaccount(self):
'''
self.log("Deleting the Batch Account instance {0}".format(self.name))
try:
response = self.mgmt_client.batch_account.begin_delete(resource_group_name=self.resource_group,
account_name=self.name)
response = self.batch_account_client.batch_account.begin_delete(resource_group_name=self.resource_group,
account_name=self.name)
except Exception as e:
self.log('Error attempting to delete the Batch Account instance.')
self.fail("Error deleting the Batch Account instance: {0}".format(str(e)))
Expand All @@ -338,8 +333,8 @@ def get_batchaccount(self):
self.log("Checking if the Batch Account instance {0} is present".format(self.name))
found = False
try:
response = self.mgmt_client.batch_account.get(resource_group_name=self.resource_group,
account_name=self.name)
response = self.batch_account_client.batch_account.get(resource_group_name=self.resource_group,
account_name=self.name)
found = True
self.log("Response : {0}".format(response))
self.log("Batch Account instance : {0} found".format(response.name))
Expand Down
12 changes: 4 additions & 8 deletions plugins/modules/azure_rm_batchaccount_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@

try:
from azure.core.exceptions import ResourceNotFoundError
from azure.mgmt.batch import BatchManagementClient
except ImportError:
# This is handled in azure_rm_common
pass
Expand Down Expand Up @@ -97,7 +96,6 @@ def __init__(self):
self.tags = None

self.results = dict(changed=False)
self.mgmt_client = None

super(AzureRMBatchAccountInfo, self).__init__(derived_arg_spec=self.module_arg_spec,
supports_check_mode=True,
Expand All @@ -111,8 +109,6 @@ def exec_module(self, **kwargs):

response = []

self.mgmt_client = self.get_mgmt_svc_client(BatchManagementClient,
base_url=self._cloud_environment.endpoints.resource_manager)
if self.resource_group is not None and self.name is not None:
response = [self.get_batchaccount()]
elif self.resource_group is not None:
Expand All @@ -129,7 +125,7 @@ def list_by_resourcegroup(self):
result = []
response = []
try:
response = self.mgmt_client.batch_account.list_by_resource_group(resource_group_name=self.resource_group)
response = self.batch_account_client.batch_account.list_by_resource_group(resource_group_name=self.resource_group)
self.log("Response : {0}".format(response))
except Exception as e:
self.log('Did not find the Batch Account instance. Exception as {0}'.format(e))
Expand All @@ -142,7 +138,7 @@ def list_all(self):
result = []
response = []
try:
response = self.mgmt_client.batch_account.list()
response = self.batch_account_client.batch_account.list()
self.log("Response : {0}".format(response))
except Exception as e:
self.log('Did not find the Batch Account instance.')
Expand All @@ -157,8 +153,8 @@ def get_batchaccount(self):
'''
self.log("Checking if the Batch Account instance {0} is present".format(self.name))
try:
response = self.mgmt_client.batch_account.get(resource_group_name=self.resource_group,
account_name=self.name)
response = self.batch_account_client.batch_account.get(resource_group_name=self.resource_group,
account_name=self.name)
self.log("Response : {0}".format(response))
self.log("Batch Account instance : {0} found".format(response.name))
except ResourceNotFoundError as e:
Expand Down
Loading

0 comments on commit 6a444bb

Please sign in to comment.