|
| 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 | +} |
0 commit comments