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

Add Parameter File Option to run cloud CLI command #1582

Merged
merged 6 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ These changes are available in the [master branch](https://github.com/PrefectHQ/
- Replace `DotDict` with `box.Box` - [#1518](https://github.com/PrefectHQ/prefect/pull/1518)
- Store `cached_inputs` on Failed states and call their result handlers if they were provided - [#1557](https://github.com/PrefectHQ/prefect/pull/1557)
- `raise_on_exception` no longer raises for Prefect Signals, as these are typically intentional / for control flow - [#1562](https://github.com/PrefectHQ/prefect/pull/1562)
- `run cloud` CLI command takes in optional `--parameters` as a file path pointing to a JSON file - [#1582](https://github.com/PrefectHQ/prefect/pull/1582)
- Always consider `Constant` tasks successful and unpack them immediately instead of submitting them for execution - [#1527](https://github.com/PrefectHQ/prefect/issues/1527)

### Task Library
Expand Down
26 changes: 19 additions & 7 deletions src/prefect/cli/run.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import time

import click
Expand Down Expand Up @@ -45,6 +46,12 @@ def run():
hidden=True,
)
@click.option("--version", "-v", type=int, help="A flow version to run.", hidden=True)
@click.option(
"--parameters",
help="A parameters JSON file.",
hidden=True,
type=click.Path(exists=True),
)
@click.option(
"--watch",
"-w",
Expand All @@ -55,17 +62,18 @@ def run():
@click.option(
"--logs", "-l", is_flag=True, help="Live logs of the flow run.", hidden=True
)
def cloud(name, project, version, watch, logs):
def cloud(name, project, version, parameters, watch, logs):
"""
Run a deployed flow in Prefect Cloud.

\b
Options:
--name, -n TEXT The name of a flow to run [required]
--project, -p TEXT The name of a project that contains the flow [required]
--version, -v INTEGER A flow version to run
--watch, -w Watch current state of the flow run, stream output to stdout
--logs, -l Get logs of the flow run, stream output to stdout
--name, -n TEXT The name of a flow to run [required]
--project, -p TEXT The name of a project that contains the flow [required]
--version, -v INTEGER A flow version to run
--parameters FILE PATH A filepath of a JSON file containing parameters
--watch, -w Watch current state of the flow run, stream output to stdout
--logs, -l Get logs of the flow run, stream output to stdout
"""

if watch and logs:
Expand Down Expand Up @@ -107,7 +115,11 @@ def cloud(name, project, version, watch, logs):
click.secho("{} not found".format(name), fg="red")
return

flow_run_id = client.create_flow_run(flow_id=flow_id)
loaded_params = None
if parameters:
loaded_params = json.load(open(parameters))
joshmeek marked this conversation as resolved.
Show resolved Hide resolved

flow_run_id = client.create_flow_run(flow_id=flow_id, parameters=loaded_params)
click.echo("Flow Run ID: {}".format(flow_run_id))

if watch:
Expand Down
86 changes: 77 additions & 9 deletions tests/cli/test_run.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import json
import os
import sys
import tempfile
from unittest.mock import MagicMock

import click
Expand Down Expand Up @@ -38,7 +41,7 @@ def test_run_cloud(monkeypatch):
session.return_value.post = post
monkeypatch.setattr("requests.Session", session)

create_flow_run = MagicMock(resurn_value="id")
create_flow_run = MagicMock(return_value="id")
monkeypatch.setattr(
"prefect.client.Client.create_flow_run", MagicMock(return_value=create_flow_run)
)
Expand All @@ -65,9 +68,6 @@ def test_run_cloud(monkeypatch):
assert post.call_args[1]["json"]["query"].split() == query.split()


@pytest.mark.skipif(
sys.version_info < (3, 6), reason="3.5 does not preserve dictionary order"
)
def test_run_cloud_watch(monkeypatch):
post = MagicMock(
return_value=MagicMock(
Expand All @@ -90,7 +90,7 @@ def test_run_cloud_watch(monkeypatch):
session.return_value.post = post
monkeypatch.setattr("requests.Session", session)

create_flow_run = MagicMock(resurn_value="id")
create_flow_run = MagicMock(return_value="id")
monkeypatch.setattr(
"prefect.client.Client.create_flow_run", MagicMock(return_value=create_flow_run)
)
Expand Down Expand Up @@ -118,9 +118,6 @@ def test_run_cloud_watch(monkeypatch):
assert post.called


@pytest.mark.skipif(
sys.version_info < (3, 6), reason="3.5 does not preserve dictionary order"
)
def test_run_cloud_logs(monkeypatch):
post = MagicMock(
return_value=MagicMock(
Expand Down Expand Up @@ -149,7 +146,7 @@ def test_run_cloud_logs(monkeypatch):
session.return_value.post = post
monkeypatch.setattr("requests.Session", session)

create_flow_run = MagicMock(resurn_value="id")
create_flow_run = MagicMock(return_value="id")
monkeypatch.setattr(
"prefect.client.Client.create_flow_run", MagicMock(return_value=create_flow_run)
)
Expand Down Expand Up @@ -195,3 +192,74 @@ def test_run_cloud_fails(monkeypatch):
)
assert result.exit_code == 0
assert "flow not found" in result.output


def test_run_cloud_no_param_file(monkeypatch):
with set_temporary_config(
{"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
):
runner = CliRunner()
result = runner.invoke(
run,
[
"cloud",
"--name",
"flow",
"--project",
"project",
"--version",
"2",
"--parameters",
"no_file.json",
],
)
assert result.exit_code == 2
assert (
'Invalid value for "--parameters": Path "no_file.json" does not exist.'
in result.output
)


def test_run_cloud_param_file(monkeypatch):
post = MagicMock(
return_value=MagicMock(
json=MagicMock(return_value=dict(data=dict(flow=[{"id": "flow"}])))
)
)
session = MagicMock()
session.return_value.post = post
monkeypatch.setattr("requests.Session", session)

mock_client = MagicMock()
mock_client.create_flow_run.return_value = "id"
monkeypatch.setattr("prefect.cli.run.Client", MagicMock(return_value=mock_client))

with tempfile.TemporaryDirectory() as directory:
file_path = os.path.join(directory, "file.json")
with open(file_path, "w") as tmp:
json.dump({"test": 42}, tmp)

with set_temporary_config(
{"cloud.graphql": "http://my-cloud.foo", "cloud.auth_token": "secret_token"}
):
runner = CliRunner()
result = runner.invoke(
run,
[
"cloud",
"--name",
"flow",
"--project",
"project",
"--version",
"2",
"--parameters",
file_path,
],
)
assert result.exit_code == 0
assert "Flow Run ID" in result.output
assert mock_client.create_flow_run.called
assert mock_client.create_flow_run.call_args[1]["parameters"] == {
"test": 42
}