forked from canonical/iot-management
/
device.go
162 lines (134 loc) · 3.8 KB
/
device.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* This file is part of the IoT Management Service
* Copyright 2019 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License version 3, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
* SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package twinapi
import (
"encoding/json"
"path"
"github.com/go-resty/resty/v2"
"github.com/everactive/iot-devicetwin/web"
)
// DeviceList lists the devices for an account
func (a *ClientAdapter) DeviceList(orgID string) web.DevicesResponse {
r := web.DevicesResponse{}
p := path.Join("device", orgID)
resp, err := a.get(a.urlPath(p))
if err != nil {
r.StandardResponse.Message = err.Error()
return r
}
// Parse the response
err = json.Unmarshal(resp.Body(), &r)
if err != nil {
r.StandardResponse.Message = err.Error()
}
return r
}
// DeviceGet fetches a device for an account
func (a *ClientAdapter) DeviceGet(orgID, deviceID string) web.DeviceResponse {
r := web.DeviceResponse{}
p := path.Join("device", orgID, deviceID)
resp, err := a.get(a.urlPath(p))
if err != nil {
r.StandardResponse.Message = err.Error()
return r
}
// Parse the response
err = json.Unmarshal(resp.Body(), &r)
if err != nil {
r.StandardResponse.Message = err.Error()
}
return r
}
// DeviceDelete fetches a device for an account
func (a *ClientAdapter) DeviceDelete(orgID, deviceID string) web.StandardResponse {
r := web.StandardResponse{}
p := path.Join("device", orgID, deviceID)
resp, err := a.delete(a.urlPath(p))
if err != nil {
r.Message = err.Error()
return r
}
// Parse the response
err = json.Unmarshal(resp.Body(), &r)
if err != nil {
r.Message = err.Error()
}
return r
}
// DeviceLogs sends a request to the DeviceTwin REST API for a given devices logs
func (a *ClientAdapter) DeviceLogs(orgID, deviceID string, body []byte) web.StandardResponse {
r := web.StandardResponse{}
p := path.Join("device", orgID, deviceID, "logs")
resp, err := a.post(a.urlPath(p), body)
if err != nil {
r.Code = "DeviceLogs"
r.Message = err.Error()
return r
}
err = json.Unmarshal(resp.Body(), &r)
if err != nil {
r.Code = "DeviceLogs"
r.Message = err.Error()
return r
}
return r
}
type deviceUser struct {
Action string `json:"action,omitempty"`
Email string `json:"email,omitempty"`
ForceManaged bool `json:"force-managed,omitempty"`
Sudoer bool `json:"sudoer,omitempty"`
Username string `json:"username,omitempty"`
}
// DeviceUsersAction sends a user action to a device and decodes it into a StandardResponse
func (a *ClientAdapter) DeviceUsersAction(orgID, deviceID string, body []byte) web.StandardResponse {
r := web.StandardResponse{}
p := path.Join("device", orgID, deviceID, "users")
d := deviceUser{}
var err error
var resp *resty.Response
err = json.Unmarshal(body, &d)
if err != nil {
r.Code = deviceUserLiteral
r.Message = err.Error()
return r
}
switch d.Action {
case "create":
resp, err = a.post(a.urlPath(p), body)
if err != nil {
r.Code = deviceUserLiteral
r.Message = err.Error()
return r
}
case "remove":
resp, err = a.deleteWithBody(a.urlPath(p), body)
if err != nil {
r.Code = "DeviceUser"
r.Message = err.Error()
return r
}
}
err = json.Unmarshal(resp.Body(), &r)
if err != nil {
r.Code = "DeviceUser"
r.Message = err.Error()
return r
}
return r
}