Skip to content

Commit ab3faee

Browse files
author
Peter Jönsson
committed
Add support for creating templates
1 parent 45bbcef commit ab3faee

File tree

6 files changed

+97
-20
lines changed

6 files changed

+97
-20
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
gopherstack
22
===========
33

4-
CloudStack API library written in Go. Only tested towards CS 3.0.6 so far.
4+
Cloudstack API library written in Go. Only tested towards Cloudstack
5+
3.0.6 so far.

client.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ func NewRequest(c CloudStackClient, request string, params url.Values) (interfac
108108
json.Unmarshal(body, &decodedResponse)
109109
return decodedResponse, nil
110110

111+
case "listVolumes":
112+
var decodedResponse ListVolumesResponse
113+
json.Unmarshal(body, &decodedResponse)
114+
return decodedResponse, nil
115+
116+
case "createTemplate":
117+
var decodedResponse CreateTemplateResponse
118+
json.Unmarshal(body, &decodedResponse)
119+
return decodedResponse, nil
120+
111121
case "queryAsyncJobResult":
112122
var decodedResponse QueryAsyncJobResultResponse
113123
json.Unmarshal(body, &decodedResponse)

template.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,49 @@ import (
44
"net/url"
55
)
66

7-
type Template struct {
8-
Id string
9-
Name string
10-
}
11-
12-
type TemplatesResponse struct {
13-
Templates []Template
14-
}
15-
167
// Creates a Template of a Virtual Machine by it's ID
178
func (c CloudStackClient) CreateTemplate(displaytext string, name string, volumeid string, ostypeid string) (string, error) {
189
params := url.Values{}
1910
params.Set("displaytext", displaytext)
2011
params.Set("name", name)
2112
params.Set("ostypeid", ostypeid)
2213
params.Set("volumeid", volumeid)
23-
_, err := NewRequest(c, "createTemplate", params)
24-
// return async job id
25-
return "jobId", err
14+
15+
response, err := NewRequest(c, "createTemplate", params)
16+
if err != nil {
17+
return "", err
18+
}
19+
20+
jobId := response.(CreateTemplateResponse).Createtemplateresponse.Jobid
21+
return jobId, err
2622
}
2723

2824
// Returns all available templates
29-
func (c CloudStackClient) Templates() ([]Template, error) {
25+
func (c CloudStackClient) ListTemplates(name string) ([]string, error) {
3026
params := url.Values{}
27+
params.Set("name", name)
3128
_, err := NewRequest(c, "listTemplates", params)
32-
// unmarshall json to a proper list
29+
if err != nil {
30+
return nil, err
31+
}
32+
3333
return nil, err
3434
}
3535

3636
// Deletes an template by its ID.
37-
func (c CloudStackClient) DeleteTemplate(id string) (uint, error) {
37+
func (c CloudStackClient) DeleteTemplate(id string) (string, error) {
3838
params := url.Values{}
3939
params.Set("id", id)
4040
_, err := NewRequest(c, "deleteTemplate", params)
41-
return 0, err
41+
if err != nil {
42+
return "", err
43+
}
44+
return "", err
45+
}
46+
47+
type CreateTemplateResponse struct {
48+
Createtemplateresponse struct {
49+
ID string `json:"id"`
50+
Jobid string `json:"jobid"`
51+
} `json:"createtemplateresponse"`
4252
}

virtualmachine.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (c CloudStackClient) DestroyVirtualMachine(id string) (string, error) {
5454
}
5555

5656
// Returns CloudStack string representation of the Virtual Machine state
57-
func (c CloudStackClient) VirtualMachineState(id string) (string, string, error) {
57+
func (c CloudStackClient) ListVirtualMachines(id string) (string, string, error) {
5858
params := url.Values{}
5959
params.Set("id", id)
6060
response, err := NewRequest(c, "listVirtualMachines", params)
@@ -64,7 +64,6 @@ func (c CloudStackClient) VirtualMachineState(id string) (string, string, error)
6464

6565
count := response.(ListVirtualMachinesResponse).Listvirtualmachinesresponse.Count
6666
if count != 1 {
67-
// TODO: Return some like no virtual machines found.
6867
return "", "", err
6968
}
7069

volume.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package gopherstack
2+
3+
import (
4+
"net/url"
5+
)
6+
7+
// List volumes for Virtual Machine by it's ID
8+
func (c CloudStackClient) ListVolumes(vmid string) (string, error) {
9+
params := url.Values{}
10+
params.Set("virtualmachineid", vmid)
11+
response, err := NewRequest(c, "listVolumes", params)
12+
if err != nil {
13+
return "", err
14+
}
15+
16+
count := response.(ListVolumesResponse).Listvolumesresponse.Count
17+
// if there are no volumes we just return here
18+
if count < 1 {
19+
return "", err
20+
}
21+
22+
volumeId := response.(ListVolumesResponse).Listvolumesresponse.Volume[0].ID
23+
24+
return volumeId, err
25+
}
26+
27+
type ListVolumesResponse struct {
28+
Listvolumesresponse struct {
29+
Count float64 `json:"count"`
30+
Volume []struct {
31+
Account string `json:"account"`
32+
Created string `json:"created"`
33+
Destroyed bool `json:"destroyed"`
34+
Deviceid float64 `json:"deviceid"`
35+
Domain string `json:"domain"`
36+
Domainid string `json:"domainid"`
37+
ID string `json:"id"`
38+
Isextractable bool `json:"isextractable"`
39+
Name string `json:"name"`
40+
Serviceofferingdisplaytext string `json:"serviceofferingdisplaytext"`
41+
Serviceofferingid string `json:"serviceofferingid"`
42+
Serviceofferingname string `json:"serviceofferingname"`
43+
Size float64 `json:"size"`
44+
State string `json:"state"`
45+
Storage string `json:"storage"`
46+
Storagetype string `json:"storagetype"`
47+
Tags []interface{} `json:"tags"`
48+
Type string `json:"type"`
49+
Virtualmachineid string `json:"virtualmachineid"`
50+
Vmdisplayname string `json:"vmdisplayname"`
51+
Vmname string `json:"vmname"`
52+
Vmstate string `json:"vmstate"`
53+
Zoneid string `json:"zoneid"`
54+
Zonename string `json:"zonename"`
55+
} `json:"volume"`
56+
} `json:"listvolumesresponse"`
57+
}

wait.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (c CloudStackClient) WaitForVirtualMachineState(vmid string, wantedState st
6868
attempts += 1
6969

7070
log.Printf("Checking virtual machine state... (attempt: %d)", attempts)
71-
_, currentState, err := c.VirtualMachineState(vmid)
71+
_, currentState, err := c.ListVirtualMachines(vmid)
7272
if err != nil {
7373
result <- err
7474
return

0 commit comments

Comments
 (0)