Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_virtualnetworkpeering_info.py

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2019 Yunge Zhu (@yungezz)
4 #
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10 DOCUMENTATION = '''
11 ---
12 module: azure_rm_virtualnetworkpeering_info
13 version_added: "0.0.1"
14 short_description: Get facts of Azure Virtual Network Peering
15 description:
16 - Get facts of Azure Virtual Network Peering.
17
18 options:
19 resource_group:
20 description:
21 - Name of a resource group where the vnet exists.
22 required: True
23 type: str
24 virtual_network:
25 description:
26 - Name or resource ID of a virtual network.
27 required: True
28 type: raw
29 name:
30 description:
31 - Name of the virtual network peering.
32 type: str
33
34 extends_documentation_fragment:
35 - azure.azcollection.azure
36
37 author:
38 - Yunge Zhu (@yungezz)
39 '''
40
41 EXAMPLES = '''
42 - name: Get virtual network peering by name
43 azure_rm_virtualnetworkpeering_info:
44 resource_group: myResourceGroup
45 virtual_network: myVnet1
46 name: myVnetPeer
47
48 - name: List virtual network peering of virtual network
49 azure_rm_virtualnetworkpeering:
50 resource_group: myResourceGroup
51 virtual_network: myVnet1
52 '''
53
54 RETURN = '''
55 vnetpeerings:
56 description:
57 - A list of Virtual Network Peering facts.
58 returned: always
59 type: complex
60 contains:
61 id:
62 description: ID of current Virtual Network peering.
63 returned: always
64 type: str
65 sample:
66 "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myVnet/virtualNetworkPeerings/peer1"
67 name:
68 description:
69 - Name of Virtual Network peering.
70 returned: always
71 type: str
72 sample: myPeering
73 remote_virtual_network:
74 description:
75 - ID of remote Virtual Network to be peered to.
76 returned: always
77 type: str
78 sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myVnet2
79 remote_address_space:
80 description:
81 - The reference of the remote Virtual Network address space.
82 type: complex
83 returned: always
84 contains:
85 address_prefixes:
86 description:
87 - A list of address blocks reserved for this Virtual Network in CIDR notation.
88 returned: always
89 type: list
90 sample: 10.1.0.0/16
91 peering_state:
92 description:
93 - The state of the virtual network peering.
94 returned: always
95 type: str
96 sample: Connected
97 peering_sync_level:
98 description:
99 - The Sync Level of the Peering
100 type: str
101 returned: always
102 sample: "FullyInSync"
103 provisioning_state:
104 description:
105 - The provisioning state of the resource.
106 returned: always
107 type: str
108 sample: Succeeded
109 allow_forwarded_traffic:
110 description:
111 - Whether forwarded traffic from the VMs in the remote Virtual Network will be allowed/disallowed.
112 returned: always
113 type: bool
114 sample: False
115 allow_gateway_transit:
116 description:
117 - Whether gateway links can be used in remote Virtual Networking to link to this Virtual Network.
118 returned: always
119 type: bool
120 sample: False
121 allow_virtual_network_access:
122 description:
123 - Whether the VMs in the linked Virtual Network space can access all the VMs in local Virtual Network space.
124 returned: always
125 type: bool
126 sample: False
127 use_remote_gateways:
128 description:
129 - Whether remote gateways can be used on this Virtual Network.
130 returned: always
131 type: bool
132 sample: False
133 '''
134
135 try:
136 from azure.core.exceptions import ResourceNotFoundError
137 except ImportError:
138 # This is handled in azure_rm_common
139 pass
140
141 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
142
143
144 def vnetpeering_to_dict(vnetpeering):
145 '''
146 Convert a virtual network peering object to a dict.
147 '''
148 results = dict(
149 id=vnetpeering.id,
150 name=vnetpeering.name,
151 remote_virtual_network=vnetpeering.remote_virtual_network.id,
152 remote_address_space=dict(
153 address_prefixes=vnetpeering.remote_address_space.address_prefixes
154 ),
155 peering_state=vnetpeering.peering_state,
156 provisioning_state=vnetpeering.provisioning_state,
157 use_remote_gateways=vnetpeering.use_remote_gateways,
158 allow_gateway_transit=vnetpeering.allow_gateway_transit,
159 allow_forwarded_traffic=vnetpeering.allow_forwarded_traffic,
160 allow_virtual_network_access=vnetpeering.allow_virtual_network_access,
161 peering_sync_level=vnetpeering.peering_sync_level
162 )
163 return results
164
165
166 class AzureRMVirtualNetworkPeeringInfo(AzureRMModuleBase):
167
168 def __init__(self):
169 self.module_arg_spec = dict(
170 resource_group=dict(
171 type='str',
172 required=True
173 ),
174 name=dict(
175 type='str'
176 ),
177 virtual_network=dict(
178 type='raw',
179 required=True
180 )
181 )
182
183 self.resource_group = None
184 self.name = None
185 self.virtual_network = None
186
187 self.results = dict(changed=False)
188
189 super(AzureRMVirtualNetworkPeeringInfo, self).__init__(derived_arg_spec=self.module_arg_spec,
190 supports_check_mode=True,
191 supports_tags=False)
192
193 def exec_module(self, **kwargs):
194 """Main module execution method"""
195
196 for key in list(self.module_arg_spec.keys()):
197 setattr(self, key, kwargs[key])
198
199 # parse virtual_network
200 self.virtual_network = self.parse_resource_to_dict(self.virtual_network)
201 if self.virtual_network['resource_group'] != self.resource_group:
202 self.fail('Resource group of virtual_network is not same as param resource_group')
203
204 self.results['vnetpeerings'] = []
205 # get vnet peering
206 if self.name:
207 self.results['vnetpeerings'] = self.get_by_name()
208 else:
209 self.results['vnetpeerings'] = self.list_by_vnet()
210
211 return self.results
212
213 def get_by_name(self):
214 '''
215 Gets the Virtual Network Peering.
216
217 :return: List of Virtual Network Peering
218 '''
219 self.log(
220 "Get Virtual Network Peering {0}".format(self.name))
221 results = []
222 try:
223 response = self.network_client.virtual_network_peerings.get(resource_group_name=self.resource_group,
224 virtual_network_name=self.virtual_network['name'],
225 virtual_network_peering_name=self.name)
226 self.log("Response : {0}".format(response))
227 results.append(vnetpeering_to_dict(response))
228 except ResourceNotFoundError:
229 self.log('Did not find the Virtual Network Peering.')
230 return results
231
232 def list_by_vnet(self):
233 '''
234 Lists the Virtual Network Peering in specific Virtual Network.
235
236 :return: List of Virtual Network Peering
237 '''
238 self.log(
239 "List Virtual Network Peering in Virtual Network {0}".format(self.virtual_network['name']))
240 results = []
241 try:
242 response = self.network_client.virtual_network_peerings.list(resource_group_name=self.resource_group,
243 virtual_network_name=self.virtual_network['name'])
244 self.log("Response : {0}".format(response))
245 if response:
246 for p in response:
247 results.append(vnetpeering_to_dict(p))
248 except ResourceNotFoundError:
249 self.log('Did not find the Virtual Network Peering.')
250 return results
251
252
253 def main():
254 """Main execution"""
255 AzureRMVirtualNetworkPeeringInfo()
256
257
258 if __name__ == '__main__':
259 main()