From 6d38833872f9736c847f60bd6e49e8abe3135049 Mon Sep 17 00:00:00 2001 From: Ivan Santos <301291+pragmaticivan@users.noreply.github.com> Date: Mon, 11 Apr 2022 14:19:10 -0300 Subject: [PATCH] feat(newrelic): newrelic account value from authentication resources fixes #2883 Signed-off-by: Ivan Santos --- pkg/scalers/newrelic_scaler.go | 16 ++++++----- pkg/scalers/newrelic_scaler_test.go | 41 +++++++++++++++++------------ 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/pkg/scalers/newrelic_scaler.go b/pkg/scalers/newrelic_scaler.go index 5b414584d79..40360238a0b 100644 --- a/pkg/scalers/newrelic_scaler.go +++ b/pkg/scalers/newrelic_scaler.go @@ -78,16 +78,18 @@ func NewNewRelicScaler(config *ScalerConfig) (Scaler, error) { func parseNewRelicMetadata(config *ScalerConfig) (*newrelicMetadata, error) { meta := newrelicMetadata{} var err error - if val, ok := config.TriggerMetadata[account]; ok && val != "" { - t, err := strconv.Atoi(val) - if err != nil { - return nil, fmt.Errorf("error parsing %s: %s", account, err) - } - meta.account = t - } else { + + val, err := GetFromAuthOrMeta(config, account) + if err != nil { return nil, fmt.Errorf("no %s given", account) } + t, err := strconv.Atoi(val) + if err != nil { + return nil, fmt.Errorf("error parsing %s: %s", account, err) + } + meta.account = t + if val, ok := config.TriggerMetadata[nrql]; ok && val != "" { meta.nrql = val } else { diff --git a/pkg/scalers/newrelic_scaler_test.go b/pkg/scalers/newrelic_scaler_test.go index 66d1f236c69..cabd142280c 100644 --- a/pkg/scalers/newrelic_scaler_test.go +++ b/pkg/scalers/newrelic_scaler_test.go @@ -7,8 +7,9 @@ import ( ) type parseNewRelicMetadataTestData struct { - metadata map[string]string - isError bool + metadata map[string]string + authParams map[string]string + isError bool } type newrelicMetricIdentifier struct { @@ -18,28 +19,34 @@ type newrelicMetricIdentifier struct { } var testNewRelicMetadata = []parseNewRelicMetadataTestData{ - {map[string]string{}, true}, + {map[string]string{}, map[string]string{}, true}, // all properly formed - {map[string]string{"account": "0", "threshold": "100", "queryKey": "somekey", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, false}, + {map[string]string{"account": "0", "threshold": "100", "queryKey": "somekey", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, map[string]string{}, false}, // all properly formed - {map[string]string{"account": "0", "region": "EU", "threshold": "100", "queryKey": "somekey", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, false}, + {map[string]string{"account": "0", "region": "EU", "threshold": "100", "queryKey": "somekey", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, map[string]string{}, false}, + // account passed via auth params + {map[string]string{"region": "EU", "threshold": "100", "queryKey": "somekey", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, map[string]string{"account": "0"}, false}, + // region passed via auth params + {map[string]string{"account": "0", "threshold": "100", "queryKey": "somekey", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, map[string]string{"region": "EU"}, false}, // account as String - {map[string]string{"account": "ABC", "threshold": "100", "queryKey": "somekey", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, true}, + {map[string]string{"account": "ABC", "threshold": "100", "queryKey": "somekey", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, map[string]string{}, true}, // missing account - {map[string]string{"threshold": "100", "queryKey": "somekey", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, true}, + {map[string]string{"threshold": "100", "queryKey": "somekey", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, map[string]string{}, true}, + // missing account + {map[string]string{"threshold": "100", "queryKey": "somekey", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, map[string]string{}, true}, // malformed threshold - {map[string]string{"account": "0", "threshold": "one", "queryKey": "somekey", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, true}, + {map[string]string{"account": "0", "threshold": "one", "queryKey": "somekey", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, map[string]string{}, true}, // missing threshold - {map[string]string{"account": "0", "queryKey": "somekey", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, true}, + {map[string]string{"account": "0", "queryKey": "somekey", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, map[string]string{}, true}, // missing query - {map[string]string{"account": "0", "threshold": "100", "queryKey": "somekey"}, true}, + {map[string]string{"account": "0", "threshold": "100", "queryKey": "somekey"}, map[string]string{}, true}, // noDataError invalid value - {map[string]string{"account": "0", "threshold": "100", "queryKey": "somekey", "noDataError": "invalid", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, true}, + {map[string]string{"account": "0", "threshold": "100", "queryKey": "somekey", "noDataError": "invalid", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, map[string]string{}, true}, // noDataError valid value - {map[string]string{"account": "0", "threshold": "100", "queryKey": "somekey", "noDataError": "true", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, false}, - {map[string]string{"account": "0", "threshold": "100", "queryKey": "somekey", "noDataError": "false", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, false}, - {map[string]string{"account": "0", "threshold": "100", "queryKey": "somekey", "noDataError": "0", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, false}, - {map[string]string{"account": "0", "threshold": "100", "queryKey": "somekey", "noDataError": "1", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, false}, + {map[string]string{"account": "0", "threshold": "100", "queryKey": "somekey", "noDataError": "true", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, map[string]string{}, false}, + {map[string]string{"account": "0", "threshold": "100", "queryKey": "somekey", "noDataError": "false", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, map[string]string{}, false}, + {map[string]string{"account": "0", "threshold": "100", "queryKey": "somekey", "noDataError": "0", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, map[string]string{}, false}, + {map[string]string{"account": "0", "threshold": "100", "queryKey": "somekey", "noDataError": "1", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, map[string]string{}, false}, } var newrelicMetricIdentifiers = []newrelicMetricIdentifier{ @@ -49,7 +56,7 @@ var newrelicMetricIdentifiers = []newrelicMetricIdentifier{ func TestNewRelicParseMetadata(t *testing.T) { for _, testData := range testNewRelicMetadata { - _, err := parseNewRelicMetadata(&ScalerConfig{TriggerMetadata: testData.metadata}) + _, err := parseNewRelicMetadata(&ScalerConfig{TriggerMetadata: testData.metadata, AuthParams: testData.authParams}) if err != nil && !testData.isError { fmt.Printf("X: %s", testData.metadata) t.Error("Expected success but got error", err) @@ -62,7 +69,7 @@ func TestNewRelicParseMetadata(t *testing.T) { } func TestNewRelicGetMetricSpecForScaling(t *testing.T) { for _, testData := range newrelicMetricIdentifiers { - meta, err := parseNewRelicMetadata(&ScalerConfig{TriggerMetadata: testData.metadataTestData.metadata, ScalerIndex: testData.scalerIndex}) + meta, err := parseNewRelicMetadata(&ScalerConfig{TriggerMetadata: testData.metadataTestData.metadata, AuthParams: testData.metadataTestData.authParams, ScalerIndex: testData.scalerIndex}) if err != nil { t.Fatal("Could not parse metadata:", err) }