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

Add support for setting Fluid on vercel_project resource #288

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
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
9 changes: 8 additions & 1 deletion client/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ type ProjectResponse struct {
GitComments *GitComments `json:"gitComments"`
Security *Security `json:"security"`
DeploymentExpiration *DeploymentExpiration `json:"deploymentExpiration"`
ResourceConfig *ResourceConfig `json:"resourceConfig"`
ResourceConfig *ResourceConfigResponse `json:"resourceConfig"`
NodeVersion string `json:"nodeVersion"`
}

Expand All @@ -217,9 +217,16 @@ type Security struct {
AttackModeEnabled bool `json:"attackModeEnabled"`
}

type ResourceConfigResponse struct {
FunctionDefaultMemoryType *string `json:"functionDefaultMemoryType"`
FunctionDefaultTimeout *int64 `json:"functionDefaultTimeout"`
Fluid bool `json:"fluid"`
}

type ResourceConfig struct {
FunctionDefaultMemoryType *string `json:"functionDefaultMemoryType,omitempty"`
FunctionDefaultTimeout *int64 `json:"functionDefaultTimeout,omitempty"`
Fluid *bool `json:"fluid,omitempty"`
}

// GetProject retrieves information about an existing project from Vercel.
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/project.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ Read-Only:

Read-Only:

- `fluid` (Boolean) Enable fluid compute for your Vercel Functions to automatically manage concurrency and optimize performance. Vercel will handle the defaults to ensure the best experience for your workload.
- `function_default_cpu_type` (String) The amount of CPU available to your Serverless Functions. Should be one of 'standard_legacy' (0.6vCPU), 'standard' (1vCPU) or 'performance' (1.7vCPUs).
- `function_default_timeout` (Number) The default timeout for Serverless Functions.

Expand Down
1 change: 1 addition & 0 deletions docs/resources/project.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ Required:

Optional:

- `fluid` (Boolean) Enable fluid compute for your Vercel Functions to automatically manage concurrency and optimize performance. Vercel will handle the defaults to ensure the best experience for your workload.
- `function_default_cpu_type` (String) The amount of CPU available to your Serverless Functions. Should be one of 'standard_legacy' (0.6vCPU), 'standard' (1vCPU) or 'performance' (1.7vCPUs).
- `function_default_timeout` (Number) The default timeout for Serverless Functions.

Expand Down
4 changes: 4 additions & 0 deletions vercel/data_source_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,10 @@ For more detailed information, please see the [Vercel documentation](https://ver
Description: "The default timeout for Serverless Functions.",
Computed: true,
},
"fluid": schema.BoolAttribute{
Description: "Enable fluid compute for your Vercel Functions to automatically manage concurrency and optimize performance. Vercel will handle the defaults to ensure the best experience for your workload.",
Computed: true,
},
},
},
},
Expand Down
5 changes: 3 additions & 2 deletions vercel/resource_dns_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import (
)

var (
_ resource.Resource = &dnsRecordResource{}
_ resource.ResourceWithConfigure = &dnsRecordResource{}
_ resource.Resource = &dnsRecordResource{}
_ resource.ResourceWithConfigure = &dnsRecordResource{}
_ resource.ResourceWithValidateConfig = &dnsRecordResource{}
)

func newDNSRecordResource() resource.Resource {
Expand Down
27 changes: 23 additions & 4 deletions vercel/resource_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ import (
)

var (
_ resource.Resource = &projectResource{}
_ resource.ResourceWithConfigure = &projectResource{}
_ resource.ResourceWithImportState = &projectResource{}
_ resource.ResourceWithModifyPlan = &projectResource{}
_ resource.Resource = &projectResource{}
_ resource.ResourceWithConfigure = &projectResource{}
_ resource.ResourceWithImportState = &projectResource{}
_ resource.ResourceWithModifyPlan = &projectResource{}
_ resource.ResourceWithConfigValidators = &projectResource{}
)

func newProjectResource() resource.Resource {
Expand Down Expand Up @@ -543,12 +544,24 @@ At this time you cannot use a Vercel Project resource with in-line ` + "`environ
},
PlanModifiers: []planmodifier.Int64{int64planmodifier.UseStateForUnknown()},
},
"fluid": schema.BoolAttribute{
Description: "Enable fluid compute for your Vercel Functions to automatically manage concurrency and optimize performance. Vercel will handle the defaults to ensure the best experience for your workload.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Bool{boolplanmodifier.UseStateForUnknown()},
},
},
},
},
}
}

func (r *projectResource) ConfigValidators(ctx context.Context) []resource.ConfigValidator {
return []resource.ConfigValidator{
&fluidComputeBasicCPUValidator{},
}
}

