Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[openstack] - 키페어 관리 로직 개선 (이슈 #480) #516

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package resources

import (
"errors"
"fmt"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/keypairs"

Expand Down Expand Up @@ -32,48 +34,75 @@ func setterKeypair(keypair keypairs.KeyPair) *irs.KeyPairInfo {
func (keyPairHandler *OpenStackKeyPairHandler) CreateKey(keyPairReqInfo irs.KeyPairReqInfo) (irs.KeyPairInfo, error) {
// log HisCall
hiscallInfo := GetCallLogScheme(keyPairHandler.Client.IdentityEndpoint, call.VMKEYPAIR, keyPairReqInfo.IId.NameId, "CreateKey()")
start := call.Start()

// 0. Check keyPairReqInfo
err := CheckKeyPairReqInfo(keyPairReqInfo)
if err != nil {
createErr := errors.New(fmt.Sprintf("Failed to Create Key. err = %s", err.Error()))
cblogger.Error(createErr.Error())
LoggingError(hiscallInfo, createErr)
return irs.KeyPairInfo{}, createErr
}

// 1. Check Exist
exist, err := CheckExistKey(keyPairHandler.Client, keyPairReqInfo.IId)
if err != nil {
createErr := errors.New(fmt.Sprintf("Failed to Create Key. err = %s", err))
cblogger.Error(createErr.Error())
LoggingError(hiscallInfo, createErr)
return irs.KeyPairInfo{}, createErr
}
if exist {
keyName := keyPairReqInfo.IId.SystemId
if keyPairReqInfo.IId.SystemId == "" {
keyName = keyPairReqInfo.IId.NameId
}
createErr := errors.New(fmt.Sprintf("Failed to Create Key. err = The Key name %s already exists", keyName))
cblogger.Error(createErr.Error())
LoggingError(hiscallInfo, createErr)
return irs.KeyPairInfo{}, createErr
}
// 2. Set keyPairReqInfo
create0pts := keypairs.CreateOpts{
Name: keyPairReqInfo.IId.NameId,
PublicKey: "",
}

start := call.Start()
// 3. Create KeyPair
keyPair, err := keypairs.Create(keyPairHandler.Client, create0pts).Extract()
if err != nil {
cblogger.Error(err.Error())
LoggingError(hiscallInfo, err)
return irs.KeyPairInfo{}, err
createErr := errors.New(fmt.Sprintf("Failed to Create Key. err = %s", err.Error()))
cblogger.Error(createErr.Error())
LoggingError(hiscallInfo, createErr)
return irs.KeyPairInfo{}, createErr
}
LoggingInfo(hiscallInfo, start)

// 생성된 KeyPair 정보 리턴
// 4. Set keyPairInfo
keyPairInfo := setterKeypair(*keyPair)
return *keyPairInfo, nil
}

func (keyPairHandler *OpenStackKeyPairHandler) ListKey() ([]*irs.KeyPairInfo, error) {
// log HisCall
hiscallInfo := GetCallLogScheme(keyPairHandler.Client.IdentityEndpoint, call.VMKEYPAIR, KeyPair, "ListKey()")

// 키페어 목록 조회
start := call.Start()
// 0. Get List Resource
pager, err := keypairs.List(keyPairHandler.Client).AllPages()
if err != nil {
cblogger.Error(err.Error())
LoggingError(hiscallInfo, err)
return nil, err
getErr := errors.New(fmt.Sprintf("Failed to Get KeyList err = %s", err.Error()))
cblogger.Error(getErr.Error())
LoggingError(hiscallInfo, getErr)
return nil, getErr
}
LoggingInfo(hiscallInfo, start)

keypair, err := keypairs.ExtractKeyPairs(pager)
if err != nil {
cblogger.Error(err.Error())
LoggingError(hiscallInfo, err)
return nil, err
getErr := errors.New(fmt.Sprintf("Failed to Get KeyList err = %s", err.Error()))
cblogger.Error(getErr.Error())
LoggingError(hiscallInfo, getErr)
return nil, getErr
}

// 키페어 목록 정보 매핑
// 1. Set List Resource
keyPairList := make([]*irs.KeyPairInfo, len(keypair))
for i, k := range keypair {
keyPairList[i] = setterKeypair(k)
Expand All @@ -84,31 +113,104 @@ func (keyPairHandler *OpenStackKeyPairHandler) ListKey() ([]*irs.KeyPairInfo, er
func (keyPairHandler *OpenStackKeyPairHandler) GetKey(keyIID irs.IID) (irs.KeyPairInfo, error) {
// log HisCall
hiscallInfo := GetCallLogScheme(keyPairHandler.Client.IdentityEndpoint, call.VMKEYPAIR, keyIID.NameId, "GetKey()")
// 0. Check keyPairInfo
if iidCheck := CheckIIDValidation(keyIID); !iidCheck {
getErr := errors.New(fmt.Sprintf("Failed to Get Key err = InValid IID"))
cblogger.Error(getErr.Error())
LoggingError(hiscallInfo, getErr)
return irs.KeyPairInfo{}, getErr
}

start := call.Start()
keyPair, err := keypairs.Get(keyPairHandler.Client, keyIID.NameId).Extract()

// 1. Get Resource
keyPair, err := GetRawKey(keyPairHandler.Client, keyIID)
if err != nil {
cblogger.Error(err.Error())
LoggingError(hiscallInfo, err)
return irs.KeyPairInfo{}, err
getErr := errors.New(fmt.Sprintf("Failed to Get Key. err = %s", err.Error()))
cblogger.Error(getErr.Error())
LoggingError(hiscallInfo, getErr)
return irs.KeyPairInfo{}, getErr
}
LoggingInfo(hiscallInfo, start)

keyPairInfo := setterKeypair(*keyPair)
// 2. Set Resource
keyPairInfo := setterKeypair(keyPair)
return *keyPairInfo, nil
}

func (keyPairHandler *OpenStackKeyPairHandler) DeleteKey(keyIID irs.IID) (bool, error) {
// log HisCall
hiscallInfo := GetCallLogScheme(keyPairHandler.Client.IdentityEndpoint, call.VMKEYPAIR, keyIID.NameId, "DeleteKey()")

// 0. Check keyPairInfo
exist, err := CheckExistKey(keyPairHandler.Client, keyIID)
if err != nil {
delErr := errors.New(fmt.Sprintf("Failed to Delete Key. err = %s", err))
cblogger.Error(delErr.Error())
LoggingError(hiscallInfo, delErr)
return false, delErr
}
// 1. Check Exist
if !exist {
keyName := keyIID.SystemId
if keyIID.SystemId == "" {
keyName = keyIID.NameId
}
delErr := errors.New(fmt.Sprintf("Failed to Delete Key. err = The Key name %s not found", keyName))
cblogger.Error(delErr.Error())
LoggingError(hiscallInfo, delErr)
return false, delErr
}
start := call.Start()
err := keypairs.Delete(keyPairHandler.Client, keyIID.NameId).ExtractErr()
// 2. Delete Resource
err = keypairs.Delete(keyPairHandler.Client, keyIID.NameId).ExtractErr()
if err != nil {
cblogger.Error(err.Error())
LoggingError(hiscallInfo, err)
return false, err
delErr := errors.New(fmt.Sprintf("Failed to Delete Key. err = %s", err.Error()))
cblogger.Error(delErr.Error())
LoggingError(hiscallInfo, delErr)
return false, delErr
}
LoggingInfo(hiscallInfo, start)
return true, nil
}

func CheckExistKey(client *gophercloud.ServiceClient, keyIID irs.IID) (bool, error) {
if ok := CheckIIDValidation(keyIID); !ok {
return false, errors.New(fmt.Sprintf("InValid IID"))
}
keyName := keyIID.SystemId
if keyIID.SystemId == "" {
keyName = keyIID.NameId
}
pager, err := keypairs.List(client).AllPages()
if err != nil {
return false, err
}
keypairList, err := keypairs.ExtractKeyPairs(pager)
if err != nil {
return false, err
}
for _, keypair := range keypairList {
if keypair.Name == keyName {
return true, nil
}
}
return false, nil
}

func CheckKeyPairReqInfo(keyPairReqInfo irs.KeyPairReqInfo) error {
if keyPairReqInfo.IId.NameId == "" {
return errors.New("invalid KeyPairReqInfo IID")
}
return nil
}

func GetRawKey(client *gophercloud.ServiceClient, keyIID irs.IID) (keypairs.KeyPair, error) {
keyName := keyIID.SystemId
if keyIID.SystemId == "" {
keyName = keyIID.NameId
}
keyPair, err := keypairs.Get(client, keyName).Extract()
if err != nil {
return keypairs.KeyPair{}, err
}
return *keyPair, nil
}