-
Notifications
You must be signed in to change notification settings - Fork 44
/
InfoStore.go
116 lines (96 loc) · 3.84 KB
/
InfoStore.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// KeyPair <-> CB-Store Handler for Cloud Driver
// The CB-Spider is a sub-Framework of the Cloud-Barista Multi-Cloud Project.
// The CB-Spider Mission is to connect all the clouds with a single interface.
//
// * Cloud-Barista: https://github.com/cloud-barista
//
// by CB-Spider Team, 2021.11.
package common
import (
"fmt"
"github.com/cloud-barista/cb-store/utils"
"github.com/cloud-barista/cb-store"
icbs "github.com/cloud-barista/cb-store/interfaces"
irs "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources"
)
var store icbs.Store
const STORE_KEYPAIR_PREFIX string ="/driver-info-spaces/keypair"
func init() {
store = cbstore.GetStore()
}
// format
// /driver-info-spaces/keypair/{Param1}/{Param2}/{Param3} [Param4]
// /driver-info-spaces/keypair/{ProviderName}/{HashString}/{KeyPairNameId} [privateKey]
// ex) /driver-info-spaces/keypair/CLOUDIT/c4240bec42480e764a4381c10c92e2ce/keypair-0-c6ncl9aba5o081np93og [private key]
func insertInfo(providerName string, hashString string, keyPairNameId string, privateKey string) error {
key := STORE_KEYPAIR_PREFIX + "/" + providerName + "/" + hashString + "/" + keyPairNameId
err := store.Put(key, privateKey)
if err != nil {
//cblog.Error(err)
return err
}
return nil
}
// create KeyValue{KeyPairNameId, PrivateKey} List & return
func listInfo(providerName string, hashString string) ([]*irs.KeyValue, error) {
key := STORE_KEYPAIR_PREFIX + "/" + providerName + "/" + hashString
keyValueList, err := store.GetList(key, true)
if err != nil {
return nil, err
}
keyList := make([]*irs.KeyValue, len(keyValueList))
for count, kv := range keyValueList {
keyValue := &irs.KeyValue{
Key : utils.GetNodeValue(kv.Key, 5), // KeyPairNameId
Value : kv.Value, // private key
}
keyList[count] = keyValue
}
return keyList, nil
}
// create KeyValue{KeyPairNameId, PrivateKey} & return
func getInfo(providerName string, hashString string, keyPairNameId string) (*irs.KeyValue, error) {
key := STORE_KEYPAIR_PREFIX + "/" + providerName + "/" + hashString + "/" + keyPairNameId
// key is not the key of cb-store, so we have to use GetList()
keyValueList, err := store.GetList(key, true)
if err != nil {
return nil, err
}
if len(keyValueList) < 1 {
return nil, fmt.Errorf(keyPairNameId + ": does not exist!")
}
for _, kv := range keyValueList {
// keyValueList should have ~/keypair or ~/keypair-01
// so we have to check the sameness of keyPairNameId.
if utils.GetNodeValue(kv.Key, 5) == keyPairNameId {
keyValue := &irs.KeyValue{
Key : utils.GetNodeValue(kv.Key, 5), // KeyPairNameId
Value : kv.Value, // private key
}
return keyValue, nil
} // end of if
} // end of for
return nil, fmt.Errorf(keyPairNameId + ": does not exist!")
}
// 1. get the original Key.
// 2. delete the key.
func deleteInfo(providerName string, hashString string, keyPairNameId string) (bool, error) {
key := STORE_KEYPAIR_PREFIX + "/" + providerName + "/" + hashString + "/" + keyPairNameId
// key is not the key of cb-store, so we have to use GetList(
keyValueList, err := store.GetList(key, true)
if err != nil {
return false, err
}
for _, kv := range keyValueList {
// keyValueList should have ~/keypair or ~/keypair-01
// so we have to check the sameness of keyPairNameId.
if utils.GetNodeValue(kv.Key, 5) == keyPairNameId {
err = store.Delete(kv.Key)
if err != nil {
return false, err
}
return true, nil
}
}
return false, fmt.Errorf(keyPairNameId + ": does not exist!")
}