Skip to content

Commit

Permalink
feat(devices): add missing devices fields
Browse files Browse the repository at this point in the history
A number of the fields on the [devices model](https://tailscale.com/api#model/device) are not present
in the corresponding go struct resulting in those fields being unreachable from the client.
This updates said struct to match the current spec.
  • Loading branch information
ndisidore committed Nov 1, 2024
1 parent bd4d815 commit 3ce4d06
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 25 deletions.
68 changes: 50 additions & 18 deletions v2/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,57 @@ func (t *Time) UnmarshalJSON(data []byte) error {
return nil
}

type DeviceClientConnectivityClientSupports struct {
HairPinning bool `json:"hairPinning"`
IPv6 bool `json:"ipv6"`
PCP bool `json:"pcp"`
PMP bool `json:"pmp"`
UDP bool `json:"udp"`
UPnP bool `json:"upnp"`
}

type DeviceClientConnectivityLocationLatency struct {
Preferred bool `json:"preferred"`
LatencyMs float64 `json:"latencyMs"`
}

type DeviceClientConnectivity struct {
Endpoints []string `json:"endpoints"`
MappingVariesByDestIP bool `json:"mappingVariesByDestIP"`
Latency map[string]DeviceClientConnectivityLocationLatency `json:"latency"`
ClientSupports DeviceClientConnectivityClientSupports `json:"clientSupports"`
}

type DevicePostureIdentity struct {
Disabled bool `json:"disabled"`
SerialNumbers []string `json:"serialNumbers"`
}

type Device struct {
Addresses []string `json:"addresses"`
Name string `json:"name"`
ID string `json:"id"`
Authorized bool `json:"authorized"`
User string `json:"user"`
Tags []string `json:"tags"`
KeyExpiryDisabled bool `json:"keyExpiryDisabled"`
BlocksIncomingConnections bool `json:"blocksIncomingConnections"`
ClientVersion string `json:"clientVersion"`
Created Time `json:"created"`
Expires Time `json:"expires"`
Hostname string `json:"hostname"`
IsExternal bool `json:"isExternal"`
LastSeen Time `json:"lastSeen"`
MachineKey string `json:"machineKey"`
NodeKey string `json:"nodeKey"`
OS string `json:"os"`
UpdateAvailable bool `json:"updateAvailable"`
Addresses []string `json:"addresses"`
Name string `json:"name"`
ID string `json:"id"`
Authorized bool `json:"authorized"`
User string `json:"user"`
Tags []string `json:"tags"`
KeyExpiryDisabled bool `json:"keyExpiryDisabled"`
BlocksIncomingConnections bool `json:"blocksIncomingConnections"`
ClientVersion string `json:"clientVersion"`
Created Time `json:"created"`
Expires Time `json:"expires"`
Hostname string `json:"hostname"`
IsExternal bool `json:"isExternal"`
LastSeen Time `json:"lastSeen"`
MachineKey string `json:"machineKey"`
NodeKey string `json:"nodeKey"`
OS string `json:"os"`
UpdateAvailable bool `json:"updateAvailable"`
EnabledRoutes []string `json:"enabledRoutes"`
AdvertisedRoutes []string `json:"advertisedRoutes"`
ClientConnectivity DeviceClientConnectivity `json:"clientConnectivity"`
TailnetLockError string `json:"tailnetLockError"`
TailnetLockKey string `json:"tailnetLockKey"`
PostureIdentity DevicePostureIdentity `json:"postureIdentity"`
}

// Get gets the [Device] identified by deviceID.
Expand Down
69 changes: 63 additions & 6 deletions v2/devices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ func TestClient_Devices_Get(t *testing.T) {
NodeKey: "nodekey:test",
OS: "windows",
UpdateAvailable: true,
EnabledRoutes: []string{"10.0.0.0/16"},
AdvertisedRoutes: []string{"10.0.0.0/16"},
ClientConnectivity: tsclient.DeviceClientConnectivity{
Endpoints: []string{"199.9.14.201:59128", "192.68.0.21:59128"},
Latency: map[string]tsclient.DeviceClientConnectivityLocationLatency{"Dallas": {LatencyMs: 60.463043}, "New York City": {Preferred: true, LatencyMs: 31.323811}},
MappingVariesByDestIP: false,
ClientSupports: tsclient.DeviceClientConnectivityClientSupports{
HairPinning: false,
IPv6: true,
PCP: false,
PMP: false,
UDP: false,
UPnP: false,
},
},
TailnetLockError: "test",
TailnetLockKey: "test",
PostureIdentity: tsclient.DevicePostureIdentity{
Disabled: false,
SerialNumbers: []string{"test"},
},
}

client, server := NewTestHarness(t)
Expand Down Expand Up @@ -101,6 +122,27 @@ func TestClient_Devices_List(t *testing.T) {
NodeKey: "nodekey:test",
OS: "windows",
UpdateAvailable: true,
EnabledRoutes: []string{"10.0.0.0/16"},
AdvertisedRoutes: []string{"10.0.0.0/16"},
ClientConnectivity: tsclient.DeviceClientConnectivity{
Endpoints: []string{"199.9.14.201:59128", "192.68.0.21:59128"},
Latency: map[string]tsclient.DeviceClientConnectivityLocationLatency{"Dallas": {LatencyMs: 60.463043}, "New York City": {Preferred: true, LatencyMs: 31.323811}},
MappingVariesByDestIP: false,
ClientSupports: tsclient.DeviceClientConnectivityClientSupports{
HairPinning: false,
IPv6: true,
PCP: false,
PMP: false,
UDP: false,
UPnP: false,
},
},
TailnetLockError: "test",
TailnetLockKey: "test",
PostureIdentity: tsclient.DevicePostureIdentity{
Disabled: false,
SerialNumbers: []string{"test"},
},
},
},
}
Expand Down Expand Up @@ -171,12 +213,27 @@ func TestDevices_Unmarshal(t *testing.T) {
LastSeen: tsclient.Time{
time.Date(2022, 4, 15, 13, 25, 21, 0, time.UTC),
},
MachineKey: "mkey:30dc3c061ac8b33fdc6d88a4a67b053b01b56930d78cae0cf7a164411d424c0d",
Name: "foo.example.com",
NodeKey: "nodekey:30dc3c061ac8b33fdc6d88a4a67b053b01b56930d78cae0cf7a164411d424c0d",
OS: "linux",
UpdateAvailable: false,
User: "[email protected]",
MachineKey: "mkey:30dc3c061ac8b33fdc6d88a4a67b053b01b56930d78cae0cf7a164411d424c0d",
Name: "foo.example.com",
NodeKey: "nodekey:30dc3c061ac8b33fdc6d88a4a67b053b01b56930d78cae0cf7a164411d424c0d",
OS: "linux",
UpdateAvailable: false,
User: "[email protected]",
EnabledRoutes: []string{"10.0.0.0/16", "192.168.1.0/24"},
AdvertisedRoutes: []string{"10.0.0.0/16", "192.168.1.0/24"},
ClientConnectivity: tsclient.DeviceClientConnectivity{
Endpoints: []string{"199.9.14.201:59128", "192.68.0.21:59128"},
Latency: map[string]tsclient.DeviceClientConnectivityLocationLatency{"Dallas": {LatencyMs: 60.463043}, "New York City": {Preferred: true, LatencyMs: 31.323811}},
MappingVariesByDestIP: false,
ClientSupports: tsclient.DeviceClientConnectivityClientSupports{
HairPinning: false,
IPv6: true,
PCP: false,
PMP: false,
UDP: false,
UPnP: false,
},
},
},
},
},
Expand Down
34 changes: 33 additions & 1 deletion v2/testdata/devices.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,39 @@
"nodeKey": "nodekey:30dc3c061ac8b33fdc6d88a4a67b053b01b56930d78cae0cf7a164411d424c0d",
"os": "linux",
"updateAvailable": false,
"user": "[email protected]"
"user": "[email protected]",
"enabledRoutes": [
"10.0.0.0/16",
"192.168.1.0/24"
],
"advertisedRoutes": [
"10.0.0.0/16",
"192.168.1.0/24"
],
"clientConnectivity": {
"endpoints": [
"199.9.14.201:59128",
"192.68.0.21:59128"
],
"latency": {
"Dallas": {
"latencyMs": 60.463043
},
"New York City": {
"preferred": true,
"latencyMs": 31.323811
}
},
"mappingVariesByDestIP": false,
"clientSupports": {
"hairPinning": false,
"ipv6": true,
"pcp": false,
"pmp": false,
"udp": false,
"upnp": false
}
}
}
]
}

0 comments on commit 3ce4d06

Please sign in to comment.