Skip to content
Merged
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
77 changes: 77 additions & 0 deletions api/v1alpha2/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package v1alpha2

import (
"maps"

"github.com/windsorcli/cli/api/v1alpha2/config/providers"
"github.com/windsorcli/cli/api/v1alpha2/config/secrets"
"github.com/windsorcli/cli/api/v1alpha2/config/terraform"
"github.com/windsorcli/cli/api/v1alpha2/config/workstation"
)

// Config represents the entire configuration
type Config struct {
Version string `yaml:"version"`
Workstation *workstation.WorkstationConfig `yaml:"workstation,omitempty"`
Environment map[string]string `yaml:"environment,omitempty"`
Secrets *secrets.SecretsConfig `yaml:"secrets,omitempty"`
Providers *providers.ProvidersConfig `yaml:"providers,omitempty"`
Terraform *terraform.TerraformConfig `yaml:"terraform,omitempty"`
}

// Merge performs a deep merge of the current Config with another Config.
func (base *Config) Merge(overlay *Config) {
if overlay == nil {
return
}
if overlay.Environment != nil {
if base.Environment == nil {
base.Environment = make(map[string]string)
}
maps.Copy(base.Environment, overlay.Environment)
}
if overlay.Secrets != nil {
if base.Secrets == nil {
base.Secrets = &secrets.SecretsConfig{}
}
base.Secrets.Merge(overlay.Secrets)
}
if overlay.Providers != nil {
if base.Providers == nil {
base.Providers = &providers.ProvidersConfig{}
}
base.Providers.Merge(overlay.Providers)
}
if overlay.Terraform != nil {
if base.Terraform == nil {
base.Terraform = &terraform.TerraformConfig{}
}
base.Terraform.Merge(overlay.Terraform)
}
if overlay.Workstation != nil {
if base.Workstation == nil {
base.Workstation = &workstation.WorkstationConfig{}
}
base.Workstation.Merge(overlay.Workstation)
}
}

// DeepCopy creates a deep copy of the Config object
func (c *Config) DeepCopy() *Config {
if c == nil {
return nil
}
var environmentCopy map[string]string
if c.Environment != nil {
environmentCopy = make(map[string]string, len(c.Environment))
maps.Copy(environmentCopy, c.Environment)
}
return &Config{
Version: c.Version,
Workstation: c.Workstation.DeepCopy(),
Environment: environmentCopy,
Secrets: c.Secrets.DeepCopy(),
Providers: c.Providers.DeepCopy(),
Terraform: c.Terraform.DeepCopy(),
}
}
236 changes: 236 additions & 0 deletions api/v1alpha2/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
package v1alpha2

import (
"testing"

"github.com/windsorcli/cli/api/v1alpha2/config/providers"
"github.com/windsorcli/cli/api/v1alpha2/config/secrets"
"github.com/windsorcli/cli/api/v1alpha2/config/terraform"
"github.com/windsorcli/cli/api/v1alpha2/config/workstation"
)

// TestConfig_Merge tests the Merge functionality of the Config struct
func TestConfig_Merge(t *testing.T) {
t.Run("HandleNilOverlayGracefully", func(t *testing.T) {
base := &Config{
Version: "v1alpha2",
Environment: map[string]string{
"key1": "value1",
},
}
original := base.DeepCopy()

base.Merge(nil)

if base.Version != original.Version {
t.Errorf("expected version to remain unchanged, got %s", base.Version)
}
if base.Environment["key1"] != original.Environment["key1"] {
t.Errorf("expected environment to remain unchanged")
}
})

t.Run("MergeEnvironmentVariables", func(t *testing.T) {
base := &Config{
Version: "v1alpha2",
Environment: map[string]string{
"key1": "value1",
"key2": "value2",
},
}
overlay := &Config{
Environment: map[string]string{
"key2": "newvalue2",
"key3": "value3",
},
}

base.Merge(overlay)

if base.Environment["key1"] != "value1" {
t.Errorf("expected key1 to remain unchanged, got %s", base.Environment["key1"])
}
if base.Environment["key2"] != "newvalue2" {
t.Errorf("expected key2 to be updated, got %s", base.Environment["key2"])
}
if base.Environment["key3"] != "value3" {
t.Errorf("expected key3 to be added, got %s", base.Environment["key3"])
}
})

t.Run("InitializeEnvironmentMapWhenNil", func(t *testing.T) {
base := &Config{
Version: "v1alpha2",
}
overlay := &Config{
Environment: map[string]string{
"key1": "value1",
},
}

base.Merge(overlay)

if base.Environment == nil {
t.Error("expected environment map to be initialized")
}
if base.Environment["key1"] != "value1" {
t.Errorf("expected key1 to be set, got %s", base.Environment["key1"])
}
})

t.Run("MergeSecretsConfiguration", func(t *testing.T) {
base := &Config{
Version: "v1alpha2",
}
overlay := &Config{
Secrets: &secrets.SecretsConfig{},
}

base.Merge(overlay)

if base.Secrets == nil {
t.Error("expected secrets to be initialized")
}
})

t.Run("MergeProvidersConfiguration", func(t *testing.T) {
base := &Config{
Version: "v1alpha2",
}
overlay := &Config{
Providers: &providers.ProvidersConfig{},
}

base.Merge(overlay)

if base.Providers == nil {
t.Error("expected Providers config to be initialized")
}
})

t.Run("MergeTerraformConfiguration", func(t *testing.T) {
base := &Config{
Version: "v1alpha2",
}
overlay := &Config{
Terraform: &terraform.TerraformConfig{},
}

base.Merge(overlay)

if base.Terraform == nil {
t.Error("expected Terraform config to be initialized")
}
})

t.Run("MergeWorkstationConfiguration", func(t *testing.T) {
base := &Config{
Version: "v1alpha2",
}
overlay := &Config{
Workstation: &workstation.WorkstationConfig{},
}

base.Merge(overlay)

if base.Workstation == nil {
t.Error("expected Workstation config to be initialized")
}
})
}

