1111import sys
1212from pathlib import Path
1313
14- import netfoundry
15- from netfoundry .utility import DC_PROVIDERS
16-
14+ from .organization import Organization
15+ from .network_group import NetworkGroup
16+ from .network import Network
17+ from .utility import DC_PROVIDERS
1718
1819def main ():
1920 """Run the demo script."""
@@ -106,22 +107,22 @@ def main():
106107 network_name = args .network
107108
108109 # use the session with some organization, default is to use the first and there's typically only one
109- organization = netfoundry . Organization (
110+ organization = Organization (
110111 credentials = args .credentials if 'credentials' in args else None ,
111112 organization_label = args .organization if 'organization' in args else None ,
112113 proxy = args .proxy
113114 )
114115
115116 # use some Network Group, default is to use the first and there's typically only one
116- network_group = netfoundry . NetworkGroup (
117+ network_group = NetworkGroup (
117118 organization ,
118119 network_group_name = args .network_group if 'network_group' in args else None
119120 )
120121
121122 # create a Network
122- if network_name in network_group .networks_by_name (). keys ( ):
123+ if network_group .network_exists ( network_name ):
123124 # use the Network
124- network = netfoundry . Network (network_group , network_name = network_name )
125+ network = Network (network_group , network_name = network_name )
125126 if args .command == "create" :
126127 network .wait_for_status ("PROVISIONED" ,wait = 999 ,progress = True )
127128 elif args .command == "delete" :
@@ -132,13 +133,14 @@ def main():
132133 sys .exit ()
133134 elif args .command == "create" :
134135 network_id = network_group .create_network (name = network_name ,size = args .size ,version = args .version )['id' ]
135- network = netfoundry . Network (network_group , network_id = network_id )
136+ network = Network (network_group , network_id = network_id )
136137 network .wait_for_status ("PROVISIONED" ,wait = 999 ,progress = True )
137138 elif args .command == "delete" :
138139 print ("Network \" {network_name}\" does not exist." .format (network_name = network_name ))
139140 sys .exit ()
140141 else :
141- raise Exception ("ERROR: failed to find a network named \" {:s}\" " .format (network_name ))
142+ raise Exception ("ERROR: failed to find a network named \" {name}\" and no valid command in \" {command}\" ."
143+ + " Need \" create\" (default) or \" delete\" ." .format (name = network_name , command = args .command ))
142144
143145 # existing hosted routers
144146 hosted_edge_routers = network .edge_routers (only_hosted = True )
@@ -211,54 +213,52 @@ def main():
211213 raise
212214
213215 # create a simple global Edge Router Policy unless one exists with the same name
214- ERPs = network .edge_router_policies ()
215216 blanket_policy_name = "defaultRouters"
216- if not blanket_policy_name in [ erp [ ' name' ] for erp in ERPs ] :
217+ if not network . edge_router_policy_exists ( name = blanket_policy_name ) :
217218 try : network .create_edge_router_policy (name = blanket_policy_name ,edge_router_attributes = ["#defaultRouters" ],endpoint_attributes = ["#all" ])
218219 except : raise
219220
220- endpoints = network .endpoints ()
221221 clients = list ()
222222 client1_name = "Desktop1"
223- if not client1_name in [ end [ ' name' ] for end in endpoints ] :
223+ if not network . endpoint_exists ( name = client1_name ) :
224224 # create an Endpoint for the dialing device that will access Services
225225 client1 = network .create_endpoint (name = client1_name ,attributes = ["#workFromAnywhere" ])
226226 print ("INFO: created Endpoint \" {:s}\" " .format (client1 ['name' ]))
227227 else :
228- client1 = [ end for end in endpoints if end [ ' name' ] == client1_name ] [0 ]
228+ client1 = network . endpoints ( name = client1_name ) [0 ]
229229 print ("INFO: found Endpoint \" {:s}\" " .format (client1 ['name' ]))
230230 clients += [client1 ]
231231
232232 client2_name = "Mobile1"
233- if not client2_name in [ end [ ' name' ] for end in endpoints ] :
233+ if not network . endpoint_exists ( name = client2_name ) :
234234 # create an Endpoint for the dialing device that will access Services
235235 client2 = network .create_endpoint (name = client2_name ,attributes = ["#workFromAnywhere" ])
236236 print ("INFO: created Endpoint \" {:s}\" " .format (client2 ['name' ]))
237237 else :
238- client2 = [ end for end in endpoints if end [ ' name' ] == client2_name ] [0 ]
238+ client2 = network . endpoints ( name = client2_name ) [0 ]
239239 print ("INFO: found Endpoint \" {:s}\" " .format (client2 ['name' ]))
240240 clients += [client2 ]
241241
242242 if args .client :
243243 client3_name = "Linux1"
244- if not client3_name in [ end [ ' name' ] for end in endpoints ] :
244+ if not network . endpoint_exists ( name = client3_name ) :
245245 # create an Endpoint for the dialing device that will access Services
246246 client3 = network .create_endpoint (name = client3_name ,attributes = ["#workFromAnywhere" ])
247247 print ("INFO: created Endpoint \" {:s}\" " .format (client3 ['name' ]))
248248 else :
249- client3 = [ end for end in endpoints if end [ ' name' ] == client3_name ] [0 ]
249+ client3 = network . endpoints ( name = client3_name ) [0 ]
250250 print ("INFO: found Endpoint \" {:s}\" " .format (client3 ['name' ]))
251251 clients += [client3 ]
252252
253253 exits = list ()
254254 if args .private :
255255 exit1_name = "Exit1"
256- if not exit1_name in [ end [ ' name' ] for end in endpoints ] :
256+ if not network . endpoint_exists ( name = exit1_name ) :
257257 # create an Endpoint for the hosting device that will provide access to the server
258258 exit1 = network .create_endpoint (name = exit1_name ,attributes = ["#exits" ])
259259 print ("INFO: created Endpoint \" {:s}\" " .format (exit1 ['name' ]))
260260 else :
261- exit1 = [ end for end in endpoints if end [ ' name' ] == exit1_name ] [0 ]
261+ exit1 = network . endpoints ( name = client3_name ) [0 ]
262262 print ("INFO: found Endpoint \" {:s}\" " .format (exit1 ['name' ]))
263263 exits += [exit1 ]
264264
@@ -278,15 +278,16 @@ def main():
278278 print ("DEBUG: cleaning up used OTT for enrolled Endpoint {end} from {path}" .format (end = end ['name' ],path = token_file ))
279279 os .remove (token_file )
280280
281- services = network . services ()
281+ demo_services = dict ()
282282
283283 if args .private :
284284 # create Endpoint-hosted Services unless name exists
285- service1_name = "Hello Service"
286- if not service1_name in [svc ['name' ] for svc in services ]:
285+ demo_services ['hello_service' ] = dict ()
286+ demo_services ['hello_service' ]['display_name' ] = "Hello Service"
287+ if not network .service_exists (name = demo_services ['hello_service' ]['display_name' ]):
287288 # traffic sent to hello.netfoundry:80 leaves Endpoint exit1 to server hello:3000
288- service1 = network .create_service (
289- name = service1_name ,
289+ demo_services [ 'hello_service' ][ 'entity' ] = network .create_service (
290+ name = demo_services [ 'hello_service' ][ 'display_name' ] ,
290291 attributes = ["#welcomeWagon" ],
291292 client_host_name = "hello.netfoundry" ,
292293 client_port = "80" ,
@@ -295,16 +296,17 @@ def main():
295296 server_port = "3000" ,
296297 server_protocol = "TCP"
297298 )
298- print ("INFO: created Service \" {:s}\" " .format (service1 ['name' ]))
299+ print ("INFO: created Service \" {:s}\" " .format (demo_services [ 'hello_service' ][ 'entity' ] ['name' ]))
299300 else :
300- service1 = [ svc for svc in services if svc [ 'name ' ] == service1_name ] [0 ]
301- print ("INFO: found Service \" {:s}\" " .format (service1 ['name' ]))
301+ demo_services [ 'hello_service' ][ 'entity ' ] = network . services ( name = demo_services [ 'hello_service' ][ 'display_name' ]) [0 ]
302+ print ("INFO: found Service \" {:s}\" " .format (demo_services [ 'hello_service' ][ 'entity' ] ['name' ]))
302303
303- service2_name = "REST Service"
304- if not service2_name in [svc ['name' ] for svc in services ]:
304+ demo_services ['rest_service' ] = dict ()
305+ demo_services ['rest_service' ]['display_name' ] = "REST Service"
306+ if not network .service_exists (name = demo_services ['rest_service' ]['display_name' ]):
305307 # traffic sent to httpbin.netfoundry:80 leaves Endpoint exit1 to server httpbin:80
306- service2 = network .create_service (
307- name = service2_name ,
308+ demo_services [ 'rest_service' ][ 'entity' ] = network .create_service (
309+ name = demo_services [ 'rest_service' ][ 'display_name' ] ,
308310 attributes = ["#welcomeWagon" ],
309311 client_host_name = "httpbin.netfoundry" ,
310312 client_port = "80" ,
@@ -313,19 +315,20 @@ def main():
313315 server_port = "80" ,
314316 server_protocol = "TCP"
315317 )
316- print ("INFO: created Service \" {:s}\" " .format (service2 ['name' ]))
318+ print ("INFO: created Service \" {:s}\" " .format (demo_services [ 'rest_service' ][ 'entity' ] ['name' ]))
317319 else :
318- service2 = [ svc for svc in services if svc [ 'name ' ] == service2_name ] [0 ]
319- print ("INFO: found Service \" {:s}\" " .format (service2 ['name' ]))
320+ demo_services [ 'rest_service' ][ 'entity ' ] = network . services ( name = demo_services [ 'rest_service' ][ 'display_name' ]) [0 ]
321+ print ("INFO: found Service \" {:s}\" " .format (demo_services [ 'rest_service' ][ 'entity' ] ['name' ]))
320322
321323 # Create router-hosted Services unless exists
322324 hosting_router = random .choice (hosted_edge_routers ) # nosec
323325
324- service3_name = "Fireworks Service"
325- if not service3_name in [svc ['name' ] for svc in services ]:
326+ demo_services ['fireworks_service' ] = dict ()
327+ demo_services ['fireworks_service' ]['display_name' ] = "Fireworks Service"
328+ if not network .service_exists (name = demo_services ['fireworks_service' ]['display_name' ]):
326329 # traffic sent to fireworks.netfoundry:80 leaves Routers to 34.204.78.203:80
327- service3 = network .create_service (
328- name = service3_name ,
330+ demo_services [ 'fireworks_service' ][ 'entity' ] = network .create_service (
331+ name = demo_services [ 'fireworks_service' ][ 'display_name' ] ,
329332 attributes = ["#welcomeWagon" ],
330333 client_host_name = "fireworks.netfoundry" ,
331334 client_port = "80" ,
@@ -334,16 +337,17 @@ def main():
334337 server_port = "80" ,
335338 server_protocol = "TCP"
336339 )
337- print ("INFO: created Service \" {:s}\" " .format (service3 ['name' ]))
340+ print ("INFO: created Service \" {:s}\" " .format (demo_services [ 'fireworks_service' ][ 'entity' ] ['name' ]))
338341 else :
339- service3 = [ svc for svc in services if svc [ 'name ' ] == service3_name ] [0 ]
340- print ("INFO: found Service \" {:s}\" " .format (service3 ['name' ]))
342+ demo_services [ 'fireworks_service' ][ 'entity ' ] = network . services ( name = demo_services [ 'fireworks_service' ][ 'display_name' ]) [0 ]
343+ print ("INFO: found Service \" {:s}\" " .format (demo_services [ 'fireworks_service' ][ 'entity' ] ['name' ]))
341344
342- service4_name = "Weather Service"
343- if not service4_name in [svc ['name' ] for svc in services ]:
345+ demo_services ['weather_service' ] = dict ()
346+ demo_services ['weather_service' ]['display_name' ] = "Weather Service"
347+ if not network .service_exists (name = demo_services ['weather_service' ]['display_name' ]):
344348 # traffic sent to weather.netfoundry:80 leaves Routers to wttr.in:80
345- service4 = network .create_service (
346- name = service4_name ,
349+ demo_services [ 'weather_service' ][ 'entity' ] = network .create_service (
350+ name = demo_services [ 'weather_service' ][ 'display_name' ] ,
347351 attributes = ["#welcomeWagon" ],
348352 client_host_name = "weather.netfoundry" ,
349353 client_port = "80" ,
@@ -352,19 +356,20 @@ def main():
352356 server_port = "80" ,
353357 server_protocol = "TCP"
354358 )
355- print ("INFO: created Service \" {:s}\" " .format (service4 ['name' ]))
359+ print ("INFO: created Service \" {:s}\" " .format (demo_services [ 'weather_service' ][ 'entity' ] ['name' ]))
356360 else :
357- service4 = [ svc for svc in services if svc [ 'name ' ] == service4_name ] [0 ]
358- print ("INFO: found Service \" {:s}\" " .format (service4 ['name' ]))
361+ demo_services [ 'weather_service' ][ 'entity ' ] = network . services ( name = demo_services [ 'weather_service' ][ 'display_name' ]) [0 ]
362+ print ("INFO: found Service \" {:s}\" " .format (demo_services [ 'weather_service' ][ 'entity' ] ['name' ]))
359363
360364 # fireworks
361365 # heartbeat
362366
363- service5_name = "Echo Service"
364- if not service5_name in [svc ['name' ] for svc in services ]:
367+ demo_services ['echo_service' ] = dict ()
368+ demo_services ['echo_service' ]['display_name' ] = "Echo Service"
369+ if not network .service_exists (name = demo_services ['echo_service' ]['display_name' ]):
365370 # traffic sent to echo.netfoundry:80 leaves Routers to eth0.me:80
366- service5 = network .create_service (
367- name = service5_name ,
371+ demo_services [ 'echo_service' ][ 'entity' ] = network .create_service (
372+ name = demo_services [ 'echo_service' ][ 'display_name' ] ,
368373 attributes = ["#welcomeWagon" ],
369374 client_host_name = "echo.netfoundry" ,
370375 client_port = "80" ,
@@ -373,22 +378,22 @@ def main():
373378 server_port = "80" ,
374379 server_protocol = "TCP"
375380 )
376- print ("INFO: created Service \" {:s}\" " .format (service5 ['name' ]))
381+ print ("INFO: created Service \" {:s}\" " .format (demo_services [ 'echo_service' ][ 'entity' ] ['name' ]))
377382 else :
378- service5 = [ svc for svc in services if svc [ 'name ' ] == service5_name ] [0 ]
379- print ("INFO: found Service \" {:s}\" " .format (service5 ['name' ]))
383+ demo_services [ 'echo_service' ][ 'entity ' ] = network . services ( name = demo_services [ 'echo_service' ][ 'display_name' ]) [0 ]
384+ print ("INFO: found Service \" {:s}\" " .format (demo_services [ 'echo_service' ][ 'entity' ] ['name' ]))
380385
381386 # create a customer-hosted ER unless exists
382- customer_routers = network .edge_routers (only_customer = True )
383387 customer_router_name = "Branch Exit Router 1"
384- if not customer_router_name in [ er [ ' name' ] for er in customer_routers ] :
388+ if not network . edge_router_exists ( name = customer_router_name ) :
385389 customer_router = network .create_edge_router (
386390 name = customer_router_name ,
387391 attributes = ["#vmWareExitRouters" ],
388392 tunneler_enabled = True
389393 )
390394 else :
391- customer_router = [er for er in customer_routers if er ['name' ] == customer_router_name ][0 ]
395+ customer_router = network .edge_routers (name = customer_router_name )[0 ]
396+
392397 # wait for customer router to be PROVISIONED so that registration will be available
393398 try :
394399 network .wait_for_status ("PROVISIONED" ,id = customer_router ['id' ],type = "edge-router" ,wait = 999 ,progress = True )
@@ -401,22 +406,20 @@ def main():
401406 expiry = customer_router_registration ['expiresAt' ],
402407 ))
403408
404-
405409 # create unless exists
406410 app_wan1_name = "Welcome"
407- app_wans = network .app_wans ()
408- if not app_wan1_name in [aw ['name' ] for aw in app_wans ]:
411+ if not network .app_wan_exists (name = app_wan1_name ):
409412 # workFromAnywhere may connect to welcomeWagon
410413 app_wan1 = network .create_app_wan (name = app_wan1_name ,endpoint_attributes = ["#workFromAnywhere" ],service_attributes = ["#welcomeWagon" ])
411414 print ("INFO: created AppWAN \" {:s}\" " .format (app_wan1 ['name' ]))
412415 else :
413- app_wan1 = [ aw for aw in app_wans if aw [ ' name' ] == app_wan1_name ] [0 ]
416+ app_wan1 = network . app_wans ( name = app_wan1_name ) [0 ]
414417 print ("INFO: found AppWAN \" {:s}\" " .format (app_wan1 ['name' ]))
415418
416419 print ("SUCCESS! The next step is to enroll one or more of your client Endpoints on some device(s) and visit one of the demo Service URLs described in the demo document ({doc})."
417420 "You may also log in to the web console ({nfconsole}) to play with your Network" .format (doc = "https://developer.netfoundry.io/v2/tools/#demos" ,nfconsole = network_group .nfconsole ))
418- for svc in network . services () :
419- print ("* {name}:\t http://{url}/" .format (name = svc [ ' name' ],url = svc ['model' ]['clientIngress' ]['host' ]))
421+ for svc in demo_services :
422+ print ("* {name}:\t http://{url}/" .format (name = demo_services [ svc ][ 'entity' ][ ' name' ],url = demo_services [ svc ][ 'entity' ] ['model' ]['clientIngress' ]['host' ]))
420423
421424def query_yes_no (question : str , default : str = "no" ):
422425 """Ask a yes/no question via input() and return their answer.
0 commit comments