-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutils.go
87 lines (75 loc) · 2.59 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
package utils
import (
"context"
"fmt"
"strings"
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
"github.com/stackitcloud/stackit-sdk-go/services/redis"
)
const (
service = "redis"
)
func ValidatePlanId(planId string, offerings *redis.ListOfferingsResponse) error {
for _, offer := range *offerings.Offerings {
for _, plan := range *offer.Plans {
if plan.Id != nil && strings.EqualFold(*plan.Id, planId) {
return nil
}
}
}
return &errors.DSAInvalidPlanError{
Service: service,
Details: fmt.Sprintf("You provided plan ID %q, which is invalid.", planId),
}
}
func LoadPlanId(planName, version string, offerings *redis.ListOfferingsResponse) (*string, error) {
availableVersions := ""
availablePlanNames := ""
isValidVersion := false
for _, offer := range *offerings.Offerings {
if !strings.EqualFold(*offer.Version, version) {
availableVersions = fmt.Sprintf("%s\n- %s", availableVersions, *offer.Version)
continue
}
isValidVersion = true
for _, plan := range *offer.Plans {
if plan.Name == nil {
continue
}
if strings.EqualFold(*plan.Name, planName) && plan.Id != nil {
return plan.Id, nil
}
availablePlanNames = fmt.Sprintf("%s\n- %s", availablePlanNames, *plan.Name)
}
}
if !isValidVersion {
details := fmt.Sprintf("You provided version %q, which is invalid. Available versions are: %s", version, availableVersions)
return nil, &errors.DSAInvalidPlanError{
Service: service,
Details: details,
}
}
details := fmt.Sprintf("You provided plan_name %q for version %s, which is invalid. Available plan names for that version are: %s", planName, version, availablePlanNames)
return nil, &errors.DSAInvalidPlanError{
Service: service,
Details: details,
}
}
type RedisClient interface {
GetInstanceExecute(ctx context.Context, projectId, instanceId string) (*redis.Instance, error)
GetCredentialsExecute(ctx context.Context, projectId, instanceId, credentialsId string) (*redis.CredentialsResponse, error)
}
func GetInstanceName(ctx context.Context, apiClient RedisClient, projectId, instanceId string) (string, error) {
resp, err := apiClient.GetInstanceExecute(ctx, projectId, instanceId)
if err != nil {
return "", fmt.Errorf("get Redis instance: %w", err)
}
return *resp.Name, nil
}
func GetCredentialsUsername(ctx context.Context, apiClient RedisClient, projectId, instanceId, credentialsId string) (string, error) {
resp, err := apiClient.GetCredentialsExecute(ctx, projectId, instanceId, credentialsId)
if err != nil {
return "", fmt.Errorf("get Redis credentials: %w", err)
}
return *resp.Raw.Credentials.Username, nil
}