Skip to content

Commit 6d6ae51

Browse files
authored
fix: add namespace to urls (#342)
* add: get namespace to base command * add namespace to deployment, machines, projects urls * add namespce to job urls * add namespace to experiment urls * lint * check for ok * check for instance * test: update to has calls * fix: add basecommand * fix: add basecommand
1 parent 35d615b commit 6d6ae51

File tree

8 files changed

+55
-26
lines changed

8 files changed

+55
-26
lines changed

gradient/commands/common.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
from click import style
99
from halo import halo
1010

11+
from gradient import api_sdk, exceptions
12+
from gradient.api_sdk import sdk_exceptions
13+
from gradient.api_sdk.config import config
1114
from gradient.clilogger import CliLogger
1215
from gradient.cliutils import get_terminal_lines, TerminalPrinter
16+
from gradient.cli_constants import CLI_PS_CLIENT_NAME
1317
from gradient.exceptions import ApplicationError
1418

1519

@@ -20,6 +24,22 @@ def __init__(self, api_key, logger=CliLogger()):
2024
self.client = self._get_client(api_key, logger)
2125
self.logger = logger
2226

27+
def get_namespace(self):
28+
client = api_sdk.clients.http_client.API(
29+
config.CONFIG_HOST,
30+
api_key=self.api_key,
31+
logger=self.logger,
32+
ps_client_name=CLI_PS_CLIENT_NAME,
33+
)
34+
35+
try:
36+
resp = client.get(url='/teams/namespace')
37+
except sdk_exceptions.GradientSdkError as e:
38+
raise exceptions.ReceivingDataFailedError(e)
39+
if isinstance(resp.json(), str):
40+
return resp.json()
41+
return ''
42+
2343
@abc.abstractmethod
2444
def execute(self, *args, **kwargs):
2545
pass

gradient/commands/datasets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ def update_status():
692692
pool.put(self._delete, url=pre_signed.url)
693693

694694

695-
class ImportDatasetCommand(BaseCreateJobCommandMixin, BaseJobCommand):
695+
class ImportDatasetCommand(BaseCreateJobCommandMixin):
696696
def create_secret(self, key, value, expires_in=86400):
697697
client = api_sdk.clients.SecretsClient(
698698
api_key=self.api_key,

gradient/commands/deployments.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def execute(self, **kwargs):
7777
self.logger.log(self.get_instance_url(deployment_id))
7878

7979
def get_instance_url(self, instance_id):
80-
url = concatenate_urls(config.WEB_URL, "/deployments/{}".format(instance_id))
80+
url = concatenate_urls(config.WEB_URL, "{}/deployments/{}".format(self.get_namespace(), instance_id))
8181
return url
8282

8383
def _handle_auth(self, kwargs):

gradient/commands/experiments.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def _create_tensorboard_with_experiment(self, experiment_id):
8686

8787

8888
@six.add_metaclass(abc.ABCMeta)
89-
class BaseCreateExperimentCommandMixin(object):
89+
class BaseCreateExperimentCommandMixin(BaseCommand):
9090
SPINNER_MESSAGE = "Creating new experiment"
9191
CREATE_SUCCESS_MESSAGE_TEMPLATE = "New experiment created with ID: {}"
9292

@@ -108,7 +108,7 @@ def execute(self, json_, add_to_tensorboard=False):
108108
return experiment_id
109109

110110
def get_instance_url(self, instance_id, project_id):
111-
url = concatenate_urls(config.WEB_URL, "projects/{}/experiments/{}".format(project_id, instance_id))
111+
url = concatenate_urls(config.WEB_URL, "{}/projects/{}/experiments/{}".format(self.get_namespace(), project_id, instance_id))
112112
return url
113113

114114
def _handle_workspace(self, instance_dict):

gradient/commands/jobs.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def _get_client(self, api_key, logger_):
2828

2929

3030
@six.add_metaclass(abc.ABCMeta)
31-
class BaseCreateJobCommandMixin(object):
31+
class BaseCreateJobCommandMixin(BaseJobCommand):
3232
SPINNER_MESSAGE = "Creating new job"
3333
CREATE_SUCCESS_MESSAGE_TEMPLATE = "New job created with ID: {}"
3434

@@ -45,9 +45,8 @@ def execute(self, json_):
4545
self.logger.log(self.CREATE_SUCCESS_MESSAGE_TEMPLATE.format(job_id))
4646
self.logger.log(self.get_instance_url(job_id))
4747

48-
@staticmethod
49-
def get_instance_url(instance_id):
50-
url = concatenate_urls(config.config.WEB_URL, "jobs/{}".format(instance_id))
48+
def get_instance_url(self, instance_id):
49+
url = concatenate_urls(config.config.WEB_URL, "{}/jobs/{}".format(self.get_namespace(), instance_id))
5150
return url
5251

5352
def _handle_workspace(self, instance_dict):
@@ -158,7 +157,7 @@ class JobLogsCommand(LogsCommandMixin, BaseJobCommand):
158157
ENTITY = "Job"
159158

160159

161-
class CreateJobCommand(BaseCreateJobCommandMixin, BaseJobCommand):
160+
class CreateJobCommand(BaseCreateJobCommandMixin):
162161

163162
def _create(self, json_, data):
164163
# because ignore_files is used by workspace handlers and not needed anymore (will fail if not "popped")

gradient/commands/machines.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def execute(self, kwargs):
3333
self.logger.log(self.get_instance_url(handle))
3434

3535
def get_instance_url(self, machine_id):
36-
url = concatenate_urls(config.WEB_URL, "machines/{}".format(machine_id))
36+
url = concatenate_urls(config.WEB_URL, "{}/machines/{}".format(self.get_namespace(), machine_id))
3737
return url
3838

3939

gradient/commands/projects.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def execute(self, project_dict):
3434
self.logger.log(self.get_instance_url(project_id))
3535

3636
def get_instance_url(self, project_id):
37-
url = concatenate_urls(config.WEB_URL, "projects/{}".format(project_id))
37+
url = concatenate_urls(config.WEB_URL, "{}/projects/{}".format(self.get_namespace(), project_id))
3838
return url
3939

4040

tests/functional/test_deployments.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -320,18 +320,23 @@ def test_should_send_proper_data_and_print_message_when_create_deployment_with_z
320320
post_params['workspaceUrl'] = workspace_url
321321

322322
cli_command = self.BASIC_OPTIONS_COMMAND + ['--workspace', workspace_path]
323-
323+
324324
runner = CliRunner()
325325
result = runner.invoke(cli.cli, cli_command)
326326

327327
assert result.output == self.EXPECTED_STDOUT, result.exc_info
328-
get_patched.assert_called_once_with(self.GET_PRESIGNED_URL,
329-
headers=EXPECTED_HEADERS,
330-
json=None,
331-
params={
332-
"contentType": content_type,
333-
"fileName": archive_name,
334-
})
328+
get_patched.assert_has_calls(
329+
[
330+
mock.call(
331+
self.GET_PRESIGNED_URL,
332+
headers=EXPECTED_HEADERS,
333+
json=None,
334+
params={
335+
"contentType": content_type,
336+
"fileName": archive_name,
337+
})
338+
]
339+
)
335340
post_patched.assert_called_once_with(self.URL,
336341
headers=EXPECTED_HEADERS,
337342
json=post_params,
@@ -389,13 +394,18 @@ def test_should_send_proper_data_and_print_message_when_create_deployment_with_w
389394

390395
assert result.output == self.EXPECTED_STDOUT_WITH_WORKSPACE_ARCHIVING, result.exc_info
391396
mocked_archiver.archive.assert_called()
392-
get_patched.assert_called_once_with(self.GET_PRESIGNED_URL,
393-
headers=EXPECTED_HEADERS,
394-
json=None,
395-
params={
396-
"contentType": content_type,
397-
"fileName": archive_name,
398-
})
397+
get_patched.assert_has_calls(
398+
[
399+
mock.call(
400+
self.GET_PRESIGNED_URL,
401+
headers=EXPECTED_HEADERS,
402+
json=None,
403+
params={
404+
"contentType": content_type,
405+
"fileName": archive_name,
406+
})
407+
]
408+
)
399409
post_patched.assert_called_once_with(self.URL,
400410
headers=EXPECTED_HEADERS,
401411
json=post_params,

0 commit comments

Comments
 (0)