From f73ec3ba0e143e162af699bc78c80d3aa6d024d2 Mon Sep 17 00:00:00 2001 From: Fred-sun Date: Wed, 10 Aug 2022 00:14:10 +0800 Subject: [PATCH 01/13] add new module --- azure_rm_virtualhubconnection --- .../modules/azure_rm_virtualhubconnection.py | 461 ++++++++++++++++++ .../azure_rm_virtualhubconnection_info.py | 251 ++++++++++ 2 files changed, 712 insertions(+) create mode 100644 plugins/modules/azure_rm_virtualhubconnection.py create mode 100644 plugins/modules/azure_rm_virtualhubconnection_info.py diff --git a/plugins/modules/azure_rm_virtualhubconnection.py b/plugins/modules/azure_rm_virtualhubconnection.py new file mode 100644 index 000000000..581952cb9 --- /dev/null +++ b/plugins/modules/azure_rm_virtualhubconnection.py @@ -0,0 +1,461 @@ +#!/usr/bin/python +# +# Copyright (c) 2020 XiuxiSun, (@Fred-sun) +# +# 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_virtualhubconnection +version_added: '1.14.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 + name: + description: + - The name of the VirtualHub connection. + required: true + type: str + vhub_name: + description: + - The VirtualHub name. + type: str + required: True + enable_internet_security: + description: + - Enable internet security. + type: bool + allow_remote_vnet_to_use_hub_vnet_gateways: + description: + - Allow RemoteVnet to use Virtual Hub's gateways. + type: bool + allow_hub_to_remote_vnet_transit: + description: + - VirtualHub to RemoteVnet transit to enabled or not. + type: bool + remote_virtual_network: + type: dict + suboptions: + id: + description: + - The remote virtual network ID. + type: str + route_configuration: + description: + - The Routing Configuration indicating the associated and propagated route tables on this connection. + type: dict + suboptions: + associated_route_table: + description: + - The resource id RouteTable associated with this RoutingConfiguration. + type: dict + suboptions: + id: + description: + - The ID of RouteTable associated with this RoutingConfiguration. + type: str + propagated_route_tables: + description: + - The list of RouteTables to advertise the routes to. + type: list + elements: dict + suboptions: + labels: + description: + - The list of labels. + type: list + elements: str + ids: + description: + -The list of resource ids of all the RouteTables. + type: list + elements: dict + suboptions: + id: + description: + - The ID of the RouteTables. + type: str + vnet_routes: + description: + - List of routes that control routing from VirtualHub into a virtual network connection. + type: list + elements: dict + suboptions: + bgp_connections: + description: + - The list of references to HubBgpConnection objects. + type: list + elements: dict + suboptions: + id: + description: + - The ID of the HubBgpConnection object. + type: str + static_routes: + description: + - List of all Static Routes. + type: list + elements: dict + suoptions: + name: + description: + - The name of the StaticRoute that is unique within a VnetRoute. + type: str + address_prefixes: + description: + - List of all address prefixes. + type: list + elements: str + next_hop_ip_address: + description: + - The ip address of the next hop. + type: str + state: + description: + - Assert the state of the VirtualHub connection. + - Use C(present) to create or update an VirtualHub connection and C(absent) to delete it. + default: present + type: str + choices: + - absent + - present +extends_documentation_fragment: + - azure.azcollection.azure +author: + - Fred-Sun (@Fred-Sun) + - Xu Zhang (@xuzhang3) + +''' + +EXAMPLES = ''' + +''' + +RETURN = ''' +state: + description: + - A list of dict results for the virtual hub connection info. + returned: always + type: complex + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: "/subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualHubs/vhub/hubVirtualNetworkConnections/MyConnection + name: + description: + - Resource name. + returned: always + type: str + sample: MyConnection + etag: + description: + - A unique read-only string that changes whenever the resource is updated. + returned: always + type: str + sample: 31102041-49e7-4cac-8573-aac1e1a16793 + remote_virtual_network: + description: + - Name of ID of the remote VNet to connect to. + returned: always + type: complex + contains: + id: + description: + - The ID of the remote VNet to connect to. + returned: always + type: str + sample: /subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualNetworks/testvnet + routing_configuration: + description: + - The routing configuration information + returned: always + type: complex + contains: + associated_route_table: + description: + - The resource ID of route table associated with this routing configuration. + type: complex + returned: always + contains: + id: + description: + - The ID of the routetable. + type: str + returned: always + sample: /subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualHubs/testhub/hubRouteTables/rt_name + propagated_route_tables: + description: + - Space-separated list of resource id of propagated route tables. + type: complex + returned: always + contains: + ids: + description: + - The list resource ID of propagated route tables. + type: str + returned: always + sample: /subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualHubs/testhub/hubRouteTables/rt_name + labels: + description: + - Space-separated list of labels for propagated route tables. + type: list + returned: always + sample: ['labels1', 'labels2'] + vnet_routes: + description: + - The name of the Static Route that is unique within a Vnet Route. + returned: always + type: complex + contains: + static_routes: + description: + - The name of the Static Route. + type: list + returned: always + sample: [] + provisioning_state: + description: + - The provisioning state of the virtual hub connection resource. + returned: always + type: str + sample: Succeeded + allow_hub_to_remote_vnet_transit: + description: + - Enable hub to remote VNet transit. + returned: always + type: bool + sample: true + allow_remote_vnet_to_use_hub_vnet_gateways: + description: + - Allow remote VNet to use hub's VNet gateways. + returned: always + type: bool + sample: true + enable_internet_security: + description: + - Enable internet security and default is enabled. + type: bool + returned: always + sample: true +''' + +from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt +try: + from msrestazure.azure_operation import AzureOperationPoller + from azure.core.exceptions import ResourceNotFoundError + from azure.core.polling import LROPoller +except ImportError: + # This is handled in azure_rm_common + pass + + +class Actions: + NoAction, Create, Update, Delete = range(4) + + +static_routes_spec = dict( + name=dict(type='str'), + address_prefixes=dict(type='list', elements='str'), + next_hop_ip_address=dict(type='str') +) + + +class AzureRMVirtualHubConnection(AzureRMModuleBaseExt): + def __init__(self): + self.module_arg_spec = dict( + resource_group=dict( + type='str', + required=True + ), + vhub_name=dict( + type='str', + required=True + ), + name=dict( + type='str', + required=True + ), + enable_internet_security=dict( + type='bool' + ), + allow_remote_vnet_to_use_hub_vnet_gateways=dict( + type='bool' + ), + allow_hub_to_remote_vnet_transit=dict( + type='bool' + ), + remote_virtual_network=dict( + type='dict', + options=dict( + id=dict( + type='str', + ) + ), + routing_configuration=dict( + type='dict', + options=dict( + associated_route_table=dict( + type='dict', + options=dict( + id=dict( + type='str', + ) + ), + propagated_route_tables=dict( + type='list', + elements='dict', + options=dict( + labels=dict( + type='list', + elements='str' + ), + ids=dict( + type='list', + elements='dict', + options=dict( + id=dict( + type='str', + ) + ) + ) + ) + ), + vnet_routes=dict( + type='list', + elements: 'dict', + options=dict( + static_routes=dict( + type='list', + elements='dict', + options=static_routes_spec + ), + bgp_connections=dict( + type='dict', + options=dict( + id=dict( + type='str' + ) + ) + ) + ) + ), + ) + ), + state=dict( + type='str', + default='present', + choices=['present', 'absent'] + ) + + self.resource_group = None + self.name = None + self.body = {} + + self.results = dict(changed=False) + self.state = None + self.to_do = Actions.NoAction + + super(AzureRMVirtualHubConnection, self).__init__(derived_arg_spec=self.module_arg_spec, + supports_check_mode=True, + supports_tags=False) + + 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) + + 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.hub_virtual_network_connections.begin_create_or_update(resource_group_name=self.resource_group, + virtual_hub_name=self.vhub_name, + connection_name=self.name, + hub_virtual_network_connection_parameters=self.body) + if isinstance(response, AzureOperationPoller) or isinstance(response, LROPoller): + response = self.get_poller_result(response) + except Exception 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.hub_virtual_network_connections.begin_delete(resource_group_name=self.resource_group, + virtual_hub_name=self.vhub_name, + connection_name=self.name) + except Exception as e: + self.log('Error attempting to delete the VirtualHub connection instance.') + self.fail('Error deleting the VirtualHub connection instance: {0}'.format(str(e))) + + return True + + def get_resource(self): + try: + response = self.network_client.hub_virtual_network_connections.get(resource_group_name=self.resource_group, + virtual_hub_name=self.vhub_name, + connection_name=self.name) + except ResourceNotFoundError as e: + return False + return response.as_dict() + + +def main(): + AzureRMVirtualHubConnection() + + +if __name__ == '__main__': + main() diff --git a/plugins/modules/azure_rm_virtualhubconnection_info.py b/plugins/modules/azure_rm_virtualhubconnection_info.py new file mode 100644 index 000000000..9fac597a7 --- /dev/null +++ b/plugins/modules/azure_rm_virtualhubconnection_info.py @@ -0,0 +1,251 @@ +#!/usr/bin/python +# +# Copyright (c) 2020 XiuxiSun, (@Fred-sun) +# +# 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_virtualhubconnection_info +version_added: '1.14.0' +short_description: Get VirtualHub info +description: + - Get info of VirtualHub. +options: + resource_group: + description: + - The resource group name of the VirtualHub. + type: str + required: True + virtual_hub_name: + description: + - The resource name of the VirtualHub. + type: str + required: True + name: + description: + - The name of the VirtualHub connection. + type: str +extends_documentation_fragment: + - azure.azcollection.azure +author: + - Fred-Sun (@Fred-Sun) + - Xu Zhang (@xuzhang3) + +''' + +EXAMPLES = ''' + - name: Get virtual hub connection info by name + azure_rm_virtualhubconnection_info: + resource_group: myResourceGroup + virtual_hub_name: virtualHub + name: vhubname + + - name: Get virtual hub connection info by resource group + azure_rm_virtualhubconnection_info: + resource_group: myResourceGroup + virtual_hub_name: virtualHub +''' + +RETURN = ''' +virtual_hub_connection: + description: + - A list of dict results for the virtual hub connection info. + returned: always + type: complex + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: "/subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualHubs/vhub/hubVirtualNetworkConnections/MyConnection + name: + description: + - Resource name. + returned: always + type: str + sample: MyConnection + etag: + description: + - A unique read-only string that changes whenever the resource is updated. + returned: always + type: str + sample: 31102041-49e7-4cac-8573-aac1e1a16793 + remote_virtual_network: + description: + - Name of ID of the remote VNet to connect to. + returned: always + type: complex + contains: + id: + description: + - The ID of the remote VNet to connect to. + returned: always + type: str + sample: /subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualNetworks/testvnet + routing_configuration: + description: + - The routing configuration information + returned: always + type: complex + contains: + associated_route_table: + description: + - The resource ID of route table associated with this routing configuration. + type: complex + returned: always + contains: + id: + description: + - The ID of the routetable. + type: str + returned: always + sample: /subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualHubs/testhub/hubRouteTables/rt_name + propagated_route_tables: + description: + - Space-separated list of resource id of propagated route tables. + type: complex + returned: always + contains: + ids: + description: + - The list resource ID of propagated route tables. + type: str + returned: always + sample: /subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualHubs/testhub/hubRouteTables/rt_name + labels: + description: + - Space-separated list of labels for propagated route tables. + type: list + returned: always + sample: ['labels1', 'labels2'] + vnet_routes: + description: + - The name of the Static Route that is unique within a Vnet Route. + returned: always + type: complex + contains: + static_routes: + description: + - The name of the Static Route. + type: list + returned: always + sample: [] + provisioning_state: + description: + - The provisioning state of the virtual hub connection resource. + returned: always + type: str + sample: Succeeded + allow_hub_to_remote_vnet_transit: + description: + - Enable hub to remote VNet transit. + returned: always + type: bool + sample: true + allow_remote_vnet_to_use_hub_vnet_gateways: + description: + - Allow remote VNet to use hub's VNet gateways. + returned: always + type: bool + sample: true + enable_internet_security: + description: + - Enable internet security and default is enabled. + type: bool + returned: always + sample: true +''' + +from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBase +try: + from azure.core.exceptions import ResourceNotFoundError +except ImportError: + # This is handled in azure_rm_common + pass + + +class AzureRMVirtualHubConnectionInfo(AzureRMModuleBase): + def __init__(self): + self.module_arg_spec = dict( + resource_group=dict( + type='str', + required=True + ), + name=dict( + type='str' + ), + virtual_hub_name=dict( + type='str', + required=True + ) + ) + + self.resource_group = None + self.name = None + self.virtual_hub_name = None + + self.results = dict(changed=False) + self.state = None + self.status_code = [200] + + super(AzureRMVirtualHubConnectionInfo, 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.name is not None: + self.results['virtual_hub_connection'] = self.format_item(self.get()) + else: + self.results['virtual_hub_connection'] = self.format_item(self.list()) + return self.results + + def get(self): + response = None + + try: + response = self.network_client.hub_virtual_network_connections.get(resource_group_name=self.resource_group, + virtual_hub_name=self.virtual_hub_name, + connection_name=self.name) + except ResourceNotFoundError: + self.log('Could not get info for @(Model.ModuleOperationNameUpper).') + + return response + + def list(self): + response = None + + try: + response = self.network_client.hub_virtual_network_connections.list(resource_group_name=self.resource_group, + virtual_hub_name=self.virtual_hub_name) + except Exception: + 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(): + AzureRMVirtualHubConnectionInfo() + + +if __name__ == '__main__': + main() From 5dbdc3ef56dd0e36f1c9b6055a4f1f12dd1e869d Mon Sep 17 00:00:00 2001 From: Fred-sun Date: Wed, 10 Aug 2022 11:02:15 +0800 Subject: [PATCH 02/13] improve the code --- .../modules/azure_rm_virtualhubconnection.py | 940 +++++++++--------- .../azure_rm_virtualhubconnection_info.py | 520 +++++----- 2 files changed, 748 insertions(+), 712 deletions(-) diff --git a/plugins/modules/azure_rm_virtualhubconnection.py b/plugins/modules/azure_rm_virtualhubconnection.py index 581952cb9..574bb3fe2 100644 --- a/plugins/modules/azure_rm_virtualhubconnection.py +++ b/plugins/modules/azure_rm_virtualhubconnection.py @@ -1,461 +1,479 @@ -#!/usr/bin/python -# -# Copyright (c) 2020 XiuxiSun, (@Fred-sun) -# -# 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_virtualhubconnection -version_added: '1.14.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 - name: - description: - - The name of the VirtualHub connection. - required: true - type: str - vhub_name: - description: - - The VirtualHub name. - type: str - required: True - enable_internet_security: - description: - - Enable internet security. - type: bool - allow_remote_vnet_to_use_hub_vnet_gateways: - description: - - Allow RemoteVnet to use Virtual Hub's gateways. - type: bool - allow_hub_to_remote_vnet_transit: - description: - - VirtualHub to RemoteVnet transit to enabled or not. - type: bool - remote_virtual_network: - type: dict - suboptions: - id: - description: - - The remote virtual network ID. - type: str - route_configuration: - description: - - The Routing Configuration indicating the associated and propagated route tables on this connection. - type: dict - suboptions: - associated_route_table: - description: - - The resource id RouteTable associated with this RoutingConfiguration. - type: dict - suboptions: - id: - description: - - The ID of RouteTable associated with this RoutingConfiguration. - type: str - propagated_route_tables: - description: - - The list of RouteTables to advertise the routes to. - type: list - elements: dict - suboptions: - labels: - description: - - The list of labels. - type: list - elements: str - ids: - description: - -The list of resource ids of all the RouteTables. - type: list - elements: dict - suboptions: - id: - description: - - The ID of the RouteTables. - type: str - vnet_routes: - description: - - List of routes that control routing from VirtualHub into a virtual network connection. - type: list - elements: dict - suboptions: - bgp_connections: - description: - - The list of references to HubBgpConnection objects. - type: list - elements: dict - suboptions: - id: - description: - - The ID of the HubBgpConnection object. - type: str - static_routes: - description: - - List of all Static Routes. - type: list - elements: dict - suoptions: - name: - description: - - The name of the StaticRoute that is unique within a VnetRoute. - type: str - address_prefixes: - description: - - List of all address prefixes. - type: list - elements: str - next_hop_ip_address: - description: - - The ip address of the next hop. - type: str - state: - description: - - Assert the state of the VirtualHub connection. - - Use C(present) to create or update an VirtualHub connection and C(absent) to delete it. - default: present - type: str - choices: - - absent - - present -extends_documentation_fragment: - - azure.azcollection.azure -author: - - Fred-Sun (@Fred-Sun) - - Xu Zhang (@xuzhang3) - -''' - -EXAMPLES = ''' - -''' - -RETURN = ''' -state: - description: - - A list of dict results for the virtual hub connection info. - returned: always - type: complex - contains: - id: - description: - - Resource ID. - returned: always - type: str - sample: "/subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualHubs/vhub/hubVirtualNetworkConnections/MyConnection - name: - description: - - Resource name. - returned: always - type: str - sample: MyConnection - etag: - description: - - A unique read-only string that changes whenever the resource is updated. - returned: always - type: str - sample: 31102041-49e7-4cac-8573-aac1e1a16793 - remote_virtual_network: - description: - - Name of ID of the remote VNet to connect to. - returned: always - type: complex - contains: - id: - description: - - The ID of the remote VNet to connect to. - returned: always - type: str - sample: /subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualNetworks/testvnet - routing_configuration: - description: - - The routing configuration information - returned: always - type: complex - contains: - associated_route_table: - description: - - The resource ID of route table associated with this routing configuration. - type: complex - returned: always - contains: - id: - description: - - The ID of the routetable. - type: str - returned: always - sample: /subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualHubs/testhub/hubRouteTables/rt_name - propagated_route_tables: - description: - - Space-separated list of resource id of propagated route tables. - type: complex - returned: always - contains: - ids: - description: - - The list resource ID of propagated route tables. - type: str - returned: always - sample: /subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualHubs/testhub/hubRouteTables/rt_name - labels: - description: - - Space-separated list of labels for propagated route tables. - type: list - returned: always - sample: ['labels1', 'labels2'] - vnet_routes: - description: - - The name of the Static Route that is unique within a Vnet Route. - returned: always - type: complex - contains: - static_routes: - description: - - The name of the Static Route. - type: list - returned: always - sample: [] - provisioning_state: - description: - - The provisioning state of the virtual hub connection resource. - returned: always - type: str - sample: Succeeded - allow_hub_to_remote_vnet_transit: - description: - - Enable hub to remote VNet transit. - returned: always - type: bool - sample: true - allow_remote_vnet_to_use_hub_vnet_gateways: - description: - - Allow remote VNet to use hub's VNet gateways. - returned: always - type: bool - sample: true - enable_internet_security: - description: - - Enable internet security and default is enabled. - type: bool - returned: always - sample: true -''' - -from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt -try: - from msrestazure.azure_operation import AzureOperationPoller - from azure.core.exceptions import ResourceNotFoundError - from azure.core.polling import LROPoller -except ImportError: - # This is handled in azure_rm_common - pass - - -class Actions: - NoAction, Create, Update, Delete = range(4) - - -static_routes_spec = dict( - name=dict(type='str'), - address_prefixes=dict(type='list', elements='str'), - next_hop_ip_address=dict(type='str') -) - - -class AzureRMVirtualHubConnection(AzureRMModuleBaseExt): - def __init__(self): - self.module_arg_spec = dict( - resource_group=dict( - type='str', - required=True - ), - vhub_name=dict( - type='str', - required=True - ), - name=dict( - type='str', - required=True - ), - enable_internet_security=dict( - type='bool' - ), - allow_remote_vnet_to_use_hub_vnet_gateways=dict( - type='bool' - ), - allow_hub_to_remote_vnet_transit=dict( - type='bool' - ), - remote_virtual_network=dict( - type='dict', - options=dict( - id=dict( - type='str', - ) - ), - routing_configuration=dict( - type='dict', - options=dict( - associated_route_table=dict( - type='dict', - options=dict( - id=dict( - type='str', - ) - ), - propagated_route_tables=dict( - type='list', - elements='dict', - options=dict( - labels=dict( - type='list', - elements='str' - ), - ids=dict( - type='list', - elements='dict', - options=dict( - id=dict( - type='str', - ) - ) - ) - ) - ), - vnet_routes=dict( - type='list', - elements: 'dict', - options=dict( - static_routes=dict( - type='list', - elements='dict', - options=static_routes_spec - ), - bgp_connections=dict( - type='dict', - options=dict( - id=dict( - type='str' - ) - ) - ) - ) - ), - ) - ), - state=dict( - type='str', - default='present', - choices=['present', 'absent'] - ) - - self.resource_group = None - self.name = None - self.body = {} - - self.results = dict(changed=False) - self.state = None - self.to_do = Actions.NoAction - - super(AzureRMVirtualHubConnection, self).__init__(derived_arg_spec=self.module_arg_spec, - supports_check_mode=True, - supports_tags=False) - - 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) - - 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.hub_virtual_network_connections.begin_create_or_update(resource_group_name=self.resource_group, - virtual_hub_name=self.vhub_name, - connection_name=self.name, - hub_virtual_network_connection_parameters=self.body) - if isinstance(response, AzureOperationPoller) or isinstance(response, LROPoller): - response = self.get_poller_result(response) - except Exception 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.hub_virtual_network_connections.begin_delete(resource_group_name=self.resource_group, - virtual_hub_name=self.vhub_name, - connection_name=self.name) - except Exception as e: - self.log('Error attempting to delete the VirtualHub connection instance.') - self.fail('Error deleting the VirtualHub connection instance: {0}'.format(str(e))) - - return True - - def get_resource(self): - try: - response = self.network_client.hub_virtual_network_connections.get(resource_group_name=self.resource_group, - virtual_hub_name=self.vhub_name, - connection_name=self.name) - except ResourceNotFoundError as e: - return False - return response.as_dict() - - -def main(): - AzureRMVirtualHubConnection() - - -if __name__ == '__main__': - main() +#!/usr/bin/python +# +# Copyright (c) 2020 XiuxiSun, (@Fred-sun) +# +# 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_virtualhubconnection +version_added: '1.14.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 + name: + description: + - The name of the VirtualHub connection. + required: true + type: str + vhub_name: + description: + - The VirtualHub name. + type: str + required: True + enable_internet_security: + description: + - Enable internet security. + type: bool + allow_remote_vnet_to_use_hub_vnet_gateways: + description: + - Allow RemoteVnet to use Virtual Hub's gateways. + type: bool + allow_hub_to_remote_vnet_transit: + description: + - VirtualHub to RemoteVnet transit to enabled or not. + type: bool + remote_virtual_network: + type: dict + suboptions: + id: + description: + - The remote virtual network ID. + type: str + route_configuration: + description: + - The Routing Configuration indicating the associated and propagated route tables on this connection. + type: dict + suboptions: + propagated_route_tables: + description: + - The list of RouteTables to advertise the routes to. + type: dict + suboptions: + labels: + description: + - The list of labels. + type: list + elements: str + ids: + description: + -The list of resource ids of all the virtual hub RouteTables. + type: list + elements: dict + suboptions: + id: + description: + - The ID of the RouteTables. + type: str + vnet_routes: + description: + - List of routes that control routing from VirtualHub into a virtual network connection. + type: dict + suboptions: + static_routes: + description: + - List of all Static Routes. + type: list + elements: dict + suoptions: + name: + description: + - The name of the StaticRoute that is unique within a VnetRoute. + type: str + address_prefixes: + description: + - List of all address prefixes. + type: list + elements: str + next_hop_ip_address: + description: + - The ip address of the next hop. + type: str + state: + description: + - Assert the state of the VirtualHub connection. + - Use C(present) to create or update an VirtualHub connection and C(absent) to delete it. + default: present + type: str + choices: + - absent + - present +extends_documentation_fragment: + - azure.azcollection.azure +author: + - Fred-Sun (@Fred-Sun) + - Xu Zhang (@xuzhang3) + +''' + +EXAMPLES = ''' +- name: Create virtual hub connection + azure_rm_virtualhubconnection: + resource_group: myRG + vhub_name: testhub + name: Myconnection + enable_internet_security: false + allow_remote_vnet_to_use_hub_vnet_gateways: true + allow_hub_to_remote_vnet_transit: true + remote_virtual_network: + id: /subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualNetworks/testvnet + routing_configuration: + propagated_route_tables: + labels: + - labels1 + - labels3 + ids: + - id: /subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualHubs/testhub01/hubRouteTables/testtable + vnet_routes: + static_routes: + - name: route1 + address_prefixes: + - 10.1.0.0/16 + - 10.2.0.0/16 + next_hop_ip_address: 10.0.0.68 + - name: route2 + address_prefixes: + - 10.4.0.0/16 + next_hop_ip_address: 10.0.0.65 + +- name: Delete virtual hub connection + azure_rm_virtualhubconnection: + resource_group: myRG + vhub_name: testhub + name: Myconnection + state: absent + +''' + +RETURN = ''' +state: + description: + - A list of dict results for the virtual hub connection info. + returned: always + type: complex + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: "/subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualHubs/vhub/hubVirtualNetworkConnections/MyConnection + name: + description: + - Resource name. + returned: always + type: str + sample: MyConnection + etag: + description: + - A unique read-only string that changes whenever the resource is updated. + returned: always + type: str + sample: 31102041-49e7-4cac-8573-aac1e1a16793 + remote_virtual_network: + description: + - Name of ID of the remote VNet to connect to. + returned: always + type: complex + contains: + id: + description: + - The ID of the remote VNet to connect to. + returned: always + type: str + sample: /subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualNetworks/testvnet + routing_configuration: + description: + - The routing configuration information + returned: always + type: complex + contains: + associated_route_table: + description: + - The resource ID of route table associated with this routing configuration. + type: complex + returned: always + contains: + id: + description: + - The ID of the routetable. + type: str + returned: always + sample: /subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualHubs/testhub/hubRouteTables/rt_name + propagated_route_tables: + description: + - Space-separated list of resource id of propagated route tables. + type: complex + returned: always + contains: + ids: + description: + - The list resource ID of propagated route tables. + type: list + returned: always + sample: [{ id: '/subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualHubs/testhub/hubRouteTables/rt_name'}] + labels: + description: + - Space-separated list of labels for propagated route tables. + type: list + returned: always + sample: ['labels1', 'labels2'] + vnet_routes: + description: + - The name of the Static Route that is unique within a Vnet Route. + returned: always + type: complex + contains: + static_routes: + description: + - The name of the Static Route. + type: list + returned: always + suboptions: + address_prefixes: + description: + - + type: list + returned: always + sample: ["10.1.0.0/16", "10.2.0.0/16"] + name: + description: + - The name of static router. + type: str + returned: always + sample: route1 + next_hop_ip_address: + description: + - The next hop ip address. + type: str + returned: always + sample: 10.0.0.65 + provisioning_state: + description: + - The provisioning state of the virtual hub connection resource. + returned: always + type: str + sample: Succeeded + allow_hub_to_remote_vnet_transit: + description: + - Enable hub to remote VNet transit. + returned: always + type: bool + sample: true + allow_remote_vnet_to_use_hub_vnet_gateways: + description: + - Allow remote VNet to use hub's VNet gateways. + returned: always + type: bool + sample: true + enable_internet_security: + description: + - Enable internet security and default is enabled. + type: bool + returned: always + sample: true +''' + +from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt +try: + from msrestazure.azure_operation import AzureOperationPoller + from azure.core.exceptions import ResourceNotFoundError + from azure.core.polling import LROPoller +except ImportError: + # This is handled in azure_rm_common + pass + + +class Actions: + NoAction, Create, Update, Delete = range(4) + + +static_routes_spec = dict( + name=dict(type='str'), + address_prefixes=dict(type='list', elements='str'), + next_hop_ip_address=dict(type='str') +) + + +class AzureRMVirtualHubConnection(AzureRMModuleBaseExt): + def __init__(self): + self.module_arg_spec = dict( + resource_group=dict( + type='str', + required=True + ), + vhub_name=dict( + type='str', + required=True + ), + name=dict( + type='str', + required=True + ), + enable_internet_security=dict( + type='bool' + ), + allow_remote_vnet_to_use_hub_vnet_gateways=dict( + type='bool' + ), + allow_hub_to_remote_vnet_transit=dict( + type='bool' + ), + remote_virtual_network=dict( + type='dict', + options=dict( + id=dict( + type='str', + ) + ) + ), + routing_configuration=dict( + type='dict', + options=dict( + propagated_route_tables=dict( + type='dict', + options=dict( + labels=dict( + type='list', + elements='str' + ), + ids=dict( + type='list', + elements='dict', + options=dict( + id=dict( + type='str', + ) + ) + ) + ) + ), + vnet_routes=dict( + type='dict', + options=dict( + static_routes=dict( + type='list', + elements='dict', + options=static_routes_spec + ) + ) + ) + ) + ), + state=dict( + type='str', + default='present', + choices=['present', 'absent'] + ) + ) + + self.resource_group = None + self.vhub_name = None + self.name = None + self.body = {} + + self.results = dict(changed=False) + self.state = None + self.to_do = Actions.NoAction + + super(AzureRMVirtualHubConnection, self).__init__(derived_arg_spec=self.module_arg_spec, + supports_check_mode=True, + supports_tags=False) + + 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) + + 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.hub_virtual_network_connections.begin_create_or_update(resource_group_name=self.resource_group, + virtual_hub_name=self.vhub_name, + connection_name=self.name, + hub_virtual_network_connection_parameters=self.body) + if isinstance(response, AzureOperationPoller) or isinstance(response, LROPoller): + response = self.get_poller_result(response) + except Exception 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.hub_virtual_network_connections.begin_delete(resource_group_name=self.resource_group, + virtual_hub_name=self.vhub_name, + connection_name=self.name) + except Exception as e: + self.log('Error attempting to delete the VirtualHub connection instance.') + self.fail('Error deleting the VirtualHub connection instance: {0}'.format(str(e))) + + return True + + def get_resource(self): + try: + response = self.network_client.hub_virtual_network_connections.get(resource_group_name=self.resource_group, + virtual_hub_name=self.vhub_name, + connection_name=self.name) + except ResourceNotFoundError as e: + return False + return response.as_dict() + + +def main(): + AzureRMVirtualHubConnection() + + +if __name__ == '__main__': + main() diff --git a/plugins/modules/azure_rm_virtualhubconnection_info.py b/plugins/modules/azure_rm_virtualhubconnection_info.py index 9fac597a7..693c9c280 100644 --- a/plugins/modules/azure_rm_virtualhubconnection_info.py +++ b/plugins/modules/azure_rm_virtualhubconnection_info.py @@ -1,251 +1,269 @@ -#!/usr/bin/python -# -# Copyright (c) 2020 XiuxiSun, (@Fred-sun) -# -# 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_virtualhubconnection_info -version_added: '1.14.0' -short_description: Get VirtualHub info -description: - - Get info of VirtualHub. -options: - resource_group: - description: - - The resource group name of the VirtualHub. - type: str - required: True - virtual_hub_name: - description: - - The resource name of the VirtualHub. - type: str - required: True - name: - description: - - The name of the VirtualHub connection. - type: str -extends_documentation_fragment: - - azure.azcollection.azure -author: - - Fred-Sun (@Fred-Sun) - - Xu Zhang (@xuzhang3) - -''' - -EXAMPLES = ''' - - name: Get virtual hub connection info by name - azure_rm_virtualhubconnection_info: - resource_group: myResourceGroup - virtual_hub_name: virtualHub - name: vhubname - - - name: Get virtual hub connection info by resource group - azure_rm_virtualhubconnection_info: - resource_group: myResourceGroup - virtual_hub_name: virtualHub -''' - -RETURN = ''' -virtual_hub_connection: - description: - - A list of dict results for the virtual hub connection info. - returned: always - type: complex - contains: - id: - description: - - Resource ID. - returned: always - type: str - sample: "/subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualHubs/vhub/hubVirtualNetworkConnections/MyConnection - name: - description: - - Resource name. - returned: always - type: str - sample: MyConnection - etag: - description: - - A unique read-only string that changes whenever the resource is updated. - returned: always - type: str - sample: 31102041-49e7-4cac-8573-aac1e1a16793 - remote_virtual_network: - description: - - Name of ID of the remote VNet to connect to. - returned: always - type: complex - contains: - id: - description: - - The ID of the remote VNet to connect to. - returned: always - type: str - sample: /subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualNetworks/testvnet - routing_configuration: - description: - - The routing configuration information - returned: always - type: complex - contains: - associated_route_table: - description: - - The resource ID of route table associated with this routing configuration. - type: complex - returned: always - contains: - id: - description: - - The ID of the routetable. - type: str - returned: always - sample: /subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualHubs/testhub/hubRouteTables/rt_name - propagated_route_tables: - description: - - Space-separated list of resource id of propagated route tables. - type: complex - returned: always - contains: - ids: - description: - - The list resource ID of propagated route tables. - type: str - returned: always - sample: /subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualHubs/testhub/hubRouteTables/rt_name - labels: - description: - - Space-separated list of labels for propagated route tables. - type: list - returned: always - sample: ['labels1', 'labels2'] - vnet_routes: - description: - - The name of the Static Route that is unique within a Vnet Route. - returned: always - type: complex - contains: - static_routes: - description: - - The name of the Static Route. - type: list - returned: always - sample: [] - provisioning_state: - description: - - The provisioning state of the virtual hub connection resource. - returned: always - type: str - sample: Succeeded - allow_hub_to_remote_vnet_transit: - description: - - Enable hub to remote VNet transit. - returned: always - type: bool - sample: true - allow_remote_vnet_to_use_hub_vnet_gateways: - description: - - Allow remote VNet to use hub's VNet gateways. - returned: always - type: bool - sample: true - enable_internet_security: - description: - - Enable internet security and default is enabled. - type: bool - returned: always - sample: true -''' - -from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBase -try: - from azure.core.exceptions import ResourceNotFoundError -except ImportError: - # This is handled in azure_rm_common - pass - - -class AzureRMVirtualHubConnectionInfo(AzureRMModuleBase): - def __init__(self): - self.module_arg_spec = dict( - resource_group=dict( - type='str', - required=True - ), - name=dict( - type='str' - ), - virtual_hub_name=dict( - type='str', - required=True - ) - ) - - self.resource_group = None - self.name = None - self.virtual_hub_name = None - - self.results = dict(changed=False) - self.state = None - self.status_code = [200] - - super(AzureRMVirtualHubConnectionInfo, 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.name is not None: - self.results['virtual_hub_connection'] = self.format_item(self.get()) - else: - self.results['virtual_hub_connection'] = self.format_item(self.list()) - return self.results - - def get(self): - response = None - - try: - response = self.network_client.hub_virtual_network_connections.get(resource_group_name=self.resource_group, - virtual_hub_name=self.virtual_hub_name, - connection_name=self.name) - except ResourceNotFoundError: - self.log('Could not get info for @(Model.ModuleOperationNameUpper).') - - return response - - def list(self): - response = None - - try: - response = self.network_client.hub_virtual_network_connections.list(resource_group_name=self.resource_group, - virtual_hub_name=self.virtual_hub_name) - except Exception: - 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(): - AzureRMVirtualHubConnectionInfo() - - -if __name__ == '__main__': - main() +#!/usr/bin/python +# +# Copyright (c) 2020 XiuxiSun, (@Fred-sun) +# +# 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_virtualhubconnection_info +version_added: '1.14.0' +short_description: Get VirtualHub info +description: + - Get info of VirtualHub. +options: + resource_group: + description: + - The resource group name of the VirtualHub. + type: str + required: True + virtual_hub_name: + description: + - The resource name of the VirtualHub. + type: str + required: True + name: + description: + - The name of the VirtualHub connection. + type: str +extends_documentation_fragment: + - azure.azcollection.azure +author: + - Fred-Sun (@Fred-Sun) + - Xu Zhang (@xuzhang3) + +''' + +EXAMPLES = ''' + - name: Get virtual hub connection info by name + azure_rm_virtualhubconnection_info: + resource_group: myResourceGroup + virtual_hub_name: virtualHub + name: vhubname + + - name: Get virtual hub connection info by resource group + azure_rm_virtualhubconnection_info: + resource_group: myResourceGroup + virtual_hub_name: virtualHub +''' + +RETURN = ''' +virtual_hub_connection: + description: + - A list of dict results for the virtual hub connection info. + returned: always + type: complex + contains: + id: + description: + - Resource ID. + returned: always + type: str + sample: "/subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualHubs/vhub/hubVirtualNetworkConnections/MyConnection + name: + description: + - Resource name. + returned: always + type: str + sample: MyConnection + etag: + description: + - A unique read-only string that changes whenever the resource is updated. + returned: always + type: str + sample: 31102041-49e7-4cac-8573-aac1e1a16793 + remote_virtual_network: + description: + - Name of ID of the remote VNet to connect to. + returned: always + type: complex + contains: + id: + description: + - The ID of the remote VNet to connect to. + returned: always + type: str + sample: /subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualNetworks/testvnet + routing_configuration: + description: + - The routing configuration information + returned: always + type: complex + contains: + associated_route_table: + description: + - The resource ID of route table associated with this routing configuration. + type: complex + returned: always + contains: + id: + description: + - The ID of the routetable. + type: str + returned: always + sample: /subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualHubs/testhub/hubRouteTables/rt_name + propagated_route_tables: + description: + - Space-separated list of resource id of propagated route tables. + type: complex + returned: always + contains: + ids: + description: + - The list resource ID of propagated route tables. + type: list + returned: always + sample: [{id: '/subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualHubs/testhub/hubRouteTables/rt_name'}] + labels: + description: + - Space-separated list of labels for propagated route tables. + type: list + returned: always + sample: ['labels1', 'labels2'] + vnet_routes: + description: + - The name of the Static Route that is unique within a Vnet Route. + returned: always + type: complex + contains: + static_routes: + description: + - The name of the Static Route. + type: list + returned: always + suboptions: + address_prefixes: + description: + - + type: list + returned: always + sample: ["10.1.0.0/16", "10.2.0.0/16"] + name: + description: + - The name of static router. + type: str + returned: always + sample: route1 + next_hop_ip_address: + description: + - The next hop ip address. + type: str + returned: always + sample: 10.0.0.65 + provisioning_state: + description: + - The provisioning state of the virtual hub connection resource. + returned: always + type: str + sample: Succeeded + allow_hub_to_remote_vnet_transit: + description: + - Enable hub to remote VNet transit. + returned: always + type: bool + sample: true + allow_remote_vnet_to_use_hub_vnet_gateways: + description: + - Allow remote VNet to use hub's VNet gateways. + returned: always + type: bool + sample: true + enable_internet_security: + description: + - Enable internet security and default is enabled. + type: bool + returned: always + sample: true +''' + +from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBase +try: + from azure.core.exceptions import ResourceNotFoundError +except ImportError: + # This is handled in azure_rm_common + pass + + +class AzureRMVirtualHubConnectionInfo(AzureRMModuleBase): + def __init__(self): + self.module_arg_spec = dict( + resource_group=dict( + type='str', + required=True + ), + name=dict( + type='str' + ), + virtual_hub_name=dict( + type='str', + required=True + ) + ) + + self.resource_group = None + self.name = None + self.virtual_hub_name = None + + self.results = dict(changed=False) + self.state = None + self.status_code = [200] + + super(AzureRMVirtualHubConnectionInfo, 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.name is not None: + self.results['virtual_hub_connection'] = self.format_item(self.get()) + else: + self.results['virtual_hub_connection'] = self.format_item(self.list()) + return self.results + + def get(self): + response = None + + try: + response = self.network_client.hub_virtual_network_connections.get(resource_group_name=self.resource_group, + virtual_hub_name=self.virtual_hub_name, + connection_name=self.name) + except ResourceNotFoundError: + self.log('Could not get info for @(Model.ModuleOperationNameUpper).') + + return response + + def list(self): + response = None + + try: + response = self.network_client.hub_virtual_network_connections.list(resource_group_name=self.resource_group, + virtual_hub_name=self.virtual_hub_name) + except Exception: + 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(): + AzureRMVirtualHubConnectionInfo() + + +if __name__ == '__main__': + main() From 67ca191c7adda7028a452ad1d075ffaa44b6095a Mon Sep 17 00:00:00 2001 From: Fred-sun Date: Wed, 10 Aug 2022 14:41:35 +0800 Subject: [PATCH 03/13] add test case --- pr-pipelines.yml | 1 + .../azure_rm_virtualhubconnection/aliases | 3 + .../meta/main.yml | 2 + .../tasks/main.yml | 134 ++++++++++++++++++ 4 files changed, 140 insertions(+) create mode 100644 tests/integration/targets/azure_rm_virtualhubconnection/aliases create mode 100644 tests/integration/targets/azure_rm_virtualhubconnection/meta/main.yml create mode 100644 tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml diff --git a/pr-pipelines.yml b/pr-pipelines.yml index a5d26e0ce..d00022cbd 100644 --- a/pr-pipelines.yml +++ b/pr-pipelines.yml @@ -116,6 +116,7 @@ parameters: - "azure_rm_virtualwan" - "azure_rm_vpnsite" - "azure_rm_virtualhub" + - "azure_rm_virtualhubconnection" - "azure_rm_backupazurevm" - "azure_rm_recoveryservicesvault" - "azure_rm_vmbackuppolicy" diff --git a/tests/integration/targets/azure_rm_virtualhubconnection/aliases b/tests/integration/targets/azure_rm_virtualhubconnection/aliases new file mode 100644 index 000000000..5d29c6c4d --- /dev/null +++ b/tests/integration/targets/azure_rm_virtualhubconnection/aliases @@ -0,0 +1,3 @@ +cloud/azure +shippable/azure/group10 +destructive diff --git a/tests/integration/targets/azure_rm_virtualhubconnection/meta/main.yml b/tests/integration/targets/azure_rm_virtualhubconnection/meta/main.yml new file mode 100644 index 000000000..95e1952f9 --- /dev/null +++ b/tests/integration/targets/azure_rm_virtualhubconnection/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: + - setup_azure diff --git a/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml b/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml new file mode 100644 index 000000000..44d085571 --- /dev/null +++ b/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml @@ -0,0 +1,134 @@ +- name: Set random Names + set_fact: + rpfx: "{{ resource_group | hash('md5') | truncate(15, True, '') }}" + +- name: Create virtual network + azure_rm_virtualnetwork: + name: "vnet{{ rpfx }}" + address_prefixes_cidr: + - 10.1.0.0/16 + - 172.100.0.0/16 + resource_group: "{{ resource_group }}" + register: vnet_output + +- name: Create subnet + azure_rm_subnet: + name: "sub{{ rpfx }}" + virtual_network_name: "vnet{{ rpfx }}" + resource_group: "{{ resource_group }}" + address_prefix_cidr: "10.1.0.0/24" + +- name: Create a Virtual Wan + azure_rm_virtualwan: + resource_group: "{{ resource_group }}" + name: "virtualwan{{ rpfx }}" + office365_local_breakout_category: All + disable_vpn_encryption: true + allow_branch_to_branch_traffic: true + allow_vnet_to_vnet_traffic: true + virtual_wan_type: Basic + register: vwan_output + +- name: Create a VirtualHub + azure_rm_virtualhub: + resource_group: "{{ resource_group }}" + name: "vhub{{ rpfx }}" + sku: Standard + address_prefix: 12.0.0.0/16 + virtual_wan: + id: "{{ vwan_output.state.id }}" + virtual_router_asn: 65515 + virtual_router_ips: + - 12.0.32.4 + - 12.0.32.5 + register: output + +- name: Create virtual hub connection + azure_rm_virtualhubconnection: + resource_group: "{{ resource_group }}" + vhub_name: "vhub{{ rpfx }}" + name: "connection{{ rpfx }}" + enable_internet_security: false + allow_remote_vnet_to_use_hub_vnet_gateways: false + allow_hub_to_remote_vnet_transit: true + remote_virtual_network: + id: "{{ vnet_output.state.id }}" + routing_configuration: + propagated_route_tables: + labels: + - labels1 + - labels3 + vnet_routes: + static_routes: + - name: route1 + address_prefixes: + - 10.1.0.0/16 + - 10.2.0.0/16 + - 10.6.0.0/16 + next_hop_ip_address: 10.0.0.68 + - name: route2 + address_prefixes: + - 10.4.0.0/16 + next_hop_ip_address: 10.0.0.65 + register: output + +- name: Assert the virtual hub connection is well created + assert: + that: + - output.changed + +- name: Create virtual hub connection (idempotent test) + azure_rm_virtualhubconnection: + resource_group: "{{ resource_group }}" + vhub_name: "vhub{{ rpfx }}" + name: "connection{{ rpfx }}" + enable_internet_security: false + allow_remote_vnet_to_use_hub_vnet_gateways: false + allow_hub_to_remote_vnet_transit: true + remote_virtual_network: + id: "{{ vnet_output.state.id }}" + routing_configuration: + propagated_route_tables: + labels: + - labels1 + - labels3 + vnet_routes: + static_routes: + - name: route1 + address_prefixes: + - 10.1.0.0/16 + - 10.2.0.0/16 + - 10.6.0.0/16 + next_hop_ip_address: 10.0.0.68 + - name: route2 + address_prefixes: + - 10.4.0.0/16 + next_hop_ip_address: 10.0.0.65 + register: output + +- name: Assert the virtual hub connection no changed + assert: + that: + - not output.changed + +- name: Get virtual hub connection info + azure_rm_virtualhubconnection_info: + resource_group: "{{ resource_group }}" + virtual_hub_name: "vhub{{ rpfx }}" + name: "connection{{ rpfx }}" + register: output + +- name: Assert fact returns + assert: + - output.virtual_hub_connection.allow_hub_to_remote_vnet_transit + - not output.virtual_hub_connection.allow_remote_vnet_to_use_hub_vnet_gateways + - not output.virtual_hub_connection.enable_internet_security + - output.virtual_hub_connection.routing_configuration.propagated_route_tables.labels | length == 2 + - output.virtual_hub_connection.routing_configuration.vnet_routes.static_routes | length == 2 + +- name: Delete the virtual hub connection + azure_rm_virtualhubconnection: + resource_group: "{{ resource_group }}" + vhub_name: "vhub{{ rpfx }}" + name: "connection{{ rpfx }}" + state: absent From a11b8bb0cf8704dee68445e52dfbc67027735d27 Mon Sep 17 00:00:00 2001 From: Fred-sun Date: Wed, 10 Aug 2022 16:22:26 +0800 Subject: [PATCH 04/13] change update logic --- .../modules/azure_rm_virtualhubconnection.py | 33 ++++++++++++--- .../tasks/main.yml | 42 +++++++++++++++++-- 2 files changed, 66 insertions(+), 9 deletions(-) diff --git a/plugins/modules/azure_rm_virtualhubconnection.py b/plugins/modules/azure_rm_virtualhubconnection.py index 574bb3fe2..6dcf1d8fe 100644 --- a/plugins/modules/azure_rm_virtualhubconnection.py +++ b/plugins/modules/azure_rm_virtualhubconnection.py @@ -411,12 +411,33 @@ def exec_module(self, **kwargs): 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.body.get('enable_internet_security') is not None: + if bool(self.body['enable_internet_security']) != bool(old_response['enable_internet_security']): + self.to_do = Actions.Update + else: + self.body['enable_internet_security'] = old_response['enable_internet_security'] + if self.body.get('allow_remote_vnet_to_use_hub_vnet_gateways') is not None: + if bool(self.body['allow_remote_vnet_to_use_hub_vnet_gateways']) != bool(old_response['allow_remote_vnet_to_use_hub_vnet_gateways']): + self.to_do = Actions.Update + else: + self.body['allow_remote_vnet_to_use_hub_vnet_gateways'] = old_response['allow_remote_vnet_to_use_hub_vnet_gateways'] + if self.body.get('allow_hub_to_remote_vnet_transit') is not None: + if bool(self.body['allow_hub_to_remote_vnet_transit']) != bool(old_response['allow_hub_to_remote_vnet_transit']): + self.to_do = Actions.Update + else: + self.body['allow_hub_to_remote_vnet_transit'] = old_response['allow_hub_to_remote_vnet_transit'] + + if self.body.get('routing_configuration') is not None: + 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['routing_configuration'], old_response['routing_configuration'], '', self.results): + self.to_do = Actions.Update + else: + self.body['routing_configuration'] = old_response['routing_configuration'] + else: + self.body['routing_configuration'] = old_response['routing_configuration'] if (self.to_do == Actions.Create) or (self.to_do == Actions.Update): self.results['changed'] = True diff --git a/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml b/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml index 44d085571..8c814e3cd 100644 --- a/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml +++ b/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml @@ -49,7 +49,7 @@ vhub_name: "vhub{{ rpfx }}" name: "connection{{ rpfx }}" enable_internet_security: false - allow_remote_vnet_to_use_hub_vnet_gateways: false + allow_remote_vnet_to_use_hub_vnet_gateways: true allow_hub_to_remote_vnet_transit: true remote_virtual_network: id: "{{ vnet_output.state.id }}" @@ -83,7 +83,7 @@ vhub_name: "vhub{{ rpfx }}" name: "connection{{ rpfx }}" enable_internet_security: false - allow_remote_vnet_to_use_hub_vnet_gateways: false + allow_remote_vnet_to_use_hub_vnet_gateways: true allow_hub_to_remote_vnet_transit: true remote_virtual_network: id: "{{ vnet_output.state.id }}" @@ -111,6 +111,42 @@ that: - not output.changed +- name: Update virtual hub connection + azure_rm_virtualhubconnection: + resource_group: "{{ resource_group }}" + vhub_name: "vhub{{ rpfx }}" + name: "connection{{ rpfx }}" + enable_internet_security: true + allow_remote_vnet_to_use_hub_vnet_gateways: true + allow_hub_to_remote_vnet_transit: true + remote_virtual_network: + id: "{{ vnet_output.state.id }}" + routing_configuration: + propagated_route_tables: + labels: + - labels1 + - labels2 + - labels3 + vnet_routes: + static_routes: + - name: route1 + address_prefixes: + - 10.1.0.0/16 + - 10.2.0.0/16 + - 10.6.0.0/16 + - 10.6.0.0/16 + next_hop_ip_address: 10.0.0.68 + - name: route2 + address_prefixes: + - 10.4.0.0/16 + next_hop_ip_address: 10.0.0.65 + register: output + +- name: Assert the virtual hub connection no changed + assert: + that: + - not output.changed + - name: Get virtual hub connection info azure_rm_virtualhubconnection_info: resource_group: "{{ resource_group }}" @@ -123,7 +159,7 @@ - output.virtual_hub_connection.allow_hub_to_remote_vnet_transit - not output.virtual_hub_connection.allow_remote_vnet_to_use_hub_vnet_gateways - not output.virtual_hub_connection.enable_internet_security - - output.virtual_hub_connection.routing_configuration.propagated_route_tables.labels | length == 2 + - output.virtual_hub_connection.routing_configuration.propagated_route_tables.labels | length == 3 - output.virtual_hub_connection.routing_configuration.vnet_routes.static_routes | length == 2 - name: Delete the virtual hub connection From b19b5de91073bc0b385f609e9535e5a1fcc9052f Mon Sep 17 00:00:00 2001 From: Fred-sun Date: Wed, 10 Aug 2022 16:36:00 +0800 Subject: [PATCH 05/13] fix small --- .../modules/azure_rm_virtualhubconnection.py | 6 +- .../tasks/main.yml | 340 +++++++++--------- 2 files changed, 173 insertions(+), 173 deletions(-) diff --git a/plugins/modules/azure_rm_virtualhubconnection.py b/plugins/modules/azure_rm_virtualhubconnection.py index 6dcf1d8fe..e116540d9 100644 --- a/plugins/modules/azure_rm_virtualhubconnection.py +++ b/plugins/modules/azure_rm_virtualhubconnection.py @@ -387,8 +387,8 @@ def __init__(self): self.to_do = Actions.NoAction super(AzureRMVirtualHubConnection, self).__init__(derived_arg_spec=self.module_arg_spec, - supports_check_mode=True, - supports_tags=False) + supports_check_mode=True, + supports_tags=False) def exec_module(self, **kwargs): for key in list(self.module_arg_spec.keys()): @@ -401,7 +401,7 @@ def exec_module(self, **kwargs): old_response = None response = None - +et by' old_response = self.get_resource() if not old_response: diff --git a/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml b/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml index 8c814e3cd..f4074c5b8 100644 --- a/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml +++ b/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml @@ -1,170 +1,170 @@ -- name: Set random Names - set_fact: - rpfx: "{{ resource_group | hash('md5') | truncate(15, True, '') }}" - -- name: Create virtual network - azure_rm_virtualnetwork: - name: "vnet{{ rpfx }}" - address_prefixes_cidr: - - 10.1.0.0/16 - - 172.100.0.0/16 - resource_group: "{{ resource_group }}" - register: vnet_output - -- name: Create subnet - azure_rm_subnet: - name: "sub{{ rpfx }}" - virtual_network_name: "vnet{{ rpfx }}" - resource_group: "{{ resource_group }}" - address_prefix_cidr: "10.1.0.0/24" - -- name: Create a Virtual Wan - azure_rm_virtualwan: - resource_group: "{{ resource_group }}" - name: "virtualwan{{ rpfx }}" - office365_local_breakout_category: All - disable_vpn_encryption: true - allow_branch_to_branch_traffic: true - allow_vnet_to_vnet_traffic: true - virtual_wan_type: Basic - register: vwan_output - -- name: Create a VirtualHub - azure_rm_virtualhub: - resource_group: "{{ resource_group }}" - name: "vhub{{ rpfx }}" - sku: Standard - address_prefix: 12.0.0.0/16 - virtual_wan: - id: "{{ vwan_output.state.id }}" - virtual_router_asn: 65515 - virtual_router_ips: - - 12.0.32.4 - - 12.0.32.5 - register: output - -- name: Create virtual hub connection - azure_rm_virtualhubconnection: - resource_group: "{{ resource_group }}" - vhub_name: "vhub{{ rpfx }}" - name: "connection{{ rpfx }}" - enable_internet_security: false - allow_remote_vnet_to_use_hub_vnet_gateways: true - allow_hub_to_remote_vnet_transit: true - remote_virtual_network: - id: "{{ vnet_output.state.id }}" - routing_configuration: - propagated_route_tables: - labels: - - labels1 - - labels3 - vnet_routes: - static_routes: - - name: route1 - address_prefixes: - - 10.1.0.0/16 - - 10.2.0.0/16 - - 10.6.0.0/16 - next_hop_ip_address: 10.0.0.68 - - name: route2 - address_prefixes: - - 10.4.0.0/16 - next_hop_ip_address: 10.0.0.65 - register: output - -- name: Assert the virtual hub connection is well created - assert: - that: - - output.changed - -- name: Create virtual hub connection (idempotent test) - azure_rm_virtualhubconnection: - resource_group: "{{ resource_group }}" - vhub_name: "vhub{{ rpfx }}" - name: "connection{{ rpfx }}" - enable_internet_security: false - allow_remote_vnet_to_use_hub_vnet_gateways: true - allow_hub_to_remote_vnet_transit: true - remote_virtual_network: - id: "{{ vnet_output.state.id }}" - routing_configuration: - propagated_route_tables: - labels: - - labels1 - - labels3 - vnet_routes: - static_routes: - - name: route1 - address_prefixes: - - 10.1.0.0/16 - - 10.2.0.0/16 - - 10.6.0.0/16 - next_hop_ip_address: 10.0.0.68 - - name: route2 - address_prefixes: - - 10.4.0.0/16 - next_hop_ip_address: 10.0.0.65 - register: output - -- name: Assert the virtual hub connection no changed - assert: - that: - - not output.changed - -- name: Update virtual hub connection - azure_rm_virtualhubconnection: - resource_group: "{{ resource_group }}" - vhub_name: "vhub{{ rpfx }}" - name: "connection{{ rpfx }}" - enable_internet_security: true - allow_remote_vnet_to_use_hub_vnet_gateways: true - allow_hub_to_remote_vnet_transit: true - remote_virtual_network: - id: "{{ vnet_output.state.id }}" - routing_configuration: - propagated_route_tables: - labels: - - labels1 - - labels2 - - labels3 - vnet_routes: - static_routes: - - name: route1 - address_prefixes: - - 10.1.0.0/16 - - 10.2.0.0/16 - - 10.6.0.0/16 - - 10.6.0.0/16 - next_hop_ip_address: 10.0.0.68 - - name: route2 - address_prefixes: - - 10.4.0.0/16 - next_hop_ip_address: 10.0.0.65 - register: output - -- name: Assert the virtual hub connection no changed - assert: - that: - - not output.changed - -- name: Get virtual hub connection info - azure_rm_virtualhubconnection_info: - resource_group: "{{ resource_group }}" - virtual_hub_name: "vhub{{ rpfx }}" - name: "connection{{ rpfx }}" - register: output - -- name: Assert fact returns - assert: - - output.virtual_hub_connection.allow_hub_to_remote_vnet_transit - - not output.virtual_hub_connection.allow_remote_vnet_to_use_hub_vnet_gateways - - not output.virtual_hub_connection.enable_internet_security - - output.virtual_hub_connection.routing_configuration.propagated_route_tables.labels | length == 3 - - output.virtual_hub_connection.routing_configuration.vnet_routes.static_routes | length == 2 - -- name: Delete the virtual hub connection - azure_rm_virtualhubconnection: - resource_group: "{{ resource_group }}" - vhub_name: "vhub{{ rpfx }}" - name: "connection{{ rpfx }}" - state: absent + - name: Set random Names + set_fact: + rpfx: "{{ resource_group | hash('md5') | truncate(7, True, '') }}{{ 1000 | random }}" + + - name: Create virtual network + azure_rm_virtualnetwork: + name: "vnet{{ rpfx }}" + address_prefixes_cidr: + - 10.1.0.0/16 + - 172.100.0.0/16 + resource_group: "{{ resource_group }}" + register: vnet_output + + - name: Create subnet + azure_rm_subnet: + name: "sub{{ rpfx }}" + virtual_network_name: "vnet{{ rpfx }}" + resource_group: "{{ resource_group }}" + address_prefix_cidr: "10.1.0.0/24" + + - name: Create a Virtual Wan + azure_rm_virtualwan: + resource_group: "{{ resource_group }}" + name: "virtualwan{{ rpfx }}" + office365_local_breakout_category: All + disable_vpn_encryption: true + allow_branch_to_branch_traffic: true + allow_vnet_to_vnet_traffic: true + virtual_wan_type: Basic + register: vwan_output + + - name: Create a VirtualHub + azure_rm_virtualhub: + resource_group: "{{ resource_group }}" + name: "vhub{{ rpfx }}" + sku: Standard + address_prefix: 12.0.0.0/16 + virtual_wan: + id: "{{ vwan_output.state.id }}" + virtual_router_asn: 65515 + virtual_router_ips: + - 12.0.32.4 + - 12.0.32.5 + register: output + + - name: Create virtual hub connection + azure_rm_virtualhubconnection: + resource_group: "{{ resource_group }}" + vhub_name: "vhub{{ rpfx }}" + name: "connection{{ rpfx }}" + enable_internet_security: false + allow_remote_vnet_to_use_hub_vnet_gateways: true + allow_hub_to_remote_vnet_transit: true + remote_virtual_network: + id: "{{ vnet_output.state.id }}" + routing_configuration: + propagated_route_tables: + labels: + - labels1 + - labels3 + vnet_routes: + static_routes: + - name: route1 + address_prefixes: + - 10.1.0.0/16 + - 10.2.0.0/16 + - 10.6.0.0/16 + next_hop_ip_address: 10.0.0.68 + - name: route2 + address_prefixes: + - 10.4.0.0/16 + next_hop_ip_address: 10.0.0.65 + register: output + + - name: Assert the virtual hub connection is well created + assert: + that: + - output.changed + + - name: Create virtual hub connection (idempotent test) + azure_rm_virtualhubconnection: + resource_group: "{{ resource_group }}" + vhub_name: "vhub{{ rpfx }}" + name: "connection{{ rpfx }}" + enable_internet_security: false + allow_remote_vnet_to_use_hub_vnet_gateways: true + allow_hub_to_remote_vnet_transit: true + remote_virtual_network: + id: "{{ vnet_output.state.id }}" + routing_configuration: + propagated_route_tables: + labels: + - labels1 + - labels3 + vnet_routes: + static_routes: + - name: route1 + address_prefixes: + - 10.1.0.0/16 + - 10.2.0.0/16 + - 10.6.0.0/16 + next_hop_ip_address: 10.0.0.68 + - name: route2 + address_prefixes: + - 10.4.0.0/16 + next_hop_ip_address: 10.0.0.65 + register: output + + - name: Assert the virtual hub connection no changed + assert: + that: + - not output.changed + + - name: Update virtual hub connection + azure_rm_virtualhubconnection: + resource_group: "{{ resource_group }}" + vhub_name: "vhub{{ rpfx }}" + name: "connection{{ rpfx }}" + enable_internet_security: true + allow_remote_vnet_to_use_hub_vnet_gateways: true + allow_hub_to_remote_vnet_transit: true + remote_virtual_network: + id: "{{ vnet_output.state.id }}" + routing_configuration: + propagated_route_tables: + labels: + - labels1 + - labels2 + - labels3 + vnet_routes: + static_routes: + - name: route1 + address_prefixes: + - 10.1.0.0/16 + - 10.2.0.0/16 + - 10.6.0.0/16 + - 10.6.0.0/16 + next_hop_ip_address: 10.0.0.68 + - name: route2 + address_prefixes: + - 10.4.0.0/16 + next_hop_ip_address: 10.0.0.65 + register: output + + - name: Assert the virtual hub connection no changed + assert: + that: + - not output.changed + + - name: Get virtual hub connection info + azure_rm_virtualhubconnection_info: + resource_group: "{{ resource_group }}" + virtual_hub_name: "vhub{{ rpfx }}" + name: "connection{{ rpfx }}" + register: output + + - name: Assert fact returns + assert: + - output.virtual_hub_connection.allow_hub_to_remote_vnet_transit + - not output.virtual_hub_connection.allow_remote_vnet_to_use_hub_vnet_gateways + - not output.virtual_hub_connection.enable_internet_security + - output.virtual_hub_connection.routing_configuration.propagated_route_tables.labels | length == 3 + - output.virtual_hub_connection.routing_configuration.vnet_routes.static_routes | length == 2 + + - name: Delete the virtual hub connection + azure_rm_virtualhubconnection: + resource_group: "{{ resource_group }}" + vhub_name: "vhub{{ rpfx }}" + name: "connection{{ rpfx }}" + state: absent From 21d1cad2d84f855c1b3073c30171c9f84d9d79d1 Mon Sep 17 00:00:00 2001 From: Fred-sun Date: Wed, 10 Aug 2022 17:40:11 +0800 Subject: [PATCH 06/13] add new change --- plugins/modules/azure_rm_virtualhubconnection.py | 2 +- .../targets/azure_rm_virtualhubconnection/tasks/main.yml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/modules/azure_rm_virtualhubconnection.py b/plugins/modules/azure_rm_virtualhubconnection.py index e116540d9..5bac6d4e8 100644 --- a/plugins/modules/azure_rm_virtualhubconnection.py +++ b/plugins/modules/azure_rm_virtualhubconnection.py @@ -401,7 +401,7 @@ def exec_module(self, **kwargs): old_response = None response = None -et by' + old_response = self.get_resource() if not old_response: diff --git a/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml b/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml index f4074c5b8..3c37b761a 100644 --- a/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml +++ b/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml @@ -1,7 +1,7 @@ - - name: Set random Names + - name: Set random parameter set_fact: rpfx: "{{ resource_group | hash('md5') | truncate(7, True, '') }}{{ 1000 | random }}" - + - name: Create virtual network azure_rm_virtualnetwork: name: "vnet{{ rpfx }}" From 21e0bc9c356d1a77bec31621b8bddb564ba31171 Mon Sep 17 00:00:00 2001 From: Fred-sun Date: Wed, 10 Aug 2022 18:20:20 +0800 Subject: [PATCH 07/13] small change 02 --- plugins/modules/azure_rm_virtualhubconnection.py | 6 +++--- plugins/modules/azure_rm_virtualhubconnection_info.py | 6 +++--- .../targets/azure_rm_virtualhubconnection/tasks/main.yml | 5 ++--- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/plugins/modules/azure_rm_virtualhubconnection.py b/plugins/modules/azure_rm_virtualhubconnection.py index 5bac6d4e8..11c61f1b4 100644 --- a/plugins/modules/azure_rm_virtualhubconnection.py +++ b/plugins/modules/azure_rm_virtualhubconnection.py @@ -167,7 +167,7 @@ - Resource ID. returned: always type: str - sample: "/subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualHubs/vhub/hubVirtualNetworkConnections/MyConnection + sample: /subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualHubs/vhub/hubVirtualNetworkConnections/MyConnection name: description: - Resource name. @@ -239,10 +239,10 @@ - The name of the Static Route. type: list returned: always - suboptions: + contains: address_prefixes: description: - - + - Space-separated list of all address prefixes. type: list returned: always sample: ["10.1.0.0/16", "10.2.0.0/16"] diff --git a/plugins/modules/azure_rm_virtualhubconnection_info.py b/plugins/modules/azure_rm_virtualhubconnection_info.py index 693c9c280..21761f712 100644 --- a/plugins/modules/azure_rm_virtualhubconnection_info.py +++ b/plugins/modules/azure_rm_virtualhubconnection_info.py @@ -63,7 +63,7 @@ - Resource ID. returned: always type: str - sample: "/subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualHubs/vhub/hubVirtualNetworkConnections/MyConnection + sample: "/subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualHubs/vhub/hubVirtualNetworkConnections/MyConnection" name: description: - Resource name. @@ -135,10 +135,10 @@ - The name of the Static Route. type: list returned: always - suboptions: + contains: address_prefixes: description: - - + - Space-separated list of all address prefixes. type: list returned: always sample: ["10.1.0.0/16", "10.2.0.0/16"] diff --git a/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml b/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml index 3c37b761a..963d7edea 100644 --- a/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml +++ b/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml @@ -1,6 +1,5 @@ - - name: Set random parameter - set_fact: - rpfx: "{{ resource_group | hash('md5') | truncate(7, True, '') }}{{ 1000 | random }}" + - set_fact: + rpfx: "{{ resource_group | hash('md5') | truncate(8, True, '') }}" - name: Create virtual network azure_rm_virtualnetwork: From e103eaafd53a97c3c877df4e7b7021824047ffef Mon Sep 17 00:00:00 2001 From: Fred-sun Date: Wed, 10 Aug 2022 19:26:15 +0800 Subject: [PATCH 08/13] new change --- plugins/modules/azure_rm_virtualhubconnection.py | 6 ++++-- .../azure_rm_virtualhubconnection/tasks/main.yml | 11 ++++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/plugins/modules/azure_rm_virtualhubconnection.py b/plugins/modules/azure_rm_virtualhubconnection.py index 11c61f1b4..f675bae5f 100644 --- a/plugins/modules/azure_rm_virtualhubconnection.py +++ b/plugins/modules/azure_rm_virtualhubconnection.py @@ -44,13 +44,15 @@ - VirtualHub to RemoteVnet transit to enabled or not. type: bool remote_virtual_network: + description: + - ID of the remote VNet to connect to. type: dict suboptions: id: description: - The remote virtual network ID. type: str - route_configuration: + routing_configuration: description: - The Routing Configuration indicating the associated and propagated route tables on this connection. type: dict @@ -85,7 +87,7 @@ - List of all Static Routes. type: list elements: dict - suoptions: + suboptions: name: description: - The name of the StaticRoute that is unique within a VnetRoute. diff --git a/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml b/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml index 963d7edea..a0efc6f93 100644 --- a/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml +++ b/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml @@ -155,11 +155,12 @@ - name: Assert fact returns assert: - - output.virtual_hub_connection.allow_hub_to_remote_vnet_transit - - not output.virtual_hub_connection.allow_remote_vnet_to_use_hub_vnet_gateways - - not output.virtual_hub_connection.enable_internet_security - - output.virtual_hub_connection.routing_configuration.propagated_route_tables.labels | length == 3 - - output.virtual_hub_connection.routing_configuration.vnet_routes.static_routes | length == 2 + that: + - output.virtual_hub_connection.allow_hub_to_remote_vnet_transit + - not output.virtual_hub_connection.allow_remote_vnet_to_use_hub_vnet_gateways + - not output.virtual_hub_connection.enable_internet_security + - output.virtual_hub_connection.routing_configuration.propagated_route_tables.labels | length == 3 + - output.virtual_hub_connection.routing_configuration.vnet_routes.static_routes | length == 2 - name: Delete the virtual hub connection azure_rm_virtualhubconnection: From 200c7d4aec150c1de1c0dbabf299508781ba284d Mon Sep 17 00:00:00 2001 From: Fred-sun Date: Wed, 10 Aug 2022 20:01:03 +0800 Subject: [PATCH 09/13] add new change --- .../targets/azure_rm_virtualhubconnection/tasks/main.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml b/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml index a0efc6f93..d0160cd03 100644 --- a/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml +++ b/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml @@ -47,7 +47,7 @@ resource_group: "{{ resource_group }}" vhub_name: "vhub{{ rpfx }}" name: "connection{{ rpfx }}" - enable_internet_security: false + enable_internet_security: true allow_remote_vnet_to_use_hub_vnet_gateways: true allow_hub_to_remote_vnet_transit: true remote_virtual_network: @@ -81,7 +81,7 @@ resource_group: "{{ resource_group }}" vhub_name: "vhub{{ rpfx }}" name: "connection{{ rpfx }}" - enable_internet_security: false + enable_internet_security: true allow_remote_vnet_to_use_hub_vnet_gateways: true allow_hub_to_remote_vnet_transit: true remote_virtual_network: @@ -115,7 +115,7 @@ resource_group: "{{ resource_group }}" vhub_name: "vhub{{ rpfx }}" name: "connection{{ rpfx }}" - enable_internet_security: true + enable_internet_security: false allow_remote_vnet_to_use_hub_vnet_gateways: true allow_hub_to_remote_vnet_transit: true remote_virtual_network: @@ -156,7 +156,7 @@ - name: Assert fact returns assert: that: - - output.virtual_hub_connection.allow_hub_to_remote_vnet_transit + - not output.virtual_hub_connection.allow_hub_to_remote_vnet_transit - not output.virtual_hub_connection.allow_remote_vnet_to_use_hub_vnet_gateways - not output.virtual_hub_connection.enable_internet_security - output.virtual_hub_connection.routing_configuration.propagated_route_tables.labels | length == 3 From 58a6fc7219328774f824c52b6608eeda5bb749c5 Mon Sep 17 00:00:00 2001 From: Fred-sun Date: Wed, 10 Aug 2022 23:30:03 +0800 Subject: [PATCH 10/13] add new change --- .../targets/azure_rm_virtualhubconnection/tasks/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml b/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml index d0160cd03..d3f6d6682 100644 --- a/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml +++ b/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml @@ -12,7 +12,7 @@ - name: Create subnet azure_rm_subnet: - name: "sub{{ rpfx }}" + name: testsubnet virtual_network_name: "vnet{{ rpfx }}" resource_group: "{{ resource_group }}" address_prefix_cidr: "10.1.0.0/24" From d297232800377ea75bbf0826a4a83de01da1adc6 Mon Sep 17 00:00:00 2001 From: Fred-sun Date: Thu, 11 Aug 2022 00:13:12 +0800 Subject: [PATCH 11/13] add new change --- .../tasks/main.yml | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml b/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml index d3f6d6682..4d02e3a52 100644 --- a/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml +++ b/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml @@ -48,7 +48,7 @@ vhub_name: "vhub{{ rpfx }}" name: "connection{{ rpfx }}" enable_internet_security: true - allow_remote_vnet_to_use_hub_vnet_gateways: true + allow_remote_vnet_to_use_hub_vnet_gateways: false allow_hub_to_remote_vnet_transit: true remote_virtual_network: id: "{{ vnet_output.state.id }}" @@ -82,7 +82,7 @@ vhub_name: "vhub{{ rpfx }}" name: "connection{{ rpfx }}" enable_internet_security: true - allow_remote_vnet_to_use_hub_vnet_gateways: true + allow_remote_vnet_to_use_hub_vnet_gateways: false allow_hub_to_remote_vnet_transit: true remote_virtual_network: id: "{{ vnet_output.state.id }}" @@ -116,7 +116,7 @@ vhub_name: "vhub{{ rpfx }}" name: "connection{{ rpfx }}" enable_internet_security: false - allow_remote_vnet_to_use_hub_vnet_gateways: true + allow_remote_vnet_to_use_hub_vnet_gateways: false allow_hub_to_remote_vnet_transit: true remote_virtual_network: id: "{{ vnet_output.state.id }}" @@ -133,7 +133,7 @@ - 10.1.0.0/16 - 10.2.0.0/16 - 10.6.0.0/16 - - 10.6.0.0/16 + - 10.7.0.0/16 next_hop_ip_address: 10.0.0.68 - name: route2 address_prefixes: @@ -144,7 +144,7 @@ - name: Assert the virtual hub connection no changed assert: that: - - not output.changed + - output.changed - name: Get virtual hub connection info azure_rm_virtualhubconnection_info: @@ -156,11 +156,11 @@ - name: Assert fact returns assert: that: - - not output.virtual_hub_connection.allow_hub_to_remote_vnet_transit - - not output.virtual_hub_connection.allow_remote_vnet_to_use_hub_vnet_gateways - - not output.virtual_hub_connection.enable_internet_security - - output.virtual_hub_connection.routing_configuration.propagated_route_tables.labels | length == 3 - - output.virtual_hub_connection.routing_configuration.vnet_routes.static_routes | length == 2 + - output.virtual_hub_connection[0].allow_hub_to_remote_vnet_transit + - not output.virtual_hub_connection[0].allow_remote_vnet_to_use_hub_vnet_gateways + - not output.virtual_hub_connection[0].enable_internet_security + - output.virtual_hub_connection[0].routing_configuration.propagated_route_tables.labels | length == 3 + - output.virtual_hub_connection[0].routing_configuration.vnet_routes.static_routes | length == 2 - name: Delete the virtual hub connection azure_rm_virtualhubconnection: From 4e1f6ec99682a262af1a7d1680c9c10958a2707d Mon Sep 17 00:00:00 2001 From: Fred-sun Date: Thu, 11 Aug 2022 08:23:50 +0800 Subject: [PATCH 12/13] Update test case --- .../targets/azure_rm_virtualhubconnection/tasks/main.yml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml b/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml index 4d02e3a52..562ecbacc 100644 --- a/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml +++ b/tests/integration/targets/azure_rm_virtualhubconnection/tasks/main.yml @@ -10,13 +10,6 @@ resource_group: "{{ resource_group }}" register: vnet_output - - name: Create subnet - azure_rm_subnet: - name: testsubnet - virtual_network_name: "vnet{{ rpfx }}" - resource_group: "{{ resource_group }}" - address_prefix_cidr: "10.1.0.0/24" - - name: Create a Virtual Wan azure_rm_virtualwan: resource_group: "{{ resource_group }}" From d8e40754e30c0e746d5e6674f89c7462bc5a2b40 Mon Sep 17 00:00:00 2001 From: Fred-sun Date: Fri, 19 Aug 2022 15:18:45 +0800 Subject: [PATCH 13/13] copyright change --- plugins/modules/azure_rm_virtualhubconnection.py | 3 ++- plugins/modules/azure_rm_virtualhubconnection_info.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/plugins/modules/azure_rm_virtualhubconnection.py b/plugins/modules/azure_rm_virtualhubconnection.py index f675bae5f..5208f665e 100644 --- a/plugins/modules/azure_rm_virtualhubconnection.py +++ b/plugins/modules/azure_rm_virtualhubconnection.py @@ -1,6 +1,7 @@ #!/usr/bin/python # -# Copyright (c) 2020 XiuxiSun, (@Fred-sun) +# Copyright (c) 2022 xuzhang3 (@xuzhang3) +# XiuxiSun, (@Fred-sun) # # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) diff --git a/plugins/modules/azure_rm_virtualhubconnection_info.py b/plugins/modules/azure_rm_virtualhubconnection_info.py index 21761f712..d2d01dc25 100644 --- a/plugins/modules/azure_rm_virtualhubconnection_info.py +++ b/plugins/modules/azure_rm_virtualhubconnection_info.py @@ -1,6 +1,7 @@ #!/usr/bin/python # -# Copyright (c) 2020 XiuxiSun, (@Fred-sun) +# Copyright (c) 2022 xuzhang3 (@xuzhang3) +# XiuxiSun, (@Fred-sun) # # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)