-
Notifications
You must be signed in to change notification settings - Fork 2
/
device.go
113 lines (94 loc) · 2.17 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
package cloud
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"path"
)
// Device represents an ISE deployment that's registered with pxGrid Cloud
type Device struct {
id string
kind string
name string
region string
status string
tenant *Tenant
fqdn string
}
func (d *Device) String() string {
return fmt.Sprintf("Device[ID: %s, Name: %s, Type: %s: Tenant: %s]",
d.id, d.name, d.kind, d.tenant.id)
}
// ID returns device's id
func (d *Device) ID() string {
return d.id
}
// Name returns device's name
func (d *Device) Name() string {
return d.name
}
// Type returns device's type
func (d *Device) Type() string {
return d.kind
}
// Region returns device's region
func (d *Device) Region() string {
return d.region
}
// Tenant returns device's tenant
func (d *Device) Tenant() *Tenant {
return d.tenant
}
// Region returns device's fqdn
func (d *Device) Fqdn() string {
return d.fqdn
}
// DeviceStatus represents the status of a device
type DeviceStatus struct {
Status string
}
type getDeviceResponse struct {
ID string `json:"deviceId"`
DeviceInfo struct {
Kind string `json:"deviceType"`
Name string `json:"name"`
} `json:"deviceInfo"`
MgtInfo struct {
Region string `json:"region"`
Fqdn string `json:"fqdn"`
} `json:"mgtInfo"`
Meta struct {
EnrollmentStatus string `json:"enrollmentStatus"`
} `json:"meta"`
}
// Status fetches actual status of the device
func (d *Device) Status() (*DeviceStatus, error) {
queryPath := fmt.Sprintf(path.Join(getDevicesPath, "/%s"), url.PathEscape(d.id))
var device getDeviceResponse
var errorResp errorResponse
response, err := d.tenant.httpClient.R().
SetResult(&device).
SetError(&errorResp).
Get(queryPath)
if err != nil {
return nil, err
}
if response.IsError() {
return nil, errors.New(errorResp.GetError())
}
resp := &DeviceStatus{
Status: device.Meta.EnrollmentStatus,
}
return resp, err
}
func (d *Device) MarshalJSON() ([]byte, error) {
device := make(map[string]interface{})
device["id"] = d.ID()
device["kind"] = d.Type()
device["name"] = d.Name()
device["region"] = d.Region()
device["status"] = d.status
device["tenant"] = d.tenant.ID()
return json.Marshal(device)
}