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

Fix checking org/cluster name validity before re-fetching config #2543

Merged
merged 2 commits into from
Jan 25, 2022
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
2 changes: 2 additions & 0 deletions CHANGELOG.D/2543.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cluster/org name validness now checked after config re-fetch in `neuro config switch-cluster`
and `neuro config switch-org` commands.
12 changes: 12 additions & 0 deletions neuro-cli/src/neuro_cli/click_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,13 +345,18 @@ async def async_shell_complete(
class ClusterType(AsyncType[str]):
name = "cluster"

def __init__(self, allow_unknown: bool = False):
self._allow_unknown = allow_unknown

async def async_convert(
self,
root: Root,
value: str,
param: Optional[click.Parameter],
ctx: Optional[click.Context],
) -> str:
if self._allow_unknown:
return value
client = await root.init_client()
if value not in client.config.clusters:
raise click.BadParameter(
Expand All @@ -373,19 +378,25 @@ async def async_shell_complete(


CLUSTER = ClusterType()
CLUSTER_ALLOW_UNKNOWN = ClusterType(allow_unknown=True)


class OrgType(AsyncType[str]):
name = "org"
NO_ORG_STR = "NO_ORG"

def __init__(self, allow_unknown: bool = False):
self._allow_unknown = allow_unknown

async def async_convert(
self,
root: Root,
value: str,
param: Optional[click.Parameter],
ctx: Optional[click.Context],
) -> str:
if self._allow_unknown:
return value
client = await root.init_client()
org_name = value if value != self.NO_ORG_STR else None
if org_name not in client.config.clusters[client.config.cluster_name].orgs:
Expand All @@ -412,6 +423,7 @@ async def async_shell_complete(


ORG = OrgType()
ORG_ALLOW_UNKNOWN = OrgType(allow_unknown=True)


class JobType(AsyncType[str]):
Expand Down
6 changes: 3 additions & 3 deletions neuro-cli/src/neuro_cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from neuro_cli.formatters.config import ClustersFormatter

from .alias import list_aliases
from .click_types import CLUSTER, ORG
from .click_types import CLUSTER_ALLOW_UNKNOWN, ORG, ORG_ALLOW_UNKNOWN
from .formatters.config import AliasesFormatter, ConfigFormatter
from .root import Root
from .utils import argument, command, group, option
Expand Down Expand Up @@ -226,7 +226,7 @@ async def get_clusters(root: Root) -> None:


@command()
@argument("cluster_name", required=False, default=None, type=CLUSTER)
@argument("cluster_name", required=False, default=None, type=CLUSTER_ALLOW_UNKNOWN)
async def switch_cluster(root: Root, cluster_name: Optional[str]) -> None:
"""Switch the active cluster.

Expand All @@ -252,7 +252,7 @@ async def switch_cluster(root: Root, cluster_name: Optional[str]) -> None:


@command()
@argument("org_name", required=True, type=ORG)
@argument("org_name", required=True, type=ORG_ALLOW_UNKNOWN)
async def switch_org(root: Root, org_name: Optional[str]) -> None:
"""Switch the active organization.

Expand Down
2 changes: 0 additions & 2 deletions neuro-sdk/src/neuro_sdk/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ async def switch_cluster(self, name: str) -> None:
raise RuntimeError(
f"Cluster {name} doesn't exist in "
f"a list of available clusters {list(self.clusters)}. "
f"Please logout and login again."
)
org_name = self.org_name
if org_name not in self.clusters[name].orgs:
Expand All @@ -221,7 +220,6 @@ async def switch_org(self, name: Optional[str]) -> None:
f"Org {name or 'NO_ORG'} doesn't exist in "
f"a list of available orgs {list(cluster_org_names)} for "
f"cluster '{self.cluster_name}'. "
f"Please logout and login again."
)
self.__config_data = replace(self._config_data, org_name=name)
_save(self._config_data, self._path)
Expand Down