diff --git a/lib/ansible/modules/cloud/azure/azure_rm_availabilityset.py b/lib/ansible/modules/cloud/azure/azure_rm_availabilityset.py index 4e30b3d69fc..6854e6c3276 100644 --- a/lib/ansible/modules/cloud/azure/azure_rm_availabilityset.py +++ b/lib/ansible/modules/cloud/azure/azure_rm_availabilityset.py @@ -70,7 +70,7 @@ extends_documentation_fragment: - azure_tags author: - - "Julien Stroheker (@ju_stroh)" + - "Julien Stroheker (@julienstroheker)" ''' EXAMPLES = ''' @@ -121,6 +121,10 @@ except ImportError: def availability_set_to_dict(avaset): + ''' + Serialazing the availability set from the API to Dict + :return: dict + ''' return dict( id=avaset.id, name=avaset.name, @@ -180,6 +184,8 @@ class AzureRMAvailabilitySet(AzureRMModuleBase): self.platform_update_domain_count = None self.platform_fault_domain_count = None self.sku = None + self.state = None + self.warning = False self.results = dict(changed=False, state=dict()) @@ -193,9 +199,9 @@ class AzureRMAvailabilitySet(AzureRMModuleBase): for key in list(self.module_arg_spec.keys()) + ['tags']: setattr(self, key, kwargs[key]) - results = dict() resource_group = None response = None + to_be_updated = False try: resource_group = self.get_resource_group(self.resource_group) @@ -207,25 +213,57 @@ class AzureRMAvailabilitySet(AzureRMModuleBase): # Check if the AS already present in the RG if self.state == 'present': response = self.get_availabilityset() + self.results['state'] = response + if not response: - self.results['state'] = self.create_availabilityset() - self.results['changed'] = True + to_be_updated = 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 + self.log("Tags has to be updated") + to_be_updated = True + + if response['platform_update_domain_count'] != self.platform_update_domain_count: + self.faildeploy('platform_update_domain_count') + + if response['platform_fault_domain_count'] != self.platform_fault_domain_count: + self.faildeploy('platform_fault_domain_count') + + if response['sku'] != self.sku: + self.faildeploy('sku') + + if self.check_mode: + return self.results + + if to_be_updated: + self.results['state'] = self.create_or_update_availabilityset() + self.results['changed'] = True + elif self.state == 'absent': self.delete_availabilityset() self.results['changed'] = True return self.results - def create_availabilityset(self): + def faildeploy(self, param): + ''' + Helper method to push fail message in the console. + Usefull to notify that the users cannot change some values in a Availibility Set + + :param: variable's name impacted + :return: void + ''' + self.fail("You tried to change {0} but is was unsuccessful. An Availability Set is immutable, except tags".format(str(param))) + + def create_or_update_availabilityset(self): + ''' + Method calling the Azure SDK to create or update the AS. + :return: void + ''' self.log("Creating availabilityset {0}".format(self.name)) try: - paramsSku = Sku( + params_sku = Sku( name=self.sku ) params = AvailabilitySet( @@ -233,7 +271,7 @@ class AzureRMAvailabilitySet(AzureRMModuleBase): tags=self.tags, platform_update_domain_count=self.platform_update_domain_count, platform_fault_domain_count=self.platform_fault_domain_count, - sku=paramsSku + sku=params_sku ) response = self.compute_client.availability_sets.create_or_update(self.resource_group, self.name, params) except CloudError as e: @@ -243,6 +281,10 @@ class AzureRMAvailabilitySet(AzureRMModuleBase): return availability_set_to_dict(response) def delete_availabilityset(self): + ''' + Method calling the Azure SDK to delete the AS. + :return: void + ''' self.log("Deleting availabilityset {0}".format(self.name)) try: response = self.compute_client.availability_sets.delete(self.resource_group, self.name) @@ -253,6 +295,10 @@ class AzureRMAvailabilitySet(AzureRMModuleBase): return True def get_availabilityset(self): + ''' + Method calling the Azure SDK to get an AS. + :return: void + ''' self.log("Checking if the availabilityset {0} is present".format(self.name)) found = False try: diff --git a/test/integration/targets/azure_rm_availabilityset/aliases b/test/integration/targets/azure_rm_availabilityset/aliases index 30a167dea3b..b1cd4a5978c 100644 --- a/test/integration/targets/azure_rm_availabilityset/aliases +++ b/test/integration/targets/azure_rm_availabilityset/aliases @@ -1,4 +1,2 @@ cloud/azure -posix/ci/cloud/azure -posix/ci/cloud/smoketest destructive diff --git a/test/integration/targets/azure_rm_availabilityset/tasks/main.yml b/test/integration/targets/azure_rm_availabilityset/tasks/main.yml index d01aa487e67..f09be7d9f4b 100644 --- a/test/integration/targets/azure_rm_availabilityset/tasks/main.yml +++ b/test/integration/targets/azure_rm_availabilityset/tasks/main.yml @@ -4,10 +4,10 @@ resource_group: "{{ resource_group }}" tags: tag1: testtag - register: output + register: results - assert: - that: output.changed + that: results.changed - name: Create an availability set with advanced options azure_rm_availabilityset: @@ -16,22 +16,41 @@ platform_update_domain_count: 5 platform_fault_domain_count: 2 sku: Aligned - register: output + register: results - assert: - that: output.changed + that: results.changed -- name: Attempt to modify availabilty set options (we don't support modify so no changes) +- name: Modify availabilty set immutable options - no changes, issue warning azure_rm_availabilityset: name: myavailabilityset2 resource_group: "{{ resource_group }}" platform_update_domain_count: 2 platform_fault_domain_count: 2 sku: Aligned - register: output + register: results - assert: - that: not output.changed + that: + - not results.changed + - results.warnings[0] == 'You tried to change platform_update_domain_count but is was unsuccessful' + +- name: Modify availabilty set immutable options and set tags - change tags and issue warning for immutable options + azure_rm_availabilityset: + name: myavailabilityset2 + resource_group: "{{ resource_group }}" + platform_update_domain_count: 2 + platform_fault_domain_count: 2 + sku: Aligned + tags: + test1: modified + register: results + +- assert: + that: + - results.changed + - results.state.tags.test1 == 'modified' + - results.warnings[0] == 'You tried to change platform_update_domain_count but is was unsuccessful' - name: Modify availabilty set options to update tags azure_rm_availabilityset: @@ -41,12 +60,12 @@ platform_fault_domain_count: 2 sku: Aligned tags: - test: modified - register: output + test2: modified + register: results - assert: that: - - "output.state.tags.test == 'modified'" + - results.state.tags.test2 == 'modified' - name: Create availability set with incorrect fault domain parameter azure_rm_availabilityset: @@ -55,11 +74,29 @@ platform_update_domain_count: 5 platform_fault_domain_count: 4 sku: Aligned - register: output + register: results ignore_errors: yes - assert: - { that: "'The specified fault domain count 4 must fall in the range 1 to' in output['msg']" } + { that: "'The specified fault domain count 4 must fall in the range 1 to' in results['msg']" } + +- name: Test check_mode + azure_rm_availabilityset: + name: myavailabilityset2 + resource_group: "{{ resource_group }}" + platform_update_domain_count: 5 + platform_fault_domain_count: 2 + sku: Aligned + tags: + checktest1: modified1 + checktest2: modified2 + check_mode: yes + register: results + +- assert: + that: + - not results.changed + - results.state.tags.checktest1 == 'modified1' - name: Delete an availability set azure_rm_availabilityset: