Skip to content

Commit

Permalink
chore: consistently validate workspace_id
Browse files Browse the repository at this point in the history
Consistently checks if `workspace_id` is configured for endpoints that
use that field.

Adds a helper function to consistently provide this check.

Closes https://linear.app/prefect/issue/PLA-405/consistently-handle-resources-that-require-a-workspace-id
  • Loading branch information
mitchnielsen committed Dec 13, 2024
1 parent 3fa3161 commit e178f5c
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 23 deletions.
5 changes: 2 additions & 3 deletions internal/client/automations.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

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

var _ = api.AutomationsClient(&AutomationsClient{})
Expand All @@ -30,8 +29,8 @@ func (c *Client) Automations(accountID uuid.UUID, workspaceID uuid.UUID) (api.Au
workspaceID = c.defaultWorkspaceID
}

if helpers.IsCloudEndpoint(c.endpoint) && (accountID == uuid.Nil || workspaceID == uuid.Nil) {
return nil, fmt.Errorf("prefect Cloud endpoints require an account_id and workspace_id to be set")
if err := validateCloudEndpoint(c.endpoint, accountID, workspaceID); err != nil {
return nil, err
}

return &AutomationsClient{
Expand Down
5 changes: 2 additions & 3 deletions internal/client/block_documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

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

var _ = api.BlockDocumentClient(&BlockDocumentClient{})
Expand All @@ -31,8 +30,8 @@ func (c *Client) BlockDocuments(accountID uuid.UUID, workspaceID uuid.UUID) (api
workspaceID = c.defaultWorkspaceID
}

if helpers.IsCloudEndpoint(c.endpoint) && (accountID == uuid.Nil || workspaceID == uuid.Nil) {
return nil, fmt.Errorf("prefect Cloud endpoints require an account_id and workspace_id to be set on either the provider or the resource")
if err := validateCloudEndpoint(c.endpoint, accountID, workspaceID); err != nil {
return nil, err
}

return &BlockDocumentClient{
Expand Down
5 changes: 2 additions & 3 deletions internal/client/block_schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

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

// BlockSchemaClient is a client for working with block schemas.
Expand All @@ -28,8 +27,8 @@ func (c *Client) BlockSchemas(accountID uuid.UUID, workspaceID uuid.UUID) (api.B
workspaceID = c.defaultWorkspaceID
}

if helpers.IsCloudEndpoint(c.endpoint) && (accountID == uuid.Nil || workspaceID == uuid.Nil) {
return nil, fmt.Errorf("prefect Cloud endpoints require an account_id and workspace_id to be set on either the provider or the resource")
if err := validateCloudEndpoint(c.endpoint, accountID, workspaceID); err != nil {
return nil, err
}

return &BlockSchemaClient{
Expand Down
5 changes: 2 additions & 3 deletions internal/client/block_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

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

var _ = api.BlockTypeClient(&BlockTypeClient{})
Expand All @@ -30,8 +29,8 @@ func (c *Client) BlockTypes(accountID uuid.UUID, workspaceID uuid.UUID) (api.Blo
workspaceID = c.defaultWorkspaceID
}

if helpers.IsCloudEndpoint(c.endpoint) && (accountID == uuid.Nil || workspaceID == uuid.Nil) {
return nil, fmt.Errorf("prefect Cloud endpoints require an account_id and workspace_id to be set on either the provider or the resource")
if err := validateCloudEndpoint(c.endpoint, accountID, workspaceID); err != nil {
return nil, err
}

return &BlockTypeClient{
Expand Down
4 changes: 2 additions & 2 deletions internal/client/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func (c *Client) Collections(accountID, workspaceID uuid.UUID) (api.CollectionsC
workspaceID = c.defaultWorkspaceID
}

if helpers.IsCloudEndpoint(c.endpoint) && (accountID == uuid.Nil || workspaceID == uuid.Nil) {
return nil, fmt.Errorf("prefect Cloud endpoints require an account_id and workspace_id to be set on either the provider or the resource")
if err := validateCloudEndpoint(c.endpoint, accountID, workspaceID); err != nil {
return nil, err
}

return &CollectionsClient{
Expand Down
5 changes: 2 additions & 3 deletions internal/client/deployment_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

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

var _ = api.DeploymentAccessClient(&DeploymentAccessClient{})
Expand All @@ -30,8 +29,8 @@ func (c *Client) DeploymentAccess(accountID uuid.UUID, workspaceID uuid.UUID) (a
workspaceID = c.defaultWorkspaceID
}

if helpers.IsCloudEndpoint(c.endpoint) && (accountID == uuid.Nil || workspaceID == uuid.Nil) {
return nil, fmt.Errorf("prefect Cloud endpoints require an account_id and workspace_id to be set on either the provider or the resource")
if err := validateCloudEndpoint(c.endpoint, accountID, workspaceID); err != nil {
return nil, err
}

return &DeploymentAccessClient{
Expand Down
5 changes: 2 additions & 3 deletions internal/client/deployment_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

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

var _ = api.DeploymentScheduleClient(&DeploymentScheduleClient{})
Expand All @@ -30,8 +29,8 @@ func (c *Client) DeploymentSchedule(accountID, workspaceID uuid.UUID) (api.Deplo
workspaceID = c.defaultWorkspaceID
}

if helpers.IsCloudEndpoint(c.endpoint) && (accountID == uuid.Nil || workspaceID == uuid.Nil) {
return nil, fmt.Errorf("prefect Cloud endpoints require an account_id and workspace_id to be set on either the provider or the resource")
if err := validateCloudEndpoint(c.endpoint, accountID, workspaceID); err != nil {
return nil, err
}

return &DeploymentScheduleClient{
Expand Down
4 changes: 4 additions & 0 deletions internal/client/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func (c *Client) Deployments(accountID uuid.UUID, workspaceID uuid.UUID) (api.De
workspaceID = c.defaultWorkspaceID
}

if err := validateCloudEndpoint(c.endpoint, accountID, workspaceID); err != nil {
return nil, err
}

return &DeploymentsClient{
hc: c.hc,
routePrefix: getWorkspaceScopedURL(c.endpoint, accountID, workspaceID, "deployments"),
Expand Down
4 changes: 4 additions & 0 deletions internal/client/flows.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func (c *Client) Flows(accountID uuid.UUID, workspaceID uuid.UUID) (api.FlowsCli
workspaceID = c.defaultWorkspaceID
}

if err := validateCloudEndpoint(c.endpoint, accountID, workspaceID); err != nil {
return nil, err
}

return &FlowsClient{
hc: c.hc,
routePrefix: getWorkspaceScopedURL(c.endpoint, accountID, workspaceID, "flows"),
Expand Down
11 changes: 11 additions & 0 deletions internal/client/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

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

// getAccountScopedURL constructs a URL for an account-scoped route.
Expand Down Expand Up @@ -64,6 +65,16 @@ func setDefaultHeaders(request *http.Request, apiKey string) {
request.Header.Set("Accept", "application/json")
}

// validateCloudEndpoint validates that proper configuration is provided
// when the endpoint points to Prefect Cloud.
func validateCloudEndpoint(endpoint string, accountID, workspaceID uuid.UUID) error {
if helpers.IsCloudEndpoint(endpoint) && (accountID == uuid.Nil || workspaceID == uuid.Nil) {
return fmt.Errorf("prefect Cloud endpoints require an account_id and workspace_id to be set on either the provider or the resource")
}

return nil
}

// requestConfig is a configuration object for an HTTP request.
type requestConfig struct {
method string
Expand Down
5 changes: 5 additions & 0 deletions internal/client/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ func (c *Client) Variables(accountID uuid.UUID, workspaceID uuid.UUID) (api.Vari
if accountID == uuid.Nil {
accountID = c.defaultAccountID
}

if workspaceID == uuid.Nil {
workspaceID = c.defaultWorkspaceID
}

if err := validateCloudEndpoint(c.endpoint, accountID, workspaceID); err != nil {
return nil, err
}

return &VariablesClient{
hc: c.hc,
apiKey: c.apiKey,
Expand Down
5 changes: 2 additions & 3 deletions internal/client/work_pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

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

var _ = api.WorkPoolsClient(&WorkPoolsClient{})
Expand All @@ -31,8 +30,8 @@ func (c *Client) WorkPools(accountID uuid.UUID, workspaceID uuid.UUID) (api.Work
workspaceID = c.defaultWorkspaceID
}

if helpers.IsCloudEndpoint(c.endpoint) && (accountID == uuid.Nil || workspaceID == uuid.Nil) {
return nil, fmt.Errorf("prefect Cloud endpoints require an account_id and workspace_id to be set on either the provider or the resource")
if err := validateCloudEndpoint(c.endpoint, accountID, workspaceID); err != nil {
return nil, err
}

return &WorkPoolsClient{
Expand Down

0 comments on commit e178f5c

Please sign in to comment.