-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi_utility.go
262 lines (228 loc) · 6.98 KB
/
api_utility.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
package bigfix
import (
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
)
// GetComputerCount : Will return Count of computers
func GetComputerCount(config Config) string {
var count string
url := GetComputerCountAPI(config.ServerIP, config.Port)
log.Println("[DEBUG] Getting Count of total computers...")
response, err := config.BfxConnection(GET, url, nil)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
var countComputerstruct CountComputerStruct
if response != nil {
defer response.Body.Close()
data, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println(err)
}
log.Println("[DEBUG] response is :")
log.Println(string(data))
xml.Unmarshal([]byte(data), &countComputerstruct)
count = countComputerstruct.Query.Result.Answer.Text
return count
}
log.Println("[DEBUG] Computer list is empty !!!")
return ""
}
// DeleteAction : Will delete action from console
func DeleteAction(config Config, id string) (bool, error) {
url := GetDeleteActionAPI(config.ServerIP, config.Port, id)
response, err := config.BfxConnection(DELETE, url, nil)
if err != nil {
return false, err
}
if response.StatusCode == http.StatusOK {
log.Println("[DEBUG] Action Deleted Successfully")
return true, nil
}
data, err1 := ioutil.ReadAll(response.Body)
if err1 != nil {
log.Printf("[DEBUG] Error reading response body : %v\n", err1)
return false, err
}
return false, fmt.Errorf(string(data))
}
// SetSourcedMemberList : will set members for multiple action group
func SetSourcedMemberList(config Config, site []string, computerID string) ([]SourcedMemberAction, error) {
var resultStruct []SourcedMemberAction
var totalSize int = 0
totalSite := len(site)
length := make([]int, totalSite)
length, totalSize = GetCountofRelevantFixlets(config, site, computerID)
log.Printf("[DEBUG] total length of fixlets is %d", totalSize)
if totalSize == 0 {
log.Printf("[DEBUG] No relevant fixlets found from any site !!!")
return nil, fmt.Errorf("No relevant fixlets found from any site")
}
// to store final list of members
resultStruct = make([]SourcedMemberAction, totalSize)
var k int = 0
for i := 0; i < len(site); i++ {
// to store members from site[i]
var sourcedMemberContentStruct SourcedMemeberContent
valid, err := checkSiteValidity(config, site[i])
if err != nil {
return nil, err
}
if valid {
url := GetRelevantFixletsAPI(config.ServerIP, config.Port, site[i], computerID)
response, err := config.BfxConnection(GET, url, nil)
if err != nil {
return nil, err
}
if response != nil {
defer response.Body.Close()
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
log.Println("[DEBUG] response is :")
log.Println(string(data))
xml.Unmarshal([]byte(data), &sourcedMemberContentStruct)
}
}
fixletCount := length[i]
for j := 0; j < fixletCount; j++ {
resultStruct[k] = SourcedMemberAction{
SourceFixlet: SourceFixlet{
Sitename: sourcedMemberContentStruct.Query.Result.Tuple[j].Answer[0].Text,
FixletID: sourcedMemberContentStruct.Query.Result.Tuple[j].Answer[1].Text,
Action: "Action1",
},
}
// for final list
k++
}
}
return resultStruct, nil
}
// GetCountofRelevantFixlets : will return count of total relevent fixlets from multiple sites
func GetCountofRelevantFixlets(config Config, site []string, computerID string) ([]int, int) {
var result CountofRelevantFixlet
// length : to count relevant fixlets from each site
length := make([]int, len(site))
var total int = 0
for i := 0; i < len(site); i++ {
log.Printf("[DEBUG] for Site `%s` setting relevant fixlet members out of %d ", site[i], len(site))
url := GetCountofRelevantFixletsAPI(config.ServerIP, config.Port, site[i], computerID)
response, err := config.BfxConnection(GET, url, nil)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if response != nil {
defer response.Body.Close()
data, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println(err)
}
// log.Println("[DEBUG] response is :")
// log.Println(string(data))
xml.Unmarshal([]byte(data), &result)
tempLength := result.Query.Result.Answer.Text
length[i], _ = strconv.Atoi(tempLength)
log.Printf("[DEBUG] length of %d site is %d", i, length[i])
total = total + length[i]
}
}
log.Printf("[DEBUG] total length of fixlets is %d", total)
return length, total
}
func checkActionDestroyed(data []byte, title string) (bool, error) {
var actionStruct Action
err := xml.Unmarshal(data, &actionStruct)
if err != nil {
return false, fmt.Errorf("Error while unmarshaling data %s", err)
}
if actionStruct.MultipleActionGroup.Title == title {
return true, nil
}
return false, nil
}
// checkSiteValidity : Function will check if given site name is valid
func checkSiteValidity(config Config, sitename string) (bool, error) {
url := GetAllSitesAPI(config.ServerIP, config.Port)
response, err := config.BfxConnection(GET, url, nil)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if response != nil {
defer response.Body.Close()
var sitesStruct AllSites
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return false, err
}
log.Println("[DEBUG] response is :")
log.Println(string(data))
xml.Unmarshal([]byte(data), &sitesStruct)
total := len(sitesStruct.ExternalSite)
for i := 0; i < total; i++ {
if sitesStruct.ExternalSite[i].Name == sitename {
return true, nil
} else if sitesStruct.OperatorSite.Name == sitename {
return true, nil
} else if sitesStruct.ActionSite.Name == sitename {
return true, nil
}
}
}
// Not Found in list
return false, fmt.Errorf("Given site Name `%s` is not valid", sitename)
}
// getNameOfComputer : Function will return name of computer
func getNameOfComputer(config Config, id string) string {
var computerNameStruct ComputerName
url := GetComputerNameAPI(config.ServerIP, config.Port, id)
response, err := config.BfxConnection(GET, url, nil)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if response != nil {
defer response.Body.Close()
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return ""
}
log.Println("[DEBUG] response is :")
log.Println(string(data))
xml.Unmarshal([]byte(data), &computerNameStruct)
return computerNameStruct.Query.Result.Answer.Text
}
return ""
}
// GetActionStatus : will return status of action
func GetActionStatus(config Config, actionID string) string {
var actionStatus ActionStatus
url := GetActionStatusAPI(config.ServerIP, config.Port, actionID)
response, err := config.BfxConnection(GET, url, nil)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if response != nil {
defer response.Body.Close()
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return ""
}
log.Println("[DEBUG] status response is :")
log.Println(string(data))
xml.Unmarshal([]byte(data), &actionStatus)
log.Println("[DEBUG] ActionStatus :", actionStatus.ActionResults.Status)
return actionStatus.ActionResults.Status
}
return ""
}