Skip to content

Commit a2568f5

Browse files
hosomoxtoacart
authored andcommitted
Add SetPostureAttribute
Signed-off-by: hosom <[email protected]>
1 parent 7679ab1 commit a2568f5

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

v2/devices.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ type DevicePostureAttributes struct {
7272
Expiries map[string]Time `json:"expiries"`
7373
}
7474

75+
type DevicePostureAttributeRequest struct {
76+
Value any `json:"value"`
77+
Expiry Time `json:"expiry"`
78+
Comment string `json:"comment"`
79+
}
80+
7581
// Get gets the [Device] identified by deviceID.
7682
func (dr *DevicesResource) Get(ctx context.Context, deviceID string) (*Device, error) {
7783
req, err := dr.buildRequest(ctx, http.MethodGet, dr.buildURL("device", deviceID))
@@ -82,6 +88,7 @@ func (dr *DevicesResource) Get(ctx context.Context, deviceID string) (*Device, e
8288
return body[Device](dr, req)
8389
}
8490

91+
// GetPostureAttributes retrieves the posture attributes of the device identified by deviceID.
8592
func (dr *DevicesResource) GetPostureAttributes(ctx context.Context, deviceID string) (*DevicePostureAttributes, error) {
8693
req, err := dr.buildRequest(ctx, http.MethodGet, dr.buildURL("device", deviceID, "attributes"))
8794
if err != nil {
@@ -153,6 +160,16 @@ func (dr *DevicesResource) SetTags(ctx context.Context, deviceID string, tags []
153160
return dr.do(req, nil)
154161
}
155162

163+
// SetPostureAttribute sets the posture attribute of the device identified by deviceID.
164+
func (dr *DevicesResource) SetPostureAttribute(ctx context.Context, deviceID, attributeKey string, request DevicePostureAttributeRequest) error {
165+
req, err := dr.buildRequest(ctx, http.MethodPost, dr.buildURL("device", deviceID, "attributes", attributeKey), requestBody(request))
166+
if err != nil {
167+
return err
168+
}
169+
170+
return dr.do(req, nil)
171+
}
172+
156173
// DeviceKey type represents the properties of the key of an individual device within
157174
// the tailnet.
158175
type DeviceKey struct {

v2/devices_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,32 @@ func TestClient_SetDeviceTags(t *testing.T) {
310310
assert.EqualValues(t, tags, body["tags"])
311311
}
312312

313+
func TestClient_SetDevicePostureAttributes(t *testing.T) {
314+
t.Parallel()
315+
316+
client, server := NewTestHarness(t)
317+
server.ResponseCode = http.StatusOK
318+
server.ResponseBody = nil
319+
320+
const deviceID = "test"
321+
const attributeKey = "custom:test"
322+
323+
setRequest := tsclient.DevicePostureAttributeRequest{
324+
Value: "value",
325+
Expiry: tsclient.Time{time.Date(2022, 2, 10, 11, 50, 23, 0, time.UTC)},
326+
Comment: "test",
327+
}
328+
329+
assert.NoError(t, client.Devices().SetPostureAttribute(context.Background(), deviceID, attributeKey, setRequest))
330+
assert.EqualValues(t, http.MethodPost, server.Method)
331+
assert.EqualValues(t, "/api/v2/device/"+deviceID+"/attributes/"+attributeKey, server.Path)
332+
333+
var receivedRequest tsclient.DevicePostureAttributeRequest
334+
err := json.Unmarshal(server.Body.Bytes(), &receivedRequest)
335+
assert.NoError(t, err)
336+
assert.EqualValues(t, setRequest, receivedRequest)
337+
}
338+
313339
func TestClient_SetDeviceKey(t *testing.T) {
314340
t.Parallel()
315341

@@ -329,8 +355,8 @@ func TestClient_SetDeviceKey(t *testing.T) {
329355
var actual tsclient.DeviceKey
330356
assert.NoError(t, json.Unmarshal(server.Body.Bytes(), &actual))
331357
assert.EqualValues(t, expected, actual)
332-
333358
}
359+
334360
func TestClient_SetDeviceIPv4Address(t *testing.T) {
335361
t.Parallel()
336362

0 commit comments

Comments
 (0)