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

Support configuration_aliases argument #40

Closed
takumin opened this issue May 2, 2021 · 3 comments · Fixed by #43
Closed

Support configuration_aliases argument #40

takumin opened this issue May 2, 2021 · 3 comments · Fixed by #43

Comments

@takumin
Copy link

takumin commented May 2, 2021

Starting with Terraform 0.15, configuration_aliases is available.

See also 1: https://www.terraform.io/upgrade-guides/0-15.html#alternative-provider-configurations-within-modules
See also 2: https://www.terraform.io/docs/language/modules/develop/providers.html#provider-aliases-within-modules

For example, the following declaration.

terraform {
  required_providers {
    aws = {
      version = "2.65.0"
      source  = "hashicorp/aws"

      configuration_aliases = [
        aws.primary,
        aws.secondary,
      ]
    }
  }
}

Are you planning to support this syntax?

@minamijoyo
Copy link
Owner

minamijoyo commented May 2, 2021

@takumin Thank you for reporting this!

I confirmed that if we have the configuration_aliases key in the required_providers block, the tfupdate fails to parse and silently ignores the block.

I found the root cause of this problem is a dirty hack in the tfupdate implementation due to limitations of the hcl library. We expect a literal expression here:
https://github.com/minamijoyo/tfupdate/blob/v0.4.3/tfupdate/hclwrite.go#L48-L71

If my understanding is correct, the configuration_aliases contains provider references, that is, variables in hcl and it requires a EvalContext to parse.

I think it should be fixed, but it may be quite difficult under the current technical constraints. I'm not sure how to fix it for now 🤔

@minamijoyo
Copy link
Owner

I found the terraform-config-inspect seems to parse the required_providers block without an EvalContext using hcl.ExprMap. It's worth investigating.
hashicorp/terraform-config-inspect#59

minamijoyo added a commit that referenced this issue May 27, 2021
Fixes #40

The tfupdate mainly uses the hclwrite parser to preserve comments, but
the hclwrite doesn't have enough AST for an object type, as a
compromise, the tfupdate also uses the hclsyntax parser to detect old
version.

Terraform v0.15 introduced a new configuration_aliases syntax.
https://www.terraform.io/upgrade-guides/0-15.html#alternative-provider-configurations-within-modules

It's not directly related version updating but it contains provider
references and it causes a parse error without an EvalContext.

The ideal solution would be extending the hclwrite to be able to parse
such a complicated object, but it's not realistic to expect it to be
possible in the short term. So I decided to add a new hack on top of the
existing hack to achieve the short term goal.

After seeking a temporary solution, I found that the
terraform-config-inspect parses the configuration_aliases by using a
hcl.ExprMap which doesn't require to decode fully references.

I feel the implementation is messy and fragile. It's probably better not
to re-implement by ourselves and depend on the terraform-config-inspect.
However the terraform-config-inspect loads a root module not a file, and
the current implementation of tfupdate detects and updates version in a
single file. It's hard to use it for now. Using the
terraform-config-inspect would require additional refactors for
splitting the implementation to detecting and updating. To minimize the
patch, I fixed only how to parse the required_providers block.
minamijoyo added a commit that referenced this issue May 27, 2021
Fixes #40

The tfupdate mainly uses the hclwrite parser to preserve comments, but
the hclwrite doesn't have enough AST for an object type, as a
compromise, the tfupdate also uses the hclsyntax parser to detect old
version.

Terraform v0.15 introduced a new configuration_aliases syntax.
https://www.terraform.io/upgrade-guides/0-15.html#alternative-provider-configurations-within-modules

It's not directly related version updating but it contains provider
references and it causes a parse error without an EvalContext.

The ideal solution would be extending the hclwrite to be able to parse
such a complicated object, but it's not realistic to expect it to be
possible in the short term. So I decided to add a new hack on top of the
existing hack to achieve the short term goal.

After seeking a temporary solution, I found that the
terraform-config-inspect parses the configuration_aliases by using a
hcl.ExprMap which doesn't require to decode fully references.

I feel the implementation is messy and fragile. It's probably better not
to re-implement by ourselves and depend on the terraform-config-inspect.
However the terraform-config-inspect loads a root module not a file, and
the current implementation of tfupdate detects and updates version in a
single file. It's hard to use it for now. Using the
terraform-config-inspect would require additional refactors for
splitting the implementation to detecting and updating. To minimize the
patch, I fixed only how to parse the required_providers block.
minamijoyo added a commit that referenced this issue May 27, 2021
Fixes #40

The tfupdate mainly uses the hclwrite parser to preserve comments, but
the hclwrite doesn't have enough AST for an object type, as a
compromise, the tfupdate also uses the hclsyntax parser to detect old
version.

Terraform v0.15 introduced a new configuration_aliases syntax.
https://www.terraform.io/upgrade-guides/0-15.html#alternative-provider-configurations-within-modules

It's not directly related version updating but it contains provider
references and it causes a parse error without an EvalContext.

The ideal solution would be extending the hclwrite to be able to parse
such a complicated object, but it's not realistic to expect it to be
possible in the short term. So I decided to add a new hack on top of the
existing hack to achieve the short term goal.

After seeking a temporary solution, I found that the
terraform-config-inspect parses the configuration_aliases by using a
hcl.ExprMap which doesn't require to decode fully references.

I feel the implementation is messy and fragile. It's probably better not
to re-implement by ourselves and depend on the terraform-config-inspect.
However the terraform-config-inspect loads a root module not a file, and
the current implementation of tfupdate detects and updates version in a
single file. It's hard to use it for now. Using the
terraform-config-inspect would require additional refactors for
splitting the implementation to detecting and updating. To minimize the
patch, I fixed only how to parse the required_providers block.
@minamijoyo
Copy link
Owner

@takumin Fixed in v0.5.1 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants