-
Notifications
You must be signed in to change notification settings - Fork 47
/
AnyCallHandler.go
99 lines (81 loc) · 2.8 KB
/
AnyCallHandler.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
// 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, 2020.09.
package resources
import (
"strconv"
"errors"
cblog "github.com/cloud-barista/cb-log"
irs "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources"
_ "github.com/sirupsen/logrus"
)
type MockAnyCallHandler struct {
MockName string
}
/********************************************************
// call example
curl -sX POST http://localhost:1024/spider/anycall -H 'Content-Type: application/json' -d \
'{
"ConnectionName" : "mock-config01",
"ReqInfo" : {
"FID" : "countAll",
"IKeyValueList" : [{"Key":"rsType", "Value":"vpc"}]
}
}' | json_pp
********************************************************/
func (anyCallHandler *MockAnyCallHandler) AnyCall(callInfo irs.AnyCallInfo) (irs.AnyCallInfo, error) {
cblogger := cblog.GetLogger("CB-SPIDER")
cblogger.Info("Mock Driver: called AnyCall()!")
switch callInfo.FID {
case "countAll" :
return countAll(anyCallHandler, callInfo)
// add more ...
default :
return irs.AnyCallInfo{}, errors.New("Mock Driver: " + callInfo.FID + " Function is not implemented!")
}
}
///////////////////////////////////////////////////////////////////
// implemented by developer user, like 'countAll(rsType string) int'
///////////////////////////////////////////////////////////////////
func countAll(anyCallHandler *MockAnyCallHandler, callInfo irs.AnyCallInfo) (irs.AnyCallInfo, error) {
cblogger := cblog.GetLogger("CB-SPIDER")
cblogger.Info("Mock Driver: called AnyCall()/countAll()!")
mockName := anyCallHandler.MockName
// Input Arg Validation
if callInfo.IKeyValueList == nil {
return irs.AnyCallInfo{}, errors.New("Mock Driver: " + callInfo.FID + "'s Argument is empty!")
}
if callInfo.IKeyValueList[0].Key != "rsType" {
return irs.AnyCallInfo{}, errors.New("Mock Driver: " + callInfo.FID + "'s Argument is not 'rsType'!")
}
// get info
strCount := ""
switch callInfo.IKeyValueList[0].Value {
case "vpc":
infoList, ok := vpcInfoMap[mockName]
if !ok {
strCount = "0"
} else {
strCount = strconv.Itoa(len(infoList))
}
case "sg":
infoList, ok := securityInfoMap[mockName]
if !ok {
strCount = "0"
} else {
strCount = strconv.Itoa(len(infoList))
}
}
// make results
if callInfo.OKeyValueList == nil {
callInfo.OKeyValueList = []irs.KeyValue{}
}
callInfo.OKeyValueList = append(callInfo.OKeyValueList, irs.KeyValue{"Count", strCount} )
return callInfo, nil
}