From 2a19c6b7a0659123d05b97f2e30e84f70b5faac1 Mon Sep 17 00:00:00 2001 From: tjeom-inno Date: Thu, 4 Nov 2021 12:55:56 +0900 Subject: [PATCH] =?UTF-8?q?[openstack]=20-=20=ED=82=A4=ED=8E=98=EC=96=B4?= =?UTF-8?q?=20=EA=B4=80=EB=A6=AC=20=EB=A1=9C=EC=A7=81=20=EA=B0=9C=EC=84=A0?= =?UTF-8?q?=20(=EC=9D=B4=EC=8A=88=20#480)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Openstack keypair 관리 로직 개선 > 생성 및 삭제 시 리소스 존재 유무 체크 및 에러 메세지 구체화 - cloud-init 스크립트 > 공통 cloud-init 사용 불가 : subflatform 기능 미제공 --- .../openstack/resources/KeyPairHandler.go | 160 ++++++++++++++---- 1 file changed, 131 insertions(+), 29 deletions(-) diff --git a/cloud-control-manager/cloud-driver/drivers/openstack/resources/KeyPairHandler.go b/cloud-control-manager/cloud-driver/drivers/openstack/resources/KeyPairHandler.go index f86bc5cba..de52780fb 100644 --- a/cloud-control-manager/cloud-driver/drivers/openstack/resources/KeyPairHandler.go +++ b/cloud-control-manager/cloud-driver/drivers/openstack/resources/KeyPairHandler.go @@ -1,6 +1,8 @@ package resources import ( + "errors" + "fmt" "github.com/gophercloud/gophercloud" "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/keypairs" @@ -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) @@ -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 +}