forked from dell/goscaleio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.go
161 lines (126 loc) · 4.59 KB
/
template.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
// 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 (
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
types "github.com/dell/goscaleio/types/v1"
)
// GetTemplateByID gets the node details based on ID
func (gc *GatewayClient) GetTemplateByID(id string) (*types.TemplateDetails, error) {
defer TimeSpent("GetTemplateByID", time.Now())
path := fmt.Sprintf("/Api/V1/template/%v", id)
var template types.TemplateDetails
req, httpError := http.NewRequest(http.MethodGet, gc.host+path, nil)
if httpError != nil {
return nil, httpError
}
if gc.version == "4.0" {
req.Header.Set("Authorization", "Bearer "+gc.token)
err := setCookie(req.Header, gc.host)
if err != nil {
return nil, fmt.Errorf("Error While Handling Cookie: %s", err)
}
} else {
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(gc.username+":"+gc.password)))
}
req.Header.Set("Content-Type", "application/json")
client := gc.http
httpResp, httpRespError := client.Do(req)
if httpRespError != nil {
return nil, httpRespError
}
if httpResp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Template not found")
}
responseString, _ := extractString(httpResp)
err := json.Unmarshal([]byte(responseString), &template)
if err != nil {
return nil, fmt.Errorf("Error parsing response data for template: %s", err)
}
return &template, nil
}
// GetAllTemplates gets all the Template details
func (gc *GatewayClient) GetAllTemplates() ([]types.TemplateDetails, error) {
defer TimeSpent("GetAllTemplates", time.Now())
path := "/Api/V1/template"
var templates types.TemplateDetailsFilter
req, httpError := http.NewRequest(http.MethodGet, gc.host+path, nil)
if httpError != nil {
return nil, httpError
}
if gc.version == "4.0" {
req.Header.Set("Authorization", "Bearer "+gc.token)
err := setCookie(req.Header, gc.host)
if err != nil {
return nil, fmt.Errorf("Error While Handling Cookie: %s", err)
}
} else {
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(gc.username+":"+gc.password)))
}
req.Header.Set("Content-Type", "application/json")
client := gc.http
httpResp, httpRespError := client.Do(req)
if httpRespError != nil {
return nil, httpRespError
}
if httpResp.StatusCode == 200 {
responseString, _ := extractString(httpResp)
parseError := json.Unmarshal([]byte(responseString), &templates)
if parseError != nil {
return nil, fmt.Errorf("Error While Parsing Response Data For Template: %s", parseError)
}
}
return templates.TemplateDetails, nil
}
// GetTemplateByFilters gets the Template details based on the provided filter
func (gc *GatewayClient) GetTemplateByFilters(key string, value string) ([]types.TemplateDetails, error) {
defer TimeSpent("GetTemplateByFilters", time.Now())
encodedValue := url.QueryEscape(value)
path := `/Api/V1/template?filter=` + key + `%20eq%20%22` + encodedValue + `%22`
var templates types.TemplateDetailsFilter
req, httpError := http.NewRequest(http.MethodGet, gc.host+path, nil)
if httpError != nil {
return nil, httpError
}
if gc.version == "4.0" {
req.Header.Set("Authorization", "Bearer "+gc.token)
err := setCookie(req.Header, gc.host)
if err != nil {
return nil, fmt.Errorf("Error While Handling Cookie: %s", err)
}
} else {
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(gc.username+":"+gc.password)))
}
req.Header.Set("Content-Type", "application/json")
client := gc.http
httpResp, httpRespError := client.Do(req)
if httpRespError != nil {
return nil, httpRespError
}
if httpResp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Template not found")
}
responseString, _ := extractString(httpResp)
parseError := json.Unmarshal([]byte(responseString), &templates)
if parseError != nil {
return nil, fmt.Errorf("Error While Parsing Response Data For Template: %s", parseError)
}
if len(templates.TemplateDetails) == 0 {
return nil, fmt.Errorf("Template not found")
}
return templates.TemplateDetails, nil
}