* 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
204 lines
5.6 KiB
Go
204 lines
5.6 KiB
Go
package sqlserverFlexAlphaFlavor
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
sqlserverflex "github.com/mhenselin/terraform-provider-stackitprivatepreview/pkg/sqlserverflexalpha"
|
|
)
|
|
|
|
type flavorsClient interface {
|
|
GetFlavorsRequestExecute(
|
|
ctx context.Context,
|
|
projectId, region string,
|
|
page, size *int64,
|
|
sort *sqlserverflex.FlavorSort,
|
|
) (*sqlserverflex.GetFlavorsResponse, error)
|
|
}
|
|
|
|
// func loadFlavorId(ctx context.Context, client flavorsClient, model *Model, flavor *flavorModel, storage *storageModel) error {
|
|
// if model == nil {
|
|
// return fmt.Errorf("nil model")
|
|
// }
|
|
// if flavor == nil {
|
|
// return fmt.Errorf("nil flavor")
|
|
// }
|
|
// cpu := flavor.CPU.ValueInt64()
|
|
// if cpu == 0 {
|
|
// return fmt.Errorf("nil CPU")
|
|
// }
|
|
// ram := flavor.RAM.ValueInt64()
|
|
// if ram == 0 {
|
|
// return fmt.Errorf("nil RAM")
|
|
// }
|
|
//
|
|
// nodeType := flavor.NodeType.ValueString()
|
|
// if nodeType == "" {
|
|
// if model.Replicas.IsNull() || model.Replicas.IsUnknown() {
|
|
// return fmt.Errorf("nil NodeType")
|
|
// }
|
|
// switch model.Replicas.ValueInt64() {
|
|
// case 1:
|
|
// nodeType = "Single"
|
|
// case 3:
|
|
// nodeType = "Replica"
|
|
// default:
|
|
// return fmt.Errorf("unknown Replicas value: %d", model.Replicas.ValueInt64())
|
|
// }
|
|
// }
|
|
//
|
|
// storageClass := conversion.StringValueToPointer(storage.Class)
|
|
// if storageClass == nil {
|
|
// return fmt.Errorf("nil StorageClass")
|
|
// }
|
|
// storageSize := conversion.Int64ValueToPointer(storage.Size)
|
|
// if storageSize == nil {
|
|
// return fmt.Errorf("nil StorageSize")
|
|
// }
|
|
//
|
|
// projectId := model.ProjectId.ValueString()
|
|
// region := model.Region.ValueString()
|
|
//
|
|
// flavorList, err := getAllFlavors(ctx, client, projectId, region)
|
|
// if err != nil {
|
|
// return err
|
|
// }
|
|
//
|
|
// avl := ""
|
|
// foundFlavorCount := 0
|
|
// var foundFlavors []string
|
|
// for _, f := range flavorList {
|
|
// if f.Id == nil || f.Cpu == nil || f.Memory == nil {
|
|
// continue
|
|
// }
|
|
// if !strings.EqualFold(*f.NodeType, nodeType) {
|
|
// continue
|
|
// }
|
|
// if *f.Cpu == cpu && *f.Memory == ram {
|
|
// var useSc *sqlserverflex.FlavorStorageClassesStorageClass
|
|
// for _, sc := range *f.StorageClasses {
|
|
// if *sc.Class != *storageClass {
|
|
// continue
|
|
// }
|
|
// if *storageSize < *f.MinGB || *storageSize > *f.MaxGB {
|
|
// return fmt.Errorf("storage size %d out of bounds (min: %d - max: %d)", *storageSize, *f.MinGB, *f.MaxGB)
|
|
// }
|
|
// useSc = &sc
|
|
// }
|
|
// if useSc == nil {
|
|
// return fmt.Errorf("no storage class found for %s", *storageClass)
|
|
// }
|
|
//
|
|
// flavor.Id = types.StringValue(*f.Id)
|
|
// flavor.Description = types.StringValue(*f.Description)
|
|
// foundFlavors = append(foundFlavors, fmt.Sprintf("%s (%d/%d - %s)", *f.Id, *f.Cpu, *f.Memory, *f.NodeType))
|
|
// foundFlavorCount++
|
|
// }
|
|
// for _, cls := range *f.StorageClasses {
|
|
// avl = fmt.Sprintf("%s\n- %d CPU, %d GB RAM, storage %s (min: %d - max: %d)", avl, *f.Cpu, *f.Memory, *cls.Class, *f.MinGB, *f.MaxGB)
|
|
// }
|
|
// }
|
|
// if foundFlavorCount > 1 {
|
|
// return fmt.Errorf(
|
|
// "number of flavors returned: %d\nmultiple flavors found: %d flavors\n %s",
|
|
// len(flavorList),
|
|
// foundFlavorCount,
|
|
// strings.Join(foundFlavors, "\n "),
|
|
// )
|
|
// }
|
|
// if flavor.Id.ValueString() == "" {
|
|
// return fmt.Errorf("couldn't find flavor, available specs are:%s", avl)
|
|
// }
|
|
//
|
|
// return nil
|
|
//}
|
|
|
|
func getAllFlavors(ctx context.Context, client flavorsClient, projectId, region string) (
|
|
[]sqlserverflex.ListFlavors,
|
|
error,
|
|
) {
|
|
if projectId == "" || region == "" {
|
|
return nil, fmt.Errorf("listing sqlserverflex flavors: projectId and region are required")
|
|
}
|
|
var flavorList []sqlserverflex.ListFlavors
|
|
|
|
page := int64(1)
|
|
size := int64(10)
|
|
sort := sqlserverflex.FLAVORSORT_INDEX_ASC
|
|
counter := 0
|
|
for {
|
|
res, err := client.GetFlavorsRequestExecute(ctx, projectId, region, &page, &size, &sort)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("listing sqlserverflex flavors: %w", err)
|
|
}
|
|
if res.Flavors == nil {
|
|
return nil, fmt.Errorf("finding flavors for project %s", projectId)
|
|
}
|
|
pagination := res.GetPagination()
|
|
flavors := res.GetFlavors()
|
|
flavorList = append(flavorList, flavors...)
|
|
|
|
if *pagination.TotalRows < int64(len(flavorList)) {
|
|
return nil, fmt.Errorf("total rows is smaller than current accumulated list - that should not happen")
|
|
}
|
|
if *pagination.TotalRows == int64(len(flavorList)) {
|
|
break
|
|
}
|
|
page++
|
|
|
|
if page > *pagination.TotalPages {
|
|
break
|
|
}
|
|
|
|
// implement a breakpoint
|
|
counter++
|
|
if counter > 1000 {
|
|
panic("too many pagination results")
|
|
}
|
|
}
|
|
return flavorList, nil
|
|
}
|
|
|
|
// func getFlavorModelById(ctx context.Context, client flavorsClient, model *Model, flavor *flavorModel) error {
|
|
// if model == nil {
|
|
// return fmt.Errorf("nil model")
|
|
// }
|
|
// if flavor == nil {
|
|
// return fmt.Errorf("nil flavor")
|
|
// }
|
|
// id := conversion.StringValueToPointer(flavor.Id)
|
|
// if id == nil {
|
|
// return fmt.Errorf("nil flavor ID")
|
|
// }
|
|
//
|
|
// flavor.Id = types.StringValue("")
|
|
//
|
|
// projectId := model.ProjectId.ValueString()
|
|
// region := model.Region.ValueString()
|
|
//
|
|
// flavorList, err := getAllFlavors(ctx, client, projectId, region)
|
|
// if err != nil {
|
|
// return err
|
|
// }
|
|
//
|
|
// avl := ""
|
|
// for _, f := range flavorList {
|
|
// if f.Id == nil || f.Cpu == nil || f.Memory == nil {
|
|
// continue
|
|
// }
|
|
// if *f.Id == *id {
|
|
// flavor.Id = types.StringValue(*f.Id)
|
|
// flavor.Description = types.StringValue(*f.Description)
|
|
// flavor.CPU = types.Int64Value(*f.Cpu)
|
|
// flavor.RAM = types.Int64Value(*f.Memory)
|
|
// flavor.NodeType = types.StringValue(*f.NodeType)
|
|
// break
|
|
// }
|
|
// avl = fmt.Sprintf("%s\n- %d CPU, %d GB RAM", avl, *f.Cpu, *f.Memory)
|
|
// }
|
|
// if flavor.Id.ValueString() == "" {
|
|
// return fmt.Errorf("couldn't find flavor, available specs are: %s", avl)
|
|
// }
|
|
//
|
|
// return nil
|
|
//}
|