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

[projects] add support for default project max duration #203

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

Closed
wants to merge 5 commits into from
Closed
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
82 changes: 82 additions & 0 deletions client/project_function_max_duration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package client

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-log/tflog"
)

type ProjectFunctionMaxDurationRequest struct {
ProjectID string
TeamID string
MaxDuration int64
}

type functionMaxDuration struct {
DefaultFunctionTimeout *int64 `json:"defaultFunctionTimeout"`
}

type ProjectFunctionMaxDuration struct {
ProjectID string
TeamID string
MaxDuration *int64
}

func (c *Client) GetProjectFunctionMaxDuration(ctx context.Context, projectID, teamID string) (p ProjectFunctionMaxDuration, err error) {
url := fmt.Sprintf("%s/v1/projects/%s/resource-config", c.baseURL, projectID)
if c.teamID(teamID) != "" {
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(teamID))
}
tflog.Info(ctx, "get project function max duration", map[string]interface{}{
"url": url,
})
var f functionMaxDuration
err = c.doRequest(clientRequest{
ctx: ctx,
method: "GET",
url: url,
}, &f)
if err != nil {
return p, err
}
var maxDuration *int64
if f.DefaultFunctionTimeout != nil {
maxDuration = f.DefaultFunctionTimeout
}
return ProjectFunctionMaxDuration{
ProjectID: projectID,
TeamID: teamID,
MaxDuration: maxDuration,
}, err
}

func (c *Client) UpdateProjectFunctionMaxDuration(ctx context.Context, request ProjectFunctionMaxDurationRequest) (p ProjectFunctionMaxDuration, err error) {
url := fmt.Sprintf("%s/v1/projects/%s/resource-config", c.baseURL, request.ProjectID)
if c.teamID(request.TeamID) != "" {
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(request.TeamID))
}

payload := string(mustMarshal(functionMaxDuration{
DefaultFunctionTimeout: &request.MaxDuration,
}))
var f functionMaxDuration
err = c.doRequest(clientRequest{
ctx: ctx,
method: "PATCH",
url: url,
body: payload,
}, &f)
if err != nil {
return p, err
}
var maxDuration *int64
if f.DefaultFunctionTimeout != nil {
maxDuration = f.DefaultFunctionTimeout
}
return ProjectFunctionMaxDuration{
ProjectID: request.ProjectID,
TeamID: request.TeamID,
MaxDuration: maxDuration,
}, err
}
32 changes: 32 additions & 0 deletions docs/data-sources/project_function_max_duration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "vercel_project_function_max_duration Data Source - terraform-provider-vercel"
subcategory: ""
description: |-
Provides information about a Project's Function max duration setting.
This controls the default maximum duration of your Serverless Functions can use while executing. 10s is recommended for most workloads. Can be configured from 1 to 900 seconds (plan limits apply). You can override this per function using the vercel.json file.
---

# vercel_project_function_max_duration (Data Source)

Provides information about a Project's Function max duration setting.

This controls the default maximum duration of your Serverless Functions can use while executing. 10s is recommended for most workloads. Can be configured from 1 to 900 seconds (plan limits apply). You can override this per function using the vercel.json file.



<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `project_id` (String) The ID of the Project to read the Function max duration setting for.

### Optional

- `team_id` (String) The ID of the team the Project exists under. Required when configuring a team resource if a default team has not been set in the provider.

### Read-Only

- `id` (String) The ID of the resource.
- `max_duration` (Number) The default max duration for your Serverless Functions. Must be between 1 and 900 (plan limits apply)
57 changes: 57 additions & 0 deletions docs/resources/project_function_max_duration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "vercel_project_function_max_duration Resource - terraform-provider-vercel"
subcategory: ""
description: |-
Provides a Function Max Duration resource for a Project.
This controls the default maximum duration of your Serverless Functions can use while executing. 10s is recommended for most workloads. Can be configured from 1 to 900 seconds (plan limits apply). You can override this per function using the vercel.json file.
A new Deployment is required for your changes to take effect.
---

# vercel_project_function_max_duration (Resource)

Provides a Function Max Duration resource for a Project.

This controls the default maximum duration of your Serverless Functions can use while executing. 10s is recommended for most workloads. Can be configured from 1 to 900 seconds (plan limits apply). You can override this per function using the vercel.json file.

A new Deployment is required for your changes to take effect.

## Example Usage

