Skip to content

Commit

Permalink
tailscale: support set device ipv4 address (#81)
Browse files Browse the repository at this point in the history
* tailscale: support set device ipv4 address

https://github.com/tailscale/tailscale/blob/main/api.md#set-device-ipv4-address
Signed-off-by: Cameron Stokes <cameron@cameronstokes.com>

* add test for SetDeviceIPv4Address

Signed-off-by: Cameron Stokes <cameron@cameronstokes.com>

---------

Signed-off-by: Cameron Stokes <cameron@cameronstokes.com>
  • Loading branch information
clstokes committed May 17, 2024
1 parent 488fd69 commit 4ffdd2d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tailscale/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,20 @@ func (c *Client) SetDeviceKey(ctx context.Context, deviceID string, key DeviceKe
return c.performRequest(req, nil)
}

// SetDeviceIPv4Address sets the Tailscale IPv4 address of the device.
func (c *Client) SetDeviceIPv4Address(ctx context.Context, deviceID string, ipv4Address string) error {
const uriFmt = "/api/v2/device/%s/ip"

req, err := c.buildRequest(ctx, http.MethodPost, fmt.Sprintf(uriFmt, deviceID), requestBody(map[string]string{
"ipv4": ipv4Address,
}))
if err != nil {
return err
}

return c.performRequest(req, nil)
}

// IsNotFound returns true if the provided error implementation is an APIError with a status of 404.
func IsNotFound(err error) bool {
var apiErr APIError
Expand Down
13 changes: 13 additions & 0 deletions tailscale/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,19 @@ func TestClient_SetDeviceKey(t *testing.T) {
assert.EqualValues(t, expected, actual)

}
func TestClient_SetDeviceIPv4Address(t *testing.T) {
t.Parallel()

client, server := NewTestHarness(t)
server.ResponseCode = http.StatusOK

const deviceID = "test"
address := "100.64.0.1"

assert.NoError(t, client.SetDeviceIPv4Address(context.Background(), deviceID, address))
assert.Equal(t, http.MethodPost, server.Method)
assert.EqualValues(t, "/api/v2/device/"+deviceID+"/ip", server.Path)
}

func TestErrorData(t *testing.T) {
t.Parallel()
Expand Down

0 comments on commit 4ffdd2d

Please sign in to comment.