ducky/devices

Paddy 2016-01-02 Parent:a700ede02f91

20:ed1b5ba69551 Go to Latest

ducky/devices/vendor/github.com/pborman/uuid/node.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.

History
1 // Copyright 2011 Google Inc. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 package uuid
7 import "net"
9 var (
10 interfaces []net.Interface // cached list of interfaces
11 ifname string // name of interface being used
12 nodeID []byte // hardware for version 1 UUIDs
13 )
15 // NodeInterface returns the name of the interface from which the NodeID was
16 // derived. The interface "user" is returned if the NodeID was set by
17 // SetNodeID.
18 func NodeInterface() string {
19 return ifname
20 }
22 // SetNodeInterface selects the hardware address to be used for Version 1 UUIDs.
23 // If name is "" then the first usable interface found will be used or a random
24 // Node ID will be generated. If a named interface cannot be found then false
25 // is returned.
26 //
27 // SetNodeInterface never fails when name is "".
28 func SetNodeInterface(name string) bool {
29 if interfaces == nil {
30 var err error
31 interfaces, err = net.Interfaces()
32 if err != nil && name != "" {
33 return false
34 }
35 }
37 for _, ifs := range interfaces {
38 if len(ifs.HardwareAddr) >= 6 && (name == "" || name == ifs.Name) {
39 if setNodeID(ifs.HardwareAddr) {
40 ifname = ifs.Name
41 return true
42 }
43 }
44 }
46 // We found no interfaces with a valid hardware address. If name
47 // does not specify a specific interface generate a random Node ID
48 // (section 4.1.6)
49 if name == "" {
50 if nodeID == nil {
51 nodeID = make([]byte, 6)
52 }
53 randomBits(nodeID)
54 return true
55 }
56 return false
57 }
59 // NodeID returns a slice of a copy of the current Node ID, setting the Node ID
60 // if not already set.
61 func NodeID() []byte {
62 if nodeID == nil {
63 SetNodeInterface("")
64 }
65 nid := make([]byte, 6)
66 copy(nid, nodeID)
67 return nid
68 }
70 // SetNodeID sets the Node ID to be used for Version 1 UUIDs. The first 6 bytes
71 // of id are used. If id is less than 6 bytes then false is returned and the
72 // Node ID is not set.
73 func SetNodeID(id []byte) bool {
74 if setNodeID(id) {
75 ifname = "user"
76 return true
77 }
78 return false
79 }
81 func setNodeID(id []byte) bool {
82 if len(id) < 6 {
83 return false
84 }
85 if nodeID == nil {
86 nodeID = make([]byte, 6)
87 }
88 copy(nodeID, id)
89 return true
90 }
92 // NodeID returns the 6 byte node id encoded in uuid. It returns nil if uuid is
93 // not valid. The NodeID is only well defined for version 1 and 2 UUIDs.
94 func (uuid UUID) NodeID() []byte {
95 if len(uuid) != 16 {
96 return nil
97 }
98 node := make([]byte, 6)
99 copy(node, uuid[10:])
100 return node
101 }