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

Query the resource version prior to making an update #46

Merged
merged 6 commits into from
Feb 14, 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
16 changes: 1 addition & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
# Terraform Provider Scaffolding (Terraform Plugin Framework)

_This template repository is built on the [Terraform Plugin Framework](https://github.com/hashicorp/terraform-plugin-framework). The template repository built on the [Terraform Plugin SDK](https://github.com/hashicorp/terraform-plugin-sdk) can be found at [terraform-provider-scaffolding](https://github.com/hashicorp/terraform-provider-scaffolding). See [Which SDK Should I Use?](https://developer.hashicorp.com/terraform/plugin/framework-benefits) in the Terraform documentation for additional information._

This repository is a *template* for a [Terraform](https://www.terraform.io) provider. It is intended as a starting point for creating Terraform providers, containing:

- A resource and a data source (`internal/provider/`),
- Examples (`examples/`) and generated documentation (`docs/`),
- Miscellaneous meta files.

These files contain boilerplate code that you will need to edit to create your own Terraform provider. Tutorials for creating Terraform providers can be found on the [HashiCorp Developer](https://developer.hashicorp.com/terraform/tutorials/providers-plugin-framework) platform. _Terraform Plugin Framework specific guides are titled accordingly._

Please see the [GitHub template repository documentation](https://help.github.com/en/github/creating-cloning-and-archiving-repositories/creating-a-repository-from-a-template) for how to create a new repository from this template on GitHub.

Once you've written your provider, you'll want to [publish it on the Terraform Registry](https://developer.hashicorp.com/terraform/registry/providers/publishing) so that others can use it.
# Temporal Cloud Terraform Provider

## Requirements

Expand Down
1 change: 0 additions & 1 deletion docs/resources/namespace.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ description: |-
### Read-Only

- `id` (String) The ID of this resource.
- `resource_version` (String)

<a id="nestedatt--certificate_filters"></a>
### Nested Schema for `certificate_filters`
Expand Down
34 changes: 23 additions & 11 deletions internal/provider/namespace_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"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 All @@ -58,7 +57,6 @@ type (
Regions types.List `tfsdk:"regions"`
AcceptedClientCA types.String `tfsdk:"accepted_client_ca"`
RetentionDays types.Int64 `tfsdk:"retention_days"`
ResourceVersion types.String `tfsdk:"resource_version"`
CertificateFilters types.List `tfsdk:"certificate_filters"`

Timeouts timeouts.Value `tfsdk:"timeouts"`
Expand Down Expand Up @@ -134,9 +132,6 @@ func (r *namespaceResource) Schema(ctx context.Context, _ resource.SchemaRequest
"retention_days": schema.Int64Attribute{
Required: true,
},
"resource_version": schema.StringAttribute{
Computed: true,
},
"certificate_filters": schema.ListNestedAttribute{
Optional: true,
NestedObject: schema.NestedAttributeObject{
Expand Down Expand Up @@ -220,9 +215,6 @@ func (r *namespaceResource) Create(ctx context.Context, req resource.CreateReque
return
}

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 Down Expand Up @@ -263,6 +255,11 @@ func (r *namespaceResource) Update(ctx context.Context, req resource.UpdateReque
if resp.Diagnostics.HasError() {
return
}
resourceVersion, err := getCurrentResourceVersion(ctx, r.client, &plan)
if err != nil {
resp.Diagnostics.AddError("Failed to get current resource version", err.Error())
return
}
svcResp, err := r.client.UpdateNamespace(ctx, &cloudservicev1.UpdateNamespaceRequest{
Namespace: plan.ID.ValueString(),
Spec: &namespacev1.NamespaceSpec{
Expand All @@ -274,7 +271,7 @@ func (r *namespaceResource) Update(ctx context.Context, req resource.UpdateReque
CertificateFilters: certFilters,
},
},
ResourceVersion: plan.ResourceVersion.ValueString(),
ResourceVersion: resourceVersion,
})
if err != nil {
resp.Diagnostics.AddError("Failed to update namespace", err.Error())
Expand Down Expand Up @@ -312,11 +309,16 @@ func (r *namespaceResource) Delete(ctx context.Context, req resource.DeleteReque
return
}

resouceVersion, err := getCurrentResourceVersion(ctx, r.client, &state)
if err != nil {
resp.Diagnostics.AddError("Failed to get current resource version", err.Error())
return
}
ctx, cancel := context.WithTimeout(ctx, deleteTimeout)
defer cancel()
svcResp, err := r.client.DeleteNamespace(ctx, &cloudservicev1.DeleteNamespaceRequest{
Namespace: state.ID.ValueString(),
ResourceVersion: state.ResourceVersion.ValueString(),
ResourceVersion: resouceVersion,
})
if err != nil {
resp.Diagnostics.AddError("Failed to delete namespace", err.Error())
Expand Down Expand Up @@ -382,7 +384,6 @@ func updateModelFromSpec(ctx context.Context, diags diag.Diagnostics, state *nam
state.CertificateFilters = certificateFilter
state.AcceptedClientCA = types.StringValue(ns.GetSpec().GetMtlsAuth().GetAcceptedClientCa())
state.RetentionDays = types.Int64Value(int64(ns.GetSpec().GetRetentionDays()))
state.ResourceVersion = types.StringValue(ns.GetResourceVersion())
}

func getCertFiltersFromModel(ctx context.Context, diags diag.Diagnostics, model *namespaceResourceModel) []*namespacev1.CertificateFilterSpec {
Expand Down Expand Up @@ -415,6 +416,17 @@ func getCertFiltersFromModel(ctx context.Context, diags diag.Diagnostics, model
return certificateFilters
}

func getCurrentResourceVersion(ctx context.Context, client cloudservicev1.CloudServiceClient, model *namespaceResourceModel) (string, error) {
ns, err := client.GetNamespace(ctx, &cloudservicev1.GetNamespaceRequest{
Namespace: model.ID.ValueString(),
})
if err != nil {
return "", err
}

return ns.GetNamespace().GetResourceVersion(), nil
}

func stringOrNull(s string) types.String {
if s == "" {
return types.StringNull()
Expand Down
28 changes: 18 additions & 10 deletions internal/provider/namespace_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package provider

import (
"fmt"
"math/rand"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccBasicNamespace(t *testing.T) {
name := fmt.Sprintf("%s-%s", "tf-basic-namespace", randomString(10))
config := func(name string, retention int) string {
return fmt.Sprintf(`
provider "temporalcloud" {
Expand Down Expand Up @@ -43,21 +46,19 @@ PEM
Steps: []resource.TestStep{
{
// New namespace with retention of 7
Config: config("tf-basic-namespace", 7),
Config: config(name, 7),
},
/* Does not work yet: CLD-1971
{
// Update retention to 14
Config: testAccBasicNamespaceConfig("terraform-test", 14),
Config: config(name, 14),
},
*/
// Delete testing automatically occurs in TestCase
},
})

}

func TestAccBasicNamespaceWithCertFilters(t *testing.T) {
name := fmt.Sprintf("%s-%s", "tf-cert-filters", randomString(10))
config := func(name string, retention int) string {
return fmt.Sprintf(`
provider "temporalcloud" {
Expand Down Expand Up @@ -101,15 +102,22 @@ PEM
Steps: []resource.TestStep{
{
// New namespace with retention of 7
Config: config("terraform-test", 7),
Config: config(name, 7),
},
/* Does not work yet: CLD-1971
{
// Update retention to 14
Config: testAccBasicNamespaceConfig("terraform-test", 14),
Config: config(name, 14),
},
*/
// Delete testing automatically occurs in TestCase
},
})
}

func randomString(length int) string {
r := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
const charset = "abcdefghijklmnopqrstuvwxyz"
b := make([]byte, length)
for i := range b {
b[i] = charset[r.Intn(len(charset))]
}
return string(b)
}
Loading