Skip to content

Commit

Permalink
fix user data source
Browse files Browse the repository at this point in the history
  • Loading branch information
maciaszczykm committed Jul 25, 2024
1 parent 8678c9a commit 81b04d7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 24 deletions.
27 changes: 7 additions & 20 deletions internal/datasource/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
console "github.com/pluralsh/console/go/client"
)

func NewUserDataSource() datasource.DataSource {
Expand Down Expand Up @@ -43,7 +42,7 @@ func (d *userDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, r
},
"email": schema.StringAttribute{
MarkdownDescription: "Email address of this user.",
Computed: true,
Required: true,
Validators: []validator.String{stringvalidator.ExactlyOneOf(path.MatchRoot("id"))},
},
},
Expand Down Expand Up @@ -74,28 +73,16 @@ func (d *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
return
}

if data.Email.IsNull() {
resp.Diagnostics.AddError(
"Missing user email",
"The provider could not read user data. Email must be specified.",
)
}

// First try to fetch cluster by ID if it was provided.
var user *console.UserFragment
if !data.Email.IsNull() {
if c, err := d.client.GetUser(ctx, data.Email.ValueString()); err != nil {
resp.Diagnostics.AddWarning("Client Error", fmt.Sprintf("Unable to read user by email, got error: %s", err))
} else {
user = c.User
}
response, err := d.client.GetUser(ctx, data.Email.ValueString())
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read user by email, got error: %s", err))
}

if user == nil {
resp.Diagnostics.AddError("Client Error", "Unable to read user, see warnings for more information")
if response == nil || response.User == nil {
resp.Diagnostics.AddError("Client Error", "Unable to find user, got no error")
return
}

data.From(user, ctx, resp.Diagnostics)
data.From(response.User)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
5 changes: 1 addition & 4 deletions internal/datasource/user_model.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package datasource

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
console "github.com/pluralsh/console/go/client"
)
Expand All @@ -14,7 +11,7 @@ type user struct {
Email types.String `tfsdk:"email"`
}

func (c *user) From(cl *console.UserFragment, ctx context.Context, d diag.Diagnostics) {
func (c *user) From(cl *console.UserFragment) {
c.Id = types.StringValue(cl.ID)
c.Name = types.StringValue(cl.Name)
c.Email = types.StringValue(cl.Email)
Expand Down

0 comments on commit 81b04d7

Please sign in to comment.