Skip to content

Commit 9f9aa66

Browse files
authored
Merge pull request #104 from OpenVPN/bugfix/device-crud
Fix CRUD for devices
2 parents 01a37f4 + 1eefa7e commit 9f9aa66

2 files changed

Lines changed: 28 additions & 25 deletions

File tree

cloudconnexa/devices.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,18 @@ func (d *DevicesService) ListAll() ([]DeviceDetail, error) {
136136
}
137137

138138
// GetByID retrieves a specific device by its ID.
139-
func (d *DevicesService) GetByID(deviceID string) (*DeviceDetail, error) {
139+
// userID is sent as the required ?userId= query parameter.
140+
func (d *DevicesService) GetByID(userID, deviceID string) (*DeviceDetail, error) {
141+
if err := validateID(userID); err != nil {
142+
return nil, err
143+
}
140144
if err := validateID(deviceID); err != nil {
141145
return nil, err
142146
}
143-
endpoint := buildURL(d.client.GetV1Url(), "devices", deviceID)
147+
148+
params := url.Values{}
149+
params.Set("userId", userID)
150+
endpoint := fmt.Sprintf("%s?%s", buildURL(d.client.GetV1Url(), "devices", deviceID), params.Encode())
144151
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
145152
if err != nil {
146153
return nil, err
@@ -161,7 +168,11 @@ func (d *DevicesService) GetByID(deviceID string) (*DeviceDetail, error) {
161168
}
162169

163170
// Update updates an existing device by its ID.
164-
func (d *DevicesService) Update(deviceID string, updateRequest DeviceUpdateRequest) (*DeviceDetail, error) {
171+
// userID is sent as the required ?userId= query parameter.
172+
func (d *DevicesService) Update(userID, deviceID string, updateRequest DeviceUpdateRequest) (*DeviceDetail, error) {
173+
if err := validateID(userID); err != nil {
174+
return nil, err
175+
}
165176
if err := validateID(deviceID); err != nil {
166177
return nil, err
167178
}
@@ -170,8 +181,10 @@ func (d *DevicesService) Update(deviceID string, updateRequest DeviceUpdateReque
170181
return nil, err
171182
}
172183

173-
endpoint := buildURL(d.client.GetV1Url(), "devices", deviceID)
174-
req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(requestJSON))
184+
params := url.Values{}
185+
params.Set("userId", userID)
186+
endpoint := fmt.Sprintf("%s?%s", buildURL(d.client.GetV1Url(), "devices", deviceID), params.Encode())
187+
req, err := http.NewRequest(http.MethodPut, endpoint, bytes.NewBuffer(requestJSON))
175188
if err != nil {
176189
return nil, err
177190
}
@@ -219,22 +232,6 @@ func (d *DevicesService) ListByUserID(userID string) ([]DeviceDetail, error) {
219232
return allDevices, nil
220233
}
221234

222-
// UpdateName updates the name of a device.
223-
func (d *DevicesService) UpdateName(deviceID string, name string) (*DeviceDetail, error) {
224-
updateRequest := DeviceUpdateRequest{
225-
Name: name,
226-
}
227-
return d.Update(deviceID, updateRequest)
228-
}
229-
230-
// UpdateDescription updates the description of a device.
231-
func (d *DevicesService) UpdateDescription(deviceID string, description string) (*DeviceDetail, error) {
232-
updateRequest := DeviceUpdateRequest{
233-
Description: description,
234-
}
235-
return d.Update(deviceID, updateRequest)
236-
}
237-
238235
// Create creates a new device for the given user.
239236
// userID is sent as the required ?userId= query parameter.
240237
func (d *DevicesService) Create(userID string, req DeviceCreateRequest) (*DeviceDetail, error) {

cloudconnexa/devices_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ func TestDevicesService_GetByID(t *testing.T) {
9898
if r.URL.Path != "/api/v1/devices/device-123" {
9999
t.Errorf("Expected path /api/v1/devices/device-123, got %s", r.URL.Path)
100100
}
101+
if got := r.URL.Query().Get("userId"); got != "user-123" {
102+
t.Errorf("Expected userId=user-123, got %s", got)
103+
}
101104

102105
// Mock response
103106
device := DeviceDetail{
@@ -115,7 +118,7 @@ func TestDevicesService_GetByID(t *testing.T) {
115118
client := createTestClient(server)
116119

117120
// Test the GetByID method
118-
result, err := client.Devices.GetByID("device-123")
121+
result, err := client.Devices.GetByID("user-123", "device-123")
119122
if err != nil {
120123
t.Fatalf("Expected no error, got %v", err)
121124
}
@@ -133,12 +136,15 @@ func TestDevicesService_Update(t *testing.T) {
133136
// Create a mock server
134137
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
135138
// Check the request method and path
136-
if r.Method != http.MethodPost {
137-
t.Errorf("Expected POST request, got %s", r.Method)
139+
if r.Method != http.MethodPut {
140+
t.Errorf("Expected PUT request, got %s", r.Method)
138141
}
139142
if r.URL.Path != "/api/v1/devices/device-123" {
140143
t.Errorf("Expected path /api/v1/devices/device-123, got %s", r.URL.Path)
141144
}
145+
if got := r.URL.Query().Get("userId"); got != "user-123" {
146+
t.Errorf("Expected userId=user-123, got %s", got)
147+
}
142148

143149
// Mock response
144150
device := DeviceDetail{
@@ -161,7 +167,7 @@ func TestDevicesService_Update(t *testing.T) {
161167
Name: "Updated Device Name",
162168
Description: "Updated description",
163169
}
164-
result, err := client.Devices.Update("device-123", updateRequest)
170+
result, err := client.Devices.Update("user-123", "device-123", updateRequest)
165171
if err != nil {
166172
t.Fatalf("Expected no error, got %v", err)
167173
}

0 commit comments

Comments
 (0)