From 9cd8761ff0c28d983cc52083bb25feae11361a59 Mon Sep 17 00:00:00 2001 From: Tomer Heber Date: Tue, 15 Mar 2022 10:21:56 -0500 Subject: [PATCH] Feat: add support for bitbucket templates (#199) (#254) Co-authored-by: update generated docs action Co-authored-by: Yaron Yarimi --- client/model.go | 2 + docs/resources/template.md | 3 +- env0/resource_template.go | 21 ++++++-- env0/resource_template_test.go | 51 +++++++++++++++++++- examples/resources/env0_template/resource.tf | 2 +- 5 files changed, 73 insertions(+), 6 deletions(-) diff --git a/client/model.go b/client/model.go index 8e5b6080..55e3569f 100644 --- a/client/model.go +++ b/client/model.go @@ -219,6 +219,7 @@ type TemplateCreatePayload struct { OrganizationId string `json:"organizationId"` TerraformVersion string `json:"terraformVersion"` IsGitlabEnterprise bool `json:"isGitLabEnterprise"` + BitbucketClientKey string `json:"bitbucketClientKey,omitempty"` } type TemplateAssignmentToProjectPayload struct { @@ -269,6 +270,7 @@ type Template struct { UpdatedAt string `json:"updatedAt"` TerraformVersion string `json:"terraformVersion"` IsDeleted bool `json:"isDeleted,omitempty"` + BitbucketClientKey string `json:"bitbucketClientKey"` } type Environment struct { diff --git a/docs/resources/template.md b/docs/resources/template.md index e31dfd84..ae580fd5 100644 --- a/docs/resources/template.md +++ b/docs/resources/template.md @@ -26,7 +26,7 @@ resource "env0_template" "example" { description = "Example template" repository = "https://github.com/env0/templates" path = "aws/hello-world" - ssh_keys = [data.ssh_keys.my_key] + ssh_keys = [data.env0_ssh_key.my_key] } resource "env0_template_project_assignment" "assignment" { @@ -45,6 +45,7 @@ resource "env0_template_project_assignment" "assignment" { ### Optional +- **bitbucket_client_key** (String) The bitbucket client key used for integration - **description** (String) description for the template - **github_installation_id** (Number) The env0 application installation id on the relevant github repository - **gitlab_project_id** (Number) The project id of the relevant repository diff --git a/env0/resource_template.go b/env0/resource_template.go index 36907c34..9eaa376a 100644 --- a/env0/resource_template.go +++ b/env0/resource_template.go @@ -99,13 +99,13 @@ func resourceTemplate() *schema.Resource { Type: schema.TypeInt, Description: "The env0 application installation id on the relevant github repository", Optional: true, - ConflictsWith: []string{"token_id"}, + ConflictsWith: []string{"token_id", "bitbucket_client_key"}, }, "token_id": { Type: schema.TypeString, Description: "The token id used for private git repos or for integration with GitLab, you can get this value by using a data resource of an existing Gitlab template or contact our support team", Optional: true, - ConflictsWith: []string{"github_installation_id"}, + ConflictsWith: []string{"github_installation_id", "bitbucket_client_key"}, }, "gitlab_project_id": { Type: schema.TypeInt, @@ -124,7 +124,13 @@ func resourceTemplate() *schema.Resource { Description: "Does this template use gitlab enterprise repository?", Optional: true, Default: "false", - ConflictsWith: []string{"gitlab_project_id", "token_id", "github_installation_id"}, + ConflictsWith: []string{"gitlab_project_id", "token_id", "github_installation_id", "bitbucket_client_key"}, + }, + "bitbucket_client_key": { + Type: schema.TypeString, + Description: "The bitbucket client key used for integration", + Optional: true, + ConflictsWith: []string{"token_id", "github_installation_id"}, }, }, } @@ -214,6 +220,11 @@ func templateCreatePayloadFromParameters(d *schema.ResourceData) (client.Templat if terraformVersion, ok := d.GetOk("terraform_version"); ok { result.TerraformVersion = terraformVersion.(string) } + + if bitbucketClientKey, ok := d.GetOk("bitbucket_client_key"); ok { + result.BitbucketClientKey = bitbucketClientKey.(string) + } + return result, nil } @@ -284,6 +295,10 @@ func resourceTemplateRead(ctx context.Context, d *schema.ResourceData, meta inte d.Set("retry_on_destroy_only_when_matches_regex", "") } + if template.BitbucketClientKey != "" { + d.Set("bitbucket_client_key", template.BitbucketClientKey) + } + return nil } diff --git a/env0/resource_template_test.go b/env0/resource_template_test.go index 758b4263..dbbd85c2 100644 --- a/env0/resource_template_test.go +++ b/env0/resource_template_test.go @@ -146,7 +146,48 @@ func TestUnitTemplateResource(t *testing.T) { GithubInstallationId: 2, TerraformVersion: "0.15.1", } - + bitbucketTemplate := client.Template{ + Id: "id0", + Name: "template0", + Description: "description0", + Repository: "env0/repo", + Path: "path/zero", + Revision: "branch-zero", + Retry: client.TemplateRetry{ + OnDeploy: &client.TemplateRetryOn{ + Times: 2, + ErrorRegex: "RetryMeForDeploy.*", + }, + OnDestroy: &client.TemplateRetryOn{ + Times: 1, + ErrorRegex: "RetryMeForDestroy.*", + }, + }, + Type: "terraform", + TerraformVersion: "0.12.24", + BitbucketClientKey: "clientkey", + } + bitbucketUpdatedTemplate := client.Template{ + Id: bitbucketTemplate.Id, + Name: "new-name", + Description: "new-description", + Repository: "env0/repo-new", + Path: "path/zero/new", + Revision: "branch-zero-new", + Retry: client.TemplateRetry{ + OnDeploy: &client.TemplateRetryOn{ + Times: 1, + ErrorRegex: "NewForDeploy.*", + }, + OnDestroy: &client.TemplateRetryOn{ + Times: 2, + ErrorRegex: "NewForDestroy.*", + }, + }, + Type: "terragrunt", + BitbucketClientKey: "clientkey2", + TerraformVersion: "0.15.1", + } fullTemplateResourceConfig := func(resourceType string, resourceName string, template client.Template) string { templateAsDictionary := map[string]interface{}{ "name": template.Name, @@ -192,6 +233,9 @@ func TestUnitTemplateResource(t *testing.T) { if template.IsGitlabEnterprise != false { templateAsDictionary["is_gitlab_enterprise"] = template.IsGitlabEnterprise } + if template.BitbucketClientKey != "" { + templateAsDictionary["bitbucket_client_key"] = template.BitbucketClientKey + } return resourceConfigCreate(resourceType, resourceName, templateAsDictionary) } @@ -238,6 +282,7 @@ func TestUnitTemplateResource(t *testing.T) { {"GitLab EE", gleeTemplate, gleeUpdatedTemplate}, {"GitLab", gitlabTemplate, gitlabUpdatedTemplate}, {"GitHub", githubTemplate, githubUpdatedTemplate}, + {"Bitbucket", bitbucketTemplate, bitbucketUpdatedTemplate}, } for _, templateUseCase := range templateUseCases { t.Run("Full "+templateUseCase.vcs+" template (without SSH keys)", func(t *testing.T) { @@ -255,6 +300,7 @@ func TestUnitTemplateResource(t *testing.T) { Type: client.TemplateTypeTerraform, Retry: templateUseCase.template.Retry, TerraformVersion: templateUseCase.template.TerraformVersion, + BitbucketClientKey: templateUseCase.template.BitbucketClientKey, } updateTemplateCreateTemplate := client.TemplateCreatePayload{ Name: templateUseCase.updatedTemplate.Name, @@ -270,6 +316,7 @@ func TestUnitTemplateResource(t *testing.T) { Type: client.TemplateTypeTerragrunt, Retry: templateUseCase.updatedTemplate.Retry, TerraformVersion: templateUseCase.updatedTemplate.TerraformVersion, + BitbucketClientKey: templateUseCase.updatedTemplate.BitbucketClientKey, } testCase := resource.TestCase{ @@ -525,6 +572,8 @@ func TestUnitTemplateResource(t *testing.T) { {"GitLab", "GitHub", map[string]interface{}{"name": "test", "repository": "env0/test", "github_installation_id": 1, "token_id": "2"}, "\"github_installation_id\": conflicts with token_id"}, {"GitLab", "GitLab EE", map[string]interface{}{"name": "test", "repository": "env0/test", "token_id": "2", "is_gitlab_enterprise": "true"}, "\"is_gitlab_enterprise\": conflicts with token_id"}, {"GitHub", "GitLab EE", map[string]interface{}{"name": "test", "repository": "env0/test", "github_installation_id": 1, "is_gitlab_enterprise": "true"}, "\"is_gitlab_enterprise\": conflicts with github_installation_id"}, + {"GitHub", "Bitbucket", map[string]interface{}{"name": "test", "repository": "env0/test", "github_installation_id": 1, "bitbucket_client_key": "3"}, "\"github_installation_id\": conflicts with bitbucket_client_key"}, + {"GitLab", "Bitbucket", map[string]interface{}{"name": "test", "repository": "env0/test", "token_id": "2", "bitbucket_client_key": "3"}, "\"token_id\": conflicts with bitbucket_client_key"}, } for _, mixUseCase := range mixedUsecases { t.Run("Mixed "+mixUseCase.firstVcs+" and "+mixUseCase.secondVcs+" template", func(t *testing.T) { diff --git a/examples/resources/env0_template/resource.tf b/examples/resources/env0_template/resource.tf index e749be8d..73626ffa 100644 --- a/examples/resources/env0_template/resource.tf +++ b/examples/resources/env0_template/resource.tf @@ -11,7 +11,7 @@ resource "env0_template" "example" { description = "Example template" repository = "https://github.com/env0/templates" path = "aws/hello-world" - ssh_keys = [data.ssh_keys.my_key] + ssh_keys = [data.env0_ssh_key.my_key] } resource "env0_template_project_assignment" "assignment" {