Skip to content

Commit

Permalink
Add Region/Zone features in Mock driver
Browse files Browse the repository at this point in the history
  • Loading branch information
powerkimhub committed Sep 20, 2023
1 parent 65e652f commit 6950517
Show file tree
Hide file tree
Showing 4 changed files with 404 additions and 10 deletions.
9 changes: 4 additions & 5 deletions cloud-control-manager/cloud-driver/drivers/mock/MockDriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
package mock

import (

cblog "github.com/cloud-barista/cb-log"
"github.com/sirupsen/logrus"

Expand Down Expand Up @@ -62,11 +61,11 @@ func (driver *MockDriver) ConnectCloud(connectionInfo idrv.ConnectionInfo) (icon
Region: connectionInfo.RegionInfo,
MockName: connectionInfo.CredentialInfo.MockName,
}

// Please, do not delete this line.
mkrs.PrepareVMImage(iConn.MockName)
mkrs.PrepareVMSpec(iConn.MockName)
mkrs.PrepareVMImage(iConn.MockName)
mkrs.PrepareVMSpec(iConn.MockName)
mkrs.PrepareRegionZone(iConn.MockName)

return &iConn, nil
}

Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ type MockConnection struct {
MockName string
}

// CreateRegionZoneHandler implements connect.CloudConnection.
func (*MockConnection) CreateRegionZoneHandler() (irs.RegionZoneHandler, error) {
return nil, errors.New("Driver: not implemented")
}

