AlertService.go 11 KB

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