From 914bef60ddff60655e62f241fd7ca406c7f7fa66 Mon Sep 17 00:00:00 2001 From: Fred-sun Date: Wed, 11 Aug 2021 20:51:16 +0800 Subject: [PATCH 1/4] Add azure_rm_virtualhub*.py --- plugins/modules/azure_rm_virtualhub.py | 803 ++++++++++++++++++ plugins/modules/azure_rm_virtualhub_info.py | 620 ++++++++++++++ pr-pipelines.yml | 1 + .../targets/azure_rm_virtualhub/aliases | 3 + .../targets/azure_rm_virtualhub/meta/main.yml | 2 + .../azure_rm_virtualhub/tasks/main.yml | 94 ++ 6 files changed, 1523 insertions(+) create mode 100644 plugins/modules/azure_rm_virtualhub.py create mode 100644 plugins/modules/azure_rm_virtualhub_info.py create mode 100644 tests/integration/targets/azure_rm_virtualhub/aliases create mode 100644 tests/integration/targets/azure_rm_virtualhub/meta/main.yml create mode 100644 tests/integration/targets/azure_rm_virtualhub/tasks/main.yml diff --git a/plugins/modules/azure_rm_virtualhub.py b/plugins/modules/azure_rm_virtualhub.py new file mode 100644 index 000000000..56f8d12cf --- /dev/null +++ b/plugins/modules/azure_rm_virtualhub.py @@ -0,0 +1,803 @@ +#!/usr/bin/python +# +# Copyright (c) 2020 GuopengLin, (@t-glin) +# +# 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 + + +DOCUMENTATION = ''' +--- +module: azure_rm_virtualhub +version_added: '1.7.0' +short_description: Manage Azure VirtualHub instance +description: + - Create, update and delete instance of Azure VirtualHub. +options: + resource_group: + description: + - The resource group name of the VirtualHub. + required: true + type: str + location: + description: + - The location of the VirtualHub. + type: str + name: + description: + - The name of the VirtualHub. + required: true + type: str + virtual_wan: + description: + - The VirtualWAN to which the VirtualHub belongs. + type: dict + suboptions: + id: + description: + - Resource ID. + type: str + vpn_gateway: + description: + - The VpnGateway associated with this VirtualHub. + type: dict + suboptions: + id: + description: + - Resource ID. + type: str + p2_s_vpn_gateway: + description: + - The P2SVpnGateway associated with this VirtualHub. + type: dict + suboptions: + id: + description: + - Resource ID. + type: str + express_route_gateway: + description: + - The expressRouteGateway associated with this VirtualHub. + type: dict + suboptions: + id: + description: + - Resource ID. + type: str + azure_firewall: + description: + - The azureFirewall associated with this VirtualHub. + type: dict + suboptions: + id: + description: + - Resource ID. + type: str + security_partner_provider: + description: + - The securityPartnerProvider associated with this VirtualHub. + type: dict + suboptions: + id: + description: + - Resource ID. + type: str + address_prefix: + description: + - Address-prefix for this VirtualHub. + type: str + route_table: + description: + - The routeTable associated with this virtual hub. + type: dict + suboptions: + routes: + description: + - List of all routes. + elements: dict + type: list + suboptions: + address_prefixes: + description: + - List of all addressPrefixes. + type: list + elements: str + next_hop_ip_address: + description: + - NextHop ip address. + type: str + security_provider_name: + description: + - The Security Provider name. + type: str + virtual_hub_route_table_v2_s: + description: + - List of all virtual hub route table v2s associated with this VirtualHub. + type: list + elements: dict + suboptions: + name: + description: + - The name of the resource that is unique within a resource group. + - This name can be used to access the resource. + type: str + routes: + description: + - List of all routes. + type: list + elements: dict + suboptions: + destination_type: + description: + - The type of destinations. + type: str + destinations: + description: + - List of all destinations. + type: list + elements: str + next_hop_type: + description: + - The type of next hops. + type: str + next_hops: + description: + - NextHops ip address. + type: list + elements: str + attached_connections: + description: + - List of all connections attached to this route table v2. + elements: str + type: list + sku: + description: + - The sku of this VirtualHub. + type: str + bgp_connections: + description: + - List of references to Bgp Connections. + type: list + elements: dict + suboptions: + id: + description: + - Resource ID. + type: str + ip_configurations: + description: + - List of references to IpConfigurations. + type: list + elements: dict + suboptions: + id: + description: + - Resource ID. + type: str + virtual_router_asn: + description: + - VirtualRouter ASN. + type: int + virtual_router_ips: + description: + - VirtualRouter IPs. + type: list + elements: str + enable_virtual_router_route_propogation: + description: + - Flag to control route propogation for VirtualRouter hub. + type: bool + state: + description: + - Assert the state of the VirtualHub. + - Use C(present) to create or update an VirtualHub and C(absent) to delete it. + default: present + type: str + choices: + - absent + - present +extends_documentation_fragment: + - azure.azcollection.azure + - azure.azcollection.azure_tags +author: + - Fred-Sun (@Fred-Sun) + - Haiyuan Zhang (@haiyuazhang) + +''' + +EXAMPLES = ''' + - name: Create a VirtualHub + azure_rm_virtualhub: + resource_group: myResourceGroup + name: my_virtual_hub_name + address_prefix: 10.2.0.0/24 + sku: Standard + location: eastus + enable_virtual_router_route_propogation: false + virtual_wan: + id: /subscriptions/xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualWans/fredwan + + - name: Delete VirtualHub + azure_rm_virtualhub: + resource_group: myResourceGroup + name: my_virtual_hub_name + location: eastus + state: absent +''' + +RETURN = ''' +state: + description: + - Current state of the virtual hub. + type: complex + returned: always + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: /subscriptions/xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualHubs/my_virtual_hub_name + name: + description: + - Resource name. + returned: always + type: str + sample: my_virtual_hub_name + type: + description: + - Resource type. + returned: always + type: str + sample: Microsoft.Network/virtualHubs + location: + description: + - Resource location. + returned: always + type: str + sample: eastus + tags: + description: + - Resource tags. + returned: always + type: dict + sample: { 'key1': 'value1' } + etag: + description: + - A unique read-only string that changes whenever the resource is updated. + returned: always + type: str + sample: cf8c0b06-d339-4155-95fd-2a363945cce4 + virtual_wan: + description: + - The VirtualWAN to which the VirtualHub belongs. + returned: always + type: complex + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: /subscriptions/xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualWans/fredwan + vpn_gateway: + description: + - The VpnGateway associated with this VirtualHub. + returned: always + type: complex + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: null + p2_s_vpn_gateway: + description: + - The P2SVpnGateway associated with this VirtualHub. + returned: always + type: complex + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: null + express_route_gateway: + description: + - The expressRouteGateway associated with this VirtualHub. + returned: always + type: dict + sample: null + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: null + azure_firewall: + description: + - The azureFirewall associated with this VirtualHub. + returned: always + type: complex + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: null + security_partner_provider: + description: + - The securityPartnerProvider associated with this VirtualHub. + returned: always + type: complex + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: null + address_prefix: + description: + - Address-prefix for this VirtualHub. + returned: always + type: str + sample: 10.2.0.0/24 + route_table: + description: + - The routeTable associated with this virtual hub. + returned: always + type: complex + contains: + routes: + description: + - List of all routes. + returned: always + type: list + contains: + address_prefixes: + description: + - List of all addressPrefixes. + returned: always + type: list + sample: null + next_hop_ip_address: + description: + - NextHop ip address. + returned: always + type: str + sample: null + provisioning_state: + description: + - The provisioning state of the virtual hub resource. + returned: always + type: str + sample: Succeeded + security_provider_name: + description: + - The Security Provider name. + returned: always + type: str + sample: null + virtual_hub_route_table_v2_s: + description: + - List of all virtual hub route table v2s associated with this VirtualHub. + returned: always + type: complex + contains: + name: + description: + - The name of the resource that is unique within a resource group. + - This name can be used to access the resource. + returned: always + type: str + sample: null + routes: + description: + - List of all routes. + returned: always + type: list + contains: + destination_type: + description: + - The type of destinations. + returned: always + type: str + sample: null + destinations: + description: + - List of all destinations. + returned: always + type: list + sample: null + next_hop_type: + description: + - The type of next hops. + returned: always + type: str + sample: null + next_hops: + description: + - NextHops ip address. + returned: always + type: list + sample: null + attached_connections: + description: + - List of all connections attached to this route table v2. + returned: always + type: list + sample: null + sku: + description: + - The sku of this VirtualHub. + returned: always + type: str + sample: null + routing_state: + description: + - The routing state. + returned: always + type: str + sample: Standard + bgp_connections: + description: + - List of references to Bgp Connections. + returned: always + type: list + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: null + ip_configurations: + description: + - List of references to IpConfigurations. + returned: always + type: list + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: null + virtual_router_asn: + description: + - VirtualRouter ASN. + returned: always + type: int + sample: null + virtual_router_ips: + description: + - VirtualRouter IPs. + returned: always + type: list + sample: null + enable_virtual_router_route_propogation: + description: + - Flag to control route propogation for VirtualRouter hub. + returned: always + type: bool + sample: null + +''' + +from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt +try: + from msrestazure.azure_exceptions import CloudError + from msrestazure.azure_operation import AzureOperationPoller + from msrest.polling import LROPoller +except ImportError: + # This is handled in azure_rm_common + pass + + +class Actions: + NoAction, Create, Update, Delete = range(4) + + +class AzureRMVirtualHub(AzureRMModuleBaseExt): + def __init__(self): + self.module_arg_spec = dict( + resource_group=dict( + type='str', + required=True + ), + location=dict( + type='str', + ), + name=dict( + type='str', + required=True + ), + virtual_wan=dict( + type='dict', + disposition='/virtual_wan', + options=dict( + id=dict( + type='str', + disposition='id' + ) + ) + ), + vpn_gateway=dict( + type='dict', + disposition='/vpn_gateway', + options=dict( + id=dict( + type='str', + disposition='id' + ) + ) + ), + p2_s_vpn_gateway=dict( + type='dict', + disposition='/p2_s_vpn_gateway', + options=dict( + id=dict( + type='str', + disposition='id' + ) + ) + ), + express_route_gateway=dict( + type='dict', + disposition='/express_route_gateway', + options=dict( + id=dict( + type='str', + disposition='id' + ) + ) + ), + azure_firewall=dict( + type='dict', + disposition='/azure_firewall', + options=dict( + id=dict( + type='str', + disposition='id' + ) + ) + ), + security_partner_provider=dict( + type='dict', + disposition='/security_partner_provider', + options=dict( + id=dict( + type='str', + disposition='id' + ) + ) + ), + address_prefix=dict( + type='str', + disposition='/address_prefix' + ), + route_table=dict( + type='dict', + disposition='/route_table', + options=dict( + routes=dict( + type='list', + disposition='routes', + elements='dict', + options=dict( + address_prefixes=dict( + type='list', + disposition='address_prefixes', + elements='str' + ), + next_hop_ip_address=dict( + type='str', + disposition='next_hop_ip_address' + ) + ) + ) + ) + ), + security_provider_name=dict( + type='str', + disposition='/security_provider_name' + ), + virtual_hub_route_table_v2_s=dict( + type='list', + disposition='/virtual_hub_route_table_v2_s', + elements='dict', + options=dict( + name=dict( + type='str', + disposition='name' + ), + routes=dict( + type='list', + disposition='routes', + elements='dict', + options=dict( + destination_type=dict( + type='str', + disposition='destination_type' + ), + destinations=dict( + type='list', + disposition='destinations', + elements='str' + ), + next_hop_type=dict( + type='str', + disposition='next_hop_type' + ), + next_hops=dict( + type='list', + disposition='next_hops', + elements='str' + ) + ) + ), + attached_connections=dict( + type='list', + disposition='attached_connections', + elements='str' + ) + ) + ), + sku=dict( + type='str', + disposition='/sku' + ), + bgp_connections=dict( + type='list', + updatable=False, + disposition='/bgp_connections', + elements='dict', + options=dict( + id=dict( + type='str', + disposition='id' + ) + ) + ), + ip_configurations=dict( + type='list', + updatable=False, + disposition='/ip_configurations', + elements='dict', + options=dict( + id=dict( + type='str', + disposition='id' + ) + ) + ), + virtual_router_asn=dict( + type='int', + disposition='/virtual_router_asn' + ), + virtual_router_ips=dict( + type='list', + disposition='/virtual_router_ips', + elements='str' + ), + enable_virtual_router_route_propogation=dict( + type='bool', + disposition='/enable_virtual_router_route_propogation' + ), + state=dict( + type='str', + default='present', + choices=['present', 'absent'] + ) + ) + + self.resource_group = None + self.name = None + self.location = None + self.body = {} + + self.results = dict(changed=False) + self.state = None + self.to_do = Actions.NoAction + + super(AzureRMVirtualHub, self).__init__(derived_arg_spec=self.module_arg_spec, + supports_check_mode=True, + supports_tags=True) + + def exec_module(self, **kwargs): + 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.body[key] = kwargs[key] + + self.inflate_parameters(self.module_arg_spec, self.body, 0) + + resource_group = self.get_resource_group(self.resource_group) + if self.location is None: + # Set default location + self.location = resource_group.location + self.body['location'] = self.location + + old_response = None + response = None + + old_response = self.get_resource() + + if not old_response: + if self.state == 'present': + self.to_do = Actions.Create + else: + if self.state == 'absent': + self.to_do = Actions.Delete + else: + modifiers = {} + self.create_compare_modifiers(self.module_arg_spec, '', modifiers) + self.results['modifiers'] = modifiers + self.results['compare'] = [] + if not self.default_compare(modifiers, self.body, old_response, '', self.results): + self.to_do = Actions.Update + + if (self.to_do == Actions.Create) or (self.to_do == Actions.Update): + self.results['changed'] = True + if self.check_mode: + return self.results + response = self.create_update_resource() + elif self.to_do == Actions.Delete: + self.results['changed'] = True + if self.check_mode: + return self.results + self.delete_resource() + else: + self.results['changed'] = False + response = old_response + + if response is not None: + self.results['state'] = response + + return self.results + + def create_update_resource(self): + try: + response = self.network_client.virtual_hubs.create_or_update(resource_group_name=self.resource_group, + virtual_hub_name=self.name, + virtual_hub_parameters=self.body) + if isinstance(response, AzureOperationPoller) or isinstance(response, LROPoller): + response = self.get_poller_result(response) + except CloudError as exc: + self.log('Error attempting to create the VirtualHub instance.') + self.fail('Error creating the VirtualHub instance: {0}'.format(str(exc))) + return response.as_dict() + + def delete_resource(self): + try: + response = self.network_client.virtual_hubs.delete(resource_group_name=self.resource_group, + virtual_hub_name=self.name) + except CloudError as e: + self.log('Error attempting to delete the VirtualHub instance.') + self.fail('Error deleting the VirtualHub instance: {0}'.format(str(e))) + + return True + + def get_resource(self): + try: + response = self.network_client.virtual_hubs.get(resource_group_name=self.resource_group, + virtual_hub_name=self.name) + except CloudError as e: + return False + return response.as_dict() + + +def main(): + AzureRMVirtualHub() + + +if __name__ == '__main__': + main() diff --git a/plugins/modules/azure_rm_virtualhub_info.py b/plugins/modules/azure_rm_virtualhub_info.py new file mode 100644 index 000000000..7fe6b4be6 --- /dev/null +++ b/plugins/modules/azure_rm_virtualhub_info.py @@ -0,0 +1,620 @@ +#!/usr/bin/python +# +# Copyright (c) 2020 GuopengLin, (@t-glin) +# +# 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 + + +DOCUMENTATION = ''' +--- +module: azure_rm_virtualhub_info +version_added: '1.7.0' +short_description: Get VirtualHub info +description: + - Get info of VirtualHub. +options: + resource_group: + description: + - The resource group name of the VirtualHub. + type: str + name: + description: + - The name of the VirtualHub. + type: str +extends_documentation_fragment: + - azure.azcollection.azure +author: + - Fred-Sun (@Fred-Sun) + - Haiyuan Zhang (@haiyuazhang) + +''' + +EXAMPLES = ''' + - name: Get virtual hub info by name + azure_rm_virtualhub_info: + resource_group: myResourceGroup + name: virtualHub + + - name: Get virtual hub info by resource group + azure_rm_virtualhub_info: + resource_group: myResourceGroup + + - name: Get birtual hub info by sub + azure_rm_virtualhub_info: + +''' + +RETURN = ''' +virtual_hubs: + description: + - A list of dict results where the key is the name of the VirtualHub and the values are the facts for that VirtualHub. + returned: always + type: complex + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: /subscriptions/xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualHubs/fredhub + name: + description: + - Resource name. + returned: always + type: str + sample: fredhub + type: + description: + - Resource type. + returned: always + type: str + sample: Microsoft.Network/virtualHubs + location: + description: + - Resource location. + returned: always + type: str + sample: eastus + tags: + description: + - Resource tags. + returned: always + type: dict + sample: {"key1":"value1"} + etag: + description: + - A unique read-only string that changes whenever the resource is updated. + returned: always + type: str + sample: 4462b9db-6569-49be-8d00-000178b29e90 + virtual_wan: + description: + - The VirtualWAN to which the VirtualHub belongs. + returned: always + type: complex + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: /subscriptions/xxx-xxx/resourceGroups/v-xisuRG/providers/Microsoft.Network/virtualWans/virtualwanfred01 + vpn_gateway: + description: + - The VpnGateway associated with this VirtualHub. + returned: always + type: complex + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: null + p2_s_vpn_gateway: + description: + - The P2SVpnGateway associated with this VirtualHub. + returned: always + type: complex + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: null + express_route_gateway: + description: + - The expressRouteGateway associated with this VirtualHub. + returned: always + type: complex + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: null + azure_firewall: + description: + - The azureFirewall associated with this VirtualHub. + returned: always + type: complex + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: null + security_partner_provider: + description: + - The securityPartnerProvider associated with this VirtualHub. + returned: always + type: complex + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: null + address_prefix: + description: + - Address-prefix for this VirtualHub. + returned: always + type: str + sample: 10.0.0.0/24 + route_table: + description: + - The routeTable associated with this virtual hub. + returned: always + type: complex + contains: + routes: + description: + - List of all routes. + returned: always + type: list + contains: + address_prefixes: + description: + - List of all addressPrefixes. + returned: always + type: list + sample: null + next_hop_ip_address: + description: + - NextHop ip address. + returned: always + type: str + sample: null + provisioning_state: + description: + - The provisioning state of the virtual hub resource. + returned: always + type: str + sample: Succeeded + security_provider_name: + description: + - The Security Provider name. + returned: always + type: str + sample: null + virtual_hub_route_table_v2_s: + description: + - List of all virtual hub route table v2s associated with this VirtualHub. + returned: always + type: complex + contains: + name: + description: + - The name of the resource that is unique within a resource group. + - This name can be used to access the resource. + returned: always + type: str + sample: null + routes: + description: + - List of all routes. + returned: always + type: complex + contains: + destination_type: + description: + - The type of destinations. + returned: always + type: str + sample: null + destinations: + description: + - List of all destinations. + returned: always + type: list + sample: null + next_hop_type: + description: + - The type of next hops. + returned: always + type: str + sample: null + next_hops: + description: + - NextHops ip address. + returned: always + type: list + sample: null + attached_connections: + description: + - List of all connections attached to this route table v2. + returned: always + type: list + sample: null + sku: + description: + - The sku of this VirtualHub. + returned: always + type: str + sample: Standard + routing_state: + description: + - The routing state. + returned: always + type: str + sample: null + bgp_connections: + description: + - List of references to Bgp Connections. + returned: always + type: list + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: null + ip_configurations: + description: + - List of references to IpConfigurations. + returned: always + type: list + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: null + virtual_router_asn: + description: + - VirtualRouter ASN. + returned: always + type: int + sample: null + virtual_router_ips: + description: + - VirtualRouter IPs. + returned: always + type: list + sample: null + enable_virtual_router_route_propogation: + description: + - Flag to control route propogation for VirtualRouter hub. + returned: always + type: bool + sample: null + value: + description: + - List of VirtualHubs. + returned: always + type: complex + contains: + virtual_wan: + description: + - The VirtualWAN to which the VirtualHub belongs. + returned: always + type: complex + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: null + vpn_gateway: + description: + - The VpnGateway associated with this VirtualHub. + returned: always + type: complex + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: null + p2_s_vpn_gateway: + description: + - The P2SVpnGateway associated with this VirtualHub. + returned: always + type: complex + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: null + express_route_gateway: + description: + - The expressRouteGateway associated with this VirtualHub. + returned: always + type: complex + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: null + azure_firewall: + description: + - The azureFirewall associated with this VirtualHub. + returned: always + type: complex + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: null + security_partner_provider: + description: + - The securityPartnerProvider associated with this VirtualHub. + returned: always + type: complex + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: null + address_prefix: + description: + - Address-prefix for this VirtualHub. + returned: always + type: str + sample: null + route_table: + description: + - The routeTable associated with this virtual hub. + returned: always + type: complex + contains: + routes: + description: + - List of all routes. + returned: always + type: list + contains: + address_prefixes: + description: + - List of all addressPrefixes. + returned: always + type: list + sample: null + next_hop_ip_address: + description: + - NextHop ip address. + returned: always + type: str + sample: null + security_provider_name: + description: + - The Security Provider name. + returned: always + type: str + sample: null + virtual_hub_route_table_v2_s: + description: + - List of all virtual hub route table v2s associated with this VirtualHub. + returned: always + type: complex + contains: + name: + description: + - The name of the resource that is unique within a resource group. + - This name can be used to access the resource. + returned: always + type: str + sample: null + routes: + description: + - List of all routes. + returned: always + type: list + contains: + destination_type: + description: + - The type of destinations. + returned: always + type: str + sample: null + destinations: + description: + - List of all destinations. + returned: always + type: list + sample: null + next_hop_type: + description: + - The type of next hops. + returned: always + type: str + sample: null + next_hops: + description: + - NextHops ip address. + returned: always + type: list + sample: null + attached_connections: + description: + - List of all connections attached to this route table v2. + returned: always + type: list + sample: null + sku: + description: + - The sku of this VirtualHub. + returned: always + type: str + sample: null + bgp_connections: + description: + - List of references to Bgp Connections. + returned: always + type: list + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: null + ip_configurations: + description: + - List of references to IpConfigurations. + returned: always + type: list + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: null + virtual_router_asn: + description: + - VirtualRouter ASN. + returned: always + type: int + sample: null + virtual_router_ips: + description: + - VirtualRouter IPs. + returned: always + type: list + sample: null + enable_virtual_router_route_propogation: + description: + - Flag to control route propogation for VirtualRouter hub. + returned: always + type: bool + sample: null + next_link: + description: + - URL to get the next set of operation list results if there are any. + returned: always + type: str + sample: null + +''' + +from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBase +try: + from msrestazure.azure_exceptions import CloudError +except ImportError: + # This is handled in azure_rm_common + pass + + +class AzureRMVirtualHubInfo(AzureRMModuleBase): + def __init__(self): + self.module_arg_spec = dict( + resource_group=dict( + type='str' + ), + name=dict( + type='str' + ) + ) + + self.resource_group = None + self.name = None + + self.results = dict(changed=False) + self.state = None + self.status_code = [200] + + super(AzureRMVirtualHubInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False) + + def exec_module(self, **kwargs): + + for key in self.module_arg_spec: + setattr(self, key, kwargs[key]) + + if (self.resource_group is not None and self.name is not None): + self.results['virtual_hubs'] = self.format_item(self.get()) + elif (self.resource_group is not None): + self.results['virtual_hubs'] = self.format_item(self.list_by_resource_group()) + else: + self.results['virtual_hubs'] = self.format_item(self.list()) + return self.results + + def get(self): + response = None + + try: + response = self.network_client.virtual_hubs.get(resource_group_name=self.resource_group, + virtual_hub_name=self.name) + except CloudError as e: + self.log('Could not get info for @(Model.ModuleOperationNameUpper).') + + return response + + def list_by_resource_group(self): + response = None + + try: + response = self.network_client.virtual_hubs.list_by_resource_group(resource_group_name=self.resource_group) + except CloudError as e: + self.log('Could not get info for @(Model.ModuleOperationNameUpper).') + + return response + + def list(self): + response = None + + try: + response = self.network_client.virtual_hubs.list() + except CloudError as e: + self.log('Could not get info for @(Model.ModuleOperationNameUpper).') + + return response + + def format_item(self, item): + if item is None: + return None + elif hasattr(item, 'as_dict'): + return [item.as_dict()] + else: + result = [] + items = list(item) + for tmp in items: + result.append(tmp.as_dict()) + return result + + +def main(): + AzureRMVirtualHubInfo() + + +if __name__ == '__main__': + main() diff --git a/pr-pipelines.yml b/pr-pipelines.yml index 6eaf37781..f4bb0f218 100644 --- a/pr-pipelines.yml +++ b/pr-pipelines.yml @@ -103,6 +103,7 @@ parameters: - "azure_rm_virtualnetworkpeering" - "azure_rm_virtualwan" - "azure_rm_vpnsite" + - "azure_rm_virtualhub" - "azure_rm_backupazurevm" - "azure_rm_recoveryservicesvault" - "azure_rm_vmbackuppolicy" diff --git a/tests/integration/targets/azure_rm_virtualhub/aliases b/tests/integration/targets/azure_rm_virtualhub/aliases new file mode 100644 index 000000000..5d29c6c4d --- /dev/null +++ b/tests/integration/targets/azure_rm_virtualhub/aliases @@ -0,0 +1,3 @@ +cloud/azure +shippable/azure/group10 +destructive diff --git a/tests/integration/targets/azure_rm_virtualhub/meta/main.yml b/tests/integration/targets/azure_rm_virtualhub/meta/main.yml new file mode 100644 index 000000000..95e1952f9 --- /dev/null +++ b/tests/integration/targets/azure_rm_virtualhub/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: + - setup_azure diff --git a/tests/integration/targets/azure_rm_virtualhub/tasks/main.yml b/tests/integration/targets/azure_rm_virtualhub/tasks/main.yml new file mode 100644 index 000000000..36707bec5 --- /dev/null +++ b/tests/integration/targets/azure_rm_virtualhub/tasks/main.yml @@ -0,0 +1,94 @@ +- set_fact: + name: "{{ resource_group | hash('md5') | truncate(22, True, '') }}" + +- name: Create a VirtualHub (check mode) + azure_rm_virtualhub: + resource_group: "{{ resource_group }}" + name: "{{ name }}" + sku: Standard + check_mode: yes + +- name: Create a VirtualHub + azure_rm_virtualhub: + resource_group: "{{ resource_group }}" + name: "{{ name }}" + sku: Standard + register: output + +- name: Assert the virtual hub is well created + assert: + that: + - output.changed + - output.state.provisioning_state == 'Succeeded' + +- name: Create a VirtualHub (idempotent) + azure_rm_virtualhub: + resource_group: "{{ resource_group }}" + name: "{{ name }}" + sku: Standard + register: output + +- name: Assert idempotent + assert: + that: + - not output.changed + +- name: Get Virtual Hub Info + azure_rm_virtualhub_info: + resource_group: "{{ resource_group }}" + name: "{{ name }}" + register: output + +- name: Assert fact returns + assert: + that: + - output.virtual_hubs[0].provisioning_state == "Succeeded" + - output.virtual_hubs[0].sku == "Standard" + +- name: Create a VirtualWan + azure_rm_virtualwan: + resource_group: "{{ resource_group }}" + name: "wan-{{ name }}" + disable_vpn_encryption: true + allow_branch_to_branch_traffic: true + allow_vnet_to_vnet_traffic: true + virtual_wan_type: Standard + register: output + +- name: Create Second VirtualHub + azure_rm_virtualhub: + resource_group: "{{ resource_group }}" + name: "{{ name }}-seondary" + sku: Standard + address_prefix: 12.0.0.0/16 + virtual_wan: + id: "{{ output.state.id }}" + virtual_router_asn: 65515 + virtual_router_ips: + - 12.0.32.4 + - 12.0.32.5 + register: output + +- name: Assert the virtual hub is well created + assert: + that: + - output.changed + - output.state.provisioning_state == 'Succeeded' + +- name: Delete Second VirtualHub + azure_rm_virtualhub: + resource_group: "{{ resource_group }}" + name: "{{ name }}-seondary" + state: absent + +- name: Delete virtual hub + azure_rm_virtualhub: + resource_group: "{{ resource_group }}" + name: "{{ name }}" + state: absent + register: output + +- name: Assert the AKS instance is upgraded + assert: + that: + - output.changed From 25e304163f4442d88802aca4807ceb0162cfa1de Mon Sep 17 00:00:00 2001 From: Fred-sun Date: Thu, 12 Aug 2021 13:32:17 +0800 Subject: [PATCH 2/4] fix sanity error --- plugins/modules/azure_rm_virtualhub.py | 68 +++++++++++++------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/plugins/modules/azure_rm_virtualhub.py b/plugins/modules/azure_rm_virtualhub.py index 56f8d12cf..1f31d5957 100644 --- a/plugins/modules/azure_rm_virtualhub.py +++ b/plugins/modules/azure_rm_virtualhub.py @@ -521,85 +521,85 @@ def __init__(self): ), virtual_wan=dict( type='dict', - disposition='/virtual_wan', + # disposition='/virtual_wan', options=dict( id=dict( type='str', - disposition='id' + # disposition='id' ) ) ), vpn_gateway=dict( type='dict', - disposition='/vpn_gateway', + # disposition='/vpn_gateway', options=dict( id=dict( type='str', - disposition='id' + # disposition='id' ) ) ), p2_s_vpn_gateway=dict( type='dict', - disposition='/p2_s_vpn_gateway', + # disposition='/p2_s_vpn_gateway', options=dict( id=dict( type='str', - disposition='id' + # disposition='id' ) ) ), express_route_gateway=dict( type='dict', - disposition='/express_route_gateway', + # disposition='/express_route_gateway', options=dict( id=dict( type='str', - disposition='id' + # disposition='id' ) ) ), azure_firewall=dict( type='dict', - disposition='/azure_firewall', + # disposition='/azure_firewall', options=dict( id=dict( type='str', - disposition='id' + # disposition='id' ) ) ), security_partner_provider=dict( type='dict', - disposition='/security_partner_provider', + # disposition='/security_partner_provider', options=dict( id=dict( type='str', - disposition='id' + # disposition='id' ) ) ), address_prefix=dict( type='str', - disposition='/address_prefix' + # disposition='/address_prefix' ), route_table=dict( type='dict', - disposition='/route_table', + # disposition='/route_table', options=dict( routes=dict( type='list', - disposition='routes', + # disposition='routes', elements='dict', options=dict( address_prefixes=dict( type='list', - disposition='address_prefixes', + # disposition='address_prefixes', elements='str' ), next_hop_ip_address=dict( type='str', - disposition='next_hop_ip_address' + # disposition='next_hop_ip_address' ) ) ) @@ -607,89 +607,89 @@ def __init__(self): ), security_provider_name=dict( type='str', - disposition='/security_provider_name' + # disposition='/security_provider_name' ), virtual_hub_route_table_v2_s=dict( type='list', - disposition='/virtual_hub_route_table_v2_s', + # disposition='/virtual_hub_route_table_v2_s', elements='dict', options=dict( name=dict( type='str', - disposition='name' + # disposition='name' ), routes=dict( type='list', - disposition='routes', + # disposition='routes', elements='dict', options=dict( destination_type=dict( type='str', - disposition='destination_type' + # disposition='destination_type' ), destinations=dict( type='list', - disposition='destinations', + # disposition='destinations', elements='str' ), next_hop_type=dict( type='str', - disposition='next_hop_type' + # disposition='next_hop_type' ), next_hops=dict( type='list', - disposition='next_hops', + # disposition='next_hops', elements='str' ) ) ), attached_connections=dict( type='list', - disposition='attached_connections', + # disposition='attached_connections', elements='str' ) ) ), sku=dict( type='str', - disposition='/sku' + # disposition='/sku' ), bgp_connections=dict( type='list', updatable=False, - disposition='/bgp_connections', + # disposition='/bgp_connections', elements='dict', options=dict( id=dict( type='str', - disposition='id' + # disposition='id' ) ) ), ip_configurations=dict( type='list', updatable=False, - disposition='/ip_configurations', + # disposition='/ip_configurations', elements='dict', options=dict( id=dict( type='str', - disposition='id' + # disposition='id' ) ) ), virtual_router_asn=dict( type='int', - disposition='/virtual_router_asn' + # disposition='/virtual_router_asn' ), virtual_router_ips=dict( type='list', - disposition='/virtual_router_ips', + # disposition='/virtual_router_ips', elements='str' ), enable_virtual_router_route_propogation=dict( type='bool', - disposition='/enable_virtual_router_route_propogation' + # disposition='/enable_virtual_router_route_propogation' ), state=dict( type='str', From 0761c3b019ce5e974e83b854dbc09f941b4c7b20 Mon Sep 17 00:00:00 2001 From: Fred-sun Date: Thu, 12 Aug 2021 13:45:09 +0800 Subject: [PATCH 3/4] fix sanity error 02 --- plugins/modules/azure_rm_virtualhub.py | 36 -------------------------- 1 file changed, 36 deletions(-) diff --git a/plugins/modules/azure_rm_virtualhub.py b/plugins/modules/azure_rm_virtualhub.py index 1f31d5957..cc3839b34 100644 --- a/plugins/modules/azure_rm_virtualhub.py +++ b/plugins/modules/azure_rm_virtualhub.py @@ -521,85 +521,68 @@ def __init__(self): ), virtual_wan=dict( type='dict', - # disposition='/virtual_wan', options=dict( id=dict( type='str', - # disposition='id' ) ) ), vpn_gateway=dict( type='dict', - # disposition='/vpn_gateway', options=dict( id=dict( type='str', - # disposition='id' ) ) ), p2_s_vpn_gateway=dict( type='dict', - # disposition='/p2_s_vpn_gateway', options=dict( id=dict( type='str', - # disposition='id' ) ) ), express_route_gateway=dict( type='dict', - # disposition='/express_route_gateway', options=dict( id=dict( type='str', - # disposition='id' ) ) ), azure_firewall=dict( type='dict', - # disposition='/azure_firewall', options=dict( id=dict( type='str', - # disposition='id' ) ) ), security_partner_provider=dict( type='dict', - # disposition='/security_partner_provider', options=dict( id=dict( type='str', - # disposition='id' ) ) ), address_prefix=dict( type='str', - # disposition='/address_prefix' ), route_table=dict( type='dict', - # disposition='/route_table', options=dict( routes=dict( type='list', - # disposition='routes', elements='dict', options=dict( address_prefixes=dict( type='list', - # disposition='address_prefixes', elements='str' ), next_hop_ip_address=dict( type='str', - # disposition='next_hop_ip_address' ) ) ) @@ -607,89 +590,70 @@ def __init__(self): ), security_provider_name=dict( type='str', - # disposition='/security_provider_name' ), virtual_hub_route_table_v2_s=dict( type='list', - # disposition='/virtual_hub_route_table_v2_s', elements='dict', options=dict( name=dict( type='str', - # disposition='name' ), routes=dict( type='list', - # disposition='routes', elements='dict', options=dict( destination_type=dict( type='str', - # disposition='destination_type' ), destinations=dict( type='list', - # disposition='destinations', elements='str' ), next_hop_type=dict( type='str', - # disposition='next_hop_type' ), next_hops=dict( type='list', - # disposition='next_hops', elements='str' ) ) ), attached_connections=dict( type='list', - # disposition='attached_connections', elements='str' ) ) ), sku=dict( type='str', - # disposition='/sku' ), bgp_connections=dict( type='list', - updatable=False, - # disposition='/bgp_connections', elements='dict', options=dict( id=dict( type='str', - # disposition='id' ) ) ), ip_configurations=dict( type='list', - updatable=False, - # disposition='/ip_configurations', elements='dict', options=dict( id=dict( type='str', - # disposition='id' ) ) ), virtual_router_asn=dict( type='int', - # disposition='/virtual_router_asn' ), virtual_router_ips=dict( type='list', - # disposition='/virtual_router_ips', elements='str' ), enable_virtual_router_route_propogation=dict( type='bool', - # disposition='/enable_virtual_router_route_propogation' ), state=dict( type='str', From 723e074a61b431dc9432f17aec8a7a359683e9c3 Mon Sep 17 00:00:00 2001 From: Fred-sun Date: Wed, 8 Dec 2021 11:58:03 +0800 Subject: [PATCH 4/4] update small --- plugins/modules/azure_rm_virtualhub.py | 4 ++-- plugins/modules/azure_rm_virtualhub_info.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/modules/azure_rm_virtualhub.py b/plugins/modules/azure_rm_virtualhub.py index cc3839b34..f56a8afd2 100644 --- a/plugins/modules/azure_rm_virtualhub.py +++ b/plugins/modules/azure_rm_virtualhub.py @@ -1,6 +1,6 @@ #!/usr/bin/python # -# Copyright (c) 2020 GuopengLin, (@t-glin) +# Copyright (c) 2020 XiuxiSun, (@Fred-sun) # # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) @@ -11,7 +11,7 @@ DOCUMENTATION = ''' --- module: azure_rm_virtualhub -version_added: '1.7.0' +version_added: '1.10.0' short_description: Manage Azure VirtualHub instance description: - Create, update and delete instance of Azure VirtualHub. diff --git a/plugins/modules/azure_rm_virtualhub_info.py b/plugins/modules/azure_rm_virtualhub_info.py index 7fe6b4be6..a310236f7 100644 --- a/plugins/modules/azure_rm_virtualhub_info.py +++ b/plugins/modules/azure_rm_virtualhub_info.py @@ -1,6 +1,6 @@ #!/usr/bin/python # -# Copyright (c) 2020 GuopengLin, (@t-glin) +# Copyright (c) 2020 XiuxiSun, (@Fred-sun) # # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) @@ -11,7 +11,7 @@ DOCUMENTATION = ''' --- module: azure_rm_virtualhub_info -version_added: '1.7.0' +version_added: '1.10.0' short_description: Get VirtualHub info description: - Get info of VirtualHub.