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

Commit ee7cf30

Browse files
committed
Add --apiKey for deployments commands
1 parent a30c50f commit ee7cf30

File tree

3 files changed

+139
-12
lines changed

3 files changed

+139
-12
lines changed

paperspace/cli.py

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,14 @@ def deployments():
367367
type=int,
368368
required=True,
369369
)
370-
def create_deployment(**kwargs):
370+
@click.option(
371+
"--apiKey",
372+
"api_key",
373+
)
374+
def create_deployment(api_key=None, **kwargs):
371375
del_if_value_is_none(kwargs)
372-
command = deployments_commands.CreateDeploymentCommand()
376+
deployments_api = client.API(config.CONFIG_HOST, api_key=api_key)
377+
command = deployments_commands.CreateDeploymentCommand(api=deployments_api)
373378
command.execute(kwargs)
374379

375380

@@ -392,8 +397,13 @@ def create_deployment(**kwargs):
392397
"state",
393398
type=ChoiceType(DEPLOYMENT_STATES_MAP, case_sensitive=False)
394399
)
395-
def get_deployments_list(**kwargs):
396-
command = deployments_commands.ListDeploymentsCommand()
400+
@click.option(
401+
"--apiKey",
402+
"api_key",
403+
)
404+
def get_deployments_list(api_key=None, **kwargs):
405+
deployments_api = client.API(config.CONFIG_HOST, api_key=api_key)
406+
command = deployments_commands.ListDeploymentsCommand(api=deployments_api)
397407
command.execute(kwargs)
398408

399409

@@ -423,9 +433,14 @@ def get_deployments_list(**kwargs):
423433
"--instanceCount",
424434
"instanceCount",
425435
)
426-
def update_deployment_model(id=None, **kwargs):
436+
@click.option(
437+
"--apiKey",
438+
"api_key",
439+
)
440+
def update_deployment_model(id=None, api_key=None, **kwargs):
427441
del_if_value_is_none(kwargs)
428-
command = deployments_commands.UpdateModelCommand()
442+
deployments_api = client.API(config.CONFIG_HOST, api_key=api_key)
443+
command = deployments_commands.UpdateModelCommand(api=deployments_api)
429444
command.execute(id, kwargs)
430445

431446

@@ -435,8 +450,13 @@ def update_deployment_model(id=None, **kwargs):
435450
"id",
436451
required=True,
437452
)
438-
def start_deployment(id):
439-
command = deployments_commands.StartDeploymentCommand()
453+
@click.option(
454+
"--apiKey",
455+
"api_key",
456+
)
457+
def start_deployment(id, api_key=None):
458+
deployments_api = client.API(config.CONFIG_HOST, api_key=api_key)
459+
command = deployments_commands.StartDeploymentCommand(api=deployments_api)
440460
command.execute(id)
441461

442462

@@ -446,6 +466,11 @@ def start_deployment(id):
446466
"id",
447467
required=True,
448468
)
449-
def delete_deployment(id):
450-
command = deployments_commands.DeleteDeploymentCommand()
469+
@click.option(
470+
"--apiKey",
471+
"api_key",
472+
)
473+
def delete_deployment(id, api_key=None):
474+
deployments_api = client.API(config.CONFIG_HOST, api_key=api_key)
475+
command = deployments_commands.DeleteDeploymentCommand(api=deployments_api)
451476
command.execute(id)

tests/functional/test_deployments.py

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import mock
22
from click.testing import CliRunner
33

4+
import paperspace.client
45
from paperspace import cli
56
from paperspace.commands import deployments as deployments_commands
67
from tests import example_responses, MockResponse
@@ -10,6 +11,8 @@
1011

