func()

in teamcity/user_role_assignment.go [263:317]


func (r *userRoleAssignmentResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
	var state userRoleAssignmentResourceModel
	diags := req.State.Get(ctx, &state)
	resp.Diagnostics.Append(diags...)
	if resp.Diagnostics.HasError() {
		return
	}

	// Get current user
	user, err := r.client.GetUser(state.UserId.ValueString())
	if err != nil {
		// If user doesn't exist, consider it deleted
		if strings.Contains(err.Error(), "404") {
			return
		}
		resp.Diagnostics.AddError(
			"Error getting user",
			err.Error(),
		)
		return
	}

	if user == nil {
		// User doesn't exist, nothing to delete
		return
	}

	// Build updated user without the role
	updatedUser := client.User{
		Id:       user.Id,
		Username: user.Username,
	}

	updatedUser.Roles = &client.RoleAssignments{
		RoleAssignment: []client.RoleAssignment{},
	}

	// Copy existing roles except the one being deleted
	if user.Roles != nil {
		for _, role := range user.Roles.RoleAssignment {
			if !(role.Id == state.RoleId.ValueString() && role.Scope == state.Scope.ValueString()) {
				updatedUser.Roles.RoleAssignment = append(updatedUser.Roles.RoleAssignment, role)
			}
		}
	}

	// Update user
	_, err = r.client.SetUser(updatedUser)
	if err != nil {
		resp.Diagnostics.AddError(
			"Error removing role from user",
			err.Error(),
		)
	}
}