Skip to content
This repository has been archived by the owner on Jan 29, 2025. It is now read-only.

Commit

Permalink
fully implement user data source
Browse files Browse the repository at this point in the history
after some bad implementations, this should be a more thought out implementation
  • Loading branch information
Luiggi33 committed Jul 24, 2024
1 parent fddfc71 commit c2343f5
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 9 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ require (
github.com/hashicorp/hc-install v0.7.0 // indirect
github.com/hashicorp/terraform-exec v0.21.0 // indirect
github.com/hashicorp/terraform-json v0.22.1 // indirect
github.com/hashicorp/terraform-plugin-framework-validators v0.13.0 // indirect
github.com/hashicorp/terraform-plugin-go v0.23.0 // indirect
github.com/hashicorp/terraform-registry-address v0.2.3 // indirect
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ github.com/hashicorp/terraform-plugin-docs v0.19.4 h1:G3Bgo7J22OMtegIgn8Cd/CaSey
github.com/hashicorp/terraform-plugin-docs v0.19.4/go.mod h1:4pLASsatTmRynVzsjEhbXZ6s7xBlUw/2Kt0zfrq8HxA=
github.com/hashicorp/terraform-plugin-framework v1.10.0 h1:xXhICE2Fns1RYZxEQebwkB2+kXouLC932Li9qelozrc=
github.com/hashicorp/terraform-plugin-framework v1.10.0/go.mod h1:qBXLDn69kM97NNVi/MQ9qgd1uWWsVftGSnygYG1tImM=
github.com/hashicorp/terraform-plugin-framework-validators v0.13.0 h1:bxZfGo9DIUoLLtHMElsu+zwqI4IsMZQBRRy4iLzZJ8E=
github.com/hashicorp/terraform-plugin-framework-validators v0.13.0/go.mod h1:wGeI02gEhj9nPANU62F2jCaHjXulejm/X+af4PdZaNo=
github.com/hashicorp/terraform-plugin-go v0.23.0 h1:AALVuU1gD1kPb48aPQUjug9Ir/125t+AAurhqphJ2Co=
github.com/hashicorp/terraform-plugin-go v0.23.0/go.mod h1:1E3Cr9h2vMlahWMbsSEcNrOCxovCZhOOIXjFHbjc/lQ=
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=
Expand Down
85 changes: 76 additions & 9 deletions internal/provider/user_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import (
"time"

"github.com/Luiggi33/pterodactyl-client-go"
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

// Ensure the implementation satisfies the expected interfaces.
Expand Down Expand Up @@ -52,16 +56,76 @@ func (d *userDataSource) Schema(ctx context.Context, req datasource.SchemaReques
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.Int64Attribute{
Description: "The ID of the user.",
Optional: true,
Optional: true,
Computed: true,
Validators: []validator.Int64{
int64validator.ExactlyOneOf(
path.MatchRoot("id"),
path.MatchRoot("external_id"),
path.MatchRoot("username"),
path.MatchRoot("email"),
),
},
},
"external_id": schema.StringAttribute{
Optional: true,
Computed: true,
Validators: []validator.String{
stringvalidator.ExactlyOneOf(
path.MatchRoot("id"),
path.MatchRoot("external_id"),
path.MatchRoot("username"),
path.MatchRoot("email"),
),
},
},
"uuid": schema.StringAttribute{
Computed: true,
},
"username": schema.StringAttribute{
Description: "The username of the user.",
Optional: true,
Optional: true,
Computed: true,
Validators: []validator.String{
stringvalidator.ExactlyOneOf(
path.MatchRoot("id"),
path.MatchRoot("external_id"),
path.MatchRoot("username"),
path.MatchRoot("email"),
),
},
},
"email": schema.StringAttribute{
Description: "The email of the user.",
Optional: true,
Optional: true,
Computed: true,
Validators: []validator.String{
stringvalidator.ExactlyOneOf(
path.MatchRoot("id"),
path.MatchRoot("external_id"),
path.MatchRoot("username"),
path.MatchRoot("email"),
),
},
},
"first_name": schema.StringAttribute{
Computed: true,
},
"last_name": schema.StringAttribute{
Computed: true,
},
"language": schema.StringAttribute{
Computed: true,
},
"root_admin": schema.BoolAttribute{
Computed: true,
},
"is_2fa": schema.BoolAttribute{
Computed: true,
},
"created_at": schema.StringAttribute{
Computed: true,
},
"updated_at": schema.StringAttribute{
Computed: true,
},
},
}
Expand All @@ -73,9 +137,10 @@ func (d *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, r

// Get the attributes from the request
var target struct {
ID int `tfsdk:"id"`
Username string `tfsdk:"username"`
Email string `tfsdk:"email"`
ID int `tfsdk:"id"`
ExternalID string `tfsdk:"external_id"`
Username string `tfsdk:"username"`
Email string `tfsdk:"email"`
}
diags := req.Config.Get(ctx, &target)
resp.Diagnostics.Append(diags...)
Expand All @@ -92,6 +157,8 @@ func (d *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
user, err = d.client.GetUserUsername(target.Username)
} else if target.Email != "" {
user, err = d.client.GetUserEmail(target.Email)
} else if target.ExternalID != "" {
user, err = d.client.GetUserExternalID(target.ExternalID)
} else {
resp.Diagnostics.AddError(
"Missing Attribute",
Expand Down

0 comments on commit c2343f5

Please sign in to comment.