Skip to content

Commit

Permalink
Merge pull request #38 from fr0gger03/master
Browse files Browse the repository at this point in the history
feature: attach-t0-prefix list, detach-t0-prefix-list
  • Loading branch information
Patrick Kremer authored Sep 19, 2021
2 parents a50f7fe + 7ec84af commit 978b806
Showing 1 changed file with 60 additions and 17 deletions.
77 changes: 60 additions & 17 deletions pyVMC.py
Original file line number Diff line number Diff line change
Expand Up @@ -1607,6 +1607,49 @@ def newBGPprefixlist(csp_url, session_token):
else:
print("Please choose 1, 2, 3 or 4 - Try again or check the help.")

def attachT0BGPprefixlist(csp_url, session_token, prefix_list_id, route_filter_dir, neighbor_id):
myHeader = {'Authorization': f'Bearer {session_token}', 'Content-type': 'application/json'}
myURL = f'{csp_url}/policy/api/v1/infra/tier-0s/vmc/locale-services/default/bgp/neighbors/' + neighbor_id
response = requests.get(myURL, headers=myHeader)
direction = route_filter_dir + '_route_filters'
if response.status_code == 200:
json_response = response.json()
neighbor_json = json_response
for key in list(neighbor_json.keys()):
if key.startswith('_'):
del neighbor_json[key]
neighbor_json['route_filtering'] = [{'enabled': True, 'address_family': 'IPV4', direction: ['/infra/tier-0s/vmc/prefix-lists/' + prefix_list_id]}]
response = requests.patch(myURL, headers=myHeader, json = neighbor_json)
if response.status_code == 200:
print("Prefix list " + prefix_list_id + " added to " + direction + " for " + neighbor_id)
else:
print(response.status_code)
print(response.json())
print()
else:
print (f'API call failed with status code {response.status_code}. URL: {myURL}.')

def detachT0BGPprefixlists(csp_url, session_token, neighbor_id):
myHeader = {'Authorization': f'Bearer {session_token}', 'Content-type': 'application/json'}
myURL = f'{csp_url}/policy/api/v1/infra/tier-0s/vmc/locale-services/default/bgp/neighbors/' + neighbor_id
response = requests.get(myURL, headers=myHeader)
if response.status_code == 200:
json_response = response.json()
neighbor_json = json_response
for key in list(neighbor_json.keys()):
if key.startswith('_'):
del neighbor_json[key]
neighbor_json['route_filtering'] = [{'enabled': True, 'address_family': 'IPV4'}]
response = requests.patch(myURL, headers=myHeader, json = neighbor_json)
if response.status_code == 200:
print("Prefix lists detached from " + neighbor_id)
else:
print(response.status_code)
print(response.json())
print()
else:
print (f'API call failed with status code {response.status_code}. URL: {myURL}.')

def removeBPGprefixlist(csp_url, session_token, prefix_list_id):
myHeader = {'csp-auth-token': session_token}
myURL = f'{csp_url}/policy/api/v1/infra/tier-0s/vmc/prefix-lists/' + prefix_list_id
Expand All @@ -1625,26 +1668,16 @@ def getSDDCT0BGPneighbors(csp_url, session_token):
if response.status_code == 200:
json_response = response.json()
bgp_neighbors = json_response['results']
bgp_table = PrettyTable(['ID','Remote AS Num','Remote Address'])
bgp_table = PrettyTable(['ID','Remote AS Num','Remote Address','In_route_filter','Out_route_filter'])
for neighbor in bgp_neighbors:
bgp_table.add_row([neighbor['id'],neighbor['remote_as_num'],neighbor['neighbor_address']])
if neighbor.get("in_route_filters"):
bgp_table.add_row([neighbor['id'],neighbor['remote_as_num'],neighbor['neighbor_address'],neighbor['in_route_filters'], "-"])
elif neighbor.get("out_route_filters"):
bgp_table.add_row([neighbor['id'],neighbor['remote_as_num'],neighbor['neighbor_address'],"-",neighbor['out_route_filters'],])
else:
bgp_table.add_row([neighbor['id'],neighbor['remote_as_num'],neighbor['neighbor_address'],"-", "-"])
print('NEIGHBORS:')
print(bgp_table)
filter_table = PrettyTable(['Enabled','Address Family','Out Filter','In Filter'])
if neighbor.get("route_filtering"):
for filter in neighbor['route_filtering']:
if filter.get('out_route_filters'):
out_route_filters = filter['out_route_filters']
else:
out_route_filters = "-"

if filter.get('in_route_filters'):
in_route_filters = filter['in_route_filters']
else:
in_route_filters = "-"
filter_table.add_row([filter['enabled'],filter['address_family'],out_route_filters,in_route_filters])
print("FILTERS:")
print (filter_table)
if len(sys.argv) == 3:
if sys.argv[2] == "showjson":
print('RAW JSON:')
Expand Down Expand Up @@ -1712,6 +1745,8 @@ def getHelp():
print("\tshow-sddc-connected-vpc: show the VPC connected to the SDDC")
print("\tshow-shadow-account: show the Shadow AWS Account VMC is deployed in")
print("\nBGP and Networking")
print("\tattach-t0-prefix-list [PREFIX LIST ID] [in / out] [BGP NEIGHBOR ID]: attach a BGP Prefix List to a T0 BGP neighbor")
print("\tdetach-t0-prefix-lists [BGP NEIGHBOR ID]: detach all prefix lists from specified neighbor")
print("\tnew-t0-prefix-list: create a new T0 BGP Prefix List")
print("\tremove-t0-prefix-list [PREFIX LIST ID]: you can see current prefix list with 'show-t0-prefix-lists': remove a T0 BGP Prefix List")
print("\tset-bgp-as [ASN]: update the BGP AS number")
Expand Down Expand Up @@ -1832,6 +1867,14 @@ def getHelp():
getSDDCT0BGPneighbors(proxy, session_token)
elif intent_name == "new-t0-prefix-list":
newBGPprefixlist(proxy, session_token)
elif intent_name == "attach-t0-prefix-list":
prefix_list_id = sys.argv[2]
route_filter_dir = sys.argv[3]
neighbor_id = sys.argv[4]
attachT0BGPprefixlist(proxy, session_token, prefix_list_id, route_filter_dir, neighbor_id)
elif intent_name == "detach-t0-prefix-lists":
neighbor_id = sys.argv[2]
detachT0BGPprefixlists(proxy, session_token, neighbor_id)
elif intent_name == "remove-t0-prefix-list":
prefix_list_id = sys.argv[2]
removeBPGprefixlist(proxy, session_token, prefix_list_id)
Expand Down

0 comments on commit 978b806

Please sign in to comment.