// TestConfig_Copy tests the Copy functionality of the Config struct
func TestConfig_Copy(t *testing.T) {
t.Run("ReturnNilForNilConfig", func(t *testing.T) {
var config *Config
result := config.DeepCopy()

if result != nil {
t.Error("expected nil result for nil config")
}
})

t.Run("CreateDeepCopyOfConfig", func(t *testing.T) {
original := &Config{
Version: "v1alpha2",
Environment: map[string]string{
"key1": "value1",
"key2": "value2",
},
}

copy := original.DeepCopy()

if copy == nil {
t.Fatal("expected non-nil copy")
}
if copy == original {
t.Error("expected different pointer")
}
if copy.Version != original.Version {
t.Errorf("expected version to be copied, got %s", copy.Version)
}
if copy.Environment == nil {
t.Error("expected environment to be copied")
}
if copy.Environment["key1"] != original.Environment["key1"] {
t.Errorf("expected environment values to be copied")
}
})

t.Run("HandleNilEnvironmentInCopy", func(t *testing.T) {
original := &Config{
Version: "v1alpha2",
}

copy := original.DeepCopy()

if copy.Environment != nil {
t.Error("expected nil environment in copy")
}
})

t.Run("CopyAllConfigSections", func(t *testing.T) {
original := &Config{
Version: "v1alpha2",
Environment: map[string]string{
"key1": "value1",
},
Secrets: &secrets.SecretsConfig{},
Providers: &providers.ProvidersConfig{},
Terraform: &terraform.TerraformConfig{},
Workstation: &workstation.WorkstationConfig{},
}

copy := original.DeepCopy()

if copy.Secrets == nil {
t.Error("expected secrets to be copied")
}
if copy.Providers == nil {
t.Error("expected Providers config to be copied")
}
if copy.Terraform == nil {
t.Error("expected Terraform config to be copied")
}
if copy.Workstation == nil {
t.Error("expected Workstation config to be copied")
}
})

t.Run("CreateIndependentEnvironmentMap", func(t *testing.T) {
original := &Config{
Version: "v1alpha2",
Environment: map[string]string{
"key1": "value1",
},
}

copy := original.DeepCopy()
copy.Environment["key1"] = "modified"

if original.Environment["key1"] == "modified" {
t.Error("expected original environment to remain unchanged")
}
})
}
69 changes: 69 additions & 0 deletions api/v1alpha2/config/providers/aws/aws.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package aws

// AWSConfig represents the AWS configuration
type AWSConfig struct {
// Enabled indicates whether AWS integration is enabled.
Enabled *bool `yaml:"enabled,omitempty"`

// AWSEndpointURL specifies the custom endpoint URL for AWS services.
AWSEndpointURL *string `yaml:"aws_endpoint_url,omitempty"`

// AWSProfile defines the AWS CLI profile to use for authentication.
AWSProfile *string `yaml:"aws_profile,omitempty"`

// S3Hostname sets the custom hostname for the S3 service.
S3Hostname *string `yaml:"s3_hostname,omitempty"`

// MWAAEndpoint specifies the endpoint for Managed Workflows for Apache Airflow.
MWAAEndpoint *string `yaml:"mwaa_endpoint,omitempty"`
}

// Merge performs a deep merge of the current AWSConfig with another AWSConfig.
func (base *AWSConfig) Merge(overlay *AWSConfig) {
if overlay.Enabled != nil {
base.Enabled = overlay.Enabled
}
if overlay.AWSEndpointURL != nil {
base.AWSEndpointURL = overlay.AWSEndpointURL
}
if overlay.AWSProfile != nil {
base.AWSProfile = overlay.AWSProfile
}
if overlay.S3Hostname != nil {
base.S3Hostname = overlay.S3Hostname
}
if overlay.MWAAEndpoint != nil {
base.MWAAEndpoint = overlay.MWAAEndpoint
}
}

// DeepCopy creates a deep copy of the AWSConfig object
func (c *AWSConfig) DeepCopy() *AWSConfig {
if c == nil {
return nil
}
copied := &AWSConfig{}

if c.Enabled != nil {
enabledCopy := *c.Enabled
copied.Enabled = &enabledCopy
}
if c.AWSEndpointURL != nil {
urlCopy := *c.AWSEndpointURL
copied.AWSEndpointURL = &urlCopy
}
if c.AWSProfile != nil {
profileCopy := *c.AWSProfile
copied.AWSProfile = &profileCopy
}
if c.S3Hostname != nil {
hostnameCopy := *c.S3Hostname
copied.S3Hostname = &hostnameCopy
}
if c.MWAAEndpoint != nil {
endpointCopy := *c.MWAAEndpoint
copied.MWAAEndpoint = &endpointCopy
}

return copied
}
Loading
Loading