From 0242b9856103ddd91233a277ae955a5449fa4758 Mon Sep 17 00:00:00 2001 From: Jorge Turrado Ferrero Date: Mon, 4 Oct 2021 10:20:47 +0200 Subject: [PATCH] Add support to get connection data from Trigger Authorization in MSSQL Scaler (#2112) Signed-off-by: jorturfer Co-authored-by: Zbynek Roubalik <726523+zroubalik@users.noreply.github.com> --- CHANGELOG.md | 1 + pkg/scalers/mssql_scaler.go | 33 ++++++++++++++++++++++---------- pkg/scalers/mssql_scaler_test.go | 8 ++++++++ 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c82ebb2ec3c..c9e6320e45a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ - Escape `queueName` and `vhostName` in RabbitMQ Scaler before use them in query string (bug fix) ([#2055](https://github.com/kedacore/keda/pull/2055)) - TriggerAuthentication/Vault: add support for HashiCorp Vault namespace (Vault Enterprise) ([#2085](https://github.com/kedacore/keda/pull/2085)) - Add custom http timeout in RabbitMQ Scaler ([#2086](https://github.com/kedacore/keda/pull/2086)) +- Add support to get connection data from Trigger Authorization in MSSQL Scaler ([#2112](https://github.com/kedacore/keda/pull/2112)) - Add support to provide the metric name in Azure Log Analytics Scaler ([#2106](https://github.com/kedacore/keda/pull/2106)) ### Breaking Changes diff --git a/pkg/scalers/mssql_scaler.go b/pkg/scalers/mssql_scaler.go index 805bae96f29..981327b887e 100644 --- a/pkg/scalers/mssql_scaler.go +++ b/pkg/scalers/mssql_scaler.go @@ -108,28 +108,41 @@ func parseMSSQLMetadata(config *ScalerConfig) (*mssqlMetadata, error) { meta.connectionString = config.ResolvedEnv[config.TriggerMetadata["connectionStringFromEnv"]] default: meta.connectionString = "" - if val, ok := config.TriggerMetadata["host"]; ok { - meta.host = val - } else { + + if config.AuthParams["host"] != "" { + meta.host = config.AuthParams["host"] + } else if config.TriggerMetadata["host"] != "" { + meta.host = config.TriggerMetadata["host"] + } + if meta.host == "" { return nil, fmt.Errorf("no host given") } - if val, ok := config.TriggerMetadata["port"]; ok { - port, err := strconv.Atoi(val) + var paramPort string + if config.AuthParams["port"] != "" { + paramPort = config.AuthParams["port"] + } else if config.TriggerMetadata["port"] != "" { + paramPort = config.TriggerMetadata["port"] + } + if paramPort != "" { + port, err := strconv.Atoi(paramPort) if err != nil { return nil, fmt.Errorf("port parsing error %s", err.Error()) } - meta.port = port } - if val, ok := config.TriggerMetadata["username"]; ok { - meta.username = val + if config.AuthParams["username"] != "" { + meta.username = config.AuthParams["username"] + } else if config.TriggerMetadata["username"] != "" { + meta.username = config.TriggerMetadata["username"] } // database is optional in SQL s - if val, ok := config.TriggerMetadata["database"]; ok { - meta.database = val + if config.AuthParams["database"] != "" { + meta.database = config.AuthParams["database"] + } else if config.TriggerMetadata["database"] != "" { + meta.database = config.TriggerMetadata["database"] } if config.AuthParams["password"] != "" { diff --git a/pkg/scalers/mssql_scaler_test.go b/pkg/scalers/mssql_scaler_test.go index 8f5291333a8..b8d9043976e 100644 --- a/pkg/scalers/mssql_scaler_test.go +++ b/pkg/scalers/mssql_scaler_test.go @@ -65,6 +65,14 @@ var testInputs = []mssqlTestData{ expectedMetricName: "mssql-AdventureWorks", expectedConnectionString: "sqlserver://user2:Password%232@example.database.windows.net?database=AdventureWorks", }, + // connection string generated from full authParams + { + metadata: map[string]string{"query": "SELECT 1", "targetValue": "1"}, + resolvedEnv: map[string]string{}, + authParams: map[string]string{"password": "Password#2", "host": "example.database.windows.net", "username": "user2", "database": "AdventureWorks", "port": "1433"}, + expectedMetricName: "mssql-AdventureWorks", + expectedConnectionString: "sqlserver://user2:Password%232@example.database.windows.net:1433?database=AdventureWorks", + }, // variation of previous: no database name, metricName from host { metadata: map[string]string{"query": "SELECT 1", "targetValue": "1", "host": "example.database.windows.net", "username": "user3"},