/
config.go
91 lines (79 loc) · 2.37 KB
/
config.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
package internal
import (
"fmt"
"time"
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"golang.org/x/exp/slices"
)
type ConfigStruct struct {
AuthenticationData AuthenticationData
Namespaces []string
Port int
WaitDuration time.Duration
ResourceIdNameMappingFlag bool
}
type AuthenticationData struct {
Username string
Password string
AccessKey string
SecretKey string
IsAkSkAuthentication bool
ProjectId string
DomainName string
Region OtcRegion
}
func (ad AuthenticationData) ToOtcGopherAuthOptionsProvider() golangsdk.AuthOptionsProvider {
var opts golangsdk.AuthOptionsProvider
if ad.IsAkSkAuthentication {
opts = golangsdk.AKSKAuthOptions{
IdentityEndpoint: ad.Region.IamEndpoint(),
AccessKey: ad.AccessKey,
SecretKey: ad.SecretKey,
ProjectId: ad.ProjectId,
}
} else {
opts = golangsdk.AuthOptions{
IdentityEndpoint: ad.Region.IamEndpoint(),
Username: ad.Username,
Password: ad.Password,
DomainName: ad.DomainName,
TenantID: ad.ProjectId,
AllowReauth: true,
}
}
return opts
}
type OtcRegion string
const (
otcRegionEuDe OtcRegion = "eu-de"
otcRegionEuNl OtcRegion = "eu-nl"
)
func NewOtcRegionFromString(region string) (OtcRegion, error) {
otcRegion := OtcRegion(region)
if slices.Contains([]OtcRegion{otcRegionEuNl, otcRegionEuDe}, otcRegion) {
return otcRegion, nil
}
return "", fmt.Errorf("invalid argument %s does not represent a valid region", region)
}
func (r OtcRegion) IamEndpoint() string {
return fmt.Sprintf("https://iam.%s.otc.t-systems.com:443/v3", r)
}
// ResolveOtcShortHandNamespace maps the short code for the namespaces to the actual namespace name.
func ResolveOtcShortHandNamespace(namespaces []string, logger ILogger) []string {
fullNamespaces := make([]string, len(namespaces))
for i, v := range namespaces {
correctNamespaceName, ok := OtcNamespacesMapping[v]
fullNamespaces[i] = v
if ok {
fullNamespaces[i] = correctNamespaceName
if logger != nil {
logger.Debug("Mapping shorthand to full namespace", "from", v, "to", correctNamespaceName)
}
} else {
if logger != nil {
logger.Debug("Mapping not performed for input namespace", "namespace", v)
}
}
}
return fullNamespaces
}