Skip to content

Commit ad0cf50

Browse files
committed
Add support for tags
1 parent 3037a13 commit ad0cf50

File tree

3 files changed

+213
-25
lines changed

3 files changed

+213
-25
lines changed

client.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,21 @@ func NewRequest(c CloudstackClient, request string, params url.Values) (interfac
155155
json.Unmarshal(body, &decodedResponse)
156156
return decodedResponse, nil
157157

158+
case "createTags":
159+
var decodedResponse CreateTagsResponse
160+
json.Unmarshal(body, &decodedResponse)
161+
return decodedResponse, nil
162+
163+
case "listTags":
164+
var decodedResponse ListTagsResponse
165+
json.Unmarshal(body, &decodedResponse)
166+
return decodedResponse, nil
167+
168+
case "deleteTags":
169+
var decodedResponse DeleteTagsResponse
170+
json.Unmarshal(body, &decodedResponse)
171+
return decodedResponse, nil
172+
158173
}
159174

160175
// only reached with unknown request

tag.go

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
package gopherstack
2+
3+
import (
4+
"net/url"
5+
"strconv"
6+
"strings"
7+
)
8+
9+
// Add tags to specified resources
10+
func (c CloudstackClient) CreateTags(options *CreateTags) (CreateTagsResponse, error) {
11+
var resp CreateTagsResponse
12+
params := url.Values{}
13+
14+
params.Set("resourceids", strings.Join(options.Resourceids, ","))
15+
params.Set("resourcetype", options.Resourcetype)
16+
for j, tag := range options.Tags {
17+
params.Set("tag["+strconv.Itoa(j+1)+"].key", tag.Key)
18+
params.Set("tag["+strconv.Itoa(j+1)+"].value", tag.Value)
19+
}
20+
21+
if options.Customer != "" {
22+
params.Set("customer", options.Customer)
23+
}
24+
25+
response, err := NewRequest(c, "createTags", params)
26+
if err != nil {
27+
return resp, err
28+
}
29+
30+
resp = response.(CreateTagsResponse)
31+
return resp, err
32+
}
33+
34+
// Remove tags from specified resources
35+
func (c CloudstackClient) DeleteTags(options *DeleteTags) (DeleteTagsResponse, error) {
36+
var resp DeleteTagsResponse
37+
params := url.Values{}
38+
39+
params.Set("resourceids", strings.Join(options.Resourceids, ","))
40+
params.Set("resourcetype", options.Resourcetype)
41+
for j, tag := range options.Tags {
42+
params.Set("tag["+strconv.Itoa(j+1)+"].key", tag.Key)
43+
params.Set("tag["+strconv.Itoa(j+1)+"].value", tag.Value)
44+
}
45+
46+
response, err := NewRequest(c, "deleteTags", params)
47+
if err != nil {
48+
return resp, err
49+
}
50+
51+
resp = response.(DeleteTagsResponse)
52+
return resp, err
53+
}
54+
55+
// Returns all items with a particular tag
56+
func (c CloudstackClient) ListTags(options *ListTags) (ListTagsResponse, error) {
57+
var resp ListTagsResponse
58+
params := url.Values{}
59+
if options.Account != "" {
60+
params.Set("account", options.Account)
61+
}
62+
if options.Customer != "" {
63+
params.Set("customer", options.Customer)
64+
}
65+
if options.Domainid != "" {
66+
params.Set("domainid", options.Domainid)
67+
}
68+
if options.Isrecursive {
69+
params.Set("isrecrusive", "true")
70+
}
71+
if options.Key != "" {
72+
params.Set("key", options.Key)
73+
}
74+
if options.Keyword != "" {
75+
params.Set("keyword", options.Keyword)
76+
}
77+
if options.Listall {
78+
params.Set("listall", "true")
79+
}
80+
if options.Page != "" {
81+
params.Set("page", options.Page)
82+
}
83+
if options.Pagesize != "" {
84+
params.Set("pagesize", options.Pagesize)
85+
}
86+
if options.Projectid != "" {
87+
params.Set("projectid", options.Projectid)
88+
}
89+
if options.Resourceid != "" {
90+
params.Set("resourceid", options.Resourceid)
91+
}
92+
if options.Resourcetype != "" {
93+
params.Set("resourcetype", options.Resourcetype)
94+
}
95+
if options.Value != "" {
96+
params.Set("value", options.Value)
97+
}
98+
response, err := NewRequest(c, "listTags", params)
99+
if err != nil {
100+
return resp, err
101+
}
102+
103+
resp = response.(ListTagsResponse)
104+
return resp, err
105+
}
106+
107+
type Tag struct {
108+
Account string `json:"account"`
109+
Customer string `json:"customer"`
110+
Domain string `json:"domain"`
111+
Domainid string `json:"domainid"`
112+
Key string `json:"key"`
113+
Project string `json:"project"`
114+
Projectid string `json:"projectid"`
115+
Resourceid string `json:"resourceid"`
116+
Resourcetype string `json:"resourcetype`
117+
Value string `json:"value`
118+
}
119+
120+
type ListTagsResponse struct {
121+
Listtagsresponse struct {
122+
Count float64 `json:"count"`
123+
Tag []Tag `json:"tag"`
124+
} `json:"listtagsresponse"`
125+
}
126+
127+
type ListTags struct {
128+
Account string `json:"account"`
129+
Customer string `json:"customer"`
130+
Domainid string `json:"domainid"`
131+
Isrecursive bool `json:"isrecursive"`
132+
Key string `json:"key"`
133+
Keyword string `json:"keyword"`
134+
Listall bool `json:"listall"`
135+
Page string `json:"page"`
136+
Pagesize string `json:"pagesize"`
137+
Projectid string `json:"projectid"`
138+
Resourceid string `json:"resourceid"`
139+
Resourcetype string `json:"resourcetype`
140+
Value string `json:"value`
141+
}
142+
143+
type CreateTags struct {
144+
Customer string `json:"customer"`
145+
Resourceids []string `json:"resourceids"`
146+
Resourcetype string `json:"resourcetype`
147+
Tags []TagArg `json:"tags`
148+
}
149+
150+
type CreateTagsResponse struct {
151+
Createtagsresponse struct {
152+
Displaytext string `json:"displaytext"`
153+
Success string `json:"success"`
154+
} `json:"createtagsresponse"`
155+
}
156+
157+
type DeleteTags struct {
158+
Resourceids []string `json:"resourceids"`
159+
Resourcetype string `json:"resourcetype`
160+
Tags []TagArg `json:"tags`
161+
}
162+
163+
type DeleteTagsResponse struct {
164+
Deletetagsresponse struct {
165+
Displaytext string `json:"displaytext"`
166+
Success string `json:"success"`
167+
} `json:"deletetagsresponse"`
168+
}
169+
170+
type TagArg struct {
171+
Key string `json:"key"`
172+
Value string `json:"value"`
173+
}

