-
Notifications
You must be signed in to change notification settings - Fork 22
/
api.go
146 lines (125 loc) · 4.23 KB
/
api.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package api
import (
"errors"
log "github.com/sirupsen/logrus"
"github.com/kolo/xmlrpc"
"github.com/megamsys/libgo/cmd"
"strconv"
"strings"
)
const (
ENDPOINT = "endpoint"
USERID = "userid"
CURRENTUSER = "username"
PASSWORD = "password"
TEMPLATE = "template"
IMAGE = "image"
ONEZONE = "region"
VCPU_PERCENTAGE = "vcpu_percentage"
CLUSTER = "cluster"
// user action methods
ONE_USER_CREATE = "one.user.allocate"
// virtual network action methods
VNET_CREATE = "one.vn.allocate"
VNET_ADDIP = "one.vn.add_ar"
VNET_SHOW = "one.vn.info"
VNET_LIST = "one.vnpool.info"
VNET_HOLD = "one.vn.hold"
VNET_RELEASE = "one.vn.release"
// host action methods
ONE_HOST_INFO = "one.host.info"
ONE_HOST_POOL = "one.hostpool.info"
ONE_HOST_ALLOCATE = "one.host.allocate"
ONE_HOST_DELETE = "one.host.delete"
// datastore action methods
ONE_DATASTORE_INFO = "one.datastore.info"
ONE_DATASTOREPOOL_INFO = "one.datastorepool.info"
ONE_DATASTORE_ALLOCATE = "one.datastore.allocate"
// image action methods
ONE_IMAGE_SHOW = "one.image.info"
ONE_IMAGE_LIST = "one.imagepool.info"
ONE_IMAGE_CREATE = "one.image.allocate"
ONE_IMAGE_DELETE = "one.image.delete"
ONE_IMAGE_PERSISTENT = "one.image.persistent"
ONE_IMAGE_TYPECHANGE = "one.image.chtype"
ONE_IMAGE_RENAME = "one.image.rename"
ONE_IMAGE_ENABLE = "one.image.enable"
ONE_IMAGE_REMOVE = "one.image.delete"
ONE_IMAGE_UPDATE = "one.image.update"
// virtualmachine action methods
VM_INFO = "one.vm.info"
DISK_ATTACH = "one.vm.attach"
DISK_DETACH = "one.vm.detach"
ONE_VM_ACTION = "one.vm.action"
ONE_DISK_SNAPSHOT = "one.vm.disksaveas"
ONE_RECOVER = "one.vm.recover"
DISK_SNAPSHOT_CREATE = "one.vm.disksnapshotcreate"
DISK_SNAPSHOT_DELETE = "one.vm.disksnapshotdelete"
DISK_SNAPSHOT_REVERT = "one.vm.disksnapshotrevert"
VMPOOL_ACCOUNTING = "one.vmpool.accounting"
VMPOOL_INFO = "one.vmpool.info"
ONE_VM_ATTACHNIC = "one.vm.attachnic"
ONE_VM_DETACHNIC = "one.vm.detachnic"
ONE_VM_RESIZE = "one.vm.resize"
// template action methods
TEMPLATE_INSTANTIATE = "one.template.instantiate"
TEMPLATEPOOL_INFO = "one.templatepool.info"
TEMPLATE_UPDATE = "one.template.update"
ONE_TEMPLATE_ALLOCATE = "one.template.allocate"
)
var (
ErrArgsNotSatisfied = errors.New("[" + ENDPOINT + "," + USERID + "," + PASSWORD + "] one (or) more args missing!")
)
type Rpc struct {
Client xmlrpc.Client
Key string
}
func NewClient(config map[string]string) (*Rpc, error) {
log.Debugf(cmd.Colorfy(" > [one-go] connecting", "blue", "", "bold"))
if !satisfied(config) {
return nil, ErrArgsNotSatisfied
}
client, err := xmlrpc.NewClient(config[ENDPOINT], nil)
if err != nil {
return nil, err
}
//log.Debugf(cmd.Colorfy(" > [one-go] connection response", "blue", "", "bold")+"%#v",client)
log.Debugf(cmd.Colorfy(" > [one-go] connected", "blue", "", "bold")+" %s", config[ENDPOINT])
return &Rpc{
Client: *client,
Key: config[USERID] + ":" + config[PASSWORD]}, nil
}
func (c *Rpc) Call(command string, args []interface{}) (string, error) {
log.Debugf(cmd.Colorfy(" > [one-go] ", "blue", "", "bold")+"%s", command)
log.Debugf(cmd.Colorfy("\n> args ", "cyan", "", "bold")+" %v\n", args)
result := []interface{}{}
if err := c.Client.Call(command, args, &result); err != nil {
return "", err
}
res, err := c.isSuccess(result)
if err != nil {
return "", err
}
//log.Debugf(cmd.Colorfy("\n> response ", "cyan", "", "bold")+" %v", result)
log.Debugf(cmd.Colorfy(" > [one-go] ( ´ ▽ ` ) SUCCESS", "blue", "", "bold"))
return res, nil
}
func (c *Rpc) isSuccess(result []interface{}) (string, error) {
var res string
success := result[0].(bool)
if !success {
return "", errors.New(result[1].(string))
}
if w, ok := result[1].(int64); ok {
res = strconv.FormatInt(w, 10)
} else if w, ok := result[1].(string); ok {
res = w
}
//result[1] is error message or ID of action vm,vnet,cluster and etc.,
return res, nil
}
func satisfied(c map[string]string) bool {
return len(strings.TrimSpace(c[ENDPOINT])) > 0 &&
len(strings.TrimSpace(c[USERID])) > 0 &&
len(strings.TrimSpace(c[PASSWORD])) > 0
}