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

Add support for managing Function CPU #180

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
merged 2 commits into from
May 2, 2024
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
97 changes: 97 additions & 0 deletions client/project_function_cpu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package client

import (
"context"
"fmt"

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

type ProjectFunctionCPURequest struct {
ProjectID string
TeamID string
CPU string
}

type functionCPU struct {
DefaultMemoryType *string `json:"defaultMemoryType"`
}

type ProjectFunctionCPU struct {
ProjectID string
TeamID string
CPU *string
}

var toCPUNetwork = map[string]string{
"basic": "standard_legacy",
"standard": "standard",
"performance": "performance",
}

var fromCPUNetwork = map[string]string{
"standard_legacy": "basic",
"standard": "standard",
"performance": "performance",
}

func (c *Client) GetProjectFunctionCPU(ctx context.Context, projectID, teamID string) (p ProjectFunctionCPU, 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 cpu", map[string]interface{}{
"url": url,
})
var f functionCPU
err = c.doRequest(clientRequest{
ctx: ctx,
method: "GET",
url: url,
}, &f)
if err != nil {
return p, err
}
var cpu *string
if f.DefaultMemoryType != nil {
v := fromCPUNetwork[*f.DefaultMemoryType]
cpu = &v
}
return ProjectFunctionCPU{
ProjectID: projectID,
TeamID: teamID,
CPU: cpu,
}, err
}

func (c *Client) UpdateProjectFunctionCPU(ctx context.Context, request ProjectFunctionCPURequest) (p ProjectFunctionCPU, 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))
}

v := toCPUNetwork[request.CPU]
payload := string(mustMarshal(functionCPU{
DefaultMemoryType: &v,
}))
var f functionCPU
err = c.doRequest(clientRequest{
ctx: ctx,
method: "PATCH",
url: url,
body: payload,
}, &f)
if err != nil {
return p, err
}
var cpu *string
if f.DefaultMemoryType != nil {
v := fromCPUNetwork[*f.DefaultMemoryType]
cpu = &v
}
return ProjectFunctionCPU{
ProjectID: request.ProjectID,
TeamID: request.TeamID,
CPU: cpu,
}, err
}
42 changes: 42 additions & 0 deletions docs/data-sources/project_function_cpu.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "vercel_project_function_cpu Data Source - terraform-provider-vercel"
subcategory: ""
description: |-
Provides information about a Project's Function CPU setting.
This controls the maximum amount of CPU utilization your Serverless Functions can use while executing. Standard is optimal for most frontend workloads. You can override this per function using the vercel.json file.
---

# vercel_project_function_cpu (Data Source)

Provides information about a Project's Function CPU setting.

This controls the maximum amount of CPU utilization your Serverless Functions can use while executing. Standard is optimal for most frontend workloads. You can override this per function using the vercel.json file.

## Example Usage

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

data "vercel_project_function_cpu" "example" {
project_id = data.vercel_project.example.id
}
```

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

### Required

- `project_id` (String) The ID of the Project to read the Function CPU 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

- `cpu` (String) The amount of CPU available to your Serverless Functions. Should be one of 'basic' (0.6vCPU), 'standard' (1vCPU) or 'performance' (1.7vCPUs).
- `id` (String) The ID of the resource.
57 changes: 57 additions & 0 deletions docs/resources/project_function_cpu.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_cpu Resource - terraform-provider-vercel"
subcategory: ""
description: |-
Provides a Function CPU resource for a Project.
This controls the maximum amount of CPU utilization your Serverless Functions can use while executing. Standard is optimal for most frontend workloads. 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_cpu (Resource)

Provides a Function CPU resource for a Project.

This controls the maximum amount of CPU utilization your Serverless Functions can use while executing. Standard is optimal for most frontend workloads. 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_cpu" "example" {
project_id = vercel_project.example.id
cpu = "performance"
}
```

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

### Required

- `cpu` (String) The amount of CPU available to your Serverless Functions. Should be one of 'basic' (0.6vCPU), 'standard' (1vCPU) or 'performance' (1.7vCPUs).
- `project_id` (String) The ID of the Project to adjust the CPU 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_cpu.example team_xxxxxxxxxxxxxxxxxxxxxxxx/prj_xxxxxxxxxxxxxxxxxxxxxxxxxxxx
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
data "vercel_project" "example" {
name = "example"
}

data "vercel_project_function_cpu" "example" {
project_id = data.vercel_project.example.id
}
4 changes: 4 additions & 0 deletions examples/resources/vercel_project_function_cpu/import.sh
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_cpu.example team_xxxxxxxxxxxxxxxxxxxxxxxx/prj_xxxxxxxxxxxxxxxxxxxxxxxxxxxx
8 changes: 8 additions & 0 deletions examples/resources/vercel_project_function_cpu/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
resource "vercel_project" "example" {
name = "example"
}

resource "vercel_project_function_cpu" "example" {
project_id = vercel_project.example.id
cpu = "performance"
}
113 changes: 113 additions & 0 deletions vercel/data_source_project_function_cpu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
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-log/tflog"
"github.com/vercel/terraform-provider-vercel/client"
)

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

func newProjectFunctionCPUDataSource() datasource.DataSource {
return &projectFunctionCPUDataSource{}
}

type projectFunctionCPUDataSource struct {
client *client.Client
}

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

func (d *projectFunctionCPUDataSource) 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 *projectFunctionCPUDataSource) Schema(_ context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: `Provides information about a Project's Function CPU setting.

This controls the maximum amount of CPU utilization your Serverless Functions can use while executing. Standard is optimal for most frontend workloads. 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 CPU 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.",
},
"cpu": schema.StringAttribute{
Description: "The amount of CPU available to your Serverless Functions. Should be one of 'basic' (0.6vCPU), 'standard' (1vCPU) or 'performance' (1.7vCPUs).",
Computed: true,
},
},
}
}

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

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

result := convertResponseToProjectFunctionCPU(out)
tflog.Info(ctx, "read project function cpu", 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
}
}
Loading