1010from .SSHKey import SSHKey
1111from .Project import Project
1212from .Facility import Facility
13+ from .Metro import Metro
1314from .OperatingSystem import OperatingSystem
1415from .Volume import Volume
1516from .BGPConfig import BGPConfig
@@ -41,6 +42,14 @@ def list_facilities(self, params={}):
4142 facilities .append (facility )
4243 return facilities
4344
45+ def list_metros (self , params = {}):
46+ data = self .call_api ("metros" , params = params )
47+ metros = list ()
48+ for jsoned in data ["metros" ]:
49+ metro = Metro (jsoned )
50+ metros .append (metro )
51+ return metros
52+
4453 def list_plans (self , params = {}):
4554 data = self .call_api ("plans" , params = params )
4655 plans = list ()
@@ -121,8 +130,8 @@ def create_device(
121130 project_id ,
122131 hostname ,
123132 plan ,
124- facility ,
125- operating_system ,
133+ facility = "" ,
134+ operating_system = "" ,
126135 always_pxe = False ,
127136 billing_cycle = "hourly" ,
128137 features = {},
@@ -139,11 +148,11 @@ def create_device(
139148 hardware_reservation_id = "" ,
140149 storage = {},
141150 customdata = {},
151+ metro = "" ,
142152 ):
143153
144154 params = {
145155 "billing_cycle" : billing_cycle ,
146- "facility" : facility ,
147156 "features" : features ,
148157 "hostname" : hostname ,
149158 "locked" : locked ,
@@ -157,6 +166,10 @@ def create_device(
157166 "userdata" : userdata ,
158167 }
159168
169+ if metro != "" :
170+ params ["metro" ] = metro
171+ if facility != "" :
172+ params ["facility" ] = facility
160173 if hardware_reservation_id != "" :
161174 params ["hardware_reservation_id" ] = hardware_reservation_id
162175 if storage :
@@ -319,6 +332,23 @@ def validate_capacity(self, servers):
319332 else :
320333 raise e
321334
335+ # servers is a list of tuples of metro, plan, and quantity.
336+ def validate_metro_capacity (self , servers ):
337+ params = {"servers" : []}
338+ for server in servers :
339+ params ["servers" ].append (
340+ {"facility" : server [0 ], "plan" : server [1 ], "quantity" : server [2 ]}
341+ )
342+
343+ try :
344+ data = self .call_api ("/capacity/metros" , "POST" , params )
345+ return all (s ["available" ] for s in data ["servers" ])
346+ except PacketError as e : # pragma: no cover
347+ if e .args [0 ] == "Error 503: Service Unavailable" :
348+ return False
349+ else :
350+ raise e
351+
322352 def get_spot_market_prices (self , params = {}):
323353 data = self .call_api ("/market/spot/prices" , params = params )
324354 return data ["spot_market_prices" ]
@@ -405,20 +435,24 @@ def reserve_ip_address(
405435 project_id ,
406436 type ,
407437 quantity ,
408- facility ,
438+ facility = "" ,
409439 details = None ,
410440 comments = None ,
411441 tags = list (),
442+ metro = "" ,
412443 ):
413444 request = {
414445 "type" : type ,
415446 "quantity" : quantity ,
416- "facility" : facility ,
417447 "details" : details ,
418448 "comments" : comments ,
419449 "tags" : tags ,
420450 }
421451
452+ if facility != "" :
453+ request ["facility" ] = facility
454+ if metro != "" :
455+ request ["metro" ] = metro
422456 data = self .call_api (
423457 "/projects/%s/ips" % project_id , params = request , type = "POST"
424458 )
@@ -575,15 +609,18 @@ def list_vlans(self, project_id, params=None):
575609 return vlans
576610
577611 def create_vlan (
578- self , project_id , facility , vxlan = None , vlan = None , description = None
612+ self , project_id , facility = "" , vxlan = None , vlan = None , description = None , metro = ""
579613 ):
580614 params = {
581615 "project_id" : project_id ,
582- "facility" : facility ,
583616 "vxlan" : vxlan ,
584617 "vlan" : vlan ,
585618 "description" : description ,
586619 }
620+ if facility != "" :
621+ params ["facility" ] = facility
622+ if metro != "" :
623+ params ["metro" ] = metro
587624 data = self .call_api (
588625 "projects/%s/virtual-networks" % project_id , type = "POST" , params = params
589626 )
@@ -638,14 +675,12 @@ def create_packet_connections(self, params):
638675 "project_id" : params ["project_id" ],
639676 "provider_id" : params ["provider_id" ],
640677 "provider_payload" : params ["provider_payload" ],
641- "facility" : params ["facility" ],
642678 "port_speed" : params ["port_speed" ],
643679 "vlan" : params ["vlan" ],
644680 }
645- if "tags" in params :
646- body ["tags" ] = params ["tags" ]
647- if "description" in params :
648- body ["description" ] = params ["description" ]
681+ for opt in ["tags" , "description" , "facility" , "metro" ]:
682+ if opt in params :
683+ body [opt ] = params [opt ]
649684
650685 data = self .call_api ("/packet-connect/connections" , type = "POST" , params = body )
651686 return data
0 commit comments