Skip to content
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

feat: Add Webhook resource & data source #251

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,5 @@ require (
gopkg.in/yaml.v2 v2.3.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/prefecthq/terraform-provider-prefect => /Users/adeeb.rahman/Documents/dev/personal/terraform-provider-prefect
1 change: 1 addition & 0 deletions internal/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ type PrefectClient interface {
WorkPools(accountID uuid.UUID, workspaceID uuid.UUID) (WorkPoolsClient, error)
Variables(accountID uuid.UUID, workspaceID uuid.UUID) (VariablesClient, error)
ServiceAccounts(accountID uuid.UUID) (ServiceAccountsClient, error)
Webhooks(accountID, workspaceID uuid.UUID) (WebhooksClient, error)
}
57 changes: 57 additions & 0 deletions internal/api/webhooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package api

import (
"context"
"time"

"github.com/google/uuid"
)

type WebhooksClient interface {
Create(ctx context.Context, accountID, workspaceID string, request WebhookCreateRequest) (*Webhook, error)
Get(ctx context.Context, accountID, workspaceID, webhookID string) (*Webhook, error)
List(ctx context.Context, accountID, workspaceID string) ([]*Webhook, error)
Update(ctx context.Context, accountID, workspaceID, webhookID string, request WebhookUpdateRequest) error
Delete(ctx context.Context, accountID, workspaceID, webhookID string) error
}

/*** REQUEST DATA STRUCTS ***/

type WebhookCreateRequest struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Enabled bool `json:"enabled"`
Template string `json:"template"`
}

type WebhookUpdateRequest struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Enabled bool `json:"enabled"`
Template string `json:"template"`
}

/*** RESPONSE DATA STRUCTS ***/

type Webhook struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Enabled bool `json:"enabled"`
Template string `json:"template"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
AccountID uuid.UUID `json:"account"`
WorkspaceID uuid.UUID `json:"workspace"`
Slug string `json:"slug"`
}

type ErrorResponse struct {
Detail []ErrorDetail `json:"detail"`
}

type ErrorDetail struct {
Loc []string `json:"loc"`
Msg string `json:"msg"`
Type string `json:"type"`
}
172 changes: 172 additions & 0 deletions internal/client/webhooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package client

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"

"github.com/google/uuid"
"github.com/prefecthq/terraform-provider-prefect/internal/api"
)

type webhooksClient struct {
hc *http.Client
apiKey string
routePrefix string
}

func (c *Client) Webhooks(accountID, workspaceID uuid.UUID) (api.WebhooksClient, error) {
if c.apiKey == "" {
return nil, fmt.Errorf("apiKey is not set")
}

if c.endpoint == "" {
return nil, fmt.Errorf("endpoint is not set")
}

routePrefix := fmt.Sprintf("%s/api/accounts/%s/workspaces/%s/webhooks", c.endpoint, accountID, workspaceID)

return &webhooksClient{
hc: c.hc,
apiKey: c.apiKey,
routePrefix: routePrefix,
}, nil
}

func (wc *webhooksClient) Create(ctx context.Context, accountID, workspaceID string, request api.WebhookCreateRequest) (*api.Webhook, error) {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(&request); err != nil {
return nil, fmt.Errorf("failed to encode request data: %w", err)
}

req, err := http.NewRequestWithContext(ctx, http.MethodPost, wc.routePrefix+"/", &buf)
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}

setDefaultHeaders(req, wc.apiKey)

resp, err := wc.hc.Do(req)
if err != nil {
return nil, fmt.Errorf("http error: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusCreated {
errorBody, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("status code %s, error=%s", resp.Status, errorBody)
}

var response api.Webhook
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}

return &response, nil
}

func (wc *webhooksClient) Get(ctx context.Context, accountID, workspaceID, webhookID string) (*api.Webhook, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, wc.routePrefix+"/"+webhookID, http.NoBody)
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}

setDefaultHeaders(req, wc.apiKey)

resp, err := wc.hc.Do(req)
if err != nil {
return nil, fmt.Errorf("http error: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
errorBody, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("status code %s, error=%s", resp.Status, errorBody)
}

var response api.Webhook
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}

return &response, nil
}

func (wc *webhooksClient) Update(ctx context.Context, accountID, workspaceID, webhookID string, request api.WebhookUpdateRequest) error {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(&request); err != nil {
return fmt.Errorf("failed to encode request data: %w", err)
}

req, err := http.NewRequestWithContext(ctx, http.MethodPut, wc.routePrefix+"/"+webhookID, &buf)
if err != nil {
return fmt.Errorf("error creating request: %w", err)
}

setDefaultHeaders(req, wc.apiKey)

resp, err := wc.hc.Do(req)
if err != nil {
return fmt.Errorf("http error: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
errorBody, _ := io.ReadAll(resp.Body)
return fmt.Errorf("status code %s, error=%s", resp.Status, errorBody)
}

return nil
}

func (wc *webhooksClient) Delete(ctx context.Context, accountID, workspaceID, webhookID string) error {
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, wc.routePrefix+"/"+webhookID, http.NoBody)
if err != nil {
return fmt.Errorf("error creating request: %w", err)
}

setDefaultHeaders(req, wc.apiKey)

resp, err := wc.hc.Do(req)
if err != nil {
return fmt.Errorf("http error: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
errorBody, _ := io.ReadAll(resp.Body)
return fmt.Errorf("status code %s, error=%s", resp.Status, errorBody)
}

return nil
}

func (w *webhooksClient) List(ctx context.Context, accountID, workspaceID string) ([]*api.Webhook, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("%s/", w.routePrefix), nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}

setDefaultHeaders(req, w.apiKey)

resp, err := w.hc.Do(req)
if err != nil {
return nil, fmt.Errorf("http error: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
errorBody, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("status code %s, error=%s", resp.Status, errorBody)
}

var webhooks []*api.Webhook
if err := json.NewDecoder(resp.Body).Decode(&webhooks); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}

return webhooks, nil
}
Loading
Loading