Skip to content
Open
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
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,29 @@ Host mybastion

`~/.annet/context.yml` the same because gnetcli read .ssh/config by default.

## Connecting to an externally-running gnetcli_server

If `gnetcli_server` is already running on a bastion host, set `url` and the
adapter will connect to it instead of spawning a local server binary. `login`
and `password` authenticate to the gnetcli server itself (Basic auth);
`dev_login`/`dev_password` are still the network device credentials.

```yaml
fetcher:
default:
adapter: gnetcli
params: &gnetcli
url: 192.0.2.10:50051
login: gnetcli-user
password: gnetcli-secret
dev_login: mylogin
dev_password: mypassword
deployer:
default:
adapter: gnetcli
params:
<<: *gnetcli
```

When `url` is unset, the adapter starts a local `gnetcli_server` subprocess
(`server_path` defaults to `gnetcli_server` on `$PATH`).
13 changes: 10 additions & 3 deletions src/gnetcli_adapter/gnetcli_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,21 @@ class ApiMaker(metaclass=abc.ABCMeta):

@asynccontextmanager
async def make_api(self) -> AsyncIterator[Gnetcli]:
async with GnetcliStarter(self.conf.server_path, self.conf.server_conf) as gnetcli_url:
yield Gnetcli(
server=gnetcli_url,
def _client(server_url: str) -> Gnetcli:
return Gnetcli(
server=server_url,
auth_token=self.conf.make_server_credentials(),
insecure_grpc=self.conf.insecure_grpc,
user_agent="annet",
)

if self.conf.url:
yield _client(self.conf.url)
return

async with GnetcliStarter(self.conf.server_path, self.conf.server_conf) as gnetcli_url:
yield _client(gnetcli_url)


class GnetcliFetcher(Fetcher, AdapterWithConfig, AdapterWithName, ApiMaker):
def __init__(
Expand Down
17 changes: 17 additions & 0 deletions tests/test_methods.py
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
from __future__ import annotations

import asyncio
from unittest.mock import patch

from gnetcli_adapter.gnetcli_adapter import GnetcliFetcher


def test_make_api_uses_url_without_starter():
fetcher = GnetcliFetcher(url="127.0.0.1:50050")

async def run():
with patch("gnetcli_adapter.gnetcli_adapter.GnetcliStarter") as starter:
async with fetcher.make_api() as api:
assert api._server == "127.0.0.1:50050"
starter.assert_not_called()

asyncio.run(run())
Loading