Skip to content

Commit

Permalink
Require workspace ID for work pools in Cloud (#282)
Browse files Browse the repository at this point in the history
* Require workspace ID for work pools in Cloud

In Cloud, workspace_id is required. It's marked as optional to support
OSS.

Related to #281

Related to https://linear.app/prefect/issue/PLA-399/prefect-terraform-provider-errors-with-404-when-trying-to-create-a-new

* Add test case
  • Loading branch information
mitchnielsen authored Oct 16, 2024
1 parent e237d13 commit a7d9465
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/resources/work_pool.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions internal/client/work_pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resources/work_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
13 changes: 13 additions & 0 deletions internal/provider/resources/work_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"regexp"
"strings"
"testing"

Expand All @@ -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
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit a7d9465

Please sign in to comment.