Skip to content

Commit

Permalink
feature: namespace update (#37)
Browse files Browse the repository at this point in the history
* feature: namespace update

This commit implements updating and reading namespaces, though updating doesn't really work due to CLD-1971.

* lint
  • Loading branch information
swgillespie authored Jan 5, 2024
1 parent fa4f1be commit 955ff95
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 18 deletions.
1 change: 1 addition & 0 deletions docs/data-sources/regions.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ description: |-

### Read-Only

- `id` (String) The ID of this resource.
- `regions` (Attributes List) (see [below for nested schema](#nestedatt--regions))

<a id="nestedatt--regions"></a>
Expand Down
Empty file removed examples/data-sources/.keep
Empty file.
Empty file removed examples/resources/.keep
Empty file.
2 changes: 1 addition & 1 deletion examples/resources/namespace/namespace.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ resource "temporalcloud_namespace" "terraform" {
name = "terraform"
regions = ["aws-us-east-1"]
accepted_client_ca = base64encode(file("${path.module}/ca.pem"))
retention_days = 45
retention_days = 14
}
10 changes: 3 additions & 7 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ func newConnection(addrStr string, allowInsecure bool, opts ...grpc.DialOption)
if err != nil {
return nil, fmt.Errorf("unable to parse server address: %s", err)
}
defaultOpts, err := defaultDialOptions(addr, allowInsecure)
if err != nil {
return nil, fmt.Errorf("failed to generate default dial options: %s", err)
}

defaultOpts := defaultDialOptions(addr, allowInsecure)
conn, err := grpc.Dial(
addr.String(),
append(defaultOpts, opts...)...,
Expand Down Expand Up @@ -134,7 +130,7 @@ func AwaitAsyncOperation(ctx context.Context, client cloudservicev1.CloudService
}
}

func defaultDialOptions(addr *url.URL, allowInsecure bool) ([]grpc.DialOption, error) {
func defaultDialOptions(addr *url.URL, allowInsecure bool) []grpc.DialOption {
var opts []grpc.DialOption

transport := credentials.NewTLS(&tls.Config{
Expand All @@ -147,7 +143,7 @@ func defaultDialOptions(addr *url.URL, allowInsecure bool) ([]grpc.DialOption, e

opts = append(opts, grpc.WithTransportCredentials(transport))
opts = append(opts, grpc.WithUnaryInterceptor(setAPIVersionInterceptor))
return opts, nil
return opts
}

func setAPIVersionInterceptor(
Expand Down
77 changes: 67 additions & 10 deletions internal/provider/namespace_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/temporalio/terraform-provider-temporalcloud/internal/client"
cloudservicev1 "github.com/temporalio/terraform-provider-temporalcloud/proto/go/temporal/api/cloud/cloudservice/v1"
namespacev1 "github.com/temporalio/terraform-provider-temporalcloud/proto/go/temporal/api/cloud/namespace/v1"
Expand Down Expand Up @@ -177,16 +178,10 @@ func (r *namespaceResource) Create(ctx context.Context, req resource.CreateReque
return
}

planRegions, diags := types.ListValueFrom(ctx, types.StringType, ns.Namespace.Spec.Regions)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

plan.ID = types.StringValue(ns.Namespace.Namespace)
plan.Regions = planRegions
plan.RetentionDays = types.Int64Value(int64(ns.Namespace.Spec.RetentionDays))
plan.ResourceVersion = types.StringValue(ns.Namespace.ResourceVersion)
tflog.Debug(ctx, "responded with namespace model", map[string]any{
"resource_version": ns.GetNamespace().GetResourceVersion(),
})
updateModelFromSpec(ctx, resp.Diagnostics, &plan, ns.Namespace)
resp.Diagnostics.Append(resp.State.Set(ctx, plan)...)
}

Expand All @@ -197,6 +192,17 @@ func (r *namespaceResource) Read(ctx context.Context, req resource.ReadRequest,
if resp.Diagnostics.HasError() {
return
}

model, err := r.client.GetNamespace(ctx, &cloudservicev1.GetNamespaceRequest{
Namespace: state.ID.ValueString(),
})
if err != nil {
resp.Diagnostics.AddError("Failed to get namespace", err.Error())
return
}

updateModelFromSpec(ctx, resp.Diagnostics, &state, model.Namespace)
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}

// Update updates the resource and sets the updated Terraform state on success.
Expand All @@ -206,6 +212,43 @@ func (r *namespaceResource) Update(ctx context.Context, req resource.UpdateReque
if resp.Diagnostics.HasError() {
return
}

regions := getRegionsFromModel(ctx, resp.Diagnostics, &plan)
if resp.Diagnostics.HasError() {
return
}
svcResp, err := r.client.UpdateNamespace(ctx, &cloudservicev1.UpdateNamespaceRequest{
Namespace: plan.ID.ValueString(),
Spec: &namespacev1.NamespaceSpec{
Name: plan.Name.ValueString(),
Regions: regions,
RetentionDays: int32(plan.RetentionDays.ValueInt64()),
MtlsAuth: &namespacev1.MtlsAuthSpec{
AcceptedClientCa: plan.AcceptedClientCA.ValueString(),
},
},
ResourceVersion: plan.ResourceVersion.ValueString(),
})
if err != nil {
resp.Diagnostics.AddError("Failed to update namespace", err.Error())
return
}

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

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

updateModelFromSpec(ctx, resp.Diagnostics, &plan, ns.Namespace)
resp.Diagnostics.Append(resp.State.Set(ctx, plan)...)
}

// Delete deletes the resource and removes the Terraform state on success.
Expand Down Expand Up @@ -252,3 +295,17 @@ func getRegionsFromModel(ctx context.Context, diags diag.Diagnostics, plan *name

return requestRegions
}

func updateModelFromSpec(ctx context.Context, diags diag.Diagnostics, state *namespaceResourceModel, ns *namespacev1.Namespace) {
state.ID = types.StringValue(ns.GetNamespace())
planRegions, listDiags := types.ListValueFrom(ctx, types.StringType, ns.GetSpec().GetRegions())
diags.Append(listDiags...)
if diags.HasError() {
return
}

state.Regions = planRegions
state.AcceptedClientCA = types.StringValue(ns.GetSpec().GetMtlsAuth().GetAcceptedClientCa())
state.RetentionDays = types.Int64Value(int64(ns.GetSpec().GetRetentionDays()))
state.ResourceVersion = types.StringValue(ns.GetResourceVersion())
}
6 changes: 6 additions & 0 deletions internal/provider/namespace_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ func TestAccBasicNamespace(t *testing.T) {
// New namespace with retention of 7
Config: testAccBasicNamespaceConfig("terraform-test", 7),
},
/* Does not work yet: CLD-1971
{
// Update retention to 14
Config: testAccBasicNamespaceConfig("terraform-test", 14),
},
*/
// Delete testing automatically occurs in TestCase
},
})
Expand Down

0 comments on commit 955ff95

Please sign in to comment.