feat: initial copy of v0.1.0
This commit is contained in:
parent
4cc801a7f3
commit
7d4cbb6b08
538 changed files with 63361 additions and 55213 deletions
|
|
@ -0,0 +1,87 @@
|
|||
package postgresflexalpha
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v3alpha1api"
|
||||
)
|
||||
|
||||
// databaseClientReader represents the contract to listing databases from postgresflex.APIClient.
|
||||
type databaseClientReader interface {
|
||||
ListDatabasesRequest(
|
||||
ctx context.Context,
|
||||
projectId string,
|
||||
region string,
|
||||
instanceId string,
|
||||
) v3alpha1api.ApiListDatabasesRequestRequest
|
||||
}
|
||||
|
||||
// getDatabaseById gets a database by its ID.
|
||||
func getDatabaseById(
|
||||
ctx context.Context,
|
||||
client databaseClientReader,
|
||||
projectId, region, instanceId string,
|
||||
databaseId int64,
|
||||
) (*v3alpha1api.ListDatabase, error) {
|
||||
filter := func(db v3alpha1api.ListDatabase) bool {
|
||||
return int64(db.Id) == databaseId
|
||||
}
|
||||
return getDatabase(ctx, client, projectId, region, instanceId, filter)
|
||||
}
|
||||
|
||||
// getDatabaseByName gets a database by its name.
|
||||
func getDatabaseByName(
|
||||
ctx context.Context,
|
||||
client databaseClientReader,
|
||||
projectId, region, instanceId, databaseName string,
|
||||
) (*v3alpha1api.ListDatabase, error) {
|
||||
filter := func(db v3alpha1api.ListDatabase) bool {
|
||||
return db.Name == databaseName
|
||||
}
|
||||
return getDatabase(ctx, client, projectId, region, instanceId, filter)
|
||||
}
|
||||
|
||||
// getDatabase is a helper function to retrieve a database using a filter function.
|
||||
// Hint: The API does not have a GetDatabase endpoint, only ListDatabases
|
||||
func getDatabase(
|
||||
ctx context.Context,
|
||||
client databaseClientReader,
|
||||
projectId, region, instanceId string,
|
||||
filter func(db v3alpha1api.ListDatabase) bool,
|
||||
) (*v3alpha1api.ListDatabase, error) {
|
||||
if projectId == "" || region == "" || instanceId == "" {
|
||||
return nil, fmt.Errorf("all parameters (project, region, instance) are required")
|
||||
}
|
||||
|
||||
const pageSize = 25
|
||||
|
||||
for page := int32(1); ; page++ {
|
||||
res, err := client.ListDatabasesRequest(ctx, projectId, region, instanceId).
|
||||
Page(page).Size(pageSize).Sort(v3alpha1api.DATABASESORT_DATABASE_ID_ASC).Execute()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("requesting database list (page %d): %w", page, err)
|
||||
}
|
||||
|
||||
// If the API returns no databases, we have reached the end of the list.
|
||||
if len(res.Databases) == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
// Iterate over databases to find a match
|
||||
for _, db := range res.Databases {
|
||||
if filter(db) {
|
||||
foundDb := db
|
||||
return &foundDb, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("database not found for instance %s", instanceId)
|
||||
}
|
||||
|
||||
// cleanString removes leading and trailing quotes which are sometimes returned by the API.
|
||||
func cleanString(s string) string {
|
||||
return strings.Trim(s, "\"")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue