EventService.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. "strings"
  23. )
  24. type ListEventsParams struct {
  25. p map[string]interface{}
  26. }
  27. func (p *ListEventsParams) toURLValues() url.Values {
  28. u := url.Values{}
  29. if p.p == nil {
  30. return u
  31. }
  32. if v, found := p.p["account"]; found {
  33. u.Set("account", v.(string))
  34. }
  35. if v, found := p.p["domainid"]; found {
  36. u.Set("domainid", v.(string))
  37. }
  38. if v, found := p.p["duration"]; found {
  39. vv := strconv.Itoa(v.(int))
  40. u.Set("duration", vv)
  41. }
  42. if v, found := p.p["enddate"]; found {
  43. u.Set("enddate", v.(string))
  44. }
  45. if v, found := p.p["entrytime"]; found {
  46. vv := strconv.Itoa(v.(int))
  47. u.Set("entrytime", vv)
  48. }
  49. if v, found := p.p["id"]; found {
  50. u.Set("id", v.(string))
  51. }
  52. if v, found := p.p["isrecursive"]; found {
  53. vv := strconv.FormatBool(v.(bool))
  54. u.Set("isrecursive", vv)
  55. }
  56. if v, found := p.p["keyword"]; found {
  57. u.Set("keyword", v.(string))
  58. }
  59. if v, found := p.p["level"]; found {
  60. u.Set("level", v.(string))
  61. }
  62. if v, found := p.p["listall"]; found {
  63. vv := strconv.FormatBool(v.(bool))
  64. u.Set("listall", vv)
  65. }
  66. if v, found := p.p["page"]; found {
  67. vv := strconv.Itoa(v.(int))
  68. u.Set("page", vv)
  69. }
  70. if v, found := p.p["pagesize"]; found {
  71. vv := strconv.Itoa(v.(int))
  72. u.Set("pagesize", vv)
  73. }
  74. if v, found := p.p["projectid"]; found {
  75. u.Set("projectid", v.(string))
  76. }
  77. if v, found := p.p["startdate"]; found {
  78. u.Set("startdate", v.(string))
  79. }
  80. if v, found := p.p["type"]; found {
  81. u.Set("type", v.(string))
  82. }
  83. return u
  84. }
  85. func (p *ListEventsParams) SetAccount(v string) {
  86. if p.p == nil {
  87. p.p = make(map[string]interface{})
  88. }
  89. p.p["account"] = v
  90. return
  91. }
  92. func (p *ListEventsParams) SetDomainid(v string) {
  93. if p.p == nil {
  94. p.p = make(map[string]interface{})
  95. }
  96. p.p["domainid"] = v
  97. return
  98. }
  99. func (p *ListEventsParams) SetDuration(v int) {
  100. if p.p == nil {
  101. p.p = make(map[string]interface{})
  102. }
  103. p.p["duration"] = v
  104. return
  105. }
  106. func (p *ListEventsParams) SetEnddate(v string) {
  107. if p.p == nil {
  108. p.p = make(map[string]interface{})
  109. }
  110. p.p["enddate"] = v
  111. return
  112. }
  113. func (p *ListEventsParams) SetEntrytime(v int) {
  114. if p.p == nil {
  115. p.p = make(map[string]interface{})
  116. }
  117. p.p["entrytime"] = v
  118. return
  119. }
  120. func (p *ListEventsParams) SetId(v string) {
  121. if p.p == nil {
  122. p.p = make(map[string]interface{})
  123. }
  124. p.p["id"] = v
  125. return
  126. }
  127. func (p *ListEventsParams) SetIsrecursive(v bool) {
  128. if p.p == nil {
  129. p.p = make(map[string]interface{})
  130. }
  131. p.p["isrecursive"] = v
  132. return
  133. }
  134. func (p *ListEventsParams) SetKeyword(v string) {
  135. if p.p == nil {
  136. p.p = make(map[string]interface{})
  137. }
  138. p.p["keyword"] = v
  139. return
  140. }
  141. func (p *ListEventsParams) SetLevel(v string) {
  142. if p.p == nil {
  143. p.p = make(map[string]interface{})
  144. }
  145. p.p["level"] = v
  146. return
  147. }
  148. func (p *ListEventsParams) SetListall(v bool) {
  149. if p.p == nil {
  150. p.p = make(map[string]interface{})
  151. }
  152. p.p["listall"] = v
  153. return
  154. }
  155. func (p *ListEventsParams) SetPage(v int) {
  156. if p.p == nil {
  157. p.p = make(map[string]interface{})
  158. }
  159. p.p["page"] = v
  160. return
  161. }
  162. func (p *ListEventsParams) SetPagesize(v int) {
  163. if p.p == nil {
  164. p.p = make(map[string]interface{})
  165. }
  166. p.p["pagesize"] = v
  167. return
  168. }
  169. func (p *ListEventsParams) SetProjectid(v string) {
  170. if p.p == nil {
  171. p.p = make(map[string]interface{})
  172. }
  173. p.p["projectid"] = v
  174. return
  175. }
  176. func (p *ListEventsParams) SetStartdate(v string) {
  177. if p.p == nil {
  178. p.p = make(map[string]interface{})
  179. }
  180. p.p["startdate"] = v
  181. return
  182. }
  183. func (p *ListEventsParams) SetType(v string) {
  184. if p.p == nil {
  185. p.p = make(map[string]interface{})
  186. }
  187. p.p["eventType"] = v
  188. return
  189. }
  190. // You should always use this function to get a new ListEventsParams instance,
  191. // as then you are sure you have configured all required params
  192. func (s *EventService) NewListEventsParams() *ListEventsParams {
  193. p := &ListEventsParams{}
  194. p.p = make(map[string]interface{})
  195. return p
  196. }
  197. // This is a courtesy helper function, which in some cases may not work as expected!
  198. func (s *EventService) GetEventByID(id string, opts ...OptionFunc) (*Event, int, error) {
  199. p := &ListEventsParams{}
  200. p.p = make(map[string]interface{})
  201. p.p["id"] = id
  202. for _, fn := range opts {
  203. if err := fn(s.cs, p); err != nil {
  204. return nil, -1, err
  205. }
  206. }
  207. l, err := s.ListEvents(p)
  208. if err != nil {
  209. if strings.Contains(err.Error(), fmt.Sprintf(
  210. "Invalid parameter id value=%s due to incorrect long value format, "+
  211. "or entity does not exist", id)) {
  212. return nil, 0, fmt.Errorf("No match found for %s: %+v", id, l)
  213. }
  214. return nil, -1, err
  215. }
  216. if l.Count == 0 {
  217. return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l)
  218. }
  219. if l.Count == 1 {
  220. return l.Events[0], l.Count, nil
  221. }
  222. return nil, l.Count, fmt.Errorf("There is more then one result for Event UUID: %s!", id)
  223. }
  224. // A command to list events.
  225. func (s *EventService) ListEvents(p *ListEventsParams) (*ListEventsResponse, error) {
  226. resp, err := s.cs.newRequest("listEvents", p.toURLValues())
  227. if err != nil {
  228. return nil, err
  229. }
  230. var r ListEventsResponse
  231. if err := json.Unmarshal(resp, &r); err != nil {
  232. return nil, err
  233. }
  234. return &r, nil
  235. }
  236. type ListEventsResponse struct {
  237. Count int `json:"count"`
  238. Events []*Event `json:"event"`
  239. }
  240. type Event struct {
  241. Account string `json:"account,omitempty"`
  242. Created string `json:"created,omitempty"`
  243. Description string `json:"description,omitempty"`
  244. Domain string `json:"domain,omitempty"`
  245. Domainid string `json:"domainid,omitempty"`
  246. Id string `json:"id,omitempty"`
  247. Level string `json:"level,omitempty"`
  248. Parentid string `json:"parentid,omitempty"`
  249. Project string `json:"project,omitempty"`
  250. Projectid string `json:"projectid,omitempty"`
  251. State string `json:"state,omitempty"`
  252. Type string `json:"type,omitempty"`
  253. Username string `json:"username,omitempty"`
  254. }
  255. type ListEventTypesParams struct {
  256. p map[string]interface{}
  257. }
  258. func (p *ListEventTypesParams) toURLValues() url.Values {
  259. u := url.Values{}
  260. if p.p == nil {
  261. return u
  262. }
  263. return u
  264. }
  265. // You should always use this function to get a new ListEventTypesParams instance,
  266. // as then you are sure you have configured all required params
  267. func (s *EventService) NewListEventTypesParams() *ListEventTypesParams {
  268. p := &ListEventTypesParams{}
  269. p.p = make(map[string]interface{})
  270. return p
  271. }
  272. // List Event Types
  273. func (s *EventService) ListEventTypes(p *ListEventTypesParams) (*ListEventTypesResponse, error) {
  274. resp, err := s.cs.newRequest("listEventTypes", p.toURLValues())
  275. if err != nil {
  276. return nil, err
  277. }
  278. var r ListEventTypesResponse
  279. if err := json.Unmarshal(resp, &r); err != nil {
  280. return nil, err
  281. }
  282. return &r, nil
  283. }
  284. type ListEventTypesResponse struct {
  285. Count int `json:"count"`
  286. EventTypes []*EventType `json:"eventtype"`
  287. }
  288. type EventType struct {
  289. Name string `json:"name,omitempty"`
  290. }
  291. type ArchiveEventsParams struct {
  292. p map[string]interface{}
  293. }
  294. func (p *ArchiveEventsParams) toURLValues() url.Values {
  295. u := url.Values{}
  296. if p.p == nil {
  297. return u
  298. }
  299. if v, found := p.p["enddate"]; found {
  300. u.Set("enddate", v.(string))
  301. }
  302. if v, found := p.p["ids"]; found {
  303. vv := strings.Join(v.([]string), ",")
  304. u.Set("ids", vv)
  305. }
  306. if v, found := p.p["startdate"]; found {
  307. u.Set("startdate", v.(string))
  308. }
  309. if v, found := p.p["type"]; found {
  310. u.Set("type", v.(string))
  311. }
  312. return u
  313. }
  314. func (p *ArchiveEventsParams) SetEnddate(v string) {
  315. if p.p == nil {
  316. p.p = make(map[string]interface{})
  317. }
  318. p.p["enddate"] = v
  319. return
  320. }
  321. func (p *ArchiveEventsParams) SetIds(v []string) {
  322. if p.p == nil {
  323. p.p = make(map[string]interface{})
  324. }
  325. p.p["ids"] = v
  326. return
  327. }
  328. func (p *ArchiveEventsParams) SetStartdate(v string) {
  329. if p.p == nil {
  330. p.p = make(map[string]interface{})
  331. }
  332. p.p["startdate"] = v
  333. return
  334. }
  335. func (p *ArchiveEventsParams) SetType(v string) {
  336. if p.p == nil {
  337. p.p = make(map[string]interface{})
  338. }
  339. p.p["eventType"] = v
  340. return
  341. }
  342. // You should always use this function to get a new ArchiveEventsParams instance,
  343. // as then you are sure you have configured all required params
  344. func (s *EventService) NewArchiveEventsParams() *ArchiveEventsParams {
  345. p := &ArchiveEventsParams{}
  346. p.p = make(map[string]interface{})
  347. return p
  348. }
  349. // Archive one or more events.
  350. func (s *EventService) ArchiveEvents(p *ArchiveEventsParams) (*ArchiveEventsResponse, error) {
  351. resp, err := s.cs.newRequest("archiveEvents", p.toURLValues())
  352. if err != nil {
  353. return nil, err
  354. }
  355. var r ArchiveEventsResponse
  356. if err := json.Unmarshal(resp, &r); err != nil {
  357. return nil, err
  358. }
  359. return &r, nil
  360. }
  361. type ArchiveEventsResponse struct {
  362. Displaytext string `json:"displaytext,omitempty"`
  363. Success string `json:"success,omitempty"`
  364. }
  365. type DeleteEventsParams struct {
  366. p map[string]interface{}
  367. }
  368. func (p *DeleteEventsParams) toURLValues() url.Values {
  369. u := url.Values{}
  370. if p.p == nil {
  371. return u
  372. }
  373. if v, found := p.p["enddate"]; found {
  374. u.Set("enddate", v.(string))
  375. }
  376. if v, found := p.p["ids"]; found {
  377. vv := strings.Join(v.([]string), ",")
  378. u.Set("ids", vv)
  379. }
  380. if v, found := p.p["startdate"]; found {
  381. u.Set("startdate", v.(string))
  382. }
  383. if v, found := p.p["type"]; found {
  384. u.Set("type", v.(string))
  385. }
  386. return u
  387. }
  388. func (p *DeleteEventsParams) SetEnddate(v string) {
  389. if p.p == nil {
  390. p.p = make(map[string]interface{})
  391. }
  392. p.p["enddate"] = v
  393. return
  394. }
  395. func (p *DeleteEventsParams) SetIds(v []string) {
  396. if p.p == nil {
  397. p.p = make(map[string]interface{})
  398. }
  399. p.p["ids"] = v
  400. return
  401. }
  402. func (p *DeleteEventsParams) SetStartdate(v string) {
  403. if p.p == nil {
  404. p.p = make(map[string]interface{})
  405. }
  406. p.p["startdate"] = v
  407. return
  408. }
  409. func (p *DeleteEventsParams) SetType(v string) {
  410. if p.p == nil {
  411. p.p = make(map[string]interface{})
  412. }
  413. p.p["eventType"] = v
  414. return
  415. }
  416. // You should always use this function to get a new DeleteEventsParams instance,
  417. // as then you are sure you have configured all required params
  418. func (s *EventService) NewDeleteEventsParams() *DeleteEventsParams {
  419. p := &DeleteEventsParams{}
  420. p.p = make(map[string]interface{})
  421. return p
  422. }
  423. // Delete one or more events.
  424. func (s *EventService) DeleteEvents(p *DeleteEventsParams) (*DeleteEventsResponse, error) {
  425. resp, err := s.cs.newRequest("deleteEvents", p.toURLValues())
  426. if err != nil {
  427. return nil, err
  428. }
  429. var r DeleteEventsResponse
  430. if err := json.Unmarshal(resp, &r); err != nil {
  431. return nil, err
  432. }
  433. return &r, nil
  434. }
  435. type DeleteEventsResponse struct {
  436. Displaytext string `json:"displaytext,omitempty"`
  437. Success string `json:"success,omitempty"`
  438. }