ducky/devices

Paddy 2015-12-14 Parent:1ae5bae472c1

15:c24a6c5fcd8c Go to Latest

ducky/devices/memstore.go

Begin implementation on apiv1. Begin implementing the apiv1 package, which will define the first iteration of our API endpoints and logic. Each API package should be self-contained and able to run without depending on each other. Think of them as interfaces into manipulating the business logic defined in the devices package. The point is to have total control over backwards compatibility, as long as our business logic doesn't change. If that happens, we're in a bad place, but not as bad as it could be. This required us to pull in all our API tools; the api package, its dependencies, the scopeTypes package (so we can define scopes for our API), the trout router, etc. We also updated uuid to the latest, which now includes a license. Hooray? The new apiv1 package consists of a few things: * The devices.go file defines the types the API will use to communicate, along with some helpers to convert from API types to devices types. There's also a stub for validating the device creation requests, which I haven't implemented yet because I'm a pretty bad person. * endpoints.go just contains a helper function that builds our routes and assigns handlers to them, giving us an http.Handler in the returns that we can listen with. * handlers.go defines our HTTP handlers, which will read requests and write responses, after doing the appropriate validation and executing the appropriating business logic. Right now, we only have a handler for creating devices, and it doesn't actually do any validation. Also, we have some user-correctable errors being returned as 500s right now, which is Bad. Fortunately, they're all marked with BUG, so I can at least come back to them. * response.go defines the Response type that will be used for returning information after a request is executed. It may eventually get some helpers, but for now it's pretty basic. * scopes.go defines the Scopes that we're going to be using in the package to control access. It should probably (eventually) include a helper to register the Scopes, or we should have a collector service that pulls in all the packages, finds all their Scopes, and registers them. I haven't decided how I want to manage Scope registration just yet. We exported the getStorer function (now GetStorer) so other packages can use it. I'm not sure how I feel about this just yet. We also had to create a WithStorer helper method that embeds the Storer into a context.Context, so we can bootstrap in devicesd. We erroneously had Created in the DeviceChange struct, but there's no reason the Created property of a Device should ever change, so it was removed from the logic, from the struct, and from the tests. Our CreateMany helper was erroneously creating the un-modified Devices that were passed in, instead of the Devices that had sensible defaults filled. We created a _very minimal_ (e.g., needs some work before it's ready for production) devicesd package that will spin up a simple server, just so we could take a peek at our apiv1 endpoints as they'd actually be used. (It worked. Yay?) We should continue to expand on this with configuration, more information being logged, etc.

History
1 package devices
3 import (
4 "sync"
6 "code.secondbit.org/uuid.hg"
7 "golang.org/x/net/context"
8 )
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
14 lock sync.RWMutex
15 }
17 // NewMemstore returns a Memstore that is ready to be used as a
18 // Storer implementation.
19 func NewMemstore() *Memstore {
20 return &Memstore{
21 devices: map[string]Device{},
22 }
23 }
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.
30 //
31 // An empty map is a possible response, if none of the IDs could
32 // be found.
33 func (m *Memstore) GetDevices(ids []uuid.ID, c context.Context) (map[string]Device, error) {
34 m.lock.RLock()
35 defer m.lock.RUnlock()
37 results := map[string]Device{}
39 for _, id := range ids {
40 device, ok := m.devices[id.String()]
41 if !ok {
42 continue
43 }
44 results[id.String()] = device
45 }
46 return results, nil
47 }
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
53 // returned.
54 func (m *Memstore) UpdateDevice(change DeviceChange, c context.Context) error {
55 m.lock.Lock()
56 defer m.lock.Unlock()
58 device, ok := m.devices[change.DeviceID.String()]
59 if !ok {
60 return ErrDeviceNotFound
61 }
63 device = ApplyChange(device, change)
64 m.devices[change.DeviceID.String()] = device
66 return nil
67 }
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 {
73 m.lock.Lock()
74 defer m.lock.Unlock()
76 for _, id := range ids {
77 delete(m.devices, id.String())
78 }
80 return nil
81 }
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 {
88 m.lock.Lock()
89 defer m.lock.Unlock()
91 for _, device := range devices {
92 if _, ok := m.devices[device.ID.String()]; ok {
93 return ErrDeviceAlreadyExists(device.ID)
94 }
95 }
97 for _, device := range devices {
98 m.devices[device.ID.String()] = device
99 }
100 return nil
101 }
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.
106 //
107 // ListDevicesByOwner does not guarantee any sort order for the Devices.
108 func (m *Memstore) ListDevicesByOwner(user uuid.ID, c context.Context) ([]Device, error) {
109 var devices []Device
110 for _, device := range m.devices {
111 if !device.Owner.Equal(user) {
112 continue
113 }
114 devices = append(devices, device)
115 }
116 return devices, nil
117 }