terraform-provider-stackitp.../stackit/internal/services/postgresflexalpha/flavor/functions.go
harmsan 979220be66
chore: adjust pagination for postgres database and flavor listing (#20)
* feat: implement pagination for database listing

* fix: change database_id attribute type from string to int64

* refactor: rename getDatabase to getDatabaseById for clarity

* fix: improve error handling for database not found scenario

* feat: add validation for database_id and name attributes; implement separate functions for fetching databases by ID and name

* feat: implement database client interface and update database fetching functions

* refactor: rename matcher to filter for clarity and update pagination logic

* feat: implement flavors retrieval with pagination and filtering support

* refactor: rename flavor import for consistency and clarity

* feat: add support for InstanceStatePending in wait handler logic

* refactor: simplify GetFlavorsRequest and GetFlavorsRequestExecute by removing pagination parameters

* refactor: improve readability of test cases by formatting function signatures and restructuring test runs

* refactor: remove pagination parameters from GetFlavorsRequest in test case

* refactor: simplify function signatures and improve readability in datasource and resource files

* refactor: add descriptions for user-related attributes in datasource schema

* refactor: enhance user resource schema with additional attributes and improve logging

* refactor: delete unused file

* refactor: standardize formatting and improve function naming for user resource management

* refactor: remove skip from TestMapFields and update roles initialization in resource tests

* fix: golangci lint issues

* fix: golangci lint issues again

* fix: golangci lint issues again
2026-01-16 16:23:10 +01:00

65 lines
1.7 KiB
Go

package postgresFlexAlphaFlavor
import (
"context"
"fmt"
postgresflex "github.com/mhenselin/terraform-provider-stackitprivatepreview/pkg/postgresflexalpha"
)
type flavorsClientReader interface {
GetFlavorsRequest(
ctx context.Context,
projectId, region string,
) postgresflex.ApiGetFlavorsRequestRequest
}
func getAllFlavors(ctx context.Context, client flavorsClientReader, projectId, region string) (
[]postgresflex.ListFlavors,
error,
) {
getAllFilter := func(_ postgresflex.ListFlavors) bool { return true }
flavorList, err := getFlavorsByFilter(ctx, client, projectId, region, getAllFilter)
if err != nil {
return nil, err
}
return flavorList, nil
}
// getFlavorsByFilter is a helper function to retrieve flavors using a filtern function.
// Hint: The API does not have a GetFlavors endpoint, only ListFlavors
func getFlavorsByFilter(
ctx context.Context,
client flavorsClientReader,
projectId, region string,
filter func(db postgresflex.ListFlavors) bool,
) ([]postgresflex.ListFlavors, error) {
if projectId == "" || region == "" {
return nil, fmt.Errorf("listing postgresflex flavors: projectId and region are required")
}
const pageSize = 25
var result = make([]postgresflex.ListFlavors, 0)
for page := int64(1); ; page++ {
res, err := client.GetFlavorsRequest(ctx, projectId, region).
Page(page).Size(pageSize).Sort(postgresflex.FLAVORSORT_INDEX_ASC).Execute()
if err != nil {
return nil, fmt.Errorf("requesting flavors list (page %d): %w", page, err)
}
// If the API returns no flavors, we have reached the end of the list.
if res.Flavors == nil || len(*res.Flavors) == 0 {
break
}
for _, flavor := range *res.Flavors {
if filter(flavor) {
result = append(result, flavor)
}
}
}
return result, nil
}