Skip to content

Commit

Permalink
Feat: add support for bitbucket templates (#199) (#254)
Browse files Browse the repository at this point in the history
Co-authored-by: update generated docs action <[email protected]>
Co-authored-by: Yaron Yarimi <[email protected]>
  • Loading branch information
3 people committed Mar 15, 2022
1 parent ae7034d commit 9cd8761
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 6 deletions.
2 changes: 2 additions & 0 deletions client/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion docs/resources/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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
Expand Down
21 changes: 18 additions & 3 deletions env0/resource_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"},
},
},
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
51 changes: 50 additions & 1 deletion env0/resource_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
Expand All @@ -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{
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion examples/resources/env0_template/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down

0 comments on commit 9cd8761

Please sign in to comment.