Skip to content

Commit

Permalink
chore: remove redundant data from v2.Project
Browse files Browse the repository at this point in the history
  • Loading branch information
jskelin committed Dec 3, 2024
1 parent 63b1ae4 commit 8dd6d57
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 191 deletions.
18 changes: 10 additions & 8 deletions pkg/converter/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ package converter

import (
"fmt"
"reflect"
"testing"

"github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/template"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config/parameter"
compoundParam "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config/parameter/compound"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/converter/v1environment"
"reflect"
"testing"

"github.com/spf13/afero"
"github.com/stretchr/testify/assert"

"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/api"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config/coordinate"
Expand All @@ -34,8 +38,6 @@ import (
valueParam "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config/parameter/value"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/manifest"
projectV1 "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/project/v1"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
)

const simpleParameterName = "randomValue"
Expand Down Expand Up @@ -130,8 +132,8 @@ func TestConvertParameters(t *testing.T) {
envParameter, found := parameters[envParameterName]
assert.Equal(t, true, found)
assert.Equal(t, "SOME_ENV_VAR", envParameter.(*envParam.EnvironmentVariableParameter).Name)
//assert.Len(t, envParameter.(*compoundParam.CompoundParameter).GetReferences(), 1)
//assert.Equal(t, "__ENV_SOME_ENV_VAR__", envParameter.(*compoundParam.CompoundParameter).GetReferences()[0].Property)
// assert.Len(t, envParameter.(*compoundParam.CompoundParameter).GetReferences(), 1)
// assert.Equal(t, "__ENV_SOME_ENV_VAR__", envParameter.(*compoundParam.CompoundParameter).GetReferences()[0].Property)

compound := parameters["nickname"]

Expand Down Expand Up @@ -875,7 +877,7 @@ func TestConvertProjects(t *testing.T) {

assert.Equal(t, projectId, projectDefinition.Name)
assert.Equal(t, projectId, projectDefinition.Path)
assert.Nil(t, convertedProject.Dependencies, "Dependencies should not be resolved")
// assert.Empty(t, projectV2.ToDependenciesPerEnvironment(convertedProject), "Dependencies should not be resolved")

convertedConfigs := convertedProject.Configs

Expand Down Expand Up @@ -1161,7 +1163,7 @@ func Test_parseReference(t *testing.T) {
"returns error for unknown api reference",
"test-param",
"/some-project/alerting-profile/some-configV1.id",
api.APIs{}, //no APIs known
api.APIs{}, // no APIs known
nil,
true,
},
Expand Down
15 changes: 0 additions & 15 deletions pkg/deploy/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,11 +518,6 @@ func TestDeployConfigGraph_DoesNotDeployConfigsDependingOnSkippedConfigs(t *test
},
},
},
Dependencies: project.DependenciesPerEnvironment{
environmentName: []string{
referencedProjectId,
},
},
},
{
Id: referencedProjectId,
Expand Down Expand Up @@ -631,11 +626,6 @@ func TestDeployConfigGraph_DeploysIndependentConfigurations(t *testing.T) {
},
},
},
Dependencies: project.DependenciesPerEnvironment{
environmentName: []string{
referencedProjectId,
},
},
},
{
Id: referencedProjectId,
Expand Down Expand Up @@ -750,11 +740,6 @@ func TestDeployConfigGraph_DeploysIndependentConfigurations_IfContinuingAfterFai
},
},
},
Dependencies: project.DependenciesPerEnvironment{
environmentName: []string{
referencedProjectId,
},
},
},
{
Id: referencedProjectId,
Expand Down
25 changes: 6 additions & 19 deletions pkg/graph/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@
package graph_test

import (
"testing"

"github.com/stretchr/testify/assert"
graph2 "gonum.org/v1/gonum/graph"
"gonum.org/v1/gonum/graph/simple"

"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/graph"
project "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/project/v2"
"github.com/stretchr/testify/assert"
graph2 "gonum.org/v1/gonum/graph"
"gonum.org/v1/gonum/graph/simple"
"testing"
)

func TestConfigGraphPerEnvironment_GetConnectedConfigs(t *testing.T) {
Expand Down Expand Up @@ -90,11 +92,6 @@ func TestConfigGraphPerEnvironment_GetConnectedConfigs(t *testing.T) {
},
},
},
Dependencies: project.DependenciesPerEnvironment{
environmentName: []string{
referencedProjectId,
},
},
},
{
Id: referencedProjectId,
Expand Down Expand Up @@ -198,11 +195,6 @@ func TestGraphExport(t *testing.T) {
},
},
},
Dependencies: project.DependenciesPerEnvironment{
environmentName: []string{
referencedProjectId,
},
},
},
{
Id: referencedProjectId,
Expand Down Expand Up @@ -388,11 +380,6 @@ func TestGraphCycleErrors(t *testing.T) {
},
},
},
Dependencies: project.DependenciesPerEnvironment{
environmentName: []string{
referencedProjectId,
},
},
},
{
Id: referencedProjectId,
Expand Down
28 changes: 8 additions & 20 deletions pkg/project/v2/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package v2

import (
"fmt"
"maps"

"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config/coordinate"
)
Expand All @@ -32,10 +34,6 @@ type (
// ConfigsPerEnvironment is a map of EnvironmentName to configs. This is a flattened version of ConfigsPerTypePerEnvironments
ConfigsPerEnvironment map[EnvironmentName][]config.Config

ProjectID = string
// DependenciesPerEnvironment is a map of EnvironmentName to project IDs
DependenciesPerEnvironment map[EnvironmentName][]ProjectID

// ActionOverConfig is a function that will be performed over each config that is part of a project via a Project.ForEveryConfigDo method
ActionOverConfig func(c config.Config)
)
Expand All @@ -48,26 +46,16 @@ type Project struct {

// Configs are the configurations within this Project
Configs ConfigsPerTypePerEnvironments

// Dependencies of this project to other projects
Dependencies DependenciesPerEnvironment
}

// HasDependencyOn returns whether the project it is called on, has a dependency on the given project, for the given environment
func (p Project) HasDependencyOn(environment string, project Project) bool {
dependencies, found := p.Dependencies[environment]

if !found {
return false
}

for _, dep := range dependencies {
if dep == project.Id {
return true
func (p Project) ConfigList() []config.Config {
var configs []config.Config
for x := range maps.Values(p.Configs) {
for y := range maps.Values(x) {
configs = append(configs, y...)
}
}

return false
return configs
}

// GetConfigFor searches a config object for matching the given coordinate in the
Expand Down
55 changes: 24 additions & 31 deletions pkg/project/v2/project_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ func LoadProjects(fs afero.Fs, context ProjectLoaderContext, specificProjectName

loadedProjects = append(loadedProjects, project)

for _, environment := range environments {
projectNamesToLoad = append(projectNamesToLoad, project.Dependencies[environment.Name]...)
}
projectNamesToLoad = append(projectNamesToLoad, getConnectedProjects(project)...)
}

if len(errors) > 0 {
Expand All @@ -126,6 +124,26 @@ func LoadProjects(fs afero.Fs, context ProjectLoaderContext, specificProjectName
return loadedProjects, nil
}

func getConnectedProjects(p Project) []string {
var result []string

for _, c := range p.ConfigList() {
// ignore skipped configs
if c.Skip {
continue
}

for _, r := range c.References() {
// ignore project on same project
if p.Id == r.Project {
continue
}
result = append(result, r.Project)
}
}
return result
}

// Gets full project names to load specified by project or grouping project names. If none are specified, all project names are returned. Errors are returned for any project names that do not exist.
func getProjectNamesToLoad(allProjectsDefinitions manifest.ProjectDefinitionByProjectID, specificProjectNames []string) ([]string, []error) {
projectNamesToLoad := make([]string, 0, len(specificProjectNames))
Expand Down Expand Up @@ -185,10 +203,9 @@ func loadProject(fs afero.Fs, context ProjectLoaderContext, projectDefinition ma
insertNetworkZoneParameter(configs)

return Project{
Id: projectDefinition.Name,
GroupId: projectDefinition.Group,
Configs: toConfigMap(configs),
Dependencies: toDependenciesMap(projectDefinition.Name, configs),
Id: projectDefinition.Name,
GroupId: projectDefinition.Group,
Configs: toConfigMap(configs),
}, nil
}

Expand Down Expand Up @@ -327,27 +344,3 @@ func findDuplicatedConfigIdentifiers(configs []config.Config) []error {
func toFullyQualifiedConfigIdentifier(config config.Config) string {
return fmt.Sprintf("%s:%s:%s", config.Group, config.Environment, config.Coordinate)
}

func toDependenciesMap(projectId string, configs []config.Config) DependenciesPerEnvironment {
result := make(DependenciesPerEnvironment)

for _, c := range configs {
// ignore skipped configs
if c.Skip {
continue
}

for _, ref := range c.References() {
// ignore project on same project
if projectId == ref.Project {
continue
}

if !slices.Contains(result[c.Environment], ref.Project) {
result[c.Environment] = append(result[c.Environment], ref.Project)
}
}
}

return result
}
64 changes: 4 additions & 60 deletions pkg/project/v2/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
package v2_test

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config/coordinate"
project "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/project/v2"
"github.com/stretchr/testify/assert"
"testing"
)

func TestGetConfigFor(t *testing.T) {
Expand Down Expand Up @@ -86,64 +88,6 @@ func TestGetConfigFor(t *testing.T) {
})
}
}
func TestHasDependencyOn(t *testing.T) {
environment := "dev"
referencedProjectId := "projct2"

p := project.Project{
Id: "project1",
Dependencies: project.DependenciesPerEnvironment{
environment: []string{
referencedProjectId,
},
},
}

referencedProject := project.Project{
Id: referencedProjectId,
}

result := p.HasDependencyOn(environment, referencedProject)

assert.True(t, result, "should have dependency")
}

func TestHasDependencyOnShouldReturnFalseIfNoDependenciesForEnvironmentAreDefined(t *testing.T) {
environment := "dev"

p := project.Project{
Id: "project1",
}

p2 := project.Project{
Id: "project2",
}

result := p.HasDependencyOn(environment, p2)

assert.False(t, result, "should not have dependency")
}

func TestHasDependencyOnShouldReturnFalseIfNoDependencyDefined(t *testing.T) {
environment := "dev"

p := project.Project{
Id: "project1",
Dependencies: project.DependenciesPerEnvironment{
environment: []string{
"project3",
},
},
}

project2 := project.Project{
Id: "project2",
}

result := p.HasDependencyOn(environment, project2)

assert.False(t, result, "should not have dependency")
}

func TestProject_ForEveryConfigDo(t *testing.T) {
t.Run("simple case", func(t *testing.T) {
Expand Down
11 changes: 4 additions & 7 deletions pkg/project/v2/sort/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
package sort_test

import (
"testing"

"github.com/stretchr/testify/assert"

"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"
project "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/project/v2"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/project/v2/sort"
"github.com/stretchr/testify/assert"
"testing"
)

func TestGetSortedConfigsForEnvironments(t *testing.T) {
Expand Down Expand Up @@ -86,11 +88,6 @@ func TestGetSortedConfigsForEnvironments(t *testing.T) {
},
},
},
Dependencies: project.DependenciesPerEnvironment{
environmentName: []string{
referencedProjectId,
},
},
},
{
Id: referencedProjectId,
Expand Down
Loading

0 comments on commit 8dd6d57

Please sign in to comment.