Skip to content
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

refactor: update the struct of TerraformConfig #670

Merged
merged 1 commit into from
Dec 12, 2023
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
17 changes: 16 additions & 1 deletion pkg/apis/workspace/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,22 @@ type KubernetesConfig struct {

// TerraformConfig contains the config of multiple terraform provider config, whose key is
// the provider name.
type TerraformConfig map[string]GenericConfig
type TerraformConfig map[string]*ProviderConfig

// ProviderConfig contains the full configurations of a specified provider. It is the combination
// of the specified provider's config in blocks "terraform/required_providers" and "providers" in
// terraform hcl file, where the former is described by fields Source and Version, and the latter
// is described by GenericConfig cause different provider has different config.
type ProviderConfig struct {
// Source of the provider.
Source string `yaml:"source" json:"source"`

// Version of the provider.
Version string `yaml:"version" json:"version"`

// GenericConfig is used to describe the config of a specified terraform provider.
GenericConfig `yaml:",inline,omitempty" json:",inline,omitempty"`
}

// BackendConfigs contains config of the backend, which is used to store state, etc. Only one kind
// backend can be configured.
Expand Down
2 changes: 1 addition & 1 deletion pkg/workspace/testdata/workspaces/for_delete_ws.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ runtimes:
kubeConfig: /etc/kubeconfig.yaml
terraform:
aws:
region: us-east-1
source: hashicorp/aws
version: 1.0.4
region: us-east-1
backends:
local: {}
2 changes: 1 addition & 1 deletion pkg/workspace/testdata/workspaces/for_update_ws.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ runtimes:
kubeConfig: /etc/kubeconfig.yaml
terraform:
aws:
region: us-east-1
source: hashicorp/aws
version: 1.0.4
region: us-east-1
backends:
local: {}
6 changes: 3 additions & 3 deletions pkg/workspace/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ func GetTerraformConfig(configs *workspace.RuntimeConfigs) (workspace.TerraformC
return configs.Terraform, nil
}

// GetTerraformProviderConfig returns the specified terraform provider config from runtime config, should
// be called after ValidateRuntimeConfigs.
// GetProviderConfig returns the specified terraform provider config from runtime config, should be called
// after ValidateRuntimeConfigs.
// If got empty terraform config, ErrEmptyTerraformProviderConfig will get returned.
func GetTerraformProviderConfig(configs *workspace.RuntimeConfigs, providerName string) (workspace.GenericConfig, error) {
func GetProviderConfig(configs *workspace.RuntimeConfigs, providerName string) (*workspace.ProviderConfig, error) {
if providerName == "" {
return nil, ErrEmptyTerraformProviderName
}
Expand Down
14 changes: 8 additions & 6 deletions pkg/workspace/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,19 @@ func Test_GetTerraformProviderConfig(t *testing.T) {
name string
success bool
providerName string
providerConfig workspace.GenericConfig
providerConfig *workspace.ProviderConfig
runtimeConfigs *workspace.RuntimeConfigs
}{
{
name: "successfully get terraform provider config",
success: true,
providerName: "aws",
providerConfig: workspace.GenericConfig{
"version": "1.0.4",
"source": "hashicorp/aws",
"region": "us-east-1",
providerConfig: &workspace.ProviderConfig{
Source: "hashicorp/aws",
Version: "1.0.4",
GenericConfig: workspace.GenericConfig{
"region": "us-east-1",
},
},
runtimeConfigs: mockValidRuntimeConfigs(),
},
Expand All @@ -145,7 +147,7 @@ func Test_GetTerraformProviderConfig(t *testing.T) {

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
cfg, err := GetTerraformProviderConfig(tc.runtimeConfigs, tc.providerName)
cfg, err := GetProviderConfig(tc.runtimeConfigs, tc.providerName)
assert.Equal(t, tc.success, err == nil)
assert.Equal(t, tc.providerConfig, cfg)
})
Expand Down
36 changes: 31 additions & 5 deletions pkg/workspace/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ var (
ErrRepeatedModuleConfigSelectedProjects = errors.New("project should not repeat in one patcher block's projectSelector")
ErrMultipleModuleConfigSelectedProjects = errors.New("a project cannot assign in more than one patcher block's projectSelector")

ErrEmptyKubeConfig = errors.New("empty kubeconfig")
ErrEmptyTerraformProviderName = errors.New("empty terraform provider name")
ErrEmptyTerraformProviderConfig = errors.New("empty terraform provider config")
ErrEmptyKubeConfig = errors.New("empty kubeconfig")
ErrEmptyTerraformProviderName = errors.New("empty terraform provider name")
ErrEmptyTerraformProviderConfig = errors.New("empty terraform provider config")
ErrEmptyTerraformProviderSource = errors.New("empty provider source")
ErrEmptyTerraformProviderVersion = errors.New("empty provider version")
ErrEmptyTerraformProviderConfigKey = errors.New("empty provider config key")
ErrEmptyTerraformProviderConfigValue = errors.New("empty provider config value")

ErrMultipleBackends = errors.New("more than one backend configured")
ErrEmptyMysqlDBName = errors.New("empty db name")
Expand Down Expand Up @@ -159,8 +163,30 @@ func ValidateTerraformConfig(config workspace.TerraformConfig) error {
if name == "" {
return ErrEmptyTerraformProviderName
}
if len(cfg) == 0 {
return ErrEmptyTerraformProviderConfig
if cfg == nil {
return fmt.Errorf("%w of provider %s", ErrEmptyTerraformProviderConfig, name)
}
if err := ValidateProviderConfig(cfg); err != nil {
return fmt.Errorf("invalid terraform provider %s: %w", name, err)
}
}
return nil
}

// ValidateProviderConfig is used to validate the providerConfig is valid or not.
func ValidateProviderConfig(config *workspace.ProviderConfig) error {
if config.Source == "" {
return ErrEmptyTerraformProviderSource
}
if config.Version == "" {
return ErrEmptyTerraformProviderVersion
}
for k, v := range config.GenericConfig {
if k == "" {
return ErrEmptyTerraformProviderConfigKey
}
if v == nil {
return fmt.Errorf("%w of field %s", ErrEmptyTerraformProviderConfigValue, k)
}
}
return nil
Expand Down
70 changes: 63 additions & 7 deletions pkg/workspace/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,11 @@ func mockValidKubernetesConfig() *workspace.KubernetesConfig {
func mockValidTerraformConfig() workspace.TerraformConfig {
return workspace.TerraformConfig{
"aws": {
"version": "1.0.4",
"source": "hashicorp/aws",
"region": "us-east-1",
Source: "hashicorp/aws",
Version: "1.0.4",
GenericConfig: workspace.GenericConfig{
"region": "us-east-1",
},
},
}
}
Expand Down Expand Up @@ -346,17 +348,71 @@ func TestValidateTerraformConfig(t *testing.T) {
success: false,
terraformConfig: workspace.TerraformConfig{
"": {
"version": "1.0.4",
"source": "hashicorp/aws",
"region": "us-east-1",
Source: "hashicorp/aws",
Version: "1.0.4",
GenericConfig: workspace.GenericConfig{
"region": "us-east-1",
},
},
},
},
{
name: "invalid terraform config empty provider config",
success: false,
terraformConfig: workspace.TerraformConfig{
"aws": {},
"aws": nil,
},
},
{
name: "invalid terraform config empty provider source",
success: false,
terraformConfig: workspace.TerraformConfig{
"aws": {
Source: "",
Version: "1.0.4",
GenericConfig: workspace.GenericConfig{
"region": "us-east-1",
},
},
},
},
{
name: "invalid terraform config empty provider version",
success: false,
terraformConfig: workspace.TerraformConfig{
"aws": {
Source: "hashicorp/aws",
Version: "",
GenericConfig: workspace.GenericConfig{
"region": "us-east-1",
},
},
},
},
{
name: "invalid terraform config empty provider config key",
success: false,
terraformConfig: workspace.TerraformConfig{
"aws": {
Source: "hashicorp/aws",
Version: "1.0.4",
GenericConfig: workspace.GenericConfig{
"": "us-east-1",
},
},
},
},
{
name: "invalid terraform config empty provider config value",
success: false,
terraformConfig: workspace.TerraformConfig{
"aws": {
Source: "hashicorp/aws",
Version: "1.0.4",
GenericConfig: workspace.GenericConfig{
"region": nil,
},
},
},
},
}
Expand Down
Loading