summaryrefslogtreecommitdiff
path: root/lib/ansible/modules/cloud/azure/azure_rm_virtualnetworkpeering_info.py
blob: 4548894cda78916b27e9ed7c74759d517bbea465 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/python
#
# Copyright (c) 2019 Yunge Zhu (@yungezz)
#
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type

ANSIBLE_METADATA = {'metadata_version': '1.1',
                    'status': ['preview'],
                    'supported_by': 'community'}

DOCUMENTATION = '''
---
module: azure_rm_virtualnetworkpeering_info
version_added: "2.9"
short_description: Get facts of Azure Virtual Network Peering
description:
    - Get facts of Azure Virtual Network Peering.

options:
    resource_group:
        description:
            - Name of a resource group where the vnet exists.
        required: True
    virtual_network:
        description:
            - Name or resource ID of a virtual network.
        required: True
    name:
        description:
            - Name of the virtual network peering.

extends_documentation_fragment:
    - azure

author:
    - Yunge Zhu (@yungezz)
'''

EXAMPLES = '''
    - name: Get virtual network peering by name
      azure_rm_virtualnetworkpeering_info:
        resource_group: myResourceGroup
        virtual_network: myVnet1
        name: myVnetPeer

    - name: List virtual network peering of virtual network
      azure_rm_virtualnetworkpeering:
        resource_group: myResourceGroup
        virtual_network: myVnet1
'''

RETURN = '''
vnetpeerings:
    description:
        - A list of Virtual Network Peering facts.
    returned: always
    type: complex
    contains:
        id:
            description: ID of current Virtual Network peering.
            returned: always
            type: str
            sample:
                "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myVnet/virtualNetworkPeerings/peer1"
        name:
            description:
                - Name of Virtual Network peering.
            returned: always
            type: str
            sample: myPeering
        remote_virtual_network:
            description:
                - ID of remote Virtual Network to be peered to.
            returned: always
            type: str
            sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myVnet2
        remote_address_space:
            description:
                - The reference of the remote Virtual Network address space.
            type: complex
            returned: always
            contains:
                address_prefixes:
                    description:
                        - A list of address blocks reserved for this Virtual Network in CIDR notation.
                    returned: always
                    type: list
                    sample: 10.1.0.0/16
        peering_state:
            description:
                - The state of the virtual network peering.
            returned: always
            type: str
            sample: Connected
        provisioning_state:
            description:
                - The provisioning state of the resource.
            returned: always
            type: str
            sample: Succeeded
        allow_forwarded_traffic:
            description:
                - Whether forwarded traffic from the VMs in the remote Virtual Network will be allowed/disallowed.
            returned: always
            type: bool
            sample: False
        allow_gateway_transit:
            description:
                - Whether gateway links can be used in remote Virtual Networking to link to this Virtual Network.
            returned: always
            type: bool
            sample: False
        allow_virtual_network_access:
            description:
                - Whether the VMs in the linked Virtual Network space can access all the VMs in local Virtual Network space.
            returned: always
            type: bool
            sample: False
        use_remote_gateways:
            description:
                - Whether remote gateways can be used on this Virtual Network.
            returned: always
            type: bool
            sample: False
'''

try:
    from msrestazure.azure_exceptions import CloudError
    from msrest.polling import LROPoller
except ImportError:
    # This is handled in azure_rm_common
    pass

from ansible.module_utils.azure_rm_common import AzureRMModuleBase


def vnetpeering_to_dict(vnetpeering):
    '''
    Convert a virtual network peering object to a dict.
    '''
    results = dict(
        id=vnetpeering.id,
        name=vnetpeering.name,
        remote_virtual_network=vnetpeering.remote_virtual_network.id,
        remote_address_space=dict(
            address_prefixes=vnetpeering.remote_address_space.address_prefixes
        ),
        peering_state=vnetpeering.peering_state,
        provisioning_state=vnetpeering.provisioning_state,
        use_remote_gateways=vnetpeering.use_remote_gateways,
        allow_gateway_transit=vnetpeering.allow_gateway_transit,
        allow_forwarded_traffic=vnetpeering.allow_forwarded_traffic,
        allow_virtual_network_access=vnetpeering.allow_virtual_network_access
    )
    return results


class AzureRMVirtualNetworkPeeringInfo(AzureRMModuleBase):

    def __init__(self):
        self.module_arg_spec = dict(
            resource_group=dict(
                type='str',
                required=True
            ),
            name=dict(
                type='str'
            ),
            virtual_network=dict(
                type='raw',
                required=True
            )
        )

        self.resource_group = None
        self.name = None
        self.virtual_network = None

        self.results = dict(changed=False)

        super(AzureRMVirtualNetworkPeeringInfo, self).__init__(derived_arg_spec=self.module_arg_spec,
                                                               supports_tags=False)

    def exec_module(self, **kwargs):
        """Main module execution method"""
        is_old_facts = self.module._name == 'azure_rm_virtualnetworkpeering_facts'
        if is_old_facts:
            self.module.deprecate("The 'azure_rm_virtualnetworkpeering_facts' module has been renamed to 'azure_rm_virtualnetworkpeering_info'", version='2.13')

        for key in list(self.module_arg_spec.keys()):
            setattr(self, key, kwargs[key])

        # parse virtual_network
        self.virtual_network = self.parse_resource_to_dict(self.virtual_network)
        if self.virtual_network['resource_group'] != self.resource_group:
            self.fail('Resource group of virtual_network is not same as param resource_group')

        self.results['vnetpeerings'] = []
        # get vnet peering
        if self.name:
            self.results['vnetpeerings'] = self.get_by_name()
        else:
            self.results['vnetpeerings'] = self.list_by_vnet()

        return self.results

    def get_by_name(self):
        '''
        Gets the Virtual Network Peering.

        :return: List of Virtual Network Peering
        '''
        self.log(
            "Get Virtual Network Peering {0}".format(self.name))
        results = []
        try:
            response = self.network_client.virtual_network_peerings.get(resource_group_name=self.resource_group,
                                                                        virtual_network_name=self.virtual_network['name'],
                                                                        virtual_network_peering_name=self.name)
            self.log("Response : {0}".format(response))
            results.append(vnetpeering_to_dict(response))
        except CloudError:
            self.log('Did not find the Virtual Network Peering.')
        return results

    def list_by_vnet(self):
        '''
        Lists the Virtual Network Peering in specific Virtual Network.

        :return: List of Virtual Network Peering
        '''
        self.log(
            "List Virtual Network Peering in Virtual Network {0}".format(self.virtual_network['name']))
        results = []
        try:
            response = self.network_client.virtual_network_peerings.list(resource_group_name=self.resource_group,
                                                                         virtual_network_name=self.virtual_network['name'])
            self.log("Response : {0}".format(response))
            if response:
                for p in response:
                    results.append(vnetpeering_to_dict(p))
        except CloudError:
            self.log('Did not find the Virtual Network Peering.')
        return results


def main():
    """Main execution"""
    AzureRMVirtualNetworkPeeringInfo()


if __name__ == '__main__':
    main()