Skip to content

Commit

Permalink
feat: add await for a search attribute creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ganievs committed Dec 27, 2024
1 parent 3629018 commit d60c27c
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ jobs:
- uses: temporalio/setup-temporal@v0
- run: temporal server start-dev --headless &
shell: bash
- run: temporal operator namespace update --namespace default --description "Default namespace for Temporal Server."
shell: bash
- uses: actions/setup-go@v4
with:
go-version-file: 'go.mod'
Expand Down
5 changes: 3 additions & 2 deletions docker-compose/.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ COMPOSE_PROJECT_NAME=temporal
CASSANDRA_VERSION=3.11.9
ELASTICSEARCH_VERSION=7.16.2
MYSQL_VERSION=8
TEMPORAL_VERSION=1.22.3
TEMPORAL_UI_VERSION=2.21.0
TEMPORAL_VERSION=1.26.2
TEMPORAL_ADMINTOOLS_VERSION=1.26.2
TEMPORAL_UI_VERSION=2.31.2
POSTGRESQL_VERSION=13
POSTGRES_PASSWORD=temporal
POSTGRES_USER=temporal
Expand Down
7 changes: 2 additions & 5 deletions docker-compose/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ services:
- postgresql
- elasticsearch
environment:
- DB=postgresql
- DB=postgres12
- DB_PORT=5432
- POSTGRES_USER=temporal
- POSTGRES_PWD=temporal
Expand All @@ -49,8 +49,6 @@ services:
- temporal-network
ports:
- 7233:7233
labels:
kompose.volume.type: configMap
volumes:
- ./dynamicconfig:/etc/temporal/config/dynamicconfig
temporal-admin-tools:
Expand All @@ -60,7 +58,7 @@ services:
environment:
- TEMPORAL_ADDRESS=temporal:7233
- TEMPORAL_CLI_ADDRESS=temporal:7233
image: temporalio/admin-tools:${TEMPORAL_VERSION}
image: temporalio/admin-tools:${TEMPORAL_ADMINTOOLS_VERSION}
networks:
- temporal-network
stdin_open: true
Expand All @@ -81,4 +79,3 @@ networks:
temporal-network:
driver: bridge
name: temporal-network

5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module terraform-provider-temporal

go 1.21
toolchain go1.22.5
go 1.22.0

toolchain go1.23.4

require (
github.com/hashicorp/terraform-plugin-docs v0.19.4
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/namespace_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ data "temporal_namespace" "default" {

Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.temporal_namespace.default", "name", "default"),
resource.TestCheckResourceAttr("data.temporal_namespace.default", "description", ""),
resource.TestCheckResourceAttr("data.temporal_namespace.default", "description", "Default namespace for Temporal Server."),
),
},
},
Expand Down
40 changes: 28 additions & 12 deletions internal/provider/search_attribute_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand All @@ -25,6 +26,31 @@ var (
_ resource.ResourceWithImportState = &SearchAttributeResource{}
)

// AwaitAddSearchAttributes waits for the completion of AddSearchAttributesRequest using ListSearchAttributes.
func AwaitAddSearchAttributes(ctx context.Context, operatorClient operatorservice.OperatorServiceClient, data SearchAttributeResourceModel) error {
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()

for {
select {
case <-ticker.C:
// Verify attribute creation
attributes, err := operatorClient.ListSearchAttributes(ctx, &operatorservice.ListSearchAttributesRequest{
Namespace: data.Namespace.ValueString(),
})
if err != nil {
return err
}

if _, ok := attributes.CustomAttributes[data.Name.ValueString()]; ok {
return nil
}
case <-ctx.Done():
return ctx.Err()
}
}
}

// NewSearchAttributeResource creates a new instance of SearchAttributeResource.
func NewSearchAttributeResource() resource.Resource {
return &SearchAttributeResource{}
Expand Down Expand Up @@ -134,7 +160,6 @@ func (r *SearchAttributeResource) Create(ctx context.Context, req resource.Creat
SearchAttributes: map[string]enums.IndexedValueType{data.Name.ValueString(): indexedValueType},
}
_, err = client.AddSearchAttributes(ctx, request)

if err != nil {
if _, ok := err.(*serviceerror.AlreadyExists); ok {
resp.Diagnostics.AddError("Request Error", "Search attribute with that name is already registered: "+err.Error())
Expand All @@ -144,18 +169,9 @@ func (r *SearchAttributeResource) Create(ctx context.Context, req resource.Creat
return
}

// Verify attribute creation
attributes, err := client.ListSearchAttributes(ctx, &operatorservice.ListSearchAttributesRequest{
Namespace: data.Namespace.ValueString(),
})
err = AwaitAddSearchAttributes(ctx, client, data)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read search attribute info, got error: %s", err))
return
}

_, ok := attributes.CustomAttributes[data.Name.ValueString()]
if !ok {
resp.Diagnostics.AddError("Verification Error", fmt.Sprintf("%s not found, creation could not be verified", data.Name.ValueString()))
resp.Diagnostics.AddError("Request Error", "Error awaiting results: "+err.Error())
return
}

Expand Down

0 comments on commit d60c27c

Please sign in to comment.