Skip to content

Commit

Permalink
Add synchronizing of VNet peering when sync level is LocalNotInSync (
Browse files Browse the repository at this point in the history
…#1025)

* Add synchronizing of VNet peering when sync level is `LocalNotInSync`

* Remove commented out code.

* Remove more commented out code.

* Remove whitespace and add returned: always for attribute.

* Remove more whitespace.

* Remove whitespace and fix indentation.
  • Loading branch information
kmj251 authored Jan 12, 2023
1 parent f1c0485 commit d503fde
Showing 1 changed file with 62 additions and 2 deletions.
64 changes: 62 additions & 2 deletions plugins/modules/azure_rm_virtualnetworkpeering.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@
type: str
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myVirtualN
etwork/virtualNetworkPeerings/myPeering"
peering_sync_level:
description:
- The Sync Level of the Peering
type: str
returned: always
sample: "FullyInSync"
'''

try:
Expand Down Expand Up @@ -152,7 +158,8 @@ def vnetpeering_to_dict(vnetpeering):
allow_gateway_transit=vnetpeering.allow_gateway_transit,
allow_forwarded_traffic=vnetpeering.allow_forwarded_traffic,
allow_virtual_network_access=vnetpeering.allow_virtual_network_access,
etag=vnetpeering.etag
etag=vnetpeering.etag,
peering_sync_level=vnetpeering.peering_sync_level
)
return results

Expand Down Expand Up @@ -220,6 +227,7 @@ def exec_module(self, **kwargs):
setattr(self, key, kwargs[key])

to_be_updated = False
to_be_synced = False

resource_group = self.get_resource_group(self.resource_group)

Expand Down Expand Up @@ -248,6 +256,7 @@ def exec_module(self, **kwargs):

# check if update
to_be_updated = self.check_update(response)
to_be_synced = self.check_sync(response)

else:
# not exists, create new vnet peering
Expand Down Expand Up @@ -281,6 +290,15 @@ def exec_module(self, **kwargs):

response = self.create_or_update_vnet_peering()
self.results['id'] = response['id']
to_be_synced = self.check_sync(response)

if to_be_synced:
self.results['changed'] = True

if self.check_mode:
return self.results
sync_response = self.sync_vnet_peering()
self.results['peering_sync_level'] = sync_response['peering_sync_level']

return self.results

Expand All @@ -306,6 +324,11 @@ def format_vnet_id(self, vnet):
self.fail("remote_virtual_network could be a valid resource id, dict of name and resource_group, name of virtual network in same resource group.")
return remote_vnet_id

def check_sync(self, exisiting_vnet_peering):
if exisiting_vnet_peering['peering_sync_level'] == 'LocalNotInSync':
return True
return False

def check_update(self, exisiting_vnet_peering):
if self.allow_forwarded_traffic != exisiting_vnet_peering['allow_forwarded_traffic']:
return True
Expand All @@ -330,6 +353,41 @@ def get_vnet(self, resource_group, vnet_name):
return results
return False

def sync_vnet_peering(self):
'''
Creates or Update Azure Virtual Network Peering.
:return: deserialized Azure Virtual Network Peering instance state dictionary
'''
self.log("Creating or Updating the Azure Virtual Network Peering {0}".format(self.name))

vnet_id = format_resource_id(self.virtual_network['name'],
self.subscription_id,
'Microsoft.Network',
'virtualNetworks',
self.virtual_network['resource_group'])
peering = self.network_models.VirtualNetworkPeering(
id=vnet_id,
name=self.name,
remote_virtual_network=self.network_models.SubResource(id=self.remote_virtual_network),
allow_virtual_network_access=self.allow_virtual_network_access,
allow_gateway_transit=self.allow_gateway_transit,
allow_forwarded_traffic=self.allow_forwarded_traffic,
use_remote_gateways=self.use_remote_gateways
)

try:
response = self.network_client.virtual_network_peerings.begin_create_or_update(self.resource_group,
self.virtual_network['name'],
self.name,
peering,
sync_remote_address_space=True)
if isinstance(response, LROPoller):
response = self.get_poller_result(response)
return vnetpeering_to_dict(response)
except Exception as exc:
self.fail("Error creating Azure Virtual Network Peering: {0}.".format(exc.message))

def create_or_update_vnet_peering(self):
'''
Creates or Update Azure Virtual Network Peering.
Expand All @@ -350,7 +408,8 @@ def create_or_update_vnet_peering(self):
allow_virtual_network_access=self.allow_virtual_network_access,
allow_gateway_transit=self.allow_gateway_transit,
allow_forwarded_traffic=self.allow_forwarded_traffic,
use_remote_gateways=self.use_remote_gateways)
use_remote_gateways=self.use_remote_gateways
)

try:
response = self.network_client.virtual_network_peerings.begin_create_or_update(self.resource_group,
Expand Down Expand Up @@ -392,6 +451,7 @@ def get_vnet_peering(self):
self.virtual_network['name'],
self.name)
self.log("Response : {0}".format(response))

return vnetpeering_to_dict(response)
except ResourceNotFoundError:
self.log('Did not find the Virtual Network Peering.')
Expand Down

0 comments on commit d503fde

Please sign in to comment.