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

Add BlockSchema HTTP client and struct model #180

Merged
merged 8 commits into from
May 31, 2024
2 changes: 1 addition & 1 deletion docs/data-sources/account_member.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
page_title: "prefect_account_member Data Source - prefect"
subcategory: ""
description: |-
Get information about an existing Account Member (user) by their email.
Get information about an existing Account Member (user) by their email.

Use this data source to obtain user or actor IDs to manage Workspace Access.
---
Expand Down
4 changes: 2 additions & 2 deletions internal/api/block_documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type BlockDocument struct {
Name *string `json:"name"` // names are optional for anonymous blocks
Data string `json:"data"`

BlockSchemaID uuid.UUID `json:"block_schema_id"`
// BlockSchema *BlockSchema `json:"block_schema"`
BlockSchemaID uuid.UUID `json:"block_schema_id"`
BlockSchema *BlockSchema `json:"block_schema"`
mitchnielsen marked this conversation as resolved.
Show resolved Hide resolved

BlockTypeID uuid.UUID `json:"block_type_id"`
BlockTypeName *string `json:"block_type_name"`
Expand Down
38 changes: 38 additions & 0 deletions internal/api/block_schemas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package api

import (
"context"

"github.com/google/uuid"
)

// BlockSchemaClient is a client for working with block schemas.
type BlockSchemaClient interface {
List(ctx context.Context, blockTypeIDs []uuid.UUIDs) ([]*BlockSchema, error)
}

// BlockSchema is a representation of a block schema.
type BlockSchema struct {
BaseModel
BlockType
mitchnielsen marked this conversation as resolved.
Show resolved Hide resolved

Checksum string `json:"checksum"`
BlockTypeID uuid.UUID `json:"block_type_id"`
Capabilities []string `json:"capabilities"`
Version string `json:"version"`
Fields interface{} `json:"fields"`
}

// BlockSchemaFilter defines the search filter payload
// when searching for block schemas by slug.
type BlockSchemaFilter struct {
// BlockSchemas
BlockSchemas struct {
BlockTypeID struct {
Any []uuid.UUIDs `json:"any_"`
} `json:"block_type_id"`
BlockCapabilities struct {
All []string `json:"all_"`
} `json:"block_capabilities"`
} `json:"block_schemas"`
}
1 change: 1 addition & 0 deletions internal/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type PrefectClient interface {
AccountMemberships(accountID uuid.UUID) (AccountMembershipsClient, error)
AccountRoles(accountID uuid.UUID) (AccountRolesClient, error)
BlockDocuments(accountID uuid.UUID, workspaceID uuid.UUID) (BlockDocumentClient, error)
BlockSchemas(accountID uuid.UUID, workspaceID uuid.UUID) (BlockSchemaClient, error)
BlockTypes(accountID uuid.UUID, workspaceID uuid.UUID) (BlockTypeClient, error)
Collections() (CollectionsClient, error)
Teams(accountID uuid.UUID) (TeamsClient, error)
Expand Down
75 changes: 75 additions & 0 deletions internal/client/block_schemas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package client

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

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

// BlockSchemaClient is a client for working with block schemas.
type BlockSchemaClient struct {
hc *http.Client
routePrefix string
apiKey string
}

// BlockSchemas returns a BlockSchemaClient.
//
//nolint:ireturn // required to support PrefectClient mocking
func (c *Client) BlockSchemas(accountID uuid.UUID, workspaceID uuid.UUID) (api.BlockSchemaClient, error) {
if accountID == uuid.Nil {
accountID = c.defaultAccountID
}
if workspaceID == uuid.Nil {
workspaceID = c.defaultWorkspaceID
}

return &BlockSchemaClient{
hc: c.hc,
apiKey: c.apiKey,
routePrefix: getWorkspaceScopedURL(c.endpoint, accountID, workspaceID, "block_schemas"),
}, nil
}

// List gets a list of BlockSchemas for a given list of block type slugs.
func (c *BlockSchemaClient) List(ctx context.Context, blockTypeIDs []uuid.UUIDs) ([]*api.BlockSchema, error) {
filterQuery := &api.BlockSchemaFilter{}
filterQuery.BlockSchemas.BlockTypeID.Any = blockTypeIDs

var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(&filterQuery); err != nil {
return nil, fmt.Errorf("failed to encode create payload data: %w", err)
}

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

setDefaultHeaders(req, c.apiKey)

resp, err := c.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 blockSchemas []*api.BlockSchema
if err := json.NewDecoder(resp.Body).Decode(&blockSchemas); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}

return blockSchemas, nil
}
Loading