Skip to content

Commit 93d98b2

Browse files
committed
finish adopting the regions endpoint
1 parent 509e492 commit 93d98b2

4 files changed

Lines changed: 65 additions & 59 deletions

File tree

netfoundry/ctl.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,14 @@ def get(cli, echo: bool = True, spinner: object = None):
487487
matches = organization.find_roles(**cli.args.query)
488488
if len(matches) == 1:
489489
match = matches[0]
490+
elif cli.args.resource_type == "region":
491+
if 'id' in query_keys:
492+
cli.log.error("regions do not have an ID property, try provider and location_code params")
493+
sysexit(1)
494+
else:
495+
matches = networks.find_regions(**cli.args.query)
496+
if len(matches) == 1:
497+
match = matches[0]
490498
elif cli.args.resource_type == "network":
491499
if 'id' in query_keys:
492500
if len(query_keys) > 1:
@@ -524,27 +532,15 @@ def get(cli, echo: bool = True, spinner: object = None):
524532
else:
525533
cli.log.error("need --network=ACMENet")
526534
sysexit(1)
527-
if cli.args.resource_type == "region":
528-
if 'id' in query_keys:
529-
cli.log.warn("regions fetched by ID may not support this network's product version, try provider or locationCode params for safety")
530-
if len(query_keys) > 1:
531-
query_keys.remove('id')
532-
cli.log.warn(f"using 'id' only, ignoring params: '{', '.join(query_keys)}'")
533-
match = network.get_region_by_id(id=cli.args.query['id'])
534-
else:
535-
matches = networks.find_regions(**cli.args.query)
536-
if len(matches) == 1:
537-
match = network.get_region_by_id(id=matches[0]['id'])
535+
if 'id' in query_keys:
536+
if len(query_keys) > 1:
537+
query_keys.remove('id')
538+
cli.log.warn(f"using 'id' only, ignoring params: '{', '.join(query_keys)}'")
539+
match = network.get_resource_by_id(type=cli.args.resource_type, id=cli.args.query['id'], accept=cli.args.accept)
538540
else:
539-
if 'id' in query_keys:
540-
if len(query_keys) > 1:
541-
query_keys.remove('id')
542-
cli.log.warn(f"using 'id' only, ignoring params: '{', '.join(query_keys)}'")
543-
match = network.get_resource_by_id(type=cli.args.resource_type, id=cli.args.query['id'], accept=cli.args.accept)
544-
else:
545-
matches = network.find_resources(type=cli.args.resource_type, accept=cli.args.accept, params=cli.args.query)
546-
if len(matches) == 1:
547-
match = matches[0]
541+
matches = network.find_resources(type=cli.args.resource_type, accept=cli.args.accept, params=cli.args.query)
542+
if len(matches) == 1:
543+
match = matches[0]
548544

549545
if match:
550546
cli.log.debug(f"found exactly one {cli.args.resource_type} by '{', '.join(query_keys)}'")
@@ -686,6 +682,7 @@ def list(cli, echo: bool = True, spinner: object = None):
686682

687683
valid_keys = set()
688684
for match in matches:
685+
# cli.log.debug(match)
689686
valid_keys = valid_keys.union(match.keys())
690687

691688
# intersection of the set of valid, observed keys in the first match
@@ -973,7 +970,7 @@ def demo(cli):
973970
# a list of locations to place a hosted router
974971
fabric_placements = []
975972
for region in cli.config.demo.regions:
976-
region_matches = networks.find_regions(provider=cli.config.demo.provider, location_code=region)
973+
region_matches = networks.find_regions(providers=[cli.config.demo.provider], location_code=region)
977974
if not len(region_matches) == 1:
978975
raise RuntimeError(f"invalid region '{region}'")
979976
else:

netfoundry/network.py

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -267,16 +267,6 @@ def validate_entity_roles(self, entities: list, type: str):
267267

268268
return(valid_entities)
269269

270-
def get_region_by_id(self, id: str):
271-
"""Get data center region by UUIDv4.
272-
273-
:param id: required UUIDv4 of data center
274-
"""
275-
url = self.audience+'core/v2/regions/'+id
276-
data_center, status_symbol = get_generic_resource_by_url(setup=self, url=url)
277-
return(data_center)
278-
get_data_center_by_id = get_region_by_id
279-
280270
@docstring_parameters(providers=str(DC_PROVIDERS))
281271
def find_regions(self, **kwargs):
282272
"""Find regions for hosted router placement.
@@ -632,15 +622,9 @@ def create_edge_router(self, name: str, attributes: list = list(), link_listener
632622
"linkListener": link_listener,
633623
"tunnelerEnabled": tunneler_enabled
634624
}
635-
if data_center_id:
636-
self.logger.warning('data_center_id is deprecated by provider, location_code. ')
637-
data_center = self.get_data_center_by_id(id=data_center_id)
638-
body['provider'] = data_center['provider']
639-
body['locationCode'] = data_center['locationCode']
640-
body['linkListener'] = True
641-
elif provider or location_code:
625+
if provider or location_code:
642626
if provider and location_code:
643-
data_centers = self.get_edge_router_data_centers(provider=provider, location_code=location_code)
627+
data_centers = self.find_regions(provider=provider, location_code=location_code)
644628
if len(data_centers) == 1:
645629
body['provider'] = provider
646630
body['locationCode'] = location_code
@@ -1642,18 +1626,39 @@ def get_network_domain_resource(self, resource_type: str, id: str, **kwargs):
16421626
resource = get_generic_resource_by_url(setup=self, url=url, **params)
16431627
return(resource)
16441628

1645-
def find_regions(self, **kwargs):
1646-
"""Find regions."""
1647-
# data centers returns a list of dicts (data center objects)
1648-
params = dict()
1649-
for param in kwargs.keys():
1650-
params[param] = kwargs[param]
1629+
def find_regions(self, providers: list = [], provider: str = None, location_code: str = None):
1630+
"""
1631+
Find regions.
16511632
1652-
if params.get('provider') and not params['provider'] in DC_PROVIDERS:
1653-
raise RuntimeError(f"unknown cloud provider '{params['provider']}'. Need one of {str(DC_PROVIDERS)}")
1633+
Optionally filter by provider, and optionally get exactly one region by sending both a single element for providers and a location_code
1634+
1635+
:param providers: optional list of providers from DC_PROVIDERS
1636+
:param provider: optional string matching one provider from DC_PROVIDERS
1637+
:param location_code: optional string that uniquely identifies a region for some provider
1638+
"""
1639+
# regions returns a list of dicts (data center objects)
1640+
1641+
if provider:
1642+
providers.append(provider)
1643+
1644+
if providers:
1645+
for provider in providers:
1646+
if provider not in DC_PROVIDERS:
1647+
raise RuntimeError(f"unknown cloud provider '{provider}'. Need one of {str(DC_PROVIDERS)}")
16541648

16551649
url = self.audience+NET_RESOURCES['regions'].find_url
1656-
regions = list()
1657-
for i in find_generic_resources(setup=self, url=url, embedded=NET_RESOURCES['regions']._embedded, **params):
1658-
regions.extend(i)
1659-
return(regions)
1650+
1651+
if len(providers) == 1 and location_code:
1652+
url = f"{url}/{providers[0]}/{location_code}"
1653+
region, status = get_generic_resource_by_url(setup=self, url=url)
1654+
return([region])
1655+
else:
1656+
regions = list()
1657+
for i in find_generic_resources(setup=self, url=url, embedded=NET_RESOURCES['regions']._embedded, providers=providers):
1658+
regions.extend(i)
1659+
1660+
if location_code and len(regions) > 0:
1661+
regions = [r for r in regions if r['locationCode'] == location_code]
1662+
return(regions)
1663+
else:
1664+
return(regions)

netfoundry/network_group.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Use a network group and find its networks."""
22

3+
# from .organization import Organization
34
from .network import Networks
45
from .utility import NET_RESOURCES, STATUS_CODES, caseless_equal, create_generic_resource, find_generic_resources, get_generic_resource_by_url, http, is_uuidv4, normalize_caseless
56

@@ -12,6 +13,7 @@ class NetworkGroup:
1213

1314
def __init__(self, Organization: object, network_group_id: str = None, network_group_name: str = None, group: str = None):
1415
"""Initialize the network group class with a group name or ID."""
16+
# self.organization = Organization
1517
self.logger = Organization.logger
1618
self.network_groups = Organization.find_network_groups_by_organization()
1719
if (not network_group_id and not network_group_name) and group:
@@ -72,9 +74,9 @@ def __init__(self, Organization: object, network_group_id: str = None, network_g
7274
def map_region_id_by_location_code(self):
7375
"""Map all region ids by their location code e.g. us-west-1: id."""
7476
region_map = dict()
75-
for region in Networks.find_regions(provider='OCP') + Networks.find_regions(provider='AWS'):
76-
region_map[region['locationCode']] = region['id']
77-
# e.g. { us-east-1: 02f0eb51-fb7a-4d2e-8463-32bd9f6fa4d7 }
77+
for region in Networks.find_regions(providers=['OCI', 'AWS']):
78+
region_map[region['provider']-region['locationCode']] = region['name']
79+
# e.g. { AWS-us-east-1: 02f0eb51-fb7a-4d2e-8463-32bd9f6fa4d7 }
7880
return(region_map)
7981
nc_data_centers_by_location = map_region_id_by_location_code
8082

@@ -147,7 +149,7 @@ def find_latest_network_version(self, network_versions: list = list(), is_active
147149

148150
find_latest_product_version = find_latest_network_version
149151

150-
def create_network(self, name: str, network_group_id: str = None, location: str = "us-east-1", version: str = None, size: str = "medium", wait: int = 1200, sleep: int = 10, **kwargs):
152+
def create_network(self, name: str, network_group_id: str = None, location: str = "us-ashburn-1", provider: str = "OCI", version: str = None, size: str = "medium", wait: int = 1200, sleep: int = 10, **kwargs):
151153
"""
152154
Create a network in this network group.
153155
@@ -159,7 +161,7 @@ def create_network(self, name: str, network_group_id: str = None, location: str
159161
"""
160162
# my_nc_data_centers_by_location = self.nc_data_centers_by_location()
161163
# if not my_nc_data_centers_by_location.get(location):
162-
# raise RuntimeError(f"unexpected network location '{location}'. Valid locations include: {', '.join(my_nc_data_centers_by_location.keys())}.")
164+
# raise RuntimeError(f"unexpected network location '{location}'. Valid locations include: {', '.join(my_nc_data_centers_by_location.keys())}.")
163165

164166
# map incongruent api keys from kwargs to function params ("name", "size" are congruent)
165167
for param, value in kwargs.items():
@@ -178,13 +180,15 @@ def create_network(self, name: str, network_group_id: str = None, location: str
178180
else:
179181
self.logger.warn(f"ignoring unexpected keyword argument '{param}'")
180182

181-
matching_regions = Networks.find_regions(region=location)
183+
networks = Networks(setup=self)
184+
matching_regions = networks.find_regions(provider=provider, location_code=location)
182185
if not len(matching_regions) == 1:
183186
raise RuntimeError(f"failed to find exactly one match for requested controller region '{location}'")
184187

185188
body = {
186189
"name": name.strip('"'),
187-
"locationCode": location,
190+
"provider": provider,
191+
"region": location,
188192
"size": size,
189193
}
190194

netfoundry/utility.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ def __post_init__(self):
885885
}
886886
}
887887

888-
DC_PROVIDERS = ["AWS", "AZURE", "GCP", "OCP"]
888+
DC_PROVIDERS = ["AWS", "AZURE", "GCP", "OCP", "OCI", "ALICLOUD", "NETFOUNDRY"]
889889
VALID_SERVICE_PROTOCOLS = ["tcp", "udp"]
890890
VALID_SEPARATORS = '[:-]' # : or - will match regex pattern
891891

0 commit comments

Comments
 (0)