-
Notifications
You must be signed in to change notification settings - Fork 1
/
ibm.go
246 lines (199 loc) · 5.38 KB
/
ibm.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
package cloudman
import (
"bytes"
"encoding/json"
"gopkg.in/resty.v1"
"io/ioutil"
"log"
"net/http"
"strings"
)
type IBMCloud struct {
credentials IBMCredentials
}
const (
IBM_Kube_GetCluster_Endpoint = "https://containers.cloud.ibm.com/global/v2/vpc/getClusters"
IBM_Kube_GetWorkers_Endpoint = "https://containers.cloud.ibm.com/global/v2/vpc/getWorkers"
IBM_Kube_GetWorker_Endpoint = "https://containers.cloud.ibm.com/global/v2/vpc/getWorker"
IBM_IAM_Endpoint = "https://iam.cloud.ibm.com/identity/token"
Ibm_Resource_Manager = "https://resource-controller.cloud.ibm.com/v2/resource_groups"
)
// first login using gcloud cli
// use autogenerated credentials from cli to authenicate as acount level
func NewIBMCloud(c IBMCredentials) *IBMCloud {
payloadSlice := "grant_type=urn:ibm:params:oauth:grant-type:apikey&apikey=" + c.APIKey
res, err := http.Post(IBM_IAM_Endpoint, "application/x-www-form-urlencoded", bytes.NewBuffer([]byte(payloadSlice)))
if err != nil {
log.Println(err)
}
if res.StatusCode != 200 {
log.Println("resp code not 200")
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Println(err)
}
var token Token
err = json.Unmarshal(body, &token)
if err != nil {
log.Println(err)
}
creds := IBMCredentials{
APIKey: c.APIKey,
iamToken: token.TokenType + " " + token.AccessToken,
refreshToken: token.RefreshToken,
}
out := IBMCloud{
credentials: creds,
}
return &out
}
type Token struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
Expiration int `json:"expiration"`
Scope string `json:"scope"`
}
type IBMCredentials struct {
APIKey string
iamToken string
refreshToken string
}
func (c *IBMCloud) GetInstanceListAllRegions() ([]InstanceListResponse, error) {
ibmClient := resty.New()
m := make(map[string]string)
m["Content-Type"] = "application/json"
m["Accept"] = "application/json"
m["Authorization"] = c.credentials.iamToken
resp, err := ibmClient.
SetRedirectPolicy(resty.FlexibleRedirectPolicy(20)).
R().
SetHeaders(m).
Get(IBM_Kube_GetCluster_Endpoint)
if err != nil {
log.Println(err)
return nil, err
}
//log.Println(string(resp.Body()))
var clusters []kCluster
err = json.Unmarshal(resp.Body(), &clusters)
if err != nil {
log.Println(err)
return nil, err
}
log.Println(clusters)
var out []InstanceListResponse
recvChan := make(chan []InstanceListResponse, len(clusters))
for _, cluster := range clusters {
go func(cls kCluster) {
res, _ := c.GetInstanceList(cls.ID)
addClusterData(res, cls.CreatedDate, cls.Name)
recvChan <- res
}(cluster)
}
for j := 0; j < len(clusters); j++ {
list := <-recvChan
out = append(out, list...)
}
return out, nil
}
func addClusterData(list []InstanceListResponse, date, cname string) {
for i, item := range list {
item.LaunchDate = date
item.Project = cname
list[i] = item
}
}
func (c *IBMCloud) GetInstanceList(cid string) ([]InstanceListResponse, error) {
ibmClient := resty.New()
m := make(map[string]string)
m["Content-Type"] = "application/json"
m["Accept"] = "application/json"
m["Authorization"] = c.credentials.iamToken
resp, err := ibmClient.
SetRedirectPolicy(resty.FlexibleRedirectPolicy(20)).
R().
SetHeaders(m).
SetQueryParam("cluster", cid).
SetQueryParam("showDeleted", "false").
Get(IBM_Kube_GetWorkers_Endpoint)
if err != nil {
log.Println(err)
return nil, err
}
//log.Println(string(resp.Body()))
var workers = []kWorker{}
err = json.Unmarshal(resp.Body(), &workers)
if err != nil {
log.Println(err)
return nil, err
}
//log.Println(workers)
//c.GetWorkerDetails(workers[0].Id, cid)
var instList []InstanceListResponse
for _, worker := range workers {
t := strings.Split(worker.Id, "-")
name := t[len(t)-1]
inst := InstanceListResponse{
Name: name,
Id: worker.Id,
Status: worker.Health.State,
LaunchDate: "",
Region: worker.Location,
MachineType: worker.Flavor,
Project: "",
}
instList = append(instList, inst)
}
return instList, nil
}
func (c *IBMCloud) GetWorkerDetails(wid, cid string) {
ibmClient := resty.New()
m := make(map[string]string)
m["Content-Type"] = "application/json"
m["Accept"] = "application/json"
m["Authorization"] = c.credentials.iamToken
resp, err := ibmClient.
SetRedirectPolicy(resty.FlexibleRedirectPolicy(20)).
R().
SetHeaders(m).
SetQueryParam("cluster", cid).
SetQueryParam("worker", wid).
Get(IBM_Kube_GetWorker_Endpoint)
if err != nil {
log.Println(err)
return
}
log.Println(string(resp.Body()))
var w kWorker
err = json.Unmarshal(resp.Body(), &w)
if err != nil {
log.Println(err)
return
}
log.Println(w)
}
func (c *IBMCloud) GetInstanceDetails() {
}
type kCluster struct {
CreatedDate string `json:"createdDate"`
Name string `json:"name"`
ID string `json:"id"`
Location string `json:"location"`
State string `json:"state"`
WorkerCount int `json:"workerCount"`
ResourceGroup string `json:"resourceGroup"`
}
type kWorker struct {
Id string `json:"id"`
Location string `json:"location"`
Flavor string `json:"flavor"`
Health kwHealth `json:"health"`
}
type kwHealth struct {
Message string `json:"message"`
State string `json:"state"`
}