func()

in teamcity/user_data_source.go [85:170]


func (d *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
	var config userDataSourceModel
	diags := req.Config.Get(ctx, &config)
	resp.Diagnostics.Append(diags...)
	if resp.Diagnostics.HasError() {
		return
	}

	// Validate that either id or username is specified
	if config.Id.IsNull() && config.Username.IsNull() {
		resp.Diagnostics.AddError(
			"Invalid configuration",
			"Either 'id' or 'username' must be specified",
		)
		return
	}

	var user *client.User
	var err error

	// Get user by id or username
	if !config.Id.IsNull() {
		user, err = d.client.GetUser(config.Id.ValueString())
	} else {
		user, err = d.client.GetUserByName(config.Username.ValueString())
	}

	if err != nil {
		resp.Diagnostics.AddError(
			"Error reading user",
			err.Error(),
		)
		return
	}

	if user == nil {
		identifier := config.Id.ValueString()
		if config.Id.IsNull() {
			identifier = config.Username.ValueString()
		}
		resp.Diagnostics.AddError(
			"User not found",
			"The user '"+identifier+"' was not found",
		)
		return
	}

	// Map response to model
	state := userDataSourceModel{
		Id:       types.StringValue(strconv.FormatInt(*user.Id, 10)),
		Username: types.StringValue(user.Username),
	}

	// Extract GitHub username from properties
	if user.Properties != nil {
		for _, p := range user.Properties.Property {
			if p.Name == "plugin:auth:GitHubApp-oauth:userName" {
				state.Github = types.StringValue(p.Value)
				break
			}
		}
	}
	if state.Github.IsNull() {
		state.Github = types.StringNull()
	}

	// Map roles
	if user.Roles != nil && len(user.Roles.RoleAssignment) > 0 {
		state.Roles = []roleAssignment{}
		for _, role := range user.Roles.RoleAssignment {
			assignment := roleAssignment{
				Id: types.StringValue(role.Id),
			}
			if role.Scope == "g" {
				assignment.Global = types.BoolValue(true)
			} else {
				assignment.Global = types.BoolValue(false)
				assignment.Project = types.StringValue(role.Scope[2:])
			}
			state.Roles = append(state.Roles, assignment)
		}
	}

	diags = resp.State.Set(ctx, &state)
	resp.Diagnostics.Append(diags...)
}