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

Support Log Drains #168

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 1 commit into from
Apr 19, 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
100 changes: 100 additions & 0 deletions client/log_drain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package client

import (
"context"
"fmt"

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

type LogDrain struct {
ID string `json:"id"`
TeamID string `json:"ownerId"`
DeliveryFormat string `json:"deliveryFormat"`
Environments []string `json:"environments"`
Headers map[string]string `json:"headers"`
ProjectIDs []string `json:"projectIds"`
SamplingRate *float64 `json:"samplingRate"`
Secret string `json:"secret"`
Sources []string `json:"sources"`
Endpoint string `json:"url"`
}

type CreateLogDrainRequest struct {
TeamID string `json:"-"`
DeliveryFormat string `json:"deliveryFormat"`
Environments []string `json:"environments"`
Headers map[string]string `json:"headers,omitempty"`
ProjectIDs []string `json:"projectIds,omitempty"`
SamplingRate float64 `json:"samplingRate,omitempty"`
Secret string `json:"secret,omitempty"`
Sources []string `json:"sources"`
Endpoint string `json:"url"`
}

func (c *Client) CreateLogDrain(ctx context.Context, request CreateLogDrainRequest) (l LogDrain, err error) {
url := fmt.Sprintf("%s/v1/log-drains", c.baseURL)
if c.teamID(request.TeamID) != "" {
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(request.TeamID))
}
payload := string(mustMarshal(request))
tflog.Info(ctx, "creating log drain", map[string]interface{}{
"url": url,
"payload": payload,
})
err = c.doRequest(clientRequest{
ctx: ctx,
method: "POST",
url: url,
body: payload,
}, &l)
return l, err
}

func (c *Client) DeleteLogDrain(ctx context.Context, id, teamID string) error {
url := fmt.Sprintf("%s/v1/log-drains/%s", c.baseURL, id)
if c.teamID(teamID) != "" {
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(teamID))
}
tflog.Info(ctx, "deleting log drain", map[string]interface{}{
"url": url,
})
return c.doRequest(clientRequest{
ctx: ctx,
method: "DELETE",
url: url,
}, nil)
}

func (c *Client) GetLogDrain(ctx context.Context, id, teamID string) (l LogDrain, err error) {
url := fmt.Sprintf("%s/v1/log-drains/%s", c.baseURL, id)
if c.teamID(teamID) != "" {
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(teamID))
}
tflog.Info(ctx, "reading log drain", map[string]interface{}{
"url": url,
})
err = c.doRequest(clientRequest{
ctx: ctx,
method: "GET",
url: url,
}, &l)
return l, err
}

func (c *Client) GetEndpointVerificationCode(ctx context.Context, teamID string) (code string, err error) {
url := fmt.Sprintf("%s/v1/verify-endpoint", c.baseURL)
if c.teamID(teamID) != "" {
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(teamID))
}

var l struct {
Code string `json:"verificationCode"`
}
err = c.doRequest(clientRequest{
ctx: ctx,
method: "GET",
url: url,
}, &l)
return l.Code, err
}
29 changes: 29 additions & 0 deletions docs/data-sources/endpoint_verification.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "vercel_endpoint_verification Data Source - terraform-provider-vercel"
subcategory: ""
description: |-
Provides a verification code that can be used to prove ownership over an API.
---

# vercel_endpoint_verification (Data Source)

Provides a verification code that can be used to prove ownership over an API.

## Example Usage

```terraform
data "vercel_endpoint_verification" "example" {}
```

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

### Optional

- `team_id` (String) The ID of the team the Edge Config should exist 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 this resource.
- `verification_code` (String) A verification code that should be set in the `x-vercel-verify` response header for your API. This is used to verify that the endpoint belongs to you.
46 changes: 46 additions & 0 deletions docs/data-sources/log_drain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "vercel_log_drain Data Source - terraform-provider-vercel"
subcategory: ""
description: |-
Provides information about an existing Log Drain.
Log Drains collect all of your logs using a service specializing in storing app logs.
Teams on Pro and Enterprise plans can subscribe to log drains that are generic and configurable from the Vercel dashboard without creating an integration. This allows you to use a HTTP service to receive logs through Vercel's log drains.
---

# vercel_log_drain (Data Source)

Provides information about an existing Log Drain.

