-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutils.go
107 lines (90 loc) · 3.2 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package utils
import (
"context"
"fmt"
"strings"
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
"github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex"
)
const (
ServiceCmd = "beta sqlserverflex"
)
// enforce implementation of interfaces
var (
_ SQLServerFlexClient = &sqlserverflex.APIClient{}
)
type SQLServerFlexClient interface {
ListVersionsExecute(ctx context.Context, projectId string, region string) (*sqlserverflex.ListVersionsResponse, error)
GetInstanceExecute(ctx context.Context, projectId, instanceId string, region string) (*sqlserverflex.GetInstanceResponse, error)
GetUserExecute(ctx context.Context, projectId, instanceId, userId string, region string) (*sqlserverflex.GetUserResponse, error)
}
func ValidateFlavorId(flavorId string, flavors *[]sqlserverflex.InstanceFlavorEntry) error {
if flavors == nil {
return fmt.Errorf("nil flavors")
}
for _, f := range *flavors {
if f.Id != nil && strings.EqualFold(*f.Id, flavorId) {
return nil
}
}
return &errors.DatabaseInvalidFlavorError{
Service: ServiceCmd,
Details: fmt.Sprintf("You provided flavor ID '%s', which is invalid.", flavorId),
}
}
func ValidateStorage(storageClass *string, storageSize *int64, storages *sqlserverflex.ListStoragesResponse, flavorId string) error {
if storages == nil {
return fmt.Errorf("nil storages")
}
if storageSize != nil {
if *storageSize < *storages.StorageRange.Min || *storageSize > *storages.StorageRange.Max {
return fmt.Errorf("%s", fmt.Sprintf("You provided storage size '%d', which is invalid. The valid range is %d-%d.", *storageSize, *storages.StorageRange.Min, *storages.StorageRange.Max))
}
}
if storageClass == nil {
return nil
}
for _, sc := range *storages.StorageClasses {
if strings.EqualFold(*storageClass, sc) {
return nil
}
}
return &errors.DatabaseInvalidStorageError{
Service: ServiceCmd,
Details: fmt.Sprintf("You provided storage class '%s', which is invalid.", *storageClass),
FlavorId: flavorId,
}
}
func LoadFlavorId(cpu, ram int64, flavors *[]sqlserverflex.InstanceFlavorEntry) (*string, error) {
if flavors == nil {
return nil, fmt.Errorf("nil flavors")
}
availableFlavors := ""
for _, f := range *flavors {
if f.Id == nil || f.Cpu == nil || f.Memory == nil {
continue
}
if *f.Cpu == cpu && *f.Memory == ram {
return f.Id, nil
}
availableFlavors = fmt.Sprintf("%s\n- %d CPU, %d GB RAM", availableFlavors, *f.Cpu, *f.Cpu)
}
return nil, &errors.DatabaseInvalidFlavorError{
Service: ServiceCmd,
Details: "You provided an invalid combination for CPU and RAM.",
}
}
func GetInstanceName(ctx context.Context, apiClient SQLServerFlexClient, projectId, instanceId, region string) (string, error) {
resp, err := apiClient.GetInstanceExecute(ctx, projectId, instanceId, region)
if err != nil {
return "", fmt.Errorf("get SQLServer Flex instance: %w", err)
}
return *resp.Item.Name, nil
}
func GetUserName(ctx context.Context, apiClient SQLServerFlexClient, projectId, instanceId, userId, region string) (string, error) {
resp, err := apiClient.GetUserExecute(ctx, projectId, instanceId, userId, region)
if err != nil {
return "", fmt.Errorf("get SQLServer Flex user: %w", err)
}
return *resp.Item.Username, nil
}