Skip to content

Commit

Permalink
updating map[string]any to []map[string]any as value of provider config
Browse files Browse the repository at this point in the history
  • Loading branch information
lakshmimsft committed May 21, 2024
1 parent a8df1a2 commit 07a4aca
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 37 deletions.
12 changes: 5 additions & 7 deletions pkg/recipes/terraform/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,6 @@ func (cfg *TerraformConfig) updateModuleWithProviderAliases(requiredProviders ma
moduleAliasConfig := map[string]string{}

for providerName, providerConfigList := range cfg.Provider {
providerConfigDetails, ok := providerConfigList.([]map[string]any)
if !ok {
return fmt.Errorf("provider configuration for %s is not in the expected format", providerName)
}

// For each provider in the providerConfigs, if provider has a property "alias",
// add entry to the module provider configuration.
// Provider configurations (those with the alias argument set) are never inherited automatically by modules,
Expand All @@ -193,7 +188,7 @@ func (cfg *TerraformConfig) updateModuleWithProviderAliases(requiredProviders ma
// the required provider configuration (ConfigurationAliases) to the environment recipe provider configuration data.
// This is being done to ensure that the provider configuration is passed to the module correctly.

for _, providerConfig := range providerConfigDetails {
for _, providerConfig := range providerConfigList {
if alias, ok := providerConfig["alias"]; ok {
aliasProviderConfig := providerName + "." + fmt.Sprintf("%v", alias)

Expand Down Expand Up @@ -261,7 +256,10 @@ func newModuleConfig(moduleSource string, moduleVersion string, params ...Recipe

// getProviderConfigs generates the Terraform provider configurations. This is built from a combination of environment level recipe configuration for
// providers and the provider configurations registered with UCP. The environment level recipe configuration for providers takes precedence over UCP provider configurations.
func getProviderConfigs(ctx context.Context, requiredProviders map[string]*RequiredProviderInfo, ucpConfiguredProviders map[string]providers.Provider, envConfig *recipes.Configuration) (map[string]any, error) {
// The function returns a map where the keys are provider names and the values are slices of maps.
// Each map in the slice represents a specific configuration for the corresponding provider.
// This structure allows for multiple configurations per provider.
func getProviderConfigs(ctx context.Context, requiredProviders map[string]*RequiredProviderInfo, ucpConfiguredProviders map[string]providers.Provider, envConfig *recipes.Configuration) (map[string][]map[string]any, error) {
// Get recipe provider configurations from the environment configuration
providerConfigs := providers.GetRecipeProviderConfigs(ctx, envConfig)

Expand Down
27 changes: 7 additions & 20 deletions pkg/recipes/terraform/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ func Test_updateModuleWithProviderAliases(t *testing.T) {
{
name: "Test with valid provider config",
cfg: &TerraformConfig{
Provider: map[string]any{
Provider: map[string][]map[string]any{
"aws": []map[string]any{
{
"alias": "alias1",
Expand Down Expand Up @@ -742,7 +742,7 @@ func Test_updateModuleWithProviderAliases(t *testing.T) {
},
},
expectedConfig: &TerraformConfig{
Provider: map[string]any{
Provider: map[string][]map[string]any{
"aws": []map[string]any{
{
"alias": "alias1",
Expand Down Expand Up @@ -774,7 +774,7 @@ func Test_updateModuleWithProviderAliases(t *testing.T) {
{
name: "Test with subset of required_provider aliases in provider config",
cfg: &TerraformConfig{
Provider: map[string]any{
Provider: map[string][]map[string]any{
"aws": []map[string]any{
{
"alias": "alias1",
Expand All @@ -800,7 +800,7 @@ func Test_updateModuleWithProviderAliases(t *testing.T) {
},
},
expectedConfig: &TerraformConfig{
Provider: map[string]any{
Provider: map[string][]map[string]any{
"aws": []map[string]any{
{
"alias": "alias1",
Expand All @@ -827,7 +827,7 @@ func Test_updateModuleWithProviderAliases(t *testing.T) {
{
name: "Test with unmatched required_provider aliases in provider config",
cfg: &TerraformConfig{
Provider: map[string]any{
Provider: map[string][]map[string]any{
"aws": []map[string]any{
{
"region": "us-west-2",
Expand Down Expand Up @@ -869,7 +869,7 @@ func Test_updateModuleWithProviderAliases(t *testing.T) {
{
name: "Test with no required_provider aliases",
cfg: &TerraformConfig{
Provider: map[string]any{
Provider: map[string][]map[string]any{
"aws": []map[string]any{
{
"alias": "alias1",
Expand All @@ -894,7 +894,7 @@ func Test_updateModuleWithProviderAliases(t *testing.T) {
},
},
expectedConfig: &TerraformConfig{
Provider: map[string]any{
Provider: map[string][]map[string]any{
"aws": []map[string]any{
{
"alias": "alias1",
Expand All @@ -921,19 +921,6 @@ func Test_updateModuleWithProviderAliases(t *testing.T) {
cfg: nil,
wantErr: true,
},
{
name: "Provider Config in unexpected format",
requiredProviders: nil,
cfg: &TerraformConfig{
Provider: map[string]any{
"aws": []string{
"alias: alias1",
"region: us-west-2",
},
},
},
wantErr: true,
},
}

for _, tt := range tests {
Expand Down
4 changes: 2 additions & 2 deletions pkg/recipes/terraform/config/providers/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func GetUCPConfiguredTerraformProviders(ucpConn sdk.Connection, secretProvider *

// GetRecipeProviderConfigs returns the Terraform provider configurations for Terraform providers
// specified under the RecipeConfig/Terraform/Providers section under environment configuration.
func GetRecipeProviderConfigs(ctx context.Context, envConfig *recipes.Configuration) map[string]any {
providerConfigs := make(map[string]any)
func GetRecipeProviderConfigs(ctx context.Context, envConfig *recipes.Configuration) map[string][]map[string]any {
providerConfigs := make(map[string][]map[string]any)

// If the provider is not configured, or has empty configuration, skip this iteration
if envConfig != nil && envConfig.RecipeConfig.Terraform.Providers != nil {
Expand Down
12 changes: 6 additions & 6 deletions pkg/recipes/terraform/config/providers/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ func TestGetRecipeProviderConfigs(t *testing.T) {
testCases := []struct {
desc string
envConfig *recipes.Configuration
expected map[string]any
expected map[string][]map[string]any
}{
{
desc: "envConfig not set",
envConfig: nil,
expected: map[string]any{},
expected: map[string][]map[string]any{},
},
{
desc: "no providers configured",
envConfig: &recipes.Configuration{},
expected: map[string]any{},
expected: map[string][]map[string]any{},
},
{
desc: "empty provider config",
Expand All @@ -36,7 +36,7 @@ func TestGetRecipeProviderConfigs(t *testing.T) {
},
},
},
expected: map[string]any{},
expected: map[string][]map[string]any{},
},
{
desc: "Additional Properties set to nil in provider config",
Expand All @@ -53,7 +53,7 @@ func TestGetRecipeProviderConfigs(t *testing.T) {
},
},
},
expected: map[string]any{"aws": []map[string]any{}},
expected: map[string][]map[string]any{"aws": []map[string]any{}},
},
{
desc: "provider with config",
Expand All @@ -80,7 +80,7 @@ func TestGetRecipeProviderConfigs(t *testing.T) {
},
},
},
expected: map[string]any{
expected: map[string][]map[string]any{
"azurerm": []map[string]any{
{
"subscriptionid": 1234,
Expand Down
34 changes: 32 additions & 2 deletions pkg/recipes/terraform/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,40 @@ type TerraformConfig struct {
// Terraform represents number of settings related to Terraform's behavior.
Terraform *TerraformDefinition `json:"terraform"`

// Provider is the Terraform provider configuration.
// Provider represents the the configuration for Terraform providers.
// The key of the map is a string that represents the name of the provider.
// The value is a slice of maps, where each map represents a specific configuration for the provider.
// Each configuration map has string keys and values of any type.
// This structure allows for multiple configurations per provider.
//
// For example:
// {
// "aws": [
// {
// "region": "us-west-2",
// "version": "3.0"
// },
// {
// "alias": "east",
// "region": "us-east-1"
// }
// ],
// "azurerm": [
// {
// "tenant": "my-tenantId",
// "subscription_id": "my-subscriptionId"
// }
// ]
// }
//
// In this example, there are two providers: "aws" and "azurerm".
// The "aws" provider has two configurations: one for the "us-west-2" region and another for the "us-east-1" region with an alias "east".
// The "azurerm" provider has one configuration.
//
// For more information on Terraform provider configuration, refer to:
// https://developer.hashicorp.com/terraform/language/providers/configuration
// https://developer.hashicorp.com/terraform/language/syntax/json#provider-blocks
Provider map[string]any `json:"provider,omitempty"`
Provider map[string][]map[string]any `json:"provider,omitempty"`

// Module is the Terraform module configuration.
// https://developer.hashicorp.com/terraform/language/modules/syntax
Expand Down

0 comments on commit 07a4aca

Please sign in to comment.