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: helm template support + helm repo vcs option support #658

Merged
merged 3 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions client/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ type Template struct {
FileName string `json:"fileName,omitempty" tfschema:",omitempty"`
IsTerragruntRunAll bool `json:"isTerragruntRunAll"`
IsAzureDevOps bool `json:"isAzureDevOps" tfschema:"is_azure_devops"`
IsHelmRepository bool `json:"isHelmRepository"`
HelmChartName string `json:"helmChartName,omitempty" tfschema:",omitempty"`
}

type TemplateCreatePayload struct {
Expand All @@ -84,6 +86,8 @@ type TemplateCreatePayload struct {
FileName string `json:"fileName,omitempty"`
IsTerragruntRunAll bool `json:"isTerragruntRunAll"`
IsAzureDevOps bool `json:"isAzureDevOps" tfschema:"is_azure_devops"`
IsHelmRepository bool `json:"isHelmRepository"`
HelmChartName string `json:"helmChartName,omitempty"`
}

type TemplateAssignmentToProjectPayload struct {
Expand Down
17 changes: 17 additions & 0 deletions env0/resource_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var allowedTemplateTypes = []string{
"k8s",
"workflow",
"cloudformation",
"helm",
}

func getTemplateSchema(prefix string) map[string]*schema.Schema {
Expand All @@ -33,6 +34,8 @@ func getTemplateSchema(prefix string) map[string]*schema.Schema {
"is_bitbucket_server",
"is_github_enterprise",
"is_azure_devops",
"helm_chart_name",
"is_helm_repository",
}

allVCSAttributesBut := func(strs ...string) []string {
Expand Down Expand Up @@ -208,6 +211,20 @@ func getTemplateSchema(prefix string) map[string]*schema.Schema {
ConflictsWith: allVCSAttributesBut("is_azure_devops", "token_id"),
RequiredWith: requiredWith("token_id"),
},
"helm_chart_name": {
Type: schema.TypeString,
Optional: true,
Description: "the helm chart name. Required if is_helm_repository is set to 'true'",
ConflictsWith: allVCSAttributesBut("helm_chart_name", "is_helm_repository"),
},
"is_helm_repository": {
tomporat247 marked this conversation as resolved.
Show resolved Hide resolved
tomporat247 marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeBool,
Optional: true,
Description: "true if this template integrates with a helm repository",
Default: "false",
ConflictsWith: allVCSAttributesBut("helm_chart_name", "is_helm_repository"),
RequiredWith: requiredWith("helm_chart_name"),
tomporat247 marked this conversation as resolved.
Show resolved Hide resolved
},
}

if prefix == "" {
Expand Down
64 changes: 63 additions & 1 deletion env0/resource_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,50 @@ func TestUnitTemplateResource(t *testing.T) {
TerraformVersion: "0.15.1",
IsAzureDevOps: true,
}

helmTemplate := client.Template{
Id: "helmTemplate",
Name: "template0",
Description: "description0",
Repository: "env0/repo",
Path: "path/zero/new",
Type: "helm",
HelmChartName: "chart1",
IsHelmRepository: true,
Retry: client.TemplateRetry{
OnDeploy: &client.TemplateRetryOn{
Times: 2,
ErrorRegex: "RetryMeForDeploy.*",
},
OnDestroy: &client.TemplateRetryOn{
Times: 1,
ErrorRegex: "RetryMeForDestroy.*",
},
},
TerraformVersion: "0.12.24",
tomporat247 marked this conversation as resolved.
Show resolved Hide resolved
}
helmUpdatedTemplate := client.Template{
Id: helmTemplate.Id,
Name: "new-name",
Description: "new-description",
Repository: "env0/repo-new",
Path: "path/zero/new",
Type: "helm",
HelmChartName: "chart1",
IsHelmRepository: true,
Retry: client.TemplateRetry{
OnDeploy: &client.TemplateRetryOn{
Times: 2,
ErrorRegex: "RetryMeForDeploy.*",
},
OnDestroy: &client.TemplateRetryOn{
Times: 1,
ErrorRegex: "RetryMeForDestroy.*",
},
},
TerraformVersion: "0.12.24",
}

fullTemplateResourceConfig := func(resourceType string, resourceName string, template client.Template) string {
templateAsDictionary := map[string]interface{}{
"name": template.Name,
Expand Down Expand Up @@ -435,6 +479,12 @@ func TestUnitTemplateResource(t *testing.T) {
if template.IsAzureDevOps {
templateAsDictionary["is_azure_devops"] = true
}
if template.IsHelmRepository {
templateAsDictionary["is_helm_repository"] = true
}
if template.HelmChartName != "" {
templateAsDictionary["helm_chart_name"] = template.HelmChartName
}

return resourceConfigCreate(resourceType, resourceName, templateAsDictionary)
}
Expand All @@ -453,6 +503,11 @@ func TestUnitTemplateResource(t *testing.T) {
tokenIdAssertion = resource.TestCheckNoResourceAttr(resourceFullName, "token_id")
}

helmChartNameAssertion := resource.TestCheckResourceAttr(resourceFullName, "helm_chart_name", template.HelmChartName)
if template.HelmChartName == "" {
helmChartNameAssertion = resource.TestCheckNoResourceAttr(resourceFullName, "helm_chart_name")
}

filenameAssertion := resource.TestCheckResourceAttr(resourceFullName, "file_name", template.FileName)
if template.FileName == "" {
filenameAssertion = resource.TestCheckNoResourceAttr(resourceFullName, "file_name")
Expand Down Expand Up @@ -485,9 +540,11 @@ func TestUnitTemplateResource(t *testing.T) {
gitlabProjectIdAssertion,
terragruntVersionAssertion,
githubInstallationIdAssertion,
helmChartNameAssertion,
resource.TestCheckResourceAttr(resourceFullName, "terraform_version", template.TerraformVersion),
resource.TestCheckResourceAttr(resourceFullName, "is_terragrunt_run_all", strconv.FormatBool(template.IsTerragruntRunAll)),
resource.TestCheckResourceAttr(resourceFullName, "is_azure_devops", strconv.FormatBool(template.IsAzureDevOps)),
resource.TestCheckResourceAttr(resourceFullName, "is_helm_repository", strconv.FormatBool(template.IsHelmRepository)),
)
}

Expand All @@ -504,6 +561,7 @@ func TestUnitTemplateResource(t *testing.T) {
{"Bitbucket Server", bitbucketServerTemplate, bitbucketServerUpdatedTemplate},
{"Cloudformation", cloudformationTemplate, cloudformationUpdatedTemplate},
{"Azure DevOps", azureDevOpsTemplate, azureDevOpsUpdatedTemplate},
{"Helm Chart", helmTemplate, helmUpdatedTemplate},
}
for _, templateUseCase := range templateUseCases {
t.Run("Full "+templateUseCase.vcs+" template (without SSH keys)", func(t *testing.T) {
Expand All @@ -526,7 +584,7 @@ func TestUnitTemplateResource(t *testing.T) {
TokenId: templateUseCase.template.TokenId,
Path: templateUseCase.template.Path,
Revision: templateUseCase.template.Revision,
Type: "terraform",
Type: templateUseCase.template.Type,
Retry: templateUseCase.template.Retry,
TerraformVersion: templateUseCase.template.TerraformVersion,
BitbucketClientKey: templateUseCase.template.BitbucketClientKey,
Expand All @@ -536,6 +594,8 @@ func TestUnitTemplateResource(t *testing.T) {
TerragruntVersion: templateUseCase.template.TerragruntVersion,
IsTerragruntRunAll: templateUseCase.template.IsTerragruntRunAll,
IsAzureDevOps: templateUseCase.template.IsAzureDevOps,
IsHelmRepository: templateUseCase.template.IsHelmRepository,
HelmChartName: templateUseCase.template.HelmChartName,
}
updateTemplateCreateTemplate := client.TemplateCreatePayload{
Name: templateUseCase.updatedTemplate.Name,
Expand All @@ -558,6 +618,8 @@ func TestUnitTemplateResource(t *testing.T) {
TerragruntVersion: templateUseCase.updatedTemplate.TerragruntVersion,
IsTerragruntRunAll: templateUseCase.updatedTemplate.IsTerragruntRunAll,
IsAzureDevOps: templateUseCase.updatedTemplate.IsAzureDevOps,
IsHelmRepository: templateUseCase.template.IsHelmRepository,
HelmChartName: templateUseCase.template.HelmChartName,
}

if templateUseCase.vcs == "Cloudformation" {
Expand Down
17 changes: 17 additions & 0 deletions tests/integration/004_template/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,23 @@ resource "env0_template" "github_template_source_code" {
terraform_version = "0.15.1"
}

resource "env0_template" "helm_template" {
name = "helm-${random_string.random.result}-1"
description = "Template description helm"
repository = "https://github.com/env0/templates"
path = "misc/helm/dummy"
type = "helm"
}

resource "env0_template" "helm_template_repo" {
name = "helm-${random_string.random.result}-2"
description = "Template description helm repo"
repository = "https://charts.bitnami.com/bitnami"
type = "helm"
helm_chart_name = "nginx"
is_helm_repository = true
}

data "env0_source_code_variables" "variables" {
template_id = env0_template.github_template_source_code.id
}
Expand Down