Skip to content

feat(manifest): loader.Load(...) includes unselected environments as skipped with Skip: true #1876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/monaco/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ import (
func Delete(ctx context.Context, environments manifest.Environments, entriesToDelete delete.DeleteEntries) error {
var envsWithDeleteErrs []string
for _, env := range environments {
if env.Skip {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We now have a lot of these env.Skip checks (7), and I am thinking if it would be possible to not even pass them into the consuming functions, aka filter them beforehand.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering the same. Would it be possible to add to the Manifest struct something like a getter method where only active Environments are returned?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a fair point - let's do this!

I just tried with an iterator, but it doesn't really solve the problem: defining it on the Manifest would require changing many function signatures (including this one), definition it on manifest.Environments doesn't improve the problem, as you could still forget to use it. So I think I'll go for an actual getter on the Manifest that does the filtering when called.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't like how this looked, so have tried something different: #1897

continue
}
ctx := context.WithValue(ctx, log.CtxKeyEnv{}, log.CtxValEnv{Name: env.Name, Group: env.Group})
if containsPlatformTypes(entriesToDelete) && env.Auth.OAuth == nil {
log.WithCtxFields(ctx).Warn("Delete file contains Dynatrace Platform specific types, but no oAuth credentials are defined for environment %q - Dynatrace Platform configurations won't be deleted.", env.Name)
Expand Down
6 changes: 6 additions & 0 deletions cmd/monaco/dynatrace/dynatrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func VerifyEnvironmentGeneration(ctx context.Context, envs manifest.Environments
return true
}
for _, env := range envs {
if env.Skip {
continue
}
if !isValidEnvironment(ctx, env) {
return false
}
Expand Down Expand Up @@ -189,6 +192,9 @@ func (e EnvironmentClients) Names() []string {
func CreateEnvironmentClients(ctx context.Context, environments manifest.Environments, dryRun bool) (EnvironmentClients, error) {
clients := make(EnvironmentClients, len(environments))
for _, env := range environments {
if env.Skip {
continue
}
if dryRun {
clients[EnvironmentInfo{
Name: env.Name,
Expand Down
3 changes: 3 additions & 0 deletions cmd/monaco/integrationtest/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ func AssertAllConfigsAvailability(t *testing.T, fs afero.Fs, manifestPath string
envNames := make([]string, 0, len(loadedManifest.Environments))

for _, env := range loadedManifest.Environments {
if env.Skip {
continue
}
envNames = append(envNames, env.Name)
}

Expand Down
3 changes: 3 additions & 0 deletions cmd/monaco/integrationtest/v2/skip_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ func TestSkip(t *testing.T) {
clients := make(map[string]client.SettingsClient)

for name, def := range loadedManifest.Environments {
if def.Skip {
continue
}
set := integrationtest.CreateDynatraceClients(t, def)
clients[name] = set.SettingsClient
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/monaco/purge/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ func purge(ctx context.Context, fs afero.Fs, deploymentManifestPath string, envi
func purgeConfigs(ctx context.Context, environments []manifest.EnvironmentDefinition, apis api.APIs) error {

for _, env := range environments {
if env.Skip {
continue
}
err := purgeForEnvironment(ctx, env, apis)
if err != nil {
return err
Expand Down
3 changes: 3 additions & 0 deletions pkg/config/loader/config_entry_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func parseConfigEntry(
var results []config.Config
var errs []error
for _, env := range loaderContext.Environments {
if env.Skip {
continue
}

result, definitionErrors := parseDefinitionForEnvironment(fs, singleConfigContext, configId, env, definition, groupOverrideMap, environmentOverrideMap)

Expand Down
103 changes: 103 additions & 0 deletions pkg/config/loader/config_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/featureflags"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/pointer"
Expand Down Expand Up @@ -2108,6 +2109,108 @@ func Test_validateParameter(t *testing.T) {
}
}

// TestLoadConfigFile_ConfigsGeneratedForNonSkippedEnvironments tests that a config will be created for each non-skipped environment.
func TestLoadConfigFile_ConfigsGeneratedForNonSkippedEnvironments(t *testing.T) {
testLoaderContext := &LoaderContext{
ProjectId: "project",
Path: "some-dir/",
Environments: []manifest.EnvironmentDefinition{
{
Name: "environment1",
URL: manifest.URLDefinition{Type: manifest.ValueURLType, Value: "env url"},
Group: "group1",
Auth: manifest.Auth{
Token: &manifest.AuthSecret{Name: "token var"},
},
},
{
Name: "environment2",
URL: manifest.URLDefinition{Type: manifest.ValueURLType, Value: "env url"},
Group: "group1",
Auth: manifest.Auth{
Token: &manifest.AuthSecret{Name: "token var"},
},
},
},
ParametersSerDe: config.DefaultParameterParsers,
}

testFs := afero.NewMemMapFs()
require.NoError(t, afero.WriteFile(testFs, "config.yaml", []byte(`configs:
- id: profile-id
config:
name: 'Star Trek > Star Wars'
template: 'profile.json'
originObjectId: origin-object-id
type:
settings:
schema: 'builtin:profile.test'
schemaVersion: '1.0'
scope:
type: value
value: environment`), 0644))
require.NoError(t, afero.WriteFile(testFs, "profile.json", []byte("{}"), 0644))

configs, errs := LoadConfigFile(t.Context(), testFs, testLoaderContext, "config.yaml")

require.Empty(t, errs)
require.Len(t, configs, 2)
assert.Equal(t, "environment1", configs[0].Environment)
assert.Equal(t, "group1", configs[0].Group)
assert.Equal(t, "environment2", configs[1].Environment)
assert.Equal(t, "group1", configs[1].Group)
}

// TestLoadConfigFile_NoConfigsGeneratedForSkippedEnvironments tests that no config will be created for a skipped environment.
func TestLoadConfigFile_NoConfigsGeneratedForSkippedEnvironments(t *testing.T) {
testLoaderContext := &LoaderContext{
ProjectId: "project",
Path: "some-dir/",
Environments: []manifest.EnvironmentDefinition{
{
Name: "environment1",
URL: manifest.URLDefinition{Type: manifest.ValueURLType, Value: "env url"},
Group: "group1",
Auth: manifest.Auth{
Token: &manifest.AuthSecret{Name: "token var"},
},
},
{
Skip: true,
Name: "environment2",
URL: manifest.URLDefinition{Type: manifest.ValueURLType, Value: "env url"},
Group: "group1",
Auth: manifest.Auth{
Token: &manifest.AuthSecret{Name: "token var"},
},
},
},
ParametersSerDe: config.DefaultParameterParsers,
}

testFs := afero.NewMemMapFs()
require.NoError(t, afero.WriteFile(testFs, "config.yaml", []byte(`configs:
- id: profile-id
config:
name: 'Star Trek > Star Wars'
template: 'profile.json'
originObjectId: origin-object-id
type:
settings:
schema: 'builtin:profile.test'
schemaVersion: '1.0'
scope:
type: value
value: environment`), 0644))
require.NoError(t, afero.WriteFile(testFs, "profile.json", []byte("{}"), 0644))

configs, errs := LoadConfigFile(t.Context(), testFs, testLoaderContext, "config.yaml")
require.Empty(t, errs)
require.Len(t, configs, 1)
assert.Equal(t, "environment1", configs[0].Environment)
assert.Equal(t, "group1", configs[0].Group)
}

func makeCompoundParam(t *testing.T, refs []parameter.ParameterReference) *compound.CompoundParameter {
compoundParam, err := compound.New("param", "{}", refs)
assert.NoError(t, err)
Expand Down
3 changes: 2 additions & 1 deletion pkg/manifest/loader/manifest_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,10 @@ func parseEnvironments(context *Context, groups []persistence.Group) (map[string
}
envNames[env.Name] = true

// skip loading if environments is not empty, the environments does not contain the env name, or the group should not be included
// skip actually loading if environments is not empty, the environments does not contain the env name, or the group should not be included
if shouldSkipEnv(context, group, env) {
log.WithFields(field.F("manifestPath", context.ManifestPath)).Debug("skipping loading of environment %q", env.Name)
environments[env.Name] = manifest.EnvironmentDefinition{Skip: true, Name: env.Name, Group: group.Name}
continue
}

Expand Down
46 changes: 39 additions & 7 deletions pkg/manifest/loader/manifest_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@ package loader

import (
"fmt"
monacoVersion "github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/version"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/manifest"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/manifest/internal/persistence"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/version"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
"math"
"path/filepath"
"reflect"
"testing"

"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"

monacoVersion "github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/version"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/manifest"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/manifest/internal/persistence"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/version"
)

func Test_extractUrlType(t *testing.T) {
Expand Down Expand Up @@ -902,6 +904,11 @@ environmentGroups:
},
},
},
"envB": {
Skip: true,
Name: "envB",
Group: "groupB",
},
},
Accounts: map[string]manifest.Account{},
},
Expand Down Expand Up @@ -938,6 +945,11 @@ environmentGroups:
},
},
},
"envB": {
Skip: true,
Name: "envB",
Group: "groupB",
},
},
Accounts: map[string]manifest.Account{},
},
Expand Down Expand Up @@ -990,6 +1002,11 @@ environmentGroups:
},
},
},
"envC": {
Skip: true,
Name: "envC",
Group: "groupC",
},
},
Accounts: map[string]manifest.Account{},
},
Expand Down Expand Up @@ -1041,6 +1058,11 @@ environmentGroups:
},
},
},
"envC": {
Skip: true,
Name: "envC",
Group: "groupC",
},
},
Accounts: map[string]manifest.Account{},
},
Expand Down Expand Up @@ -1092,6 +1114,11 @@ environmentGroups:
},
},
},
"envC": {
Skip: true,
Name: "envC",
Group: "groupC",
},
},
Accounts: map[string]manifest.Account{},
},
Expand Down Expand Up @@ -1144,6 +1171,11 @@ environmentGroups:
},
},
},
"envC": {
Skip: true,
Name: "envC",
Group: "groupC",
},
},
Accounts: map[string]manifest.Account{},
},
Expand Down
1 change: 1 addition & 0 deletions pkg/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type Auth struct {

// EnvironmentDefinition holds all information about a Dynatrace environment
type EnvironmentDefinition struct {
Skip bool
Name string
Group string
URL URLDefinition
Expand Down
10 changes: 6 additions & 4 deletions pkg/manifest/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ package manifest_test

import (
"errors"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/manifest"
manifestloader "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/manifest/loader"
"path/filepath"
"testing"

"github.com/google/uuid"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"path/filepath"
"testing"

"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/manifest"
manifestloader "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/manifest/loader"
)

func TestDefaultTokenEndpoint(t *testing.T) {
Expand Down