Skip to content

Commit

Permalink
fix: changes related to PR
Browse files Browse the repository at this point in the history
  • Loading branch information
jskelin committed Dec 12, 2024
1 parent 867f905 commit 43b708f
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 26 deletions.
3 changes: 2 additions & 1 deletion cmd/monaco/download/download_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/client"
clientAuth "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/client/auth"
versionClient "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/client/version"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/manifest"
)

Expand Down Expand Up @@ -101,7 +102,7 @@ func GetDownloadCommand(fs afero.Fs, command Command) (cmd *cobra.Command) {
}

if featureflags.Temporary[featureflags.GrailFilterSegment].Enabled() {
cmd.Flags().BoolVar(&f.onlyGrailFilterSegments, "only-filtersegments", false, "Only download Grail filter-segment configurations, skip all other configuration types")
cmd.Flags().BoolVar(&f.onlyGrailFilterSegments, "only-"+string(config.GrailFilterSegmentID)+"s", false, "Only download Grail filter-segment configurations, skip all other configuration types")
}

err := errors.Join(
Expand Down
32 changes: 16 additions & 16 deletions cmd/monaco/download/download_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,23 +235,23 @@ func doDownloadConfigs(fs afero.Fs, clientSet *client.ClientSet, apisToDownload
}

type downloadFn struct {
classicDownload func(client.ConfigClient, string, api.APIs, classic.ContentFilters) (projectv2.ConfigsPerType, error)
settingsDownload func(client.SettingsClient, string, settings.Filters, ...config.SettingsType) (projectv2.ConfigsPerType, error)
automationDownload func(client.AutomationClient, string, ...config.AutomationType) (projectv2.ConfigsPerType, error)
bucketDownload func(client.BucketClient, string) (projectv2.ConfigsPerType, error)
documentDownload func(client.DocumentClient, string) (projectv2.ConfigsPerType, error)
openPipelineDownload func(client.OpenPipelineClient, string) (projectv2.ConfigsPerType, error)
grailFilterSegment func(client.GrailFilterSegmentClient, string) (projectv2.ConfigsPerType, error)
classicDownload func(client.ConfigClient, string, api.APIs, classic.ContentFilters) (projectv2.ConfigsPerType, error)
settingsDownload func(client.SettingsClient, string, settings.Filters, ...config.SettingsType) (projectv2.ConfigsPerType, error)
automationDownload func(client.AutomationClient, string, ...config.AutomationType) (projectv2.ConfigsPerType, error)
bucketDownload func(client.BucketClient, string) (projectv2.ConfigsPerType, error)
documentDownload func(client.DocumentClient, string) (projectv2.ConfigsPerType, error)
openPipelineDownload func(client.OpenPipelineClient, string) (projectv2.ConfigsPerType, error)
grailFilterSegmentDownload func(client.GrailFilterSegmentClient, string) (projectv2.ConfigsPerType, error)
}

var defaultDownloadFn = downloadFn{
classicDownload: classic.Download,
settingsDownload: settings.Download,
automationDownload: automation.Download,
bucketDownload: bucket.Download,
documentDownload: document.Download,
openPipelineDownload: openpipeline.Download,
grailFilterSegment: grailfiltersegment.Download,
classicDownload: classic.Download,
settingsDownload: settings.Download,
automationDownload: automation.Download,
bucketDownload: bucket.Download,
documentDownload: document.Download,
openPipelineDownload: openpipeline.Download,
grailFilterSegmentDownload: grailfiltersegment.Download,
}

func downloadConfigs(clientSet *client.ClientSet, apisToDownload api.APIs, opts downloadConfigsOptions, fn downloadFn) (project.ConfigsPerType, error) {
Expand Down Expand Up @@ -329,11 +329,11 @@ func downloadConfigs(clientSet *client.ClientSet, apisToDownload api.APIs, opts

if featureflags.Temporary[featureflags.GrailFilterSegment].Enabled() {
if shouldDownloadGrailFilterSegments(opts) {
cgfs, err := fn.grailFilterSegment(clientSet.GrailFilterSegmentClient, opts.projectName)
grailFilterSegmentCgfs, err := fn.grailFilterSegmentDownload(clientSet.GrailFilterSegmentClient, opts.projectName)
if err != nil {
return nil, err
}
copyConfigs(configs, cgfs)
copyConfigs(configs, grailFilterSegmentCgfs)
} else if opts.onlyGrailFilterSegment {
return nil, errors.New("can't download filter-segment resources: no OAuth credentials configured")
}
Expand Down
20 changes: 12 additions & 8 deletions cmd/monaco/download/download_configs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"

"github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/featureflags"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/testutils"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/api"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/client"
Expand Down Expand Up @@ -195,12 +196,13 @@ func TestDownload_Options(t *testing.T) {
},
},
wantDownload{
config: true,
settings: true,
bucket: true,
automation: true,
document: true,
openpipeline: true,
config: true,
settings: true,
bucket: true,
automation: true,
document: true,
openpipeline: true,
grailFilterSegment: true,
},
},
{
Expand Down Expand Up @@ -238,7 +240,8 @@ func TestDownload_Options(t *testing.T) {
auth: manifest.Auth{OAuth: &manifest.OAuth{}},
}},
wantDownload{openpipeline: true},
}, {
},
{
"only grail filter-segment requested",
downloadConfigsOptions{
onlyGrailFilterSegment: true,
Expand Down Expand Up @@ -289,6 +292,7 @@ func TestDownload_Options(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv(featureflags.Temporary[featureflags.GrailFilterSegment].EnvName(), "true")
fn := downloadFn{
classicDownload: func(client.ConfigClient, string, api.APIs, classic.ContentFilters) (projectv2.ConfigsPerType, error) {
if !tt.want.config {
Expand Down Expand Up @@ -326,7 +330,7 @@ func TestDownload_Options(t *testing.T) {
}
return nil, nil
},
grailFilterSegment: func(b client.GrailFilterSegmentClient, s string) (projectv2.ConfigsPerType, error) {
grailFilterSegmentDownload: func(b client.GrailFilterSegmentClient, s string) (projectv2.ConfigsPerType, error) {
if !tt.want.grailFilterSegment {
t.Fatalf("grail file-segment download was not meant to be called but was")
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/download/grailfiltersegment/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ func createConfig(projectName string, response openpipeline.Response) (config.Co

id, ok := jsonObj.Get("uid").(string)
if !ok {
return config.Config{}, fmt.Errorf("failed to extract id as string from payload")
return config.Config{}, fmt.Errorf("API payload is missing \"uid\"")
}

// delete fields that prevent a re-upload of the configuration
jsonObj.Delete("uid")
jsonObj.Delete("version")
jsonObj.Delete("externalId")
Expand Down
61 changes: 61 additions & 0 deletions pkg/download/grailfiltersegment/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,67 @@ func TestDownloader_Download(t *testing.T) {
require.Len(t, result[string(config.GrailFilterSegmentID)], 2, "all listed segments should be downloaded")
})

t.Run("grail filter-segment without uio is ignored", func(t *testing.T) {
t.Setenv(featureflags.Temporary[featureflags.GrailFilterSegment].EnvName(), "true")
server := testutils.NewHTTPTestServer(t, []testutils.ResponseDef{
{
GET: func(t *testing.T, req *http.Request) testutils.Response {
data, err := os.ReadFile("./testdata/listResponse.json")
assert.NoError(t, err)

return testutils.Response{
ResponseCode: http.StatusOK,
ResponseBody: string(data),
}
},
ValidateRequest: func(t *testing.T, request *http.Request) {
assert.Equal(t, "/platform/storage/filter-segments/v1/filter-segments:lean", request.URL.Path)
assert.Equal(t, "add-fields=EXTERNALID", request.URL.RawQuery)
},
},
{
GET: func(t *testing.T, req *http.Request) testutils.Response {
data, err := os.ReadFile("./testdata/uid_1_getResponse_wo_uid.json")
assert.NoError(t, err)

return testutils.Response{
ResponseCode: http.StatusOK,
ResponseBody: string(data),
}
},
ValidateRequest: func(t *testing.T, request *http.Request) {
assert.Equal(t, "/platform/storage/filter-segments/v1/filter-segments/uid_1", request.URL.Path)
assert.Equal(t, "add-fields=INCLUDES&add-fields=VARIABLES&add-fields=EXTERNALID&add-fields=RESOURCECONTEXT", request.URL.RawQuery)
},
},
{
GET: func(t *testing.T, req *http.Request) testutils.Response {
data, err := os.ReadFile("./testdata/uid_2_getResponse.json")
assert.NoError(t, err)

return testutils.Response{
ResponseCode: http.StatusOK,
ResponseBody: string(data),
}
},
ValidateRequest: func(t *testing.T, request *http.Request) {
assert.Equal(t, "/platform/storage/filter-segments/v1/filter-segments/uid_2", request.URL.Path)
assert.Equal(t, "add-fields=INCLUDES&add-fields=VARIABLES&add-fields=EXTERNALID&add-fields=RESOURCECONTEXT", request.URL.RawQuery)
},
},
})
defer server.Close()

client := coreLib.NewClient(rest.NewClient(server.URL(), server.Client()))
result, err := grailfiltersegment.Download(client, "project")

assert.NoError(t, err)
assert.Len(t, result, 1)

assert.Len(t, result[string(config.GrailFilterSegmentID)], 1, "all listed segments should be downloaded")
assert.Equal(t, "uid_2", result[string(config.GrailFilterSegmentID)][0].OriginObjectId)
})

t.Run("no error downloading grail filter segments with faulty client", func(t *testing.T) {
server := testutils.NewHTTPTestServer(t, []testutils.ResponseDef{})
defer server.Close()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "private_grail_filter_segment",
"description": "description for private test grail filter segment",
"variables": {
"type": "query",
"value": "fetch logs | limit 1"
},
"isPublic": false,
"owner": "bd62720e-e72a-49c6-aff2-43ca82662bee",
"allowedOperations": [
"READ",
"WRITE",
"DELETE"
],
"includes": [],
"version": 1
}

0 comments on commit 43b708f

Please sign in to comment.