Skip to content

Commit

Permalink
Merge branch 'main' into feat-ds-variable-set-#892
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber authored Jul 29, 2024
2 parents 593cd9a + e7b53a1 commit 98d5bb5
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 39 deletions.
3 changes: 2 additions & 1 deletion client/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type Template struct {
HelmChartName string `json:"helmChartName" tfschema:",omitempty"`
IsGitLab bool `json:"isGitLab" tfschema:"is_gitlab"`
TerragruntTfBinary string `json:"terragruntTfBinary" tfschema:",omitempty"`
TokenName string `json:"tokenName" tfschema:",omitempty"`
}

type TemplateCreatePayload struct {
Expand All @@ -74,7 +75,7 @@ type TemplateCreatePayload struct {
Repository string `json:"repository"`
Path string `json:"path,omitempty"`
IsGitLab bool `json:"isGitLab"`
TokenName string `json:"tokenName"`
TokenName string `json:"tokenName,omitempty"`
TokenId string `json:"tokenId,omitempty"`
GithubInstallationId int `json:"githubInstallationId,omitempty"`
GitlabProjectId int `json:"gitlabProjectId,omitempty"`
Expand Down
27 changes: 14 additions & 13 deletions docs/data-sources/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,18 @@ data "env0_template" "example" {

### Optional

- `bitbucket_client_key` (String) the bitbucket client key used for integration
- `github_installation_id` (Number) The env0 application installation id on the relevant github repository
- `id` (String) id of the template
- `is_azure_devops` (Boolean) true if this template integrates with azure dev ops
- `is_bitbucket_server` (Boolean) true if this template uses bitbucket server repository
- `is_github_enterprise` (Boolean) true if this template uses github enterprise repository
- `is_gitlab_enterprise` (Boolean) Does this template use gitlab enterprise repository?
- `name` (String) the name of the template
- `ssh_keys` (Block List) an array of references to 'data_ssh_key' to use when accessing git over ssh (see [below for nested schema](#nestedblock--ssh_keys))
- `terragrunt_version` (String) terragrunt version to use
- `token_id` (String) The token id used for private git repos or for integration with GitLab

### Read-Only

- `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
- `is_azure_devops` (Boolean) true if this template integrates with azure dev ops
- `is_bitbucket_server` (Boolean) true if this template uses bitbucket server repository
- `is_github_enterprise` (Boolean) true if this template uses github enterprise repository
- `is_gitlab_enterprise` (Boolean) Does this template use gitlab enterprise repository?
- `path` (String) terraform / terrgrunt folder inside source code repository
- `project_ids` (List of String) which projects may access this template (id of project)
- `repository` (String) template source code repository url
Expand All @@ -46,13 +43,17 @@ data "env0_template" "example" {
- `retry_on_deploy_only_when_matches_regex` (String) if specified, will only retry (on deploy) if error matches specified regex
- `retry_on_destroy_only_when_matches_regex` (String) if specified, will only retry (on destroy) if error matches specified regex
- `revision` (String) source code revision (branch / tag) to use
- `ssh_keys` (List of Object) an array of references to 'data_ssh_key' to use when accessing git over ssh (see [below for nested schema](#nestedatt--ssh_keys))
- `terraform_version` (String) terraform version to use
- `terragrunt_version` (String) terragrunt version to use
- `token_id` (String) The token id used for private git repos or for integration with GitLab
- `token_name` (String) the token name used for integration with GitLab
- `type` (String) template type (allowed values: terraform, terragrunt, pulumi, k8s, workflow, cloudformation, helm, opentofu)

<a id="nestedblock--ssh_keys"></a>
<a id="nestedatt--ssh_keys"></a>
### Nested Schema for `ssh_keys`

Required:
Read-Only:

- `id` (String) ssh key id
- `name` (String) ssh key name
- `id` (String)
- `name` (String)
3 changes: 2 additions & 1 deletion docs/resources/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Important note: the template must first be assigned to the same project as the e
Important note: After the environment is created, this field cannot be modified.
- `terragrunt_working_directory` (String) The working directory path to be used by a Terragrunt template. If left empty '/' is used. Note: modifying this field destroys the current environment and creates a new one
- `ttl` (String) the date the environment should be destroyed at (iso format). omitting this attribute will result in infinite ttl.
- `variable_sets` (List of String) a list of variable set to assign to this environment. Note: must not be used with 'env0_variable_set_assignment'
- `variable_sets` (List of String) a list of IDs of variable sets to assign to this environment. Note: must not be used with 'env0_variable_set_assignment'
- `vcs_commands_alias` (String) set an alias for this environment in favor of running VCS commands using PR comments against it. Additional details: https://docs.env0.com/docs/plan-and-apply-from-pr-comments
- `vcs_pr_comments_enabled` (Boolean) set to 'true' to enable running VCS PR plan/apply commands using PR comments. This can be set to 'true' (enabled) without setting alias in 'vcs_commands_alias'. Additional details: https://docs.env0.com/docs/plan-and-apply-from-pr-comments#configuration
- `without_template_settings` (Block List, Max: 1) settings for creating an environment without a template (see [below for nested schema](#nestedblock--without_template_settings))
Expand Down Expand Up @@ -188,6 +188,7 @@ Optional:
- `terragrunt_tf_binary` (String) the binary to use if the template type is 'terragrunt'. Valid values 'opentofu' and 'terraform'. For new templates defaults to 'opentofu'
- `terragrunt_version` (String) the Terragrunt version to use (example: 0.36.5)
- `token_id` (String) the git token id to be used
- `token_name` (String) token name for Gitlab
- `type` (String) template type (allowed values: terraform, terragrunt, pulumi, k8s, workflow, cloudformation, helm, opentofu)

Read-Only:
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ resource "env0_module" "example" {
### Required

- `module_name` (String) name of the module (Match pattern: ^[0-9A-Za-z](?:[0-9A-Za-z-_]{0,62}[0-9A-Za-z])?$)
- `module_provider` (String) the provider name in the module source (Match pattern: ^[0-9a-z]{0,64}$)
- `repository` (String) the repository containing the module files

### Optional
Expand All @@ -36,7 +37,6 @@ resource "env0_module" "example" {
- `description` (String) description of the module
- `github_installation_id` (Number) the env0 application installation id on the relevant Github repository
- `is_azure_devops` (Boolean) true if this module integrates with azure dev ops
- `module_provider` (String) the provider name in the module source (Match pattern: ^[0-9a-z]{0,64}$)
- `module_test_enabled` (Boolean) set to 'true' to enable module test (defaults to 'false')
- `opentofu_version` (String) the opentofu version to use, Can only be set if 'module_test_enabled' is enabled
- `path` (String) the folder in the repository to create the module from
Expand Down
1 change: 1 addition & 0 deletions docs/resources/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ resource "env0_template_project_assignment" "assignment" {
- `terragrunt_tf_binary` (String) the binary to use if the template type is 'terragrunt'. Valid values 'opentofu' and 'terraform'. For new templates defaults to 'opentofu'
- `terragrunt_version` (String) the Terragrunt version to use (example: 0.36.5)
- `token_id` (String) the git token id to be used
- `token_name` (String) token name for Gitlab
- `type` (String) template type (allowed values: terraform, terragrunt, pulumi, k8s, workflow, cloudformation, helm, opentofu)

### Read-Only
Expand Down
20 changes: 9 additions & 11 deletions env0/data_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ func dataTemplate() *schema.Resource {
"github_installation_id": {
Type: schema.TypeInt,
Description: "The env0 application installation id on the relevant github repository",
Optional: true,
Computed: true,
},
"token_id": {
Type: schema.TypeString,
Description: "The token id used for private git repos or for integration with GitLab",
Optional: true,
Computed: true,
},
"terraform_version": {
Type: schema.TypeString,
Expand All @@ -100,58 +100,56 @@ func dataTemplate() *schema.Resource {
Type: schema.TypeString,
Description: "terragrunt version to use",
Computed: true,
Optional: true,
},
"is_gitlab_enterprise": {
Type: schema.TypeBool,
Description: "Does this template use gitlab enterprise repository?",
Optional: true,
Computed: true,
},
"bitbucket_client_key": {
Type: schema.TypeString,
Description: "the bitbucket client key used for integration",
Optional: true,
Computed: true,
},
"is_bitbucket_server": {
Type: schema.TypeBool,
Description: "true if this template uses bitbucket server repository",
Optional: true,
Computed: true,
},
"is_github_enterprise": {
Type: schema.TypeBool,
Description: "true if this template uses github enterprise repository",
Optional: true,
Computed: true,
},
"is_azure_devops": {
Type: schema.TypeBool,
Description: "true if this template integrates with azure dev ops",
Optional: true,
Computed: true,
},
"ssh_keys": {
Type: schema.TypeList,
Description: "an array of references to 'data_ssh_key' to use when accessing git over ssh",
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: "ssh key id",
Required: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Description: "ssh key name",
Required: true,
Computed: true,
},
},
},
},
"token_name": {
Type: schema.TypeString,
Description: "the token name used for integration with GitLab",
Computed: true,
},
},
}
}
Expand Down
2 changes: 2 additions & 0 deletions env0/data_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func TestUnitTemplateData(t *testing.T) {
},
IsBitbucketServer: true,
IsAzureDevOps: true,
TokenName: "tokenname",
}

getValidTestCase := func(input map[string]interface{}) resource.TestCase {
Expand Down Expand Up @@ -69,6 +70,7 @@ func TestUnitTemplateData(t *testing.T) {
resource.TestCheckResourceAttr(resourceFullName, "ssh_keys.0.name", template.SshKeys[0].Name),
resource.TestCheckResourceAttr(resourceFullName, "is_bitbucket_server", "true"),
resource.TestCheckResourceAttr(resourceFullName, "is_azure_devops", "true"),
resource.TestCheckResourceAttr(resourceFullName, "token_name", template.TokenName),
),
},
},
Expand Down
56 changes: 47 additions & 9 deletions env0/resource_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ func resourceEnvironment() *schema.Resource {
},
"variable_sets": {
Type: schema.TypeList,
Description: "a list of variable set to assign to this environment. Note: must not be used with 'env0_variable_set_assignment'",
Description: "a list of IDs of variable sets to assign to this environment. Note: must not be used with 'env0_variable_set_assignment'",
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -446,7 +446,34 @@ func setEnvironmentSchema(ctx context.Context, d *schema.ResourceData, environme
setEnvironmentConfigurationSchema(ctx, d, configurationVariables)

if d.Get("variable_sets") != nil {
if err := d.Set("variable_sets", variableSetsIds); err != nil {
// To avoid drifts keep the schema order as much as possible.
variableSetsFromSchema := getEnvironmentVariableSetIdsFromSchema(d)
sortedVariablesSet := []string{}

for _, schemav := range variableSetsFromSchema {
for _, newv := range variableSetsIds {
if schemav == newv {
sortedVariablesSet = append(sortedVariablesSet, schemav)
break
}
}
}

for _, newv := range variableSetsIds {
found := false
for _, sortedv := range sortedVariablesSet {
if newv == sortedv {
found = true
break
}
}

if !found {
sortedVariablesSet = append(sortedVariablesSet, newv)
}
}

if err := d.Set("variable_sets", sortedVariablesSet); err != nil {
return fmt.Errorf("failed to set variable_sets value: %w", err)
}
}
Expand Down Expand Up @@ -732,6 +759,12 @@ func shouldUpdateTemplate(d *schema.ResourceData) bool {
}

func shouldDeploy(d *schema.ResourceData) bool {
if _, ok := d.GetOk("without_template_settings.0"); ok {
if d.HasChange("without_template_settings.0.revision") {
return true
}
}

return d.HasChanges("revision", "configuration", "sub_environment_configuration", "variable_sets")
}

Expand Down Expand Up @@ -1126,8 +1159,7 @@ func getDeployPayload(d *schema.ResourceData, apiClient client.ApiClientInterfac
var err error

if isTemplateless(d) {
templateId, ok := d.GetOk("without_template_settings.0.id")
if ok {
if templateId, ok := d.GetOk("without_template_settings.0.id"); ok {
payload.BlueprintId = templateId.(string)
}
} else {
Expand All @@ -1139,6 +1171,10 @@ func getDeployPayload(d *schema.ResourceData, apiClient client.ApiClientInterfac
}

if isRedeploy {
if revision, ok := d.GetOk("without_template_settings.0.revision"); ok {
payload.BlueprintRevision = revision.(string)
}

if configuration, ok := d.GetOk("configuration"); ok && isRedeploy {
configurationChanges := getConfigurationVariablesFromSchema(configuration.([]interface{}))
scope := client.ScopeEnvironment
Expand Down Expand Up @@ -1340,6 +1376,7 @@ func getEnvironmentById(environmentId string, meta interface{}) (client.Environm
func resourceEnvironmentImporter(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
id := d.Id()
var getErr diag.Diagnostics

var environment client.Environment
_, err := uuid.Parse(id)
if err == nil {
Expand All @@ -1350,6 +1387,11 @@ func resourceEnvironmentImporter(ctx context.Context, d *schema.ResourceData, me

environment, getErr = getEnvironmentByName(meta, id, "", false)
}

if getErr != nil {
return nil, errors.New(getErr[0].Summary)
}

apiClient := meta.(client.ApiClientInterface)
d.SetId(environment.Id)

Expand Down Expand Up @@ -1406,9 +1448,5 @@ func resourceEnvironmentImporter(ctx context.Context, d *schema.ResourceData, me

d.Set("vcs_pr_comments_enabled", environment.VcsCommandsAlias != "" || environment.VcsPrCommentsEnabled)

if getErr != nil {
return nil, errors.New(getErr[0].Summary)
} else {
return []*schema.ResourceData{d}, nil
}
return []*schema.ResourceData{d}, nil
}
Loading

0 comments on commit 98d5bb5

Please sign in to comment.