Skip to content

Commit 3f59dfe

Browse files
oxtoacartrajsinghtech
authored andcommitted
client: support leaving Tailnet empty to use default tailnet
If users leave the Tailnet setting blank, the client will connect to the default tailnet of the credential, using the "-" (dash) tailnet path. Updates tailscale/tailscale#16950 Signed-off-by: Percy Wegmann <[email protected]>
1 parent aa42ae8 commit 3f59dfe

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

client.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ type Client struct {
3030
// APIKey allows specifying an APIKey to use for authentication.
3131
// To use OAuth Client credentials, construct an [http.Client] using [OAuthConfig] and specify that below.
3232
APIKey string
33-
// Tailnet allows specifying a specific Tailnet by name, to which this Client will connect by default.
33+
// Tailnet allows specifying a specific tailnet by name, to which this Client will connect by default.
34+
// If Tailnet is left blank, the client will connect to default tailnet based on the client's credential,
35+
// using the "-" (dash) default tailnet path.
3436
Tailnet string
3537

3638
// HTTP is the [http.Client] to use for requests to the API server.
@@ -92,6 +94,9 @@ func (c *Client) init() {
9294
if c.HTTP == nil {
9395
c.HTTP = &http.Client{Timeout: defaultHttpClientTimeout}
9496
}
97+
if c.Tailnet == "" {
98+
c.Tailnet = "-"
99+
}
95100
c.contacts = &ContactsResource{c}
96101
c.devicePosture = &DevicePostureResource{c}
97102
c.devices = &DevicesResource{c}

client_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,19 @@ func Test_BuildTailnetURL(t *testing.T) {
5252
require.NoError(t, err)
5353
assert.EqualValues(t, expected.String(), actual.String())
5454
}
55+
56+
func Test_BuildTailnetURLDefault(t *testing.T) {
57+
t.Parallel()
58+
59+
base, err := url.Parse("http://example.com")
60+
require.NoError(t, err)
61+
62+
c := &Client{
63+
BaseURL: base,
64+
}
65+
c.init()
66+
actual := c.buildTailnetURL("path")
67+
expected, err := url.Parse("http://example.com/api/v2/tailnet/-/path")
68+
require.NoError(t, err)
69+
assert.EqualValues(t, expected.String(), actual.String())
70+
}

0 commit comments

Comments
 (0)