Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions kernelci/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import click
import requests

import kernelci.api
import kernelci.api.helper
import kernelci.config
import kernelci.settings


Expand Down Expand Up @@ -240,3 +243,24 @@ def get_pagination(page_length: int, page_number: int):
f"Page number must be at least 0, got {page_number}"
)
return page_number * page_length, page_length


def get_api(config, api,
secrets: typing.Optional[kernelci.settings.Secrets] = None):
"""Get an API object instance

Return an API object based on the given `api` config name loaded from the
YAML config found in the `config` path or in the `config` dictionary
already loaded.
"""
if not isinstance(config, dict):
config = kernelci.config.load(config)
api_config = config['api'][api]
token = secrets.api.token if secrets else None
return kernelci.api.get_api(api_config, token)


def get_api_helper(*args, **kwargs):
"""Wrapper around get_api() to get an APIHelper object"""
api = get_api(*args, **kwargs)
return kernelci.api.helper.APIHelper(api)
14 changes: 7 additions & 7 deletions kernelci/cli/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

import click

import kernelci.api
import kernelci.config
from . import Args, kci
from . import (
Args,
get_api,
kci,
)


@kci.group(name='api')
Expand All @@ -23,10 +25,8 @@ def kci_api():
@Args.config
@Args.api
@Args.indent
def hello(config, indent, api):
def hello(config, api, indent):
"""Query the API root endpoint"""
configs = kernelci.config.load(config)
api_config = configs['api'][api]
api = kernelci.api.get_api(api_config)
api = get_api(config, api)
data = api.hello()
click.echo(json.dumps(data, indent=indent or None))
36 changes: 12 additions & 24 deletions kernelci/cli/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

import click

import kernelci.api
import kernelci.api.helper
import kernelci.config
from . import Args, kci
from . import (
Args,
get_api,
get_api_helper,
kci,
)


@kci.group(name='event')
Expand All @@ -28,9 +30,7 @@ def kci_event():
@Args.api
def subscribe(config, api, channel, secrets):
"""Subscribe to a Pub/Sub channel"""
configs = kernelci.config.load(config)
api_config = configs['api'][api]
api = kernelci.api.get_api(api_config, secrets.api.token)
api = get_api(config, api, secrets)
sub_id = api.subscribe(channel)
click.echo(sub_id)

Expand All @@ -41,9 +41,7 @@ def subscribe(config, api, channel, secrets):
@Args.api
def unsubscribe(config, api, sub_id, secrets):
"""Unsubscribe from a Pub/Sub channel"""
configs = kernelci.config.load(config)
api_config = configs['api'][api]
api = kernelci.api.get_api(api_config, secrets.api.token)
api = get_api(config, api, secrets)
api.unsubscribe(sub_id)


Expand All @@ -54,9 +52,7 @@ def unsubscribe(config, api, sub_id, secrets):
@click.argument('channel')
def send(config, api, is_json, channel, secrets):
"""Read some data on stdin and send it as an event on a channel"""
configs = kernelci.config.load(config)
api_config = configs['api'][api]
api = kernelci.api.get_api(api_config, secrets.api.token)
api = get_api(config, api, secrets)
data = sys.stdin.read()
if is_json:
data = json.loads(data)
Expand All @@ -70,10 +66,7 @@ def send(config, api, is_json, channel, secrets):
@Args.indent
def receive(config, api, indent, sub_id, secrets):
"""Wait and receive an event from a subscription and print on stdout"""
configs = kernelci.config.load(config)
api_config = configs['api'][api]
api = kernelci.api.get_api(api_config, secrets.api.token)
helper = kernelci.api.helper.APIHelper(api)
helper = get_api_helper(config, api, secrets)
event = helper.receive_event_data(sub_id)
if isinstance(event, str):
click.echo(event.strip())
Expand All @@ -90,9 +83,7 @@ def receive(config, api, indent, sub_id, secrets):
@click.argument('list_name')
def push(config, api, is_json, list_name, secrets):
"""Read some data on stdin and push it as an event on a list"""
configs = kernelci.config.load(config)
api_config = configs['api'][api]
api = kernelci.api.get_api(api_config, secrets.api.token)
api = get_api(config, api, secrets)
data = sys.stdin.read()
if is_json:
data = json.loads(data)
Expand All @@ -106,10 +97,7 @@ def push(config, api, is_json, list_name, secrets):
@Args.indent
def pop(config, api, indent, list_name, secrets):
"""Wait and pop an event from a List when received print on stdout"""
configs = kernelci.config.load(config)
api_config = configs['api'][api]
api = kernelci.api.get_api(api_config, secrets.api.token)
helper = kernelci.api.helper.APIHelper(api)
helper = get_api_helper(config, api, secrets)
event = helper.pop_event_data(list_name)
if isinstance(event, str):
click.echo(event.strip())
Expand Down
19 changes: 10 additions & 9 deletions kernelci/cli/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@