// Project reflects the state terraform stores internally for a project.
type Project struct {
BuildCommand types.String `tfsdk:"build_command"`
Expand Down Expand Up @@ -1003,12 +1016,14 @@ var resourceConfigAttrType = types.ObjectType{
AttrTypes: map[string]attr.Type{
"function_default_cpu_type": types.StringType,
"function_default_timeout": types.Int64Type,
"fluid": types.BoolType,
},
}

type ResourceConfig struct {
FunctionDefaultCPUType types.String `tfsdk:"function_default_cpu_type"`
FunctionDefaultTimeout types.Int64 `tfsdk:"function_default_timeout"`
Fluid types.Bool `tfsdk:"fluid"`
}

func (p *Project) resourceConfig(ctx context.Context) (rc *ResourceConfig, diags diag.Diagnostics) {
Expand All @@ -1031,6 +1046,9 @@ func (r *ResourceConfig) toClientResourceConfig() *client.ResourceConfig {
if !r.FunctionDefaultTimeout.IsUnknown() {
resourceConfig.FunctionDefaultTimeout = r.FunctionDefaultTimeout.ValueInt64Pointer()
}
if !r.Fluid.IsUnknown() {
resourceConfig.Fluid = r.Fluid.ValueBoolPointer()
}
return resourceConfig
}

Expand Down Expand Up @@ -1253,6 +1271,7 @@ func convertResponseToProject(ctx context.Context, response client.ProjectRespon
resourceConfig = types.ObjectValueMust(resourceConfigAttrType.AttrTypes, map[string]attr.Value{
"function_default_cpu_type": types.StringPointerValue(response.ResourceConfig.FunctionDefaultMemoryType),
"function_default_timeout": types.Int64PointerValue(response.ResourceConfig.FunctionDefaultTimeout),
"fluid": types.BoolValue(response.ResourceConfig.Fluid),
})
}

Expand Down
73 changes: 73 additions & 0 deletions vercel/resource_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"regexp"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
Expand Down Expand Up @@ -103,6 +104,78 @@ func TestAcc_Project(t *testing.T) {
})
}

func TestAcc_ProjectFluidCompute(t *testing.T) {
projectSuffix := acctest.RandString(16)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
CheckDestroy: testAccProjectDestroy("vercel_project.test", testTeam()),
Steps: []resource.TestStep{
{
// check we get a sensible error if fluid + invalid CPU combination.
Config: `
resource "vercel_project" "test" {
name = "foo"
resource_config = {
fluid = true
function_default_cpu_type = "standard_legacy"
}
}
`,
ExpectError: regexp.MustCompile(strings.ReplaceAll("Fluid compute is only supported with the standard or performance CPU types.", " ", `\s*`)),
},
{
// check creating a project with Fluid
Config: fmt.Sprintf(`
resource "vercel_project" "test" {
name = "test-acc-fluid-%[1]s"
%[2]s

resource_config = {
fluid = true
}
}
`, projectSuffix, teamIDConfig()),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("vercel_project.test", "name", fmt.Sprintf("test-acc-fluid-%s", projectSuffix)),
resource.TestCheckResourceAttr("vercel_project.test", "resource_config.fluid", "true"),
),
},
{
// check updating Fluid on a project
Config: fmt.Sprintf(`
resource "vercel_project" "test" {
name = "test-acc-fluid-%[1]s"
%[2]s

resource_config = {
fluid = false
}
}
`, projectSuffix, teamIDConfig()),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("vercel_project.test", "name", fmt.Sprintf("test-acc-fluid-%s", projectSuffix)),
resource.TestCheckResourceAttr("vercel_project.test", "resource_config.fluid", "false"),
),
},
{
// check new projects without fluid specified shows fluid as false
Config: fmt.Sprintf(`
resource "vercel_project" "test" {
name = "test-acc-fluid-disabled-%[1]s"
%[2]s
}
`, projectSuffix, teamIDConfig()),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("vercel_project.test", "name", fmt.Sprintf("test-acc-fluid-disabled-%s", projectSuffix)),
resource.TestCheckResourceAttr("vercel_project.test", "resource_config.fluid", "false"),
),
},
},
})
}

func TestAcc_ProjectAddingEnvAfterInitialCreation(t *testing.T) {
projectSuffix := acctest.RandString(16)
resource.Test(t, resource.TestCase{
Expand Down
47 changes: 47 additions & 0 deletions vercel/resource_project_validators.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package vercel

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/resource"
)

var _ resource.ConfigValidator = &fluidComputeBasicCPUValidator{}

type fluidComputeBasicCPUValidator struct{}

func (v *fluidComputeBasicCPUValidator) Description(ctx context.Context) string {
return "Validates that the CPU type is one of the allowed values for Vercel fluid compute."
}

func (v *fluidComputeBasicCPUValidator) MarkdownDescription(ctx context.Context) string {
return v.Description(ctx)
}

func (v *fluidComputeBasicCPUValidator) ValidateResource(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) {
var project Project
diags := req.Config.Get(ctx, &project)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
resourceConfig, diags := project.resourceConfig(ctx)
if diags.HasError() {
resp.Diagnostics.Append(diags...)
return
}
if resourceConfig == nil {
return
}
if !resourceConfig.Fluid.ValueBool() {
return
}
if resourceConfig.FunctionDefaultCPUType.ValueString() != "standard_legacy" {
return
}

resp.Diagnostics.AddError(
"Error validating project fluid compute configuration",
"Fluid compute is only supported with the standard or performance CPU types.",
)
}