ducky/devices
ducky/devices/vendor/code.secondbit.org/uuid.hg/uuid.go
Add updating devices to apiv1. We needed a way to be able to update devices after they were created. This is supported in the devices package, we just needed to expose it using apiv1 endpoints. In doing so, it became apparent that allowing users to change the Owner of their Devices wasn't properly thought through, and pending a reason to use it, I'm just removing it. The biggest issue came when trying to return usable error messages; we couldn't distinguish between "you don't own the device you're trying to update" and "you're not allowed to change the owner of the device". I also couldn't figure out _who should be able to_ change the owner of the device, which is generally an indication that I'm building a feature before I have a use case for it. To support this change, the apiv1.DeviceChange type needed its Owner property removed. I also needed to add deviceFromAPI and devicesFromAPI helpers to return devices.Device types from apiv1.Device types. There's now a new validateDeviceUpdate helper that checks to ensure that a device update request is valid and the user has the appropriate permissions. The createRequest type now accepts a slice of Devices, not a slice of DeviceChanges, because we want to pass the Owner in. A new updateRequest type is created, which accepts a DeviceChange to apply. A new handleUpdateDevice handler is created, which is assigned to the endpoint for PATCH requests against a device ID. It checks that the user is logged in, the Device they're trying to update exists, and that it's a valid update. If all of that is true, the device is updated and the updated device is returned. Finally, we had to add two new scopes to support new functionality: ScopeUpdateOtherUserDevices allows a user to update other user's devices, and ScopeUpdateLastSeen allows a user to update the LastSeen property of a device. Pending some better error messages, this should be a full implementation of updating a device, which leaves only the deletion endpoint to deal with.
| paddy@0 | 1 package uuid |
| paddy@0 | 2 |
| paddy@0 | 3 import ( |
| paddy@0 | 4 "database/sql/driver" |
| paddy@0 | 5 "errors" |
| paddy@0 | 6 |
| paddy@16 | 7 "github.com/pborman/uuid" |
| paddy@0 | 8 ) |
| paddy@0 | 9 |
| paddy@0 | 10 var InvalidIDError = errors.New("Invalid ID format.") |
| paddy@0 | 11 |
| paddy@0 | 12 type ID uuid.UUID |
| paddy@0 | 13 |
| paddy@0 | 14 func NewID() ID { |
| paddy@0 | 15 return ID(uuid.NewRandom()) |
| paddy@0 | 16 } |
| paddy@0 | 17 |
| paddy@0 | 18 func (id ID) String() string { |
| paddy@0 | 19 return uuid.UUID(id).String() |
| paddy@0 | 20 } |
| paddy@0 | 21 |
| paddy@0 | 22 func (id ID) IsZero() bool { |
| paddy@0 | 23 if id == nil { |
| paddy@0 | 24 return true |
| paddy@0 | 25 } |
| paddy@0 | 26 if len(id) == 0 { |
| paddy@0 | 27 return true |
| paddy@0 | 28 } |
| paddy@0 | 29 return false |
| paddy@0 | 30 } |
| paddy@0 | 31 |
| paddy@0 | 32 func (id ID) Copy() ID { |
| paddy@0 | 33 resp, _ := Parse(id.String()) |
| paddy@0 | 34 // ignore the error because they asked for a copy of the ID, they |
| paddy@0 | 35 // never asked if it was valid or not. |
| paddy@0 | 36 // This is, overall, not the most efficient way to do this (we're |
| paddy@0 | 37 // essentially converting to a string and then back again) but the |
| paddy@0 | 38 // computational complexity involved is pretty minor, and it allows |
| paddy@0 | 39 // us to respect the boundaries between the packages, using only the |
| paddy@0 | 40 // exported interfaces to perform a copy. And that seems pretty |
| paddy@0 | 41 // valuable. |
| paddy@0 | 42 return resp |
| paddy@0 | 43 } |
| paddy@0 | 44 |
| paddy@0 | 45 func (id ID) MarshalJSON() ([]byte, error) { |
| paddy@16 | 46 return uuid.UUID(id).MarshalJSON() |
| paddy@0 | 47 } |
| paddy@0 | 48 |
| paddy@0 | 49 func (id ID) Value() (driver.Value, error) { |
| paddy@0 | 50 return id.String(), nil |
| paddy@0 | 51 } |
| paddy@0 | 52 |
| paddy@0 | 53 func (id *ID) Scan(src interface{}) error { |
| paddy@16 | 54 return (*uuid.UUID)(id).Scan(src) |
| paddy@0 | 55 } |
| paddy@0 | 56 |
| paddy@0 | 57 func (id *ID) UnmarshalJSON(in []byte) error { |
| paddy@16 | 58 return (*uuid.UUID)(id).UnmarshalJSON(in) |
| paddy@0 | 59 } |
| paddy@0 | 60 |
| paddy@0 | 61 func Parse(in string) (ID, error) { |
| paddy@0 | 62 id := ID(uuid.Parse(in)) |
| paddy@0 | 63 if id == nil { |
| paddy@0 | 64 return id, InvalidIDError |
| paddy@0 | 65 } |
| paddy@0 | 66 return id, nil |
| paddy@0 | 67 } |
| paddy@0 | 68 |
| paddy@0 | 69 func (id ID) Equal(other ID) bool { |
| paddy@0 | 70 return uuid.Equal(uuid.UUID(id), uuid.UUID(other)) |
| paddy@0 | 71 } |