1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Azure Availability Set facts module (azure_rm_availabilityset_facts) (#28378)

* Adding facts module and Int. Tests

* register results

* Upgrading metadata version

* Updating author handle

To use Github handle
This commit is contained in:
Julien Stroheker 2017-08-29 16:48:36 -04:00 committed by Matt Davis
parent 6d5b936cf4
commit 6b7e38555c
4 changed files with 211 additions and 0 deletions

View file

@ -0,0 +1,177 @@
#!/usr/bin/python
#
# Copyright (c) 2016 Julien Stroheker, <juliens@microsoft.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'}
DOCUMENTATION = '''
---
module: azure_rm_availabilityset_facts
version_added: "2.4"
short_description: Get availability set facts.
description:
- Get facts for a specific availability set or all availability sets.
options:
name:
description:
- Limit results to a specific availability set
required: false
default: null
resource_group:
description:
- The resource group to search for the desired availability set
required: false
default: null
extends_documentation_fragment:
- azure
author:
- "Julien Stroheker (@julienstroheker)"
'''
EXAMPLES = '''
- name: Get facts for one availability set
azure_rm_availabilityset_facts:
name: Testing
resource_group: TestRG
- name: Get facts for all availability sets in a specific resource group
azure_rm_availabilityset_facts:
resource_group: TestRG
'''
RETURN = '''
azure_availabilityset:
description: List of availability sets dicts.
returned: always
type: list
example: [{
"location": "eastus2",
"name": "myavailabilityset",
"properties": {
"platformFaultDomainCount": 3,
"platformUpdateDomainCount": 2,
"virtualMachines": []
},
"sku": "Aligned",
"type": "Microsoft.Compute/availabilitySets"
}]
'''
from ansible.module_utils.azure_rm_common import AzureRMModuleBase
try:
from msrestazure.azure_exceptions import CloudError
except:
# handled in azure_rm_common
pass
AZURE_OBJECT_CLASS = 'AvailabilitySet'
class AzureRMAvailabilitySetFacts(AzureRMModuleBase):
"""Utility class to get availability set facts"""
def __init__(self):
self.module_args = dict(
name=dict(type='str'),
resource_group=dict(type='str'),
tags=dict(type='list')
)
self.results = dict(
changed=False,
ansible_facts=dict(
azure_availabilitysets=[]
)
)
self.name = None
self.resource_group = None
self.tags = None
super(AzureRMAvailabilitySetFacts, self).__init__(
derived_arg_spec=self.module_args,
supports_tags=False,
facts_module=True
)
def exec_module(self, **kwargs):
for key in self.module_args:
setattr(self, key, kwargs[key])
if self.name and not self.resource_group:
self.fail("Parameter error: resource group required when filtering by name.")
if self.name:
self.results['ansible_facts']['azure_availabilitysets'] = self.get_item()
else:
self.results['ansible_facts']['azure_availabilitysets'] = self.list_items()
return self.results
def get_item(self):
"""Get a single availability set"""
self.log('Get properties for {}'.format(self.name))
item = None
result = []
try:
item = self.compute_client.availability_sets.get(self.resource_group, self.name)
except CloudError:
pass
if item and self.has_tags(item.tags, self.tags):
avase = self.serialize_obj(item, AZURE_OBJECT_CLASS)
avase['name'] = item.name
avase['type'] = item.type
avase['sku'] = item.sku.name
result = [avase]
return result
def list_items(self):
"""Get all availability sets"""
self.log('List all availability sets')
try:
response = self.compute_client.availability_sets.list(self.resource_group)
except CloudError as exc:
self.fail('Failed to list all items - {}'.format(str(exc)))
results = []
for item in response:
if self.has_tags(item.tags, self.tags):
avase = self.serialize_obj(item, AZURE_OBJECT_CLASS)
avase['name'] = item.name
avase['type'] = item.type
avase['sku'] = item.sku.name
results.append(avase)
return results
def main():
"""Main module execution code path"""
AzureRMAvailabilitySetFacts()
if __name__ == '__main__':
main()

View file

@ -0,0 +1,3 @@
cloud/azure
posix/ci/cloud/azure
destructive

View file

@ -0,0 +1,2 @@
dependencies:
- setup_azure

View file

@ -0,0 +1,29 @@
- name: Create an availability set
azure_rm_availabilityset:
name: myavailabilityset1
resource_group: "{{ resource_group }}"
platform_update_domain_count: 5
platform_fault_domain_count: 2
sku: Classic
tags:
tag1: testtag
- name: Get facts for created availability set
azure_rm_availabilityset_facts:
name: myavailabilityset1
resource_group: "{{ resource_group }}"
register: results
- assert:
that:
- not results.changed
- not results.failed
- results.ansible_facts.azure_availabilitysets[0].properties.platformFaultDomainCount == 2
- results.ansible_facts.azure_availabilitysets[0].properties.platformUpdateDomainCount == 5
- results.ansible_facts.azure_availabilitysets[0].sku == 'Classic'
- name: Delete the availability set
azure_rm_availabilityset:
name: myavailabilityset1
resource_group: "{{ resource_group }}"
state: absent