Skip to content

Commit

Permalink
Add Support to gitlab enterprise templates (#224)
Browse files Browse the repository at this point in the history
* moved validations to schema

* added conflict use case

* added gitlab ee

* tests

* fix property name

* docs + data
  • Loading branch information
elbaz committed Feb 10, 2022
1 parent 706d5cd commit b6ecb09
Show file tree
Hide file tree
Showing 6 changed files with 295 additions and 253 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 {
Revision string `json:"revision"`
OrganizationId string `json:"organizationId"`
TerraformVersion string `json:"terraformVersion"`
IsGitlabEnterprise bool `json:"isGitLabEnterprise"`
}

type TemplateAssignmentToProjectPayload struct {
Expand Down Expand Up @@ -263,6 +264,7 @@ type Template struct {
SshKeys []TemplateSshKey `json:"sshKeys"`
Type string `json:"type"`
GithubInstallationId int `json:"githubInstallationId"`
IsGitlabEnterprise bool `json:"isGitLabEnterprise"`
TokenId string `json:"tokenId,omitempty"`
GitlabProjectId int `json:"gitlabProjectId,omitempty"`
UpdatedAt string `json:"updatedAt"`
Expand Down
4 changes: 2 additions & 2 deletions docs/resources/environment_scheduling.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ data "env0_environment" "example" {
resource "env0_environment_schedling" "example" {
environment_id = data.env0_environment.example.id
deploy_cron = "5 * * * *"
destroy_cron = "10 * * * *"
deploy_cron = "5 * * * *"
destroy_cron = "10 * * * *"
}
```

Expand Down
1 change: 1 addition & 0 deletions docs/resources/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ resource "env0_template_project_assignment" "assignment" {
- **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
- **id** (String) The ID of this resource.
- **is_gitlab_enterprise** (Boolean) Does this template use gitlab enterprise repository?
- **path** (String) terraform / terragrunt file folder inside source code
- **retries_on_deploy** (Number) number of times to retry when deploying an environment based on this template
- **retries_on_destroy** (Number) number of times to retry when destroying an environment based on this template
Expand Down
10 changes: 10 additions & 0 deletions env0/data_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ func dataTemplate() *schema.Resource {
Description: "terraform version to use",
Computed: true,
},
"is_gitlab_enterprise": {
Type: schema.TypeBool,
Description: "Does this template use gitlab enterprise repository?",
Optional: true,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -148,6 +154,10 @@ func dataTemplateRead(ctx context.Context, d *schema.ResourceData, meta interfac
d.Set("gitlab_project_id", template.GitlabProjectId)
}

if template.IsGitlabEnterprise == true {
d.Set("is_gitlab_enterprise", template.IsGitlabEnterprise)
}

return nil
}

Expand Down
53 changes: 30 additions & 23 deletions env0/resource_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,26 +95,37 @@ func resourceTemplate() *schema.Resource {
RequiredWith: []string{"retries_on_destroy"},
},
"github_installation_id": {
Type: schema.TypeInt,
Description: "The env0 application installation id on the relevant github repository",
Optional: true,
Type: schema.TypeInt,
Description: "The env0 application installation id on the relevant github repository",
Optional: true,
ConflictsWith: []string{"token_id"},
},
"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,
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,
RequiredWith: []string{"gitlab_project_id"},
ConflictsWith: []string{"github_installation_id"},
},
"gitlab_project_id": {
Type: schema.TypeInt,
Description: "The project id of the relevant repository",
Optional: true,
Type: schema.TypeInt,
Description: "The project id of the relevant repository",
Optional: true,
RequiredWith: []string{"token_id"},
},
"terraform_version": {
Type: schema.TypeString,
Description: "Terraform version to use",
Optional: true,
Default: "0.15.1",
},
"is_gitlab_enterprise": {
Type: schema.TypeBool,
Description: "Does this template use gitlab enterprise repository?",
Optional: true,
Default: "false",
ConflictsWith: []string{"gitlab_project_id", "token_id", "github_installation_id"},
},
},
}
}
Expand All @@ -132,20 +143,12 @@ func templateCreatePayloadFromParameters(d *schema.ResourceData) (client.Templat
}
if tokenId, ok := d.GetOk("token_id"); ok {
result.TokenId = tokenId.(string)
}
if gitlabProjectId, ok := d.GetOk("gitlab_project_id"); ok {
result.GitlabProjectId = gitlabProjectId.(int)
}

if result.GitlabProjectId != 0 && result.TokenId == "" {
return client.TemplateCreatePayload{}, diag.Errorf("Cannot set gitlab_project_id without token_id")
}

if result.GithubInstallationId != 0 && result.TokenId != "" {
return client.TemplateCreatePayload{}, diag.Errorf("Cannot set token_id and github_installation_id for the same template")
} else {
result.GitlabProjectId = d.Get("gitlab_project_id").(int)
result.IsGitLab = result.TokenId != ""
}
if isGitlabEnterprise, ok := d.GetOk("is_gitlab_enterprise"); ok {
result.IsGitlabEnterprise = isGitlabEnterprise.(bool)
}

if path, ok := d.GetOk("path"); ok {
result.Path = path.(string)
Expand Down Expand Up @@ -246,8 +249,12 @@ func resourceTemplateRead(ctx context.Context, d *schema.ResourceData, meta inte

d.Set("name", template.Name)
d.Set("description", template.Description)
d.Set("github_installation_id", template.GithubInstallationId)
d.Set("token_id", template.TokenId)
if template.GithubInstallationId != 0 {
d.Set("github_installation_id", template.GithubInstallationId)
}
if template.TokenId != "" {
d.Set("token_id", template.TokenId)
}
d.Set("repository", template.Repository)
d.Set("path", template.Path)
d.Set("revision", template.Revision)
Expand Down
Loading

0 comments on commit b6ecb09

Please sign in to comment.