Skip to content

Commit 3a768d6

Browse files
committed
devices: support querying all fields for individual device
Add 'ListWithAllFields' to 'DevicesResource' to allow getting a device with all of its fields populated. Updates tailscale/corp#23564 Signed-off-by: Percy Wegmann <[email protected]>
1 parent 110cd8a commit 3a768d6

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

devices.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,33 @@ type DevicePostureAttributeRequest struct {
107107
Comment string `json:"comment"`
108108
}
109109

110-
// Get gets the [Device] identified by deviceID.
110+
// GetWithAllFields gets the [Device] identified by `deviceID`.
111+
// All fields will be populated.
112+
//
113+
// Using the device `NodeID` is preferred, but its numeric `ID` value can also be used.
114+
func (dr *DevicesResource) GetWithAllFields(ctx context.Context, deviceID string) (*Device, error) {
115+
return dr.get(ctx, deviceID, true)
116+
}
117+
118+
// Get gets the [Device] identified by `deviceID`.
111119
//
112120
// Using the device `NodeID` is preferred, but its numeric `ID` value can also be used.
113121
func (dr *DevicesResource) Get(ctx context.Context, deviceID string) (*Device, error) {
122+
return dr.get(ctx, deviceID, false)
123+
}
124+
125+
func (dr *DevicesResource) get(ctx context.Context, deviceID string, allFields bool) (*Device, error) {
114126
req, err := dr.buildRequest(ctx, http.MethodGet, dr.buildURL("device", deviceID))
115127
if err != nil {
116128
return nil, err
117129
}
118130

131+
if allFields {
132+
q := req.URL.Query()
133+
q.Set("fields", "all")
134+
req.URL.RawQuery = q.Encode()
135+
}
136+
119137
return body[Device](dr, req)
120138
}
121139

@@ -156,13 +174,13 @@ func (dr *DevicesResource) List(ctx context.Context) ([]Device, error) {
156174
return dr.list(ctx, false)
157175
}
158176

159-
func (dr *DevicesResource) list(ctx context.Context, all bool) ([]Device, error) {
177+
func (dr *DevicesResource) list(ctx context.Context, allFields bool) ([]Device, error) {
160178
req, err := dr.buildRequest(ctx, http.MethodGet, dr.buildTailnetURL("devices"))
161179
if err != nil {
162180
return nil, err
163181
}
164182

165-
if all {
183+
if allFields {
166184
q := req.URL.Query()
167185
q.Set("fields", "all")
168186
req.URL.RawQuery = q.Encode()

devices_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,21 @@ func TestClient_Devices_Get(t *testing.T) {
9494
server.ResponseCode = http.StatusOK
9595
server.ResponseBody = expectedDevice
9696

97-
t.Run("using legacy id", func(t *testing.T) {
97+
t.Run("using legacy id with default fields", func(t *testing.T) {
9898
actualDevice, err := client.Devices().Get(context.Background(), "12345")
9999
assert.NoError(t, err)
100100
assert.Equal(t, http.MethodGet, server.Method)
101101
assert.Equal(t, "/api/v2/device/12345", server.Path)
102+
assert.Empty(t, server.Query.Get("fields"))
102103
assert.EqualValues(t, expectedDevice, actualDevice)
103104
})
104105

105-
t.Run("using preferred nodeId", func(t *testing.T) {
106-
actualDevice, err := client.Devices().Get(context.Background(), "nTESTJ31")
106+
t.Run("using preferred nodeId with all fields", func(t *testing.T) {
107+
actualDevice, err := client.Devices().GetWithAllFields(context.Background(), "nTESTJ31")
107108
assert.NoError(t, err)
108109
assert.Equal(t, http.MethodGet, server.Method)
109110
assert.Equal(t, "/api/v2/device/nTESTJ31", server.Path)
111+
assert.Equal(t, "all", server.Query.Get("fields"))
110112
assert.EqualValues(t, expectedDevice, actualDevice)
111113
})
112114
}

0 commit comments

Comments
 (0)