From bb241776f9a4662179bab6341af174f56b580522 Mon Sep 17 00:00:00 2001 From: Mo Omer Date: Wed, 7 Aug 2024 11:25:59 -0500 Subject: [PATCH] Output a correct access URL (#22) * Output a correct access URL * pre-commit run --all --------- Co-authored-by: Mo Omer --- .task/checksum/docs | 2 +- docs/resources/cluster.md | 6 +++--- internal/cluster/resource.go | 34 ++++++++++++++++++++++++++++++++-- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/.task/checksum/docs b/.task/checksum/docs index 3f00291..5c633f1 100644 --- a/.task/checksum/docs +++ b/.task/checksum/docs @@ -1 +1 @@ -693c62999809037a6ffc3e55d8a3239 +be530b44ca1363365d41614b993d7f7c diff --git a/docs/resources/cluster.md b/docs/resources/cluster.md index 7dd278a..3ec32f9 100644 --- a/docs/resources/cluster.md +++ b/docs/resources/cluster.md @@ -103,6 +103,9 @@ Optional: - `password` (String, Sensitive) Pass holds the password to access the cluster with. +Only shown once, during cluster creation. +- `url` (String, Sensitive) URL is the Cluster endpoint for access. + Only shown once, during cluster creation. - `user` (String, Sensitive) User holds the username to access the cluster with. @@ -113,9 +116,6 @@ Read-Only: - `host` (String) Host name of the cluster. - `port` (Number) HTTP Port the cluster is running on. - `scheme` (String) HTTP Scheme needed to access the cluster. Default: "https". -- `url` (String) URL is the Cluster endpoint for access. - -Only shown once, during cluster creation. diff --git a/internal/cluster/resource.go b/internal/cluster/resource.go index a0fcc41..97f47ff 100644 --- a/internal/cluster/resource.go +++ b/internal/cluster/resource.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "net/url" "regexp" "time" @@ -13,6 +14,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-framework/types/basetypes" "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/omc/bonsai-api-go/v2/bonsai" ) @@ -236,7 +238,9 @@ func resourceSchemaAttributes() map[string]rschema.Attribute { MarkdownDescription: "URL is the Cluster endpoint for " + "access.\n\n" + "Only shown once, during cluster creation.", - Computed: true, + Sensitive: true, + Computed: true, + Optional: true, }, }, }, @@ -501,7 +505,33 @@ DiscoveryLoop: return } // Add credentials back to the refreshed state - refreshState.Access = createResultState.Access + refreshAccessModel := &accessModel{} + createAccessModel := &accessModel{} + + tflog.Debug(ctx, "converting access as accessModel") + refreshState.Access.As(context.Background(), refreshAccessModel, basetypes.ObjectAsOptions{}) + createResultState.Access.As(context.Background(), createAccessModel, basetypes.ObjectAsOptions{}) + + refreshAccessModel.Username = createAccessModel.Username + refreshAccessModel.Password = createAccessModel.Password + + // Now, update the Refresh State's Access URL, with the full URI + accessUrl := url.URL{} + accessUrl.Host = refreshAccessModel.Host.ValueString() + accessUrl.Scheme = createAccessModel.Scheme.ValueString() + accessUrl.User = url.UserPassword(createAccessModel.Username.ValueString(), createAccessModel.Password.ValueString()) + refreshAccessModel.URL = types.StringValue(accessUrl.String()) + + // Finally, we're ready to convert access back into an ObjectValue and update the refreshState object + tflog.Debug(ctx, "converting access back to ObjectValue") + access, diags := types.ObjectValueFrom(context.TODO(), accessModelTypes, &refreshAccessModel) + if diags.HasError() { + resp.Diagnostics.Append(diags...) + return + } + tflog.Debug(ctx, "Setting Access to access") + refreshState.Access = access + // And, set the unique identifier refreshState.ID = createResultState.ID