This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
credential.go
65 lines (59 loc) · 2.08 KB
/
credential.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
package openstack
import (
"errors"
"fmt"
)
const (
KEYSTONE_V2 = "cb-keystone-v2"
KEYSTONE_V3 = "cb-keystone-v3"
SCOPE_TEMPLATE = "cb-keystone-v3-%s-scope"
)
var FACINGS = []string{"public", "admin", "internal"}
var SCOPES = []string{"default", "project", "domain"}
func (p *OpenstackProvider) GetCredentialParameters(stringFinder func(string) string, boolFinder func(string) bool) (map[string]interface{}, error) {
facing, err := validateAndGet(stringFinder("facing"), FACINGS)
if err != nil {
return nil, err
}
var credentialMap = make(map[string]interface{})
credentialMap["facing"] = facing
credentialMap["userName"] = stringFinder("tenant-user")
credentialMap["password"] = stringFinder("tenant-password")
credentialMap["endpoint"] = stringFinder("endpoint")
if len(stringFinder("user-domain")) != 0 {
credentialMap["keystoneVersion"] = KEYSTONE_V3
scope, err := validateAndGet(stringFinder("keystone-scope"), SCOPES)
if err != nil {
return nil, err
}
credentialMap["keystoneAuthScope"] = fmt.Sprintf(SCOPE_TEMPLATE, scope)
credentialMap["selector"] = credentialMap["keystoneAuthScope"]
credentialMap["userDomain"] = stringFinder("user-domain")
setIfSpecified(stringFinder, "project-domain-name", "projectDomainName", credentialMap)
setIfSpecified(stringFinder, "domain-name", "domainName", credentialMap)
setIfSpecified(stringFinder, "project-name", "projectName", credentialMap)
} else {
credentialMap["keystoneVersion"] = KEYSTONE_V2
credentialMap["selector"] = KEYSTONE_V2
credentialMap["tenantName"] = stringFinder("tenant-name")
}
return credentialMap, nil
}
func validateAndGet(value string, values []string) (string, error) {
if len(value) == 0 {
return values[0], nil
} else {
for _, v := range values {
if v == value {
return value, nil
}
}
}
return "", errors.New(fmt.Sprintf("%s not allowed", value))
}
func setIfSpecified(stringFinder func(string) string, flagName, propertyName string, credentialMap map[string]interface{}) {
property := stringFinder(flagName)
if len(property) > 0 {
credentialMap[propertyName] = property
}
}