forked from ilanddev/go-sdk
/
client.go
229 lines (198 loc) · 4.78 KB
/
client.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
package iland
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
"github.com/gorilla/websocket"
)
type client struct {
username string
password string
clientID string
clientSecret string
Token Token
tokenExpiration time.Time
http *http.Client
}
func InitClient(c *http.Client, accessToken string, expiration time.Time) (ConsoleService, error) {
return &client{
Token: Token{
AccessToken: accessToken,
ExpiresIn: expiration.Unix(),
},
tokenExpiration: expiration,
http: c,
}, nil
}
func NewClient(Username, Password, clientID, clientSecret string) (ConsoleService, error) {
return &client{
username: Username,
password: Password,
clientID: clientID,
clientSecret: clientSecret,
http: &http.Client{},
}, nil
}
func (c *client) Location() LocationService {
return &locationService{c}
}
func (c *client) Company() CompanyService {
return &companyService{c}
}
func (c *client) User() UserService {
return &userService{c}
}
func (c *client) Org() OrgService {
return &orgService{c}
}
func (c *client) Catalog() CatalogService {
return &catalogService{c}
}
func (c *client) VAppTemplate() VAppTemplateService {
return &vappTemplateService{c}
}
func (c *client) Vdc() VdcService {
return &vdcService{c}
}
func (c *client) Edge() EdgeService {
return &edgeService{c}
}
func (c *client) OrgVdcNetwork() OrgVdcNetworkService {
return &orgVdcNetworkService{c}
}
func (c *client) VApp() VAppService {
return &vappService{c}
}
func (c *client) VAppNetwork() VAppNetworkService {
return &vappNetworkService{c}
}
func (c *client) VirtualMachine() VirtualMachineService {
return &virtualMachineService{c}
}
func (c *client) VCCBackupTenant() VCCBackupTenantService {
return &vccBackupTenantService{c}
}
func (c *client) VacTenant() VacTenantService {
return &vacTenantService{c}
}
func (c *client) Vpg() VpgService {
return &vpgService{c}
}
func (c *client) O365() O365Service {
return &o365Service{c}
}
func (c *client) Task() TaskService {
return &taskService{c}
}
func (c *client) GetOperatingSystems() ([]OperatingSystem, error) {
schema := struct {
OperatingSystems []OperatingSystem `json:"data"`
}{}
err := c.getObject("/v1/constants/operating-systems", &schema)
return schema.OperatingSystems, err
}
func (c *client) GetLocations() ([]Location, error) {
schema := struct {
Locations []Location `json:"data"`
}{}
err := c.getObject("/v1/locations", &schema)
if err != nil {
return []Location{}, err
}
return schema.Locations, nil
}
func (c *client) GetCompanies() ([]Company, error) {
schema := struct {
Companies []Company `json:"data"`
}{}
err := c.getObject(fmt.Sprintf("/v1/users/%s/companies", c.username), &schema)
if err != nil {
return []Company{}, err
}
for i, company := range schema.Companies {
schema.Companies[i] = company
}
return schema.Companies, nil
}
func (c *client) GetOrgs() ([]Org, error) {
schema := struct {
Orgs []Org `json:"data"`
}{}
err := c.getObject(fmt.Sprintf("/v1/users/%s/orgs", c.username), &schema)
if err != nil {
return []Org{}, err
}
return schema.Orgs, nil
}
type SocketData struct {
Type string `json:"type"`
Data json.RawMessage `json:"data"`
}
func (c *client) StreamEvents(companyID string) (chan Event, error) {
events := make(chan Event, 100)
conn, err := c.dialEventStream(companyID)
if err != nil {
return nil, err
}
go c.streamEvents(companyID, conn, events)
return events, nil
}
func (c *client) dialEventStream(companyID string) (*websocket.Conn, error) {
err := c.RefreshTokenIfNecessary()
if err != nil {
return nil, err
}
conn, _, err := websocket.DefaultDialer.Dial("wss://api.ilandcloud.com/v1/event-websocket", nil)
if err != nil {
return nil, err
}
_, message, err := conn.ReadMessage()
if err != nil {
return nil, err
}
auth := fmt.Sprintf("Bearer %s", c.Token.AccessToken)
if companyID != "" {
auth = fmt.Sprintf("companyId=%s,Bearer %s", companyID, c.Token.AccessToken)
}
if string(message) == "AUTHORIZATION" {
err = conn.WriteMessage(websocket.TextMessage, []byte(auth))
if err != nil {
return nil, err
}
return conn, nil
}
return nil, errors.New(string(message))
}
func (c *client) streamEvents(companyID string, conn *websocket.Conn, events chan Event) {
defer conn.Close()
for {
_, message, err := conn.ReadMessage()
if err != nil {
break
}
msg := SocketData{}
err = json.Unmarshal(message, &msg)
if err != nil {
break
}
switch msg.Type {
case "EVENT":
event := Event{}
err = json.Unmarshal(msg.Data, &event)
if err == nil {
events <- event
}
}
}
for {
time.Sleep(time.Second * 5)
newConn, err := c.dialEventStream(companyID)
if err != nil {
continue
}
go c.streamEvents(companyID, newConn, events)
return
}
}