Skip to content

Commit

Permalink
chore: Fix issue after rebase, remove duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
tomazpu committed Jan 7, 2025
1 parent 3b3b15e commit 05a94e6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 45 deletions.
18 changes: 0 additions & 18 deletions pkg/client/dummy_clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
buckets "github.com/dynatrace/dynatrace-configuration-as-code-core/clients/buckets"
documents "github.com/dynatrace/dynatrace-configuration-as-code-core/clients/documents"
openpipeline "github.com/dynatrace/dynatrace-configuration-as-code-core/clients/openpipeline"
segments "github.com/dynatrace/dynatrace-configuration-as-code-core/clients/segments"
dtclient "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/client/dtclient"
)

Expand All @@ -38,7 +37,6 @@ var DummyClientSet = ClientSet{
BucketClient: &DummyBucketClient{},
DocumentClient: &DummyDocumentClient{},
OpenPipelineClient: &DummyOpenPipelineClient{},
SegmentClient: &DummySegmentsClient{},
}

var _ AutomationClient = (*DummyAutomationClient)(nil)
Expand Down Expand Up @@ -157,19 +155,3 @@ func (c *DummyOpenPipelineClient) GetAll(ctx context.Context) ([]coreapi.Respons
func (c *DummyOpenPipelineClient) Update(_ context.Context, _ string, _ []byte) (openpipeline.Response, error) {
return openpipeline.Response{}, nil
}

type DummySegmentsClient struct{}

// GetAll implements GrailFilterSegmentClient
func (c *DummySegmentsClient) GetAll(_ context.Context) ([]coreapi.Response, error) {
panic("unimplemented")
}

// Upsert implements GrailFilterSegmentClient
func (c *DummySegmentsClient) Upsert(_ context.Context, _ string, _ []byte) (segments.Response, error) {
return segments.Response{}, nil
}

func (c *DummySegmentsClient) Get(_ context.Context, _ string) (segments.Response, error) {
return segments.Response{}, nil
}
2 changes: 1 addition & 1 deletion pkg/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func deployConfig(ctx context.Context, c *config.Config, clientset *client.Clien
}

case config.Segment:
if featureflags.Temporary[featureflags.Segments].Enabled() {
if featureflags.Segments.Enabled() {
resolvedEntity, deployErr = segment.Deploy(ctx, clientset.SegmentClient, properties, renderedConfig, c)
} else {
deployErr = fmt.Errorf("unknown config-type (ID: %q)", c.Type.ID())
Expand Down
50 changes: 24 additions & 26 deletions pkg/deploy/internal/segment/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ type DeploySegmentClient interface {
Get(ctx context.Context, id string) (segment.Response, error)
}

type jsonResponse struct {
UID string `json:"uid"`
Owner string `json:"owner"`
ExternalId string `json:"externalId"`
}

func Deploy(ctx context.Context, client DeploySegmentClient, properties parameter.Properties, renderedConfig string, c *config.Config) (entities.ResolvedEntity, error) {
var request map[string]any
err := json.Unmarshal([]byte(renderedConfig), &request)
Expand Down Expand Up @@ -86,16 +92,11 @@ func deployWithOriginObjectId(ctx context.Context, client DeploySegmentClient, r
return "", fmt.Errorf("failed to marshal segment request: %w", err)
}

responseUpsert, err := deploy(ctx, client, c.OriginObjectId, payload, c)
_, err = deploy(ctx, client, c.OriginObjectId, payload, c)
if err != nil {
return "", fmt.Errorf("failed API request: %w", err)
}

//When post was executed we get payload where we read the id from
if responseUpsert.StatusCode == http.StatusCreated {
return extractUidFromResponse(responseUpsert)
}

return c.OriginObjectId, nil
}

Expand All @@ -105,20 +106,16 @@ func deployWithExternalId(ctx context.Context, client DeploySegmentClient, reque
return "", fmt.Errorf("failed to GET segments: %w", err)
}

var jsonResponse struct {
Id string `json:"uid"`
Owner string `json:"owner"`
ExternalId string `json:"externalId"`
}
var responseData jsonResponse
for _, segmentResponse := range segmentsResponses {
err = json.Unmarshal(segmentResponse.Data, &jsonResponse)
responseData, err = getJsonResponseFromSegmentsResponse(segmentResponse)
if err != nil {
return "", err
}
//In case of a match, the put needs additional fields
if jsonResponse.ExternalId == request["externalId"] {
request["uid"] = jsonResponse.Id
request["owner"] = jsonResponse.Owner
if responseData.ExternalId == request["externalId"] {
request["uid"] = responseData.UID
request["owner"] = responseData.Owner
break
}
}
Expand All @@ -128,17 +125,20 @@ func deployWithExternalId(ctx context.Context, client DeploySegmentClient, reque
return "", fmt.Errorf("failed to marshal segment request: %w", err)
}

responseUpsert, err := deploy(ctx, client, jsonResponse.Id, payload, c)
responseUpsert, err := deploy(ctx, client, responseData.UID, payload, c)
if err != nil {
return "", fmt.Errorf("failed API request: %w", err)
}

//When post was executed we get payload where we read the id from
//For a POST we need to parse the response again to read out the ID
if responseUpsert.StatusCode == http.StatusCreated {
return extractUidFromResponse(responseUpsert)
responseData, err = getJsonResponseFromSegmentsResponse(responseUpsert)
if err != nil {
return "", err
}
}

return jsonResponse.Id, nil
return responseData.UID, nil
}

func deploy(ctx context.Context, client DeploySegmentClient, id string, payload []byte, c *config.Config) (segment.Response, error) {
Expand Down Expand Up @@ -169,14 +169,12 @@ func createResolveEntity(id string, externalId string, properties parameter.Prop
}
}

func extractUidFromResponse(response segment.Response) (string, error) {
var jsonResponse struct {
Id string `json:"uid"`
}
err := json.Unmarshal(response.Data, &jsonResponse)
func getJsonResponseFromSegmentsResponse(rawResponse segment.Response) (jsonResponse, error) {
var response jsonResponse
err := json.Unmarshal(rawResponse.Data, &response)
if err != nil {
return "", fmt.Errorf("failed to unmarshal response: %w", err)
return jsonResponse{}, fmt.Errorf("failed to unmarshal response: %w", err)
}

return jsonResponse.Id, nil
return response, nil
}

0 comments on commit 05a94e6

Please sign in to comment.