Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Move HCL support to be compatible with 0.12+ syntax #312

@drubin

Description

@drubin

The terraform example doesn't produce valid terraform code. With no clear example of how to make it work.

Example from https://jkcfg.github.io/#/documentation/quick-start

Invalid

"provider" "github" {
  "organization" = "myorg"
}

"github_membership" "myorg_alice84" {
  "username" = "alice84"

  "role" = "admin"
}

"github_membership" "myorg_bob93" {
  "username" = "bob93"

  "role" = "member"
}

expected valid output

provider "github" {
  organization = "myorg"
}

resource github_membership "myorg_alice84" {
  username = "alice84"
  role     = "admin"
}

resource github_membership "myorg_bob93" {
  username = "bob93"
  role     = "member"
}

Quick fixes

We can change the example to the following to get closer to valid terraform 0.11.xx syntax (this is not valid 0.12 syntax yet though because of the quotes around "resources")

const config = {
  provider: {
    github: {
      organization,
      anonymous: true
    },
  },
  resource: {
    github_membership: {},
  }
};

for (const dev of developers) {
  config.resource.github_membership[membershipId(dev)] = {
    username: dev.github.handle,
    role: role(dev),
  };
}

However, replacing the example to export .tf.json produces valid terraform parseable results.

export default [
  { value: config, file: 'github.tf.json' },
];
{
  "provider": {
    "github": {
      "anonymous": true,
      "organization": "myorg"
    }
  },
  "resource": {
    "github_membership": {
      "myorg_alice84": {
        "role": "admin",
        "username": "alice84"
      },
      "myorg_bob93": {
        "role": "member",
        "username": "bob93"
      }
    }
  }
}

Which can then be used with terraform plan

$ ls
developers.js  github.js      github.tf.json 
$ terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # github_membership.myorg_alice84 will be created
  + resource "github_membership" "myorg_alice84" {
      + etag     = (known after apply)
      + id       = (known after apply)
      + role     = "admin"
      + username = "alice84"
    }

  # github_membership.myorg_bob93 will be created
  + resource "github_membership" "myorg_bob93" {
      + etag     = (known after apply)
      + id       = (known after apply)
      + role     = "member"
      + username = "bob93"
    }

Plan: 2 to add, 0 to change, 0 to destroy.

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

Docs

https://www.terraform.io/docs/providers/github/
https://www.terraform.io/docs/providers/github/r/membership.html

Extra info

  • References std: Support writing HCL files #151 for when this was added
  • The above example fix (pr pending) will produce valid terraform 0.11 syntax but this isn't valid for 0.12 yet (excess quotes)

I tried to find how to make this possible but I couldn't find a test or example test case in the codebase. I would love to try to help make this valid as I think this is an incredible way to dynamically generate terraform.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions