Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: search attribute renaming #58

Merged
merged 3 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 79 additions & 1 deletion internal/provider/namespace_search_attribute_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,85 @@ func (r *namespaceSearchAttributeResource) Read(ctx context.Context, req resourc
}

func (r *namespaceSearchAttributeResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// TODO: NYI
var plan, state namespaceSearchAttributeModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}

resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}

withNamespaceLock(plan.NamespaceID.ValueString(), func() {
ns, err := r.client.GetNamespace(ctx, &cloudservicev1.GetNamespaceRequest{
Namespace: plan.NamespaceID.ValueString(),
})
if err != nil {
resp.Diagnostics.AddError("Failed to get namespace after update", err.Error())
return
}

if !plan.Name.Equal(state.Name) {
svcResp, err := r.client.RenameCustomSearchAttribute(ctx, &cloudservicev1.RenameCustomSearchAttributeRequest{
Namespace: plan.NamespaceID.ValueString(),
ExistingCustomSearchAttributeName: state.Name.ValueString(),
NewCustomSearchAttributeName: plan.Name.ValueString(),
ResourceVersion: ns.GetNamespace().GetResourceVersion(),
})
if err != nil {
resp.Diagnostics.AddError("Failed to rename search attribute", err.Error())
return
}

if err := client.AwaitAsyncOperation(ctx, r.client, svcResp.GetAsyncOperation()); err != nil {
resp.Diagnostics.AddError("Failed to rename search attribute", err.Error())
return
}
}

spec := ns.GetNamespace().GetSpec()
// Assumption: a search attribute named plan.Name already exists
spec.GetCustomSearchAttributes()[plan.Name.ValueString()] = plan.Type.ValueString()
svcResp, err := r.client.UpdateNamespace(ctx, &cloudservicev1.UpdateNamespaceRequest{
Namespace: plan.NamespaceID.ValueString(),
Spec: spec,
ResourceVersion: ns.GetNamespace().GetResourceVersion(),
})
if err != nil {
resp.Diagnostics.AddError("Failed to update namespace", err.Error())
return
}

if err := client.AwaitAsyncOperation(ctx, r.client, svcResp.GetAsyncOperation()); err != nil {
resp.Diagnostics.AddError("Failed to update namespace", err.Error())
return
}

updatedNs, err := r.client.GetNamespace(ctx, &cloudservicev1.GetNamespaceRequest{
Namespace: plan.NamespaceID.ValueString(),
})
if err != nil {
resp.Diagnostics.AddError("Failed to get namespace after update", err.Error())
return
}

newCSA := updatedNs.GetNamespace().GetSpec().GetCustomSearchAttributes()
newSearchAttrType, ok := newCSA[plan.Name.ValueString()]
if !ok {
resp.Diagnostics.AddError(
"Failed to find newly-created search attribute",
fmt.Sprintf("Failed to find search attribute `%s` after update (this is a bug, please report this on GitHub!)", plan.Name.ValueString()),
)
return
}
// plan.ID does not change
plan.NamespaceID = types.StringValue(updatedNs.GetNamespace().GetNamespace())
// plan.Name is already set
plan.Type = types.StringValue(newSearchAttrType)
resp.Diagnostics.Append(resp.State.Set(ctx, plan)...)
})
}

func (r *namespaceSearchAttributeResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
Expand Down
11 changes: 7 additions & 4 deletions internal/provider/namespace_search_attribute_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func TestAccNamespaceWithSearchAttributes(t *testing.T) {
t.Parallel()
name := fmt.Sprintf("%s-%s", "tf-search-attributes", randomString())
config := func(name string) string {
config := func(name string, saName string) string {
return fmt.Sprintf(`
provider "temporalcloud" {

Expand Down Expand Up @@ -39,7 +39,7 @@ PEM

resource "temporalcloud_namespace_search_attribute" "custom_search_attribute" {
namespace_id = temporalcloud_namespace.terraform.id
name = "CustomSearchAttribute"
name = "%s"
type = "Text"
}

Expand All @@ -53,15 +53,18 @@ resource "temporalcloud_namespace_search_attribute" "custom_search_attribute3" {
namespace_id = temporalcloud_namespace.terraform.id
name = "CustomSearchAttribute3"
type = "Text"
}`, name)
}`, name, saName)
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: config(name),
Config: config(name, "CustomSearchAttribute"),
},
{
Config: config(name, "CustomSearchAttribute9"),
},
},
})
Expand Down
Loading