func()

in internal/provider/people_data_source.go [108:165]


func (d *PeopleDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
	var data PeopleDataSourceModel

	// Read Terraform configuration data into the model
	resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

	if resp.Diagnostics.HasError() {
		return
	}

	// If applicable, this is a great opportunity to initialize any necessary
	// provider client data and make a call using it.
	// httpResp, err := d.client.Do(httpReq)
	// if err != nil {
	//     resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read people, got error: %s", err))
	//     return
	// }

	tflog.Info(ctx, fmt.Sprintf("HTTP Request: %#v", d.client))

	var person *person_api.Person
	var err error
	var diags diag.Diagnostics

	if data.Email.ValueString() != "" {
		person, err = d.client.GetPersonByEmail(ctx, data.Email.ValueString())
		if err != nil {
			resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read person, got error: %s", err.Error()))
		}
	}

	tflog.Info(ctx, fmt.Sprintf("Read data from API %#v", person), map[string]any{"email": data.Email.ValueString()})

	// For the purposes of this example code, hardcoding a response value to
	// save into the Terraform state.
	data.Id = types.StringValue(person.UserID.Value)

	if person.Identities.GithubIDV4 != nil {
		data.GitHub_Node_Id = types.StringValue(person.Identities.GithubIDV4.Value)
	}
	data.GitHub_Username = types.StringValue(person.Usernames.Values.GitHubUsername)
	data.Mozilliansorg_Groups, diags = types.ListValueFrom(ctx, types.StringType, person.AccessInformation.Mozilliansorg.List)
	for _, d := range diags {
		resp.Diagnostics.Append(d)
	}
	data.Username = types.StringValue(person.PrimaryUsername.Value)

	// Write logs using the tflog package
	// Documentation: https://terraform.io/plugin/log
	tflog.Trace(ctx, "read a data source")

	// Save data into Terraform state
	resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)

	if resp.Diagnostics.HasError() {
		return
	}
}