Skip to content

Commit 8448c55

Browse files
(feat): Fix PR checks
Signed-off-by: mittalvaibhav1 <[email protected]>
1 parent f802504 commit 8448c55

File tree

3 files changed

+54
-43
lines changed

3 files changed

+54
-43
lines changed

pkg/scalers/sumologic/sumologic_test.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -174,30 +174,28 @@ func TestGetLogSearchResult(t *testing.T) {
174174
w.WriteHeader(test.statusCode)
175175
w.Header().Set("Content-Type", "application/json")
176176

177-
if r.Method == "POST" && r.URL.Path == "/api/v1/search/jobs" {
177+
switch {
178+
case r.Method == "POST" && r.URL.Path == "/api/v1/search/jobs":
178179
err := json.NewEncoder(w).Encode(test.createJobResponse)
179-
180180
if err != nil {
181181
http.Error(w, fmt.Sprintf("error building the response, %v", err), http.StatusInternalServerError)
182182
return
183183
}
184-
} else if r.Method == "GET" && r.URL.Path == fmt.Sprintf("/api/v1/search/jobs/%s", test.createJobResponse.ID) {
184+
case r.Method == "GET" && r.URL.Path == fmt.Sprintf("/api/v1/search/jobs/%s", test.createJobResponse.ID):
185185
err := json.NewEncoder(w).Encode(test.jobStatusResponse)
186-
187186
if err != nil {
188187
http.Error(w, fmt.Sprintf("error building the response, %v", err), http.StatusInternalServerError)
189188
return
190189
}
191-
} else if r.Method == "GET" && r.URL.Path == fmt.Sprintf("/api/v1/search/jobs/%s/records", test.createJobResponse.ID) {
190+
case r.Method == "GET" && r.URL.Path == fmt.Sprintf("/api/v1/search/jobs/%s/records", test.createJobResponse.ID):
192191
err := json.NewEncoder(w).Encode(test.recordsResponse)
193-
194192
if err != nil {
195193
http.Error(w, fmt.Sprintf("error building the response, %v", err), http.StatusInternalServerError)
196194
return
197195
}
198-
} else if r.Method == "DELETE" {
196+
case r.Method == "DELETE":
199197
// do nothing
200-
} else {
198+
default:
201199
fmt.Println(r.Method, r.URL.Path)
202200
}
203201
}))

pkg/scalers/sumologic_scaler.go

+47-34
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import (
99
"time"
1010

1111
"github.com/go-logr/logr"
12+
v2 "k8s.io/api/autoscaling/v2"
13+
"k8s.io/metrics/pkg/apis/external_metrics"
14+
1215
"github.com/kedacore/keda/v2/pkg/scalers/scalersconfig"
1316
"github.com/kedacore/keda/v2/pkg/scalers/sumologic"
1417
kedautil "github.com/kedacore/keda/v2/pkg/util"
15-
v2 "k8s.io/api/autoscaling/v2"
16-
"k8s.io/metrics/pkg/apis/external_metrics"
1718
)
1819

