Skip to content

Commit

Permalink
Wiring of project CLI to API
Browse files Browse the repository at this point in the history
  • Loading branch information
holtgrewe committed May 3, 2021
1 parent affdb95 commit 216652d
Show file tree
Hide file tree
Showing 12 changed files with 254 additions and 81 deletions.
3 changes: 3 additions & 0 deletions sodar_cli/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
"""Code for accessing SODAR Server API."""

from sodar_cli.api import landingzone # noqa: F403, F401
from sodar_cli.api import project # noqa: F403, F401
from sodar_cli.api import samplesheet # noqa: F403, F401
from sodar_cli.api.models import * # noqa: F403, F401
5 changes: 3 additions & 2 deletions sodar_cli/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ class Project:
sodar_uuid: str
title: str
type: str
submit_status: str
roles: typing.Dict[str, RoleAssignment]
roles: typing.Optional[typing.Dict[str, RoleAssignment]] = None
submit_status: typing.Optional[str] = None
parent_uuid: typing.Optional[str] = None
parent: typing.Optional[str] = None
readme: typing.Optional[str] = None
description: typing.Optional[str] = None
Expand Down
2 changes: 1 addition & 1 deletion sodar_cli/landingzone/retrieve.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Implementation of ``varfish-cli landingzone retrieve``."""

import argparse
import sys
import json
import sys
import uuid

import cattr
Expand Down
4 changes: 2 additions & 2 deletions sodar_cli/project/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ def setup_argparse(parser: argparse.ArgumentParser) -> None:
setup_argparse_create(
subparsers.add_parser("create", help="Create project (there is no delete!).")
)
setup_argparse_update(subparsers.add_parser("delete", help="Update project."))
setup_argparse_update(subparsers.add_parser("update", help="Update project."))


def run(config, toml_config, args, parser, subparser):
"""Main entry point for case command."""
if not args.case_cmd: # pragma: nocover
if not args.project_cmd: # pragma: nocover
return run_nocmd(config, args, parser, subparser)
else:
config = ProjectConfig.create(args, config, toml_config)
Expand Down
6 changes: 0 additions & 6 deletions sodar_cli/project/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ class ProjectCreateConfig:
title: str
#: Project type, one of ``"PROJECT"`` or ``"CATEGORY"``
type: str
#: Owner user UUID.
owner_uuid: str
#: Parent category UUID.
parent_uuid: typing.Optional[str] = None
#: Project description.
Expand All @@ -80,7 +78,6 @@ def create(args, project_config, toml_config=None):
project_config=project_config,
title=args.title,
type=args.type,
owner_uuid=args.owner_uuid,
parent_uuid=args.parent_uuid,
description=args.description,
readme=args.readme,
Expand All @@ -103,8 +100,6 @@ class ProjectUpdateConfig:
type: str
#: Parent category UUID.
parent_uuid: str
#: Owner user UUID.
owner_uuid: str
#: Project description.
description: typing.Optional[str] = None
#: Project README, supports markdown.
Expand All @@ -118,7 +113,6 @@ def create(args, project_config, toml_config=None):
project_uuid=args.project_uuid,
title=args.title,
type=args.type,
owner_uuid=args.owner_uuid,
parent_uuid=args.parent_uuid,
description=args.description,
readme=args.readme,
Expand Down
27 changes: 25 additions & 2 deletions sodar_cli/project/create.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
"""Implementation of ``varfish-cli project create``."""

import argparse
import json
import sys

import cattr
from logzero import logger

# from sodar_cli import api
from sodar_cli import api
from sodar_cli.project.config import ProjectCreateConfig


def setup_argparse(parser):
parser.add_argument("--hidden-cmd", dest="project_cmd", default=run, help=argparse.SUPPRESS)
parser.add_argument("--title", default="Project Title", help="Title of the project")
parser.add_argument(
"--type", default="Project Type", help="Type of the project [PROJECT, CATEGORY]"
)
parser.add_argument("--parent-uuid", help="UUID of the parent project, if any")
parser.add_argument("--description", help="Description text, optional")
parser.add_argument("--readme", help="README text, markdown allowed, optional")


def run(config, toml_config, args, _parser, _subparser, file=sys.stdout):
def run(config, toml_config, args, _parser, _subparser, file=None):
"""Run landingzone retrieve command."""
config = ProjectCreateConfig.create(args, config, toml_config)
logger.info("Configuration: %s", config)
logger.info("Creating project")
project = api.Project(
sodar_uuid=None,
title=args.title,
type=args.type,
parent_uuid=args.parent_uuid,
description=args.description,
readme=args.readme,
)
result = api.project.create(
sodar_url=config.project_config.global_config.sodar_server_url,
sodar_api_token=config.project_config.global_config.sodar_api_token,
project=project,
)
print(json.dumps(cattr.unstructure(result)), file=(file or sys.stdout))
11 changes: 9 additions & 2 deletions sodar_cli/project/list.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
"""Implementation of ``varfish-cli project list``."""

import argparse
import json
import sys

import cattr
from logzero import logger

# from sodar_cli import api
from sodar_cli import api
from sodar_cli.project.config import ProjectListConfig


def setup_argparse(parser):
parser.add_argument("--hidden-cmd", dest="project_cmd", default=run, help=argparse.SUPPRESS)


def run(config, toml_config, args, _parser, _subparser, file=sys.stdout):
def run(config, toml_config, args, _parser, _subparser, file=None):
"""Run project list command."""
config = ProjectListConfig.create(args, config, toml_config)
logger.info("Configuration: %s", config)
logger.info("Listing projects")
result = api.project.list_(
sodar_url=config.project_config.global_config.sodar_server_url,
sodar_api_token=config.project_config.global_config.sodar_api_token,
)
print(json.dumps(cattr.unstructure(result)), file=(file or sys.stdout))
12 changes: 10 additions & 2 deletions sodar_cli/project/retrieve.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""Implementation of ``varfish-cli project retrieve``."""

