|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + devhub "terraform-provider-devhub/internal/client" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + _ datasource.DataSource = &userDataSource{} |
| 15 | + _ datasource.DataSourceWithConfigure = &userDataSource{} |
| 16 | +) |
| 17 | + |
| 18 | +func NewUserDataSource() datasource.DataSource { |
| 19 | + return &userDataSource{} |
| 20 | +} |
| 21 | + |
| 22 | +type userDataSource struct { |
| 23 | + client *devhub.Client |
| 24 | +} |
| 25 | + |
| 26 | +type userDataSourceModel struct { |
| 27 | + Id types.String `tfsdk:"id"` |
| 28 | + Name types.String `tfsdk:"name"` |
| 29 | + Email types.String `tfsdk:"email"` |
| 30 | +} |
| 31 | + |
| 32 | +func (d *userDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 33 | + resp.TypeName = req.ProviderTypeName + "_user" |
| 34 | +} |
| 35 | + |
| 36 | +func (d *userDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 37 | + resp.Schema = schema.Schema{ |
| 38 | + Attributes: map[string]schema.Attribute{ |
| 39 | + "id": schema.StringAttribute{ |
| 40 | + Computed: true, |
| 41 | + }, |
| 42 | + "name": schema.StringAttribute{ |
| 43 | + Optional: true, |
| 44 | + }, |
| 45 | + "email": schema.StringAttribute{ |
| 46 | + Optional: true, |
| 47 | + }, |
| 48 | + }, |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// Read refreshes the Terraform state with the latest data. |
| 53 | +func (d *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 54 | + var state userDataSourceModel |
| 55 | + |
| 56 | + resp.Diagnostics.Append(req.Config.Get(ctx, &state)...) |
| 57 | + if resp.Diagnostics.HasError() { |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + lookupBy := "" |
| 62 | + identifier := "" |
| 63 | + |
| 64 | + if state.Email.ValueString() != "" { |
| 65 | + lookupBy = "email" |
| 66 | + identifier = state.Email.ValueString() |
| 67 | + } |
| 68 | + |
| 69 | + if state.Name.ValueString() != "" { |
| 70 | + lookupBy = "name" |
| 71 | + identifier = state.Name.ValueString() |
| 72 | + } |
| 73 | + |
| 74 | + user, err := d.client.GetUser(identifier, lookupBy) |
| 75 | + if err != nil { |
| 76 | + resp.Diagnostics.AddError( |
| 77 | + "Error Reading User", |
| 78 | + fmt.Sprintf("Could not read User: %s", err.Error()), |
| 79 | + ) |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + state.Id = types.StringValue(user.Id) |
| 84 | + state.Name = types.StringValue(user.Name) |
| 85 | + state.Email = types.StringValue(user.Email) |
| 86 | + |
| 87 | + diags := resp.State.Set(ctx, &state) |
| 88 | + resp.Diagnostics.Append(diags...) |
| 89 | + if resp.Diagnostics.HasError() { |
| 90 | + return |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// Configure adds the provider configured client to the data source. |
| 95 | +func (d *userDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 96 | + if req.ProviderData == nil { |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + client, ok := req.ProviderData.(*devhub.Client) |
| 101 | + if !ok { |
| 102 | + resp.Diagnostics.AddError( |
| 103 | + "Unexpected Data Source Configure Type", |
| 104 | + fmt.Sprintf("Expected *devhub.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 105 | + ) |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + d.client = client |
| 110 | +} |
0 commit comments