Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber committed Mar 12, 2024
1 parent f9fc792 commit 42fd769
Show file tree
Hide file tree
Showing 3 changed files with 711 additions and 15 deletions.
2 changes: 1 addition & 1 deletion client/environment_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type EnvironmentDiscoveryPayload struct {
TerraformVersion string `json:"terraformVersion"`
OpentofuVersion string `json:"opentofuVersion"`
TerragruntVersion string `json:"terragruntVersion"`
TerragruntTfBinary string `json:"terragruntTfBinary"`
TerragruntTfBinary string `json:"terragruntTfBinary" tfschema:",omitempty"`
Type string `json:"type"`
GitlabProjectId int `json:"gitlabProjectId"`
TokenId string `json:"tokenId"`
Expand Down
45 changes: 36 additions & 9 deletions env0/resource_environment_discovery_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"strings"

"github.com/env0/terraform-provider-env0/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -214,15 +215,36 @@ func discoveryValidatePutPayload(putPayload *client.EnvironmentDiscoveryPutPaylo
return fmt.Errorf("unhandled type %s", putPayload.Type)
}

// TODO --- !!!TODO
/*
vcsAttributes := []string{
"github_installation_id",
"bitbucket_client_key",
"gitlab_project_id",
"is_azure_devops",
}
*/
vcsCounter := 0
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
}
Expand All @@ -244,6 +266,11 @@ func resourceEnvironmentDiscoveryConfigurationPut(ctx context.Context, d *schema
return diag.Errorf("validation error: %s", err.Error())
}

if putPayload.Type != "terragrunt" {
// Remove the default terragrunt_tf_binary if terragrunt isn't used.
putPayload.TerragruntTfBinary = ""
}

res, err := apiClient.EnableUpdateEnvironmentDiscovery(d.Get("project_id").(string), &putPayload)
if err != nil {
return diag.Errorf("enable/update environment discovery configuration request failed: %s", err.Error())
Expand Down
Loading

0 comments on commit 42fd769

Please sign in to comment.