in teamcity/group_data_source.go [92:195]
func (d *groupDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var config groupDataSourceModel
diags := req.Config.Get(ctx, &config)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Validate that either key or name is specified
if config.Key.IsNull() && config.Name.IsNull() {
resp.Diagnostics.AddError(
"Invalid configuration",
"Either 'key' or 'name' must be specified",
)
return
}
// If both are specified, prefer key
if !config.Key.IsNull() && !config.Name.IsNull() {
resp.Diagnostics.AddWarning(
"Both key and name specified",
"Both 'key' and 'name' were specified. Using 'key' for the lookup.",
)
}
var group *client.Group
var err error
// Get group by key or name
if !config.Key.IsNull() {
// Get by key
group, err = d.client.GetGroup(config.Key.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"Error reading group by key",
err.Error(),
)
return
}
} else if !config.Name.IsNull() {
// Get by name - this requires GetGroupByName method in client
// If the client doesn't have this method, we might need to get all groups and filter
group, err = d.client.GetGroupByName(config.Name.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"Error reading group by name",
err.Error(),
)
return
}
}
if group == nil {
identifier := config.Key.ValueString()
if config.Key.IsNull() {
identifier = "name '" + config.Name.ValueString() + "'"
} else {
identifier = "key '" + identifier + "'"
}
resp.Diagnostics.AddError(
"Group not found",
"The group with "+identifier+" was not found",
)
return
}
// Map response to model
state := groupDataSourceModel{
Id: types.StringValue(group.Key),
Key: types.StringValue(group.Key),
Name: types.StringValue(group.Name),
}
// Map roles
if group.Roles != nil && len(group.Roles.RoleAssignment) > 0 {
state.Roles = []roleAssignment{}
for _, role := range group.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)
}
}
// Map parent groups
if group.Parents != nil && len(group.Parents.Group) > 0 {
var parentKeys []attr.Value
for _, parent := range group.Parents.Group {
parentKeys = append(parentKeys, types.StringValue(parent.Key))
}
state.ParentGroups, _ = types.SetValue(types.StringType, parentKeys)
} else {
state.ParentGroups = types.SetNull(types.StringType)
}
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
}