```terraform
resource "vercel_project" "example" {
name = "example"
}

resource "vercel_project_function_max_duration" "example" {
project_id = vercel_project.example.id
max_duration = 100
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `max_duration` (Number) The default max duration for your Serverless Functions. Must be between 1 and 900 (plan limits apply)
- `project_id` (String) The ID of the Project to adjust the max duration for.

### Optional

- `team_id` (String) The ID of the team the Project exists under. Required when configuring a team resource if a default team has not been set in the provider.

### Read-Only

- `id` (String) The ID of the resource.

## Import

Import is supported using the following syntax:

```shell
# You can import via the team_id and project_id.
# - team_id can be found in the team `settings` tab in the Vercel UI.
# - project_id can be found in the project `settings` tab in the Vercel UI.
terraform import vercel_project_function_max_duration.example team_xxxxxxxxxxxxxxxxxxxxxxxx/prj_xxxxxxxxxxxxxxxxxxxxxxxxxxxx
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# You can import via the team_id and project_id.
# - team_id can be found in the team `settings` tab in the Vercel UI.
# - project_id can be found in the project `settings` tab in the Vercel UI.
terraform import vercel_project_function_max_duration.example team_xxxxxxxxxxxxxxxxxxxxxxxx/prj_xxxxxxxxxxxxxxxxxxxxxxxxxxxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
resource "vercel_project" "example" {
name = "example"
}

resource "vercel_project_function_max_duration" "example" {
project_id = vercel_project.example.id
max_duration = 100
}
118 changes: 118 additions & 0 deletions vercel/data_source_project_function_max_duration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package vercel

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/vercel/terraform-provider-vercel/client"
)

// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &projectFunctionMaxDurationDataSource{}
_ datasource.DataSourceWithConfigure = &projectFunctionMaxDurationDataSource{}
)

func newProjectFunctionMaxDurationDataSource() datasource.DataSource {
return &projectFunctionMaxDurationDataSource{}
}

type projectFunctionMaxDurationDataSource struct {
client *client.Client
}

func (d *projectFunctionMaxDurationDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_project_function_max_duration"
}

func (d *projectFunctionMaxDurationDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*client.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *client.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}

d.client = client
}

func (r *projectFunctionMaxDurationDataSource) Schema(_ context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: `Provides information about a Project's Function max duration setting.

This controls the default maximum duration of your Serverless Functions can use while executing. 10s is recommended for most workloads. Can be configured from 1 to 900 seconds (plan limits apply). You can override this per function using the vercel.json file.
`,
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "The ID of the resource.",
Computed: true,
},
"project_id": schema.StringAttribute{
Description: "The ID of the Project to read the Function max duration setting for.",
Required: true,
},
"team_id": schema.StringAttribute{
Optional: true,
Computed: true,
Description: "The ID of the team the Project exists under. Required when configuring a team resource if a default team has not been set in the provider.",
},
"max_duration": schema.Int64Attribute{
Description: "The default max duration for your Serverless Functions. Must be between 1 and 900 (plan limits apply)",
Computed: true,
Validators: []validator.Int64{
int64GreaterThan(0),
int64LessThan(900),
},
},
},
}
}

func (d *projectFunctionMaxDurationDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var config ProjectFunctionMaxDuration
diags := req.Config.Get(ctx, &config)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

out, err := d.client.GetProjectFunctionMaxDuration(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 Function max duration",
fmt.Sprintf("Could not get Project Function max duration %s %s, unexpected error: %s",
config.TeamID.ValueString(),
config.ProjectID.ValueString(),
err,
),
)
return
}

result := convertResponseToProjectFunctionMaxDuration(out)
tflog.Info(ctx, "read project function max duration", map[string]interface{}{
"team_id": result.TeamID.ValueString(),
"project_id": result.ProjectID.ValueString(),
})

diags = resp.State.Set(ctx, result)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
45 changes: 45 additions & 0 deletions vercel/data_source_project_function_max_duration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package vercel_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAcc_ProjectFunctionMaxDurationDataSource(t *testing.T) {
name := acctest.RandString(16)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccProjectFunctionMaxDurationDataSourceConfig(name, teamIDConfig()),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.vercel_project_function_max_duration.elevated", "max_duration", "100"),
),
},
},
})
}

func testAccProjectFunctionMaxDurationDataSourceConfig(name, teamID string) string {
return fmt.Sprintf(`
resource "vercel_project" "elevated" {
name = "test-acc-%[1]s"
%[2]s
}

resource "vercel_project_function_max_duration" "elevated" {
project_id = vercel_project.elevated.id
max_duration = 100
%[2]s
}

data "vercel_project_function_max_duration" "elevated" {
project_id = vercel_project_function_max_duration.elevated.project_id
%[2]s
}
`, name, teamID)
}
2 changes: 2 additions & 0 deletions vercel/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (p *vercelProvider) Resources(_ context.Context) []func() resource.Resource
newProjectDomainResource,
newProjectEnvironmentVariableResource,
newProjectFunctionCPUResource,
newProjectFunctionMaxDurationResource,
newProjectResource,
newSharedEnvironmentVariableResource,
newWebhookResource,
Expand All @@ -84,6 +85,7 @@ func (p *vercelProvider) DataSources(_ context.Context) []func() datasource.Data
newProjectDeploymentRetentionDataSource,
newProjectDirectoryDataSource,
newProjectFunctionCPUDataSource,
newProjectFunctionMaxDurationDataSource,
newSharedEnvironmentVariableDataSource,
}
}
Expand Down
Loading
Loading