|
| 1 | +// Copyright IBM Corp. 2018, 2025 |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package provider |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 13 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + _ datasource.DataSource = &dataSourceCurrentUser{} |
| 18 | + _ datasource.DataSourceWithConfigure = &dataSourceCurrentUser{} |
| 19 | +) |
| 20 | + |
| 21 | +func NewCurrentUserDataSource() datasource.DataSource { |
| 22 | + return &dataSourceCurrentUser{} |
| 23 | +} |
| 24 | + |
| 25 | +type dataSourceCurrentUser struct { |
| 26 | + config ConfiguredClient |
| 27 | +} |
| 28 | + |
| 29 | +type modelCurrentUser struct { |
| 30 | + ID types.String `tfsdk:"id"` |
| 31 | + Username types.String `tfsdk:"username"` |
| 32 | + Email types.String `tfsdk:"email"` |
| 33 | + IsServiceAccount types.Bool `tfsdk:"is_service_account"` |
| 34 | +} |
| 35 | + |
| 36 | +func (d *dataSourceCurrentUser) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 37 | + resp.TypeName = req.ProviderTypeName + "_current_user" |
| 38 | +} |
| 39 | + |
| 40 | +func (d *dataSourceCurrentUser) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 41 | + resp.Schema = schema.Schema{ |
| 42 | + MarkdownDescription: "Get the current user associated with the API token.", |
| 43 | + |
| 44 | + Attributes: map[string]schema.Attribute{ |
| 45 | + "id": schema.StringAttribute{ |
| 46 | + Computed: true, |
| 47 | + MarkdownDescription: "Service-generated identifier for the user.", |
| 48 | + }, |
| 49 | + "username": schema.StringAttribute{ |
| 50 | + Computed: true, |
| 51 | + MarkdownDescription: "The username of the current user.", |
| 52 | + }, |
| 53 | + "email": schema.StringAttribute{ |
| 54 | + Computed: true, |
| 55 | + MarkdownDescription: "The email address of the current user.", |
| 56 | + }, |
| 57 | + "is_service_account": schema.BoolAttribute{ |
| 58 | + Computed: true, |
| 59 | + MarkdownDescription: "Whether the current user is a service account.", |
| 60 | + }, |
| 61 | + }, |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func (d *dataSourceCurrentUser) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 66 | + if req.ProviderData == nil { |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + client, ok := req.ProviderData.(ConfiguredClient) |
| 71 | + if !ok { |
| 72 | + resp.Diagnostics.AddError( |
| 73 | + "Unexpected Data Source Configure Type", |
| 74 | + fmt.Sprintf("Expected ConfiguredClient, got %T. This is a bug in the tfe provider, so please report it on GitHub.", req.ProviderData), |
| 75 | + ) |
| 76 | + |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + d.config = client |
| 81 | +} |
| 82 | + |
| 83 | +func (d *dataSourceCurrentUser) Read(ctx context.Context, _ datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 84 | + tflog.Debug(ctx, "Reading current user") |
| 85 | + |
| 86 | + user, err := d.config.Client.Users.ReadCurrent(ctx) |
| 87 | + if err != nil { |
| 88 | + resp.Diagnostics.AddError("Unable to read current user", err.Error()) |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + model := modelCurrentUser{ |
| 93 | + ID: types.StringValue(user.ID), |
| 94 | + Username: types.StringValue(user.Username), |
| 95 | + Email: types.StringValue(user.Email), |
| 96 | + IsServiceAccount: types.BoolValue(user.IsServiceAccount), |
| 97 | + } |
| 98 | + |
| 99 | + tflog.Trace(ctx, "Read current user successfully", map[string]any{ |
| 100 | + "user_id": user.ID, |
| 101 | + "username": user.Username, |
| 102 | + "is_service_account": user.IsServiceAccount, |
| 103 | + }) |
| 104 | + |
| 105 | + diags := resp.State.Set(ctx, &model) |
| 106 | + resp.Diagnostics.Append(diags...) |
| 107 | +} |
0 commit comments