Skip to content

Commit

Permalink
Feat: Add Creation Policy for Environment discovery Configuration (#1001
Browse files Browse the repository at this point in the history
)
  • Loading branch information
TomerHeber authored Jan 30, 2025
1 parent 699635a commit e5d5653
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 76 deletions.
76 changes: 39 additions & 37 deletions client/environment_discovery.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,48 @@
package client

type EnvironmentDiscoveryPutPayload struct {
GlobPattern string `json:"globPattern"`
EnvironmentPlacement string `json:"environmentPlacement"`
WorkspaceNaming string `json:"workspaceNaming"`
AutoDeployByCustomGlob string `json:"autoDeployByCustomGlob,omitempty"`
Repository string `json:"repository"`
TerraformVersion string `json:"terraformVersion,omitempty"`
OpentofuVersion string `json:"opentofuVersion,omitempty"`
TerragruntVersion string `json:"terragruntVersion,omitempty"`
TerragruntTfBinary string `json:"terragruntTfBinary,omitempty"`
IsTerragruntRunAll bool `json:"is_terragrunt_run_all"`
Type string `json:"type"`
TokenId string `json:"tokenId,omitempty"`
SshKeys []TemplateSshKey `json:"sshKeys,omitempty"`
GithubInstallationId int `json:"githubInstallationId,omitempty"`
BitbucketClientKey string `json:"bitbucketClientKey,omitempty"`
IsAzureDevops bool `json:"isAzureDevOps"`
Retry TemplateRetry `json:"retry"`
RootPath string `json:"rootPath"`
GlobPattern string `json:"globPattern"`
EnvironmentPlacement string `json:"environmentPlacement"`
WorkspaceNaming string `json:"workspaceNaming"`
AutoDeployByCustomGlob string `json:"autoDeployByCustomGlob,omitempty"`
Repository string `json:"repository"`
TerraformVersion string `json:"terraformVersion,omitempty"`
OpentofuVersion string `json:"opentofuVersion,omitempty"`
TerragruntVersion string `json:"terragruntVersion,omitempty"`
TerragruntTfBinary string `json:"terragruntTfBinary,omitempty"`
IsTerragruntRunAll bool `json:"isTerragruntRunAll"`
Type string `json:"type"`
TokenId string `json:"tokenId,omitempty"`
SshKeys []TemplateSshKey `json:"sshKeys,omitempty"`
GithubInstallationId int `json:"githubInstallationId,omitempty"`
BitbucketClientKey string `json:"bitbucketClientKey,omitempty"`
IsAzureDevops bool `json:"isAzureDevOps"`
Retry TemplateRetry `json:"retry"`
RootPath string `json:"rootPath"`
CreateNewEnvironmentsFromPullRequests bool `json:"createNewEnvironmentsFromPullRequests"`
}

type EnvironmentDiscoveryPayload struct {
Id string `json:"id"`
GlobPattern string `json:"globPattern"`
EnvironmentPlacement string `json:"environmentPlacement"`
WorkspaceNaming string `json:"workspaceNaming"`
AutoDeployByCustomGlob string `json:"autoDeployByCustomGlob"`
Repository string `json:"repository"`
TerraformVersion string `json:"terraformVersion"`
OpentofuVersion string `json:"opentofuVersion"`
TerragruntVersion string `json:"terragruntVersion"`
TerragruntTfBinary string `json:"terragruntTfBinary" tfschema:",omitempty"`
IsTerragruntRunAll bool `json:"is_terragrunt_run_all"`
Type string `json:"type"`
TokenId string `json:"tokenId"`
SshKeys []TemplateSshKey `json:"sshKeys" tfschema:"-"`
GithubInstallationId int `json:"githubInstallationId"`
BitbucketClientKey string `json:"bitbucketClientKey"`
IsAzureDevops bool `json:"isAzureDevOps"`
Retry TemplateRetry `json:"retry" tfschema:"-"`
RootPath string `json:"rootPath"`
Id string `json:"id"`
GlobPattern string `json:"globPattern"`
EnvironmentPlacement string `json:"environmentPlacement"`
WorkspaceNaming string `json:"workspaceNaming"`
AutoDeployByCustomGlob string `json:"autoDeployByCustomGlob"`
Repository string `json:"repository"`
TerraformVersion string `json:"terraformVersion"`
OpentofuVersion string `json:"opentofuVersion"`
TerragruntVersion string `json:"terragruntVersion"`
TerragruntTfBinary string `json:"terragruntTfBinary" tfschema:",omitempty"`
IsTerragruntRunAll bool `json:"isTerragruntRunAll"`
Type string `json:"type"`
TokenId string `json:"tokenId"`
SshKeys []TemplateSshKey `json:"sshKeys" tfschema:"-"`
GithubInstallationId int `json:"githubInstallationId"`
BitbucketClientKey string `json:"bitbucketClientKey"`
IsAzureDevops bool `json:"isAzureDevOps"`
Retry TemplateRetry `json:"retry" tfschema:"-"`
RootPath string `json:"rootPath"`
CreateNewEnvironmentsFromPullRequests bool `json:"createNewEnvironmentsFromPullRequests"`
}

