in teamcity/user_role_assignment.go [207:252]
func (r *userRoleAssignmentResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state userRoleAssignmentResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Get user to verify role assignment still exists
user, err := r.client.GetUser(state.UserId.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"Error reading user",
err.Error(),
)
return
}
if user == nil {
resp.State.RemoveResource(ctx)
return
}
// Update username in case it changed
state.Username = types.StringValue(user.Username)
// Check if role assignment still exists
found := false
if user.Roles != nil {
for _, role := range user.Roles.RoleAssignment {
if role.Id == state.RoleId.ValueString() && role.Scope == state.Scope.ValueString() {
found = true
break
}
}
}
if !found {
resp.State.RemoveResource(ctx)
return
}
// Role assignment exists, update state
diags = resp.State.Set(ctx, state)
resp.Diagnostics.Append(diags...)
}