1112
class TestDeploymentsCreate(object):
1213
URL = "https://api.paperspace.io/deployments/createDeployment/"
14+
EXPECTED_HEADERS_WITH_CHANGED_API_KEY = paperspace.client.default_headers.copy()
15+
EXPECTED_HEADERS_WITH_CHANGED_API_KEY["X-API-Key"] = "some_key"
1316
BASIC_OPTIONS_COMMAND = [
1417
"deployments", "create",
1518
"--deploymentType", "tfserving",
@@ -19,6 +22,16 @@ class TestDeploymentsCreate(object):
1922
"--imageUrl", "https://www.latlmes.com/breaking/paperspace-now-has-a-100-bilion-valuation",
2023
"--instanceCount", "666",
2124
]
25+
BASIC_OPTIONS_COMMAND_WITH_API_KEY = [
26+
"deployments", "create",
27+
"--deploymentType", "tfserving",
28+
"--modelId", "some_model_id",
29+
"--name", "some_name",
30+
"--machineType", "HAL9000",
31+
"--imageUrl", "https://www.latlmes.com/breaking/paperspace-now-has-a-100-bilion-valuation",
32+
"--instanceCount", "666",
33+
"--apiKey", "some_key",
34+
]
2235
BASIC_OPTIONS_REQUEST = {
2336
"machineType": u"HAL9000",
2437
"name": u"some_name",
@@ -48,6 +61,20 @@ def test_should_send_proper_data_and_print_message_when_create_deployment_with_b
4861
assert result.output == self.EXPECTED_STDOUT
4962
assert result.exit_code == 0
5063

64+
@mock.patch("paperspace.cli.deployments_commands.client.requests.post")
65+
def test_should_send_different_api_key_when_api_key_parameter_was_used(self, post_patched):
66+
post_patched.return_value = MockResponse(self.RESPONSE_JSON_200, 200, "fake content")
67+
68+
runner = CliRunner()
69+
result = runner.invoke(cli.cli, self.BASIC_OPTIONS_COMMAND_WITH_API_KEY)
70+
71+
post_patched.assert_called_once_with(self.URL,
72+
headers=self.EXPECTED_HEADERS_WITH_CHANGED_API_KEY,
73+
json=self.BASIC_OPTIONS_REQUEST,
74+
params=None)
75+
assert result.output == self.EXPECTED_STDOUT
76+
assert result.exit_code == 0
77+
5178
@mock.patch("paperspace.cli.deployments_commands.client.requests.post")
5279
def test_should_send_proper_data_and_print_message_when_create_wrong_model_id_was_given(self, post_patched):
5380
post_patched.return_value = MockResponse(self.RESPONSE_JSON_404_MODEL_NOT_FOUND, 404,
@@ -65,8 +92,15 @@ def test_should_send_proper_data_and_print_message_when_create_wrong_model_id_wa
6592

6693

6794
class TestDeploymentsList(object):
95+
URL = "https://api.paperspace.io/deployments/getDeploymentList/"
96+
6897
COMMAND = ["deployments", "list"]
6998
LIST_JSON = example_responses.LIST_DEPLOYMENTS
99+
100+
COMMAND_WITH_API_KEY = ["deployments", "list", "--apiKey", "some_key"]
101+
EXPECTED_HEADERS_WITH_CHANGED_API_KEY = paperspace.client.default_headers.copy()
102+
EXPECTED_HEADERS_WITH_CHANGED_API_KEY["X-API-Key"] = "some_key"
103+
70104
COMMAND_WITH_FILTER_WITH_STATE = ["deployments", "list", "--state", "Stopped"]
71105
LIST_WITH_FILTER_REQUEST_JSON = {"filter": {"where": {"and": [{"state": "Stopped"}]}}}
72106
LIST_WITH_FILTER_RESPONSE_JSON_WHEN_NO_DEPLOYMENTS_FOUND = {"deploymentList": [], "total": 17, "displayTotal": 0,
@@ -89,6 +123,23 @@ def test_should_send_get_request_and_print_list_of_deployments(self, get_patched
89123
runner = CliRunner()
90124
result = runner.invoke(cli.cli, self.COMMAND)
91125

126+
get_patched.assert_called_once_with(self.URL,
127+
headers=EXPECTED_HEADERS,
128+
json=None,
129+
params=None)
130+
assert result.output == self.DETAILS_STDOUT
131+
132+
@mock.patch("paperspace.cli.deployments_commands.client.requests.get")
133+
def test_should_send_get_request_with_custom_api_key_when_api_key_parameter_was_provided(self, get_patched):
134+
get_patched.return_value = MockResponse(self.LIST_JSON, 200, "fake content")
135+
136+
runner = CliRunner()
137+
result = runner.invoke(cli.cli, self.COMMAND_WITH_API_KEY)
138+
139+
get_patched.assert_called_once_with(self.URL,
140+
headers=self.EXPECTED_HEADERS_WITH_CHANGED_API_KEY,
141+
json=None,
142+
params=None)
92143
assert result.output == self.DETAILS_STDOUT
93144

94145
@mock.patch("paperspace.cli.deployments_commands.pydoc")
@@ -101,6 +152,10 @@ def test_should_send_get_request_and_paginate_list_when_output_table_len_is_gt_l
101152
runner = CliRunner()
102153
result = runner.invoke(cli.cli, self.COMMAND)
103154

155+
get_patched.assert_called_once_with(self.URL,
156+
headers=EXPECTED_HEADERS,
157+
json=None,
158+
params=None)
104159
pydoc_patched.pager.assert_called_once()
105160
assert result.exit_code == 0
106161

@@ -126,6 +181,10 @@ def test_should_send_get_request_and_print_list_of_deployments_filtered_with_sta
126181
runner = CliRunner()
127182
result = runner.invoke(cli.cli, self.COMMAND_WITH_FILTER_WITH_STATE)
128183

184+
get_patched.assert_called_once_with(self.URL,
185+
headers=EXPECTED_HEADERS,
186+
json=self.LIST_WITH_FILTER_REQUEST_JSON,
187+
params=None)
129188
assert result.output == "No deployments found\n"
130189

131190

@@ -138,6 +197,15 @@ class TestDeploymentsUpdate(object):
138197
]
139198
BASIC_OPTIONS_REQUEST_JSON = {"upd": {"name": u"new_name"}, "id": u"some_id"}
140199

200+
COMMAND_WITH_API_KEY = [
201+
"deployments", "update",
202+
"--id", "some_id",
203+
"--name", "new_name",
204+
"--apiKey", "some_key"
205+
]
206+
EXPECTED_HEADERS_WITH_CHANGED_API_KEY = paperspace.client.default_headers.copy()
207+
EXPECTED_HEADERS_WITH_CHANGED_API_KEY["X-API-Key"] = "some_key"
208+
141209
RESPONSE_JSON_200 = {"upd": {"name": u"asd"}, "id": u"dennaw0wzbvvg3"}
142210
EXPECTED_STDOUT = "Deployment model updated.\n"
143211

@@ -158,6 +226,20 @@ def test_should_send_proper_data_and_print_message_when_update_deployment(self,
158226
assert result.output == self.EXPECTED_STDOUT
159227
assert result.exit_code == 0
160228

229+
@mock.patch("paperspace.cli.deployments_commands.client.requests.post")
230+
def test_should_send_proper_data_with_custom_api_key_when_api_key_parameter_was_provided(self, post_patched):
231+
post_patched.return_value = MockResponse(self.RESPONSE_JSON_200, 200, "fake content")
232+
233+
runner = CliRunner()
234+
result = runner.invoke(cli.cli, self.COMMAND_WITH_API_KEY)
235+
236+
post_patched.assert_called_once_with(self.URL,
237+
headers=self.EXPECTED_HEADERS_WITH_CHANGED_API_KEY,
238+
json=self.BASIC_OPTIONS_REQUEST_JSON,
239+
params=None)
240+
assert result.output == self.EXPECTED_STDOUT
241+
assert result.exit_code == 0
242+
161243
@mock.patch("paperspace.cli.deployments_commands.client.requests.post")
162244
def test_should_send_proper_data_and_print_message_when_update_deployment_used_with_wrong_id(self, post_patched):
163245
post_patched.return_value = MockResponse(self.RESPONSE_JSON_400, 400, "fake content")
@@ -199,10 +281,17 @@ class TestDeleteDeployment(object):
199281
URL = "https://api.paperspace.io/deployments/updateDeployment/"
200282
COMMAND = ["deployments", "delete",
201283
"--id", "some_id"]
202-
203284
REQUEST_JSON = {"upd": {"isDeleted": True}, "id": u"some_id"}
204285
EXPECTED_STDOUT = "Deployment deleted.\n"
205286

287+
COMMAND_WITH_API_KEY = [
288+
"deployments", "delete",
289+
"--id", "some_id",
290+
"--apiKey", "some_key",
291+
]
292+
EXPECTED_HEADERS_WITH_CHANGED_API_KEY = paperspace.client.default_headers.copy()
293+
EXPECTED_HEADERS_WITH_CHANGED_API_KEY["X-API-Key"] = "some_key"
294+
206295
RESPONSE_JSON_400 = {"error": {"name": "Error", "status": 400, "message": "Unable to access deployment"}}
207296
EXPECTED_STDOUT_WITH_WRONG_ID = "Unable to access deployment\n"
208297

@@ -220,6 +309,20 @@ def test_should_send_proper_data_and_print_message_when_deployments_delete_was_u
220309
assert result.output == self.EXPECTED_STDOUT
221310
assert result.exit_code == 0
222311

312+
@mock.patch("paperspace.cli.deployments_commands.client.requests.post")
313+
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")
315+
316+
runner = CliRunner()
317+
result = runner.invoke(cli.cli, self.COMMAND_WITH_API_KEY)
318+
319+
post_patched.assert_called_once_with(self.URL,
320+
headers=self.EXPECTED_HEADERS_WITH_CHANGED_API_KEY,
321+
json=self.REQUEST_JSON,
322+
params=None)
323+
assert result.output == self.EXPECTED_STDOUT
324+
assert result.exit_code == 0
325+
223326
@mock.patch("paperspace.cli.deployments_commands.client.requests.post")
224327
def test_should_send_proper_data_and_print_message_when_deployments_delete_used_with_wrong_id(self, post_patched):
225328
post_patched.return_value = MockResponse(self.RESPONSE_JSON_400, 400, "fake content")

tests/functional/test_experiments.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import paperspace.client
55
from paperspace import cli, constants
6-
from paperspace.commands import experiments as experiments_commands
76
from tests import example_responses, MockResponse
87

98

0 commit comments

Comments
 (0)