import click

import kernelci.api
import kernelci.config
import kernelci.api.helper
import kernelci.runtime
from . import Args, kci, catch_http_error
from . import (
Args,
catch_http_error,
get_api,
get_api_helper,
kci,
)


@kci.group(name='job')
Expand All @@ -34,11 +38,9 @@ def new(name, node_id, node_json, config, # pylint: disable=too-many-arguments
api, indent, secrets):
"""Create a new job node"""
configs = kernelci.config.load(config)
api_config = configs['api'][api]
api = kernelci.api.get_api(api_config, secrets.api.token)
helper = kernelci.api.helper.APIHelper(api)
helper = get_api_helper(configs, api, secrets)
if node_id:
input_node = api.get_node(node_id)
input_node = helper.api.get_node(node_id)
elif node_json:
input_node = helper.load_json(node_json)
else:
Expand Down Expand Up @@ -69,8 +71,7 @@ def generate(node_id, # pylint: disable=too-many-arguments, too-many-locals
runtime, storage, platform, output, config, api, secrets):
"""Generate a job definition in a file"""
configs = kernelci.config.load(config)
api_config = configs['api'][api]
api = kernelci.api.get_api(api_config, secrets.api.token)
api = get_api(configs, api, secrets)
job_node = api.get_node(node_id)
job = kernelci.runtime.Job(job_node, configs['jobs'][job_node['name']])
job.platform_config = configs['device_types'][platform]
Expand Down
31 changes: 14 additions & 17 deletions kernelci/cli/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@

import click

import kernelci.api
import kernelci.config
from . import Args, kci, split_attributes
from . import (
Args,
get_api,
kci,
split_attributes,
)


@kci.group(name='node')
Expand All @@ -28,9 +31,7 @@ def kci_node():
@Args.indent
def get(node_id, config, api, indent):
"""Get a node with a given ID"""
configs = kernelci.config.load(config)
api_config = configs['api'][api]
api = kernelci.api.get_api(api_config)
api = get_api(config, api)
node = api.get_node(node_id)
click.echo(json.dumps(node, indent=indent))

Expand All @@ -48,10 +49,9 @@ def get(node_id, config, api, indent):
def find(attributes, config, api, # pylint: disable=too-many-arguments
indent, offset, limit):
"""Find nodes with arbitrary attributes"""
configs = kernelci.config.load(config)
api_config = configs['api'][api]
api = kernelci.api.get_api(api_config)
nodes = api.get_nodes(split_attributes(attributes), offset, limit)
api = get_api(config, api)
attributes = split_attributes(attributes)
nodes = api.get_nodes(attributes, offset, limit)
data = json.dumps(nodes, indent=indent)
echo = click.echo_via_pager if len(nodes) > 1 else click.echo
echo(data)
Expand All @@ -63,10 +63,9 @@ def find(attributes, config, api, # pylint: disable=too-many-arguments
@Args.api
def count(attributes, config, api):
"""Count nodes with arbitrary attributes"""
configs = kernelci.config.load(config)
api_config = configs['api'][api]
api = kernelci.api.get_api(api_config)
click.echo(api.count_nodes(split_attributes(attributes)))
api = get_api(config, api)
attributes = split_attributes(attributes)
click.echo(api.count_nodes(attributes))


@kci_node.command(secrets=True)
Expand All @@ -75,9 +74,7 @@ def count(attributes, config, api):
@Args.indent
def submit(config, api, secrets, indent):
"""Submit a new node or update an existing one from stdin"""
configs = kernelci.config.load(config)
api_config = configs['api'][api]
api = kernelci.api.get_api(api_config, secrets.api.token)
api = get_api(config, api, secrets)
data = json.load(sys.stdin)
if 'id' in data:
node = api.update_node(data)
Expand Down
Loading