-
Notifications
You must be signed in to change notification settings - Fork 13
/
upgrade.go
369 lines (303 loc) · 11.2 KB
/
upgrade.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Copyright © 2024 Dell Inc. or its subsidiaries. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package goscaleio
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
types "github.com/dell/goscaleio/types/v1"
)
// UploadCompliance function is used for uploading the compliance file.
func (gc *GatewayClient) UploadCompliance(uploadComplianceParam *types.UploadComplianceParam) (*types.UploadComplianceTopologyDetails, error) {
var uploadResponse types.UploadComplianceTopologyDetails
jsonData, err := json.Marshal(uploadComplianceParam)
if err != nil {
return &uploadResponse, err
}
req, httpError := http.NewRequest(http.MethodPost, gc.host+"/Api/V1/FirmwareRepository", bytes.NewBuffer(jsonData))
if httpError != nil {
return &uploadResponse, httpError
}
req.Header.Set("Authorization", "Bearer "+gc.token)
setCookieError := setCookie(req.Header, gc.host)
if setCookieError != nil {
return nil, fmt.Errorf("Error While Handling Cookie: %s", setCookieError)
}
req.Header.Set("Content-Type", "application/json")
client := gc.http
httpResp, httpRespError := client.Do(req)
if httpRespError != nil {
return &uploadResponse, httpRespError
}
responseString, err := extractString(httpResp)
if err != nil {
return &uploadResponse, fmt.Errorf("Error Extracting Response: %s", err)
}
if httpResp.StatusCode != http.StatusCreated {
return &uploadResponse, fmt.Errorf("Error while uploading Compliance File")
}
if responseString == "" {
return &uploadResponse, fmt.Errorf("Error while uploading Compliance File")
}
err = storeCookie(httpResp.Header, gc.host)
if err != nil {
return &uploadResponse, fmt.Errorf("Error While Storing cookie: %s", err)
}
err = json.Unmarshal([]byte(responseString), &uploadResponse)
if err != nil {
return &uploadResponse, fmt.Errorf("Error getting upload compliance details: %s", err)
}
return &uploadResponse, nil
}
// GetUploadComplianceDetails function is used for getting the details of the compliance upload
func (gc *GatewayClient) GetUploadComplianceDetails(id string, newToken bool) (*types.UploadComplianceTopologyDetails, error) {
var getUploadCompResponse types.UploadComplianceTopologyDetails
var err error
if newToken {
token, err := gc.NewTokenGeneration()
if err != nil {
return nil, err
}
gc.token = token
}
req, httpError := http.NewRequest(http.MethodGet, gc.host+"/Api/V1/FirmwareRepository/"+id, nil)
if httpError != nil {
return &getUploadCompResponse, httpError
}
req.Header.Set("Authorization", "Bearer "+gc.token)
setCookieError := setCookie(req.Header, gc.host)
if setCookieError != nil {
return nil, fmt.Errorf("Error While Handling Cookie: %s", setCookieError)
}
req.Header.Set("Content-Type", "application/json")
client := gc.http
httpResp, httpRespError := client.Do(req)
if httpRespError != nil {
return &getUploadCompResponse, httpRespError
}
responseString, err := extractString(httpResp)
if err != nil {
return &getUploadCompResponse, fmt.Errorf("Error Extracting Response: %s", err)
}
if httpResp.StatusCode != http.StatusOK {
return &getUploadCompResponse, fmt.Errorf("Error while getting Compliance details")
}
if responseString == "" {
return &getUploadCompResponse, fmt.Errorf("Error Getting Compliance Details")
}
err3 := storeCookie(httpResp.Header, gc.host)
if err3 != nil {
return &getUploadCompResponse, fmt.Errorf("Error While Storing cookie: %s", err3)
}
err = json.Unmarshal([]byte(responseString), &getUploadCompResponse)
if err != nil {
return &getUploadCompResponse, fmt.Errorf("Error getting upload compliance details: %s", err)
}
return &getUploadCompResponse, nil
}
// ApproveUnsignedFile is used for approving the unsigned file to upload
func (gc *GatewayClient) ApproveUnsignedFile(id string) error {
jsonData := []byte(`{}`)
req, httpError := http.NewRequest("PUT", gc.host+"/Api/V1/FirmwareRepository/"+id+"/allowunsignedfile", bytes.NewBuffer(jsonData))
if httpError != nil {
return httpError
}
req.Header.Set("Authorization", "Bearer "+gc.token)
setCookieError := setCookie(req.Header, gc.host)
if setCookieError != nil {
return fmt.Errorf("Error While Handling Cookie: %s", setCookieError)
}
req.Header.Set("Content-Type", "application/json")
client := gc.http
httpResp, httpRespError := client.Do(req)
if httpRespError != nil {
return httpRespError
}
if httpResp.StatusCode != http.StatusNoContent {
return fmt.Errorf("Error while approving the unsigned Compliance file")
}
err3 := storeCookie(httpResp.Header, gc.host)
if err3 != nil {
return fmt.Errorf("Error While Storing cookie: %s", err3)
}
return nil
}
// GetAllUploadComplianceDetails returns all the firmware repository
func (gc *GatewayClient) GetAllUploadComplianceDetails() (*[]types.UploadComplianceTopologyDetails, error) {
var getUploadCompResponse []types.UploadComplianceTopologyDetails
req, httpError := http.NewRequest("GET", gc.host+"/Api/V1/FirmwareRepository/", nil)
if httpError != nil {
return &getUploadCompResponse, httpError
}
req.Header.Set("Authorization", "Bearer "+gc.token)
setCookieError := setCookie(req.Header, gc.host)
if setCookieError != nil {
return nil, fmt.Errorf("Error While Handling Cookie: %s", setCookieError)
}
req.Header.Set("Content-Type", "application/json")
client := gc.http
httpResp, httpRespError := client.Do(req)
if httpRespError != nil {
return &getUploadCompResponse, httpRespError
}
responseString, err := extractString(httpResp)
if err != nil {
return &getUploadCompResponse, fmt.Errorf("Error Extracting Response: %s", err)
}
if httpResp.StatusCode != http.StatusOK {
return &getUploadCompResponse, fmt.Errorf("Error while getting Compliance details")
}
if responseString == "" {
return &getUploadCompResponse, fmt.Errorf("Error Getting Compliance Details")
}
err3 := storeCookie(httpResp.Header, gc.host)
if err3 != nil {
return &getUploadCompResponse, fmt.Errorf("Error While Storing cookie: %s", err3)
}
err = json.Unmarshal([]byte(responseString), &getUploadCompResponse)
if err != nil {
return &getUploadCompResponse, fmt.Errorf("Error getting upload compliance details: %s", err)
}
return &getUploadCompResponse, nil
}
// GetUploadComplianceDetailsUsingFilter filters the firmware repository based on name
func (gc *GatewayClient) GetUploadComplianceDetailsUsingFilter(name string) (*types.UploadComplianceTopologyDetails, error) {
frDetails, err := gc.GetAllUploadComplianceDetails()
if err != nil {
return nil, err
}
for _, fr := range *frDetails {
if fr.Name == name {
return &fr, nil
}
}
return nil, errors.New("couldn't find the firmware repository")
}
// GetUploadComplianceDetailsUsingID returns all the details of the firmware repository using ID
func (gc *GatewayClient) GetUploadComplianceDetailsUsingID(id string) (*types.FirmwareRepositoryDetails, error) {
var frResponse types.FirmwareRepositoryDetails
u, err := url.Parse(gc.host + "/Api/V1/FirmwareRepository/" + id)
if err != nil {
return &frResponse, err
}
q := u.Query()
q.Set("components", "true")
u.RawQuery = q.Encode()
req, httpError := http.NewRequest("GET", u.String(), nil)
if httpError != nil {
return &frResponse, httpError
}
req.Header.Set("Authorization", "Bearer "+gc.token)
setCookieError := setCookie(req.Header, gc.host)
if setCookieError != nil {
return nil, fmt.Errorf("Error While Handling Cookie: %s", setCookieError)
}
req.Header.Set("Content-Type", "application/json")
client := gc.http
httpResp, httpRespError := client.Do(req)
if httpRespError != nil {
return &frResponse, httpRespError
}
responseString, err := extractString(httpResp)
if err != nil {
return &frResponse, fmt.Errorf("Error Extracting Response: %s", err)
}
if httpResp.StatusCode != http.StatusOK {
return &frResponse, fmt.Errorf("Error while getting Compliance details")
}
if responseString == "" {
return &frResponse, fmt.Errorf("Error Getting Compliance Details")
}
err3 := storeCookie(httpResp.Header, gc.host)
if err3 != nil {
return &frResponse, fmt.Errorf("Error While Storing cookie: %s", err3)
}
err = json.Unmarshal([]byte(responseString), &frResponse)
if err != nil {
return &frResponse, fmt.Errorf("Error getting upload compliance details: %s", err)
}
return &frResponse, nil
}
// GetFirmwareRepositoryDetailsUsingName returns all the details of the firmware repository using name
func (gc *GatewayClient) GetFirmwareRepositoryDetailsUsingName(name string) (*types.FirmwareRepositoryDetails, error) {
var fr *types.UploadComplianceTopologyDetails
var frDetails *types.FirmwareRepositoryDetails
var err error
fr, err = gc.GetUploadComplianceDetailsUsingFilter(name)
if err != nil {
return frDetails, err
}
frDetails, err = gc.GetUploadComplianceDetailsUsingID(fr.ID)
if err != nil {
return frDetails, err
}
return frDetails, err
}
// DeleteFirmwareRepository deletes the particular firmware repository
func (gc *GatewayClient) DeleteFirmwareRepository(id string) error {
req, httpError := http.NewRequest(http.MethodDelete, gc.host+"/Api/V1/FirmwareRepository/"+id, nil)
if httpError != nil {
return httpError
}
req.Header.Set("Authorization", "Bearer "+gc.token)
setCookieError := setCookie(req.Header, gc.host)
if setCookieError != nil {
return fmt.Errorf("Error While Handling Cookie: %s", setCookieError)
}
req.Header.Set("Content-Type", "application/json")
client := gc.http
httpResp, httpRespError := client.Do(req)
if httpRespError != nil {
return httpRespError
}
if httpResp.StatusCode != http.StatusNoContent {
return fmt.Errorf("Error while deleting firmware repository")
}
err3 := storeCookie(httpResp.Header, gc.host)
if err3 != nil {
return fmt.Errorf("Error While Storing cookie: %s", err3)
}
return nil
}
// TestConnection tests the connection to the source location.
func (gc *GatewayClient) TestConnection(uploadComplianceParam *types.UploadComplianceParam) error {
jsonData, err := json.Marshal(uploadComplianceParam)
if err != nil {
return err
}
req, httpError := http.NewRequest(http.MethodPost, gc.host+"/Api/V1/FirmwareRepository/connection", bytes.NewBuffer(jsonData))
if httpError != nil {
return httpError
}
req.Header.Set("Authorization", "Bearer "+gc.token)
setCookieError := setCookie(req.Header, gc.host)
if setCookieError != nil {
return fmt.Errorf("Error While Handling Cookie: %s", setCookieError)
}
req.Header.Set("Content-Type", "application/json")
client := gc.http
httpResp, httpRespError := client.Do(req)
if httpRespError != nil {
return httpRespError
}
if httpResp.StatusCode != http.StatusNoContent {
return fmt.Errorf("Error while connecting to the source location. Please chack the credentials")
}
err3 := storeCookie(httpResp.Header, gc.host)
if err3 != nil {
return fmt.Errorf("Error While Storing cookie: %s", err3)
}
return nil
}