Skip to content

Commit

Permalink
Output a correct access URL (#22)
Browse files Browse the repository at this point in the history
* Output a correct access URL

* pre-commit run --all

---------

Co-authored-by: Mo Omer <momer@dcek.com>
  • Loading branch information
momer and Mo Omer authored Aug 7, 2024
1 parent f8c917b commit bb24177
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .task/checksum/docs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
693c62999809037a6ffc3e55d8a3239
be530b44ca1363365d41614b993d7f7c
6 changes: 3 additions & 3 deletions docs/resources/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.


<a id="nestedatt--state"></a>
Expand Down
34 changes: 32 additions & 2 deletions internal/cluster/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"net/url"
"regexp"
"time"

Expand All @@ -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"
)
Expand Down Expand Up @@ -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,
},
},
},
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit bb24177

Please sign in to comment.