Skip to content

Commit

Permalink
feat(Deployments): support StorageDocumentId (#262)
Browse files Browse the repository at this point in the history
* feat(Deployments): support storage_document_id

Adds support for `storage_document_id` in Deployments.

Related to https://linear.app/prefect/issue/PLA-185/support-for-deployments-resource

* Skip specify StorageDocumentID if unset

When a StorageDocumentID, which is of type UUID, is not provided, it
defaulted to a null UUID, which is `0000...`. This meant that if you
didn't provide a StorageDocumentID, you wouldn't be able to create a
deployment because it couldn't find a storage document by that null
UUID.

This uses pointers instead, so if it's not provided we get `nil` instead
of a "null" UUID (`0000...`).

* Use an ephemeral storage block

Rather than the static storage block created for testing, this test
suite will now create an ephemeral storage block ("github-repository"
type) and we'll refer to its ID for the Deployment's StorageDocumentID
field.

* Use docs template for Deployments, update example

- Uses the terraform-docs template for Deployment resources
- Updates the example to show storage_document_id

* Replace reference to 'mitch' in examples
  • Loading branch information
mitchnielsen authored Oct 1, 2024
1 parent b64c032 commit 60fb1f6
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 10 deletions.
24 changes: 19 additions & 5 deletions docs/resources/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ resource "prefect_workspace" "workspace" {
name = "my-workspace"
}
resource "prefect_block" "demo_github_repository" {
name = "demo-github-repository"
type_slug = "github-repository"
data = jsonencode({
"repository_url" : "https://github.com/foo/bar",
"reference" : "main"
})
workspace_id = prefect_workspace.workspace.id
}
resource "prefect_flow" "flow" {
name = "my-flow"
workspace_id = prefect_workspace.workspace.id
Expand All @@ -40,11 +52,12 @@ resource "prefect_deployment" "deployment" {
"some-parameter" : "some-value",
"some-parameter2" : "some-value2"
})
path = "./foo/bar"
paused = false
version = "v1.1.1"
work_pool_name = "mitch-testing-pool"
work_queue_name = "default"
path = "./foo/bar"
paused = false
storage_document_id = prefect_block.test_gh_repository.id
version = "v1.1.1"
work_pool_name = "some-testing-pool"
work_queue_name = "default"
}
```

Expand All @@ -67,6 +80,7 @@ resource "prefect_deployment" "deployment" {
- `parameters` (String) Parameters for flow runs scheduled by the deployment.
- `path` (String) The path to the working directory for the workflow, relative to remote storage or an absolute path.
- `paused` (Boolean) Whether or not the deployment is paused.
- `storage_document_id` (String) ID of the associated storage document (UUID)
- `tags` (List of String) Tags associated with the deployment
- `version` (String) An optional version for the deployment.
- `work_pool_name` (String) The name of the deployment's work pool.
Expand Down
23 changes: 18 additions & 5 deletions examples/resources/prefect_deployment/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ resource "prefect_workspace" "workspace" {
name = "my-workspace"
}

resource "prefect_block" "demo_github_repository" {
name = "demo-github-repository"
type_slug = "github-repository"

data = jsonencode({
"repository_url" : "https://github.com/foo/bar",
"reference" : "main"
})

workspace_id = prefect_workspace.workspace.id
}

resource "prefect_flow" "flow" {
name = "my-flow"
workspace_id = prefect_workspace.workspace.id
Expand All @@ -25,10 +37,11 @@ resource "prefect_deployment" "deployment" {
"some-parameter" : "some-value",
"some-parameter2" : "some-value2"
})
path = "./foo/bar"
paused = false
version = "v1.1.1"
work_pool_name = "mitch-testing-pool"
work_queue_name = "default"
path = "./foo/bar"
paused = false
storage_document_id = prefect_block.test_gh_repository.id
version = "v1.1.1"
work_pool_name = "some-testing-pool"
work_queue_name = "default"
}

3 changes: 3 additions & 0 deletions internal/api/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Deployment struct {
Parameters map[string]interface{} `json:"parameters,omitempty"`
Path string `json:"path"`
Paused bool `json:"paused"`
StorageDocumentID uuid.UUID `json:"storage_document_id,omitempty"`
Tags []string `json:"tags"`
Version string `json:"version,omitempty"`
WorkPoolName string `json:"work_pool_name,omitempty"`
Expand All @@ -49,6 +50,7 @@ type DeploymentCreate struct {
Parameters map[string]interface{} `json:"parameters,omitempty"`
Path string `json:"path,omitempty"`
Paused bool `json:"paused,omitempty"`
StorageDocumentID *uuid.UUID `json:"storage_document_id,omitempty"`
Tags []string `json:"tags,omitempty"`
Version string `json:"version,omitempty"`
WorkPoolName string `json:"work_pool_name,omitempty"`
Expand All @@ -65,6 +67,7 @@ type DeploymentUpdate struct {
Parameters map[string]interface{} `json:"parameters,omitempty"`
Path string `json:"path,omitempty"`
Paused bool `json:"paused,omitempty"`
StorageDocumentID *uuid.UUID `json:"storage_document_id,omitempty"`
Tags []string `json:"tags,omitempty"`
Version string `json:"version,omitempty"`
WorkPoolName string `json:"work_pool_name,omitempty"`
Expand Down
10 changes: 10 additions & 0 deletions internal/provider/resources/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type DeploymentResourceModel struct {
Parameters jsontypes.Normalized `tfsdk:"parameters"`
Path types.String `tfsdk:"path"`
Paused types.Bool `tfsdk:"paused"`
StorageDocumentID customtypes.UUIDValue `tfsdk:"storage_document_id"`
Tags types.List `tfsdk:"tags"`
Version types.String `tfsdk:"version"`
WorkPoolName types.String `tfsdk:"work_pool_name"`
Expand Down Expand Up @@ -156,6 +157,12 @@ func (r *DeploymentResource) Schema(_ context.Context, _ resource.SchemaRequest,
Computed: true,
Default: booldefault.StaticBool(false),
},
"storage_document_id": schema.StringAttribute{
Optional: true,
Computed: true,
CustomType: customtypes.UUIDType{},
Description: "ID of the associated storage document (UUID)",
},
"manifest_path": schema.StringAttribute{
Description: "The path to the flow's manifest file, relative to the chosen storage.",
Optional: true,
Expand Down Expand Up @@ -249,6 +256,7 @@ func copyDeploymentToModel(ctx context.Context, deployment *api.Deployment, mode
model.Name = types.StringValue(deployment.Name)
model.Path = types.StringValue(deployment.Path)
model.Paused = types.BoolValue(deployment.Paused)
model.StorageDocumentID = customtypes.NewUUIDValue(deployment.StorageDocumentID)
model.Version = types.StringValue(deployment.Version)
model.WorkPoolName = types.StringValue(deployment.WorkPoolName)
model.WorkQueueName = types.StringValue(deployment.WorkQueueName)
Expand Down Expand Up @@ -309,6 +317,7 @@ func (r *DeploymentResource) Create(ctx context.Context, req resource.CreateRequ
Parameters: parameters,
Path: plan.Path.ValueString(),
Paused: plan.Paused.ValueBool(),
StorageDocumentID: plan.StorageDocumentID.ValueUUIDPointer(),
Tags: tags,
Version: plan.Version.ValueString(),
WorkPoolName: plan.WorkPoolName.ValueString(),
Expand Down Expand Up @@ -458,6 +467,7 @@ func (r *DeploymentResource) Update(ctx context.Context, req resource.UpdateRequ
Parameters: parameters,
Path: model.Path.ValueString(),
Paused: model.Paused.ValueBool(),
StorageDocumentID: model.StorageDocumentID.ValueUUIDPointer(),
Tags: tags,
Version: model.Version.ValueString(),
WorkPoolName: model.WorkPoolName.ValueString(),
Expand Down
20 changes: 20 additions & 0 deletions internal/provider/resources/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type deploymentConfig struct {
WorkQueueName string

FlowName string

StorageDocumentName string
}

func fixtureAccDeployment(cfg deploymentConfig) string {
Expand All @@ -42,6 +44,18 @@ data "prefect_workspace" "evergreen" {
handle = "github-ci-tests"
}
resource "prefect_block" "test_gh_repository" {
name = "{{.StorageDocumentName}}"
type_slug = "github-repository"
data = jsonencode({
"repository_url": "https://github.com/foo/bar",
"reference": "main"
})
workspace_id = data.prefect_workspace.evergreen.id
}
resource "prefect_flow" "{{.FlowName}}" {
name = "{{.FlowName}}"
tags = [{{range .Tags}}"{{.}}", {{end}}]
Expand All @@ -68,6 +82,7 @@ resource "prefect_deployment" "{{.DeploymentName}}" {
version = "{{.Version}}"
work_pool_name = "{{.WorkPoolName}}"
work_queue_name = "{{.WorkQueueName}}"
storage_document_id = prefect_block.test_gh_repository.id
workspace_id = data.prefect_workspace.evergreen.id
depends_on = [prefect_flow.{{.FlowName}}]
Expand Down Expand Up @@ -100,6 +115,7 @@ func TestAccResource_deployment(t *testing.T) {
Version: "v1.1.1",
WorkPoolName: "evergreen-pool",
WorkQueueName: "evergreen-queue",
StorageDocumentName: testutils.NewRandomPrefixedString(),
}

cfgUpdate := deploymentConfig{
Expand Down Expand Up @@ -140,6 +156,8 @@ func TestAccResource_deployment(t *testing.T) {
//
// Tags: []string{"test1", "test3"}
Tags: cfgCreate.Tags,

StorageDocumentName: cfgCreate.StorageDocumentName,
}

var deployment api.Deployment
Expand Down Expand Up @@ -167,6 +185,7 @@ func TestAccResource_deployment(t *testing.T) {
resource.TestCheckResourceAttr(cfgCreate.DeploymentResourceName, "version", cfgCreate.Version),
resource.TestCheckResourceAttr(cfgCreate.DeploymentResourceName, "work_pool_name", cfgCreate.WorkPoolName),
resource.TestCheckResourceAttr(cfgCreate.DeploymentResourceName, "work_queue_name", cfgCreate.WorkQueueName),
resource.TestCheckResourceAttrSet(cfgCreate.DeploymentResourceName, "storage_document_id"),
),
},
{
Expand All @@ -193,6 +212,7 @@ func TestAccResource_deployment(t *testing.T) {
resource.TestCheckResourceAttr(cfgUpdate.DeploymentResourceName, "version", cfgUpdate.Version),
resource.TestCheckResourceAttr(cfgCreate.DeploymentResourceName, "work_pool_name", cfgUpdate.WorkPoolName),
resource.TestCheckResourceAttr(cfgCreate.DeploymentResourceName, "work_queue_name", cfgUpdate.WorkQueueName),
resource.TestCheckResourceAttrSet(cfgCreate.DeploymentResourceName, "storage_document_id"),
),
},
// Import State checks - import by ID (default)
Expand Down
23 changes: 23 additions & 0 deletions templates/resources/deployment.md.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "{{.Name}} {{.Type}} - {{.ProviderName}}"
subcategory: ""
description: |-
{{ .Description | plainmarkdown | trimspace | prefixlines " " }}
---

# {{.Name}} ({{.Type}})

{{.Description}}

## Example Usage

{{tffile .ExampleFile}}

{{.SchemaMarkdown | trimspace}}

## Import

Import is supported using the following syntax:

{{codefile "shell" .ImportFile}}

0 comments on commit 60fb1f6

Please sign in to comment.