ducky/devices
ducky/devices/memstore.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@4 | 1 package devices |
| paddy@4 | 2 |
| paddy@4 | 3 import ( |
| paddy@4 | 4 "sync" |
| paddy@4 | 5 |
| paddy@4 | 6 "code.secondbit.org/uuid.hg" |
| paddy@4 | 7 "golang.org/x/net/context" |
| paddy@4 | 8 ) |
| paddy@4 | 9 |
| paddy@12 | 10 // Memstore is an in-memory implementation of Storer, and should |
| paddy@12 | 11 // only be used for testing or for temporary local servers. |
| paddy@4 | 12 type Memstore struct { |
| paddy@4 | 13 devices map[string]Device |
| paddy@4 | 14 lock sync.RWMutex |
| paddy@4 | 15 } |
| paddy@4 | 16 |
| paddy@12 | 17 // NewMemstore returns a Memstore that is ready to be used as a |
| paddy@12 | 18 // Storer implementation. |
| paddy@4 | 19 func NewMemstore() *Memstore { |
| paddy@4 | 20 return &Memstore{ |
| paddy@4 | 21 devices: map[string]Device{}, |
| paddy@4 | 22 } |
| paddy@4 | 23 } |
| paddy@4 | 24 |
| paddy@12 | 25 // GetDevices returns any Devices in the Memstore that match the |
| paddy@12 | 26 // passed IDs. If an ID cannot be matched to a Device in the |
| paddy@12 | 27 // Memstore, it is ignored. The result is a map, with the values |
| paddy@12 | 28 // being the Devices that could be found, and the keys being the |
| paddy@12 | 29 // result of the String() method for each Device's ID. |
| paddy@12 | 30 // |
| paddy@12 | 31 // An empty map is a possible response, if none of the IDs could |
| paddy@12 | 32 // be found. |
| paddy@4 | 33 func (m *Memstore) GetDevices(ids []uuid.ID, c context.Context) (map[string]Device, error) { |
| paddy@4 | 34 m.lock.RLock() |
| paddy@4 | 35 defer m.lock.RUnlock() |
| paddy@4 | 36 |
| paddy@4 | 37 results := map[string]Device{} |
| paddy@4 | 38 |
| paddy@4 | 39 for _, id := range ids { |
| paddy@4 | 40 device, ok := m.devices[id.String()] |
| paddy@4 | 41 if !ok { |
| paddy@4 | 42 continue |
| paddy@4 | 43 } |
| paddy@4 | 44 results[id.String()] = device |
| paddy@4 | 45 } |
| paddy@4 | 46 return results, nil |
| paddy@4 | 47 } |
| paddy@4 | 48 |
| paddy@12 | 49 // UpdateDevice applies the passed DeviceChange to the Device |
| paddy@12 | 50 // in the Memstore specified by the DeviceChange's DeviceID |
| paddy@12 | 51 // property. If no Device in the Memstore matches the DeviceChange's |
| paddy@12 | 52 // DeviceID property, then an ErrDeviceNotFound error will be |
| paddy@12 | 53 // returned. |
| paddy@4 | 54 func (m *Memstore) UpdateDevice(change DeviceChange, c context.Context) error { |
| paddy@10 | 55 m.lock.Lock() |
| paddy@10 | 56 defer m.lock.Unlock() |
| paddy@10 | 57 |
| paddy@10 | 58 device, ok := m.devices[change.DeviceID.String()] |
| paddy@10 | 59 if !ok { |
| paddy@11 | 60 return ErrDeviceNotFound |
| paddy@10 | 61 } |
| paddy@10 | 62 |
| paddy@10 | 63 device = ApplyChange(device, change) |
| paddy@10 | 64 m.devices[change.DeviceID.String()] = device |
| paddy@10 | 65 |
| paddy@4 | 66 return nil |
| paddy@4 | 67 } |
| paddy@4 | 68 |
| paddy@12 | 69 // DeleteDevices will remove any Devices from the Memstore that match |
| paddy@12 | 70 // the IDs passed in. If an ID can't be matched to a Device in the |
| paddy@12 | 71 // Memstore, then it is ignored. |
| paddy@14 | 72 func (m *Memstore) DeleteDevices(ids []uuid.ID, c context.Context) error { |
| paddy@14 | 73 m.lock.Lock() |
| paddy@14 | 74 defer m.lock.Unlock() |
| paddy@14 | 75 |
| paddy@14 | 76 for _, id := range ids { |
| paddy@14 | 77 delete(m.devices, id.String()) |
| paddy@14 | 78 } |
| paddy@14 | 79 |
| paddy@4 | 80 return nil |
| paddy@4 | 81 } |
| paddy@4 | 82 |
| paddy@12 | 83 // CreateDevices stores the passed devices in the Memstore as a single |
| paddy@12 | 84 // transaction. If a Device's ID already exists in the Memstore, an |
| paddy@12 | 85 // ErrDeviceAlreadyExists error with that Device's ID is returned, and |
| paddy@12 | 86 // none of the passed Devices are stored. |
| paddy@4 | 87 func (m *Memstore) CreateDevices(devices []Device, c context.Context) error { |
| paddy@4 | 88 m.lock.Lock() |
| paddy@4 | 89 defer m.lock.Unlock() |
| paddy@4 | 90 |
| paddy@4 | 91 for _, device := range devices { |
| paddy@5 | 92 if _, ok := m.devices[device.ID.String()]; ok { |
| paddy@5 | 93 return ErrDeviceAlreadyExists(device.ID) |
| paddy@5 | 94 } |
| paddy@5 | 95 } |
| paddy@5 | 96 |
| paddy@5 | 97 for _, device := range devices { |
| paddy@4 | 98 m.devices[device.ID.String()] = device |
| paddy@4 | 99 } |
| paddy@4 | 100 return nil |
| paddy@4 | 101 } |
| paddy@4 | 102 |
| paddy@12 | 103 // ListDevicesByOwner returns all the Devices in the Memstore that have an |
| paddy@12 | 104 // Owner property that matches the passed ID. If no Devices have an Owner |
| paddy@12 | 105 // property matching the passed ID, an empty slice is returned. |
| paddy@12 | 106 // |
| paddy@12 | 107 // ListDevicesByOwner does not guarantee any sort order for the Devices. |
| paddy@4 | 108 func (m *Memstore) ListDevicesByOwner(user uuid.ID, c context.Context) ([]Device, error) { |
| paddy@8 | 109 var devices []Device |
| paddy@8 | 110 for _, device := range m.devices { |
| paddy@8 | 111 if !device.Owner.Equal(user) { |
| paddy@8 | 112 continue |
| paddy@8 | 113 } |
| paddy@8 | 114 devices = append(devices, device) |
| paddy@8 | 115 } |
| paddy@8 | 116 return devices, nil |
| paddy@4 | 117 } |