forked from amenzhinsky/iothub
/
registry.go
282 lines (237 loc) · 10.6 KB
/
registry.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package iotservice
import (
"errors"
"time"
)
type MethodCall struct {
MethodName string `json:"methodName,omitempty"`
ConnectTimeout uint `json:"connectTimeoutInSeconds,omitempty"`
ResponseTimeout uint `json:"responseTimeoutInSeconds,omitempty"`
Payload map[string]interface{} `json:"payload,omitempty"`
}
type MethodResult struct {
Status int `json:"status,omitempty"`
Payload map[string]interface{} `json:"payload,omitempty"`
}
type DeviceStatus string
const (
Enabled DeviceStatus = "enabled"
Disabled DeviceStatus = "disabled"
)
type ConnectionState string
const (
Connected ConnectionState = "Connected"
Disconnected ConnectionState = "Disconnected"
)
type Device struct {
DeviceID string `json:"deviceId,omitempty"`
GenerationID string `json:"generationId,omitempty"`
ETag string `json:"etag,omitempty"`
ConnectionState ConnectionState `json:"connectionState,omitempty"`
Status DeviceStatus `json:"status,omitempty"`
StatusReason string `json:"statusReason,omitempty"`
ConnectionStateUpdatedTime *MicrosoftTime `json:"connectionStateUpdatedTime,omitempty"`
StatusUpdatedTime *MicrosoftTime `json:"statusUpdatedTime,omitempty"`
LastActivityTime *MicrosoftTime `json:"lastActivityTime,omitempty"`
CloudToDeviceMessageCount uint `json:"cloudToDeviceMessageCount,omitempty"`
Authentication *Authentication `json:"authentication,omitempty"`
Capabilities map[string]interface{} `json:"capabilities,omitempty"`
Tags map[string]interface{} `json:"tags,omitempty"`
Properties *Properties `json:"properties,omitempty"`
}
type PurgeMessageQueueResult struct {
DeviceID string `json:"deviceID,omitempty"`
ModuleID string `json:"moduleID,omitempty"`
TotalMessagesPurged int `json:"totalMessagesPurged,omitempty"`
}
type Module struct {
ModuleID string `json:"moduleId,omitempty"`
DeviceID string `json:"deviceId,omitempty"`
GenerationID string `json:"generationId,omitempty"`
ETag string `json:"etag,omitempty"`
ConnectionState ConnectionState `json:"connectionState,omitempty"`
ConnectionStateUpdatedTime *MicrosoftTime `json:"connectionStateUpdatedTime,omitempty"`
LastActivityTime *MicrosoftTime `json:"lastActivityTime,omitempty"`
CloudToDeviceMessageCount uint `json:"cloudToDeviceMessageCount,omitempty"`
Authentication *Authentication `json:"authentication,omitempty"`
ManagedBy string `json:"managedBy,omitempty"`
}
type Authentication struct {
SymmetricKey *SymmetricKey `json:"symmetricKey,omitempty"`
X509Thumbprint *X509Thumbprint `json:"x509Thumbprint,omitempty"`
Type AuthType `json:"type,omitempty"`
}
// AuthType is device authentication type.
type AuthType string
const (
// AuthSAS uses symmetric keys to sign requests.
AuthSAS AuthType = "sas"
// AuthSelfSigned self signed certificate with a thumbprint.
AuthSelfSigned AuthType = "selfSigned"
// AuthCA certificate signed by a registered certificate authority.
AuthCA AuthType = "certificateAuthority"
)
type X509Thumbprint struct {
PrimaryThumbprint string `json:"primaryThumbprint,omitempty"`
SecondaryThumbprint string `json:"secondaryThumbprint,omitempty"`
}
type SymmetricKey struct {
PrimaryKey string `json:"primaryKey,omitempty"`
SecondaryKey string `json:"secondaryKey,omitempty"`
}
type Twin struct {
DeviceID string `json:"deviceId,omitempty"`
ETag string `json:"etag,omitempty"`
DeviceETag string `json:"deviceEtag,omitempty"`
Status DeviceStatus `json:"status,omitempty"`
StatusReason string `json:"statusReason,omitempty"`
StatusUpdateTime *MicrosoftTime `json:"statusUpdateTime,omitempty"`
ConnectionState ConnectionState `json:"connectionState,omitempty"`
LastActivityTime *MicrosoftTime `json:"lastActivityTime,omitempty"`
CloudToDeviceMessageCount uint `json:"cloudToDeviceMessageCount,omitempty"`
AuthenticationType string `json:"authenticationType,omitempty"`
X509Thumbprint *X509Thumbprint `json:"x509Thumbprint,omitempty"`
Version int `json:"version,omitempty"`
Tags map[string]interface{} `json:"tags,omitempty"`
Properties *Properties `json:"properties,omitempty"`
Capabilities map[string]interface{} `json:"capabilities,omitempty"`
}
type ModuleTwin struct {
DeviceID string `json:"deviceId,omitempty"`
ModuleID string `json:"moduleId,omitempty"`
ETag string `json:"etag,omitempty"`
DeviceETag string `json:"deviceEtag,omitempty"`
Status DeviceStatus `json:"status,omitempty"`
StatusUpdateTime *MicrosoftTime `json:"statusUpdateTime,omitempty"`
ConnectionState ConnectionState `json:"connectionState,omitempty"`
LastActivityTime *MicrosoftTime `json:"lastActivityTime,omitempty"`
AuthenticationType string `json:"authenticationType,omitempty"`
X509Thumbprint *X509Thumbprint `json:"x509Thumbprint,omitempty"`
Version uint `json:"version,omitempty"`
Tags map[string]interface{} `json:"tags,omitempty"`
Properties *Properties `json:"properties,omitempty"`
}
type Properties struct {
Desired map[string]interface{} `json:"desired,omitempty"`
Reported map[string]interface{} `json:"reported,omitempty"`
}
type DeviceStats struct {
DisabledDeviceCount uint `json:"disabledDeviceCount,omitempty"`
EnabledDeviceCount uint `json:"enabledDeviceCount,omitempty"`
TotalDeviceCount uint `json:"totalDeviceCount,omitempty"`
}
type ServiceStats struct {
ConnectedDeviceCount uint `json:"connectedDeviceCount"`
}
type Configuration struct {
ID string `json:"id,omitempty"`
SchemaVersion string `json:"schemaVersion,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Content *ConfigurationContent `json:"content,omitempty"`
TargetCondition string `json:"targetCondition,omitempty"`
CreatedTimeUTC *time.Time `json:"createdTimeUtc,omitempty"`
LastUpdatedTimeUTC *time.Time `json:"lastUpdatedTimeUtc,omitempty"`
Priority uint `json:"priority,omitempty"`
SystemMetrics *ConfigurationMetrics `json:"systemMetrics,omitempty"`
Metrics *ConfigurationMetrics `json:"metrics,omitempty"`
ETag string `json:"etag,omitempty"`
}
type ConfigurationContent struct {
ModulesContent map[string]interface{} `json:"modulesContent,omitempty"`
DeviceContent map[string]interface{} `json:"deviceContent,omitempty"`
}
type ConfigurationMetrics struct {
Results map[string]uint `json:"results,omitempty"`
Queries map[string]string `json:"queries,omitempty"`
}
type JobType string
const (
JobExport JobType = "export"
JobImport JobType = "import"
)
type Job struct {
Type JobType `json:"type"`
InputBlobContainerURI string `json:"inputBlobContainerUri"`
OutputBlobContainerURI string `json:"outputBlobContainerUri,omitempty"`
ExcludeKeysInExport bool `json:"excludeKeysInExport"`
}
type BulkResult struct {
IsSuccessful bool `json:"isSuccessful"`
Errors []*BulkError `json:"errors"`
// TODO: figure out the structure of a warning
Warnings []interface{} `json:"warnings"`
}
type BulkError struct {
DeviceID string `json:"deviceId"`
ErrorCode uint `json:"errorCode"`
ErrorStatus string `json:"errorStatus"`
}
type JobV2 struct {
JobID string `json:"jobId"`
Type JobV2Type `json:"type"`
Status JobV2Status `json:"status,omitempty"`
CloudToDeviceMethod *DeviceMethodParams `json:"cloudToDeviceMethod,omitempty"`
UpdateTwin interface{} `json:"updateTwin,omitempty"`
QueryCondition string `json:"queryCondition"`
StartTime time.Time `json:"startTime"`
MaxExecutionTimeInSeconds uint `json:"maxExecutionTimeInSeconds"`
}
type JobV2Type string
const (
JobTypeUnknown JobV2Type = "unknown"
JobTypeUpdateTwin JobV2Type = "scheduleUpdateTwin"
JobTypeDeviceMethod JobV2Type = "scheduleDeviceMethod"
)
type JobV2Status string
const (
JobStatusUnknown JobV2Status = "unknown"
JobStatusQueued JobV2Status = "queued"
JobStatusScheduled JobV2Status = "scheduled"
JobStatusRunning JobV2Status = "running"
JobStatusCancelled JobV2Status = "cancelled"
JobStatusCompleted JobV2Status = "completed"
)
type DeviceMethodParams struct {
MethodName string `json:"methodName"`
Payload interface{} `json:"payload"`
TimeoutInSeconds uint `json:"timeoutInSeconds"`
}
type BadRequestError struct {
Message string `json:"Message"`
ExceptionMessage string `json:"ExceptionMessage"`
}
func (e *BadRequestError) Error() string {
return "bad request: " + e.Message
}
type Deployment struct {
ID string `json:"id"`
Labels map[string]string `json:"labels"`
Priority uint `json:"priority"`
TargetCondition string `json:"targetCondition"`
Content *DeploymentContent `json:"content"`
Metrics *ConfigurationMetrics `json:"metrics"`
ETag string `json:"etag"`
}
type DeploymentContent struct {
ModulesContent map[string]interface{} `json:"modulesContent"`
}
// MicrosoftTime is a hack to parse time json attributes that
// don't follow RFC3339 and don't put timezone at the end of timestamp.
type MicrosoftTime struct {
time.Time
}
func (t *MicrosoftTime) UnmarshalJSON(b []byte) error {
if len(b) < 2 {
return errors.New("malformed time")
}
layout := "2006-01-02T15:04:05"
if b[len(b)-2] == 'Z' {
layout = "2006-01-02T15:04:05Z"
}
n, err := time.Parse(layout, string(b[1:len(b)-1]))
if err != nil {
return err
}
t.Time = n
return nil
}