Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ugrade azure-mgmt-compute SDK to track2 #672

Merged
merged 21 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions plugins/module_utils/azure_rm_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1079,27 +1079,29 @@ def image_client(self):
if not self._image_client:
self._image_client = self.get_mgmt_svc_client(ComputeManagementClient,
base_url=self._cloud_environment.endpoints.resource_manager,
api_version='2018-06-01')
is_track2=True,
api_version='2021-04-01')
return self._image_client

@property
def image_models(self):
self.log("Getting compute image models")
return ComputeManagementClient.models("2018-06-01")
return ComputeManagementClient.models("2021-04-01")

@property
def compute_client(self):
self.log('Getting compute client')
if not self._compute_client:
self._compute_client = self.get_mgmt_svc_client(ComputeManagementClient,
base_url=self._cloud_environment.endpoints.resource_manager,
api_version='2019-07-01')
is_track2=True,
api_version='2021-04-01')
return self._compute_client

@property
def compute_models(self):
self.log("Getting compute models")
return ComputeManagementClient.models("2019-07-01")
return ComputeManagementClient.models("2021-04-01")

@property
def dns_client(self):
Expand Down
10 changes: 5 additions & 5 deletions plugins/modules/azure_rm_availabilityset.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase

try:
from msrestazure.azure_exceptions import CloudError
from msrestazure.tools import parse_resource_id
from azure.core.exceptions import ResourceNotFoundError
except ImportError:
# This is handled in azure_rm_common
pass
Expand Down Expand Up @@ -331,7 +331,7 @@ def create_or_update_availabilityset(self):
sku=params_sku
)
response = self.compute_client.availability_sets.create_or_update(self.resource_group, self.name, params)
except CloudError as e:
except Exception as e:
self.log('Error attempting to create the availability set.')
self.fail("Error creating the availability set: {0}".format(str(e)))

Expand All @@ -345,7 +345,7 @@ def delete_availabilityset(self):
self.log("Deleting availabilityset {0}".format(self.name))
try:
response = self.compute_client.availability_sets.delete(self.resource_group, self.name)
except CloudError as e:
except Exception as e:
self.log('Error attempting to delete the availability set.')
self.fail("Error deleting the availability set: {0}".format(str(e)))

Expand All @@ -361,7 +361,7 @@ def get_availabilityset(self):
try:
response = self.compute_client.availability_sets.get(self.resource_group, self.name)
found = True
except CloudError as e:
except ResourceNotFoundError as e:
self.log('Did not find the Availability set.')
if found is True:
return availability_set_to_dict(response)
Expand All @@ -371,7 +371,7 @@ def get_availabilityset(self):
def get_proximity_placement_group(self, resource_group, name):
try:
return self.compute_client.proximity_placement_groups.get(resource_group, name)
except Exception as exc:
except ResourceNotFoundError as exc:
self.fail("Error fetching proximity placement group {0} - {1}".format(name, str(exc)))


Expand Down
6 changes: 3 additions & 3 deletions plugins/modules/azure_rm_availabilityset_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase

try:
from msrestazure.azure_exceptions import CloudError
from azure.core.exceptions import ResourceNotFoundError
except Exception:
# handled in azure_rm_common
pass
Expand Down Expand Up @@ -169,7 +169,7 @@ def get_item(self):

try:
item = self.compute_client.availability_sets.get(self.resource_group, self.name)
except CloudError:
except ResourceNotFoundError:
pass

if item and self.has_tags(item.tags, self.tags):
Expand All @@ -188,7 +188,7 @@ def list_items(self):

try:
response = self.compute_client.availability_sets.list(self.resource_group)
except CloudError as exc:
except ResourceNotFoundError as exc:
self.fail('Failed to list all items - {0}'.format(str(exc)))

results = []
Expand Down
16 changes: 8 additions & 8 deletions plugins/modules/azure_rm_diskencryptionset.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@
format_resource_id, normalize_location_name

try:
from msrestazure.azure_exceptions import CloudError
from msrest.polling import LROPoller
from azure.core.polling import LROPoller
from azure.core.exceptions import ResourceNotFoundError
except ImportError:
# This is handled in azure_rm_common
pass
Expand Down Expand Up @@ -245,7 +245,7 @@ def exec_module(self, **kwargs):
elif self.state == 'absent':
changed = True

