From 0f06f3731839c8459fbb5e680293cbcb60e8e172 Mon Sep 17 00:00:00 2001 From: Rakshit Gondwal Date: Wed, 23 Aug 2023 16:13:59 +0530 Subject: [PATCH] fix after review Signed-off-by: Rakshit Gondwal --- .../providers/dynatrace/client/client.go | 2 +- .../providers/dynatrace/client/client_test.go | 4 +- .../providers/dynatrace/dynatrace_dql.go | 44 +++++++++---------- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/metrics-operator/controllers/common/providers/dynatrace/client/client.go b/metrics-operator/controllers/common/providers/dynatrace/client/client.go index 3e985e2f9c..1dc2324061 100644 --- a/metrics-operator/controllers/common/providers/dynatrace/client/client.go +++ b/metrics-operator/controllers/common/providers/dynatrace/client/client.go @@ -58,7 +58,7 @@ func NewAPIClient(config apiConfig, options ...APIClientOption) *apiClient { // Do sends and API request to the Dynatrace API and returns its result as a string containing the raw response payload func (client *apiClient) Do(ctx context.Context, path, method string, payload []byte) ([]byte, int, error) { if err := client.auth(ctx); err != nil { - return nil, http.StatusInternalServerError, err + return nil, http.StatusUnauthorized, err } api := fmt.Sprintf("%s%s", client.config.serverURL, path) req, err := http.NewRequestWithContext(ctx, method, api, bytes.NewBuffer(payload)) diff --git a/metrics-operator/controllers/common/providers/dynatrace/client/client_test.go b/metrics-operator/controllers/common/providers/dynatrace/client/client_test.go index 3333ab3bff..7996582680 100644 --- a/metrics-operator/controllers/common/providers/dynatrace/client/client_test.go +++ b/metrics-operator/controllers/common/providers/dynatrace/client/client_test.go @@ -91,7 +91,7 @@ func TestAPIClientAuthError(t *testing.T) { require.ErrorIs(t, err, ErrRequestFailed) require.Empty(t, resp) - require.Equal(t, http.StatusInternalServerError, code) + require.Equal(t, http.StatusUnauthorized, code) } func TestAPIClientAuthNoToken(t *testing.T) { @@ -129,7 +129,7 @@ func TestAPIClientAuthNoToken(t *testing.T) { require.ErrorIs(t, err, ErrAuthenticationFailed) require.Empty(t, resp) - require.Equal(t, http.StatusInternalServerError, code) + require.Equal(t, http.StatusUnauthorized, code) } func TestAPIClientRequestError(t *testing.T) { diff --git a/metrics-operator/controllers/common/providers/dynatrace/dynatrace_dql.go b/metrics-operator/controllers/common/providers/dynatrace/dynatrace_dql.go index 543bcb281e..6e6fca2c02 100644 --- a/metrics-operator/controllers/common/providers/dynatrace/dynatrace_dql.go +++ b/metrics-operator/controllers/common/providers/dynatrace/dynatrace_dql.go @@ -97,17 +97,7 @@ func NewKeptnDynatraceDQLProvider(k8sClient client.Client, opts ...KeptnDynatrac } func (d *keptnDynatraceDQLProvider) EvaluateQuery(ctx context.Context, metric metricsapi.KeptnMetric, provider metricsapi.KeptnMetricsProvider) (string, []byte, error) { - if err := d.ensureDTClientIsSetUp(ctx, provider); err != nil { - return "", nil, err - } - - b, status, err := d.postDQL(ctx, metric) - if err != nil { - d.log.Error(err, "Error while posting the DQL query", "query", metric.Spec.Query) - return "", nil, err - } - - results, err := d.parseDQLResults(b, status) + results, err := d.getResults(ctx, metric, provider) if err != nil { return "", nil, err } @@ -117,7 +107,7 @@ func (d *keptnDynatraceDQLProvider) EvaluateQuery(ctx context.Context, metric me } r := fmt.Sprintf("%f", results.Records[0].Value.Avg) - b, err = json.Marshal(results) + b, err := json.Marshal(results) if err != nil { d.log.Error(err, "Error marshaling DQL results") } @@ -126,28 +116,36 @@ func (d *keptnDynatraceDQLProvider) EvaluateQuery(ctx context.Context, metric me } func (d *keptnDynatraceDQLProvider) EvaluateQueryForStep(ctx context.Context, metric metricsapi.KeptnMetric, provider metricsapi.KeptnMetricsProvider) ([]string, []byte, error) { - if err := d.ensureDTClientIsSetUp(ctx, provider); err != nil { + results, err := d.getResults(ctx, metric, provider) + if err != nil { return nil, nil, err } - b, status, err := d.postDQL(ctx, metric) + r := d.getResultSlice(results) + b, err := json.Marshal(results) if err != nil { - d.log.Error(err, "Error while posting the DQL query", "query", metric.Spec.Query) - return nil, nil, err + d.log.Error(err, "Error marshaling DQL results") } - results, err := d.parseDQLResults(b, status) - if err != nil { - return nil, nil, err + return r, b, nil +} + +func (d *keptnDynatraceDQLProvider) getResults(ctx context.Context, metric metricsapi.KeptnMetric, provider metricsapi.KeptnMetricsProvider) (*DQLResult, error) { + if err := d.ensureDTClientIsSetUp(ctx, provider); err != nil { + return nil, err } - r := d.getResultSlice(results) - b, err = json.Marshal(results) + b, status, err := d.postDQL(ctx, metric) if err != nil { - d.log.Error(err, "Error marshaling DQL results") + d.log.Error(err, "Error while posting the DQL query", "query", metric.Spec.Query) + return nil, err } - return r, b, nil + results, err := d.parseDQLResults(b, status) + if err != nil { + return nil, err + } + return results, nil } func (d *keptnDynatraceDQLProvider) parseDQLResults(b []byte, status int) (*DQLResult, error) {