-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
820 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} |
Oops, something went wrong.