-
Notifications
You must be signed in to change notification settings - Fork 47
/
InfoStore.go
183 lines (144 loc) · 6.29 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// CloudDriverInfo <-> CB-Store Handler for Cloud Driver Info. Manager.
// Cloud Driver Info. Manager of CB-Spider.
// 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, 2020.03.
package iidmanager
import (
"fmt"
"github.com/cloud-barista/cb-store/utils"
"github.com/cloud-barista/cb-store"
"github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources"
icbs "github.com/cloud-barista/cb-store/interfaces"
)
var store icbs.Store
func init() {
store = cbstore.GetStore()
}
/* //====================================================================
type IIDInfo struct {
ConnectionName string // ex) "aws-seoul-config"
ResourceType string // ex) "VM"
IId resources.IID // ex) {NameId, SystemId} = {"powerkim_vm_01", "i-0bc7123b7e5cbf79d"}
}
*/ //====================================================================
// format
// /resource-info-spaces/iids/{ConnectionName}/{ResourceType}/<IID.NameId> [IID.SystemId]
// Key: "/resource-info-spaces/iids/{ConnectionName}/{ResourceType}/<IID.NameId>"
// Value: "[IID.SystemId]"
// ex) /resource-info-spaces/iids/aws-seoul-config/VM/powerkim_vm_01 [i-0bc7123b7e5cbf79d]
func insertInfo(connectionName string, resourceType string, iid resources.IID) error {
// ex) /resource-info-spaces/iids/aws-seoul-config/VM/powerkim_vm_01 [i-0bc7123b7e5cbf79d]
key := "/resource-info-spaces/iids/" + connectionName + "/" + resourceType + "/" + iid.NameId
value := iid.SystemId
err := store.Put(key, value)
if err != nil {
//cblog.Error(err)
return err
}
return nil
}
// 1. get key-value list
// 2. create IIDInfo List & return
func listInfo(connectionName string, resourceType string) ([]*IIDInfo, error) {
// ex) /resource-info-spaces/iids/aws-seoul-config/VM/powerkim_vm_01 [i-0bc7123b7e5cbf79d]
key := "/resource-info-spaces/iids/" + connectionName + "/" + resourceType
keyValueList, err := store.GetList(key, true)
if err != nil {
return nil, err
}
iidInfoList := make([]*IIDInfo, len(keyValueList))
for count, kv := range keyValueList {
iidInfo := &IIDInfo{connectionName, resourceType, resources.IID{utils.GetNodeValue(kv.Key, 5), kv.Value} }
iidInfoList[count] = iidInfo
}
return iidInfoList, nil
}
// 1. get a key-value
// 2. create IIDInfo & return
func getInfo(connectionName string, resourceType string, nameId string) (*IIDInfo, error) {
// ex) /resource-info-spaces/iids/aws-seoul-config/VM/powerkim_vm_01 [i-0bc7123b7e5cbf79d]
key := "/resource-info-spaces/iids/" + connectionName + "/" + resourceType + "/" + nameId
// 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("[" + connectionName + ":" + resourceType + ":" + nameId + "] does not exist!")
// }
for _, kv := range keyValueList {
// keyValueList should have ~/nameId/... or ~/nameId-01/...,
// so we have to check the sameness of nameId.
if utils.GetNodeValue(kv.Key, 5) == nameId {
iidInfo := &IIDInfo{connectionName, resourceType, resources.IID{nameId, kv.Value} }
return iidInfo, nil
}
}
return nil, fmt.Errorf("[" + connectionName + ":" + resourceType + ":" + nameId + "] does not exist!")
}
// 1. get list
// 2. find keyvalue by value
// 2. create IIDInfo & return
func getInfoByValue(connectionName string, resourceType string, systemId string) (*IIDInfo, error) {
// ex) /resource-info-spaces/iids/aws-seoul-config/VM/??? [i-0bc7123b7e5cbf79d]
key := "/resource-info-spaces/iids/" + connectionName + "/" + resourceType
keyValueList, err := store.GetList(key, true)
if err != nil {
return nil, err
}
for _, kv := range keyValueList {
// keyValueList should have ~/nameId/... or ~/nameId-01/...,
// so we have to check the sameness of nameId.
if kv.Value == systemId {
iidInfo := &IIDInfo{connectionName, resourceType, resources.IID{utils.GetNodeValue(kv.Key, 5), systemId} }
return iidInfo, nil
}
}
return &IIDInfo{}, nil
}
// 1. get a key-value
// 2. return existence of or not
func isExist(connectionName string, resourceType string, nameId string) (bool, error) {
// ex) /resource-info-spaces/iids/aws-seoul-config/VM/powerkim_vm_01 [i-0bc7123b7e5cbf79d]
key := "/resource-info-spaces/iids/" + connectionName + "/" + resourceType + "/" + nameId
// 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 ~/nameId/... or ~/nameId-01/...,
// so we have to check the sameness of nameId.
if utils.GetNodeValue(kv.Key, 5) == nameId {
return true, nil
}
}
return false, nil
}
// 1. get the original Key.
// 2. delete the key.
func deleteInfo(connectionName string, resourceType string, nameId string) (bool, error) {
// ex) /resource-info-spaces/iids/aws-seoul-config/VM/powerkim_vm_01 [i-0bc7123b7e5cbf79d]
key := "/resource-info-spaces/iids/" + connectionName + "/" + resourceType + "/" + nameId
// 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 ~/nameId/... or ~/nameId-01/...,
// so we have to check the sameness of nameId.
if utils.GetNodeValue(kv.Key, 5) == nameId {
err = store.Delete(kv.Key)
if err != nil {
return false, err
}
return true, nil
}
}
return false, fmt.Errorf("[" + connectionName + ":" + resourceType + ":" + nameId + "] does not exist!")
}