Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added "create_token" action. #14

Merged
merged 1 commit into from
Aug 2, 2024
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
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
# Change Log for npg_porch Project
<!-- markdownlint-disable MD024 -->
# Change Log for npg_porch_cli Project

The format is based on [Keep a Changelog](http://keepachangelog.com/).
This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added

* Implemented the `create_token` action. Provided the caller has an admin token,
this action generates and returns a new pipeline-specific token.

## [0.1.0] - 2024-07-23

### Added

# Initial project scaffold, code and tests
* Initial project scaffold, code and tests
34 changes: 33 additions & 1 deletion src/npg_porch_cli/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ def list_client_actions() -> list[str]:
return sorted(_PORCH_CLIENT_ACTIONS.keys())


def send(action: PorchAction, pipeline: Pipeline = None) -> dict | list:
def send(
action: PorchAction, pipeline: Pipeline = None, description: str | None = None
) -> dict | list:
"""Sends a request to the porch API server.

Sends a request to the porch API server to perform an action defined
Expand All @@ -174,6 +176,8 @@ def send(action: PorchAction, pipeline: Pipeline = None) -> dict | list:
npg_porch_cli.api.PorchAction object
pipeline:
npg_porch_cli.api.Pipeline object
description:
A description for the new token, optional

Returns:
The server's response is returned as a Python data structure.
Expand All @@ -183,6 +187,8 @@ def send(action: PorchAction, pipeline: Pipeline = None) -> dict | list:
function = _PORCH_CLIENT_ACTIONS[action.action]
if action.action == "list_pipelines":
return function(action=action)
elif action.action == "create_token":
return function(action=action, pipeline=pipeline, description=description)
return function(action=action, pipeline=pipeline)


Expand Down Expand Up @@ -333,13 +339,39 @@ def update_task(action: PorchAction, pipeline: Pipeline):
)


def create_token(action: PorchAction, pipeline: Pipeline, description: str):
"""Creates a new token for the pipeline.

Args:
action:
npg_porch_cli.api.PorchAction object
pipeline:
npg_porch_cli.api.Pipeline object
description:
A short token description

Returns:
A dictionary containing a new token.
"""

if not description:
raise TypeError("Token description should be given")

return send_request(
validate_ca_cert=action.validate_ca_cert,
url=urljoin(action.porch_url, f"pipelines/{pipeline.name}/token/{description}"),
method="POST",
)


_PORCH_CLIENT_ACTIONS = {
"list_tasks": list_tasks,
"list_pipelines": list_pipelines,
"add_pipeline": add_pipeline,
"add_task": add_task,
"claim_task": claim_task,
"update_task": update_task,
"create_token": create_token,
}


Expand Down
13 changes: 11 additions & 2 deletions src/npg_porch_cli/api_cli_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def run():
list_tasks
list_pipelines
add_pipeline
create_token
add_task
claim_task
update_task
Expand All @@ -54,13 +55,15 @@ def run():
`--pipeline_name` is defined, `list_tasks` returns a list of tasks for
this pipeline, otherwise all registered tasks are returned.

All non-list actions require all `--pipeline`, `pipeline_url` and
All non-list actions require `--pipeline`, `pipeline_url` and
`--pipeline_version` defined.

The `add_task` and `update_task` actions require the `--task_json` to
be defined. In addition to this, for the `update_task` action `--status`
should be defined.

The `create_token` action requires that the `--description` is defined.

NPG_PORCH_TOKEN environment variable should be set to the value of
either an admin or project-specific token.

Expand Down Expand Up @@ -94,6 +97,7 @@ def run():
parser.add_argument("--pipeline", type=str, help="Pipeline name, optional")
parser.add_argument("--task_json", type=str, help="Task as JSON, optional")
parser.add_argument("--status", type=str, help="New status to set, optional")
parser.add_argument("--description", type=str, help="Token description, optional")

args = parser.parse_args()

Expand All @@ -110,4 +114,9 @@ def run():
name=args.pipeline, uri=args.pipeline_url, version=args.pipeline_version
)

print(json.dumps(send(action=action, pipeline=pipeline), indent=2))
print(
json.dumps(
send(action=action, pipeline=pipeline, description=args.description),
indent=2,
)
)
31 changes: 29 additions & 2 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def test_listing_actions():
"add_pipeline",
"add_task",
"claim_task",
"create_token",
"list_pipelines",
"list_tasks",
"update_task",
Expand All @@ -76,8 +77,8 @@ def test_porch_action_class(monkeypatch):
PorchAction(porch_url=url, action="list_tools")
assert (
e.value.args[0] == "Action 'list_tools' is not valid. "
"Valid actions: add_pipeline, add_task, claim_task, list_pipelines, "
"list_tasks, update_task"
"Valid actions: add_pipeline, add_task, claim_task, create_token, "
"list_pipelines, list_tasks, update_task"
)

pa = PorchAction(porch_url=url, action="list_tasks")
Expand Down Expand Up @@ -247,3 +248,29 @@ def mock_get_200(*args, **kwargs):
porch_url=url, action="update_task", task_input=task, task_status="DONE"
)
assert send(action=pa, pipeline=p) == response_data

with monkeypatch.context() as mkp:
response_data = {
"name": "p1",
"description": "for my pipeline",
"token": "ccceddd450aaa",
}

def mock_get_200(*args, **kwargs):
return MockPorchResponse(response_data, 200)

mkp.setattr(requests, "request", mock_get_200)

pa = PorchAction(porch_url=url, action="create_token")

error_message = "Token description should be given"
with pytest.raises(TypeError) as e:
send(action=pa, pipeline=p)
assert e.value.args[0] == error_message
with pytest.raises(TypeError) as e:
send(action=pa, pipeline=p, description="")
assert e.value.args[0] == error_message

assert (
send(action=pa, pipeline=p, description="for my pipeline") == response_data
)