Skip to content

Commit

Permalink
Manageddisk info managed by (#302)
Browse files Browse the repository at this point in the history
* working improvements.

* whitespace

* right comprehension field.

* tags working

* rearange for better error handling.

* plural results

* code style

* asdf

* committing suggestions.
  • Loading branch information
paultaiton authored Oct 26, 2020
1 parent b83b4f9 commit b697acc
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 66 deletions.
140 changes: 77 additions & 63 deletions plugins/modules/azure_rm_manageddisk_info.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright: (c) 2020, Paul Aiton <@paultaiton>
# Copyright: (c) 2016, Bruno Medina Bolanos Cacho <bruno.medina@microsoft.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

Expand Down Expand Up @@ -34,19 +35,25 @@
resource_group:
description:
- Limit results to a specific resource group.
- Required if I(name) is set
type: str
tags:
description:
- Limit results by providing a list of tags.
- Format tags as 'key' or 'key:value'.
type: list
managed_by:
description:
- Limit results to disks managed by the given VM fqid.
type: str
extends_documentation_fragment:
- azure.azcollection.azure
- azure.azcollection.azure_tags
author:
- Bruno Medina (@brusMX)
- Paul Aiton (@paultaiton)
'''

EXAMPLES = r'''
Expand All @@ -58,6 +65,10 @@
- name: Get facts for all managed disks
azure_rm_manageddisk_info:
- name: Get facts for all managed disks managed by a specific vm
azure_rm_manageddisk_info:
managed_by: '/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/rgName/Microsoft.Compute/virtualMachines/vmName'
- name: Get facts by tags
azure_rm_manageddisk_info:
tags:
Expand Down Expand Up @@ -129,114 +140,117 @@
pass


# duplicated in azure_rm_manageddisk
def managed_disk_to_dict(managed_disk):
create_data = managed_disk.creation_data
return dict(
id=managed_disk.id,
name=managed_disk.name,
location=managed_disk.location,
tags=managed_disk.tags,
create_option=create_data.create_option.lower(),
source_uri=create_data.source_uri or create_data.source_resource_id,
disk_size_gb=managed_disk.disk_size_gb,
os_type=managed_disk.os_type.lower() if managed_disk.os_type else None,
storage_account_type=managed_disk.sku.name if managed_disk.sku else None,
managed_by=managed_disk.managed_by,
zone=managed_disk.zones[0] if managed_disk.zones and len(managed_disk.zones) > 0 else ''
)


class AzureRMManagedDiskInfo(AzureRMModuleBase):
"""Utility class to get managed disk facts"""

def __init__(self):
self.module_arg_spec = dict(
resource_group=dict(
type='str'
),
name=dict(
type='str'
),
tags=dict(
type='str'
),
resource_group=dict(type='str'),
name=dict(type='str'),
tags=dict(type='list'),
managed_by=dict(type='str')
)

self.results = dict(
ansible_info=dict(
azure_managed_disk=[]
)
)

self.resource_group = None
self.name = None
self.create_option = None
self.source_uri = None
self.source_resource_uri = None
self.tags = None
super(AzureRMManagedDiskInfo, self).__init__(
derived_arg_spec=self.module_arg_spec,
supports_check_mode=True,
facts_module=True,
supports_tags=True)
self.managed_by = None

super(AzureRMManagedDiskInfo, self).__init__(derived_arg_spec=self.module_arg_spec,
supports_check_mode=True,
facts_module=True,
supports_tags=True)

def exec_module(self, **kwargs):
for key in self.module_arg_spec:
setattr(self, key, kwargs[key])

self.results['ansible_info']['azure_managed_disk'] = (
self.get_item() if self.name
else (self.list_items_by_resource_group() if self.resource_group else self.list_items())
)
if self.name and not self.resource_group:
self.fail('Parameter Error: name requires that resource_group also be set.')

if self.name:
self.results['ansible_info']['azure_managed_disk'] = self.get_disk()
elif self.resource_group:
self.results['ansible_info']['azure_managed_disk'] = self.list_disks_by_resource_group()
else:
self.results['ansible_info']['azure_managed_disk'] = self.list_disks()

return self.results

def get_item(self):
def get_disk(self):
"""Get a single managed disk"""
item = None
result = []
results = []

try:
item = self.compute_client.disks.get(
self.resource_group,
self.name)
results = [self.compute_client.disks.get(self.resource_group,
self.name)]
if self.managed_by:
results = [disk for disk in results if disk.managed_by == self.managed_by]
if self.tags:
results = [disk for disk in results if self.has_tags(disk.tags, self.tags)]
results = [self.managed_disk_to_dict(disk) for disk in results]
except CloudError:
pass
self.log('Could not find disk {0} in resource group {1}'.format(self.name, self.resource_group))

if item and self.has_tags(item.tags, self.tags):
result = [managed_disk_to_dict(item)]

return result
return results

def list_items(self):
def list_disks(self):
"""Get all managed disks"""
results = []

try:
response = self.compute_client.disks.list()
results = self.compute_client.disks.list()
if self.managed_by:
results = [disk for disk in results if disk.managed_by == self.managed_by]
if self.tags:
results = [disk for disk in results if self.has_tags(disk.tags, self.tags)]
results = [self.managed_disk_to_dict(disk) for disk in results]
except CloudError as exc:
self.fail('Failed to list all items - {0}'.format(str(exc)))

results = []
for item in response:
if self.has_tags(item.tags, self.tags):
results.append(managed_disk_to_dict(item))
return results

def list_items_by_resource_group(self):
def list_disks_by_resource_group(self):
"""Get managed disks in a resource group"""
results = []

try:
response = self.compute_client.disks.list_by_resource_group(resource_group_name=self.resource_group)
results = self.compute_client.disks.list_by_resource_group(resource_group_name=self.resource_group)
if self.managed_by:
results = [disk for disk in results if disk.managed_by == self.managed_by]
if self.tags:
results = [disk for disk in results if self.has_tags(disk.tags, self.tags)]
results = [self.managed_disk_to_dict(disk) for disk in results]
except CloudError as exc:
self.fail('Failed to list items by resource group - {0}'.format(str(exc)))

results = []
for item in response:
if self.has_tags(item.tags, eval(self.tags)):
results.append(managed_disk_to_dict(item))
return results

def managed_disk_to_dict(self, managed_disk):
create_data = managed_disk.creation_data
return dict(
id=managed_disk.id,
name=managed_disk.name,
location=managed_disk.location,
tags=managed_disk.tags,
create_option=create_data.create_option.lower(),
source_uri=create_data.source_uri or create_data.source_resource_id,
disk_size_gb=managed_disk.disk_size_gb,
os_type=managed_disk.os_type.lower() if managed_disk.os_type else None,
storage_account_type=managed_disk.sku.name if managed_disk.sku else None,
managed_by=managed_disk.managed_by,
zone=managed_disk.zones[0] if managed_disk.zones and len(managed_disk.zones) > 0 else ''
)


def main():
"""Main module execution code path"""

AzureRMManagedDiskInfo()


Expand Down
2 changes: 1 addition & 1 deletion tests/sanity/ignore-2.10.txt
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ plugins/modules/azure_rm_keyvaultsecret_info.py validate-modules:required_if-unk
plugins/modules/azure_rm_manageddisk.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_manageddisk.py validate-modules:required_if-requirements-unknown
plugins/modules/azure_rm_manageddisk.py validate-modules:required_if-unknown-key
plugins/modules/azure_rm_manageddisk_info.py validate-modules:doc-type-does-not-match-spec
plugins/modules/azure_rm_manageddisk_info.py validate-modules:parameter-list-no-elements
plugins/modules/azure_rm_manageddisk_info.py validate-modules:required_if-requirements-unknown
plugins/modules/azure_rm_manageddisk_info.py validate-modules:required_if-unknown-key
plugins/modules/azure_rm_manageddisk_info.py validate-modules:return-syntax-error
Expand Down
2 changes: 1 addition & 1 deletion tests/sanity/ignore-2.11.txt
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ plugins/modules/azure_rm_keyvaultsecret_info.py validate-modules:required_if-unk
plugins/modules/azure_rm_manageddisk.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_manageddisk.py validate-modules:required_if-requirements-unknown
plugins/modules/azure_rm_manageddisk.py validate-modules:required_if-unknown-key
plugins/modules/azure_rm_manageddisk_info.py validate-modules:doc-type-does-not-match-spec
plugins/modules/azure_rm_manageddisk_info.py validate-modules:parameter-list-no-elements
plugins/modules/azure_rm_manageddisk_info.py validate-modules:required_if-requirements-unknown
plugins/modules/azure_rm_manageddisk_info.py validate-modules:required_if-unknown-key
plugins/modules/azure_rm_manageddisk_info.py validate-modules:return-syntax-error
Expand Down
1 change: 0 additions & 1 deletion tests/sanity/ignore-2.9.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ plugins/modules/azure_rm_keyvault.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_keyvaultkey.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_keyvaultsecret.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_manageddisk.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_manageddisk_info.py validate-modules:doc-type-does-not-match-spec
plugins/modules/azure_rm_networkinterface.py validate-modules:doc-missing-type
plugins/modules/azure_rm_networkinterface.py validate-modules:parameter-type-not-in-doc
plugins/modules/azure_rm_networkinterface_info.py validate-modules:parameter-type-not-in-doc
Expand Down

0 comments on commit b697acc

Please sign in to comment.