Skip to content

Commit 6649eea

Browse files
committed
add a constant NETWORK_RESOURCES to describe the types of resources in the network domain
1 parent 1215121 commit 6649eea

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

netfoundry/ctl.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from .network import Network
3232
from .network_group import NetworkGroup
3333
from .organization import Organization
34-
from .utility import RESOURCES, Utility, plural, singular
34+
from .utility import NETWORK_RESOURCES, RESOURCES, Utility, plural, singular
3535
from packaging import version
3636

3737
set_metadata(version="v"+get_versions()['version']) # must precend import milc.cli
@@ -129,13 +129,13 @@ def login(cli, api: str=None, shell: bool=None):
129129
# configure the current shell)
130130
if not cli.args.shell and cli.config.general.output == "text":
131131
summary_table = [['domain', 'summary']]
132-
summary_table.append(['organization', '"{org_name}" ({org_label}@{env}) logged in as {fullname} ({email}) until {expiry_timestamp} ({expiry_seconds}s)'.format(
132+
summary_table.append(['organization', '"{org_name}" ({org_label}@{env}) logged in as {fullname} ({email}) until {expiry_timestamp} (T-{expiry_seconds}s)'.format(
133133
fullname=summary_object['caller']['name'],
134134
email=summary_object['caller']['email'],
135135
org_label=organization.label,
136136
org_name=organization.name,
137137
env=organization.environment,
138-
expiry_timestamp=time.strftime('%Y-%m-%d %H:%M:%S GMT%z', time.localtime(organization.expiry)),
138+
expiry_timestamp=time.strftime('%H:%M GMT%z', time.localtime(organization.expiry)),
139139
expiry_seconds=int(organization.expiry_seconds)
140140
)])
141141
if network_group:
@@ -281,7 +281,7 @@ def whoami(cli, echo: bool=True, organization: object=None):
281281

282282
@cli.argument('-f', '--file', help='JSON or YAML file', type=argparse.FileType('r', encoding='UTF-8'))
283283
@cli.argument('-w','--wait', help='seconds to wait for process execution to finish', default=0)
284-
@cli.argument('resource_type', arg_only=True, help='type of resource', choices=[singular(type) for type in RESOURCES.keys()])
284+
@cli.argument('resource_type', arg_only=True, help='type of resource', choices=[singular(type) for type in NETWORK_RESOURCES.keys()])
285285
@cli.subcommand('create a resource from stdin or file')
286286
def create(cli):
287287
"""Create a resource.
@@ -293,8 +293,8 @@ def create(cli):
293293
# get the input object if available, else get the lines (serialized YAML or JSON) and try to deserialize
294294
create_input_object, create_input_lines, create_object = None, str(), None
295295
if sys.stdin.isatty() and not cli.args.file:
296-
if 'create_template' in RESOURCES[plural(cli.args.resource_type)].keys():
297-
create_input_object = RESOURCES[plural(cli.args.resource_type)]['create_template']
296+
if 'create_template' in NETWORK_RESOURCES[plural(cli.args.resource_type)].keys():
297+
create_input_object = NETWORK_RESOURCES[plural(cli.args.resource_type)]['create_template']
298298
else:
299299
create_input_object = {"hint": "No template was found for resource type {type}. Replace the contents of this buffer with the request body as YAML or JSON to create a resource. networkId will be added automatically.".format(type=cli.args.resource_type)}
300300
elif cli.args.file:
@@ -348,7 +348,7 @@ def create(cli):
348348
resource = network.create_resource(type=cli.args.resource_type, properties=create_object, wait=cli.config.create.wait)
349349

350350
@cli.argument('query', arg_only=True, action=StoreDictKeyPair, nargs='?', help="id=UUIDv4 or query params as k=v,k=v comma-separated pairs")
351-
@cli.argument('resource_type', arg_only=True, help='type of resource', choices=[singular(type_name) for type_name, type_props in RESOURCES.items() if type_props['domain'] == "network"])
351+
@cli.argument('resource_type', arg_only=True, help='type of resource', choices=NETWORK_RESOURCES.keys())
352352
# this allows us to pass the edit subcommand's cli object to function get without further modifying that functions params
353353
@cli.argument('-a', '--accept', arg_only=True, default='update', help=argparse.SUPPRESS)
354354
@cli.subcommand('edit a single resource selected by query with editor defined in NETFOUNDRY_EDITOR or EDITOR')
@@ -407,7 +407,7 @@ def get(cli, echo: bool=True):
407407
organization=organization,
408408
network=cli.config.general.network,
409409
)
410-
match = network.describe
410+
match = organization.get_network(network_id=network.id, embed="all", accept=cli.args.accept)
411411
else:
412412
matches = organization.get_networks_by_organization(**cli.args.query)
413413
if len(matches) == 1:
@@ -552,7 +552,7 @@ def list(cli):
552552
cli.echo(json_dumps(filtered_matches, indent=4))
553553

554554
@cli.argument('query', arg_only=True, action=StoreDictKeyPair, nargs='?', help="query params as k=v,k=v comma-separated pairs")
555-
@cli.argument('resource_type', arg_only=True, help='type of resource', choices=[singular(type) for type in RESOURCES.keys()])
555+
@cli.argument('resource_type', arg_only=True, help='type of resource', choices=[singular(type) for type in NETWORK_RESOURCES.keys()])
556556
@cli.argument('-w','--wait', help='seconds to wait for confirmation of delete', default=0)
557557
@cli.subcommand('delete a resource in the network domain')
558558
def delete(cli):

netfoundry/network.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from unicodedata import name # enforce a timeout; sleep
99

1010
from .utility import (DC_PROVIDERS, EXCLUDED_PATCH_PROPERTIES,
11-
MAJOR_REGIONS, RESOURCES, STATUS_CODES, VALID_SEPARATORS,
11+
MAJOR_REGIONS, NETWORK_RESOURCES, STATUS_CODES, VALID_SEPARATORS,
1212
VALID_SERVICE_PROTOCOLS, Utility, docstring_parameters,
1313
eprint, http, is_uuidv4, plural, singular)
1414

@@ -224,7 +224,7 @@ def validate_port_ranges(self, ports: list):
224224

225225
return(valid_port_ranges)
226226

227-
@docstring_parameters(resource_entity_types=str(RESOURCES.keys()))
227+
@docstring_parameters(resource_entity_types=str(NETWORK_RESOURCES.keys()))
228228
def validate_entity_roles(self, entities: list, type: str):
229229
"""Return a list of valid, existing entities and hashtag role attributes.
230230
@@ -418,11 +418,11 @@ def get_resource_by_id(self, type: str, id: str, accept: str=None):
418418
# if singular(type) == "service":
419419
# params["beta"] = ''
420420

421-
if not plural(type) in RESOURCES.keys():
421+
if not plural(type) in NETWORK_RESOURCES.keys():
422422
raise Exception("ERROR: unknown type \"{singular}\" as plural \"{plural}\". Choices: {choices}".format(
423423
singular=type,
424424
plural=plural(type),
425-
choices=RESOURCES.keys()
425+
choices=NETWORK_RESOURCES.keys()
426426
))
427427
elif plural(type) in ["edge-routers","network-controllers"]:
428428
params['embed'] = "host"
@@ -456,7 +456,7 @@ def get_resource_by_id(self, type: str, id: str, accept: str=None):
456456

457457
get_resource = get_resource_by_id
458458

459-
def get_resources(self, type: str, accept: str=None, deleted: bool=False, typeId: str=None, **kwargs):
459+
def get_resources(self, type: str, accept: str=None, deleted: bool=False, **kwargs):
460460
"""Find resources by type.
461461
462462
:param str type: plural of an entity type e.g. networks, endpoints, services, posture-checks, etc...
@@ -465,7 +465,6 @@ def get_resources(self, type: str, accept: str=None, deleted: bool=False, typeId
465465
"create" is useful for comparing an existing entity to a set of properties that are used to create the same type of
466466
entity in a POST request, and "update" may be used in the same way for a PUT update.
467467
:param bool deleted: include resource entities that have a non-null property deletedAt
468-
:param str typeId: filter results by typeId
469468
"""
470469
# pluralize if singular
471470
if not type[-1] == "s":
@@ -491,13 +490,11 @@ def get_resources(self, type: str, accept: str=None, deleted: bool=False, typeId
491490
params["sort"] = "name,asc"
492491
for param in kwargs.keys():
493492
params[param] = kwargs[param]
494-
if typeId is not None:
495-
params['typeId'] = typeId
496493
if deleted:
497494
params['status'] = "DELETED"
498495

499-
if not type in RESOURCES.keys():
500-
raise Exception("ERROR: unknown type \"{}\". Choices: {}".format(type, RESOURCES.keys()))
496+
if not type in NETWORK_RESOURCES.keys():
497+
raise Exception("ERROR: unknown type \"{}\". Choices: {}".format(type, NETWORK_RESOURCES.keys()))
501498
elif type == "edge-routers":
502499
params['embed'] = "host"
503500

@@ -2202,7 +2199,7 @@ def wait_for_property_defined(self, property_name: str, property_type: object=st
22022199
)
22032200
)
22042201

2205-
@docstring_parameters(resource_entity_types=str(RESOURCES.keys()))
2202+
@docstring_parameters(resource_entity_types=str(NETWORK_RESOURCES.keys()))
22062203
def wait_for_entity_name_exists(self, entity_name: str, entity_type: str, wait: int=60, sleep: int=3, progress: bool=False):
22072204
"""Continuously poll until expiry for the expected entity name to exist.
22082205
@@ -2222,10 +2219,10 @@ def wait_for_entity_name_exists(self, entity_name: str, entity_type: str, wait:
22222219
)
22232220
)
22242221

2225-
if not plural(entity_type) in RESOURCES.keys():
2222+
if not plural(entity_type) in NETWORK_RESOURCES.keys():
22262223
raise Exception("ERROR: unknown type \"{type}\". Choices: {choices}".format(
22272224
type=entity_type,
2228-
choices=str(RESOURCES.keys())
2225+
choices=str(NETWORK_RESOURCES.keys())
22292226
))
22302227

22312228
# poll for status until expiry

netfoundry/utility.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ def singular(plural):
193193
}
194194
}
195195

196+
NETWORK_RESOURCES = {key: RESOURCES[key] for key in RESOURCES.keys() if RESOURCES[key]['domain'] == "network"}
197+
196198
# TODO: [MOP-13441] associate locations with a short list of major geographic regions / continents
197199
MAJOR_REGIONS = {
198200
"AWS" : {

0 commit comments

Comments
 (0)