Skip to content

Commit

Permalink
Add a Key List Validator for KeyValueList arguments validation
Browse files Browse the repository at this point in the history
  • Loading branch information
powerkimhub committed Nov 15, 2021
1 parent 37006ef commit 2a2f7cf
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 74 deletions.
65 changes: 0 additions & 65 deletions api-runtime/common-runtime/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"fmt"
"reflect"
"strings"
//icbs "github.com/cloud-barista/cb-store/interfaces"
//ccm "github.com/cloud-barista/cb-spider/cloud-control-manager"
)

func EmptyCheckAndTrim(inputName string, inputValue string) (string, error) {
Expand Down Expand Up @@ -100,69 +98,6 @@ func checkNilPermission(argTypeName string, emptyPermissionList []string) error
return fmt.Errorf("%v's input value is empty!", argTypeName)
}

/*
// (1) Extract and sort the list of keys from inKeyValue,
// (2) Get the list of keys from driver, and sort it.
// (3) compare them.
func ValidateKeyValue(inKeyValueList []icbs.KeyValue, keyList []string, notNullNameList []string) error {
ccm.GetCloudDriver(
// (1) Extract the list of keys from inKeyValue and sort it.
for _, kv := range inKeyValueList {
for _, key := range keyList {
if kv.Key == key {
keyList = keyList.remove(key)
}
}
}
// (2) Get the list of keys from driver, and sort it.
// (3) compare them.
return nil
}
func getCloudDriver(providerName string) , error) {
cblog.Info("CloudDriverHandler: called getStaticCloudDriver() - " + cldDrvInfo.DriverName)
var cloudDriver idrv.CloudDriver
// select driver
switch cldDrvInfo.ProviderName {
case "AWS":
cloudDriver = new(awsdrv.AwsDriver)
case "AZURE":
cloudDriver = new(azuredrv.AzureDriver)
case "GCP":
cloudDriver = new(gcpdrv.GCPDriver)
case "ALIBABA":
cloudDriver = new(alibabadrv.AlibabaDriver)
case "OPENSTACK":
cloudDriver = new(openstackdrv.OpenStackDriver)
case "CLOUDIT":
cloudDriver = new(clouditdrv.ClouditDriver)
case "DOCKER":
cloudDriver = new(dockerdrv.DockerDriver)
case "TENCENT":
cloudDriver = new(tencentdrv.TencentDriver)
// case "NCP": // NCP
// cloudDriver = new(ncpdrv.NcpDriver) // NCP
// case "NCPVPC": // NCP-VPC
// cloudDriver = new(ncpvpcdrv.NcpVpcDriver) // NCP-VPC
case "MOCK":
cloudDriver = new(mockdrv.MockDriver)
default:
errmsg := cldDrvInfo.ProviderName + " is not supported static Cloud Driver!!"
return cloudDriver, fmt.Errorf(errmsg)
}
return cloudDriver, nil
}
*/


//----------- utility

