From b1a9e7b8c8293ccfb14ec1cd990b12a19aa4ea9e Mon Sep 17 00:00:00 2001 From: Zim Kalinowski Date: Wed, 20 Feb 2019 10:45:47 +0800 Subject: [PATCH] adding dlt vn facts (#52552) --- ...azure_rm_devtestlabvirtualnetwork_facts.py | 212 ++++++++++++++++++ .../azure_rm_devtestlab/tasks/main.yml | 35 +++ 2 files changed, 247 insertions(+) create mode 100644 lib/ansible/modules/cloud/azure/azure_rm_devtestlabvirtualnetwork_facts.py diff --git a/lib/ansible/modules/cloud/azure/azure_rm_devtestlabvirtualnetwork_facts.py b/lib/ansible/modules/cloud/azure/azure_rm_devtestlabvirtualnetwork_facts.py new file mode 100644 index 00000000000..2a630f0280c --- /dev/null +++ b/lib/ansible/modules/cloud/azure/azure_rm_devtestlabvirtualnetwork_facts.py @@ -0,0 +1,212 @@ +#!/usr/bin/python +# +# Copyright (c) 2019 Zim Kalinowski, (@zikalino) +# +# 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_devtestlabvirtualnetwork_facts +version_added: "2.8" +short_description: Get Azure DevTest Lab Virtual Network facts. +description: + - Get facts of Azure DevTest Lab Virtual Network. + +options: + resource_group: + description: + - The name of the resource group. + required: True + lab_name: + description: + - The name of DevTest Lab. + required: True + name: + description: + - The name of DevTest Lab Virtual Network. + +extends_documentation_fragment: + - azure + +author: + - "Zim Kalinowski (@zikalino)" + +''' + +EXAMPLES = ''' + - name: Get instance of DevTest Lab Virtual Network + azure_rm_devtestlabvirtualnetwork_facts: + resource_group: myResourceGroup + lab_name: myLab + name: myVirtualNetwork + + - name: List all Virtual Networks in DevTest Lab + azure_rm_devtestlabvirtualnetwork_facts: + resource_group: myResourceGroup + lab_name: myLab + name: myVirtualNetwork +''' + +RETURN = ''' +virtualnetworks: + description: A list of dictionaries containing facts for DevTest Lab Virtual Network. + returned: always + type: complex + contains: + id: + description: + - The identifier of the virtual network. + returned: always + type: str + sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/myResourceGroup/providers/microsoft.devtestlab/labs/myLab/virt + ualnetworks/myVirtualNetwork" + resource_group: + description: + - Name of the resource group. + returned: always + type: str + sample: myResourceGroup + lab_name: + description: + - Name of the lab. + returned: always + type: str + sample: myLab + name: + description: + - Name of the virtual network. + returned: always + type: str + sample: myVirtualNetwork + description: + description: + - Description of the virtual network. + returned: always + type: str + sample: My Virtual Network + external_provider_resource_id: + description: + - Resource id of an external virtual network. + returned: always + type: str + sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/my + VirtualNetwork" + provisioning_state: + description: + - Provisioning state of the virtual network. + returned: always + type: str + sample: Succeeded +''' + +from ansible.module_utils.azure_rm_common import AzureRMModuleBase + +try: + from msrestazure.azure_exceptions import CloudError + from azure.mgmt.devtestlabs import DevTestLabsClient + from msrest.serialization import Model +except ImportError: + # This is handled in azure_rm_common + pass + + +class AzureRMDevTestLabVirtualNetworkFacts(AzureRMModuleBase): + def __init__(self): + # define user inputs into argument + self.module_arg_spec = dict( + resource_group=dict( + type='str', + required=True + ), + lab_name=dict( + type='str', + required=True + ), + name=dict( + type='str' + ) + ) + # store the results of the module operation + self.results = dict( + changed=False + ) + self.mgmt_client = None + self.resource_group = None + self.lab_name = None + self.name = None + super(AzureRMDevTestLabVirtualNetworkFacts, 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(DevTestLabsClient, + base_url=self._cloud_environment.endpoints.resource_manager) + + if self.name: + self.results['virtualnetworks'] = self.get() + else: + self.results['virtualnetworks'] = self.list() + + return self.results + + def list(self): + response = None + results = [] + try: + response = self.mgmt_client.virtual_networks.list(resource_group_name=self.resource_group, + lab_name=self.lab_name) + self.log("Response : {0}".format(response)) + except CloudError as e: + self.fail('Could not list Virtual Networks for DevTest Lab.') + + if response is not None: + for item in response: + results.append(self.format_response(item)) + + return results + + def get(self): + response = None + results = [] + try: + response = self.mgmt_client.virtual_networks.get(resource_group_name=self.resource_group, + lab_name=self.lab_name, + name=self.name) + self.log("Response : {0}".format(response)) + except CloudError as e: + self.fail('Could not get facts for Virtual Network.') + + if response: + results.append(self.format_response(response)) + + return results + + def format_response(self, item): + d = item.as_dict() + d = { + 'resource_group': self.resource_group, + 'lab_name': self.lab_name, + 'name': d.get('name', None), + 'id': d.get('id', None), + 'external_provider_resource_id': d.get('external_provider_resource_id', None), + 'provisioning_state': d.get('provisioning_state', None), + 'description': d.get('description', None) + } + return d + + +def main(): + AzureRMDevTestLabVirtualNetworkFacts() + + +if __name__ == '__main__': + main() diff --git a/test/integration/targets/azure_rm_devtestlab/tasks/main.yml b/test/integration/targets/azure_rm_devtestlab/tasks/main.yml index b50e11ca14e..6d55be20d49 100644 --- a/test/integration/targets/azure_rm_devtestlab/tasks/main.yml +++ b/test/integration/targets/azure_rm_devtestlab/tasks/main.yml @@ -135,6 +135,41 @@ that: - output.changed +- name: Get DevTest Lab Virtual Network facts + azure_rm_devtestlabvirtualnetwork_facts: + resource_group: "{{ resource_group }}" + lab_name: "{{ lab_name }}" + name: "{{ vn_name }}" + register: output +- name: Assert that facts are returned + assert: + that: + - output.changed == False + - output.virtualnetworks[0]['id'] != None + - output.virtualnetworks[0]['resource_group'] != None + - output.virtualnetworks[0]['lab_name'] != None + - output.virtualnetworks[0]['name'] != None + - output.virtualnetworks[0]['external_provider_resource_id'] != None + - output.virtualnetworks[0]['description'] != None + - output.virtualnetworks[0]['provisioning_state'] != None + +- name: List all Virtual Networks in DevTest Lab + azure_rm_devtestlabvirtualnetwork_facts: + resource_group: "{{ resource_group }}" + lab_name: "{{ lab_name }}" + register: output +- name: Assert that facts are returned + assert: + that: + - output.changed == False + - output.virtualnetworks[0]['id'] != None + - output.virtualnetworks[0]['resource_group'] != None + - output.virtualnetworks[0]['lab_name'] != None + - output.virtualnetworks[0]['name'] != None + - output.virtualnetworks[0]['external_provider_resource_id'] != None + - output.virtualnetworks[0]['description'] != None + - output.virtualnetworks[0]['provisioning_state'] != None + - name: Delete instance of Lab -- check mode azure_rm_devtestlab: resource_group: "{{ resource_group }}"