1920
const (
@@ -36,7 +37,7 @@ type sumologicMetadata struct {
3637
queryType string `keda:"name=queryType, order=triggerMetadata"`
3738
query string `keda:"name=query, order=triggerMetadata"`
3839
queries map[string]string `keda:"name=query.*, order=triggerMetadata"` // Only for metrics queries
39-
resultQueryRowId string `keda:"name=resultQueryRowId, order=triggerMetadata"` // Only for metrics queries
40+
resultQueryRowID string `keda:"name=resultQueryRowID, order=triggerMetadata"` // Only for metrics queries
4041
quantization time.Duration `keda:"name=quantization, order=triggerMetadata"` // Only for metrics queries
4142
rollup string `keda:"name=rollup, order=triggerMetadata, optional"` // Only for metrics queries
4243
resultField string `keda:"name=resultField, order=triggerMetadata"` // Only for logs queries
@@ -82,30 +83,18 @@ func parseSumoMetadata(config *scalersconfig.ScalerConfig) (*sumologicMetadata,
8283
meta := sumologicMetadata{}
8384
meta.triggerIndex = config.TriggerIndex
8485

85-
if config.AuthParams["accessID"] == "" {
86-
return nil, errors.New("missing required auth params: accessID")
86+
if err := validateAuthParams(config.AuthParams); err != nil {
87+
return nil, err
8788
}
8889
meta.accessID = config.AuthParams["accessID"]
89-
90-
if config.AuthParams["accessKey"] == "" {
91-
return nil, errors.New("missing required auth params: accessKey")
92-
}
9390
meta.accessKey = config.AuthParams["accessKey"]
9491

95-
if config.TriggerMetadata["host"] == "" {
96-
return nil, errors.New("missing required metadata: host")
92+
if err := validateMetadata(config.TriggerMetadata); err != nil {
93+
return nil, err
9794
}
9895
meta.host = config.TriggerMetadata["host"]
99-
100-
if config.TriggerMetadata["queryType"] == "" {
101-
return nil, errors.New("missing required metadata: queryType")
102-
}
10396
meta.queryType = config.TriggerMetadata["queryType"]
10497

105-
if meta.queryType != "logs" && meta.queryType != "metrics" {
106-
return nil, fmt.Errorf("invalid queryType: %s, must be 'logs' or 'metrics'", meta.queryType)
107-
}
108-
10998
query := config.TriggerMetadata["query"]
11099
queries, err := parseMultiMetricsQueries(config.TriggerMetadata)
111100
if err != nil {
@@ -128,18 +117,19 @@ func parseSumoMetadata(config *scalersconfig.ScalerConfig) (*sumologicMetadata,
128117
}
129118

130119
if meta.queryType == "metrics" {
131-
if query == "" && len(queries) == 0 {
120+
switch {
121+
case query == "" && len(queries) == 0:
132122
return nil, errors.New("missing metadata, please define either of query or query.<RowId> for metrics queryType")
133-
} else if query != "" && len(queries) != 0 {
123+
case query != "" && len(queries) != 0:
134124
return nil, errors.New("incorrect metadata, please only define either query or query.<RowId> for metrics queryType, not both")
135-
} else if query != "" {
125+
case query != "":
136126
meta.query = query
137-
} else {
127+
default:
138128
meta.queries = queries
139-
if config.TriggerMetadata["resultQueryRowId"] == "" {
140-
return nil, errors.New("missing required metadata: resultQueryRowId")
129+
if config.TriggerMetadata["resultQueryRowID"] == "" {
130+
return nil, errors.New("missing required metadata: resultQueryRowID")
141131
}
142-
meta.resultQueryRowId = config.TriggerMetadata["resultQueryRowId"]
132+
meta.resultQueryRowID = config.TriggerMetadata["resultQueryRowID"]
143133
}
144134

145135
if config.TriggerMetadata["quantization"] == "" {
@@ -205,24 +195,47 @@ func parseSumoMetadata(config *scalersconfig.ScalerConfig) (*sumologicMetadata,
205195
return &meta, nil
206196
}
207197

198+
func validateAuthParams(authParams map[string]string) error {
199+
if authParams["accessID"] == "" {
200+
return errors.New("missing required auth params: accessID")
201+
}
202+
if authParams["accessKey"] == "" {
203+
return errors.New("missing required auth params: accessKey")
204+
}
205+
return nil
206+
}
207+
208+
func validateMetadata(metadata map[string]string) error {
209+
if metadata["host"] == "" {
210+
return errors.New("missing required metadata: host")
211+
}
212+
if metadata["queryType"] == "" {
213+
return errors.New("missing required metadata: queryType")
214+
}
215+
if metadata["queryType"] != "logs" && metadata["queryType"] != "metrics" {
216+
return fmt.Errorf("invalid queryType: %s, must be 'logs' or 'metrics'", metadata["queryType"])
217+
}
218+
return nil
219+
}
220+
208221
func parseMultiMetricsQueries(triggerMetadata map[string]string) (map[string]string, error) {
209222
queries := make(map[string]string)
210223
for key, value := range triggerMetadata {
211224
if strings.HasPrefix(key, multiMetricsQueryPrefix) {
212-
rowId := strings.TrimPrefix(key, multiMetricsQueryPrefix)
213-
if rowId == "" {
214-
return nil, fmt.Errorf("malformed metadata, unable to parse rowId from key: %s", key)
225+
rowID := strings.TrimPrefix(key, multiMetricsQueryPrefix)
226+
if rowID == "" {
227+
return nil, fmt.Errorf("malformed metadata, unable to parse rowID from key: %s", key)
215228
}
216229
if value == "" {
217230
return nil, fmt.Errorf("malformed metadata, invalid value for key: %s", key)
218231
}
219-
queries[rowId] = value
232+
queries[rowID] = value
220233
}
221234
}
222235
return queries, nil
223236
}
224237

225-
func (s *sumologicScaler) GetMetricSpecForScaling(ctx context.Context) []v2.MetricSpec {
238+
func (s *sumologicScaler) GetMetricSpecForScaling(_ context.Context) []v2.MetricSpec {
226239
metricName := kedautil.NormalizeString(fmt.Sprintf("sumologic-%s", s.metadata.queryType))
227240
externalMetric := &v2.ExternalMetricSource{
228241
Metric: v2.MetricIdentifier{
@@ -237,14 +250,14 @@ func (s *sumologicScaler) GetMetricSpecForScaling(ctx context.Context) []v2.Metr
237250
}
238251

239252
// No need to close connections manually, but we can close idle HTTP connections
240-
func (s *sumologicScaler) Close(ctx context.Context) error {
253+
func (s *sumologicScaler) Close(_ context.Context) error {
241254
if s.client != nil {
242255
return s.client.Close()
243256
}
244257
return nil
245258
}
246259

247-
func (s *sumologicScaler) GetMetricsAndActivity(ctx context.Context, metricName string) ([]external_metrics.ExternalMetricValue, bool, error) {
260+
func (s *sumologicScaler) GetMetricsAndActivity(_ context.Context, metricName string) ([]external_metrics.ExternalMetricValue, bool, error) {
248261
var metric external_metrics.ExternalMetricValue
249262
var num float64
250263
var err error
@@ -253,7 +266,7 @@ func (s *sumologicScaler) GetMetricsAndActivity(ctx context.Context, metricName
253266
s.metadata.queryType,
254267
s.metadata.query,
255268
s.metadata.queries,
256-
s.metadata.resultQueryRowId,
269+
s.metadata.resultQueryRowID,
257270
s.metadata.quantization,
258271
s.metadata.rollup,
259272
s.metadata.resultField,

pkg/scalers/sumologic_scaler_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ var validSumologicMultiMetricsMetadata = map[string]string{
6666
"query.A": "fakeQueryA",
6767
"query.B": "fakeQueryB",
6868
"query.C": "fakeQueryC",
69-
"resultQueryRowId": "C",
69+
"resultQueryRowID": "C",
7070
}
7171

7272
var testSumologicMetadata = []parseSumologicMetadataTestData{

0 commit comments

Comments
 (0)