template.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -85,31 +85,31 @@ type DeleteTemplateResponse struct {
8585
}
8686

8787
type Template struct {
88-
Account string `json:"account"`
89-
Created string `json:"created"`
90-
CrossZones bool `json:"crossZones"`
91-
Displaytext string `json:"displaytext"`
92-
Domain string `json:"domain"`
93-
Domainid string `json:"domainid"`
94-
Format string `json:"format"`
95-
Hypervisor string `json:"hypervisor"`
96-
ID string `json:"id"`
97-
Isextractable bool `json:"isextractable"`
98-
Isfeatured bool `json:"isfeatured"`
99-
Ispublic bool `json:"ispublic"`
100-
Isready bool `json:"isready"`
101-
Name string `json:"name"`
102-
Ostypeid string `json:"ostypeid"`
103-
Ostypename string `json:"ostypename"`
104-
Passwordenabled bool `json:"passwordenabled"`
105-
Size float64 `json:"size"`
106-
Sourcetemplateid string `json:"sourcetemplateid"`
107-
Sshkeyenabled bool `json:"sshkeyenabled"`
108-
Status string `json:"status"`
109-
Tags []interface{} `json:"tags"`
110-
Templatetype string `json:"templatetype"`
111-
Zoneid string `json:"zoneid"`
112-
Zonename string `json:"zonename"`
88+
Account string `json:"account"`
89+
Created string `json:"created"`
90+
CrossZones bool `json:"crossZones"`
91+
Displaytext string `json:"displaytext"`
92+
Domain string `json:"domain"`
93+
Domainid string `json:"domainid"`
94+
Format string `json:"format"`
95+
Hypervisor string `json:"hypervisor"`
96+
ID string `json:"id"`
97+
Isextractable bool `json:"isextractable"`
98+
Isfeatured bool `json:"isfeatured"`
99+
Ispublic bool `json:"ispublic"`
100+
Isready bool `json:"isready"`
101+
Name string `json:"name"`
102+
Ostypeid string `json:"ostypeid"`
103+
Ostypename string `json:"ostypename"`
104+
Passwordenabled bool `json:"passwordenabled"`
105+
Size float64 `json:"size"`
106+
Sourcetemplateid string `json:"sourcetemplateid"`
107+
Sshkeyenabled bool `json:"sshkeyenabled"`
108+
Status string `json:"status"`
109+
Tags []Tag `json:"tags"`
110+
Templatetype string `json:"templatetype"`
111+
Zoneid string `json:"zoneid"`
112+
Zonename string `json:"zonename"`
113113
}
114114

115115
type ListTemplatesResponse struct {

0 commit comments

Comments
 (0)