Log Drains collect all of your logs using a service specializing in storing app logs.

Teams on Pro and Enterprise plans can subscribe to log drains that are generic and configurable from the Vercel dashboard without creating an integration. This allows you to use a HTTP service to receive logs through Vercel's log drains.

## Example Usage

```terraform
data "vercel_log_drain" "example" {
id = "lg_xxxxxxx_xxxxxx_xxxxx"
}
```

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

### Required

- `endpoint` (String) Logs will be sent as POST requests to this URL. The endpoint will be verified, and must return a `200` status code and an `x-vercel-verify` header taken from the endpoint_verification data source. The value the `x-vercel-verify` header should be can be read from the `vercel_endpoint_verification_code` data source.
- `id` (String) The ID of the Log Drain.

### Optional

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

### Read-Only

- `delivery_format` (String) The format log data should be delivered in. Can be `json` or `ndjson`.
- `environments` (Set of String) Logs from the selected environments will be forwarded to your webhook. At least one must be present.
- `headers` (Map of String) Custom headers to include in requests to the log drain endpoint.
- `project_ids` (Set of String) A list of project IDs that the log drain should be associated with. Logs from these projects will be sent log events to the specified endpoint. If omitted, logs will be sent for all projects.
- `sampling_rate` (Number) A ratio of logs matching the sampling rate will be sent to your log drain. Should be a value between 0 and 1. If unspecified, all logs are sent.
- `sources` (Set of String) A set of sources that the log drain should send logs for. Valid values are `static`, `edge`, `external`, `build` and `function`.
87 changes: 87 additions & 0 deletions docs/resources/log_drain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "vercel_log_drain Resource - terraform-provider-vercel"
subcategory: ""
description: |-
Provides a Configurable Log Drain resource.
~> For Log Drain integrations, please see the Integration Log Drain docs https://vercel.com/docs/observability/log-drains#log-drains-integration.
Log Drains collect all of your logs using a service specializing in storing app logs.
Teams on Pro and Enterprise plans can subscribe to log drains that are generic and configurable from the Vercel dashboard without creating an integration. This allows you to use a HTTP service to receive logs through Vercel's log drains.
~> Only Pro and Enterprise teams can create Configurable Log Drains.
---

# vercel_log_drain (Resource)

Provides a Configurable Log Drain resource.

