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.
6 "code.secondbit.org/uuid.hg"
7 "golang.org/x/net/context"
10 // Memstore is an in-memory implementation of Storer, and should
11 // only be used for testing or for temporary local servers.
12 type Memstore struct {
13 devices map[string]Device
17 // NewMemstore returns a Memstore that is ready to be used as a
18 // Storer implementation.
19 func NewMemstore() *Memstore {
21 devices: map[string]Device{},
25 // GetDevices returns any Devices in the Memstore that match the
26 // passed IDs. If an ID cannot be matched to a Device in the
27 // Memstore, it is ignored. The result is a map, with the values
28 // being the Devices that could be found, and the keys being the
29 // result of the String() method for each Device's ID.
31 // An empty map is a possible response, if none of the IDs could
33 func (m *Memstore) GetDevices(ids []uuid.ID, c context.Context) (map[string]Device, error) {
35 defer m.lock.RUnlock()
37 results := map[string]Device{}
39 for _, id := range ids {
40 device, ok := m.devices[id.String()]
44 results[id.String()] = device
49 // UpdateDevice applies the passed DeviceChange to the Device
50 // in the Memstore specified by the DeviceChange's DeviceID
51 // property. If no Device in the Memstore matches the DeviceChange's
52 // DeviceID property, then an ErrDeviceNotFound error will be
54 func (m *Memstore) UpdateDevice(change DeviceChange, c context.Context) error {
58 device, ok := m.devices[change.DeviceID.String()]
60 return ErrDeviceNotFound
63 device = ApplyChange(device, change)
64 m.devices[change.DeviceID.String()] = device
69 // DeleteDevices will remove any Devices from the Memstore that match
70 // the IDs passed in. If an ID can't be matched to a Device in the
71 // Memstore, then it is ignored.
72 func (m *Memstore) DeleteDevices(ids []uuid.ID, c context.Context) error {
76 for _, id := range ids {
77 delete(m.devices, id.String())
83 // CreateDevices stores the passed devices in the Memstore as a single
84 // transaction. If a Device's ID already exists in the Memstore, an
85 // ErrDeviceAlreadyExists error with that Device's ID is returned, and
86 // none of the passed Devices are stored.
87 func (m *Memstore) CreateDevices(devices []Device, c context.Context) error {
91 for _, device := range devices {
92 if _, ok := m.devices[device.ID.String()]; ok {
93 return ErrDeviceAlreadyExists(device.ID)
97 for _, device := range devices {
98 m.devices[device.ID.String()] = device
103 // ListDevicesByOwner returns all the Devices in the Memstore that have an
104 // Owner property that matches the passed ID. If no Devices have an Owner
105 // property matching the passed ID, an empty slice is returned.
107 // ListDevicesByOwner does not guarantee any sort order for the Devices.
108 func (m *Memstore) ListDevicesByOwner(user uuid.ID, c context.Context) ([]Device, error) {
110 for _, device := range m.devices {
111 if !device.Owner.Equal(user) {
114 devices = append(devices, device)