这是indexloc提供的服务,不要输入任何密码
Skip to content

Improve production domain validation #213

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

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion vercel/resource_project_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (r *projectDomainResource) Create(ctx context.Context, req resource.CreateR
return
}

_, err := r.client.GetProject(ctx, plan.ProjectID.ValueString(), plan.TeamID.ValueString())
project, err := r.client.GetProject(ctx, plan.ProjectID.ValueString(), plan.TeamID.ValueString())
if client.NotFound(err) {
resp.Diagnostics.AddError(
"Error creating project domain",
Expand All @@ -157,6 +157,19 @@ func (r *projectDomainResource) Create(ctx context.Context, req resource.CreateR
return
}

// Crazy condition to add an error if the git_branch is the production branch.
if plan.GitBranch.ValueString() != "" && project.Link != nil && project.Link.ProductionBranch != nil && *project.Link.ProductionBranch == plan.GitBranch.ValueString() {
resp.Diagnostics.AddError(
"Error adding domain to project",
fmt.Sprintf(
"Could not add domain %s to project %s, the git_branch specified is the production branch. If you want to use this domain as a production domain, please omit the git_branch field.",
plan.Domain.ValueString(),
plan.ProjectID.ValueString(),
),
)
return
}

out, err := r.client.CreateProjectDomain(ctx, plan.ProjectID.ValueString(), plan.TeamID.ValueString(), plan.toCreateRequest())
if err != nil {
resp.Diagnostics.AddError(
Expand Down
29 changes: 29 additions & 0 deletions vercel/resource_project_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package vercel_test
import (
"context"
"fmt"
"regexp"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
Expand All @@ -26,6 +28,13 @@ func TestAcc_ProjectDomain(t *testing.T) {
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
CheckDestroy: noopDestroyCheck,
Steps: []resource.TestStep{
// Check error adding production domain
{
Config: testAccProjectDomainWithProductionGitBranch(projectSuffix, domain, teamIDConfig()),
ExpectError: regexp.MustCompile(
strings.ReplaceAll("the git_branch specified is the production branch. If you want to use this domain as a production domain, please omit the git_branch field.", " ", `\s*`),
),
},
// Create and Read testing
{
Config: testAccProjectDomainConfig(projectSuffix, domain, teamIDConfig()),
Expand Down Expand Up @@ -98,6 +107,26 @@ func testAccProjectDomainDestroy(n, teamID, domain string) resource.TestCheckFun
}
}

func testAccProjectDomainWithProductionGitBranch(projectSuffix, domain, teamIDConfig string) string {
return fmt.Sprintf(`
resource "vercel_project" "test" {
name = "test-acc-domain-%[1]s"
%[2]s
git_repository = {
type = "github"
repo = "%[4]s"
}
}

resource "vercel_project_domain" "test" {
domain = "%[3]s"
%[2]s
git_branch = "main"
project_id = vercel_project.test.id
}
`, projectSuffix, teamIDConfig, domain, testGithubRepo())
}

func testAccProjectDomainConfig(projectSuffix, domain, extra string) string {
return fmt.Sprintf(`
resource "vercel_project" "test" {
Expand Down
Loading