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

feat: add content block on generate_hcl #220

Merged
merged 6 commits into from
Feb 16, 2022

Conversation

katcipis
Copy link
Contributor

This PRs changes the behavior of code generation using generate_hcl. Instead of generating code by doing:

generate_hcl {
  terraform {
  }
}

All generated HCL must be inside the content block, like this:

generate_hcl {
  content {
    terraform {
    }
  }
}

A full example:

#!/bin/bash

set -o nounset
set -o errexit

basedir=$(mktemp -d)

cd "${basedir}"

# We need the project root config since it is not git
cat > terramate.tm.hcl <<- EOM
terramate {
  config {
  }
}
EOM

mkdir -p stacks/stack-{1,2}
terramate stacks init stacks/stack-{1,2}

# Defining some code for all stacks
# Globals are defined on root and also on stacks, to showcase possibilities
# Inspired on: https://github.com/mineiros-io/terramate-blueprint-iac-github/tree/main/stacks/repositories/teamA
cat > stacks/terramate.tm.hcl <<- EOM
globals {
    tf_version = "1.1.3"
    gh_owner = "the-awesome-mineiros"
    gh_provider_version = "4.19.1"
}

generate_hcl "provider.tf" {
    content {
        provider "github" {
          owner = global.gh_owner
        }

        terraform {
          required_providers {
            github = {
              source  = "integrations/github"
              version = global.gh_provider_version
            }
          }
        }

        terraform {
          required_version = global.tf_version
        }
    }
}

generate_hcl "main.tf" {
  content {
      module "repositories" {
        source = "../../../modules/terramate-github-repositories"

        repositories        = global.repositories
        repository_defaults = global.repository_defaults
        member_map          = global.member_map
      }
  }
}
EOM

# Now add some per stack globals

cat > stacks/stack-1/terramate.tm.hcl <<- EOM
globals {
    repositories = [
        {
            name = "stack-1"
            visibility = "private"
        }
    ]
    repository_defaults = {
        config = "stack-1"
    }
    member_map  = {
        "first.lastname" = "powerpuffGirls69"
    }
}
stack {}
EOM

cat > stacks/stack-2/terramate.tm.hcl <<- EOM
globals {
    repositories = [
        {
            name = "stack-2"
            visibility = "public"
        }
    ]
    repository_defaults = {
        config = "stack-2"
    }
    member_map  = {
        "first.lastname" = "onePunchMan666"
    }
}
stack {}
EOM


echo "initial tree"
tree

echo
echo "generating code for all stacks"
terramate generate

echo "tree after generating code"
tree

echo
echo "checking generated main.tf files"
terramate run -- cat main.tf

echo
echo "checking generated provider.tf files"
terramate run -- cat provider.tf

@katcipis katcipis self-assigned this Feb 15, 2022
@katcipis katcipis requested a review from i4ki February 15, 2022 17:37
mariux
mariux previously approved these changes Feb 15, 2022
Copy link
Contributor

@mariux mariux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@codecov-commenter
Copy link

codecov-commenter commented Feb 15, 2022

Codecov Report

Merging #220 (5617d26) into main (00e6f7b) will increase coverage by 0.06%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #220      +/-   ##
==========================================
+ Coverage   63.59%   63.66%   +0.06%     
==========================================
  Files          30       29       -1     
  Lines        5390     5403      +13     
==========================================
+ Hits         3428     3440      +12     
- Misses       1779     1780       +1     
  Partials      183      183              
Flag Coverage Δ
tests 63.66% <100.00%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
generate/genhcl/genhcl.go 93.02% <100.00%> (+1.07%) ⬆️
test/hcl.go 53.16% <0.00%> (-4.59%) ⬇️
cmd/terramate/cli/cli.go 0.00% <0.00%> (ø)
run/run.go
hcl/hcl.go 81.68% <0.00%> (+0.24%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 00e6f7b...5617d26. Read the comment docs.

Comment on lines 254 to +266
if len(block.Body.Attributes) != 0 {
return errors.New("attributes are not allowed")
}
if len(block.Body.Blocks) != 1 {
return fmt.Errorf("one 'content' block is required, got %d blocks", len(block.Body.Blocks))
}
contentBlock := block.Body.Blocks[0]
if contentBlock.Type != "content" {
return fmt.Errorf("one 'content' block is required, got %q block", contentBlock.Type)
}
if len(contentBlock.Labels) > 0 {
return fmt.Errorf("content block has unexpected labels: %v", contentBlock.Labels)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we manually check those... can't we use the go structures to let HCL library check this for us?

Copy link
Contributor Author

@katcipis katcipis Feb 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then we need to define the struct/type (which is a form of manual definition also, but more descriptive) and I didn't see any immediate use for the struct since I just grab the whole content body after validation (it is dynamic, I dont know the structure of the content body), if we had more specific attributes that we need we could indeed use a Go struct with some specific fields.

Copy link
Contributor Author

@katcipis katcipis Feb 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you mentioned "Go Structures" I immediately thought of hclsimple.

Which in this case seems less helpful IMO (but quite helpful on some scenarios), but now I think maybe you meant the BodySchema.

Which could make some of the basic validation for us, will take a look (haven't used it yet, sorry for the ignorance).

i4ki
i4ki previously approved these changes Feb 16, 2022
Copy link
Contributor

@i4ki i4ki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

docs/hcl-generation.md Outdated Show resolved Hide resolved
Co-authored-by: Tiago de Bem Natel de Moura <t.nateldemoura@gmail.com>
@katcipis katcipis merged commit d68152f into main Feb 16, 2022
@katcipis katcipis deleted the katcipis-change-gen-hcl-use-content branch February 16, 2022 11:00
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 this pull request may close these issues.

None yet

4 participants