From dd6460b0b4cd0b476382b376d9b667cd1f6982a0 Mon Sep 17 00:00:00 2001 From: audevbot Date: Thu, 25 Jul 2019 13:06:43 +0000 Subject: [PATCH 1/2] Code generation for Ansible. --- .../cloud/azure/azure_rm_batchaccount.py | 32 ++- .../cloud/azure/azure_rm_batchaccount_info.py | 246 ++++++++++++++++++ .../azure_rm_batchaccount/tasks/main.yml | 41 ++- 3 files changed, 302 insertions(+), 17 deletions(-) create mode 100644 lib/ansible/modules/cloud/azure/azure_rm_batchaccount_info.py diff --git a/lib/ansible/modules/cloud/azure/azure_rm_batchaccount.py b/lib/ansible/modules/cloud/azure/azure_rm_batchaccount.py index 73cc47ec60cd77..95bc780ae8bea1 100644 --- a/lib/ansible/modules/cloud/azure/azure_rm_batchaccount.py +++ b/lib/ansible/modules/cloud/azure/azure_rm_batchaccount.py @@ -13,6 +13,8 @@ # This file is automatically generated by Magic Modules and manual # changes will be clobbered when the file is regenerated. # +# Please read more about how to change this file at +# https://github.com/Azure/magic-module-specs # # ---------------------------------------------------------------------------- @@ -37,31 +39,37 @@ description: - The name of the resource group in which to create the Batch Account. required: true + type: str name: description: - The name of the Batch Account. required: true + type: str location: description: - Specifies the supported Azure location where the resource exists. + type: str auto_storage_account: description: - Existing storage account with which to associate the Batch Account. - It can be the storage account name which is in the same resource group. - "It can be the storage account ID. e.g., - /subscriptions/{subscription_id}/resourceGroups/{resource_group}/providers/Microsoft.Storage/storageAccounts/{name}." + /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Storage/storageAccounts/mystorageaccount." - It can be a dict which contains C(name) and C(resource_group) of the storage account. + type: raw key_vault: description: - Existing key vault with which to associate the Batch Account. - It can be the key vault name which is in the same resource group. - "It can be the key vault ID. e.g., - /subscriptions/{subscription_id}/resourceGroups/{resource_group}/providers/Microsoft.KeyVault/vaults/{name}." + /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.KeyVault/vaults/myKeyVault." - It can be a dict which contains C(name) and C(resource_group) of the key vault. + type: raw pool_allocation_mode: description: - The pool acclocation mode of the Batch Account. default: batch_service + type: str choices: - batch_service - user_subscription @@ -85,7 +93,7 @@ EXAMPLES = ''' - name: Create Batch Account azure_rm_batchaccount: - resource_group: MyResGroup + resource_group: MyResourceGroup name: mybatchaccount location: eastus auto_storage_account: @@ -99,7 +107,7 @@ - The ID of the Batch account. returned: always type: str - sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Batch/batchAccounts/sampleacct" + sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Batch/batchAccounts/mybatchaccount" account_endpoint: description: - The account endpoint used to interact with the Batch service. @@ -200,14 +208,14 @@ def exec_module(self, **kwargs): self.batch_account.pop('auto_storage_account'), '/subscriptions/{subscription_id}/resourceGroups/{resource_group}/providers/Microsoft.Storage/storageAccounts/{name}') } - if self.batch_account.get('key_vault') is not None: - id = self.normalize_resource_id( - self.batch_account.pop('key_vault'), - '/subscriptions/{subscription_id}/resourceGroups/{resource_group}/providers/Microsoft.KeyVault/vaults/{name}') - url = 'https://' + id.split('/').pop() + '.vault.azure.net/' - self.batch_account['key_vault_reference'] = { - 'id': id, - 'url': url + if self.batch_account.get('key_vault') is not None: + id = self.normalize_resource_id( + self.batch_account.pop('key_vault'), + '/subscriptions/{subscription_id}/resourceGroups/{resource_group}/providers/Microsoft.KeyVault/vaults/{name}') + url = 'https://' + id.split('/').pop() + '.vault.azure.net/' + self.batch_account['key_vault_reference'] = { + 'id': id, + 'url': url } self.batch_account['pool_allocation_mode'] = _snake_to_camel(self.batch_account['pool_allocation_mode'], True) diff --git a/lib/ansible/modules/cloud/azure/azure_rm_batchaccount_info.py b/lib/ansible/modules/cloud/azure/azure_rm_batchaccount_info.py new file mode 100644 index 00000000000000..910fdab98b0cea --- /dev/null +++ b/lib/ansible/modules/cloud/azure/azure_rm_batchaccount_info.py @@ -0,0 +1,246 @@ +#!/usr/bin/python +# +# Copyright (C) 2019 Junyi Yi (@JunyiYi) +# +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +# ---------------------------------------------------------------------------- +# +# *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** +# +# ---------------------------------------------------------------------------- +# +# This file is automatically generated by Magic Modules and manual +# changes will be clobbered when the file is regenerated. +# +# Please read more about how to change this file at +# https://github.com/Azure/magic-module-specs +# +# ---------------------------------------------------------------------------- + +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_batchaccount_info +version_added: "2.9" +short_description: Gather info for Azure Batch Account +description: + - Gather info for Azure Batch Account. + +options: + resource_group: + description: + - The name of the resource group in which to create the Batch Account. + required: true + type: str + name: + description: + - The name of the Batch Account. + type: str + tags: + description: + - Limit results by providing a list of tags. Format tags as 'key' or 'key:value'. + type: list + +extends_documentation_fragment: + - azure + +author: + - "Junyi Yi (@JunyiYi)" +''' + +EXAMPLES = ''' + - name: Get instance of Batch Account + azure_rm_batchaccount_info: + resource_group: MyResourceGroup + name: mybatchaccount + + - name: List instances of Batch Account + azure_rm_batchaccount_info: + resource_group: MyResourceGroup +''' + +RETURN = ''' +items: + description: List of items + returned: always + type: complex + contains: + id: + description: + - The ID of the Batch account. + returned: always + type: str + sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Batch/batchAccounts/mybatchaccount" + resource_group: + description: + - The name of the resource group in which to create the Batch Account. + returned: always + type: str + name: + description: + - The name of the Batch Account. + returned: always + type: str + location: + description: + - Specifies the supported Azure location where the resource exists. + returned: always + type: str + account_endpoint: + description: + - The account endpoint used to interact with the Batch service. + returned: always + type: str + sample: sampleacct.westus.batch.azure.com + auto_storage_account: + description: + - Existing storage account with which to associate the Batch Account. + returned: always + type: str + sample: + "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Storage/storageAccounts/mystorageaccount" + key_vault: + description: + - Existing key vault with which to associate the Batch Account. + returned: always + type: str + sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.KeyVault/vaults/myKeyVault" + pool_allocation_mode: + description: + - The pool acclocation mode of the Batch Account. + returned: always + type: str + tags: + description: + - Resource tags. + returned: always + type: list +''' + +from ansible.module_utils.azure_rm_common import AzureRMModuleBase + +try: + from msrestazure.azure_exceptions import CloudError + from azure.mgmt.batch import BatchManagementClient + from msrest.serialization import Model +except ImportError: + # This is handled in azure_rm_common + pass + + +class AzureRMBatchAccountInfo(AzureRMModuleBase): + def __init__(self): + # define user inputs into argument + self.module_arg_spec = dict( + resource_group=dict( + required=True, + type='str' + ), + name=dict( + type='str' + ), + tags=dict( + type='list' + ) + ) + # store the results of the module operation + self.results = dict( + changed=False + ) + self.mgmt_client = None + self.resource_group = None + self.name = None + self.tags = None + super(AzureRMBatchAccountInfo, self).__init__(self.module_arg_spec, supports_tags=False) + + def exec_module(self, **kwargs): + for key in self.module_arg_spec: + setattr(self, key, kwargs[key]) + self.mgmt_client = self.get_mgmt_svc_client(BatchManagementClient, + base_url=self._cloud_environment.endpoints.resource_manager) + + if self.resource_group is not None and self.name is not None: + self.results['items'] = self.get() + elif self.resource_group is not None: + self.results['items'] = self.list_by_resource_group() + else: + self.results['items'] = self.list_by_subscription() + return self.results + + def get(self): + response = None + results = [] + try: + response = self.mgmt_client.batch_account.get(resource_group_name=self.resource_group, + account_name=self.name) + self.log("Response : {0}".format(response)) + except CloudError as e: + self.log('Could not get info for Batch Account.') + + if response and self.has_tags(response.tags, self.tags): + results.append(self.format_response(response)) + + return results + + def list_by_resource_group(self): + response = None + results = [] + try: + response = self.mgmt_client.batch_account.list_by_resource_group(resource_group_name=self.resource_group) + self.log("Response : {0}".format(response)) + except CloudError as e: + self.log('Could not get info for Batch Account.') + + if response is not None: + for item in response: + if self.has_tags(item.tags, self.tags): + results.append(self.format_response(item)) + + return results + + def list_by_subscription(self): + response = None + results = [] + try: + response = self.mgmt_client.batch_account.list() + self.log("Response : {0}".format(response)) + except CloudError as e: + self.log('Could not get info for Batch Account.') + + if response is not None: + for item in response: + if self.has_tags(item.tags, self.tags): + results.append(self.format_response(item)) + + return results + + def format_response(self, item): + d = item.as_dict() + d = { + 'id': d['id'], + 'resource_group': self.resource_group, + 'name': d['name'], + 'location': d['location'], + 'account_endpoint': d['account_endpoint'], + 'auto_storage_account': d['auto_storage']['storage_account_id'], + 'key_vault': d['key_vault_reference']['id'], + 'pool_allocation_mode': d['pool_allocation_mode'], + 'tags': d['tags'], + } + return d + + +def main(): + AzureRMBatchAccountInfo() + + +if __name__ == "__main__": + main() diff --git a/test/integration/targets/azure_rm_batchaccount/tasks/main.yml b/test/integration/targets/azure_rm_batchaccount/tasks/main.yml index e62cb67cfc40f5..73df56c310789b 100644 --- a/test/integration/targets/azure_rm_batchaccount/tasks/main.yml +++ b/test/integration/targets/azure_rm_batchaccount/tasks/main.yml @@ -8,6 +8,8 @@ # This file is automatically generated by Magic Modules and manual # changes will be clobbered when the file is regenerated. # +# Please read more about how to change this file at +# https://github.com/Azure/magic-module-specs # # ---------------------------------------------------------------------------- - name: Prepare random number @@ -53,14 +55,43 @@ that: - not output.changed +- name: Get instance of Batch Account + azure_rm_batchaccount_info: + resource_group: "{{ resource_group }}" + name: "{{ batch_account_name }}" + register: output + +- name: Assert that info is returned + assert: + that: + - not output.changed + - output.items[0]['id'] != None + - output.items[0]['resource_group'] != None + - output.items[0]['name'] != None + - output.items[0]['location'] != None + - output.items[0]['auto_storage_account'] != None + - output.items[0]['pool_allocation_mode'] != None + +- name: List instances of Batch Account + azure_rm_batchaccount_info: + resource_group: "{{ resource_group }}" + register: output + +- name: Assert that info is returned + assert: + that: + - not output.changed + - output.items[0]['id'] != None + - output.items[0]['resource_group'] != None + - output.items[0]['name'] != None + - output.items[0]['location'] != None + - output.items[0]['auto_storage_account'] != None + - output.items[0]['pool_allocation_mode'] != None + - name: Delete Batch Account azure_rm_batchaccount: resource_group: "{{ resource_group }}" name: "{{ batch_account_name }}" - location: eastus - auto_storage_account: - name: "{{ storage_account_name }}" - pool_allocation_mode: batch_service state: absent register: output @@ -69,7 +100,7 @@ that: - output.changed -- name: Clean up storage account +- name: Delete Storage Account azure_rm_storageaccount: resource_group: "{{ resource_group }}" name: "{{ storage_account_name }}" From ade3c3ad44d694fed9e0d8ea6d8b4a76a0390f8f Mon Sep 17 00:00:00 2001 From: audevbot Date: Thu, 15 Aug 2019 22:08:31 +0000 Subject: [PATCH 2/2] Code generation for Ansible. --- .../azure/azure_rm_healthprobesetting.py | 322 ++++++++++++++++++ 1 file changed, 322 insertions(+) create mode 100644 lib/ansible/modules/cloud/azure/azure_rm_healthprobesetting.py diff --git a/lib/ansible/modules/cloud/azure/azure_rm_healthprobesetting.py b/lib/ansible/modules/cloud/azure/azure_rm_healthprobesetting.py new file mode 100644 index 00000000000000..54d491c99d1059 --- /dev/null +++ b/lib/ansible/modules/cloud/azure/azure_rm_healthprobesetting.py @@ -0,0 +1,322 @@ +#!/usr/bin/python +# +# Copyright (C) 2019 audevbot +# +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +# ---------------------------------------------------------------------------- +# +# *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** +# +# ---------------------------------------------------------------------------- +# +# This file is automatically generated by Magic Modules and manual +# changes will be clobbered when the file is regenerated. +# +# Please read more about how to change this file at +# https://github.com/Azure/magic-module-specs +# +# ---------------------------------------------------------------------------- + +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_healthprobesetting +version_added: "2.9" +short_description: Manage Azure HealthProbeSetting instance. +description: + - Create, update and delete instance of Azure Health Probe Setting. + +options: + id: + description: + - Resource ID. + type: str + resource_group: + description: + - Name of the Resource group within the Azure subscription. + required: true + type: str + name: + description: + - Resource name. + type: str + front_door_name: + description: + - Name of the Front Door which is globally unique. + required: true + type: str + interval_in_seconds: + description: + - The number of seconds between health probes. + type: int + path: + description: + - The path to use for the health probe. Default is /. + type: str + protocol: + description: + - Protocol scheme to use for this probe. + default: http + type: str + choices: + - http + - https + resource_state: + description: + - Resource status. + default: creating + type: str + choices: + - creating + - enabling + - enabled + - disabling + - disabled + - deleting + state: + description: + - Assert the state of the Health Probe Setting. + - Use 'present' to create or update a Health Probe Setting and 'absent' to delete it. + default: present + choices: + - present + - absent + +extends_documentation_fragment: + - azure + +author: + - audevbot +''' + + +RETURN = ''' +type: + description: + - Resource type. + returned: always + type: str +''' + +import time +from ansible.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt +from ansible.module_utils.common.dict_transformations import _snake_to_camel + +try: + from msrestazure.azure_exceptions import CloudError + from msrest.polling import LROPoller + from msrestazure.azure_operation import AzureOperationPoller + from msrest.serialization import Model + from azure.mgmt.frontdoor import FrontdoorClient +except ImportError: + # This is handled in azure_rm_common + pass + + +class Actions: + NoAction, Create, Update, Delete = range(4) + + +class AzureRMHealthProbeSetting(AzureRMModuleBaseExt): + """Configuration class for an Azure RM Health Probe Setting resource""" + + def __init__(self): + self.module_arg_spec = dict( + id=dict( + type='str', + updatable=False, + disposition='/' + ), + resource_group=dict( + required=True, + type='str' + ), + name=dict( + required=True, + type='str' + ), + name=dict( + type='str' + ), + front_door_name=dict( + required=True, + type='str', + updatable=False, + disposition='/' + ), + interval_in_seconds=dict( + type='int' + ), + path=dict( + type='str' + ), + protocol=dict( + default='http', + type='str', + choices=['http', 'https'] + ), + resource_state=dict( + default='creating', + type='str', + choices=['creating', 'enabling', 'enabled', 'disabling', 'disabled', 'deleting'] + ), + state=dict( + type='str', + default='present', + choices=['present', 'absent'] + ) + ) + + self.resource_group = None + self.front_door_name = None + self.name = None + self.health_probe_settings_parameters = dict() + + self.results = dict(changed=False) + self.mgmt_client = None + self.state = None + self.to_do = Actions.NoAction + + super(AzureRMHealthProbeSetting, self).__init__(derived_arg_spec=self.module_arg_spec, + supports_check_mode=True, + supports_tags=False) + + def exec_module(self, **kwargs): + """Main module execution method""" + + for key in list(self.module_arg_spec.keys()): + if hasattr(self, key): + setattr(self, key, kwargs[key]) + elif kwargs[key] is not None: + self.health_probe_settings_parameters[key] = kwargs[key] + + self.health_probe_settings_parameters['protocol'] = _snake_to_camel(self.health_probe_settings_parameters['protocol'], True) + self.health_probe_settings_parameters['resource_state'] = _snake_to_camel(self.health_probe_settings_parameters['resource_state'], True) + + response = None + + self.mgmt_client = self.get_mgmt_svc_client(FrontdoorClient, + base_url=self._cloud_environment.endpoints.resource_manager) + + old_response = self.get_healthprobesetting() + + if not old_response: + self.log("Health Probe Setting instance doesn't exist") + if self.state == 'absent': + self.log("Old instance didn't exist") + else: + self.to_do = Actions.Create + else: + self.log("Health Probe Setting instance already exists") + if self.state == 'absent': + self.to_do = Actions.Delete + elif self.state == 'present': + self.results['old'] = old_response + self.results['new'] = self.health_probe_settings_parameters + if not self.idempotency_check(old_response, self.health_probe_settings_parameters): + self.to_do = Actions.Update + + if (self.to_do == Actions.Create) or (self.to_do == Actions.Update): + self.log("Need to Create / Update the Health Probe Setting instance") + + self.results['changed'] = True + if self.check_mode: + return self.results + + response = self.create_update_healthprobesetting() + + self.log("Creation / Update done") + elif self.to_do == Actions.Delete: + self.log("Health Probe Setting instance deleted") + self.results['changed'] = True + + if self.check_mode: + return self.results + + self.delete_healthprobesetting() + else: + self.log("Health Probe Setting instance unchanged") + self.results['changed'] = False + response = old_response + + if self.state == 'present': + self.results.update({ + 'type': response.get('type', None) + }) + return self.results + + def create_update_healthprobesetting(self): + ''' + Creates or updates Health Probe Setting with the specified configuration. + + :return: deserialized Health Probe Setting instance state dictionary + ''' + self.log("Creating / Updating the Health Probe Setting instance {0}".format(self.name)) + + try: + response = self.mgmt_client.health_probe_settings.create_or_update(resource_group_name=self.resource_group, + front_door_name=self.front_door_name, + health_probe_settings_name=self.name, + health_probe_settings_parameters=self.health_probe_settings_parameters) + if isinstance(response, LROPoller) or isinstance(response, AzureOperationPoller): + response = self.get_poller_result(response) + except CloudError as exc: + self.log('Error attempting to create the Health Probe Setting instance.') + self.fail("Error creating the Health Probe Setting instance: {0}".format(str(exc))) + return response.as_dict() + + def delete_healthprobesetting(self): + ''' + Deletes specified Health Probe Setting instance in the specified subscription and resource group. + + :return: True + ''' + self.log("Deleting the Health Probe Setting instance {0}".format(self.name)) + try: + response = self.mgmt_client.health_probe_settings.delete(resource_group_name=self.resource_group, + front_door_name=self.front_door_name, + health_probe_settings_name=self.name) + except CloudError as e: + self.log('Error attempting to delete the Health Probe Setting instance.') + self.fail("Error deleting the Health Probe Setting instance: {0}".format(str(e))) + + if isinstance(response, LROPoller) or isinstance(response, AzureOperationPoller): + response = self.get_poller_result(response) + return True + + def get_healthprobesetting(self): + ''' + Gets the properties of the specified Health Probe Setting + :return: deserialized Health Probe Setting instance state dictionary + ''' + self.log("Checking if the Health Probe Setting instance {0} is present".format(self.name)) + found = False + try: + response = self.mgmt_client.health_probe_settings.get(resource_group_name=self.resource_group, + front_door_name=self.front_door_name, + health_probe_settings_name=self.name) + found = True + self.log("Response : {0}".format(response)) + self.log("Health Probe Setting instance : {0} found".format(response.name)) + except CloudError as e: + self.log('Did not find the Health Probe Setting instance.') + if found is True: + return response.as_dict() + return False + + +def main(): + """Main execution""" + AzureRMHealthProbeSetting() + + +if __name__ == '__main__': + main()