Skip to content

Commit

Permalink
added stitch info option to the command line for a workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
abessiari committed Mar 13, 2024
1 parent d39d93d commit 390c3b9
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ fabfed workflow --config-dir some_dir [--var-file some_var_file.yml] --session s
fabfed workflow --config-dir some_dir [--var-file some_var_file.yml] --session some_session -init [-summary] [-json]
fabfed workflow --config-dir some_dir [--var-file some_var_file.yml] --session some_session -stitch-info [-summary] [-json]
fabfed workflow --config-dir some_dir [--var-file some_var_file.yml] --session some_session -plan [-summary] [-json]
fabfed workflow --config-dir some_dir [--var-file some_var_file.yml] --session some_session -apply
Expand Down
14 changes: 14 additions & 0 deletions fabfed/util/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,20 @@ def dump_resources(*, resources, to_json: bool, summary: bool = False):
sys.stdout.write(yaml.dump(resources, Dumper=get_dumper(), default_flow_style=False, sort_keys=False))


def dump_objects(objects, to_json: bool):
import sys

if to_json:
import json

sys.stdout.write(json.dumps(objects, cls=SetEncoder, indent=3))
else:
import yaml
from fabfed.model.state import get_dumper

sys.stdout.write(yaml.dump(objects, Dumper=get_dumper(), default_flow_style=False, sort_keys=False))


def dump_states(states, to_json: bool, summary: bool = False):
import sys
from fabfed.model.state import get_dumper
Expand Down
1 change: 1 addition & 0 deletions fabfed/util/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def build_parser(*, manage_workflow, manage_sessions, display_stitch_info):
help='assembles and validates all .fab files in the config directory')
workflow_parser.add_argument('-apply', action='store_true', default=False, help='create resources')
workflow_parser.add_argument('-init', action='store_true', default=False, help='display resource ordering')
workflow_parser.add_argument('-stitch-info', action='store_true', default=False, help='display network stitch-info')
workflow_parser.add_argument('-plan', action='store_true', default=False, help='shows plan')
workflow_parser.add_argument('-use-remote-policy', action='store_true', default=False, help='use remote policy')
workflow_parser.add_argument('-show', action='store_true', default=False, help='display resource.')
Expand Down
54 changes: 54 additions & 0 deletions tools/fabfed.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,60 @@ def manage_workflow(args):
sutil.dump_resources(resources=controller.resources, to_json=args.json, summary=args.summary)
return

if args.stitch_info:
config = WorkflowConfig.parse(dir_path=config_dir, var_dict=var_dict)
controller = Controller(config=config,
policy=policy,
use_local_policy=not args.use_remote_policy)
states = sutil.load_states(args.session)
controller.init(session=args.session, provider_factory=default_provider_factory, provider_states=states)
resources = controller.resources

from collections import namedtuple

if not args.summary:
stitch_info_details = []

StitchInfoDetails = namedtuple("StitchInfoDetails", "label provider_label stitch_info")

for network in filter(lambda n: n.is_network, resources):
details = StitchInfoDetails(label=network.label,
provider_label=network.provider.label,
stitch_info=network.attributes.get(Constants.RES_STITCH_INFO))
stitch_info_details.append(details)

stitch_info_details = dict(StitchNetworkDetails = stitch_info_details )
sutil.dump_objects(objects=stitch_info_details, to_json=args.json)

NetworkInfo = namedtuple("NetworkInfo", "label provider_label")
StitchInfoSummary = namedtuple("StitchInfoSummary", "network_infos stitch_info")

stitch_info_summaries = []
stitch_info_map = {}
stitch_info_network_info_map = {}


for network in filter(lambda n: n.is_network and n.attributes.get(Constants.RES_STITCH_INFO), resources):
stitch_info = network.attributes.get(Constants.RES_STITCH_INFO)

if stitch_info:
network_info = NetworkInfo(label=network.label, provider_label=network.provider.label)
stitch_port_name = stitch_info.stitch_port['name']
stitch_info_map[stitch_port_name] = stitch_info

if stitch_port_name not in stitch_info_network_info_map:
stitch_info_network_info_map[stitch_port_name] = []

stitch_info_network_info_map[stitch_port_name].append(network_info)

for k, v in stitch_info_network_info_map.items():
stitch_info_summary = StitchInfoSummary(network_infos=v, stitch_info=stitch_info_map[k])
stitch_info_summaries.append(stitch_info_summary)

stitch_info_summaries = dict(StitchInfoSummary = stitch_info_summaries)
sutil.dump_objects(objects=stitch_info_summaries, to_json=args.json)
return

if args.plan:
config = WorkflowConfig.parse(dir_path=config_dir, var_dict=var_dict)
controller = Controller(config=config,
Expand Down

0 comments on commit 390c3b9

Please sign in to comment.