Skip to content

Commit

Permalink
Fixing generation of provider_installation block used for provider …
Browse files Browse the repository at this point in the history
…caching (#3290)
  • Loading branch information
levkohimins committed Jul 24, 2024
1 parent 0bf2be7 commit 98b6032
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 23 deletions.
9 changes: 4 additions & 5 deletions cli/provider_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,17 @@ func InitProviderCacheServer(opts *options.TerragruntOptions) (*ProviderCache, e
for _, registryName := range opts.ProviderCacheRegistryNames {
excludeAddrs = append(excludeAddrs, fmt.Sprintf("%s/*/*", registryName))
}

for _, method := range cliCfg.ProviderInstallation.Methods {
switch method := method.(type) {
case *cliconfig.ProviderInstallationFilesystemMirror:
providerHandlers = append(providerHandlers, handlers.NewProviderFilesystemMirrorHandler(providerService, cacheProviderHTTPStatusCode, method))
method.AppendExclude(excludeAddrs)
case *cliconfig.ProviderInstallationNetworkMirror:
providerHandlers = append(providerHandlers, handlers.NewProviderNetworkMirrorHandler(providerService, cacheProviderHTTPStatusCode, method))
method.AppendExclude(excludeAddrs)
case *cliconfig.ProviderInstallationDirect:
providerHandlers = append(providerHandlers, handlers.NewProviderDirectHandler(providerService, cacheProviderHTTPStatusCode, method))
method.RemoveExclude(excludeAddrs)
continue
}
method.AppendExclude(excludeAddrs)
}
providerHandlers = append(providerHandlers, handlers.NewProviderDirectHandler(providerService, cacheProviderHTTPStatusCode, new(cliconfig.ProviderInstallationDirect)))

Expand Down Expand Up @@ -181,7 +179,6 @@ func (cache *ProviderCache) TerraformCommandHook(ctx context.Context, opts *opti
if err != nil {
return nil, err
}

if err := getproviders.UpdateLockfile(ctx, opts.WorkingDir, caches); err != nil {
return nil, err
}
Expand Down Expand Up @@ -250,6 +247,8 @@ func (cache *ProviderCache) createLocalCLIConfig(opts *options.TerragruntOptions
cfg.AddProviderInstallationMethods(
cliconfig.NewProviderInstallationFilesystemMirror(opts.ProviderCacheDir, providerInstallationIncludes, nil),
)
} else {
cfg.ProviderInstallation = nil
}

cfg.AddProviderInstallationMethods(
Expand Down
3 changes: 2 additions & 1 deletion terraform/cliconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (cfg *Config) AddHost(name string, services map[string]string) {
})
}

// AddProviderInstallationMethods adds installation methods, https://developer.hashicorp.com/terraform/cli/config/config-file#provider-installation
// AddProviderInstallationMethods merges new installation methods with the current ones, https://developer.hashicorp.com/terraform/cli/config/config-file#provider-installation
//
// provider_installation {
// filesystem_mirror {
Expand All @@ -91,6 +91,7 @@ func (cfg *Config) AddHost(name string, services map[string]string) {
// exclude = ["example.com/*/*"]
// }
// }

func (cfg *Config) AddProviderInstallationMethods(newMethods ...ProviderInstallationMethod) {
if cfg.ProviderInstallation == nil {
cfg.ProviderInstallation = &ProviderInstallation{}
Expand Down
34 changes: 34 additions & 0 deletions terraform/cliconfig/provider_installation.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type ProviderInstallationMethod interface {
fmt.Stringer
AppendInclude(addrs []string)
AppendExclude(addrs []string)
RemoveInclude(addrs []string)
RemoveExclude(addrs []string)
Merge(with ProviderInstallationMethod) bool
}
Expand Down Expand Up @@ -123,6 +124,17 @@ func (method *ProviderInstallationDirect) RemoveExclude(addrs []string) {
}
}

func (method *ProviderInstallationDirect) RemoveInclude(addrs []string) {
if len(addrs) == 0 || method.Include == nil {
return
}
*method.Include = util.RemoveSublistFromList(*method.Include, addrs)

if len(*method.Include) == 0 {
method.Include = nil
}
}

func (method *ProviderInstallationDirect) String() string {
b, _ := json.Marshal(method) //nolint:errcheck
return string(b)
Expand Down Expand Up @@ -197,6 +209,17 @@ func (method *ProviderInstallationFilesystemMirror) RemoveExclude(addrs []string
}
}

func (method *ProviderInstallationFilesystemMirror) RemoveInclude(addrs []string) {
if len(addrs) == 0 || method.Include == nil {
return
}
*method.Include = util.RemoveSublistFromList(*method.Include, addrs)

if len(*method.Include) == 0 {
method.Include = nil
}
}

func (method *ProviderInstallationFilesystemMirror) String() string {
b, _ := json.Marshal(method) //nolint:errcheck
return string(b)
Expand Down Expand Up @@ -271,6 +294,17 @@ func (method *ProviderInstallationNetworkMirror) RemoveExclude(addrs []string) {
}
}

func (method *ProviderInstallationNetworkMirror) RemoveInclude(addrs []string) {
if len(addrs) == 0 || method.Include == nil {
return
}
*method.Include = util.RemoveSublistFromList(*method.Include, addrs)

if len(*method.Include) == 0 {
method.Include = nil
}
}

func (method *ProviderInstallationNetworkMirror) String() string {
b, _ := json.Marshal(method) //nolint:errcheck
return string(b)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,3 @@ terraform {
}
}
}

provider "null" {
# Configuration options
}
12 changes: 12 additions & 0 deletions test/fixture-provider-cache/network-mirror/app/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
terraform {
required_providers {
aws = {
source = "example.com/hashicorp/aws"
version = "5.59.0"
}
azurerm = {
source = "example.com/hashicorp/azurerm"
version = "3.113.0"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Intentionally empty
23 changes: 11 additions & 12 deletions test/integration_serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ import (
func TestTerragruntProviderCacheWithFilesystemMirror(t *testing.T) {
// In this test we use os.Setenv to set the Terraform env var TF_CLI_CONFIG_FILE.

cleanupTerraformFolder(t, TEST_FIXTURE_PROVIDER_CACHE_MIRROR)
tmpEnvPath := copyEnvironment(t, TEST_FIXTURE_PROVIDER_CACHE_MIRROR)
rootPath := util.JoinPath(tmpEnvPath, TEST_FIXTURE_PROVIDER_CACHE_MIRROR)
cleanupTerraformFolder(t, TEST_FIXTURE_PROVIDER_CACHE_FILESYSTEM_MIRROR)
tmpEnvPath := copyEnvironment(t, TEST_FIXTURE_PROVIDER_CACHE_FILESYSTEM_MIRROR)
rootPath := util.JoinPath(tmpEnvPath, TEST_FIXTURE_PROVIDER_CACHE_FILESYSTEM_MIRROR)

appPath := filepath.Join(rootPath, "app")
providersMirrorPath := filepath.Join(rootPath, "providers-mirror")
Expand Down Expand Up @@ -86,9 +86,9 @@ func TestTerragruntProviderCacheWithFilesystemMirror(t *testing.T) {
}
createCLIConfig(t, cliConfigFilename, cliConfigSettings)

runTerragrunt(t, fmt.Sprintf("terragrunt run-all init --terragrunt-provider-cache --terragrunt-provider-cache-registry-names example.com --terragrunt-provider-cache-registry-names registry.terraform.io --terragrunt-provider-cache-dir %s --terragrunt-log-level trace --terragrunt-non-interactive --terragrunt-working-dir %s", providerCacheDir, appPath))
runTerragrunt(t, fmt.Sprintf("terragrunt run-all init --terragrunt-provider-cache --terragrunt-provider-cache-registry-names example.com --terragrunt-provider-cache-registry-names registry.opentofu.org --terragrunt-provider-cache-registry-names registry.terraform.io --terragrunt-provider-cache-dir %s --terragrunt-log-level trace --terragrunt-non-interactive --terragrunt-working-dir %s", providerCacheDir, appPath))

expectedProviderInstallation := `provider_installation { "filesystem_mirror" { path = "%s" include = ["example.com/*/*"] exclude = ["example.com/*/*", "registry.terraform.io/*/*"] } "filesystem_mirror" { path = "%s" include = ["example.com/*/*", "registry.terraform.io/*/*"] } "direct" { } }`
expectedProviderInstallation := `provider_installation { "filesystem_mirror" { path = "%s" include = ["example.com/*/*"] exclude = ["example.com/*/*", "registry.opentofu.org/*/*", "registry.terraform.io/*/*"] } "filesystem_mirror" { path = "%s" include = ["example.com/*/*", "registry.opentofu.org/*/*", "registry.terraform.io/*/*"] } "direct" { } }`
expectedProviderInstallation = fmt.Sprintf(strings.Join(strings.Fields(expectedProviderInstallation), " "), providersMirrorPath, providerCacheDir)

terraformrcBytes, err := os.ReadFile(filepath.Join(appPath, ".terraformrc"))
Expand All @@ -101,9 +101,9 @@ func TestTerragruntProviderCacheWithFilesystemMirror(t *testing.T) {
func TestTerragruntProviderCacheWithNetworkMirror(t *testing.T) {
// In this test we use os.Setenv to set the Terraform env var TF_CLI_CONFIG_FILE.

cleanupTerraformFolder(t, TEST_FIXTURE_PROVIDER_CACHE_MIRROR)
tmpEnvPath := copyEnvironment(t, TEST_FIXTURE_PROVIDER_CACHE_MIRROR)
rootPath := util.JoinPath(tmpEnvPath, TEST_FIXTURE_PROVIDER_CACHE_MIRROR)
cleanupTerraformFolder(t, TEST_FIXTURE_PROVIDER_CACHE_NETWORK_MIRROR)
tmpEnvPath := copyEnvironment(t, TEST_FIXTURE_PROVIDER_CACHE_NETWORK_MIRROR)
rootPath := util.JoinPath(tmpEnvPath, TEST_FIXTURE_PROVIDER_CACHE_NETWORK_MIRROR)

appPath := filepath.Join(rootPath, "app")
providersNetkworMirrorPath := filepath.Join(rootPath, "providers-network-mirror")
Expand Down Expand Up @@ -170,16 +170,15 @@ func TestTerragruntProviderCacheWithNetworkMirror(t *testing.T) {
NetworkMirrorMethods: []CLIConfigProviderInstallationNetworkMirror{
{
URL: networkMirrorURL.String(),
Include: []string{"example.com/hashicorp/aws"},
Exclude: []string{"example.com/hashicorp/azurerm"},
},
},
}
createCLIConfig(t, cliConfigFilename, cliConfigSettings)

runTerragrunt(t, fmt.Sprintf("terragrunt run-all init --terragrunt-provider-cache --terragrunt-provider-cache-registry-names example.com --terragrunt-provider-cache-registry-names registry.terraform.io --terragrunt-provider-cache-dir %s --terragrunt-log-level trace --terragrunt-non-interactive --terragrunt-working-dir %s", providerCacheDir, appPath))

expectedProviderInstallation := `provider_installation { "filesystem_mirror" { path = "%s" include = ["example.com/hashicorp/azurerm"] exclude = ["example.com/*/*", "registry.terraform.io/*/*"] } "network_mirror" { url = "%s" include = ["example.com/hashicorp/aws"] exclude = ["example.com/*/*", "registry.terraform.io/*/*"] } "filesystem_mirror" { path = "%s" include = ["example.com/*/*", "registry.terraform.io/*/*"] } "direct" { } }`
runTerragrunt(t, fmt.Sprintf("terragrunt run-all init --terragrunt-provider-cache --terragrunt-provider-cache-registry-names example.com --terragrunt-provider-cache-registry-names registry.opentofu.org --terragrunt-provider-cache-registry-names registry.terraform.io --terragrunt-provider-cache-dir %s --terragrunt-log-level trace --terragrunt-non-interactive --terragrunt-working-dir %s", providerCacheDir, appPath))

expectedProviderInstallation := `provider_installation { "filesystem_mirror" { path = "%s" include = ["example.com/hashicorp/azurerm"] exclude = ["example.com/*/*", "registry.opentofu.org/*/*", "registry.terraform.io/*/*"] } "network_mirror" { url = "%s" exclude = ["example.com/hashicorp/azurerm", "example.com/*/*", "registry.opentofu.org/*/*", "registry.terraform.io/*/*"] } "filesystem_mirror" { path = "%s" include = ["example.com/*/*", "registry.opentofu.org/*/*", "registry.terraform.io/*/*"] } "direct" { exclude = ["example.com/*/*", "registry.opentofu.org/*/*", "registry.terraform.io/*/*"] } }`
expectedProviderInstallation = fmt.Sprintf(strings.Join(strings.Fields(expectedProviderInstallation), " "), providersFilesystemMirrorPath, networkMirrorURL.String(), providerCacheDir)

terraformrcBytes, err := os.ReadFile(filepath.Join(appPath, ".terraformrc"))
Expand Down
3 changes: 2 additions & 1 deletion test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ const (
TEST_FIXTURE_INIT_ONCE = "fixture-init-once"
TEST_FIXTURE_PROVIDER_CACHE_MULTIPLE_PLATFORMS = "fixture-provider-cache/multiple-platforms"
TEST_FIXTURE_PROVIDER_CACHE_DIRECT = "fixture-provider-cache/direct"
TEST_FIXTURE_PROVIDER_CACHE_MIRROR = "fixture-provider-cache/mirror"
TEST_FIXTURE_PROVIDER_CACHE_NETWORK_MIRROR = "fixture-provider-cache/network-mirror"
TEST_FIXTURE_PROVIDER_CACHE_FILESYSTEM_MIRROR = "fixture-provider-cache/filesystem-mirror"
TEST_FIXTURE_DESTROY_ORDER = "fixture-destroy-order"
TEST_FIXTURE_CODEGEN_PATH = "fixture-codegen"
TEST_FIXTURE_GCS_PATH = "fixture-gcs/"
Expand Down

0 comments on commit 98b6032

Please sign in to comment.