-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeystone.go
60 lines (50 loc) · 1.07 KB
/
keystone.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"os"
"crypto/tls"
)
type keystoneResponse struct {
Access struct {
Token struct {
Id string
}
}
}
// GetKeystoneToken extract new token from KeystoneV2
func GetKeystoneToken(endpoint string, username string, password string, tenant string) string {
url := endpoint + "/v2.0/tokens"
authBody := `{
"auth": {
"passwordCredentials": {
"username": "` + username + `",
"password": "` + password + `"
},
"tenantName": "` + tenant + `"
}
}`
// For https we will drop cert verification
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
res, err := client.Post(
url,
"application/json; charset=utf-8",
strings.NewReader(authBody))
if err != nil {
fmt.Println("Can't get responce from Keystone.")
os.Exit(1)
}
body2, _ := ioutil.ReadAll(res.Body)
kr := keystoneResponse{}
err = json.Unmarshal(body2, &kr)
if err != nil {
fmt.Println(err.Error())
}
return kr.Access.Token.Id
}