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: terramate generate: add overall tf code generation #193

Merged
merged 50 commits into from
Feb 3, 2022

Conversation

katcipis
Copy link
Contributor

@katcipis katcipis commented Jan 31, 2022

Introduces generate_hcl code generation on the cli, including outdated code detection. Here is an example to showcase how to use + expected results:

#!/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" {

    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" {
  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

@codecov-commenter
Copy link

codecov-commenter commented Feb 1, 2022

Codecov Report

Merging #193 (4f75d50) into main (4c69a40) will increase coverage by 0.91%.
The diff coverage is 92.73%.

❗ Current head 4f75d50 differs from pull request most recent head 74e1199. Consider uploading reports for the commit 74e1199 to get more accurate results

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #193      +/-   ##
==========================================
+ Coverage   61.90%   62.82%   +0.91%     
==========================================
  Files          30       30              
  Lines        5064     5197     +133     
==========================================
+ Hits         3135     3265     +130     
- Misses       1755     1757       +2     
- Partials      174      175       +1     
Flag Coverage Δ
tests 62.82% <92.73%> (+0.91%) ⬆️

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

Impacted Files Coverage Δ
generate/generate.go 90.79% <91.54%> (+1.21%) ⬆️
generate/genhcl/genhcl.go 90.62% <95.00%> (ø)
hcl/hcl.go 80.02% <100.00%> (+0.40%) ⬆️
test/os.go 84.81% <100.00%> (+0.81%) ⬆️
test/sandbox/sandbox.go 86.85% <100.00%> (+0.48%) ⬆️
list.go 91.89% <0.00%> (+0.71%) ⬆️

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 4c69a40...74e1199. Read the comment docs.

Copy link
Contributor

@kassianh kassianh left a comment

Choose a reason for hiding this comment

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

Lgtm but also a bit over my head.

@katcipis katcipis marked this pull request as draft February 2, 2022 14:09
@katcipis katcipis marked this pull request as ready for review February 2, 2022 16:53
generate/generate.go Outdated Show resolved Hide resolved
generate/generate.go Outdated Show resolved Hide resolved
}
workingDir := filepath.Join(s.RootDir(), tcase.workingDir)
err := generate.Do(s.RootDir(), workingDir)
assert.IsError(t, err, tcase.wantErr)
Copy link

Choose a reason for hiding this comment

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

are we actually setting wantErr in any of the test cases? if this is not the case, shouldn't this assertion fail?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we never set wantErr, meaning that we are not expecting a failure, and no failure happens, this is the same as:

assert.IsError(t, nil, nil)

Which works, because nil == nil (they match). If we get an unexpected error, it fails, and if we define wantErr then it would fail if the got err doesn't match.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But since we are not using it here we can remove it, I usually have some error handling tests but so far haven't added at this level. But here you can see how it works:

https://github.com/mineiros-io/terramate/blob/ad148a17de74f6f7b6e4594c69188f920f265483/generate/genhcl/genhcl_test.go#L577

When you set the error, it will check for a specific match with the sentinel wrapped inside the error, when you don't set it it will check if both errors are nil, since only a nil error "is" another nil error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link

Choose a reason for hiding this comment

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

got it! thanks for taking the time to explain! ❤️

generate/genhcl/genhcl.go Outdated Show resolved Hide resolved
thiesen
thiesen previously approved these changes Feb 2, 2022
Copy link

@thiesen thiesen left a comment

Choose a reason for hiding this comment

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

dude, looks great to me! 🧙‍♂️

I added some questions but nothing should be blocking - just https://github.com/mineiros-io/terramate/pull/193/files#r798043896 but I assume I'm missing something since the tests are green)

@katcipis
Copy link
Contributor Author

katcipis commented Feb 2, 2022

dude, looks great to me! mage_man

I added some questions but nothing should be blocking - just https://github.com/mineiros-io/terramate/pull/193/files#r798043896 but I assume I'm missing something since the tests are green)

Thanks for the review man ❤️

Tried to explain and provide some examples about errors.Is, the assert.IsError is a wrapper around errors.Is, so they behave the same way.

Also improved the code with all your suggestions, thanks again ❤️

@katcipis katcipis requested a review from thiesen February 2, 2022 22:44
Copy link

@thiesen thiesen left a comment

Choose a reason for hiding this comment

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

lgtm! 🚀

@katcipis katcipis merged commit daebe33 into main Feb 3, 2022
@katcipis katcipis deleted the katcipis-add-tf-code-to-generate branch February 3, 2022 11:29
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