except CloudError:
except ResourceNotFoundError:
if self.state == 'present':
changed = True
else:
Expand Down Expand Up @@ -284,9 +284,9 @@ def create_or_update_diskencryptionset(self, disk_encryption_set):
try:
# create the disk encryption set
response = \
self.compute_client.disk_encryption_sets.create_or_update(resource_group_name=self.resource_group,
disk_encryption_set_name=self.name,
disk_encryption_set=disk_encryption_set)
self.compute_client.disk_encryption_sets.begin_create_or_update(resource_group_name=self.resource_group,
disk_encryption_set_name=self.name,
disk_encryption_set=disk_encryption_set)
if isinstance(response, LROPoller):
response = self.get_poller_result(response)
except Exception as exc:
Expand All @@ -296,8 +296,8 @@ def create_or_update_diskencryptionset(self, disk_encryption_set):
def delete_diskencryptionset(self):
try:
# delete the disk encryption set
response = self.compute_client.disk_encryption_sets.delete(resource_group_name=self.resource_group,
disk_encryption_set_name=self.name)
response = self.compute_client.disk_encryption_sets.begin_delete(resource_group_name=self.resource_group,
disk_encryption_set_name=self.name)
if isinstance(response, LROPoller):
response = self.get_poller_result(response)
except Exception as exc:
Expand Down
9 changes: 4 additions & 5 deletions plugins/modules/azure_rm_diskencryptionset_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase

try:
from msrestazure.azure_exceptions import CloudError
from azure.common import AzureMissingResourceHttpError, AzureHttpError
from azure.core.exceptions import ResourceNotFoundError
except Exception:
# This is handled in azure_rm_common
pass
Expand Down Expand Up @@ -148,7 +147,7 @@ def get_item(self):
# get specific disk encryption set
try:
item = self.compute_client.disk_encryption_sets.get(self.resource_group, self.name)
except CloudError:
except ResourceNotFoundError:
pass

# serialize result
Expand All @@ -160,7 +159,7 @@ def list_resource_group(self):
self.log('List all disk encryption sets for resource group - {0}'.format(self.resource_group))
try:
response = self.compute_client.disk_encryption_sets.list_by_resource_group(self.resource_group)
except AzureHttpError as exc:
except ResourceNotFoundError as exc:
self.fail("Failed to list for resource group {0} - {1}".format(self.resource_group, str(exc)))

results = []
Expand All @@ -173,7 +172,7 @@ def list_items(self):
self.log('List all disk encryption sets for a subscription ')
try:
response = self.compute_client.disk_encryption_sets.list()
except AzureHttpError as exc:
except ResourceNotFoundError as exc:
self.fail("Failed to list all items - {0}".format(str(exc)))

results = []
Expand Down
4 changes: 2 additions & 2 deletions plugins/modules/azure_rm_hostgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
normalize_location_name

try:
from msrestazure.azure_exceptions import CloudError
from azure.core.exceptions import ResourceNotFoundError
except ImportError:
# This is handled in azure_rm_common
pass
Expand Down Expand Up @@ -226,7 +226,7 @@ def exec_module(self, **kwargs):
elif self.state == 'absent':
changed = True

except CloudError:
except ResourceNotFoundError:
if self.state == 'present':
changed = True
else:
Expand Down
9 changes: 4 additions & 5 deletions plugins/modules/azure_rm_hostgroup_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase

try:
from msrestazure.azure_exceptions import CloudError
from azure.common import AzureMissingResourceHttpError, AzureHttpError
from azure.core.exceptions import ResourceNotFoundError
except Exception:
# This is handled in azure_rm_common
pass
Expand Down Expand Up @@ -139,7 +138,7 @@ def get_item(self):
# get specific host group
try:
item = self.compute_client.dedicated_host_groups.get(self.resource_group, self.name)
except CloudError:
except ResourceNotFoundError:
pass

# serialize result
Expand All @@ -151,7 +150,7 @@ def list_resource_group(self):
self.log('List all host groups for resource group - {0}'.format(self.resource_group))
try:
response = self.compute_client.dedicated_host_groups.list_by_resource_group(self.resource_group)
except AzureHttpError as exc:
except ResourceNotFoundError as exc:
self.fail("Failed to list for resource group {0} - {1}".format(self.resource_group, str(exc)))

results = []
Expand All @@ -164,7 +163,7 @@ def list_items(self):
self.log('List all host groups for a subscription ')
try:
response = self.compute_client.dedicated_host_groups.list_by_subscription()
except AzureHttpError as exc:
except ResourceNotFoundError as exc:
self.fail("Failed to list all items - {0}".format(str(exc)))

results = []
Expand Down
10 changes: 4 additions & 6 deletions plugins/modules/azure_rm_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@

try:
from msrestazure.tools import parse_resource_id
from msrestazure.azure_exceptions import CloudError
from azure.core.exceptions import ResourceNotFoundError
except ImportError:
# This is handled in azure_rm_common
pass
Expand Down Expand Up @@ -328,18 +328,16 @@ def _get_resource(self, get_method, resource_group, name, expand=None):
return get_method(resource_group, name, expand=expand)
else:
return get_method(resource_group, name)
except CloudError as cloud_err:
except ResourceNotFoundError as cloud_err:
# Return None iff the resource is not found
if cloud_err.status_code == 404:
self.log('{0}'.format(str(cloud_err)))
return None
self.fail('Error: failed to get resource {0} - {1}'.format(name, str(cloud_err)))
except Exception as exc:
self.fail('Error: failed to get resource {0} - {1}'.format(name, str(exc)))

def create_image(self, image):
try:
poller = self.image_client.images.create_or_update(self.resource_group, self.name, image)
poller = self.image_client.images.begin_create_or_update(self.resource_group, self.name, image)
new_image = self.get_poller_result(poller)
except Exception as exc:
self.fail("Error creating image {0} - {1}".format(self.name, str(exc)))
Expand All @@ -349,7 +347,7 @@ def create_image(self, image):
def delete_image(self):
self.log('Deleting image {0}'.format(self.name))
try:
poller = self.image_client.images.delete(self.resource_group, self.name)
poller = self.image_client.images.begin_delete(self.resource_group, self.name)
result = self.get_poller_result(poller)
except Exception as exc:
self.fail("Error deleting image {0} - {1}".format(self.name, str(exc)))
Expand Down
8 changes: 4 additions & 4 deletions plugins/modules/azure_rm_image_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@


try:
from msrestazure.azure_exceptions import CloudError
from azure.core.exceptions import ResourceNotFoundError
except Exception:
# This is handled in azure_rm_common
pass
Expand Down Expand Up @@ -231,7 +231,7 @@ def get_image(self, resource_group, image_name):
item = None
try:
item = self.image_client.images.get(resource_group, image_name)
except CloudError as exc:
except ResourceNotFoundError as exc:
self.fail('Failed to list images - {0}'.format(str(exc)))

result = [self.format_item(item)]
Expand All @@ -246,7 +246,7 @@ def list_images_by_resource_group(self, resource_group):
response = None
try:
response = self.image_client.images.list_by_resource_group(resource_group)
except CloudError as exc:
except ResourceNotFoundError as exc:
self.fail("Failed to list images: {0}".format(str(exc)))

return [self.format_item(x) for x in response if self.has_tags(x.tags, self.tags)] if response else []
Expand All @@ -261,7 +261,7 @@ def list_images(self, image_name=None):
results = []
try:
response = self.image_client.images.list()
except CloudError as exc:
except ResourceNotFoundError as exc:
self.fail("Failed to list all images: {0}".format(str(exc)))

results = [self.format_item(x) for x in response if self.has_tags(x.tags, self.tags)] if response else []
Expand Down
22 changes: 10 additions & 12 deletions plugins/modules/azure_rm_manageddisk.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
try:
from msrestazure.tools import parse_resource_id
from msrestazure.azure_exceptions import CloudError
from azure.core.exceptions import ResourceNotFoundError
except ImportError:
# This is handled in azure_rm_common
pass
Expand Down Expand Up @@ -424,7 +424,7 @@ def detach(self, vm_name, disk):

def _update_vm(self, name, params):
try:
poller = self.compute_client.virtual_machines.create_or_update(self.resource_group, name, params)
poller = self.compute_client.virtual_machines.begin_create_or_update(self.resource_group, name, params)
self.get_poller_result(poller)
except Exception as exc:
self.fail("Error updating virtual machine {0} - {1}".format(name, str(exc)))
Expand Down Expand Up @@ -467,13 +467,12 @@ def generate_managed_disk_property(self):

def create_or_update_managed_disk(self, parameter):
try:
poller = self.compute_client.disks.create_or_update(
self.resource_group,
self.name,
parameter)
poller = self.compute_client.disks.begin_create_or_update(self.resource_group,
self.name,
parameter)
aux = self.get_poller_result(poller)
return managed_disk_to_dict(aux)
except CloudError as e:
except Exception as e:
self.fail("Error creating the managed disk: {0}".format(str(e)))

# This method accounts for the difference in structure between the
Expand All @@ -500,11 +499,10 @@ def is_different(self, found_disk, new_disk):

def delete_managed_disk(self):
try:
poller = self.compute_client.disks.delete(
self.resource_group,
self.name)
poller = self.compute_client.disks.begin_delete(self.resource_group,
self.name)
return self.get_poller_result(poller)
except CloudError as e:
except Exception as e:
self.fail("Error deleting the managed disk: {0}".format(str(e)))

def get_managed_disk(self):
Expand All @@ -513,7 +511,7 @@ def get_managed_disk(self):
self.resource_group,
self.name)
return managed_disk_to_dict(resp)
except CloudError as e:
except ResourceNotFoundError as e:
self.log('Did not find managed disk')

def is_attach_caching_option_different(self, vm_name, disk):
Expand Down
Loading