-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstartVM.go
74 lines (59 loc) · 1.85 KB
/
startVM.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
package main
import (
"bytes"
"main/cloudstack"
"log"
"fmt"
"encoding/xml"
"io/ioutil"
)
type Conf struct {
Apikey string `xml:"apikey"`
Secretkey string `xml:"secretkey"`
Ipaddress string `xml:"ipaddress"`
Port string `xml:"port"`
}
func main() {
//从配置文件中读取配置信息
b, err := ioutil.ReadFile("conf.xml")
if err != nil{
fmt.Print(err)
}
//将二进制信息转化为string
data := string(b)
//读取并解析xml
buf := bytes.NewBufferString(data)
conf := new(Conf)
decoded := xml.NewDecoder(buf)
err1 := decoded.Decode(conf)
if err1 != nil {
fmt.Printf("Error: %v\n", err1)
}
//将读取出的配置信息进行赋值
apikey := conf.Apikey
secretkey := conf.Secretkey
ipaddress := conf.Ipaddress
port := conf.Port
//生成函数变量
cloudstack_url := "http://" + ipaddress + ":" + port + "/client/api"
// Create a new API client
cs := cloudstack.NewAsyncClient(cloudstack_url, apikey, secretkey, false)
// Create a new parameter struct
p := cs.VirtualMachine.NewListVirtualMachinesParams()
// List all virtual machine
r, err := cs.VirtualMachine.ListVirtualMachines(p)
if err != nil {
log.Fatalf("Error listing the virtual machines: %s ", err)
}
for _, vm := range r.VirtualMachines{
cs1 := cloudstack.NewAsyncClient(cloudstack_url, apikey, secretkey, false)
// Create a new parameter struct
p := cs1.VirtualMachine.NewStartVirtualMachineParams(vm.Id)
// Start new virtual machine
r1, err1 := cs1.VirtualMachine.StartVirtualMachine(p)
if err1 != nil {
log.Fatalf("Error when starting new instance: %s", err1)
}
fmt.Printf("UUID of the newly started machine: %s", r1.Id)
}
}