Skip to content

Commit

Permalink
Merge branch 'External_network_fetching_logic_change' into 'v1.4.2.1_…
Browse files Browse the repository at this point in the history
…bug_fix'

Changed logic to get only required External Network instead of all External Networks

See merge request tfojta/vcd-v2t!430
  • Loading branch information
Hasan Bohra (c) committed Dec 20, 2023
2 parents b467f49 + 674eb77 commit e6422f0
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 117 deletions.
3 changes: 3 additions & 0 deletions src/core/vcd/vcdConstants.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@
# external networks uri
ALL_EXTERNAL_NETWORKS = "externalNetworks"

# filter for external networks
EXTERNAL_NETWORK_FILTER = "filter=(name=={})"

# create IP Spaces
CREATE_IP_SPACES = "ipSpaces"

Expand Down
86 changes: 53 additions & 33 deletions src/core/vcd/vcdOperations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3077,10 +3077,16 @@ def directNetworkIpCleanup(self, source=False, key="directNetworkIP"):
extNetName = extNetName + '-v2t' if not source else extNetName

# Fetching source external network
for extNet in self.fetchAllExternalNetworks():
if extNet['name'] == extNetName:
extNetData = extNet
break
externalNetworkurl = "{}{}?{}".format(vcdConstants.OPEN_API_URL.format(self.ipAddress),
vcdConstants.ALL_EXTERNAL_NETWORKS,
vcdConstants.EXTERNAL_NETWORK_FILTER.format(
extNetName))
# GET call to fetch the External Network details using its name
response = self.restClientObj.get(externalNetworkurl, headers=self.headers)

if response.status_code == requests.codes.ok:
responseDict = response.json()
extNetData = responseDict.get("values")[0]
else:
raise Exception(f"External Network {extNetName} is not present in vCD")
if ipData:
Expand Down Expand Up @@ -3207,13 +3213,15 @@ def copyIPToSegmentBackedExtNet(self, rollback=False, orgVDCIDList=None, edgeGat
if not rollback:
extNetName += '-v2t'

allExtNet = self.fetchAllExternalNetworks()
externalNetworkurl = "{}{}?{}".format(vcdConstants.OPEN_API_URL.format(self.ipAddress),
vcdConstants.ALL_EXTERNAL_NETWORKS,
vcdConstants.EXTERNAL_NETWORK_FILTER.format(extNetName))
# GET call to fetch the External Network details using its name
response = self.restClientObj.get(externalNetworkurl, headers=self.headers)

# Fetching NSX-T segment backed external network
for extNet in allExtNet:
if extNet['name'] == extNetName:
segmentBackedExtNetData = extNet
break
if response.status_code == requests.codes.ok:
responseDict = response.json()
segmentBackedExtNetData = responseDict.get("values")[0]
else:
raise Exception(f"External Network {extNetName} is not present in vCD")

Expand Down Expand Up @@ -6625,14 +6633,19 @@ def importedNetworkPayload(self, parentNetworkId, orgvdcNetwork, inputDict, nsxO
return: payload data - payload data for creating a dedicated direct network(shared/non-shared)
"""
# Getting source external network details
sourceExternalNetwork = self.fetchAllExternalNetworks()
externalList = [externalNetwork['networkBackings'] for externalNetwork in sourceExternalNetwork if
externalNetwork['id'] == parentNetworkId['id']]
if isinstance(sourceExternalNetwork, Exception):
raise sourceExternalNetwork
for value in externalList:
externalDict = value
backingid = [values['backingId'] for values in externalDict['values']]
extNetUrl = "{}{}/{}".format(vcdConstants.OPEN_API_URL.format(self.ipAddress),
vcdConstants.ALL_EXTERNAL_NETWORKS,
parentNetworkId['id'])
extNetResponse = self.restClientObj.get(extNetUrl, self.headers)
if extNetResponse.status_code == requests.codes.ok:
extNetResponseDict = extNetResponse.json()
else:
raise Exception('Failed to get external network {} details with error - {}'.format(
parentNetworkId['name'], extNetResponse["message"]))

externalList = extNetResponseDict['networkBackings']

backingid = [values['backingId'] for values in externalList['values']]
url = '{}{}'.format(vcdConstants.XML_API_URL.format(self.ipAddress),
vcdConstants.GET_PORTGROUP_VLAN_ID.format(backingid[0]))
acceptHeader = vcdConstants.GENERAL_JSON_ACCEPT_HEADER.format(self.version)
Expand Down Expand Up @@ -6697,23 +6710,30 @@ def v2tBackedNetworkPayload(self, parentNetworkId, orgvdcNetwork, Shared):
Description: THis method is used to create payload for service direct network(shared/non-shared) in non-legacy mode
return: payload data - payload data for creating a service direct network
"""

# Payload for shared direct network / service network use case
externalNetworks = self.fetchAllExternalNetworks()
for extNet in externalNetworks:
targetExternalNetworkurl = "{}{}?{}".format(vcdConstants.OPEN_API_URL.format(self.ipAddress),
vcdConstants.ALL_EXTERNAL_NETWORKS,
vcdConstants.EXTERNAL_NETWORK_FILTER.format(
parentNetworkId['name'] + '-v2t'))
# GET call to fetch the External Network details using its name
response = self.restClientObj.get(targetExternalNetworkurl, headers=self.headers)

if response.status_code == requests.codes.ok:
responseDict = response.json()
extNet = responseDict.get("values")[0]
# Finding segment backed ext net for shared direct network
if parentNetworkId['name'] + '-v2t' == extNet['name']:
if [backing for backing in extNet['networkBackings']['values'] if
backing['backingTypeValue'] == 'IMPORTED_T_LOGICAL_SWITCH']:
payload = {
'name': orgvdcNetwork['name'] + '-v2t',
'description': orgvdcNetwork['description'] if orgvdcNetwork.get(
'description') else '',
'networkType': orgvdcNetwork['networkType'],
'parentNetworkId': {'name': extNet['name'],
'id': extNet['id']},
'shared': Shared
}
break
if [backing for backing in extNet['networkBackings']['values'] if
backing['backingTypeValue'] == 'IMPORTED_T_LOGICAL_SWITCH']:
payload = {
'name': orgvdcNetwork['name'] + '-v2t',
'description': orgvdcNetwork['description'] if orgvdcNetwork.get(
'description') else '',
'networkType': orgvdcNetwork['networkType'],
'parentNetworkId': {'name': extNet['name'],
'id': extNet['id']},
'shared': Shared
}
else:
raise (
f"NSXT segment backed external network {parentNetworkId['name'] + '-v2t'} is not present, and it is "
Expand Down
Loading

0 comments on commit e6422f0

Please sign in to comment.