Skip to content
This repository was archived by the owner on Aug 11, 2020. It is now read-only.

Commit 4480440

Browse files
committed
Add filtering options to 'deployments list' command
1 parent e4e69a6 commit 4480440

File tree

4 files changed

+45
-21
lines changed

4 files changed

+45
-21
lines changed

paperspace/cli/cli.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,12 +401,22 @@ def create_deployment(api_key=None, **kwargs):
401401
type=ChoiceType(DEPLOYMENT_STATES_MAP, case_sensitive=False),
402402
help="Filter by deployment state",
403403
)
404+
@click.option(
405+
"--projectId",
406+
"projectId",
407+
help="Use to filter by project ID",
408+
)
409+
@click.option(
410+
"--modelId",
411+
"modelId",
412+
help="Use to filter by project ID",
413+
)
404414
@api_key_option
405-
def get_deployments_list(api_key=None, **kwargs):
406-
del_if_value_is_none(kwargs)
415+
def get_deployments_list(api_key=None, **filters):
416+
del_if_value_is_none(filters)
407417
deployments_api = client.API(config.CONFIG_HOST, api_key=api_key)
408418
command = deployments_commands.ListDeploymentsCommand(api=deployments_api)
409-
command.execute(kwargs)
419+
command.execute(filters)
410420

411421

412422
@deployments.command("update", help="Update deployment properties")

paperspace/commands/deployments.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,28 @@ def execute(self, kwargs):
4040

4141

4242
class ListDeploymentsCommand(_DeploymentCommandBase):
43-
def execute(self, kwargs):
44-
json_ = self._get_request_json(kwargs)
43+
def execute(self, filters=None):
44+
json_ = self._get_request_json(filters)
4545
response = self.api.get("/deployments/getDeploymentList/", json=json_)
4646

4747
try:
48-
deployments = self._get_deployments_list(response)
48+
data = response.json()
49+
if not response.ok:
50+
self.logger.log_error_response(data)
51+
return
52+
models = self._get_deployments_list(response)
4953
except (ValueError, KeyError) as e:
5054
self.logger.log("Error while parsing response data: {}".format(e))
5155
else:
52-
self._log_deployments_list(deployments)
56+
self._log_deployments_list(models)
5357

5458
@staticmethod
55-
def _get_request_json(kwargs):
56-
state = kwargs.get("state")
57-
if not state:
59+
def _get_request_json(filters):
60+
if not filters:
5861
return None
5962

60-
params = {"filter": {"where": {"and": [{"state": state}]}}}
61-
return params
63+
json_ = {"filter": {"where": {"and": [filters]}}}
64+
return json_
6265

6366
@staticmethod
6467
def _get_deployments_list(response):

paperspace/commands/models.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def execute(self, filters):
1818
if not response.ok:
1919
self.logger.log_error_response(data)
2020
return
21-
models = data["modelList"]
21+
models = self._get_objects_list(response)
2222
except (ValueError, KeyError) as e:
2323
self.logger.log("Error while parsing response data: {}".format(e))
2424
else:
@@ -32,12 +32,9 @@ def _get_request_json(filters):
3232
json_ = {"filter": {"where": {"and": [filters]}}}
3333
return json_
3434

35-
def _get_models_list(self, response):
36-
if not response.ok:
37-
raise ValueError("Unknown error")
38-
35+
@staticmethod
36+
def _get_objects_list(response):
3937
data = response.json()["modelList"]
40-
self.logger.debug(data)
4138
return data
4239

4340
def _log_models_list(self, models):

tests/functional/test_deployments.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,20 @@ def test_should_send_get_request_and_print_list_of_deployments_filtered_with_sta
187187
params=None)
188188
assert result.output == "No deployments found\n"
189189

190+
@mock.patch("paperspace.cli.cli.deployments_commands.client.requests.get")
191+
def test_should_print_proper_message_when_wrong_api_key_was_used(self, get_patched):
192+
get_patched.return_value = MockResponse({"status": 400, "message": "Invalid API token"},
193+
400)
194+
195+
runner = CliRunner()
196+
result = runner.invoke(cli.cli, self.COMMAND)
197+
198+
get_patched.assert_called_once_with(self.URL,
199+
headers=EXPECTED_HEADERS,
200+
json=None,
201+
params=None)
202+
assert result.output == "Invalid API token\n"
203+
190204

191205
class TestDeploymentsUpdate(object):
192206
URL = "https://api.paperspace.io/deployments/updateDeployment/"
@@ -264,7 +278,7 @@ class TestStartDeployment(object):
264278

265279
@mock.patch("paperspace.cli.cli.deployments_commands.client.requests.post")
266280
def test_should_send_proper_data_and_print_message_when_deployments_start_was_used(self, post_patched):
267-
post_patched.return_value = MockResponse(None, 204, "fake content")
281+
post_patched.return_value = MockResponse(status_code=204)
268282

269283
runner = CliRunner()
270284
result = runner.invoke(cli.cli, self.COMMAND)
@@ -297,7 +311,7 @@ class TestDeleteDeployment(object):
297311

298312
@mock.patch("paperspace.cli.cli.deployments_commands.client.requests.post")
299313
def test_should_send_proper_data_and_print_message_when_deployments_delete_was_used(self, post_patched):
300-
post_patched.return_value = MockResponse(None, 204, "fake content")
314+
post_patched.return_value = MockResponse(status_code=204)
301315

302316
runner = CliRunner()
303317
result = runner.invoke(cli.cli, self.COMMAND)
@@ -311,7 +325,7 @@ def test_should_send_proper_data_and_print_message_when_deployments_delete_was_u
311325

312326
@mock.patch("paperspace.cli.cli.deployments_commands.client.requests.post")
313327
def test_should_send_proper_data_with_custom_api_key_when_api_key_parameter_was_provided(self, post_patched):
314-
post_patched.return_value = MockResponse(None, 204, "fake content")
328+
post_patched.return_value = MockResponse(status_code=204)
315329

316330
runner = CliRunner()
317331
result = runner.invoke(cli.cli, self.COMMAND_WITH_API_KEY)

0 commit comments

Comments
 (0)