Skip to content

Commit

Permalink
fix: Correctly index previous settings object when downloading ordere…
Browse files Browse the repository at this point in the history
…d settings objects (#1484)

* test: Add test demonstrating panic for ordered settings with discard

* fix: Correctly reference previous configuration
  • Loading branch information
arthurpitman committed May 17, 2024
1 parent 66f9c52 commit beb2bf5
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 10 deletions.
8 changes: 5 additions & 3 deletions pkg/download/settings/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ func download(client client.SettingsClient, schemas []schema, projectName string

func convertAllObjects(objects []dtclient.DownloadSettingsObject, projectName string, ordered bool, filters Filters) []config.Config {
result := make([]config.Config, 0, len(objects))
for i, o := range objects {
var previousConfig *config.Config = nil
for _, o := range objects {

if shouldFilterUnmodifiableSettings() && o.ModificationInfo != nil && !o.ModificationInfo.Modifiable && len(o.ModificationInfo.ModifiablePaths) == 0 {
log.WithFields(field.Type(o.SchemaId), field.F("object", o)).Debug("Discarded settings object %q (%s). Reason: Unmodifiable default setting.", o.ObjectId, o.SchemaId)
Expand Down Expand Up @@ -210,10 +211,11 @@ func convertAllObjects(objects []dtclient.DownloadSettingsObject, projectName st
OriginObjectId: o.ObjectId,
}

if ordered && i > 0 {
c.Parameters[config.InsertAfterParameter] = reference.NewWithCoordinate(result[i-1].Coordinate, "id")
if ordered && (previousConfig != nil) {
c.Parameters[config.InsertAfterParameter] = reference.NewWithCoordinate(previousConfig.Coordinate, "id")
}
result = append(result, c)
previousConfig = &c

}
return result
Expand Down
107 changes: 100 additions & 7 deletions pkg/download/settings/download_test.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/v2/pkg/config"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config/coordinate"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config/parameter"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config/parameter/reference"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config/parameter/value"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config/template"
v2 "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/project/v2"
Expand All @@ -39,7 +40,8 @@ import (
)

func TestDownloadAll(t *testing.T) {
uuid := idutils.GenerateUUIDFromString("oid1")
uuid1 := idutils.GenerateUUIDFromString("oid1")
uuid3 := idutils.GenerateUUIDFromString("oid3")

type mockValues struct {
Schemas func() (dtclient.SchemaList, error)
Expand Down Expand Up @@ -140,11 +142,11 @@ func TestDownloadAll(t *testing.T) {
},
want: v2.ConfigsPerType{"id1": {
{
Template: template.NewInMemoryTemplate(uuid, "{}"),
Template: template.NewInMemoryTemplate(uuid1, "{}"),
Coordinate: coordinate.Coordinate{
Project: "projectName",
Type: "sid1",
ConfigId: uuid,
ConfigId: uuid1,
},
Type: config.SettingsType{
SchemaId: "sid1",
Expand Down Expand Up @@ -225,11 +227,11 @@ func TestDownloadAll(t *testing.T) {
},
want: v2.ConfigsPerType{"id1": {
{
Template: template.NewInMemoryTemplate(uuid, "{}"),
Template: template.NewInMemoryTemplate(uuid1, "{}"),
Coordinate: coordinate.Coordinate{
Project: "projectName",
Type: "sid1",
ConfigId: uuid,
ConfigId: uuid1,
},
Type: config.SettingsType{
SchemaId: "sid1",
Expand Down Expand Up @@ -274,11 +276,11 @@ func TestDownloadAll(t *testing.T) {
},
want: v2.ConfigsPerType{"id1": {
{
Template: template.NewInMemoryTemplate(uuid, "{}"),
Template: template.NewInMemoryTemplate(uuid1, "{}"),
Coordinate: coordinate.Coordinate{
Project: "projectName",
Type: "sid1",
ConfigId: uuid,
ConfigId: uuid1,
},
Type: config.SettingsType{
SchemaId: "sid1",
Expand All @@ -292,6 +294,97 @@ func TestDownloadAll(t *testing.T) {
},
}},
},
{
name: "DownloadSettings - ordered settings with discard should not panic",
mockValues: mockValues{
ListSchemasCalls: 1,
Schemas: func() (dtclient.SchemaList, error) {
return dtclient.SchemaList{{SchemaId: "id1"}}, nil
},
GetSchema: func(schemaID string) (dtclient.Schema, error) {
return dtclient.Schema{SchemaId: "id1", Ordered: true}, nil
},
GetSchemaCalls: 1,
Settings: func() ([]dtclient.DownloadSettingsObject, error) {
return []dtclient.DownloadSettingsObject{
{
ExternalId: "ex1",
SchemaVersion: "sv1",
SchemaId: "sid1",
ObjectId: "oid1",
Scope: "tenant",
Value: json.RawMessage("{}"),
},
{
ExternalId: "ex2",
SchemaVersion: "sv1",
SchemaId: "sid1",
ObjectId: "oid2",
Scope: "tenant",
Value: json.RawMessage("{}"),
ModificationInfo: &dtclient.SettingsModificationInfo{
Modifiable: false,
},
},
{
ExternalId: "ex3",
SchemaVersion: "sv1",
SchemaId: "sid1",
ObjectId: "oid3",
Scope: "tenant",
Value: json.RawMessage("{}"),
},
}, nil
},
ListSettingsCalls: 1,
},
want: v2.ConfigsPerType{"id1": {
{
Template: template.NewInMemoryTemplate(uuid1, "{}"),
Coordinate: coordinate.Coordinate{
Project: "projectName",
Type: "sid1",
ConfigId: uuid1,
},
Type: config.SettingsType{
SchemaId: "sid1",
SchemaVersion: "sv1",
},
Parameters: map[string]parameter.Parameter{
config.ScopeParameter: &value.ValueParameter{Value: "tenant"},
},
Skip: false,
OriginObjectId: "oid1",
},
{
Template: template.NewInMemoryTemplate(uuid3, "{}"),
Coordinate: coordinate.Coordinate{
Project: "projectName",
Type: "sid1",
ConfigId: uuid3,
},
Type: config.SettingsType{
SchemaId: "sid1",
SchemaVersion: "sv1",
},
Parameters: map[string]parameter.Parameter{
config.ScopeParameter: &value.ValueParameter{Value: "tenant"},
config.InsertAfterParameter: &reference.ReferenceParameter{
ParameterReference: parameter.ParameterReference{
Config: coordinate.Coordinate{
Project: "projectName",
Type: "sid1",
ConfigId: uuid1,
},
Property: "id",
},
},
},
Skip: false,
OriginObjectId: "oid3",
},
}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit beb2bf5

Please sign in to comment.