Skip to content

Commit

Permalink
Improve setting org_name in local conf + docs for it
Browse files Browse the repository at this point in the history
  • Loading branch information
romasku committed Dec 8, 2021
1 parent 55565b5 commit a6454e2
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 15 deletions.
2 changes: 1 addition & 1 deletion CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -2003,7 +2003,7 @@ Name | Description|

### neuro config switch-org

Switch the active organization.<br/><br/>ORG\_NAME is the organization name to select. Use "no_org" value to access<br/>current cluster directly instead of as part of some org.
Switch the active organization.<br/><br/>ORG\_NAME is the organization name to select. Use literal "NO_ORG" to switch to<br/>using current cluster directly instead of on behalf of some org.

**Usage:**

Expand Down
6 changes: 3 additions & 3 deletions neuro-cli/docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@ neuro config switch-org [OPTIONS] ORG_NAME
Switch the active organization.

`ORG`_`NAME` is the organization name to
select. Use "no_org" value to access
current cluster directly instead of as
part of some org.
select. Use literal "NO_`ORG`" to switch
to using current cluster directly
instead of on behalf of some org.

#### Options

Expand Down
7 changes: 7 additions & 0 deletions neuro-cli/docs/topic-user-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ The name of active cluster which overrides the global cluster name set by
`neuro config switch-cluster`. Can only be specified in the **local**
configuration file.

**`org-name`**

The name of active organization which overrides the global organization
name set by `neuro config switch-org`. Can only be specified in
the **local** configuration file. Use literal 'NO_ORG' to setup direct
access instead of on behalf of some organization.

**`ps-format`**

Default value for the `neuro ps --format=XXX` option.
Expand Down
4 changes: 2 additions & 2 deletions neuro-cli/src/neuro_cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ async def switch_cluster(root: Root, cluster_name: Optional[str]) -> None:
async def switch_org(root: Root, org_name: Optional[str]) -> None:
"""Switch the active organization.
ORG_NAME is the organization name to select. Use "no_org" value to access
current cluster directly instead of as part of some org.
ORG_NAME is the organization name to select. Use literal "NO_ORG" to switch
to using current cluster directly instead of on behalf of some org.
"""
with root.status("Fetching the list of available cluster/org pairs"):
await root.client.config.fetch()
Expand Down
7 changes: 7 additions & 0 deletions neuro-cli/src/neuro_cli/topics.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@ async def user_config() -> None:
`neuro config switch-cluster`. Can only be specified in the **local**
configuration file.
**`org-name`**
The name of active organization which overrides the global organization
name set by `neuro config switch-org`. Can only be specified in
the **local** configuration file. Use literal 'NO_ORG' to setup direct
access instead of on behalf of some organization.
**`ps-format`**
Default value for the `neuro ps --format=XXX` option.
Expand Down
16 changes: 7 additions & 9 deletions neuro-sdk/src/neuro_sdk/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ class _ConfigRecoveryData:
refresh_token: str


class _Unset:
pass


@rewrite_module
class Config(metaclass=NoPublicConstructor):
def __init__(self, core: _Core, path: Path, plugin_manager: PluginManager) -> None:
Expand Down Expand Up @@ -129,16 +125,18 @@ def cluster_orgs(self) -> List[Optional[str]]:
@property
def org_name(self) -> Optional[str]:
name = self._get_user_org_name()
if isinstance(name, _Unset):
if name == "NO_ORG":
return None
if name is None:
name = self._config_data.org_name
return name

def _get_user_org_name(self) -> Union[str, None, _Unset]:
def _get_user_org_name(self) -> Optional[str]:
config = self._get_user_config()
section = config.get("job")
if section is not None:
return section.get("org-name", _Unset())
return _Unset()
return section.get("org-name")
return None

@property
def _cluster(self) -> Cluster:
Expand Down Expand Up @@ -213,7 +211,7 @@ async def switch_cluster(self, name: str) -> None:
_save(self._config_data, self._path)

async def switch_org(self, name: Optional[str]) -> None:
if not isinstance(self._get_user_org_name(), _Unset):
if self._get_user_org_name() is not None:
raise RuntimeError(
"Cannot switch the project org. Please edit the '.neuro.toml' file."
)
Expand Down
22 changes: 22 additions & 0 deletions neuro-sdk/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,28 @@ async def test_switch_org_local(
assert client.config.org_name == "test-org"


async def test_no_org_local(
monkeypatch: Any,
tmp_path: Path,
make_client: _MakeClient,
multiple_clusters_config: Dict[str, Cluster],
) -> None:
plugin_manager = PluginManager()
plugin_manager.config.define_str("job", "org-name", scope=ConfigScope.LOCAL)
async with make_client(
"https://example.org",
clusters=multiple_clusters_config,
plugin_manager=plugin_manager,
) as client:
proj_dir = tmp_path / "project"
local_dir = proj_dir / "folder"
local_dir.mkdir(parents=True, exist_ok=True)
monkeypatch.chdir(local_dir)
local_conf = proj_dir / ".neuro.toml"
local_conf.write_text(toml.dumps({"job": {"org-name": "NO_ORG"}}))
assert client.config.org_name is None


async def test_check_server_mismatch_clusters(
aiohttp_server: _TestServerFactory, make_client: _MakeClient
) -> None:
Expand Down

0 comments on commit a6454e2

Please sign in to comment.