-
Notifications
You must be signed in to change notification settings - Fork 11
/
clusters_api.go
177 lines (151 loc) · 5.95 KB
/
clusters_api.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
// Copyright (c) 2020 Sorint.lab S.p.A.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package controller
import (
"fmt"
"net/http"
"time"
"github.com/360EntSecGroup-Skylar/excelize"
"github.com/ercole-io/ercole/utils"
"github.com/golang/gddo/httputil"
"github.com/gorilla/mux"
)
// SearchClusters search clusters data using the filters in the request
func (ctrl *APIController) SearchClusters(w http.ResponseWriter, r *http.Request) {
choiche := httputil.NegotiateContentType(r, []string{"application/json", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}, "application/json")
switch choiche {
case "application/json":
ctrl.SearchClustersJSON(w, r)
case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
ctrl.SearchClustersXLSX(w, r)
}
}
// SearchClustersJSON search clusters data using the filters in the request returning it in JSON format
func (ctrl *APIController) SearchClustersJSON(w http.ResponseWriter, r *http.Request) {
var full bool
var search string
var sortBy string
var sortDesc bool
var pageNumber int
var pageSize int
var location string
var environment string
var olderThan time.Time
var err utils.AdvancedErrorInterface
//parse the query params
if full, err = utils.Str2bool(r.URL.Query().Get("full"), false); err != nil {
utils.WriteAndLogError(ctrl.Log, w, http.StatusUnprocessableEntity, err)
return
}
search = r.URL.Query().Get("search")
sortBy = r.URL.Query().Get("sort-by")
if sortDesc, err = utils.Str2bool(r.URL.Query().Get("sort-desc"), false); err != nil {
utils.WriteAndLogError(ctrl.Log, w, http.StatusUnprocessableEntity, err)
return
}
if pageNumber, err = utils.Str2int(r.URL.Query().Get("page"), -1); err != nil {
utils.WriteAndLogError(ctrl.Log, w, http.StatusUnprocessableEntity, err)
return
}
if pageSize, err = utils.Str2int(r.URL.Query().Get("size"), -1); err != nil {
utils.WriteAndLogError(ctrl.Log, w, http.StatusUnprocessableEntity, err)
return
}
location = r.URL.Query().Get("location")
environment = r.URL.Query().Get("environment")
if olderThan, err = utils.Str2time(r.URL.Query().Get("older-than"), utils.MAX_TIME); err != nil {
utils.WriteAndLogError(ctrl.Log, w, http.StatusUnprocessableEntity, err)
return
}
//get the data
clusters, err := ctrl.Service.SearchClusters(full, search, sortBy, sortDesc, pageNumber, pageSize, location, environment, olderThan)
if err != nil {
utils.WriteAndLogError(ctrl.Log, w, http.StatusInternalServerError, err)
return
}
if pageNumber == -1 || pageSize == -1 {
//Write the data
utils.WriteJSONResponse(w, http.StatusOK, clusters)
} else {
//Write the data
utils.WriteJSONResponse(w, http.StatusOK, clusters[0])
}
}
// SearchClustersXLSX search clusters data using the filters in the request returning it in XLSX format
func (ctrl *APIController) SearchClustersXLSX(w http.ResponseWriter, r *http.Request) {
var search string
var sortBy string
var sortDesc bool
var location string
var environment string
var olderThan time.Time
var aerr utils.AdvancedErrorInterface
//parse the query params
search = r.URL.Query().Get("search")
sortBy = r.URL.Query().Get("sort-by")
if sortDesc, aerr = utils.Str2bool(r.URL.Query().Get("sort-desc"), false); aerr != nil {
utils.WriteAndLogError(ctrl.Log, w, http.StatusUnprocessableEntity, aerr)
return
}
location = r.URL.Query().Get("location")
environment = r.URL.Query().Get("environment")
if olderThan, aerr = utils.Str2time(r.URL.Query().Get("older-than"), utils.MAX_TIME); aerr != nil {
utils.WriteAndLogError(ctrl.Log, w, http.StatusUnprocessableEntity, aerr)
return
}
//get the data
clusters, aerr := ctrl.Service.SearchClusters(false, search, sortBy, sortDesc, -1, -1, location, environment, olderThan)
if aerr != nil {
utils.WriteAndLogError(ctrl.Log, w, http.StatusInternalServerError, aerr)
return
}
//Open the sheet
sheets, err := excelize.OpenFile(ctrl.Config.ResourceFilePath + "/templates/template_clusters.xlsx")
if err != nil {
utils.WriteAndLogError(ctrl.Log, w, http.StatusInternalServerError, utils.NewAdvancedErrorPtr(err, "READ_TEMPLATE"))
return
}
//Add the data to the sheet
for i, val := range clusters {
sheets.SetCellValue("Hypervisor", fmt.Sprintf("A%d", i+2), val["name"])
sheets.SetCellValue("Hypervisor", fmt.Sprintf("B%d", i+2), val["type"])
sheets.SetCellValue("Hypervisor", fmt.Sprintf("C%d", i+2), val["cpu"])
sheets.SetCellValue("Hypervisor", fmt.Sprintf("D%d", i+2), val["sockets"])
sheets.SetCellValue("Hypervisor", fmt.Sprintf("E%d", i+2), val["virtualizationNodes"])
}
//Write it to the response
utils.WriteXLSXResponse(w, sheets)
}
// GetCluster get cluster data using the filters in the request
func (ctrl *APIController) GetCluster(w http.ResponseWriter, r *http.Request) {
var olderThan time.Time
var err utils.AdvancedErrorInterface
clusterName := mux.Vars(r)["name"]
if olderThan, err = utils.Str2time(r.URL.Query().Get("older-than"), utils.MAX_TIME); err != nil {
utils.WriteAndLogError(ctrl.Log, w, http.StatusUnprocessableEntity, err)
return
}
//get the data
data, err := ctrl.Service.GetCluster(clusterName, olderThan)
if err == utils.AerrClusterNotFound {
utils.WriteAndLogError(ctrl.Log, w, http.StatusNotFound, err)
return
} else if err != nil {
utils.WriteAndLogError(ctrl.Log, w, http.StatusInternalServerError, err)
return
}
//Write the data
utils.WriteJSONResponse(w, http.StatusOK, data)
}