-
Notifications
You must be signed in to change notification settings - Fork 48
/
KeyPairHandler.go
139 lines (114 loc) · 3.82 KB
/
KeyPairHandler.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
// Cloud Driver Interface 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
//
// This is Mock Driver.
//
// by CB-Spider Team, 2020.09.
package resources
import (
"fmt"
cblog "github.com/cloud-barista/cb-log"
irs "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources"
_ "github.com/sirupsen/logrus"
)
var keyPairInfoMap map[string][]*irs.KeyPairInfo
type MockKeyPairHandler struct {
MockName string
}
func init() {
// cblog is a global variable.
keyPairInfoMap = make(map[string][]*irs.KeyPairInfo)
}
// (1) create keyPairInfo object
// (2) insert keyPairInfo into global Map
func (keyPairHandler *MockKeyPairHandler) CreateKey(keyPairReqInfo irs.KeyPairReqInfo) (irs.KeyPairInfo, error) {
cblogger := cblog.GetLogger("CB-SPIDER")
cblogger.Info("Mock Driver: called CreateKey()!")
mockName := keyPairHandler.MockName
keyPairReqInfo.IId.SystemId = keyPairReqInfo.IId.NameId
// (1) create keyPairInfo object
keyPairInfo := irs.KeyPairInfo{keyPairReqInfo.IId,
"XXXXFingerprint", "XXXXPublicKey", "XXXXPrivateKey", "cb-user", nil}
// (2) insert KeyPairInfo into global Map
infoList, _ := keyPairInfoMap[mockName]
infoList = append(infoList, &keyPairInfo)
keyPairInfoMap[mockName] = infoList
return CloneKeyPairInfo(keyPairInfo), nil
}
func CloneKeyPairInfoList(srcInfoList []*irs.KeyPairInfo) []*irs.KeyPairInfo {
clonedInfoList := []*irs.KeyPairInfo{}
for _, srcInfo := range srcInfoList {
clonedInfo := CloneKeyPairInfo(*srcInfo)
clonedInfoList = append(clonedInfoList, &clonedInfo)
}
return clonedInfoList
}
func CloneKeyPairInfo(srcInfo irs.KeyPairInfo) irs.KeyPairInfo {
/*
type KeyPairInfo struct {
IId IID // {NameId, SystemId}
Fingerprint string
PublicKey string
PrivateKey string
VMUserID string
KeyValueList []KeyValue
}
*/
// clone KeyPairInfo
clonedInfo := irs.KeyPairInfo{
IId: irs.IID{srcInfo.IId.NameId, srcInfo.IId.SystemId},
Fingerprint: srcInfo.Fingerprint,
PublicKey: srcInfo.PublicKey,
PrivateKey: srcInfo.PrivateKey,
VMUserID: srcInfo.VMUserID,
KeyValueList: srcInfo.KeyValueList, // now, do not need cloning
}
return clonedInfo
}
func (keyPairHandler *MockKeyPairHandler) ListKey() ([]*irs.KeyPairInfo, error) {
cblogger := cblog.GetLogger("CB-SPIDER")
cblogger.Info("Mock Driver: called ListKey()!")
mockName := keyPairHandler.MockName
infoList, ok := keyPairInfoMap[mockName]
if !ok {
return []*irs.KeyPairInfo{}, nil
}
// cloning list of KeyPair
return CloneKeyPairInfoList(infoList), nil
}
func (keyPairHandler *MockKeyPairHandler) GetKey(iid irs.IID) (irs.KeyPairInfo, error) {
cblogger := cblog.GetLogger("CB-SPIDER")
cblogger.Info("Mock Driver: called GetKey()!")
infoList, err := keyPairHandler.ListKey()
if err != nil {
cblogger.Error(err)
return irs.KeyPairInfo{}, err
}
for _, info := range infoList {
if (*info).IId.NameId == iid.NameId {
return *info, nil
}
}
return irs.KeyPairInfo{}, fmt.Errorf("%s keypair does not exist!!", iid.NameId)
}
func (keyPairHandler *MockKeyPairHandler) DeleteKey(iid irs.IID) (bool, error) {
cblogger := cblog.GetLogger("CB-SPIDER")
cblogger.Info("Mock Driver: called DeleteKey()!")
infoList, err := keyPairHandler.ListKey()
if err != nil {
cblogger.Error(err)
return false, err
}
mockName := keyPairHandler.MockName
for idx, info := range infoList {
if info.IId.SystemId == iid.SystemId {
infoList = append(infoList[:idx], infoList[idx+1:]...)
keyPairInfoMap[mockName] = infoList
return true, nil
}
}
return false, nil
}