Skip to content

Commit

Permalink
Update remote-state module (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
aknysh authored Jun 2, 2023
1 parent f302b85 commit de7da5b
Show file tree
Hide file tree
Showing 9 changed files with 472 additions and 90 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release-branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:
- 'docs/**'
- 'examples/**'
- 'test/**'
- 'README.*'

permissions:
contents: write
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-published.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ permissions:

jobs:
terraform-module:
uses: cloudposse/github-actions-workflows-terraform-module/.github/workflows/release.yml@main
uses: cloudposse/github-actions-workflows-terraform-module/.github/workflows/release-published.yml@main
21 changes: 21 additions & 0 deletions examples/remote-state/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,24 @@ module "remote_state_using_context_ignore_errors" {

context = module.this.context
}

module "remote_state_with_bypass" {
source = "../../modules/remote-state"

bypass = true

defaults = {
default_output = "default-value"
}

component = "test/test-component-override"
namespace = ""
tenant = "tenant1"
environment = "ue2"
stage = "dev"

atmos_cli_config_path = var.atmos_cli_config_path
atmos_base_path = var.atmos_base_path

context = module.this.context
}
5 changes: 5 additions & 0 deletions examples/remote-state/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ output "remote_state_using_context_ignore_errors" {
value = module.remote_state_using_context_ignore_errors.outputs
description = "Component remote state using wrong component. Errors are ignored in the 'utils' provider"
}

output "remote_state_with_bypass" {
value = module.remote_state_with_bypass.outputs
description = "Component remote state with 'bypass' set to 'true'"
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,11 @@ locals {

} # ds_configurations


}

# Due to issues like
# - https://github.com/hashicorp/terraform/issues/32023
# - https://github.com/hashicorp/terraform/issues/27849
# we want to avoid using `count` to enable or disable the data source,
# so instead we use a dummy remote state (a local file) when otherwise
# we would disable the data source via `count = 0`.
data "terraform_remote_state" "data_source" {
count = var.bypass ? 0 : 1

backend = local.ds_backend
workspace = local.ds_workspace
config = local.ds_configurations[local.ds_backend]
Expand Down
8 changes: 4 additions & 4 deletions modules/remote-state/main.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
data "utils_component_config" "config" {
count = var.bypass ? 0 : 1

component = var.component
stack = var.stack
namespace = module.always.namespace
Expand All @@ -12,7 +14,7 @@ data "utils_component_config" "config" {
}

locals {
config = yamldecode(data.utils_component_config.config.output)
config = try(yamldecode(data.utils_component_config.config[0].output), {})

remote_state_backend_type = try(local.config.remote_state_backend_type, "")
backend_type = try(coalesce(local.remote_state_backend_type, local.config.backend_type), "")
Expand All @@ -31,12 +33,10 @@ locals {
workspace = lookup(local.config, "workspace", "")
workspace_key_prefix = lookup(local.backend, "workspace_key_prefix", null)

remote_state_enabled = !var.bypass

remote_states = {
# s3 = data.terraform_remote_state.s3
# remote = data.terraform_remote_state.remote
data_source = try(data.terraform_remote_state.data_source.outputs, var.defaults)
data_source = try(data.terraform_remote_state.data_source[0].outputs, var.defaults)
bypass = var.defaults
static = local.backend
}
Expand Down
9 changes: 6 additions & 3 deletions test/src/examples_remote_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func TestExamplesRemoteState(t *testing.T) {

terraform.OutputStruct(t, terraformOptions, "remote_state_using_stack", &output)
remoteStateUsingStack := output.(map[string]any)

// Verify we're getting back the outputs we expect
assert.NotNilf(t, remoteStateUsingStack, "remote state is empty")
assert.Equal(t, true, remoteStateUsingStack["val1"])
Expand All @@ -38,7 +37,6 @@ func TestExamplesRemoteState(t *testing.T) {

terraform.OutputStruct(t, terraformOptions, "remote_state_using_context", &output)
remoteStateUsingContext := output.(map[string]any)

// Verify we're getting back the outputs we expect
assert.NotNilf(t, remoteStateUsingContext, "remote state is empty")
assert.Equal(t, true, remoteStateUsingContext["val1"])
Expand All @@ -48,8 +46,13 @@ func TestExamplesRemoteState(t *testing.T) {

terraform.OutputStruct(t, terraformOptions, "remote_state_using_context_ignore_errors", &output)
remoteStateUsingContextIgnoreErrors := output.(map[string]any)

// Verify we're getting back the outputs we expect
assert.NotNilf(t, remoteStateUsingContextIgnoreErrors, "remote state is empty")
assert.Equal(t, "default-value", remoteStateUsingContextIgnoreErrors["default_output"])

terraform.OutputStruct(t, terraformOptions, "remote_state_with_bypass", &output)
remoteStateWithBypass := output.(map[string]any)
// Verify we're getting back the outputs we expect
assert.NotNilf(t, remoteStateWithBypass, "remote state is empty")
assert.Equal(t, "default-value", remoteStateWithBypass["default_output"])
}
61 changes: 31 additions & 30 deletions test/src/go.mod
Original file line number Diff line number Diff line change
@@ -1,58 +1,59 @@
module github.com/cloudposse/terraform-yaml-stack-config

go 1.19
go 1.20

require (
github.com/gruntwork-io/terratest v0.41.10
github.com/stretchr/testify v1.8.1
github.com/gruntwork-io/terratest v0.43.0
github.com/stretchr/testify v1.8.4
)

require (
cloud.google.com/go v0.83.0 // indirect
cloud.google.com/go/storage v1.10.0 // indirect
cloud.google.com/go v0.105.0 // indirect
cloud.google.com/go/compute v1.12.1 // indirect
cloud.google.com/go/compute/metadata v0.2.1 // indirect
cloud.google.com/go/iam v0.7.0 // indirect
cloud.google.com/go/storage v1.27.0 // indirect
github.com/agext/levenshtein v1.2.3 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/aws/aws-sdk-go v1.40.56 // indirect
github.com/aws/aws-sdk-go v1.44.122 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.0 // indirect
github.com/googleapis/gax-go/v2 v2.7.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-getter v1.6.1 // indirect
github.com/hashicorp/go-getter v1.7.1 // indirect
github.com/hashicorp/go-multierror v1.1.0 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-version v1.3.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/hcl/v2 v2.9.1 // indirect
github.com/hashicorp/terraform-json v0.13.0 // indirect
github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jstemmer/go-junit-report v0.9.1 // indirect
github.com/klauspost/compress v1.13.0 // indirect
github.com/klauspost/compress v1.15.11 // indirect
github.com/mattn/go-zglob v0.0.2-0.20190814121620-e3c945676326 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.0.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tmccombs/hcl2json v0.3.3 // indirect
github.com/ulikunitz/xz v0.5.8 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/zclconf/go-cty v1.9.1 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c // indirect
golang.org/x/sys v0.0.0-20220517195934-5e4e11fc645e // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/tools v0.1.2 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/api v0.47.0 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.1.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/oauth2 v0.1.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.103.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
google.golang.org/grpc v1.38.0 // indirect
google.golang.org/protobuf v1.26.0 // indirect
google.golang.org/genproto v0.0.0-20221201164419-0e50fba7f41c // indirect
google.golang.org/grpc v1.51.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit de7da5b

Please sign in to comment.