## Description
<!-- **Please link some issue here describing what you are trying to achieve.**
In case there is no issue present for your PR, please consider creating one.
At least please give us some description what you are trying to achieve and why your change is needed. -->
relates to #1234
## Checklist
- [ ] Issue was linked above
- [ ] Code format was applied: `make fmt`
- [ ] Examples were added / adjusted (see `examples/` directory)
- [x] Docs are up-to-date: `make generate-docs` (will be checked by CI)
- [ ] Unit tests got implemented or updated
- [ ] Acceptance tests got implemented or updated (see e.g. [here](f5f99d1709/stackit/internal/services/dns/dns_acc_test.go))
- [x] Unit tests are passing: `make test` (will be checked by CI)
- [x] No linter issues: `make lint` (will be checked by CI)
Reviewed-on: #38
Co-authored-by: Marcel S. Henselin <marcel.henselin@stackit.cloud>
Co-committed-by: Marcel S. Henselin <marcel.henselin@stackit.cloud>
65 lines
1.8 KiB
Go
65 lines
1.8 KiB
Go
package postgresFlexAlphaFlavor
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
postgresflex "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/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 := int32(1); ; page++ {
|
|
res, err := client.GetFlavorsRequest(ctx, projectId, region).
|
|
Page(page).Size(pageSize).Sort(postgresflex.FLAVORSORT_ID_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
|
|
}
|