func()

in teamcity/pool_data_source.go [62:132]


func (d *poolDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {

	var name types.String

	diags := req.Config.GetAttribute(ctx, path.Root("name"), &name)
	resp.Diagnostics.Append(diags...)

	if resp.Diagnostics.HasError() {
		return
	}

	if name.IsUnknown() {
		resp.Diagnostics.AddAttributeError(
			path.Root("name"),
			"Unknown Agent Pool name attribute",
			"The Datasource cannot get an Agent Pool since there is an unknown configuration value for the Agent Pool name.",
		)
		return
	}

	pool, err := d.client.GetPool(name.ValueString())

	if err != nil && errors.Is(err, context.DeadlineExceeded) {
		resp.Diagnostics.AddError(
			"Agent Pool not found: Timeout",
			err.Error(),
		)
		return
	}

	if pool == nil || err != nil {
		resp.Diagnostics.AddAttributeError(
			path.Root("name"),
			"Agent Pool not found",
			"The Datasource cannot get an Agent Pool since there is no Agent Pool with the provided name.",
		)
		return
	}

	var state models.PoolDataModel

	size := basetypes.NewInt64Null()
	if pool.Size != nil {
		size = types.Int64Value(int64(*(pool.Size)))
	}

	state = models.PoolDataModel{
		Name:     types.StringValue(string(pool.Name)),
		Id:       types.Int64Value(int64(*(pool.Id))),
		Projects: types.SetNull(types.StringType),
		Size:     size,
	}

	if pool.Projects != nil {
		elements := []attr.Value{}
		for _, project := range pool.Projects.Project {
			elements = append(elements, types.StringValue(*project.Id))
		}

		state.Projects, diags = types.SetValue(types.StringType, elements)
		if diags.HasError() {
			return
		}
	}

	diags = resp.State.Set(ctx, &state)
	resp.Diagnostics.Append(diags...)
	if resp.Diagnostics.HasError() {
		return
	}
}