mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Azure Availability Set module (azure_rm_availabilityset) (#28353)
* Adding the module and int. tests * upgrading metadata to 1.1
This commit is contained in:
parent
400085aa5b
commit
0b29162b97
4 changed files with 359 additions and 0 deletions
274
lib/ansible/modules/cloud/azure/azure_rm_availabilityset.py
Normal file
274
lib/ansible/modules/cloud/azure/azure_rm_availabilityset.py
Normal file
|
@ -0,0 +1,274 @@
|
|||
#!/usr/bin/python
|
||||
#
|
||||
# Copyright (c) 2017 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
|
||||
|
||||
version_added: "2.4"
|
||||
|
||||
short_description: Manage Azure availability set.
|
||||
|
||||
description:
|
||||
- Create, update and delete Azure availability set. An availability set cannot be updated, you will have to
|
||||
recreate one instead. The only update operation will be for the tags.
|
||||
|
||||
options:
|
||||
resource_group:
|
||||
description:
|
||||
- Name of a resource group where the availability set exists or will be created.
|
||||
required: true
|
||||
name:
|
||||
description:
|
||||
- Name of the availability set.
|
||||
required: true
|
||||
state:
|
||||
description:
|
||||
- Assert the state of the availability set. Use 'present' to create or update a availability set and
|
||||
'absent' to delete a availability set.
|
||||
default: present
|
||||
choices:
|
||||
- absent
|
||||
- present
|
||||
required: false
|
||||
location:
|
||||
description:
|
||||
- Valid azure location. Defaults to location of the resource group.
|
||||
default: resource_group location
|
||||
required: false
|
||||
platform_update_domain_count:
|
||||
description:
|
||||
- Update domains indicate groups of virtual machines and underlying physical hardware that can be rebooted at the same time. Default is 5.
|
||||
default: 5
|
||||
required: false
|
||||
platform_fault_domain_count:
|
||||
description:
|
||||
- Fault domains define the group of virtual machines that share a common power source and network switch. Should be between 1 and 3. Default is 3
|
||||
default: 3
|
||||
required: false
|
||||
sku:
|
||||
description:
|
||||
- Define if the availability set supports managed disks.
|
||||
default: Classic
|
||||
choices:
|
||||
- Classic
|
||||
- Aligned
|
||||
required: false
|
||||
extends_documentation_fragment:
|
||||
- azure
|
||||
- azure_tags
|
||||
|
||||
author:
|
||||
- "Julien Stroheker (@ju_stroh)"
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
- name: Create an availability set with default options
|
||||
azure_rm_availabilityset:
|
||||
name: myavailabilityset
|
||||
location: eastus
|
||||
resource_group: Testing
|
||||
|
||||
- name: Create an availability set with advanced options
|
||||
azure_rm_availabilityset:
|
||||
name: myavailabilityset
|
||||
location: eastus
|
||||
resource_group: Testing
|
||||
platform_update_domain_count: 5
|
||||
platform_fault_domain_count: 3
|
||||
sku: Aligned
|
||||
|
||||
- name: Delete an availability set
|
||||
azure_rm_availabilityset:
|
||||
name: myavailabilityset
|
||||
location: eastus
|
||||
resource_group: Testing
|
||||
state: absent
|
||||
'''
|
||||
|
||||
RETURN = '''
|
||||
state:
|
||||
description: Current state of the avaibility set
|
||||
returned: always
|
||||
type: dict
|
||||
changed:
|
||||
description: Whether or not the resource has changed
|
||||
returned: always
|
||||
type: bool
|
||||
'''
|
||||
|
||||
from ansible.module_utils.azure_rm_common import AzureRMModuleBase
|
||||
|
||||
try:
|
||||
from msrestazure.azure_exceptions import CloudError
|
||||
from azure.mgmt.compute.models import (
|
||||
AvailabilitySet, Sku
|
||||
)
|
||||
except ImportError:
|
||||
# This is handled in azure_rm_common
|
||||
pass
|
||||
|
||||
|
||||
def availability_set_to_dict(avaset):
|
||||
return dict(
|
||||
id=avaset.id,
|
||||
name=avaset.name,
|
||||
location=avaset.location,
|
||||
platform_update_domain_count=avaset.platform_update_domain_count,
|
||||
platform_fault_domain_count=avaset.platform_fault_domain_count,
|
||||
tags=avaset.tags,
|
||||
sku=avaset.sku.name
|
||||
)
|
||||
|
||||
|
||||
class AzureRMAvailabilitySet(AzureRMModuleBase):
|
||||
"""Configuration class for an Azure RM availability set resource"""
|
||||
|
||||
def __init__(self):
|
||||
self.module_arg_spec = dict(
|
||||
resource_group=dict(
|
||||
type='str',
|
||||
required=True
|
||||
),
|
||||
name=dict(
|
||||
type='str',
|
||||
required=True
|
||||
),
|
||||
state=dict(
|
||||
type='str',
|
||||
required=False,
|
||||
default='present',
|
||||
choices=['present', 'absent']
|
||||
),
|
||||
location=dict(
|
||||
type='str',
|
||||
required=False
|
||||
),
|
||||
platform_update_domain_count=dict(
|
||||
type='int',
|
||||
default=5,
|
||||
required=False
|
||||
),
|
||||
platform_fault_domain_count=dict(
|
||||
type='int',
|
||||
default=3,
|
||||
required=False
|
||||
),
|
||||
sku=dict(
|
||||
type='str',
|
||||
default='Classic',
|
||||
required=False,
|
||||
choices=['Classic', 'Aligned']
|
||||
)
|
||||
)
|
||||
|
||||
self.resource_group = None
|
||||
self.name = None
|
||||
self.location = None
|
||||
self.tags = None
|
||||
self.platform_update_domain_count = None
|
||||
self.platform_fault_domain_count = None
|
||||
self.sku = None
|
||||
|
||||
self.results = dict(changed=False, state=dict())
|
||||
|
||||
super(AzureRMAvailabilitySet, self).__init__(derived_arg_spec=self.module_arg_spec,
|
||||
supports_check_mode=True,
|
||||
supports_tags=True)
|
||||
|
||||
def exec_module(self, **kwargs):
|
||||
"""Main module execution method"""
|
||||
|
||||
for key in list(self.module_arg_spec.keys()) + ['tags']:
|
||||
setattr(self, key, kwargs[key])
|
||||
|
||||
results = dict()
|
||||
resource_group = None
|
||||
response = None
|
||||
|
||||
try:
|
||||
resource_group = self.get_resource_group(self.resource_group)
|
||||
except CloudError:
|
||||
self.fail('resource group {} not found'.format(self.resource_group))
|
||||
if not self.location:
|
||||
self.location = resource_group.location
|
||||
|
||||
# Check if the AS already present in the RG
|
||||
if self.state == 'present':
|
||||
response = self.get_availabilityset()
|
||||
if not response:
|
||||
self.results['state'] = self.create_availabilityset()
|
||||
self.results['changed'] = True
|
||||
else:
|
||||
self.log("AS already there, updating Tags")
|
||||
update_tags, response['tags'] = self.update_tags(response['tags'])
|
||||
if update_tags:
|
||||
self.results['state'] = self.create_availabilityset()
|
||||
self.results['changed'] = True
|
||||
elif self.state == 'absent':
|
||||
self.delete_availabilityset()
|
||||
self.results['changed'] = True
|
||||
|
||||
return self.results
|
||||
|
||||
def create_availabilityset(self):
|
||||
self.log("Creating availabilityset {0}".format(self.name))
|
||||
try:
|
||||
paramsSku = Sku(
|
||||
name=self.sku
|
||||
)
|
||||
params = AvailabilitySet(
|
||||
location=self.location,
|
||||
tags=self.tags,
|
||||
platform_update_domain_count=self.platform_update_domain_count,
|
||||
platform_fault_domain_count=self.platform_fault_domain_count,
|
||||
sku=paramsSku
|
||||
)
|
||||
response = self.compute_client.availability_sets.create_or_update(self.resource_group, self.name, params)
|
||||
except CloudError as e:
|
||||
self.log('Error attempting to create the availability set.')
|
||||
self.fail("Error creating the availability set: {0}".format(str(e)))
|
||||
|
||||
return availability_set_to_dict(response)
|
||||
|
||||
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:
|
||||
self.log('Error attempting to delete the availability set.')
|
||||
self.fail("Error deleting the availability set: {0}".format(str(e)))
|
||||
|
||||
return True
|
||||
|
||||
def get_availabilityset(self):
|
||||
self.log("Checking if the availabilityset {0} is present".format(self.name))
|
||||
found = False
|
||||
try:
|
||||
response = self.compute_client.availability_sets.get(self.resource_group, self.name)
|
||||
found = True
|
||||
except CloudError as e:
|
||||
self.log('Did not find the Availability set.')
|
||||
if found is True:
|
||||
return availability_set_to_dict(response)
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
"""Main execution"""
|
||||
AzureRMAvailabilitySet()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
|
@ -0,0 +1,3 @@
|
|||
cloud/azure
|
||||
posix/ci/cloud/azure
|
||||
destructive
|
|
@ -0,0 +1,2 @@
|
|||
dependencies:
|
||||
- setup_azure
|
|
@ -0,0 +1,80 @@
|
|||
- name: Create an availability set with default options
|
||||
azure_rm_availabilityset:
|
||||
name: myavailabilityset1
|
||||
resource_group: "{{ resource_group }}"
|
||||
tags:
|
||||
tag1: testtag
|
||||
register: output
|
||||
|
||||
- assert:
|
||||
that: output.changed
|
||||
|
||||
- name: Create an availability set with advanced options
|
||||
azure_rm_availabilityset:
|
||||
name: myavailabilityset2
|
||||
resource_group: "{{ resource_group }}"
|
||||
platform_update_domain_count: 5
|
||||
platform_fault_domain_count: 2
|
||||
sku: Aligned
|
||||
register: output
|
||||
|
||||
- assert:
|
||||
that: output.changed
|
||||
|
||||
- name: Attempt to modify availabilty set options (we don't support modify so no changes)
|
||||
azure_rm_availabilityset:
|
||||
name: myavailabilityset2
|
||||
resource_group: "{{ resource_group }}"
|
||||
platform_update_domain_count: 2
|
||||
platform_fault_domain_count: 2
|
||||
sku: Aligned
|
||||
register: output
|
||||
|
||||
- assert:
|
||||
that: not output.changed
|
||||
|
||||
- name: Modify availabilty set options to update tags
|
||||
azure_rm_availabilityset:
|
||||
name: myavailabilityset2
|
||||
resource_group: "{{ resource_group }}"
|
||||
platform_update_domain_count: 5
|
||||
platform_fault_domain_count: 2
|
||||
sku: Aligned
|
||||
tags:
|
||||
test: modified
|
||||
register: output
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "output.state.tags.test == 'modified'"
|
||||
|
||||
- name: Create availability set with incorrect fault domain parameter
|
||||
azure_rm_availabilityset:
|
||||
name: myavailabilityset3
|
||||
resource_group: "{{ resource_group }}"
|
||||
platform_update_domain_count: 5
|
||||
platform_fault_domain_count: 4
|
||||
sku: Aligned
|
||||
register: output
|
||||
ignore_errors: yes
|
||||
|
||||
- assert:
|
||||
{ that: "'The specified fault domain count 4 must fall in the range 1 to' in output['msg']" }
|
||||
|
||||
- name: Delete an availability set
|
||||
azure_rm_availabilityset:
|
||||
name: myavailabilityset1
|
||||
resource_group: "{{ resource_group }}"
|
||||
state: absent
|
||||
|
||||
- name: Delete an availability set
|
||||
azure_rm_availabilityset:
|
||||
name: myavailabilityset2
|
||||
resource_group: "{{ resource_group }}"
|
||||
state: absent
|
||||
|
||||
- name: Delete an availability set
|
||||
azure_rm_availabilityset:
|
||||
name: myavailabilityset3
|
||||
resource_group: "{{ resource_group }}"
|
||||
state: absent
|
Loading…
Reference in a new issue