func()

in teamcity/user_role_assignment.go [319:377]


func (r *userRoleAssignmentResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
	// Import ID format: user_id/role_id/scope or username/role_id/scope
	parts := strings.Split(req.ID, "/")
	if len(parts) != 3 {
		resp.Diagnostics.AddError(
			"Invalid import ID",
			"Import ID must be in the format: user_id/role_id/scope or username/role_id/scope",
		)
		return
	}

	// Try to determine if first part is user_id or username
	var userId string
	var username string

	// Check if it's a numeric ID
	if _, err := strconv.ParseInt(parts[0], 10, 64); err == nil {
		// It's a user ID
		userId = parts[0]
		// Get username
		user, err := r.client.GetUser(userId)
		if err != nil {
			resp.Diagnostics.AddError(
				"Error getting user",
				err.Error(),
			)
			return
		}
		if user != nil {
			username = user.Username
		}
	} else {
		// It's a username
		username = parts[0]
		// Get user ID
		user, err := r.client.GetUserByName(username)
		if err != nil {
			resp.Diagnostics.AddError(
				"Error getting user",
				err.Error(),
			)
			return
		}
		if user != nil {
			userId = strconv.FormatInt(*user.Id, 10)
		}
	}

	state := userRoleAssignmentResourceModel{
		Id:       types.StringValue(fmt.Sprintf("%s_%s_%s", userId, parts[1], parts[2])),
		UserId:   types.StringValue(userId),
		Username: types.StringValue(username),
		RoleId:   types.StringValue(parts[1]),
		Scope:    types.StringValue(parts[2]),
	}

	diags := resp.State.Set(ctx, &state)
	resp.Diagnostics.Append(diags...)
}