Skip to content

Commit 70ff70c

Browse files
authored
Allow to use opentofu with generate_projects (#1757)
* Allow to use opentofu with generate_projects The current implementation allows the use of opentofu in statically defined projects, but not in projects dynamically generated by generate_projects. This patch adds the opentofu flag to the blocks in generate_projects as well, making opentofu available in a more dynamic environment. * Add opentofu attribute to digger.yml reference Also added the missing blocks attribute and Block type documentation. * Mention how to enable the opentofu attribute when using OpenTofu
1 parent 63ff5a2 commit 70ff70c

File tree

5 files changed

+69
-9
lines changed

5 files changed

+69
-9
lines changed

docs/ce/howto/specify-terraform-version.mdx

+6-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2222
```
2323

24-
And similarly for openTofu:
24+
And similarly for OpenTofu:
2525

2626
```
2727
jobs:
@@ -36,4 +36,8 @@ jobs:
3636
env:
3737
GITHUB_CONTEXT: ${{ toJson(github) }}
3838
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
39-
```
39+
```
40+
41+
<Note>
42+
When you use OpenTofu, you also need to enable `opentofu: true` in the project settings in digger.yml.
43+
</Note>

docs/ce/reference/digger.yml.mdx

+20-4
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ workflows:
115115
| name | string | | yes | name of the project | must be unique |
116116
| dir | string | | yes | directory containing the project | |
117117
| workspace | string | default | no | terraform workspace to use | |
118+
| opentofu | boolean | false | no | whether to use opentofu | |
118119
| terragrunt | boolean | false | no | whether to use terragrunt | |
119120
| workflow | string | default | no | workflow to use | default workflow will be created for you described in workflow section |
120121
| include\_patterns | array of strings | \[\] | no | list of directory glob patterns to include, e.g. `./modules` | see [Include / Exclude Patterns](/ce/howto/include-exclude-patterns) |
@@ -124,10 +125,25 @@ workflows:
124125

125126
### GenerateProjects
126127

127-
| Key | Type | Default | Required | Description | Notes |
128-
| ------- | ------ | ------- | -------- | ----------------------------------- | ----- |
129-
| include | string | | no | glob pattern to include directories | |
130-
| exclude | string | | no | glob pattern to exclude directories | |
128+
| Key | Type | Default | Required | Description | Notes |
129+
| ------- | -------------------------------------------------- | ------- | -------- | ----------------------------------- | ----- |
130+
| include | string | | no | glob pattern to include directories | |
131+
| exclude | string | | no | glob pattern to exclude directories | |
132+
| blocks | array of [Block](/ce/reference/digger.yml#block) | \[\] | no | list of blocks to generate projects | |
133+
134+
### Block
135+
136+
| Key | Type | Default | Required | Description | Notes |
137+
| ------------------ | ----------------------------------------------------- | ------- | -------- | ------------------------------------------------------------- | ---------------------------------------------------------------------- |
138+
| block_name | string | | no | name of the block | |
139+
| workflow | string | default | no | workflow to use | default workflow will be created for you described in workflow section |
140+
| workflow_file | string | | no | name of workflow file for GitHub Actions | |
141+
| aws_role_to_assume | [RoleToAssume](/ce/reference/digger.yml#roletoassume) | | no | A string representing the AWS role to assume for this project | |
142+
| include | string | | no | glob pattern to include directories | only for terraform and opentofu |
143+
| exclude | string | | no | glob pattern to exclude directories | only for terraform and opentofu |
144+
| opentofu | boolean | false | no | whether to use opentofu | only for opentofu |
145+
| terragrunt | boolean | false | no | whether to use terragrunt | only for terragrunt |
146+
| root_dir | string | | no | root directory of terragrunt projects | only for terragrunt |
131147

132148
### Workflows
133149

libs/digger_config/digger_config.go

+1
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ func HandleYamlProjectGeneration(config *DiggerConfigYaml, terraformDir string,
301301
Dir: dir,
302302
Workflow: workflow,
303303
Workspace: "default",
304+
OpenTofu: b.OpenTofu,
304305
AwsRoleToAssume: b.AwsRoleToAssume,
305306
Generated: true,
306307
}

libs/digger_config/digger_config_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,42 @@ workflows:
884884
assert.Equal(t, 3, len(dg.Projects))
885885
}
886886

887+
func TestDiggerGenerateProjectsWithOpenTofu(t *testing.T) {
888+
tempDir, teardown := setUp()
889+
defer teardown()
890+
891+
diggerCfg := `
892+
generate_projects:
893+
blocks:
894+
- include: tofu/*
895+
opentofu: true
896+
- include: terraform/*
897+
`
898+
deleteFile := createFile(path.Join(tempDir, "digger.yml"), diggerCfg)
899+
defer deleteFile()
900+
dirsToCreate := []string{"tofu/test1", "tofu/test2", "terraform/one"}
901+
902+
for _, dir := range dirsToCreate {
903+
err := os.MkdirAll(path.Join(tempDir, dir), os.ModePerm)
904+
defer createFile(path.Join(tempDir, dir, "main.tf"), "")()
905+
assert.NoError(t, err, "expected error to be nil")
906+
}
907+
908+
dg, _, _, err := LoadDiggerConfig(tempDir, true, nil)
909+
assert.NoError(t, err, "expected error to be nil")
910+
assert.NotNil(t, dg, "expected digger digger_config to be not nil")
911+
assert.Equal(t, "tofu_test1", dg.Projects[0].Name)
912+
assert.Equal(t, "tofu_test2", dg.Projects[1].Name)
913+
assert.Equal(t, "terraform_one", dg.Projects[2].Name)
914+
assert.Equal(t, true, dg.Projects[0].OpenTofu)
915+
assert.Equal(t, true, dg.Projects[1].OpenTofu)
916+
assert.Equal(t, false, dg.Projects[2].OpenTofu)
917+
assert.Equal(t, "tofu/test1", dg.Projects[0].Dir)
918+
assert.Equal(t, "tofu/test2", dg.Projects[1].Dir)
919+
assert.Equal(t, "terraform/one", dg.Projects[2].Dir)
920+
assert.Equal(t, 3, len(dg.Projects))
921+
}
922+
887923
// TestDiggerGenerateProjectsEmptyParameters test if missing parameters for generate_projects are handled correctly
888924
func TestDiggerGenerateProjectsEmptyParameters(t *testing.T) {
889925
_, teardown := setUp()

libs/digger_config/yaml.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,18 @@ type EnvVarYaml struct {
8888
}
8989

9090
type BlockYaml struct {
91-
// these flags for terraform only
91+
// these flags are only for terraform and opentofu
9292
Include string `yaml:"include"`
9393
Exclude string `yaml:"exclude"`
9494

95-
// these flags are only for terragrunt only
95+
// these flags are only for terragrunt
9696
Terragrunt bool `yaml:"terragrunt"`
9797
RootDir *string `yaml:"root_dir"`
9898

99-
// these flags for both terraform and terragrunt
99+
// these flags are only for opentofu
100+
OpenTofu bool `yaml:"opentofu"`
101+
102+
// common flags
100103
BlockName string `yaml:"block_name"`
101104
Workflow string `yaml:"workflow"`
102105
WorkflowFile string `yaml:"workflow_file"`

0 commit comments

Comments
 (0)