Skip to content

Commit

Permalink
CASMCMS-8723: Update CFS cli for v3/paging changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Bak committed Oct 17, 2023
1 parent 0d5f32c commit 7926ecf
Show file tree
Hide file tree
Showing 3 changed files with 21,400 additions and 8,236 deletions.
72 changes: 55 additions & 17 deletions cray/modules/cfs/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,40 @@
# pylint: disable=too-many-arguments
import json
import click
import urllib.parse

from cray.constants import FROM_FILE_TAG
from cray.core import option
from cray.generator import _opt_callback
from cray.generator import generate

CURRENT_VERSION = 'v2'
PRESERVE_VERSIONS = True
SWAGGER_OPTS = {
'vocabulary': {
'deleteall': 'deleteall'
}
}

cli = generate(__file__, condense=False, swagger_opts=SWAGGER_OPTS)
cli.commands = cli.commands[CURRENT_VERSION].commands

if PRESERVE_VERSIONS:
cli.commands.update(cli.commands[CURRENT_VERSION].commands)
else:
cli.commands = cli.commands[CURRENT_VERSION].commands


def setup(cfs_cli):
""" Sets up all cfs overrides """
setup_configurations_update(cfs_cli)
setup_sessions_create(cfs_cli)
setup_components_update(cfs_cli)
setup_configurations_update(cfs_cli, "v2")
setup_sessions_create(cfs_cli, "v2")
remove_sessions_update(cfs_cli, "v2")
setup_components_update(cfs_cli, "v2")
setup_configurations_update(cfs_cli, "v3")
setup_sessions_create(cfs_cli, "v3")
remove_sessions_update(cfs_cli, "v3")
setup_components_update(cfs_cli, "v3")
setup_sources(cfs_cli)


# CONFIGURATIONS #
Expand All @@ -85,18 +97,17 @@ def _decorator(configuration_id, file, update_branches, **kwargs):
return _decorator


def setup_configurations_update(cfs_cli):
def setup_configurations_update(cfs_cli, version):
""" Adds the --file and --update-branches parameters for configuration updates """
tmp_swagger_opts = {
'vocabulary': {
'patch': 'patch'
}
}
tmp_cli = generate(__file__, condense=False, swagger_opts=tmp_swagger_opts)
tmp_cli.commands = tmp_cli.commands[CURRENT_VERSION].commands

update_command = tmp_cli.commands['configurations'].commands['update']
patch_command = tmp_cli.commands['configurations'].commands['patch']
update_command = tmp_cli.commands[version].commands['configurations'].commands['update']
patch_command = tmp_cli.commands[version].commands['configurations'].commands['patch']

option(
'--file', callback=_opt_callback, type=str, metavar='TEXT',
Expand All @@ -117,14 +128,18 @@ def setup_configurations_update(cfs_cli):
patch_command.callback
)

cfs_cli.commands['configurations'].commands['update'] = update_command
cfs_cli.commands[version].commands['configurations'].commands['update'] = update_command


# SESSIONS #

# Update session should only be in the api as it is not user friendly and
# is only used by CFS to update session status.
del cli.commands['sessions'].commands['update']
def remove_sessions_update(cfs_cli, version):
"""
Update session should only be in the api as it is not user friendly and
is only used by CFS to update session status.
"""
del cfs_cli.commands[version].commands['sessions'].commands['update']



def _target_groups_callback(cb):
Expand Down Expand Up @@ -201,9 +216,9 @@ def _decorator(
IMAGE_MAP_PAYLOAD = 'target-image-map'


def setup_sessions_create(cfs_cli):
def setup_sessions_create(cfs_cli, version):
""" Adds the --tags and --target-group parameters for session creates """
command = cfs_cli.commands['sessions'].commands['create']
command = cfs_cli.commands[version].commands['sessions'].commands['create']

# Create a new option which can handle multiple groups with individual names
# and member lists. `option` acts as a decorator here.
Expand Down Expand Up @@ -255,7 +270,7 @@ def setup_sessions_create(cfs_cli):

# COMPONENTS #
def create_components_update_shim(func):
""" Callback function to custom create our own payload """
""" Callback function to custombuild create our own payload """

def _decorator(component_id, state, tags, **kwargs):
payload = {v['name']: v['value'] for _, v in kwargs.items() if
Expand All @@ -274,9 +289,9 @@ def _decorator(component_id, state, tags, **kwargs):
return _decorator


def setup_components_update(cfs_cli):
def setup_components_update(cfs_cli, version):
""" Adds the --state and --tags parameters for component updates """
command = cfs_cli.commands['components'].commands['update']
command = cfs_cli.commands[version].commands['components'].commands['update']
option(
'--state',
callback=_opt_callback,
Expand All @@ -301,4 +316,27 @@ def setup_components_update(cfs_cli):
command.callback = create_components_update_shim(command.callback)


# SOURCES #
def _encode_source_name(source_name):
# Quote twice. One level of decoding is automatically done the API framework, so one level of encoding
# produces the same problems as not encoding at all.
source_name = urllib.parse.quote(source_name, safe='')
source_name = urllib.parse.quote(source_name, safe='')
return source_name

def create_sources_shim(func):
""" Callback function to custom create our own payload """
def _decorator(source_id, **kwargs):
source_id = {"name": source_id["name"], "value": _encode_source_name(source_id["value"])}
return func(source_id=source_id, **kwargs)
return _decorator


def setup_sources(cfs_cli):
""" Adds the --state and --tags parameters for component updates """
for command_type in ["update", "delete", "describe"]:
command = cfs_cli.commands["v3"].commands['sources'].commands[command_type]
command.callback = create_sources_shim(command.callback)


setup(cli)
Loading

0 comments on commit 7926ecf

Please sign in to comment.