/
cvcontext.go
59 lines (42 loc) · 1.21 KB
/
cvcontext.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
// (C) Copyright 2019 Hewlett Packard Enterprise Development LP.
package cloudvolume
import (
"github.com/HewlettPackard/hpecli/internal/platform/context"
)
const cvAPIKeyPrefix = "hpecli_cloudvolume_token_"
const cvContextKey = "hpecli_cloudvolume_context"
const cvDefaultHost = "https://demo.cloudvolumes.hpe.com"
func hostAndToken() (host, token string, err error) {
c := context.New(cvContextKey)
apiEndpoint, err := c.ModuleContext()
if err != nil {
return "", "", err
}
key := dataKey(apiEndpoint)
if err = c.HostData(key, &token); err != nil {
return "", "", err
}
return apiEndpoint, token, nil
}
func saveData(apiEndpoint, token string) error {
c := context.New(cvContextKey)
if err := c.SetModuleContext(apiEndpoint); err != nil {
return err
}
key := dataKey(apiEndpoint)
return c.SetHostData(key, token)
}
func dataKey(apiEndpoint string) string {
return cvAPIKeyPrefix + apiEndpoint
}
func hostData(host string) (token string, err error) {
c := context.New(cvContextKey)
if err = c.HostData(dataKey(host), &token); err != nil {
return "", err
}
return token, nil
}
func deleteSavedHostData(host string) error {
c := context.New(cvContextKey)
return c.DeleteHostData(dataKey(host))
}