Skip to content

Commit a399839

Browse files
author
Raz Ben Simon
authored
fix gitlab_project_id changes every apply (#145)
* fix gitlab_project_id changes every apply * fix broken test * add test * fix docs
1 parent b9eaf7c commit a399839

File tree

3 files changed

+93
-41
lines changed

3 files changed

+93
-41
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ provider "env0" {
7070
### Run local version of the provider
7171
- Build - `./build.sh`
7272
- Create the plugins folder - `mkdir -p ~/.terraform.d/plugins/terraform.env0.com/local/env0/6.6.6/darwin_amd64`
73-
- Copy the built binary - `cp ~/env0/terraform-provider-env0/terraform-provider-env0 ~/.terraform.d/plugins/terraform.env0.com/local/env0/6.6.6/darwin_amd64` (Replace `darwin` with `linux` on Linux)
73+
- Copy the built binary - `cp ./terraform-provider-env0 ~/.terraform.d/plugins/terraform.env0.com/local/env0/6.6.6/darwin_amd64` (Replace `darwin` with `linux` on Linux)
7474
- Require the local provider in your `main.tf` -
7575
```
7676
terraform {
@@ -132,12 +132,12 @@ go generate ./...
132132

133133
## Documentation
134134
- Docs are generated using github.com/hashicorp/terraform-plugin-docs
135-
- Run `./generate-docs` to generate docs
135+
- Run `./generate-docs.sh` to generate docs
136136
- Must be run manually before releasing a version
137137

138138
## Release
139139
To release a version to the [Terraform Public Registry](https://registry.terraform.io/providers/env0/env0/latest?pollNotifications=true) -
140-
1. Docs must be generated manually and committed to the repo before release. (`./generate-docs`)
140+
1. Docs must be generated manually and committed to the repo before release. (`./generate-docs.sh`)
141141
2. Create and push a tag locally, in semver format - `git tag v0.0.9 && git push origin --tags`
142142
3. Goto [Github Releases](https://github.com/env0/terraform-provider-env0/releases) and edit the draft created by Release Drafter Bot - it should contain the change log for the release. Make sure it's pointing at the tag you created in the previous step and publish the release.
143143
4. Binaries will be automatically generated by the Github action defined in `.github/workflows/release.yml`

env0/resource_template.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,12 @@ func resourceTemplateRead(ctx context.Context, d *schema.ResourceData, meta inte
243243
d.Set("description", template.Description)
244244
d.Set("github_installation_id", template.GithubInstallationId)
245245
d.Set("token_id", template.TokenId)
246-
d.Set("gitlab_project_id", template.GitlabProjectId)
247246
d.Set("repository", template.Repository)
248247
d.Set("path", template.Path)
249248
d.Set("revision", template.Revision)
250249
d.Set("type", template.Type)
251250
d.Set("terraform_version", template.TerraformVersion)
251+
// 'gitlab_project_id' should not be set because it doesn't exist on 'template'
252252

253253
var rawSshKeys []map[string]string
254254
for _, sshKey := range template.SshKeys {

env0/resource_template_test.go

Lines changed: 89 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,52 @@ func TestUnitTemplateResource(t *testing.T) {
1818

1919
var resourceFullName = resourceAccessor(resourceType, resourceName)
2020

21+
fullTemplateResourceConfig := func(resourceType string, resourceName string, template client.Template) string {
22+
templateAsDictionary := map[string]interface{}{
23+
"name": template.Name,
24+
"repository": template.Repository,
25+
}
26+
27+
if template.Type != "" {
28+
templateAsDictionary["type"] = template.Type
29+
}
30+
if template.Description != "" {
31+
templateAsDictionary["description"] = template.Description
32+
}
33+
if template.Revision != "" {
34+
templateAsDictionary["revision"] = template.Revision
35+
}
36+
if template.Path != "" {
37+
templateAsDictionary["path"] = template.Path
38+
}
39+
if template.Retry != (client.TemplateRetry{}) && template.Retry.OnDeploy != nil {
40+
templateAsDictionary["retries_on_deploy"] = template.Retry.OnDeploy.Times
41+
if template.Retry.OnDeploy.ErrorRegex != "" {
42+
templateAsDictionary["retry_on_deploy_only_when_matches_regex"] = template.Retry.OnDeploy.ErrorRegex
43+
}
44+
}
45+
if template.Retry != (client.TemplateRetry{}) && template.Retry.OnDestroy != nil {
46+
templateAsDictionary["retries_on_destroy"] = template.Retry.OnDestroy.Times
47+
if template.Retry.OnDestroy.ErrorRegex != "" {
48+
templateAsDictionary["retry_on_destroy_only_when_matches_regex"] = template.Retry.OnDestroy.ErrorRegex
49+
}
50+
}
51+
if template.TerraformVersion != "" {
52+
templateAsDictionary["terraform_version"] = template.TerraformVersion
53+
}
54+
if template.TokenId != "" {
55+
templateAsDictionary["token_id"] = template.TokenId
56+
}
57+
if template.GitlabProjectId != 0 {
58+
templateAsDictionary["gitlab_project_id"] = template.GitlabProjectId
59+
}
60+
if template.GithubInstallationId != 0 {
61+
templateAsDictionary["github_installation_id"] = template.GithubInstallationId
62+
}
63+
64+
return resourceConfigCreate(resourceType, resourceName, templateAsDictionary)
65+
}
66+
2167
t.Run("Full Github template (without SSH keys)", func(t *testing.T) {
2268
template := client.Template{
2369
Id: "id0",
@@ -80,23 +126,6 @@ func TestUnitTemplateResource(t *testing.T) {
80126
)
81127
}
82128

83-
fullTemplateResourceConfig := func(resourceType string, resourceName string, template client.Template) string {
84-
return resourceConfigCreate(resourceType, resourceName, map[string]interface{}{
85-
"name": template.Name,
86-
"description": template.Description,
87-
"repository": template.Repository,
88-
"path": template.Path,
89-
"revision": template.Revision,
90-
"type": template.Type,
91-
"retries_on_deploy": template.Retry.OnDeploy.Times,
92-
"retry_on_deploy_only_when_matches_regex": template.Retry.OnDeploy.ErrorRegex,
93-
"retries_on_destroy": template.Retry.OnDestroy.Times,
94-
"retry_on_destroy_only_when_matches_regex": template.Retry.OnDestroy.ErrorRegex,
95-
"github_installation_id": template.GithubInstallationId,
96-
"terraform_version": template.TerraformVersion,
97-
})
98-
}
99-
100129
testCase := resource.TestCase{
101130
Steps: []resource.TestStep{
102131
{
@@ -208,24 +237,6 @@ func TestUnitTemplateResource(t *testing.T) {
208237
)
209238
}
210239

211-
fullTemplateResourceConfig := func(resourceType string, resourceName string, template client.Template) string {
212-
return resourceConfigCreate(resourceType, resourceName, map[string]interface{}{
213-
"name": template.Name,
214-
"description": template.Description,
215-
"repository": template.Repository,
216-
"path": template.Path,
217-
"revision": template.Revision,
218-
"type": template.Type,
219-
"retries_on_deploy": template.Retry.OnDeploy.Times,
220-
"retry_on_deploy_only_when_matches_regex": template.Retry.OnDeploy.ErrorRegex,
221-
"retries_on_destroy": template.Retry.OnDestroy.Times,
222-
"retry_on_destroy_only_when_matches_regex": template.Retry.OnDestroy.ErrorRegex,
223-
"token_id": template.TokenId,
224-
"gitlab_project_id": template.GitlabProjectId,
225-
"terraform_version": template.TerraformVersion,
226-
})
227-
}
228-
229240
testCase := resource.TestCase{
230241
Steps: []resource.TestStep{
231242
{
@@ -483,15 +494,18 @@ func TestUnitTemplateResource(t *testing.T) {
483494
Steps: []resource.TestStep{
484495
{
485496
Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{"name": "test", "repository": "env0/test", regexAttribute: "bla"}),
486-
ExpectError: regexp.MustCompile(fmt.Sprintf("`%s,%s` must be specified", timesAttribute, regexAttribute)),
497+
ExpectError: regexp.MustCompile(fmt.Sprintf("`%s,%s`\\s+must\\s+be\\s+specified", timesAttribute, regexAttribute)),
487498
},
488499
},
489500
})
490501
}
491502

503+
for _, testCase := range testCases {
504+
runUnitTest(t, testCase, func(mockFunc *client.MockApiClientInterface) {})
505+
}
492506
})
493507

494-
t.Run("Gitlab and Github template", func(t *testing.T) {
508+
t.Run("Mixed Gitlab and Github template", func(t *testing.T) {
495509
var testCases []resource.TestCase
496510

497511
testCases = append(testCases, resource.TestCase{
@@ -507,4 +521,42 @@ func TestUnitTemplateResource(t *testing.T) {
507521
runUnitTest(t, testCase, func(mockFunc *client.MockApiClientInterface) {})
508522
}
509523
})
524+
525+
t.Run("Should not trigger terraform changes when gitlab_project_id is provided", func(t *testing.T) {
526+
template := client.Template{
527+
Id: "id0",
528+
Name: "template0",
529+
Repository: "env0/repo",
530+
Type: "terraform",
531+
GitlabProjectId: 123456,
532+
TokenId: "abcdefg",
533+
TerraformVersion: defaultVersion,
534+
}
535+
536+
tfConfig := fullTemplateResourceConfig(resourceType, resourceName, template)
537+
var testCase = resource.TestCase{
538+
Steps: []resource.TestStep{
539+
{
540+
Config: tfConfig,
541+
Check: resource.ComposeTestCheckFunc(
542+
resource.TestCheckResourceAttr(resourceFullName, "gitlab_project_id", strconv.Itoa(template.GitlabProjectId)),
543+
),
544+
},
545+
{
546+
PlanOnly: true,
547+
ExpectNonEmptyPlan: false,
548+
Config: tfConfig,
549+
Check: resource.ComposeTestCheckFunc(
550+
resource.TestCheckResourceAttr(resourceFullName, "gitlab_project_id", strconv.Itoa(template.GitlabProjectId)),
551+
),
552+
},
553+
},
554+
}
555+
556+
runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {
557+
mock.EXPECT().Template(template.Id).Times(3).Return(template, nil) // 1 after create, 1 before update, 1 after update
558+
mock.EXPECT().TemplateCreate(gomock.Any()).Times(1).Return(template, nil)
559+
mock.EXPECT().TemplateDelete(template.Id).Times(1).Return(nil)
560+
})
561+
})
510562
}

0 commit comments

Comments
 (0)