From 81355dfeae66d8ffb5ac78a89bcf5daad9893507 Mon Sep 17 00:00:00 2001 From: Barnaby Shearer Date: Thu, 21 Apr 2022 12:30:56 +0100 Subject: [PATCH 1/2] Support per-branch environment variables Fixes #13 Straightforward change to expose the API field. --- client/project_create.go | 11 +++++---- docs/resources/project.md | 1 + vercel/resource_project.go | 6 +++++ vercel/resource_project_model.go | 40 ++++++++++++++++++-------------- 4 files changed, 35 insertions(+), 23 deletions(-) diff --git a/client/project_create.go b/client/project_create.go index 527210f4..ed86914f 100644 --- a/client/project_create.go +++ b/client/project_create.go @@ -19,11 +19,12 @@ type GitRepository struct { // EnvironmentVariable defines the information Vercel requires and surfaces about an environment variable // that is associated with a project. type EnvironmentVariable struct { - Key string `json:"key"` - Value string `json:"value"` - Target []string `json:"target"` - Type string `json:"type"` - ID string `json:"id,omitempty"` + Key string `json:"key"` + Value string `json:"value"` + Target []string `json:"target"` + GitBranch *string `json:"gitBranch,omitempty"` + Type string `json:"type"` + ID string `json:"id,omitempty"` } // CreateProjectRequest defines the information necessary to create a project. diff --git a/docs/resources/project.md b/docs/resources/project.md index 9b97664c..9920b38b 100644 --- a/docs/resources/project.md +++ b/docs/resources/project.md @@ -93,6 +93,7 @@ resource "vercel_project" "example" { Optional: +- **git_branch** (String) The git branch of the environment variable. - **id** (String) The ID of the environment variable - **key** (String) The name of the environment variable. - **target** (Set of String) The environments that the environment variable should be present on. Valid targets are either `production`, `preview`, or `development`. diff --git a/vercel/resource_project.go b/vercel/resource_project.go index 57364e45..8e7626af 100644 --- a/vercel/resource_project.go +++ b/vercel/resource_project.go @@ -73,6 +73,11 @@ terraform to your Deployment. }, Required: true, }, + "git_branch": { + Description: "The git branch of the environment variable.", + Type: types.StringType, + Optional: true, + }, "key": { Description: "The name of the environment variable.", Type: types.StringType, @@ -247,6 +252,7 @@ func containsEnvVar(env []EnvironmentItem, v EnvironmentItem) bool { for _, e := range env { if e.Key == v.Key && e.Value == v.Value && + e.GitBranch == v.GitBranch && len(e.Target) == len(v.Target) { for i, t := range e.Target { if t != v.Target[i] { diff --git a/vercel/resource_project_model.go b/vercel/resource_project_model.go index 2c30ea5e..495fbf20 100644 --- a/vercel/resource_project_model.go +++ b/vercel/resource_project_model.go @@ -30,11 +30,12 @@ func parseEnvironment(vars []EnvironmentItem) []client.EnvironmentVariable { } out = append(out, client.EnvironmentVariable{ - Key: e.Key.Value, - Value: e.Value.Value, - Target: target, - Type: "encrypted", - ID: e.ID.Value, + Key: e.Key.Value, + Value: e.Value.Value, + Target: target, + GitBranch: toStrPointer(e.GitBranch), + Type: "encrypted", + ID: e.ID.Value, }) } return out @@ -74,10 +75,11 @@ func (p *Project) toUpdateProjectRequest(oldName string) client.UpdateProjectReq // EnvironmentItem reflects the state terraform stores internally for a project's environment variable. type EnvironmentItem struct { - Target []types.String `tfsdk:"target"` - Key types.String `tfsdk:"key"` - Value types.String `tfsdk:"value"` - ID types.String `tfsdk:"id"` + Target []types.String `tfsdk:"target"` + GitBranch types.String `tfsdk:"git_branch"` + Key types.String `tfsdk:"key"` + Value types.String `tfsdk:"value"` + ID types.String `tfsdk:"id"` } func (e *EnvironmentItem) toUpsertEnvironmentVariableRequest() client.UpsertEnvironmentVariableRequest { @@ -86,11 +88,12 @@ func (e *EnvironmentItem) toUpsertEnvironmentVariableRequest() client.UpsertEnvi target = append(target, t.Value) } return client.UpsertEnvironmentVariableRequest{ - Key: e.Key.Value, - Value: e.Value.Value, - Target: target, - Type: "encrypted", - ID: e.ID.Value, + Key: e.Key.Value, + Value: e.Value.Value, + Target: target, + GitBranch: toStrPointer(e.GitBranch), + Type: "encrypted", + ID: e.ID.Value, } } @@ -125,10 +128,11 @@ func convertResponseToProject(response client.ProjectResponse, tid types.String) target = append(target, types.String{Value: t}) } env = append(env, EnvironmentItem{ - Key: types.String{Value: e.Key}, - Value: types.String{Value: e.Value}, - Target: target, - ID: types.String{Value: e.ID}, + Key: types.String{Value: e.Key}, + Value: types.String{Value: e.Value}, + Target: target, + GitBranch: fromStringPointer(e.GitBranch), + ID: types.String{Value: e.ID}, }) } teamID := types.String{Value: tid.Value} From 192871701e355e817847d1d55a3aaffad454ba42 Mon Sep 17 00:00:00 2001 From: Barnaby Shearer Date: Fri, 22 Apr 2022 09:34:04 +0100 Subject: [PATCH 2/2] tests --- vercel/resource_project_test.go | 44 +++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/vercel/resource_project_test.go b/vercel/resource_project_test.go index 1a2f1b3c..1b436ea7 100644 --- a/vercel/resource_project_test.go +++ b/vercel/resource_project_test.go @@ -61,6 +61,22 @@ func TestAcc_ProjectWithGitRepository(t *testing.T) { testAccProjectExists("vercel_project.test_git", ""), resource.TestCheckResourceAttr("vercel_project.test_git", "git_repository.type", "github"), resource.TestCheckResourceAttr("vercel_project.test_git", "git_repository.repo", "dglsparsons/test"), + resource.TestCheckTypeSetElemNestedAttrs("vercel_project.test_git", "environment.*", map[string]string{ + "key": "foo", + "value": "bar", + "git_branch": "staging", + }), + ), + }, + { + Config: testAccProjectConfigWithGitRepoUpdated(projectSuffix), + Check: resource.ComposeAggregateTestCheckFunc( + testAccProjectExists("vercel_project.test_git", ""), + resource.TestCheckTypeSetElemNestedAttrs("vercel_project.test_git", "environment.*", map[string]string{ + "key": "foo", + "value": "bar2", + "git_branch": "staging", + }), ), }, }, @@ -245,6 +261,34 @@ resource "vercel_project" "test_git" { type = "github" repo = "dglsparsons/test" } + environment = [ + { + key = "foo" + value = "bar" + target = ["preview"] + git_branch = "staging" + } + ] +} + `, projectSuffix) +} + +func testAccProjectConfigWithGitRepoUpdated(projectSuffix string) string { + return fmt.Sprintf(` +resource "vercel_project" "test_git" { + name = "test-acc-two-%s" + git_repository = { + type = "github" + repo = "dglsparsons/test" + } + environment = [ + { + key = "foo" + value = "bar2" + target = ["preview"] + git_branch = "staging" + } + ] } `, projectSuffix) }