diff --git a/operator/controllers/common/providers/datadog/datadog.go b/operator/controllers/common/providers/datadog/datadog.go index 08fd521d60..9b114de025 100644 --- a/operator/controllers/common/providers/datadog/datadog.go +++ b/operator/controllers/common/providers/datadog/datadog.go @@ -2,9 +2,13 @@ package datadog import ( "context" + "encoding/json" + "github.com/DataDog/datadog-api-client-go/v2/api/datadog" + "github.com/DataDog/datadog-api-client-go/v2/api/datadogV1" "github.com/go-logr/logr" klcv1alpha2 "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha2" "net/http" + "time" ) type KeptnDataDogProvider struct { @@ -13,6 +17,21 @@ type KeptnDataDogProvider struct { } func (k KeptnDataDogProvider) EvaluateQuery(ctx context.Context, objective klcv1alpha2.Objective, provider klcv1alpha2.KeptnEvaluationProvider) (string, []byte, error) { - //TODO implement me - panic("implement me") + + ctx, cancel := context.WithTimeout(ctx, 20*time.Second) + defer cancel() + + k.Log.Info("Running query: " + provider.Spec.TargetServer + "/api/v1/slo") + + cfg := datadog.NewConfiguration() + client := datadog.NewAPIClient(cfg) + api := datadogV1.NewServiceLevelObjectivesApi(client) + resp, _, err := api.ListSLOs(ctx, *datadogV1.NewListSLOsOptionalParameters()) + + if err != nil { + return "", nil, err + } + + responseContent, _ := json.MarshalIndent(resp, "", " ") + return string(responseContent), responseContent, nil } diff --git a/operator/controllers/common/providers/datadog/datadog_test.go b/operator/controllers/common/providers/datadog/datadog_test.go new file mode 100644 index 0000000000..5775d8cb6f --- /dev/null +++ b/operator/controllers/common/providers/datadog/datadog_test.go @@ -0,0 +1,57 @@ +package datadog + +import ( + "context" + "fmt" + "github.com/DataDog/datadog-api-client-go/v2/api/datadog" + "net/http" + "os" + "testing" + + klcv1alpha2 "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha2" + "github.com/stretchr/testify/require" + v1 "k8s.io/api/core/v1" + ctrl "sigs.k8s.io/controller-runtime" +) + +func Test_datadog(t *testing.T) { + + t.Run("datadog-test", func(t *testing.T) { + + kpp := KeptnDataDogProvider{ + HttpClient: http.Client{}, + Log: ctrl.Log.WithName("testytest"), + } + obj := klcv1alpha2.Objective{ + Query: "garbage", + } + p := klcv1alpha2.KeptnEvaluationProvider{ + Spec: klcv1alpha2.KeptnEvaluationProviderSpec{ + SecretKeyRef: v1.SecretKeySelector{ + LocalObjectReference: v1.LocalObjectReference{ + Name: "myapitoken", + }, + Key: "mykey", + }, + TargetServer: "http://mockserver", // TODO: pass mock server url + }, + } + ctx := context.WithValue( + context.Background(), + datadog.ContextAPIKeys, + map[string]datadog.APIKey{ + "apiKeyAuth": { + Key: "DD_CLIENT_API_KEY", + }, + "appKeyAuth": { + Key: "DD_CLIENT_APP_KEY", + }, + }, + ) + r, _, _ := kpp.EvaluateQuery(ctx, obj, p) + fmt.Fprintf(os.Stdout, r) + require.Equal(t, "0", "1") + + }) + +}