Skip to content

Commit e8fb505

Browse files
Examples for L3VPN CRUD operations
Signed-off-by: chandrashekh-vmw <[email protected]>
1 parent 15eae0a commit e8fb505

File tree

1 file changed

+267
-0
lines changed

1 file changed

+267
-0
lines changed
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
* *******************************************************
5+
* Copyright (c) VMware, Inc. 2019. All Rights Reserved.
6+
* SPDX-License-Identifier: MIT
7+
* *******************************************************
8+
*
9+
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
10+
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
11+
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
12+
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
13+
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
14+
"""
15+
16+
__author__ = 'VMware, Inc.'
17+
18+
import argparse
19+
import requests
20+
from com.vmware.nsx_policy_client_for_vmc import create_nsx_policy_client_for_vmc
21+
from vmware.vapi.bindings.struct import PrettyPrinter as NsxPrettyPrinter
22+
from com.vmware.nsx_policy.model_client import ApiError
23+
from com.vmware.nsx_policy.model_client import L3VpnSubnet
24+
from com.vmware.nsx_policy.model_client import RouteBasedL3VpnSession
25+
from com.vmware.nsx_policy.model_client import PolicyBasedL3VpnSession
26+
from com.vmware.nsx_policy.model_client import L3VpnSession
27+
from com.vmware.nsx_policy.model_client import L3VpnRule
28+
from com.vmware.nsx_policy.model_client import BgpNeighborConfig
29+
from com.vmware.nsx_policy.model_client import TunnelSubnet
30+
from com.vmware.nsx_policy.model_client import L3Vpn
31+
32+
# format NSXT objects for readability
33+
nsx_pp = NsxPrettyPrinter()
34+
35+
36+
class NSXPolicyL3VPN(object):
37+
"""
38+
e.g. Demonstrate access to NSX Policy Manager and show
39+
L3VPN CRUD operations
40+
"""
41+
42+
def __init__(self):
43+
parser = argparse.ArgumentParser(
44+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
45+
46+
parser.add_argument('--refresh_token',
47+
required=True,
48+
help='Refresh token obtained from CSP')
49+
50+
parser.add_argument('--org_id',
51+
required=True,
52+
help='Orgization ID')
53+
54+
parser.add_argument('--sddc_id',
55+
required=True,
56+
help='Sddc ID')
57+
58+
parser.add_argument('--remote_endpoint_public_ip',
59+
required=True,
60+
help='L3 VPN Remote end point\'s public ip')
61+
62+
parser.add_argument('--passphrase',
63+
required=True,
64+
help='Passphrase used for VPN')
65+
66+
self.args = parser.parse_args()
67+
68+
self.nsx_client = create_nsx_policy_client_for_vmc(
69+
refresh_token=self.args.refresh_token,
70+
org_id=self.args.org_id,
71+
sddc_id=self.args.sddc_id)
72+
73+
def get_l3_vpn_context(self):
74+
print(' Get L3VPN Context '.center(70, '='))
75+
try:
76+
context = self.nsx_client.infra.tier_0s.locale_services.L3vpnContext.get("vmc", "default")
77+
nsx_pp.pprint(context)
78+
return context
79+
except Exception as ex:
80+
print(ex)
81+
self.log_error(ex)
82+
83+
def create_policy_based_l3_vpn(self, vpn_id):
84+
print(' Create policy based L3VPN '.center(70, '='))
85+
try:
86+
context = self.get_l3_vpn_context()
87+
local_end_point_ip = context.available_local_addresses[0].address_value
88+
print "local_end_point_ip={}".format(local_end_point_ip)
89+
destination_subnet = [L3VpnSubnet(subnet="10.3.0.0/16")] # Value should be per the user setup config
90+
source_subnet = [L3VpnSubnet(subnet="10.2.0.0/16")] # Value should be per the user setup config
91+
92+
self.l3vpn_rule = L3VpnRule(
93+
revision=0,
94+
description="rule 1",
95+
display_name="rule1",
96+
resource_type=L3VpnSession.RESOURCE_TYPE_POLICYBASEDL3VPNSESSION,
97+
action=L3VpnRule.ACTION_PROTECT,
98+
destinations=destination_subnet,
99+
sequence_number=0,
100+
id="rule-" + vpn_id,
101+
sources=source_subnet)
102+
l3vpn_session = PolicyBasedL3VpnSession(resource_type=L3VpnSession.RESOURCE_TYPE_POLICYBASEDL3VPNSESSION,
103+
rules=[self.l3vpn_rule])
104+
105+
self.l3VPN = L3Vpn(
106+
revision=0,
107+
id=vpn_id,
108+
description="Example policy based L3VPN",
109+
display_name="Example policy based L3VPN",
110+
resource_type=L3VpnSession.RESOURCE_TYPE_POLICYBASEDL3VPNSESSION,
111+
dh_groups=[L3Vpn.DH_GROUPS_GROUP14],
112+
enable_perfect_forward_secrecy=True,
113+
enabled=True,
114+
ike_digest_algorithms=[L3Vpn.IKE_DIGEST_ALGORITHMS_SHA1], # Value should be per the user setup config
115+
ike_encryption_algorithms=[L3Vpn.IKE_ENCRYPTION_ALGORITHMS_128],
116+
# Value should be per the user setup config
117+
ike_version=L3Vpn.IKE_VERSION_V1, # Value should be per the user setup config
118+
l3vpn_session=l3vpn_session,
119+
local_address=local_end_point_ip,
120+
passphrases=[self.args.passphrase],
121+
remote_public_address=self.args.remote_endpoint_public_ip,
122+
tunnel_digest_algorithms=[L3Vpn.TUNNEL_DIGEST_ALGORITHMS_SHA1],
123+
# Value should be per the user setup config
124+
tunnel_encryption_algorithms=[L3Vpn.TUNNEL_ENCRYPTION_ALGORITHMS_128]
125+
# Value should be per the user setup config
126+
)
127+
self.nsx_client.infra.tier_0s.locale_services.L3vpns.patch("vmc", "default",
128+
l3vpn_id=vpn_id, l3_vpn=self.l3VPN)
129+
except Exception as ex:
130+
print(ex)
131+
self.log_error(ex)
132+
133+
def create_route_based_l3_vpn(self, vpn_id):
134+
print(' Create route based L3VPN '.center(70, '='))
135+
try:
136+
context = self.get_l3_vpn_context()
137+
local_end_point_ip = context.available_local_addresses[0].address_value
138+
print "local_end_point_ip={}".format(local_end_point_ip)
139+
tunnel_subnet = TunnelSubnet(ip_addresses=["169.254.2.1"], # Value should be per the user setup config
140+
prefix_length=24) # Value should be per the user setup config
141+
bgpconfig1 = BgpNeighborConfig(links=None,
142+
description="bgp neighbor config",
143+
display_name="bgp_neighbor_config_1",
144+
id="bgp_neighbor_config_1",
145+
neighbor_address="169.254.2.2", # Value should be per the user setup config
146+
remote_as_num=str(65002)) # Value should be per the user setup config
147+
self.nsx_client.infra.tier_0s.locale_services.bgp.Neighbors.patch(tier0_id="vmc",
148+
locale_service_id="default",
149+
neighbor_id="rb_neighbor_1",
150+
bgp_neighbor_config=bgpconfig1)
151+
152+
neighbor_list = self.nsx_client.infra.tier_0s.locale_services.bgp.Neighbors.list(tier0_id="vmc",
153+
locale_service_id="default"
154+
)
155+
print "List of neighbors={}".format(neighbor_list)
156+
get_neighbhor = self.nsx_client.infra.tier_0s.locale_services.bgp.Neighbors.get(
157+
tier0_id="vmc", locale_service_id="default", neighbor_id="rb_neighbor_1")
158+
print "get_neighbhor={}".format(get_neighbhor)
159+
160+
l3vpn_session = RouteBasedL3VpnSession(routing_config_path=get_neighbhor.path,
161+
tunnel_subnets=[
162+
tunnel_subnet],
163+
resource_type=L3VpnSession.RESOURCE_TYPE_ROUTEBASEDL3VPNSESSION)
164+
self.l3VPN = L3Vpn(
165+
revision=0,
166+
id=vpn_id,
167+
description="vpn config from automation",
168+
display_name="Example route based L3VPN",
169+
resource_type=L3VpnSession.RESOURCE_TYPE_ROUTEBASEDL3VPNSESSION,
170+
dh_groups=[L3Vpn.DH_GROUPS_GROUP14],
171+
enable_perfect_forward_secrecy=True,
172+
enabled=True, # To enabel/disable the VPN
173+
ike_digest_algorithms=[L3Vpn.IKE_DIGEST_ALGORITHMS_SHA1], # Value should be per the user setup config
174+
ike_encryption_algorithms=[L3Vpn.IKE_ENCRYPTION_ALGORITHMS_128],
175+
# Value should be per the user setup config
176+
ike_version=L3Vpn.IKE_VERSION_V1, # Value should be per the user setup config
177+
l3vpn_session=l3vpn_session,
178+
local_address=local_end_point_ip,
179+
passphrases=[self.args.passphrase],
180+
remote_public_address=self.args.remote_endpoint_public_ip,
181+
tunnel_digest_algorithms=[L3Vpn.TUNNEL_DIGEST_ALGORITHMS_SHA1],
182+
# Value should be per the user setup config
183+
tunnel_encryption_algorithms=[L3Vpn.TUNNEL_ENCRYPTION_ALGORITHMS_128]
184+
# Value should be per the user setup config
185+
)
186+
self.nsx_client.infra.tier_0s.locale_services.L3vpns.patch("vmc", "default",
187+
l3vpn_id=vpn_id, l3_vpn=self.l3VPN)
188+
except Exception as ex:
189+
print(ex)
190+
self.log_error(ex)
191+
192+
def list_l3_vpns(self):
193+
print(' List L3VPN '.center(70, '='))
194+
try:
195+
list_of_vpns = self.nsx_client.infra.tier_0s.locale_services.L3vpns.list("vmc", "default")
196+
for vpn_entry in list_of_vpns.results:
197+
nsx_pp.pprint(vpn_entry)
198+
except Exception as ex:
199+
print(ex)
200+
self.log_error(ex)
201+
202+
def get_l3_vpn(self, vpn_id):
203+
print(' Get L3VPN '.center(70, '='))
204+
try:
205+
vpn_entry = self.nsx_client.infra.tier_0s.locale_services.L3vpns.get("vmc", "default", vpn_id)
206+
nsx_pp.pprint(vpn_entry)
207+
except Exception as ex:
208+
print(ex)
209+
self.log_error(ex)
210+
211+
def delete_l3vpn(self, vpn_id):
212+
print(' Delete L3VPN '.center(70, '='))
213+
try:
214+
self.nsx_client.infra.tier_0s.locale_services.L3vpns.delete("vmc", "default", vpn_id)
215+
except Exception as ex:
216+
print(ex)
217+
self.log_error(ex)
218+
219+
def delete_bgp_neighbor(self, neighbor_id):
220+
print(' Delete BGP Neighbor '.center(70, '='))
221+
try:
222+
self.nsx_client.infra.tier_0s.locale_services.bgp.Neighbors.delete(tier0_id="vmc",
223+
locale_service_id="default",
224+
neighbor_id=neighbor_id)
225+
except Exception as ex:
226+
print(ex)
227+
self.log_error(ex)
228+
229+
def log_error(self, ex):
230+
"""
231+
Generic error logger that will use NSXT API Error message decoders for
232+
more descriptive information on errors
233+
"""
234+
api_error = ex.data.convert_to(ApiError)
235+
print("Error configuring {}".format(api_error.error_message))
236+
print("{}".format(api_error.__dict__))
237+
print("{}".format(api_error.details))
238+
239+
def run_policy_based_vpn(self):
240+
self.create_policy_based_l3_vpn(vpn_id="example_policy_vpn_1")
241+
self.list_l3_vpns()
242+
self.get_l3_vpn(vpn_id="example_policy_vpn_1")
243+
244+
def cleanup_policy_based_vpn(self):
245+
self.delete_l3vpn(vpn_id="example_policy_vpn_1")
246+
247+
def run_route_based_vpn(self):
248+
self.create_route_based_l3_vpn(vpn_id="example_route_vpn_1")
249+
self.list_l3_vpns()
250+
self.get_l3_vpn(vpn_id="example_route_vpn_1")
251+
252+
def cleanup_route_based_vpn(self):
253+
self.delete_bgp_neighbor(neighbor_id="rb_neighbor_1")
254+
self.delete_l3vpn(vpn_id="example_route_vpn_1")
255+
256+
257+
def main():
258+
nsx = NSXPolicyL3VPN()
259+
nsx.run_policy_based_vpn()
260+
nsx.cleanup_policy_based_vpn()
261+
nsx.run_route_based_vpn()
262+
nsx.cleanup_route_based_vpn()
263+
264+
265+
if __name__ == '__main__':
266+
main()
267+

0 commit comments

Comments
 (0)