func (cloudConn *MockConnection) CreateImageHandler() (irs.ImageHandler, error) {
cblogger.Info("Mock Driver: called CreateImageHandler()!")
handler := mkrs.MockImageHandler{cloudConn.MockName}
Expand Down Expand Up @@ -114,3 +109,10 @@ func (cloudConn *MockConnection) CreateAnyCallHandler() (irs.AnyCallHandler, err
handler := mkrs.MockAnyCallHandler{cloudConn.MockName}
return &handler, nil
}

// CreateRegionZoneHandler implements connect.CloudConnection.
func (cloudConn *MockConnection) CreateRegionZoneHandler() (irs.RegionZoneHandler, error) {
cblogger.Info("Mock Driver: called CreateRegionZoneHandler()!")
handler := mkrs.MockRegionZoneHandler{Region: cloudConn.Region, MockName: cloudConn.MockName}
return &handler, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
// 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, 2023.09.

package resources

import (
"encoding/json"
"fmt"

cblog "github.com/cloud-barista/cb-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"
)

var regionZoneInfoMap map[string][]*irs.RegionZoneInfo

type MockRegionZoneHandler struct {
Region idrv.RegionInfo
MockName string
}

var prepareRegionZoneInfoList []*irs.RegionZoneInfo

func init() {
regionZoneInfoMap = make(map[string][]*irs.RegionZoneInfo)
}

// Be called before using the User function.
// Called in MockDriver
func PrepareRegionZone(mockName string) {
cblogger := cblog.GetLogger("CB-SPIDER")
cblogger.Info("Mock Driver: called prepare()!")

if regionZoneInfoMap[mockName] != nil {
return
}

prepareRegionZoneInfoList = []*irs.RegionZoneInfo{
{
Name: "mercury",
DisplayName: "Mercury Region",
ZoneList: []irs.ZoneInfo{
{
Name: "mercury-z1",
DisplayName: "Mercury Zone 1",
Status: irs.ZoneAvailable,
KeyValueList: nil,
},
{
Name: "mercury-z2",
DisplayName: "Mercury Zone 2",
Status: irs.ZoneAvailable,
KeyValueList: nil,
},
{
Name: "mercury-z3",
DisplayName: "Mercury Zone 3",
Status: irs.ZoneAvailable,
KeyValueList: nil,
},
},
KeyValueList: nil,
},
{
Name: "venus",
DisplayName: "Venus Region",
ZoneList: []irs.ZoneInfo{
{
Name: "venus-z1",
DisplayName: "Venus Zone 1",
Status: irs.ZoneAvailable,
KeyValueList: nil,
},
{
Name: "venus-z2",
DisplayName: "Venus Zone 2",
Status: irs.ZoneAvailable,
KeyValueList: nil,
},
{
Name: "venus-z3",
DisplayName: "Venus Zone 3",
Status: irs.ZoneAvailable,
KeyValueList: nil,
},
},
KeyValueList: nil,
},
{
Name: "mars",
DisplayName: "Mars Region",
ZoneList: []irs.ZoneInfo{
{
Name: "mars-z1",
DisplayName: "Mars Zone 1",
Status: irs.ZoneAvailable,
KeyValueList: nil,
},
{
Name: "mars-z2",
DisplayName: "Mars Zone 2",
Status: irs.ZoneAvailable,
KeyValueList: nil,
},
{
Name: "mars-z3",
DisplayName: "Mars Zone 3",
Status: irs.ZoneAvailable,
KeyValueList: nil,
},
},
KeyValueList: nil,
},
{
Name: "jupiter",
DisplayName: "Jupiter Region",
ZoneList: []irs.ZoneInfo{
{
Name: "jupiter-z1",
DisplayName: "Jupiter Zone 1",
Status: irs.ZoneAvailable,
KeyValueList: nil,
},
{
Name: "jupiter-z2",
DisplayName: "Jupiter Zone 2",
Status: irs.ZoneAvailable,
KeyValueList: nil,
},
{
Name: "jupiter-z3",
DisplayName: "Jupiter Zone 3",
Status: irs.ZoneAvailable,
KeyValueList: nil,
},
},
KeyValueList: nil,
},
{
Name: "saturn",
DisplayName: "Saturn Region",
ZoneList: []irs.ZoneInfo{
{
Name: "saturn-z1",
DisplayName: "Saturn Zone 1",
Status: irs.ZoneAvailable,
KeyValueList: nil,
},
{
Name: "saturn-z2",
DisplayName: "Saturn Zone 2",
Status: irs.ZoneAvailable,
KeyValueList: nil,
},
{
Name: "saturn-z3",
DisplayName: "Saturn Zone 3",
Status: irs.ZoneAvailable,
KeyValueList: nil,
},
},
KeyValueList: nil,
},
{
Name: "uranus",
DisplayName: "Uranus Region",
ZoneList: []irs.ZoneInfo{
{
Name: "uranus-z1",
DisplayName: "Uranus Zone 1",
Status: irs.ZoneAvailable,
KeyValueList: nil,
},
{
Name: "uranus-z2",
DisplayName: "Uranus Zone 2",
Status: irs.ZoneAvailable,
KeyValueList: nil,
},
{
Name: "uranus-z3",
DisplayName: "Uranus Zone 3",
Status: irs.ZoneAvailable,
KeyValueList: nil,
},
},
KeyValueList: nil,
},
{
Name: "neptune",
DisplayName: "Neptune Region",
ZoneList: []irs.ZoneInfo{
{
Name: "neptune-z1",
DisplayName: "Neptune Zone 1",
Status: irs.ZoneAvailable,
KeyValueList: nil,
},
{
Name: "neptune-z2",
DisplayName: "Neptune Zone 2",
Status: irs.ZoneAvailable,
KeyValueList: nil,
},
{
Name: "neptune-z3",
DisplayName: "Neptune Zone 3",
Status: irs.ZoneAvailable,
KeyValueList: nil,
},
},
KeyValueList: nil,
},
}
regionZoneInfoMap[mockName] = prepareRegionZoneInfoList
}

func (handler *MockRegionZoneHandler) ListRegionZone() ([]*irs.RegionZoneInfo, error) {
cblogger := cblog.GetLogger("CB-SPIDER")
cblogger.Info("Mock Driver: called ListRegionZone()!")

mockName := handler.MockName

infoList, ok := regionZoneInfoMap[mockName]
if !ok {
return []*irs.RegionZoneInfo{}, nil
}

// cloning list of RegionZoneInfo
resultList := make([]*irs.RegionZoneInfo, len(infoList))
copy(resultList, infoList)
return resultList, nil
}

func (handler *MockRegionZoneHandler) GetRegionZone(Name string) (irs.RegionZoneInfo, error) {
cblogger := cblog.GetLogger("CB-SPIDER")
cblogger.Info("Mock Driver: called GetRegionZone()!")

infoList, err := handler.ListRegionZone()
if err != nil {
cblogger.Error(err)
return irs.RegionZoneInfo{}, err
}

for _, info := range infoList {
if (*info).Name == Name {
return *info, nil
}
}

return irs.RegionZoneInfo{}, fmt.Errorf("%s Name does not exist!!", Name)
}

// ListOrgRegion implements resources.RegionZoneHandler.
func (handler *MockRegionZoneHandler) ListOrgRegion() (string, error) {
cblogger := cblog.GetLogger("CB-SPIDER")
cblogger.Info("Mock Driver: called ListOrgRegion()!")

// Convert prepareRegionZoneInfoList to JSON
jsonData, err := json.MarshalIndent(prepareRegionZoneInfoList, "", " ")
if err != nil {
cblogger.Error("Error while converting to JSON: ", err)
return "", err
}

return string(jsonData), nil
}

// ListOrgZone implements resources.RegionZoneHandler.
func (handler *MockRegionZoneHandler) ListOrgZone() (string, error) {
cblogger := cblog.GetLogger("CB-SPIDER")
cblogger.Info("Mock Driver: called ListOrgZone()!")

for _, info := range prepareRegionZoneInfoList {
if (*info).Name == handler.Region.Region {
jsonData, err := json.MarshalIndent(info, "", " ")
if err != nil {
cblogger.Error("Error while converting to JSON: ", err)
return "", err
}
return string(jsonData), nil
}
}

return "", fmt.Errorf("%s VMSpec does not exist!!", handler.Region.Region)
}
Loading

0 comments on commit 6950517

Please sign in to comment.