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 Nov 13, 2024
1 parent e19fc3c commit c006cc9
Show file tree
Hide file tree
Showing 5 changed files with 809 additions and 0 deletions.
87 changes: 87 additions & 0 deletions api/clients/grailfiltersegements/filtersegments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// @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
}

// Get filter-segment by UID. If querry parameter "add-fields" is not specified, it will be set with next values: "INCLUDES", "VARIABLES", "EXTERNALID", "RESOURCECONTEXT"
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.Has("add-fields") {
ro.QueryParams = url.Values{
"add-fields": []string{"INCLUDES", "VARIABLES", "EXTERNALID", "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})
}
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 c006cc9

Please sign in to comment.