diff --git a/docs/resources/work_pool.md b/docs/resources/work_pool.md index e84c06bd..d6deaf34 100644 --- a/docs/resources/work_pool.md +++ b/docs/resources/work_pool.md @@ -60,7 +60,7 @@ resource "prefect_work_pool" "example" { - `description` (String) Description of the work pool - `paused` (Boolean) Whether this work pool is paused - `type` (String) Type of the work pool, eg. kubernetes, ecs, process, etc. -- `workspace_id` (String) Workspace ID (UUID), defaults to the workspace set in the provider +- `workspace_id` (String) Workspace ID (UUID), defaults to the workspace set in the provider. In Prefect Cloud, either the `work_pool` resource or the provider's `workspace_id` must be set. ### Read-Only diff --git a/internal/client/work_pools.go b/internal/client/work_pools.go index b4d4b1af..204f466a 100644 --- a/internal/client/work_pools.go +++ b/internal/client/work_pools.go @@ -10,6 +10,7 @@ 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{}) @@ -28,10 +29,15 @@ func (c *Client) WorkPools(accountID uuid.UUID, workspaceID uuid.UUID) (api.Work if accountID == uuid.Nil { accountID = c.defaultAccountID } + if workspaceID == uuid.Nil { 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") + } + return &WorkPoolsClient{ hc: c.hc, apiKey: c.apiKey, diff --git a/internal/provider/resources/work_pool.go b/internal/provider/resources/work_pool.go index 1a2f9bd6..190b80d1 100644 --- a/internal/provider/resources/work_pool.go +++ b/internal/provider/resources/work_pool.go @@ -115,7 +115,7 @@ func (r *WorkPoolResource) Schema(_ context.Context, _ resource.SchemaRequest, r }, "workspace_id": schema.StringAttribute{ CustomType: customtypes.UUIDType{}, - Description: "Workspace ID (UUID), defaults to the workspace set in the provider", + Description: "Workspace ID (UUID), defaults to the workspace set in the provider. In Prefect Cloud, either the `work_pool` resource or the provider's `workspace_id` must be set.", Optional: true, }, "name": schema.StringAttribute{ diff --git a/internal/provider/resources/work_pool_test.go b/internal/provider/resources/work_pool_test.go index 8eec9c4f..a2cb3155 100644 --- a/internal/provider/resources/work_pool_test.go +++ b/internal/provider/resources/work_pool_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "regexp" "strings" "testing" @@ -15,6 +16,13 @@ import ( "github.com/prefecthq/terraform-provider-prefect/internal/testutils" ) +const workPoolWithoutWorkspaceID = ` +resource "prefect_work_pool" "invalid_work_pool" { + name = "invalid-work-pool" + type = "kubernetes" +} +` + func fixtureAccWorkPoolCreate(workspace, workspaceName, name, poolType, baseJobTemplate string, paused bool) string { return fmt.Sprintf(` %s @@ -59,6 +67,11 @@ func TestAccResource_work_pool(t *testing.T) { ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories, PreCheck: func() { testutils.AccTestPreCheck(t) }, Steps: []resource.TestStep{ + { + // Check that workspace_id missing causes a failure + Config: workPoolWithoutWorkspaceID, + ExpectError: regexp.MustCompile(".*require an account_id and workspace_id to be set.*"), + }, { // Check creation + existence of the work pool resource Config: fixtureAccWorkPoolCreate(workspace, workspaceName, randomName, poolType, baseJobTemplate, true),