Skip to content

Commit

Permalink
Allow 'unlimited' deployment retentions to be imported / updated to (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dglsparsons authored Oct 23, 2024
1 parent efd62f4 commit cb24399
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 54 deletions.
7 changes: 6 additions & 1 deletion client/project_deployment_retention.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,12 @@ func (c *Client) GetDeploymentRetention(ctx context.Context, projectID, teamID s
body: "",
}, &p)
if p.DeploymentExpiration == nil {
return DeploymentExpiration{}, fmt.Errorf("deployment retention not found")
return DeploymentExpiration{
ExpirationPreview: 36500,
ExpirationProduction: 36500,
ExpirationCanceled: 36500,
ExpirationErrored: 36500,
}, nil
}
return *p.DeploymentExpiration, err
}
4 changes: 0 additions & 4 deletions vercel/data_source_project_deployment_retention.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,6 @@ func (r *projectDeploymentRetentionDataSource) Read(ctx context.Context, req dat
}

out, err := r.client.GetDeploymentRetention(ctx, config.ProjectID.ValueString(), config.TeamID.ValueString())
if client.NotFound(err) {
resp.State.RemoveResource(ctx)
return
}
if err != nil {
resp.Diagnostics.AddError(
"Error reading project deployment retention",
Expand Down
28 changes: 24 additions & 4 deletions vercel/data_source_project_deployment_retention_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ func TestAcc_ProjectDeploymentRetentionDataSource(t *testing.T) {
Config: testAccProjectDeploymentRetentionDataSourceConfig(nameSuffix),
Check: resource.ComposeAggregateTestCheckFunc(
testAccProjectDeploymentRetentionExists("vercel_project_deployment_retention.example", testTeam()),
resource.TestCheckResourceAttr("vercel_project_deployment_retention.example", "expiration_preview", "1m"),
resource.TestCheckResourceAttr("vercel_project_deployment_retention.example", "expiration_production", "unlimited"),
resource.TestCheckResourceAttr("vercel_project_deployment_retention.example", "expiration_canceled", "unlimited"),
resource.TestCheckResourceAttr("vercel_project_deployment_retention.example", "expiration_errored", "unlimited"),
resource.TestCheckResourceAttr("data.vercel_project_deployment_retention.example", "expiration_preview", "1m"),
resource.TestCheckResourceAttr("data.vercel_project_deployment_retention.example", "expiration_production", "unlimited"),
resource.TestCheckResourceAttr("data.vercel_project_deployment_retention.example", "expiration_canceled", "unlimited"),
resource.TestCheckResourceAttr("data.vercel_project_deployment_retention.example", "expiration_errored", "unlimited"),

resource.TestCheckResourceAttr("data.vercel_project_deployment_retention.example_2", "expiration_preview", "unlimited"),
resource.TestCheckResourceAttr("data.vercel_project_deployment_retention.example_2", "expiration_production", "unlimited"),
resource.TestCheckResourceAttr("data.vercel_project_deployment_retention.example_2", "expiration_canceled", "unlimited"),
resource.TestCheckResourceAttr("data.vercel_project_deployment_retention.example_2", "expiration_errored", "unlimited"),
),
},
},
Expand Down Expand Up @@ -53,5 +58,20 @@ data "vercel_project_deployment_retention" "example" {
project_id = vercel_project_deployment_retention.example.project_id
%[3]s
}
resource "vercel_project" "example_2" {
name = "test-acc-example-project-2-%[1]s"
%[3]s
git_repository = {
type = "github"
repo = "%[2]s"
}
}
data "vercel_project_deployment_retention" "example_2" {
project_id = vercel_project.example_2.id
%[3]s
}
`, projectName, testGithubRepo(), teamIDConfig())
}
17 changes: 1 addition & 16 deletions vercel/resource_project_deployment_retention.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package vercel
import (
"context"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand Down Expand Up @@ -303,24 +302,10 @@ func (r *projectDeploymentRetentionResource) Update(ctx context.Context, req res
}
}

// splitID is a helper function for splitting an import ID into the corresponding parts.
// It also validates whether the ID is in a correct format.
func splitProjectDeploymentRetentionID(id string) (teamID, projectID string, ok bool) {
attributes := strings.Split(id, "/")
if len(attributes) == 2 {
return attributes[0], attributes[1], true
}
if len(attributes) == 1 {
return "", attributes[0], true
}

return "", "", false
}

// ImportState takes an identifier and reads all the project deployment retention information from the Vercel API.
// The results are then stored in terraform state.
func (r *projectDeploymentRetentionResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
teamID, projectID, ok := splitProjectDeploymentRetentionID(req.ID)
teamID, projectID, ok := splitInto1Or2(req.ID)
if !ok {
resp.Diagnostics.AddError(
"Error importing project deployment retention",
Expand Down
33 changes: 33 additions & 0 deletions vercel/resource_project_deployment_retention_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ func TestAcc_ProjectDeploymentRetention(t *testing.T) {
resource.TestCheckResourceAttr("vercel_project_deployment_retention.example", "expiration_errored", "1m"),
),
},
{
Config: testAccProjectDeploymentRetentionsConfigAllUnlimited(nameSuffix),
Check: resource.ComposeAggregateTestCheckFunc(
testAccProjectDeploymentRetentionExists("vercel_project_deployment_retention.example", testTeam()),
resource.TestCheckResourceAttr("vercel_project_deployment_retention.example", "expiration_preview", "unlimited"),
resource.TestCheckResourceAttr("vercel_project_deployment_retention.example", "expiration_production", "unlimited"),
resource.TestCheckResourceAttr("vercel_project_deployment_retention.example", "expiration_canceled", "unlimited"),
resource.TestCheckResourceAttr("vercel_project_deployment_retention.example", "expiration_errored", "unlimited"),
),
},
},
})
}
Expand Down Expand Up @@ -115,6 +125,29 @@ resource "vercel_project_deployment_retention" "example" {
`, projectName, testGithubRepo(), teamIDConfig())
}

