ResourcemetadataService.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. //
  2. // Copyright 2016, Sander van Harmelen
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. package cloudstack
  17. import (
  18. "encoding/json"
  19. "fmt"
  20. "net/url"
  21. "strconv"
  22. )
  23. type AddResourceDetailParams struct {
  24. p map[string]interface{}
  25. }
  26. func (p *AddResourceDetailParams) toURLValues() url.Values {
  27. u := url.Values{}
  28. if p.p == nil {
  29. return u
  30. }
  31. if v, found := p.p["details"]; found {
  32. i := 0
  33. for k, vv := range v.(map[string]string) {
  34. u.Set(fmt.Sprintf("details[%d].key", i), k)
  35. u.Set(fmt.Sprintf("details[%d].value", i), vv)
  36. i++
  37. }
  38. }
  39. if v, found := p.p["fordisplay"]; found {
  40. vv := strconv.FormatBool(v.(bool))
  41. u.Set("fordisplay", vv)
  42. }
  43. if v, found := p.p["resourceid"]; found {
  44. u.Set("resourceid", v.(string))
  45. }
  46. if v, found := p.p["resourcetype"]; found {
  47. u.Set("resourcetype", v.(string))
  48. }
  49. return u
  50. }
  51. func (p *AddResourceDetailParams) SetDetails(v map[string]string) {
  52. if p.p == nil {
  53. p.p = make(map[string]interface{})
  54. }
  55. p.p["details"] = v
  56. return
  57. }
  58. func (p *AddResourceDetailParams) SetFordisplay(v bool) {
  59. if p.p == nil {
  60. p.p = make(map[string]interface{})
  61. }
  62. p.p["fordisplay"] = v
  63. return
  64. }
  65. func (p *AddResourceDetailParams) SetResourceid(v string) {
  66. if p.p == nil {
  67. p.p = make(map[string]interface{})
  68. }
  69. p.p["resourceid"] = v
  70. return
  71. }
  72. func (p *AddResourceDetailParams) SetResourcetype(v string) {
  73. if p.p == nil {
  74. p.p = make(map[string]interface{})
  75. }
  76. p.p["resourcetype"] = v
  77. return
  78. }
  79. // You should always use this function to get a new AddResourceDetailParams instance,
  80. // as then you are sure you have configured all required params
  81. func (s *ResourcemetadataService) NewAddResourceDetailParams(details map[string]string, resourceid string, resourcetype string) *AddResourceDetailParams {
  82. p := &AddResourceDetailParams{}
  83. p.p = make(map[string]interface{})
  84. p.p["details"] = details
  85. p.p["resourceid"] = resourceid
  86. p.p["resourcetype"] = resourcetype
  87. return p
  88. }
  89. // Adds detail for the Resource.
  90. func (s *ResourcemetadataService) AddResourceDetail(p *AddResourceDetailParams) (*AddResourceDetailResponse, error) {
  91. resp, err := s.cs.newRequest("addResourceDetail", p.toURLValues())
  92. if err != nil {
  93. return nil, err
  94. }
  95. var r AddResourceDetailResponse
  96. if err := json.Unmarshal(resp, &r); err != nil {
  97. return nil, err
  98. }
  99. // If we have a async client, we need to wait for the async result
  100. if s.cs.async {
  101. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  102. if err != nil {
  103. if err == AsyncTimeoutErr {
  104. return &r, err
  105. }
  106. return nil, err
  107. }
  108. if err := json.Unmarshal(b, &r); err != nil {
  109. return nil, err
  110. }
  111. }
  112. return &r, nil
  113. }
  114. type AddResourceDetailResponse struct {
  115. JobID string `json:"jobid,omitempty"`
  116. Displaytext string `json:"displaytext,omitempty"`
  117. Success bool `json:"success,omitempty"`
  118. }
  119. type RemoveResourceDetailParams struct {
  120. p map[string]interface{}
  121. }
  122. func (p *RemoveResourceDetailParams) toURLValues() url.Values {
  123. u := url.Values{}
  124. if p.p == nil {
  125. return u
  126. }
  127. if v, found := p.p["key"]; found {
  128. u.Set("key", v.(string))
  129. }
  130. if v, found := p.p["resourceid"]; found {
  131. u.Set("resourceid", v.(string))
  132. }
  133. if v, found := p.p["resourcetype"]; found {
  134. u.Set("resourcetype", v.(string))
  135. }
  136. return u
  137. }
  138. func (p *RemoveResourceDetailParams) SetKey(v string) {
  139. if p.p == nil {
  140. p.p = make(map[string]interface{})
  141. }
  142. p.p["key"] = v
  143. return
  144. }
  145. func (p *RemoveResourceDetailParams) SetResourceid(v string) {
  146. if p.p == nil {
  147. p.p = make(map[string]interface{})
  148. }
  149. p.p["resourceid"] = v
  150. return
  151. }
  152. func (p *RemoveResourceDetailParams) SetResourcetype(v string) {
  153. if p.p == nil {
  154. p.p = make(map[string]interface{})
  155. }
  156. p.p["resourcetype"] = v
  157. return
  158. }
  159. // You should always use this function to get a new RemoveResourceDetailParams instance,
  160. // as then you are sure you have configured all required params
  161. func (s *ResourcemetadataService) NewRemoveResourceDetailParams(resourceid string, resourcetype string) *RemoveResourceDetailParams {
  162. p := &RemoveResourceDetailParams{}
  163. p.p = make(map[string]interface{})
  164. p.p["resourceid"] = resourceid
  165. p.p["resourcetype"] = resourcetype
  166. return p
  167. }
  168. // Removes detail for the Resource.
  169. func (s *ResourcemetadataService) RemoveResourceDetail(p *RemoveResourceDetailParams) (*RemoveResourceDetailResponse, error) {
  170. resp, err := s.cs.newRequest("removeResourceDetail", p.toURLValues())
  171. if err != nil {
  172. return nil, err
  173. }
  174. var r RemoveResourceDetailResponse
  175. if err := json.Unmarshal(resp, &r); err != nil {
  176. return nil, err
  177. }
  178. // If we have a async client, we need to wait for the async result
  179. if s.cs.async {
  180. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  181. if err != nil {
  182. if err == AsyncTimeoutErr {
  183. return &r, err
  184. }
  185. return nil, err
  186. }
  187. if err := json.Unmarshal(b, &r); err != nil {
  188. return nil, err
  189. }
  190. }
  191. return &r, nil
  192. }
  193. type RemoveResourceDetailResponse struct {
  194. JobID string `json:"jobid,omitempty"`
  195. Displaytext string `json:"displaytext,omitempty"`
  196. Success bool `json:"success,omitempty"`
  197. }
  198. type ListResourceDetailsParams struct {
  199. p map[string]interface{}
  200. }
  201. func (p *ListResourceDetailsParams) toURLValues() url.Values {
  202. u := url.Values{}
  203. if p.p == nil {
  204. return u
  205. }
  206. if v, found := p.p["account"]; found {
  207. u.Set("account", v.(string))
  208. }
  209. if v, found := p.p["domainid"]; found {
  210. u.Set("domainid", v.(string))
  211. }
  212. if v, found := p.p["fordisplay"]; found {
  213. vv := strconv.FormatBool(v.(bool))
  214. u.Set("fordisplay", vv)
  215. }
  216. if v, found := p.p["isrecursive"]; found {
  217. vv := strconv.FormatBool(v.(bool))
  218. u.Set("isrecursive", vv)
  219. }
  220. if v, found := p.p["key"]; found {
  221. u.Set("key", v.(string))
  222. }
  223. if v, found := p.p["keyword"]; found {
  224. u.Set("keyword", v.(string))
  225. }
  226. if v, found := p.p["listall"]; found {
  227. vv := strconv.FormatBool(v.(bool))
  228. u.Set("listall", vv)
  229. }
  230. if v, found := p.p["page"]; found {
  231. vv := strconv.Itoa(v.(int))
  232. u.Set("page", vv)
  233. }
  234. if v, found := p.p["pagesize"]; found {
  235. vv := strconv.Itoa(v.(int))
  236. u.Set("pagesize", vv)
  237. }
  238. if v, found := p.p["projectid"]; found {
  239. u.Set("projectid", v.(string))
  240. }
  241. if v, found := p.p["resourceid"]; found {
  242. u.Set("resourceid", v.(string))
  243. }
  244. if v, found := p.p["resourcetype"]; found {
  245. u.Set("resourcetype", v.(string))
  246. }
  247. if v, found := p.p["value"]; found {
  248. u.Set("value", v.(string))
  249. }
  250. return u
  251. }
  252. func (p *ListResourceDetailsParams) SetAccount(v string) {
  253. if p.p == nil {
  254. p.p = make(map[string]interface{})
  255. }
  256. p.p["account"] = v
  257. return
  258. }
  259. func (p *ListResourceDetailsParams) SetDomainid(v string) {
  260. if p.p == nil {
  261. p.p = make(map[string]interface{})
  262. }
  263. p.p["domainid"] = v
  264. return
  265. }
  266. func (p *ListResourceDetailsParams) SetFordisplay(v bool) {
  267. if p.p == nil {
  268. p.p = make(map[string]interface{})
  269. }
  270. p.p["fordisplay"] = v
  271. return
  272. }
  273. func (p *ListResourceDetailsParams) SetIsrecursive(v bool) {
  274. if p.p == nil {
  275. p.p = make(map[string]interface{})
  276. }
  277. p.p["isrecursive"] = v
  278. return
  279. }
  280. func (p *ListResourceDetailsParams) SetKey(v string) {
  281. if p.p == nil {
  282. p.p = make(map[string]interface{})
  283. }
  284. p.p["key"] = v
  285. return
  286. }
  287. func (p *ListResourceDetailsParams) SetKeyword(v string) {
  288. if p.p == nil {
  289. p.p = make(map[string]interface{})
  290. }
  291. p.p["keyword"] = v
  292. return
  293. }
  294. func (p *ListResourceDetailsParams) SetListall(v bool) {
  295. if p.p == nil {
  296. p.p = make(map[string]interface{})
  297. }
  298. p.p["listall"] = v
  299. return
  300. }
  301. func (p *ListResourceDetailsParams) SetPage(v int) {
  302. if p.p == nil {
  303. p.p = make(map[string]interface{})
  304. }
  305. p.p["page"] = v
  306. return
  307. }
  308. func (p *ListResourceDetailsParams) SetPagesize(v int) {
  309. if p.p == nil {
  310. p.p = make(map[string]interface{})
  311. }
  312. p.p["pagesize"] = v
  313. return
  314. }
  315. func (p *ListResourceDetailsParams) SetProjectid(v string) {
  316. if p.p == nil {
  317. p.p = make(map[string]interface{})
  318. }
  319. p.p["projectid"] = v
  320. return
  321. }
  322. func (p *ListResourceDetailsParams) SetResourceid(v string) {
  323. if p.p == nil {
  324. p.p = make(map[string]interface{})
  325. }
  326. p.p["resourceid"] = v
  327. return
  328. }
  329. func (p *ListResourceDetailsParams) SetResourcetype(v string) {
  330. if p.p == nil {
  331. p.p = make(map[string]interface{})
  332. }
  333. p.p["resourcetype"] = v
  334. return
  335. }
  336. func (p *ListResourceDetailsParams) SetValue(v string) {
  337. if p.p == nil {
  338. p.p = make(map[string]interface{})
  339. }
  340. p.p["value"] = v
  341. return
  342. }
  343. // You should always use this function to get a new ListResourceDetailsParams instance,
  344. // as then you are sure you have configured all required params
  345. func (s *ResourcemetadataService) NewListResourceDetailsParams(resourcetype string) *ListResourceDetailsParams {
  346. p := &ListResourceDetailsParams{}
  347. p.p = make(map[string]interface{})
  348. p.p["resourcetype"] = resourcetype
  349. return p
  350. }
  351. // List resource detail(s)
  352. func (s *ResourcemetadataService) ListResourceDetails(p *ListResourceDetailsParams) (*ListResourceDetailsResponse, error) {
  353. resp, err := s.cs.newRequest("listResourceDetails", p.toURLValues())
  354. if err != nil {
  355. return nil, err
  356. }
  357. var r ListResourceDetailsResponse
  358. if err := json.Unmarshal(resp, &r); err != nil {
  359. return nil, err
  360. }
  361. return &r, nil
  362. }
  363. type ListResourceDetailsResponse struct {
  364. Count int `json:"count"`
  365. ResourceDetails []*ResourceDetail `json:"resourcedetail"`
  366. }
  367. type ResourceDetail struct {
  368. Account string `json:"account,omitempty"`
  369. Customer string `json:"customer,omitempty"`
  370. Domain string `json:"domain,omitempty"`
  371. Domainid string `json:"domainid,omitempty"`
  372. Key string `json:"key,omitempty"`
  373. Project string `json:"project,omitempty"`
  374. Projectid string `json:"projectid,omitempty"`
  375. Resourceid string `json:"resourceid,omitempty"`
  376. Resourcetype string `json:"resourcetype,omitempty"`
  377. Value string `json:"value,omitempty"`
  378. }