Skip to content

Commit

Permalink
feat: add grail client
Browse files Browse the repository at this point in the history
  • Loading branch information
jskelin committed Oct 21, 2024
1 parent 096085e commit 87361f2
Show file tree
Hide file tree
Showing 6 changed files with 923 additions and 0 deletions.
88 changes: 88 additions & 0 deletions api/clients/grailfiltersegements/filtersegments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// @license
// Copyright 2024 Dynatrace LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package grailfiltersegements

import (
"bytes"
"context"
"fmt"
"net/http"
"net/url"

"github.com/dynatrace/dynatrace-configuration-as-code-core/api/rest"
)

const endpointPath = "platform/storage/filter-segments/v1/filter-segments"

type Client struct {
client *rest.Client
}

func NewClient(client *rest.Client) *Client {
c := &Client{
client: client,
}
return c
}

func (c Client) Get(ctx context.Context, id string, ro rest.RequestOptions) (*http.Response, error) {
path, err := url.JoinPath(endpointPath, id)
if err != nil {
return nil, fmt.Errorf("failed to create URL: %w", err)
}

// set default behavior to pick all information by default
if ro.QueryParams == nil {
ro.QueryParams = url.Values{}
ro.QueryParams.Add("add-fields", "INCLUDES")
ro.QueryParams.Add("add-fields", "VARIABLES")
ro.QueryParams.Add("add-fields", "EXTERNALID")
ro.QueryParams.Add("add-fields", "RESOURCECONTEXT")
}

return c.client.GET(ctx, path, ro)
}

func (c Client) List(ctx context.Context) (*http.Response, error) {
path := endpointPath + ":lean" // minimal set of information is enough
return c.client.GET(ctx, path, rest.RequestOptions{CustomShouldRetryFunc: rest.RetryIfTooManyRequests})
}

func (c Client) Create(ctx context.Context, data []byte) (*http.Response, error) {
r, err := c.client.POST(ctx, endpointPath, bytes.NewReader(data), rest.RequestOptions{CustomShouldRetryFunc: rest.RetryIfTooManyRequests})
if err != nil {
return nil, fmt.Errorf("failed to create new filter segment: %w", err)
}
return r, nil
}

func (c Client) Update(ctx context.Context, id string, data []byte, ro rest.RequestOptions) (*http.Response, error) {
path, err := url.JoinPath(endpointPath, id)
if err != nil {
return nil, fmt.Errorf("failed to join URL: %w", err)
}
return c.client.PUT(ctx, path, bytes.NewReader(data), ro)
}

func (c Client) Delete(ctx context.Context, id string) (*http.Response, error) {
if id == "" {
return nil, fmt.Errorf("id must be non-empty")
}
path, err := url.JoinPath(endpointPath, id)
if err != nil {
return nil, fmt.Errorf("failed to create URL: %w", err)
}
return c.client.DELETE(ctx, path, rest.RequestOptions{CustomShouldRetryFunc: rest.RetryIfTooManyRequests})
}
97 changes: 97 additions & 0 deletions api/clients/grailfiltersegements/filtersegments_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//go:build live

// @license
// Copyright 2024 Dynatrace LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package grailfiltersegements_test

import (
"context"
"net/http"
"net/url"
"os"
"testing"

"github.com/dynatrace/dynatrace-configuration-as-code-core/api/clients/grailfiltersegements"
"github.com/dynatrace/dynatrace-configuration-as-code-core/api/rest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/oauth2/clientcredentials"
)

func Test_List(t *testing.T) {
config := clientcredentials.Config{
ClientID: os.Getenv("CLIENT_ID"),
ClientSecret: os.Getenv("CLIENT_SECRET"),
TokenURL: os.Getenv("TOKEN_URL"),
}

u, _ := url.Parse(os.Getenv("BASE_URL"))
client := grailfiltersegements.NewClient(
rest.NewClient(u, config.Client(context.TODO())))

r, err := client.List(context.TODO())

require.NoError(t, err)

assert.Equal(t, http.StatusOK, r.StatusCode)
assert.NotEmpty(t, r.Body)
}

func Test_Create(t *testing.T) {
config := clientcredentials.Config{
ClientID: os.Getenv("CLIENT_ID"),
ClientSecret: os.Getenv("CLIENT_SECRET"),
TokenURL: os.Getenv("TOKEN_URL"),
}

u, _ := url.Parse(os.Getenv("BASE_URL"))
client := grailfiltersegements.NewClient(
rest.NewClient(u, config.Client(context.TODO())))

payload := []byte(`{
"name": "dev_environment",
"description": "only includes data of the dev environment",
"variables": {
"type": "query",
"value": "fetch logs | limit 1"
},
"isPublic": false
}'`)
r, err := client.Create(context.TODO(), payload)

require.NoError(t, err)

assert.Equal(t, http.StatusCreated, r.StatusCode)
assert.NotEmpty(t, r.Body)
}

func Test_Get(t *testing.T) {
config := clientcredentials.Config{
ClientID: os.Getenv("CLIENT_ID"),
ClientSecret: os.Getenv("CLIENT_SECRET"),
TokenURL: os.Getenv("TOKEN_URL"),
}

u, _ := url.Parse(os.Getenv("BASE_URL"))
client := grailfiltersegements.NewClient(
rest.NewClient(u, config.Client(context.TODO())))

r, err := client.Get(context.TODO(), "QElQbQcjq3S", rest.RequestOptions{})

require.NoError(t, err)

assert.Equal(t, http.StatusOK, r.StatusCode)
assert.NotEmpty(t, r.Body)
}
10 changes: 10 additions & 0 deletions clients/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/dynatrace/dynatrace-configuration-as-code-core/clients/automation"
"github.com/dynatrace/dynatrace-configuration-as-code-core/clients/buckets"
"github.com/dynatrace/dynatrace-configuration-as-code-core/clients/documents"
"github.com/dynatrace/dynatrace-configuration-as-code-core/clients/grailfiltersegments"
"github.com/dynatrace/dynatrace-configuration-as-code-core/clients/openpipeline"
"golang.org/x/oauth2/clientcredentials"
)
Expand Down Expand Up @@ -172,6 +173,15 @@ func (f factory) DocumentClient() (*documents.Client, error) {
return documents.NewClient(restClient), nil
}

// FilterSegmentsClient creates and returns a new instance of grailfiltersegments.Client for interacting with the grail filter segments API.
func (f factory) FilterSegmentsClient() (*grailfiltersegments.Client, error) {
restClient, err := f.CreatePlatformClient()
if err != nil {
return nil, err
}
return grailfiltersegments.NewClient(restClient), nil
}

// BucketClientWithRetrySettings creates and returns a new instance of buckets.Client with non-default retry settings.
// For details about how retry settings are used, see buckets.WithRetrySettings.
func (f factory) BucketClientWithRetrySettings(maxRetries int, durationBetweenTries time.Duration, maxWaitDuration time.Duration) (*buckets.Client, error) {
Expand Down
19 changes: 19 additions & 0 deletions clients/grailfiltersegments/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// @license
// Copyright 2024 Dynatrace LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package grailfiltersegments

func NewTestClient(client client) *Client {
return &Client{client: client}
}
Loading

0 comments on commit 87361f2

Please sign in to comment.