Skip to content

Commit

Permalink
chore/concurrency-limit-support (#360)
Browse files Browse the repository at this point in the history
* Initial commit

* Comment

* Add client

* Refactor

* Rest of definition plus test

* Docs

* Add back images

* Fix test

* Fix test based on current usage

* Update internal/provider/resources/concurrency_limit.go

Co-authored-by: Mitchell Nielsen <[email protected]>

* Update internal/provider/resources/concurrency_limit.go

Co-authored-by: Mitchell Nielsen <[email protected]>

* Update internal/provider/resources/concurrency_limit.go

Co-authored-by: Mitchell Nielsen <[email protected]>

* Update internal/provider/resources/concurrency_limit_test.go

Co-authored-by: Mitchell Nielsen <[email protected]>

* Update internal/provider/resources/concurrency_limit.go

Co-authored-by: Mitchell Nielsen <[email protected]>

* Update internal/client/concurrency_limits.go

Co-authored-by: Mitchell Nielsen <[email protected]>

* Generate Terraform Docs

---------

Co-authored-by: Mitchell Nielsen <[email protected]>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 30, 2025
1 parent d95dc3a commit c2f14e1
Show file tree
Hide file tree
Showing 8 changed files with 484 additions and 1 deletion.
32 changes: 32 additions & 0 deletions docs/resources/concurrency_limit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "prefect_concurrency_limit Resource - prefect"
subcategory: ""
description: |-
The resource concurrency_limit represents a concurrency limit. Concurrency limits allow you to manage execution efficiently, controlling how many tasks, flows, or other operations can run simultaneously. They are ideal for optimizing resource usage, preventing bottlenecks, and customizing task execution.
---

# prefect_concurrency_limit (Resource)

The resource `concurrency_limit` represents a concurrency limit. Concurrency limits allow you to manage execution efficiently, controlling how many tasks, flows, or other operations can run simultaneously. They are ideal for optimizing resource usage, preventing bottlenecks, and customizing task execution.



<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `concurrency_limit` (Number) The concurrency limit.
- `tag` (String) A tag the concurrency limit is applied to.

### Optional

- `account_id` (String) Account ID (UUID)
- `workspace_id` (String) Workspace ID (UUID)

### Read-Only

- `created` (String) Timestamp of when the resource was created (RFC3339)
- `id` (String) Concurrency limit ID (UUID)
- `updated` (String) Timestamp of when the resource was updated (RFC3339)
1 change: 1 addition & 0 deletions internal/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type PrefectClient interface {
BlockSchemas(accountID uuid.UUID, workspaceID uuid.UUID) (BlockSchemaClient, error)
BlockTypes(accountID uuid.UUID, workspaceID uuid.UUID) (BlockTypeClient, error)
Collections(accountID uuid.UUID, workspaceID uuid.UUID) (CollectionsClient, error)
ConcurrencyLimits(accountID uuid.UUID, workspaceID uuid.UUID) (ConcurrencyLimitsClient, error)
Deployments(accountID uuid.UUID, workspaceID uuid.UUID) (DeploymentsClient, error)
DeploymentAccess(accountID uuid.UUID, workspaceID uuid.UUID) (DeploymentAccessClient, error)
DeploymentSchedule(accountID uuid.UUID, workspaceID uuid.UUID) (DeploymentScheduleClient, error)
Expand Down
25 changes: 25 additions & 0 deletions internal/api/concurrency_limits.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package api

import (
"context"
)

// ConcurrencyLimitsClient is a client for working with concurrency limits.
type ConcurrencyLimitsClient interface {
Create(ctx context.Context, concurrencyLimit ConcurrencyLimitCreate) (*ConcurrencyLimit, error)
Read(ctx context.Context, concurrencyLimitID string) (*ConcurrencyLimit, error)
Delete(ctx context.Context, concurrencyLimitID string) error
}

// ConcurrencyLimit is a representation of a concurrency limit.
type ConcurrencyLimit struct {
BaseModel
Tag string `json:"tag"`
ConcurrencyLimit int64 `json:"concurrency_limit"`
}

// ConcurrencyLimitCreate is a subset of ConcurrencyLimit used when creating concurrency limits.
type ConcurrencyLimitCreate struct {
Tag string `json:"tag"`
ConcurrencyLimit int64 `json:"concurrency_limit"`
}
95 changes: 95 additions & 0 deletions internal/client/concurrency_limits.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package client

import (
"context"
"fmt"
"net/http"

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

var _ = api.ConcurrencyLimitsClient(&ConcurrencyLimitsClient{})

// ConcurrencyLimitsClient is a client for working with concurrency limits.
type ConcurrencyLimitsClient struct {
hc *http.Client
routePrefix string
apiKey string
}

// ConcurrencyLimits returns a ConcurrencyLimitsClient.
//
//nolint:ireturn // required to support PrefectClient mocking
func (c *Client) ConcurrencyLimits(accountID uuid.UUID, workspaceID uuid.UUID) (api.ConcurrencyLimitsClient, error) {
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 &ConcurrencyLimitsClient{
hc: c.hc,
routePrefix: getWorkspaceScopedURL(c.endpoint, accountID, workspaceID, "concurrency_limits"),
apiKey: c.apiKey,
}, nil
}

// Create creates a new concurrency limit.
func (c *ConcurrencyLimitsClient) Create(ctx context.Context, data api.ConcurrencyLimitCreate) (*api.ConcurrencyLimit, error) {
cfg := requestConfig{
method: http.MethodPost,
url: c.routePrefix + "/",
body: &data,
apiKey: c.apiKey,
successCodes: successCodesStatusOK,
}

var concurrencyLimit api.ConcurrencyLimit
if err := requestWithDecodeResponse(ctx, c.hc, cfg, &concurrencyLimit); err != nil {
return nil, fmt.Errorf("failed to create concurrency limit: %w", err)
}

return &concurrencyLimit, nil
}

// Read returns a concurrency limit.
func (c *ConcurrencyLimitsClient) Read(ctx context.Context, concurrencyLimitID string) (*api.ConcurrencyLimit, error) {
cfg := requestConfig{
method: http.MethodGet,
url: fmt.Sprintf("%s/%s", c.routePrefix, concurrencyLimitID),
apiKey: c.apiKey,
successCodes: successCodesStatusOK,
}

var concurrencyLimit api.ConcurrencyLimit
if err := requestWithDecodeResponse(ctx, c.hc, cfg, &concurrencyLimit); err != nil {
return nil, fmt.Errorf("failed to get concurrency limit: %w", err)
}

return &concurrencyLimit, nil
}

// Delete deletes a concurrency limit.
func (c *ConcurrencyLimitsClient) Delete(ctx context.Context, concurrencyLimitID string) error {
cfg := requestConfig{
method: http.MethodDelete,
url: fmt.Sprintf("%s/%s", c.routePrefix, concurrencyLimitID),
apiKey: c.apiKey,
successCodes: successCodesStatusOK,
}

resp, err := request(ctx, c.hc, cfg)
if err != nil {
return fmt.Errorf("failed to delete concurrency limit: %w", err)
}
defer resp.Body.Close()

return nil
}
2 changes: 1 addition & 1 deletion internal/provider/datasources/account_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestAccDatasource_account_role_defaults(t *testing.T) {
}

// Default account role names - these exist in every account
defaultAccountRoles := []defaultAccountRole{{"Admin", "39"}, {"Member", "11"}, {"Owner", "41"}}
defaultAccountRoles := []defaultAccountRole{{"Admin", "42"}, {"Member", "12"}, {"Owner", "44"}}

testSteps := []resource.TestStep{}

Expand Down
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ func (p *PrefectProvider) Resources(_ context.Context) []func() resource.Resourc
resources.NewAutomationResource,
resources.NewBlockAccessResource,
resources.NewBlockResource,
resources.NewConcurrencyLimitResource,
resources.NewDeploymentAccessResource,
resources.NewDeploymentResource,
resources.NewDeploymentScheduleResource,
Expand Down
Loading

0 comments on commit c2f14e1

Please sign in to comment.