|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 8 | +) |
| 9 | + |
| 10 | +// Drain represents a configurable drain in the Vercel API |
| 11 | +type Drain struct { |
| 12 | + ID string `json:"id"` |
| 13 | + OwnerID string `json:"ownerId"` |
| 14 | + Name string `json:"name"` |
| 15 | + ProjectIds []string `json:"projectIds"` |
| 16 | + Schemas map[string]any `json:"schemas"` |
| 17 | + Delivery DeliveryConfig `json:"delivery"` |
| 18 | + Sampling []SamplingConfig `json:"sampling,omitempty"` |
| 19 | + TeamID string `json:"teamId"` |
| 20 | + Status string `json:"status"` |
| 21 | + Filter *string `json:"filter,omitempty"` |
| 22 | + Transforms []TransformConfig `json:"transforms,omitempty"` |
| 23 | +} |
| 24 | + |
| 25 | +// DeliveryConfig represents the delivery configuration for a drain |
| 26 | +type DeliveryConfig struct { |
| 27 | + Type string `json:"type"` |
| 28 | + Endpoint any `json:"endpoint"` // Can be string or object for different delivery types |
| 29 | + Encoding string `json:"encoding"` |
| 30 | + Compression *string `json:"compression,omitempty"` |
| 31 | + Headers map[string]string `json:"headers"` |
| 32 | + Secret *string `json:"secret,omitempty"` |
| 33 | +} |
| 34 | + |
| 35 | +// SamplingConfig represents sampling configuration for a drain |
| 36 | +type SamplingConfig struct { |
| 37 | + Type string `json:"type"` |
| 38 | + Rate float64 `json:"rate"` // Must be between 0 and 1 |
| 39 | + Env *string `json:"env,omitempty"` |
| 40 | + RequestPath *string `json:"requestPath,omitempty"` |
| 41 | +} |
| 42 | + |
| 43 | +// TransformConfig represents transform configuration for a drain |
| 44 | +type TransformConfig struct { |
| 45 | + ID string `json:"id"` |
| 46 | +} |
| 47 | + |
| 48 | +// SchemaConfig represents a schema version configuration |
| 49 | +type SchemaConfig struct { |
| 50 | + Version string `json:"version"` |
| 51 | +} |
| 52 | + |
| 53 | +// CreateDrainRequest represents the request to create a drain |
| 54 | +type CreateDrainRequest struct { |
| 55 | + TeamID string `json:"-"` |
| 56 | + Name string `json:"name"` |
| 57 | + Projects string `json:"projects"` // "some" or "all" |
| 58 | + ProjectIds []string `json:"projectIds,omitempty"` |
| 59 | + Filter *string `json:"filter,omitempty"` |
| 60 | + Schemas map[string]SchemaConfig `json:"schemas"` |
| 61 | + Delivery DeliveryConfig `json:"delivery"` |
| 62 | + Sampling []SamplingConfig `json:"sampling,omitempty"` |
| 63 | + Transforms []TransformConfig `json:"transforms,omitempty"` |
| 64 | +} |
| 65 | + |
| 66 | +// UpdateDrainRequest represents the request to update a drain |
| 67 | +type UpdateDrainRequest struct { |
| 68 | + TeamID string `json:"-"` |
| 69 | + Name *string `json:"name,omitempty"` |
| 70 | + Projects *string `json:"projects,omitempty"` |
| 71 | + ProjectIds []string `json:"projectIds,omitempty"` |
| 72 | + Filter *string `json:"filter,omitempty"` |
| 73 | + Schemas map[string]SchemaConfig `json:"schemas,omitempty"` |
| 74 | + Delivery *DeliveryConfig `json:"delivery,omitempty"` |
| 75 | + Sampling []SamplingConfig `json:"sampling,omitempty"` |
| 76 | + Transforms []TransformConfig `json:"transforms,omitempty"` |
| 77 | + Status *string `json:"status,omitempty"` // "enabled" or "disabled" |
| 78 | +} |
| 79 | + |
| 80 | +// ListDrainsResponse represents the response from listing drains |
| 81 | +type ListDrainsResponse struct { |
| 82 | + Drains []Drain `json:"drains"` |
| 83 | +} |
| 84 | + |
| 85 | +// CreateDrain creates a new configurable drain |
| 86 | +func (c *Client) CreateDrain(ctx context.Context, request CreateDrainRequest) (d Drain, err error) { |
| 87 | + url := fmt.Sprintf("%s/v1/drains", c.baseURL) |
| 88 | + if c.TeamID(request.TeamID) != "" { |
| 89 | + url = fmt.Sprintf("%s?teamId=%s", url, c.TeamID(request.TeamID)) |
| 90 | + } |
| 91 | + payload := string(mustMarshal(request)) |
| 92 | + tflog.Info(ctx, "creating drain", map[string]any{ |
| 93 | + "url": url, |
| 94 | + "payload": payload, |
| 95 | + }) |
| 96 | + err = c.doRequest(clientRequest{ |
| 97 | + ctx: ctx, |
| 98 | + method: "POST", |
| 99 | + url: url, |
| 100 | + body: payload, |
| 101 | + }, &d) |
| 102 | + return d, err |
| 103 | +} |
| 104 | + |
| 105 | +// GetDrain retrieves a drain by ID |
| 106 | +func (c *Client) GetDrain(ctx context.Context, id, teamID string) (d Drain, err error) { |
| 107 | + url := fmt.Sprintf("%s/v1/drains/%s", c.baseURL, id) |
| 108 | + if c.TeamID(teamID) != "" { |
| 109 | + url = fmt.Sprintf("%s?teamId=%s", url, c.TeamID(teamID)) |
| 110 | + } |
| 111 | + tflog.Info(ctx, "reading drain", map[string]any{ |
| 112 | + "url": url, |
| 113 | + }) |
| 114 | + err = c.doRequest(clientRequest{ |
| 115 | + ctx: ctx, |
| 116 | + method: "GET", |
| 117 | + url: url, |
| 118 | + }, &d) |
| 119 | + return d, err |
| 120 | +} |
| 121 | + |
| 122 | +// UpdateDrain updates an existing drain |
| 123 | +func (c *Client) UpdateDrain(ctx context.Context, id string, request UpdateDrainRequest) (d Drain, err error) { |
| 124 | + url := fmt.Sprintf("%s/v1/drains/%s", c.baseURL, id) |
| 125 | + if c.TeamID(request.TeamID) != "" { |
| 126 | + url = fmt.Sprintf("%s?teamId=%s", url, c.TeamID(request.TeamID)) |
| 127 | + } |
| 128 | + payload := string(mustMarshal(request)) |
| 129 | + tflog.Info(ctx, "updating drain", map[string]any{ |
| 130 | + "url": url, |
| 131 | + "payload": payload, |
| 132 | + }) |
| 133 | + err = c.doRequest(clientRequest{ |
| 134 | + ctx: ctx, |
| 135 | + method: "PATCH", |
| 136 | + url: url, |
| 137 | + body: payload, |
| 138 | + }, &d) |
| 139 | + return d, err |
| 140 | +} |
| 141 | + |
| 142 | +// DeleteDrain deletes a drain by ID |
| 143 | +func (c *Client) DeleteDrain(ctx context.Context, id, teamID string) error { |
| 144 | + url := fmt.Sprintf("%s/v1/drains/%s", c.baseURL, id) |
| 145 | + if c.TeamID(teamID) != "" { |
| 146 | + url = fmt.Sprintf("%s?teamId=%s", url, c.TeamID(teamID)) |
| 147 | + } |
| 148 | + tflog.Info(ctx, "deleting drain", map[string]any{ |
| 149 | + "url": url, |
| 150 | + }) |
| 151 | + return c.doRequest(clientRequest{ |
| 152 | + ctx: ctx, |
| 153 | + method: "DELETE", |
| 154 | + url: url, |
| 155 | + }, nil) |
| 156 | +} |
| 157 | + |
| 158 | +// ListDrains retrieves all drains for a team |
| 159 | +func (c *Client) ListDrains(ctx context.Context, teamID string) (response ListDrainsResponse, err error) { |
| 160 | + url := fmt.Sprintf("%s/v1/drains", c.baseURL) |
| 161 | + if c.TeamID(teamID) != "" { |
| 162 | + url = fmt.Sprintf("%s?teamId=%s", url, c.TeamID(teamID)) |
| 163 | + } |
| 164 | + tflog.Info(ctx, "listing drains", map[string]any{ |
| 165 | + "url": url, |
| 166 | + }) |
| 167 | + err = c.doRequest(clientRequest{ |
| 168 | + ctx: ctx, |
| 169 | + method: "GET", |
| 170 | + url: url, |
| 171 | + }, &response) |
| 172 | + return response, err |
| 173 | +} |
0 commit comments