func (client *ApiClient) PutEnvironmentDiscovery(projectId string, payload *EnvironmentDiscoveryPutPayload) (*EnvironmentDiscoveryPayload, error) {
Expand Down
8 changes: 7 additions & 1 deletion env0/resource_environment_discovery_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func resourceEnvironmentDiscoveryConfiguration() *schema.Resource {
"is_terragrunt_run_all": {
Type: schema.TypeBool,
Optional: true,
Description: "If set to 'true', execute terragrunt commands with 'run all'",
Description: "If set to 'true', execute terragrunt commands with 'run all' (default: false)",
Default: false,
},
"ssh_key_id": {
Expand Down Expand Up @@ -164,6 +164,12 @@ func resourceEnvironmentDiscoveryConfiguration() *schema.Resource {
Description: "start files Glob matching from this folder only",
Optional: true,
},
"create_new_environments_from_pull_requests": {
Type: schema.TypeBool,
Description: "create new environments from pull requests (default: false)",
Optional: true,
Default: false,
},
},
}
}
Expand Down
90 changes: 52 additions & 38 deletions env0/resource_environment_discovery_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,53 +22,60 @@ func TestUnitEnvironmentDiscoveryConfigurationResource(t *testing.T) {

t.Run("default (opentofu and terragrunt)", func(t *testing.T) {
putPayload := client.EnvironmentDiscoveryPutPayload{
GlobPattern: "**",
Repository: "https://re.po",
Type: "opentofu",
EnvironmentPlacement: "topProject",
WorkspaceNaming: "default",
OpentofuVersion: "1.6.2",
GithubInstallationId: 12345,
RootPath: "/path",
GlobPattern: "**",
Repository: "https://re.po",
Type: "opentofu",
EnvironmentPlacement: "topProject",
WorkspaceNaming: "default",
OpentofuVersion: "1.6.2",
GithubInstallationId: 12345,
RootPath: "/path",
CreateNewEnvironmentsFromPullRequests: true,
}

getPayload := client.EnvironmentDiscoveryPayload{
Id: id,
GlobPattern: putPayload.GlobPattern,
Repository: putPayload.Repository,
Type: putPayload.Type,
EnvironmentPlacement: putPayload.EnvironmentPlacement,
WorkspaceNaming: putPayload.WorkspaceNaming,
OpentofuVersion: putPayload.OpentofuVersion,
GithubInstallationId: putPayload.GithubInstallationId,
RootPath: putPayload.RootPath,
Id: id,
GlobPattern: putPayload.GlobPattern,
Repository: putPayload.Repository,
Type: putPayload.Type,
EnvironmentPlacement: putPayload.EnvironmentPlacement,
WorkspaceNaming: putPayload.WorkspaceNaming,
OpentofuVersion: putPayload.OpentofuVersion,
GithubInstallationId: putPayload.GithubInstallationId,
RootPath: putPayload.RootPath,
IsTerragruntRunAll: putPayload.IsTerragruntRunAll,
CreateNewEnvironmentsFromPullRequests: putPayload.CreateNewEnvironmentsFromPullRequests,
}

updatePutPayload := client.EnvironmentDiscoveryPutPayload{
GlobPattern: "**/**",
Repository: "https://re.po",
Type: "terragrunt",
EnvironmentPlacement: "topProject",
WorkspaceNaming: "default",
OpentofuVersion: "1.6.3",
TerragruntVersion: "0.63.0",
GithubInstallationId: 3213,
TerragruntTfBinary: "opentofu",
RootPath: "/path2",
GlobPattern: "**/**",
Repository: "https://re.po",
Type: "terragrunt",
EnvironmentPlacement: "topProject",
WorkspaceNaming: "default",
OpentofuVersion: "1.6.3",
TerragruntVersion: "0.63.0",
GithubInstallationId: 3213,
TerragruntTfBinary: "opentofu",
RootPath: "/path2",
CreateNewEnvironmentsFromPullRequests: true,
IsTerragruntRunAll: true,
}

updateGetPayload := client.EnvironmentDiscoveryPayload{
Id: id,
GlobPattern: updatePutPayload.GlobPattern,
Repository: updatePutPayload.Repository,
Type: updatePutPayload.Type,
EnvironmentPlacement: updatePutPayload.EnvironmentPlacement,
WorkspaceNaming: updatePutPayload.WorkspaceNaming,
OpentofuVersion: updatePutPayload.OpentofuVersion,
TerragruntVersion: updatePutPayload.TerragruntVersion,
GithubInstallationId: updatePutPayload.GithubInstallationId,
TerragruntTfBinary: "opentofu",
RootPath: updatePutPayload.RootPath,
Id: id,
GlobPattern: updatePutPayload.GlobPattern,
Repository: updatePutPayload.Repository,
Type: updatePutPayload.Type,
EnvironmentPlacement: updatePutPayload.EnvironmentPlacement,
WorkspaceNaming: updatePutPayload.WorkspaceNaming,
OpentofuVersion: updatePutPayload.OpentofuVersion,
TerragruntVersion: updatePutPayload.TerragruntVersion,
GithubInstallationId: updatePutPayload.GithubInstallationId,
TerragruntTfBinary: "opentofu",
RootPath: updatePutPayload.RootPath,
CreateNewEnvironmentsFromPullRequests: updatePutPayload.CreateNewEnvironmentsFromPullRequests,
IsTerragruntRunAll: updatePutPayload.IsTerragruntRunAll,
}

testCase := resource.TestCase{
Expand All @@ -81,6 +88,7 @@ func TestUnitEnvironmentDiscoveryConfigurationResource(t *testing.T) {
"opentofu_version": putPayload.OpentofuVersion,
"github_installation_id": putPayload.GithubInstallationId,
"root_path": putPayload.RootPath,
"create_new_environments_from_pull_requests": putPayload.CreateNewEnvironmentsFromPullRequests,
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(accessor, "project_id", projectId),
Expand All @@ -92,6 +100,8 @@ func TestUnitEnvironmentDiscoveryConfigurationResource(t *testing.T) {
resource.TestCheckResourceAttr(accessor, "opentofu_version", putPayload.OpentofuVersion),
resource.TestCheckResourceAttr(accessor, "github_installation_id", strconv.Itoa(putPayload.GithubInstallationId)),
resource.TestCheckResourceAttr(accessor, "root_path", putPayload.RootPath),
resource.TestCheckResourceAttr(accessor, "create_new_environments_from_pull_requests", "true"),
resource.TestCheckResourceAttr(accessor, "is_terragrunt_run_all", "false"),
),
},
{
Expand All @@ -104,6 +114,8 @@ func TestUnitEnvironmentDiscoveryConfigurationResource(t *testing.T) {
"type": updatePutPayload.Type,
"terragrunt_version": updatePutPayload.TerragruntVersion,
"root_path": updatePutPayload.RootPath,
"create_new_environments_from_pull_requests": updatePutPayload.CreateNewEnvironmentsFromPullRequests,
"is_terragrunt_run_all": updatePutPayload.IsTerragruntRunAll,
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(accessor, "project_id", projectId),
Expand All @@ -116,6 +128,8 @@ func TestUnitEnvironmentDiscoveryConfigurationResource(t *testing.T) {
resource.TestCheckResourceAttr(accessor, "github_installation_id", strconv.Itoa(updatePutPayload.GithubInstallationId)),
resource.TestCheckResourceAttr(accessor, "terragrunt_version", updatePutPayload.TerragruntVersion),
resource.TestCheckResourceAttr(accessor, "root_path", updatePutPayload.RootPath),
resource.TestCheckResourceAttr(accessor, "create_new_environments_from_pull_requests", "true"),
resource.TestCheckResourceAttr(accessor, "is_terragrunt_run_all", "true"),
),
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ resource "env0_environment_discovery_configuration" "example" {
opentofu_version = "1.6.2"
repository = data.env0_template.github_template.repository
github_installation_id = data.env0_template.github_template.github_installation_id
create_new_environments_from_pull_requests = true

depends_on = [env0_template_project_assignment.assignment]
}

0 comments on commit e5d5653

Please sign in to comment.