-
Notifications
You must be signed in to change notification settings - Fork 47
/
KeyPairHandler.go
231 lines (194 loc) · 6.79 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Proof of Concepts 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 a Cloud Driver Example for PoC Test.
//
// by ETRI, Innogrid, 2021.12.
// by ETRI, 2022.04.
package resources
import (
"fmt"
nhnsdk "github.com/cloud-barista/nhncloud-sdk-go"
"github.com/cloud-barista/nhncloud-sdk-go/openstack/compute/v2/extensions/keypairs"
call "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/call-log"
idrv "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces"
irs "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources"
)
type NhnCloudKeyPairHandler struct {
RegionInfo idrv.RegionInfo
VMClient *nhnsdk.ServiceClient
}
func (keyPairHandler *NhnCloudKeyPairHandler) CreateKey(keyPairReqInfo irs.KeyPairReqInfo) (irs.KeyPairInfo, error) {
cblogger.Info("NHN Cloud Driver: called CreateKey()")
callLogInfo := getCallLogScheme(keyPairHandler.RegionInfo.Region, call.VMKEYPAIR, keyPairReqInfo.IId.NameId, "CreateKey()")
if keyPairReqInfo.IId.NameId == "" {
newErr := fmt.Errorf("Invalid KeyPair NameId.")
cblogger.Error(newErr.Error())
LoggingError(callLogInfo, newErr)
return irs.KeyPairInfo{}, newErr
}
exist, err := checkExistKey(keyPairHandler.VMClient, keyPairReqInfo.IId)
if err != nil {
newErr := fmt.Errorf("Failed to Create Key. err = %s", err)
cblogger.Error(newErr.Error())
LoggingError(callLogInfo, newErr)
return irs.KeyPairInfo{}, newErr
}
if exist {
keyName := keyPairReqInfo.IId.SystemId
if keyPairReqInfo.IId.SystemId == "" {
keyName = keyPairReqInfo.IId.NameId
}
newErr := fmt.Errorf("Failed to Create Key. err = The Key name %s already exists", keyName)
cblogger.Error(newErr.Error())
LoggingError(callLogInfo, newErr)
return irs.KeyPairInfo{}, newErr
}
start := call.Start()
create0pts := keypairs.CreateOpts{
Name: keyPairReqInfo.IId.NameId,
PublicKey: "",
}
keyPair, err := keypairs.Create(keyPairHandler.VMClient, create0pts).Extract()
if err != nil {
newErr := fmt.Errorf("Failed to Create Key. err = %s", err.Error())
cblogger.Error(newErr.Error())
LoggingError(callLogInfo, newErr)
return irs.KeyPairInfo{}, newErr
}
LoggingInfo(callLogInfo, start)
keyPairInfo := mappingKeypairInfo(*keyPair)
return *keyPairInfo, nil
}
func (keyPairHandler *NhnCloudKeyPairHandler) ListKey() ([]*irs.KeyPairInfo, error) {
cblogger.Info("NHN Cloud Driver: called ListKey()")
callLogInfo := getCallLogScheme(keyPairHandler.RegionInfo.Region, call.VMKEYPAIR, "ListKey()", "ListKey()")
start := call.Start()
var listOptsBuilder keypairs.ListOptsBuilder
allPages, err := keypairs.List(keyPairHandler.VMClient, listOptsBuilder).AllPages()
if err != nil {
newErr := fmt.Errorf("Failed to Get KeyList err = %s", err.Error())
cblogger.Error(newErr.Error())
LoggingError(callLogInfo, newErr)
return nil, newErr
}
LoggingInfo(callLogInfo, start)
keypair, err := keypairs.ExtractKeyPairs(allPages)
if err != nil {
newErr := fmt.Errorf("Failed to Get KeyList err = %s", err.Error())
cblogger.Error(newErr.Error())
LoggingError(callLogInfo, newErr)
return nil, newErr
}
keyPairList := make([]*irs.KeyPairInfo, len(keypair))
for i, k := range keypair {
keyPairList[i] = mappingKeypairInfo(k)
}
return keyPairList, nil
}
func (keyPairHandler *NhnCloudKeyPairHandler) GetKey(keyIID irs.IID) (irs.KeyPairInfo, error) {
cblogger.Info("NHN Cloud Driver: called GetKey()")
callLogInfo := getCallLogScheme(keyPairHandler.RegionInfo.Region, call.VMKEYPAIR, keyIID.NameId, "GetKey()")
if iidCheck := checkIIDValidation(keyIID); !iidCheck {
newErr := fmt.Errorf("Failed to Get Key. InValid IID")
cblogger.Error(newErr.Error())
LoggingError(callLogInfo, newErr)
return irs.KeyPairInfo{}, newErr
}
start := call.Start()
keyPair, err := getRawKey(keyPairHandler.VMClient, keyIID)
if err != nil {
newErr := fmt.Errorf("Failed to Get Key. %s", err.Error())
cblogger.Error(newErr.Error())
LoggingError(callLogInfo, newErr)
return irs.KeyPairInfo{}, newErr
}
LoggingInfo(callLogInfo, start)
keyPairInfo := mappingKeypairInfo(keyPair)
return *keyPairInfo, nil
}
func (keyPairHandler *NhnCloudKeyPairHandler) DeleteKey(keyIID irs.IID) (bool, error) {
cblogger.Info("NHN Cloud Driver: called DeleteKey()")
callLogInfo := getCallLogScheme(keyPairHandler.RegionInfo.Region, call.VMKEYPAIR, keyIID.NameId, "DeleteKey()")
exist, err := checkExistKey(keyPairHandler.VMClient, keyIID)
if err != nil {
delErr := fmt.Errorf("Failed to Delete Key. %s", err)
cblogger.Error(delErr.Error())
LoggingError(callLogInfo, delErr)
return false, delErr
}
if !exist {
keyName := keyIID.SystemId
if keyIID.SystemId == "" {
keyName = keyIID.NameId
}
delErr := fmt.Errorf("Failed to Delete Key. The Key name %s not found", keyName)
cblogger.Error(delErr.Error())
LoggingError(callLogInfo, delErr)
return false, delErr
}
start := call.Start()
var delOptsBuilder keypairs.DeleteOptsBuilder
err = keypairs.Delete(keyPairHandler.VMClient, keyIID.NameId, delOptsBuilder).ExtractErr()
if err != nil {
delErr := fmt.Errorf("Failed to Delete Key : %v", err.Error())
cblogger.Error(delErr.Error())
LoggingError(callLogInfo, delErr)
return false, delErr
}
LoggingInfo(callLogInfo, start)
return true, nil
}
func checkExistKey(client *nhnsdk.ServiceClient, keyIID irs.IID) (bool, error) {
if ok := checkIIDValidation(keyIID); !ok {
return false, fmt.Errorf("Invalid KeyPair IID!!")
}
keyName := keyIID.SystemId
if keyIID.SystemId == "" {
keyName = keyIID.NameId
}
var listOptsBuilder keypairs.ListOptsBuilder
allPages, err := keypairs.List(client, listOptsBuilder).AllPages()
if err != nil {
return false, err
}
keypairList, err := keypairs.ExtractKeyPairs(allPages)
if err != nil {
return false, err
}
for _, keypair := range keypairList {
if keypair.Name == keyName {
return true, nil
}
}
return false, nil
}
func getRawKey(client *nhnsdk.ServiceClient, keyIID irs.IID) (keypairs.KeyPair, error) {
keyName := keyIID.SystemId
if keyIID.SystemId == "" {
keyName = keyIID.NameId
}
var getOptsBuilder keypairs.GetOptsBuilder
keyPair, err := keypairs.Get(client, keyName, getOptsBuilder).Extract()
if err != nil {
return keypairs.KeyPair{}, err
}
return *keyPair, nil
}
func mappingKeypairInfo(keypair keypairs.KeyPair) *irs.KeyPairInfo {
cblogger.Info("NHN Cloud Driver: called mappingKeypairInfo()")
keypairInfo := &irs.KeyPairInfo{
IId: irs.IID{
NameId: keypair.Name,
SystemId: keypair.Name,
},
Fingerprint: keypair.Fingerprint,
PublicKey: keypair.PublicKey,
PrivateKey: keypair.PrivateKey,
VMUserID: DefaultVMUserName,
}
return keypairInfo
}