func printType(inType reflect.Type) {
Expand Down
55 changes: 55 additions & 0 deletions cloud-info-manager/KeyValueListValidator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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, 2021.11.

package cloudos

import (
"fmt"
icbs "github.com/cloud-barista/cb-store/interfaces"
)


// (1) Remove list of valid keys with list of inpput keys
// (2) Check the list of remaining Keys
func ValidateKeyValueList(inKeyValueList []icbs.KeyValue, validKeyList []string) error {

clonedKeyList := cloneSlice(validKeyList)

// (1) Remove list of valid keys with list of inpput keys
inputKeyList := make([]string, len(inKeyValueList))
for idx, kv := range inKeyValueList {
inputKeyList[idx] = kv.Key
for _, key := range clonedKeyList {
if kv.Key == key {
clonedKeyList = removeSlice(clonedKeyList, key)
}
}
}
// (2) Check the list of remaining Keys
if len(clonedKeyList) == 0 {
return nil
} else {
errMSG := fmt.Sprintf("Invalid Key in input arguments.\n\t...... have %v\n\t...... want %v", inputKeyList, validKeyList)
return fmt.Errorf(errMSG)
}
}

func removeSlice(inSlice []string, deleteValue string) []string {
for idx, v := range inSlice {
if v == deleteValue {
inSlice = append(inSlice[:idx], inSlice[idx+1:]...)
return inSlice
}
}
return inSlice
}

func cloneSlice(inSlice []string) []string {
clonedSlice := make([]string, len(inSlice))
copy(clonedSlice, inSlice)
return clonedSlice
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"strings"
"github.com/cloud-barista/cb-store/config"
icbs "github.com/cloud-barista/cb-store/interfaces"
cim "github.com/cloud-barista/cb-spider/cloud-info-manager"

"github.com/sirupsen/logrus"

"crypto/aes"
Expand Down Expand Up @@ -229,11 +231,23 @@ func checkParams(credentialName string, providerName string, keyValueInfoList []
if providerName == "" {
return fmt.Errorf("ProviderName is empty!")
}
for _, kv := range keyValueInfoList {
if kv.Key == "" { // Value can be empty.
return fmt.Errorf("Key is empty!")
}
}
if keyValueInfoList == nil {
return fmt.Errorf("KeyValue List is nil!")
}

// get Provider's Meta Info
cloudOSMetaInfo, err := cim.GetCloudOSMetaInfo(providerName)
if err != nil {
cblog.Error(err)
return err
}

// validate the KeyValueList of Credential Input
err = cim.ValidateKeyValueList(keyValueInfoList, cloudOSMetaInfo.Credential)
if err != nil {
cblog.Error(err)
return err
}

return nil
}
22 changes: 18 additions & 4 deletions cloud-info-manager/region-info-manager/RegionInfoManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"strings"
"github.com/cloud-barista/cb-store/config"
icbs "github.com/cloud-barista/cb-store/interfaces"
cim "github.com/cloud-barista/cb-spider/cloud-info-manager"

"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -118,11 +120,23 @@ func checkParams(regionName string, providerName string, keyValueInfoList []icbs
if providerName == "" {
return fmt.Errorf("ProviderName is empty!")
}
for _, kv := range keyValueInfoList {
if kv.Key == "" { // Value can be empty.
return fmt.Errorf("Key is empty!")
}
if keyValueInfoList == nil {
return fmt.Errorf("KeyValue List is nil!")
}

// get Provider's Meta Info
cloudOSMetaInfo, err := cim.GetCloudOSMetaInfo(providerName)
if err != nil {
cblog.Error(err)
return err
}

// validate the KeyValueList of Region Input
err = cim.ValidateKeyValueList(keyValueInfoList, cloudOSMetaInfo.Region)
if err != nil {
cblog.Error(err)
return err
}

return nil
}
File renamed without changes.
48 changes: 48 additions & 0 deletions cloud-info-manager/test/validate-test/validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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, 2021.11.

package validatetest

import (
cim "github.com/cloud-barista/cb-spider/cloud-info-manager"

icbs "github.com/cloud-barista/cb-store/interfaces"
"testing"
"log"
)

func TestValid(t *testing.T) {
inKeyValueList := []icbs.KeyValue{
{"location", "kr"},
{"ResourceGroup", "barista"},
}
wantedKeyList := []string {
"location",
"ResourceGroup",
}
err := cim.ValidateKeyValueList(inKeyValueList, wantedKeyList)
if err != nil {
log.Fatal("something failed!")
}
}


func TestInvalid(t *testing.T) {
inKeyValueList := []icbs.KeyValue{
{"Location", "kr"},
{"ResourceGroup", "barista"},
}
wantedKeyList := []string {
"location",
"ResourceGroup",
}
err := cim.ValidateKeyValueList(inKeyValueList, wantedKeyList)
if err != nil {
log.Fatal(err)
}
}

0 comments on commit 2a2f7cf

Please sign in to comment.