func testAccProjectDeploymentRetentionsConfigAllUnlimited(projectName string) string {
return fmt.Sprintf(`
resource "vercel_project" "example" {
name = "test-acc-example-project-%[1]s"
%[3]s
git_repository = {
type = "github"
repo = "%[2]s"
}
}
resource "vercel_project_deployment_retention" "example" {
project_id = vercel_project.example.id
%[3]s
expiration_preview = "unlimited"
expiration_production = "unlimited"
expiration_canceled = "unlimited"
expiration_errored = "unlimited"
}
`, projectName, testGithubRepo(), teamIDConfig())
}

func testAccProjectDeploymentRetentionsConfigWithMissingFields(projectName string) string {
return fmt.Sprintf(`
resource "vercel_project" "example" {
Expand Down
17 changes: 1 addition & 16 deletions vercel/resource_project_environment_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package vercel
import (
"context"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand Down Expand Up @@ -412,24 +411,10 @@ func (r *projectEnvironmentVariableResource) Delete(ctx context.Context, req res
})
}

// splitID is a helper function for splitting an import ID into the corresponding parts.
// It also validates whether the ID is in a correct format.
func splitProjectEnvironmentVariableID(id string) (teamID, projectID, envID string, ok bool) {
attributes := strings.Split(id, "/")
if len(attributes) == 3 {
return attributes[0], attributes[1], attributes[2], true
}
if len(attributes) == 2 {
return "", attributes[0], attributes[1], true
}

return "", "", "", false
}

// ImportState takes an identifier and reads all the project environment variable information from the Vercel API.
// The results are then stored in terraform state.
func (r *projectEnvironmentVariableResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
teamID, projectID, envID, ok := splitProjectEnvironmentVariableID(req.ID)
teamID, projectID, envID, ok := splitInto2Or3(req.ID)
if !ok {
resp.Diagnostics.AddError(
"Error importing project environment variable",
Expand Down
14 changes: 1 addition & 13 deletions vercel/resource_shared_environment_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package vercel
import (
"context"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
Expand Down Expand Up @@ -421,21 +420,10 @@ func (r *sharedEnvironmentVariableResource) Delete(ctx context.Context, req reso
})
}

// splitID is a helper function for splitting an import ID into the corresponding parts.
// It also validates whether the ID is in a correct format.
func splitSharedEnvironmentVariableID(id string) (teamID, envID string, ok bool) {
attributes := strings.Split(id, "/")
if len(attributes) == 2 {
return attributes[0], attributes[1], true
}

return "", "", false
}

// ImportState takes an identifier and reads all the shared environment variable information from the Vercel API.
// The results are then stored in terraform state.
func (r *sharedEnvironmentVariableResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
teamID, envID, ok := splitSharedEnvironmentVariableID(req.ID)
teamID, envID, ok := splitInto1Or2(req.ID)
if !ok {
resp.Diagnostics.AddError(
"Error importing shared environment variable",
Expand Down

0 comments on commit cb24399

Please sign in to comment.