in teamcity/ssh_key_data_source.go [46:87]
func (d *sshKeyDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var model models.SshKeyDataModel
diags := req.Config.Get(ctx, &model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
sshKeys, err := d.client.GetSshKeys(model.ProjectId.ValueString())
if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Unable to read SSH keys in project %s", model.ProjectId.ValueString()),
err.Error(),
)
return
}
var requestedSshKey = ""
for _, value := range sshKeys {
if value == model.Name.ValueString() {
requestedSshKey = value
}
}
if requestedSshKey == "" {
resp.Diagnostics.AddAttributeError(
path.Root("name"),
"SSH Key not found",
"The Datasource cannot get SSH Key since there is no SSH Key with the provided name.",
)
return
}
var state = models.SshKeyDataModel{
Name: types.StringValue(requestedSshKey),
ProjectId: model.ProjectId,
}
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}