~> For Log Drain integrations, please see the [Integration Log Drain docs](https://vercel.com/docs/observability/log-drains#log-drains-integration).

Log Drains collect all of your logs using a service specializing in storing app logs.

Teams on Pro and Enterprise plans can subscribe to log drains that are generic and configurable from the Vercel dashboard without creating an integration. This allows you to use a HTTP service to receive logs through Vercel's log drains.

~> Only Pro and Enterprise teams can create Configurable Log Drains.

## Example Usage

```terraform
// Use the vercel_endpoint_verification data source to work out the verification code needed to
// verify the log drain endpoint.
data "vercel_endpoint_verification" "example" {
}

resource "vercel_log_drain" "example" {
delivery_format = "json"
environments = ["production"]
headers = {
some-key = "some-value"
}
project_ids = [vercel_project.example.id]
sampling_rate = 0.8
secret = "a_very_long_and_very_well_specified_secret"
sources = ["static"]
endpoint = "https://example.com/my-log-drain-endpoint"
}

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

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

### Required

- `delivery_format` (String) The format log data should be delivered in. Can be `json` or `ndjson`.
- `endpoint` (String) Logs will be sent as POST requests to this URL. The endpoint will be verified, and must return a `200` status code and an `x-vercel-verify` header taken from the endpoint_verification data source. The value the `x-vercel-verify` header should be can be read from the `vercel_endpoint_verification_code` data source.
- `environments` (Set of String) Logs from the selected environments will be forwarded to your webhook. At least one must be present.
- `sources` (Set of String) A set of sources that the log drain should send logs for. Valid values are `static`, `edge`, `external`, `build` and `lambda`.

### Optional

- `headers` (Map of String) Custom headers to include in requests to the log drain endpoint.
- `project_ids` (Set of String) A list of project IDs that the log drain should be associated with. Logs from these projects will be sent log events to the specified endpoint. If omitted, logs will be sent for all projects.
- `sampling_rate` (Number) A ratio of logs matching the sampling rate will be sent to your log drain. Should be a value between 0 and 1. If unspecified, all logs are sent.
- `secret` (String, Sensitive) A custom secret to be used for signing log events. You can use this secret to verify that log events are coming from Vercel and are not tampered with. See https://vercel.com/docs/observability/log-drains/log-drains-reference#secure-log-drains for full info.
- `team_id` (String) The ID of the team the Log Drain should exist 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 Log Drain.

## Import

Import is supported using the following syntax:

```shell
# If importing into a personal account, or with a team configured on
# the provider, simply use the log_drain_id.
# - log_drain_id can be found by querying the Vercel REST API (https://vercel.com/docs/rest-api/endpoints/logDrains#retrieves-a-list-of-all-the-log-drains).
terraform import vercel_log_drain.example ecfg_xxxxxxxxxxxxxxxxxxxxxxxxxxxx/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

# Alternatively, you can import via the team_id and edge_config_id.
# - team_id can be found in the team `settings` tab in the Vercel UI.
# - log_drain_id can be found by querying the Vercel REST API (https://vercel.com/docs/rest-api/endpoints/logDrains#retrieves-a-list-of-all-the-log-drains).
terraform import vercel_log_drain.example team_xxxxxxxxxxxxxxxxxxxxxxxx/ecfg_xxxxxxxxxxxxxxxxxxxxxxxxxxxx/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
```
4 changes: 2 additions & 2 deletions docs/resources/webhook.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ resource "vercel_webhook" "without_project_ids" {

### Required

- `endpoint` (String) Webhooks events will be sent as POST request to this URL.
- `endpoint` (String) Webhooks events will be sent as POST requests to this URL.
- `events` (Set of String) A list of the events the webhook will listen to. At least one must be present.

### Optional
Expand All @@ -55,4 +55,4 @@ resource "vercel_webhook" "without_project_ids" {
### Read-Only

- `id` (String) The ID of the Webhook.
- `secret` (String) A secret value which will be provided in the `x-vercel-signature` header and can be used to verify the authenticity of the webhook. See https://vercel.com/docs/observability/webhooks-overview/webhooks-api#securing-webhooks for further details.
- `secret` (String, Sensitive) A secret value which will be provided in the `x-vercel-signature` header and can be used to verify the authenticity of the webhook. See https://vercel.com/docs/observability/webhooks-overview/webhooks-api#securing-webhooks for further details.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data "vercel_endpoint_verification" "example" {}
3 changes: 3 additions & 0 deletions examples/data-sources/vercel_log_drain/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "vercel_log_drain" "example" {
id = "lg_xxxxxxx_xxxxxx_xxxxx"
}
9 changes: 9 additions & 0 deletions examples/resources/vercel_log_drain/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# If importing into a personal account, or with a team configured on
# the provider, simply use the log_drain_id.
# - log_drain_id can be found by querying the Vercel REST API (https://vercel.com/docs/rest-api/endpoints/logDrains#retrieves-a-list-of-all-the-log-drains).
terraform import vercel_log_drain.example ecfg_xxxxxxxxxxxxxxxxxxxxxxxxxxxx/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

# Alternatively, you can import via the team_id and edge_config_id.
# - team_id can be found in the team `settings` tab in the Vercel UI.
# - log_drain_id can be found by querying the Vercel REST API (https://vercel.com/docs/rest-api/endpoints/logDrains#retrieves-a-list-of-all-the-log-drains).
terraform import vercel_log_drain.example team_xxxxxxxxxxxxxxxxxxxxxxxx/ecfg_xxxxxxxxxxxxxxxxxxxxxxxxxxxx/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
21 changes: 21 additions & 0 deletions examples/resources/vercel_log_drain/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Use the vercel_endpoint_verification data source to work out the verification code needed to
// verify the log drain endpoint.
data "vercel_endpoint_verification" "example" {
}

resource "vercel_log_drain" "example" {
delivery_format = "json"
environments = ["production"]
headers = {
some-key = "some-value"
}
project_ids = [vercel_project.example.id]
sampling_rate = 0.8
secret = "a_very_long_and_very_well_specified_secret"
sources = ["static"]
endpoint = "https://example.com/my-log-drain-endpoint"
}

resource "vercel_project" "example" {
name = "example"
}
Loading