import argparse
import json
import sys
import uuid

import cattr
from logzero import logger

# from sodar_cli import api
from sodar_cli import api
from sodar_cli.project.config import ProjectRetrieveConfig


Expand All @@ -15,8 +17,14 @@ def setup_argparse(parser):
parser.add_argument("project_uuid", help="UUID of the project to retrieve.", type=uuid.UUID)


def run(config, toml_config, args, _parser, _subparser, file=sys.stdout):
def run(config, toml_config, args, _parser, _subparser, file=None):
"""Run project list command."""
config = ProjectRetrieveConfig.create(args, config, toml_config)
logger.info("Configuration: %s", config)
logger.info("Retrieving projects")
result = api.project.retrieve(
sodar_url=config.project_config.global_config.sodar_server_url,
sodar_api_token=config.project_config.global_config.sodar_api_token,
project_uuid=config.project_uuid,
)
print(json.dumps(cattr.unstructure(result)), file=(file or sys.stdout))
30 changes: 27 additions & 3 deletions sodar_cli/project/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,45 @@

import argparse
import sys
import json
import uuid

import cattr
from logzero import logger

# from sodar_cli import api
from sodar_cli import api
from sodar_cli.project.config import ProjectUpdateConfig


def setup_argparse(parser):
parser.add_argument("--hidden-cmd", dest="project_cmd", default=run, help=argparse.SUPPRESS)
parser.add_argument("project_uuid", help="UUID of the project to update.", type=uuid.UUID)
parser.add_argument("--title", default="Project Title", help="Title of the project")
parser.add_argument(
"--type", default="Project Type", help="Type of the project [PROJECT, CATEGORY]"
)
parser.add_argument("--parent-uuid", help="UUID of the parent project, if any")
parser.add_argument("--description", help="Description text, optional")
parser.add_argument("--readme", help="README text, markdown allowed, optional")


def run(config, toml_config, args, _parser, _subparser, file=sys.stdout):
def run(config, toml_config, args, _parser, _subparser, file=None):
"""Run landingzone retrieve command."""
config = ProjectUpdateConfig.create(args, config, toml_config)
logger.info("Configuration: %s", config)
logger.info("Deleting project")
logger.info("Creating project")
project = api.Project(
sodar_uuid=args.project_uuid,
title=args.title,
type=args.type,
parent_uuid=args.parent_uuid,
description=args.description,
readme=args.readme,
)
result = api.project.update(
sodar_url=config.project_config.global_config.sodar_server_url,
sodar_api_token=config.project_config.global_config.sodar_api_token,
project_uuid=config.project_uuid,
project=project,
)
print(json.dumps(cattr.unstructure(result)), file=(file or sys.stdout))
13 changes: 1 addition & 12 deletions tests/test_api_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,11 @@ def test_project_create(requests_mock):


def test_project_update(requests_mock):
# Move landing zone
m_args = {
r_args = {
"sodar_url": "https://sodar.example.com/",
"project_uuid": "46f4d0d7-b446-4a04-99c4-53cbffe952a3",
"sodar_api_token": "token",
}
m_tpl = "%(sodar_url)sprojects/api/submit/move/%(project_uuid)s"
m_result = factories.ProjectFactory(sodar_uuid=m_args["project_uuid"])
requests_mock.register_uri(
"POST",
m_tpl % m_args,
headers={"Authorization": "Token %s" % m_args["sodar_api_token"]},
json=cattr.unstructure(m_result),
)
# Update project.
r_args = m_args.copy()
p_obj = factories.ProjectFactory()
r_args["project"] = p_obj
r_tpl = "%(sodar_url)sproject/api/update/%(project_uuid)s"
Expand Down
Loading

0 comments on commit 216652d

Please sign in to comment.