-
Notifications
You must be signed in to change notification settings - Fork 14
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: stop requiring gitlab_project_id for gitlab templates #941
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ import ( | |
"context" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/env0/terraform-provider-env0/client" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
|
@@ -42,8 +41,8 @@ func resourceEnvironmentDiscoveryConfiguration() *schema.Resource { | |
"type": { | ||
Type: schema.TypeString, | ||
Description: "the infrastructure type use. Valid values: 'opentofu', 'terraform', 'terragrunt', 'workflow' (default: 'opentofu')", | ||
Default: "opentofu", | ||
ValidateDiagFunc: NewStringInValidator([]string{"opentofu", "terraform", "terragrunt", "workflow"}), | ||
Default: client.OPENTOFU, | ||
ValidateDiagFunc: NewStringInValidator([]string{client.OPENTOFU, client.TERRAFORM, client.TERRAGRUNT, client.WORKFLOW}), | ||
Optional: true, | ||
}, | ||
"environment_placement": { | ||
|
@@ -87,8 +86,8 @@ func resourceEnvironmentDiscoveryConfiguration() *schema.Resource { | |
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "The binary to use with Terragrunt. Valid values: 'opentofu' and 'terraform' (default: 'opentofu')", | ||
ValidateDiagFunc: NewStringInValidator([]string{"opentofu", "terraform"}), | ||
Default: "opentofu", | ||
ValidateDiagFunc: NewStringInValidator([]string{client.OPENTOFU, client.TERRAFORM}), | ||
Default: client.OPENTOFU, | ||
}, | ||
"is_terragrunt_run_all": { | ||
Type: schema.TypeBool, | ||
|
@@ -143,10 +142,10 @@ func resourceEnvironmentDiscoveryConfiguration() *schema.Resource { | |
Optional: true, | ||
}, | ||
"gitlab_project_id": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same |
||
Type: schema.TypeInt, | ||
Description: "gitlab project id", | ||
Optional: true, | ||
RequiredWith: []string{"token_id"}, | ||
Type: schema.TypeInt, | ||
Description: "gitlab project id (deprecated)", | ||
Optional: true, | ||
Deprecated: "'repository' is used instead", | ||
}, | ||
"is_azure_devops": { | ||
Type: schema.TypeBool, | ||
|
@@ -197,62 +196,31 @@ func discoveryValidatePutPayload(putPayload *client.EnvironmentDiscoveryPutPaylo | |
terragruntVersionSet := putPayload.TerragruntVersion != "" | ||
|
||
switch putPayload.Type { | ||
case "opentofu": | ||
case client.OPENTOFU: | ||
if !opentofuVersionSet { | ||
return errors.New("'opentofu_version' not set") | ||
} | ||
case "terraform": | ||
case client.TERRAFORM: | ||
if !terraformVersionSet { | ||
return errors.New("'terraform_version' not set") | ||
} | ||
case "terragrunt": | ||
case client.TERRAGRUNT: | ||
if !terragruntVersionSet { | ||
return errors.New("'terragrunt_version' not set") | ||
} | ||
|
||
if putPayload.TerragruntTfBinary == "opentofu" && !opentofuVersionSet { | ||
if putPayload.TerragruntTfBinary == client.OPENTOFU && !opentofuVersionSet { | ||
return errors.New("'terragrunt_tf_binary' is set to 'opentofu', but 'opentofu_version' not set") | ||
} | ||
|
||
if putPayload.TerragruntTfBinary == "terraform" && !terraformVersionSet { | ||
if putPayload.TerragruntTfBinary == client.TERRAFORM && !terraformVersionSet { | ||
return errors.New("'terragrunt_tf_binary' is set to 'terraform', but 'terraform_version' not set") | ||
} | ||
case "workflow": | ||
case client.WORKFLOW: | ||
default: | ||
return fmt.Errorf("unhandled type %s", putPayload.Type) | ||
} | ||
|
||
vcsCounter := 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed this because this may cause some issues. Not clear how to resolve. |
||
vcsEnabledAttributes := []string{} | ||
|
||
if putPayload.GithubInstallationId != 0 { | ||
vcsCounter++ | ||
vcsEnabledAttributes = append(vcsEnabledAttributes, "github_installation_id") | ||
} | ||
|
||
if putPayload.BitbucketClientKey != "" { | ||
vcsCounter++ | ||
vcsEnabledAttributes = append(vcsEnabledAttributes, "bitbucket_client_key") | ||
} | ||
|
||
if putPayload.GitlabProjectId != 0 { | ||
vcsCounter++ | ||
vcsEnabledAttributes = append(vcsEnabledAttributes, "gitlab_project_id") | ||
} | ||
|
||
if putPayload.IsAzureDevops { | ||
vcsCounter++ | ||
vcsEnabledAttributes = append(vcsEnabledAttributes, "is_azure_devops") | ||
} | ||
|
||
if vcsCounter == 0 { | ||
return errors.New("must set exactly one vcs, none were configured: github_installation_id, bitbucket_client_key, gitlab_project_id, or is_azure_devops") | ||
} | ||
|
||
if vcsCounter > 1 { | ||
return fmt.Errorf("must set exactly one vcs, but more were configured: %s", strings.Join(vcsEnabledAttributes, ", ")) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
|
@@ -273,7 +241,7 @@ func resourceEnvironmentDiscoveryConfigurationPut(ctx context.Context, d *schema | |
return diag.Errorf("validation error: %s", err.Error()) | ||
} | ||
|
||
if putPayload.Type != "terragrunt" { | ||
if putPayload.Type != client.TERRAGRUNT { | ||
// Remove the default terragrunt_tf_binary if terragrunt isn't used. | ||
putPayload.TerragruntTfBinary = "" | ||
} | ||
|
@@ -347,7 +315,7 @@ func resourceEnvironmentDiscoveryConfigurationImport(ctx context.Context, d *sch | |
d.Set("project_id", projectId) | ||
|
||
if _, ok := d.GetOk("terragrunt_tf_binary"); !ok { | ||
d.Set("terragrunt_tf_binary", "opentofu") | ||
d.Set("terragrunt_tf_binary", client.OPENTOFU) | ||
} | ||
|
||
return []*schema.ResourceData{d}, nil | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -312,7 +312,6 @@ func TestUnitEnvironmentDiscoveryConfigurationResource(t *testing.T) { | |
TerraformVersion: "1.7.8", | ||
EnvironmentPlacement: "topProject", | ||
WorkspaceNaming: "default", | ||
GitlabProjectId: 12345, | ||
TokenId: "abcdefg", | ||
} | ||
|
||
|
@@ -324,7 +323,6 @@ func TestUnitEnvironmentDiscoveryConfigurationResource(t *testing.T) { | |
TerraformVersion: putPayload.TerraformVersion, | ||
EnvironmentPlacement: putPayload.EnvironmentPlacement, | ||
WorkspaceNaming: putPayload.WorkspaceNaming, | ||
GitlabProjectId: putPayload.GitlabProjectId, | ||
TokenId: putPayload.TokenId, | ||
} | ||
|
||
|
@@ -336,7 +334,6 @@ func TestUnitEnvironmentDiscoveryConfigurationResource(t *testing.T) { | |
"type": putPayload.Type, | ||
"glob_pattern": putPayload.GlobPattern, | ||
"repository": putPayload.Repository, | ||
"gitlab_project_id": putPayload.GitlabProjectId, | ||
"token_id": putPayload.TokenId, | ||
"terraform_version": putPayload.TerraformVersion, | ||
}), | ||
|
@@ -347,7 +344,6 @@ func TestUnitEnvironmentDiscoveryConfigurationResource(t *testing.T) { | |
resource.TestCheckResourceAttr(accessor, "type", putPayload.Type), | ||
resource.TestCheckResourceAttr(accessor, "environment_placement", putPayload.EnvironmentPlacement), | ||
resource.TestCheckResourceAttr(accessor, "workspace_naming", putPayload.WorkspaceNaming), | ||
resource.TestCheckResourceAttr(accessor, "gitlab_project_id", strconv.Itoa(putPayload.GitlabProjectId)), | ||
resource.TestCheckResourceAttr(accessor, "token_id", putPayload.TokenId), | ||
resource.TestCheckResourceAttr(accessor, "terraform_version", putPayload.TerraformVersion), | ||
), | ||
|
@@ -711,45 +707,6 @@ func TestUnitEnvironmentDiscoveryConfigurationResource(t *testing.T) { | |
runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {}) | ||
}) | ||
|
||
t.Run("error: no vcs set", func(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed relevant tests. |
||
testCase := resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{ | ||
"project_id": projectId, | ||
"glob_pattern": "**", | ||
"repository": "https://re.po", | ||
"type": "workflow", | ||
}), | ||
ExpectError: regexp.MustCompile("must set exactly one vcs, none were configured"), | ||
}, | ||
}, | ||
} | ||
|
||
runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {}) | ||
}) | ||
|
||
t.Run("error: more than one vcs set", func(t *testing.T) { | ||
testCase := resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{ | ||
"project_id": projectId, | ||
"glob_pattern": "**", | ||
"repository": "https://re.po", | ||
"type": "workflow", | ||
"github_installation_id": 1234, | ||
"gitlab_project_id": 5678, | ||
"token_id": "1345", | ||
}), | ||
ExpectError: regexp.MustCompile("must set exactly one vcs, but more were configured: github_installation_id, gitlab_project_id"), | ||
}, | ||
}, | ||
} | ||
|
||
runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {}) | ||
}) | ||
|
||
t.Run("import", func(t *testing.T) { | ||
putPayload := client.EnvironmentDiscoveryPutPayload{ | ||
GlobPattern: "**", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,6 @@ import ( | |
func getTemplateSchema(prefix string) map[string]*schema.Schema { | ||
var allVCSAttributes = []string{ | ||
"token_id", | ||
"gitlab_project_id", | ||
"github_installation_id", | ||
"bitbucket_client_key", | ||
"is_gitlab_enterprise", | ||
|
@@ -29,14 +28,14 @@ func getTemplateSchema(prefix string) map[string]*schema.Schema { | |
} | ||
|
||
var allowedTemplateTypes = []string{ | ||
"terraform", | ||
"terragrunt", | ||
client.TERRAFORM, | ||
client.TERRAGRUNT, | ||
"pulumi", | ||
"k8s", | ||
"workflow", | ||
client.WORKFLOW, | ||
"cloudformation", | ||
"helm", | ||
"opentofu", | ||
client.OPENTOFU, | ||
"ansible", | ||
} | ||
|
||
|
@@ -56,6 +55,7 @@ func getTemplateSchema(prefix string) map[string]*schema.Schema { | |
if prefix != "" { | ||
attr = prefix + attr | ||
} | ||
|
||
butAttrs = append(butAttrs, attr) | ||
} | ||
} | ||
|
@@ -70,6 +70,7 @@ func getTemplateSchema(prefix string) map[string]*schema.Schema { | |
if prefix != "" { | ||
str = prefix + str | ||
} | ||
|
||
ret = append(ret, str) | ||
} | ||
|
||
|
@@ -101,7 +102,7 @@ func getTemplateSchema(prefix string) map[string]*schema.Schema { | |
Type: schema.TypeString, | ||
Description: fmt.Sprintf("template type (allowed values: %s)", strings.Join(allowedTemplateTypes, ", ")), | ||
Optional: true, | ||
Default: "terraform", | ||
Default: client.TERRAFORM, | ||
ValidateDiagFunc: NewStringInValidator(allowedTemplateTypes), | ||
}, | ||
"revision": { | ||
|
@@ -152,14 +153,13 @@ func getTemplateSchema(prefix string) map[string]*schema.Schema { | |
Type: schema.TypeString, | ||
Description: "the git token id to be used", | ||
Optional: true, | ||
ConflictsWith: allVCSAttributesBut("token_id", "gitlab_project_id", "is_azure_devops", "path"), | ||
ConflictsWith: allVCSAttributesBut("token_id", "is_azure_devops", "path"), | ||
}, | ||
"gitlab_project_id": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same |
||
Type: schema.TypeInt, | ||
Description: "the project id of the relevant repository", | ||
Optional: true, | ||
ConflictsWith: allVCSAttributesBut("token_id", "gitlab_project_id", "path"), | ||
RequiredWith: requiredWith("token_id"), | ||
Type: schema.TypeInt, | ||
Deprecated: "'repository' is used instead", | ||
Description: "the project id of the relevant repository (deprecated)", | ||
Optional: true, | ||
}, | ||
"terraform_version": { | ||
Type: schema.TypeString, | ||
|
@@ -214,7 +214,7 @@ func getTemplateSchema(prefix string) map[string]*schema.Schema { | |
"is_terragrunt_run_all": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Description: `true if this template should execute run-all commands on multiple modules (check https://terragrunt.gruntwork.io/docs/features/execute-terraform-commands-on-multiple-modules-at-once/#the-run-all-command for additional details). Can only be true with "terragrunt" template type and terragrunt version 0.28.1 and above`, | ||
Description: "true if this template should execute run-all commands on multiple modules (check https://terragrunt.gruntwork.io/docs/features/execute-terraform-commands-on-multiple-modules-at-once/#the-run-all-command for additional details). Can only be true with 'terragrunt' template type and terragrunt version 0.28.1 and above", | ||
Default: "false", | ||
}, | ||
"is_azure_devops": { | ||
|
@@ -243,7 +243,7 @@ func getTemplateSchema(prefix string) map[string]*schema.Schema { | |
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "the binary to use if the template type is 'terragrunt'. Valid values 'opentofu' and 'terraform'. For new templates defaults to 'opentofu'", | ||
ValidateDiagFunc: NewStringInValidator([]string{"opentofu", "terraform"}), | ||
ValidateDiagFunc: NewStringInValidator([]string{client.OPENTOFU, client.TERRAFORM}), | ||
}, | ||
"token_name": { | ||
Type: schema.TypeString, | ||
|
@@ -411,16 +411,16 @@ func templateCreatePayloadFromParameters(prefix string, d *schema.ResourceData) | |
// If the user has set a value - use it. | ||
if terragruntTfBinary := d.Get(terragruntTfBinaryKey).(string); terragruntTfBinary != "" { | ||
payload.TerragruntTfBinary = terragruntTfBinary | ||
} else if templateType.(string) == "terragrunt" && isNew { | ||
payload.TerragruntTfBinary = "opentofu" | ||
} else if templateType.(string) == client.TERRAGRUNT && isNew { | ||
payload.TerragruntTfBinary = client.OPENTOFU | ||
} | ||
} | ||
|
||
templateCreatePayloadRetryOnHelper(prefix, d, "deploy", &payload.Retry.OnDeploy) | ||
templateCreatePayloadRetryOnHelper(prefix, d, "destroy", &payload.Retry.OnDestroy) | ||
|
||
if err := payload.Invalidate(); err != nil { | ||
return payload, diag.Errorf(err.Error()) | ||
return payload, diag.FromErr(err) | ||
} | ||
|
||
return payload, nil | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Text should be changed
We should use "project id is now auto-fetched from the repository URL"