diff --git a/vercel/resource_project_domain.go b/vercel/resource_project_domain.go index d5a7468d..d801c00f 100644 --- a/vercel/resource_project_domain.go +++ b/vercel/resource_project_domain.go @@ -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", @@ -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( diff --git a/vercel/resource_project_domain_test.go b/vercel/resource_project_domain_test.go index 826e353c..e2f9a490 100644 --- a/vercel/resource_project_domain_test.go +++ b/vercel/resource_project_domain_test.go @@ -3,6 +3,8 @@ package vercel_test import ( "context" "fmt" + "regexp" + "strings" "testing" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" @@ -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()), @@ -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" {