AsyncjobService.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. "net/url"
  20. "strconv"
  21. "time"
  22. )
  23. type QueryAsyncJobResultParams struct {
  24. p map[string]interface{}
  25. }
  26. func (p *QueryAsyncJobResultParams) toURLValues() url.Values {
  27. u := url.Values{}
  28. if p.p == nil {
  29. return u
  30. }
  31. if v, found := p.p["jobid"]; found {
  32. u.Set("jobid", v.(string))
  33. }
  34. return u
  35. }
  36. func (p *QueryAsyncJobResultParams) SetJobid(v string) {
  37. if p.p == nil {
  38. p.p = make(map[string]interface{})
  39. }
  40. p.p["jobid"] = v
  41. return
  42. }
  43. // You should always use this function to get a new QueryAsyncJobResultParams instance,
  44. // as then you are sure you have configured all required params
  45. func (s *AsyncjobService) NewQueryAsyncJobResultParams(jobid string) *QueryAsyncJobResultParams {
  46. p := &QueryAsyncJobResultParams{}
  47. p.p = make(map[string]interface{})
  48. p.p["jobid"] = jobid
  49. return p
  50. }
  51. // Retrieves the current status of asynchronous job.
  52. func (s *AsyncjobService) QueryAsyncJobResult(p *QueryAsyncJobResultParams) (*QueryAsyncJobResultResponse, error) {
  53. var resp json.RawMessage
  54. var err error
  55. // We should be able to retry on failure as this call is idempotent
  56. for i := 0; i < 3; i++ {
  57. resp, err = s.cs.newRequest("queryAsyncJobResult", p.toURLValues())
  58. if err == nil {
  59. break
  60. }
  61. time.Sleep(500 * time.Millisecond)
  62. }
  63. if err != nil {
  64. return nil, err
  65. }
  66. var r QueryAsyncJobResultResponse
  67. if err := json.Unmarshal(resp, &r); err != nil {
  68. return nil, err
  69. }
  70. return &r, nil
  71. }
  72. type QueryAsyncJobResultResponse struct {
  73. Accountid string `json:"accountid,omitempty"`
  74. Cmd string `json:"cmd,omitempty"`
  75. Created string `json:"created,omitempty"`
  76. Jobinstanceid string `json:"jobinstanceid,omitempty"`
  77. Jobinstancetype string `json:"jobinstancetype,omitempty"`
  78. Jobprocstatus int `json:"jobprocstatus,omitempty"`
  79. Jobresult json.RawMessage `json:"jobresult,omitempty"`
  80. Jobresultcode int `json:"jobresultcode,omitempty"`
  81. Jobresulttype string `json:"jobresulttype,omitempty"`
  82. Jobstatus int `json:"jobstatus,omitempty"`
  83. Userid string `json:"userid,omitempty"`
  84. }
  85. type ListAsyncJobsParams struct {
  86. p map[string]interface{}
  87. }
  88. func (p *ListAsyncJobsParams) toURLValues() url.Values {
  89. u := url.Values{}
  90. if p.p == nil {
  91. return u
  92. }
  93. if v, found := p.p["account"]; found {
  94. u.Set("account", v.(string))
  95. }
  96. if v, found := p.p["domainid"]; found {
  97. u.Set("domainid", v.(string))
  98. }
  99. if v, found := p.p["isrecursive"]; found {
  100. vv := strconv.FormatBool(v.(bool))
  101. u.Set("isrecursive", vv)
  102. }
  103. if v, found := p.p["keyword"]; found {
  104. u.Set("keyword", v.(string))
  105. }
  106. if v, found := p.p["listall"]; found {
  107. vv := strconv.FormatBool(v.(bool))
  108. u.Set("listall", vv)
  109. }
  110. if v, found := p.p["page"]; found {
  111. vv := strconv.Itoa(v.(int))
  112. u.Set("page", vv)
  113. }
  114. if v, found := p.p["pagesize"]; found {
  115. vv := strconv.Itoa(v.(int))
  116. u.Set("pagesize", vv)
  117. }
  118. if v, found := p.p["startdate"]; found {
  119. u.Set("startdate", v.(string))
  120. }
  121. return u
  122. }
  123. func (p *ListAsyncJobsParams) SetAccount(v string) {
  124. if p.p == nil {
  125. p.p = make(map[string]interface{})
  126. }
  127. p.p["account"] = v
  128. return
  129. }
  130. func (p *ListAsyncJobsParams) SetDomainid(v string) {
  131. if p.p == nil {
  132. p.p = make(map[string]interface{})
  133. }
  134. p.p["domainid"] = v
  135. return
  136. }
  137. func (p *ListAsyncJobsParams) SetIsrecursive(v bool) {
  138. if p.p == nil {
  139. p.p = make(map[string]interface{})
  140. }
  141. p.p["isrecursive"] = v
  142. return
  143. }
  144. func (p *ListAsyncJobsParams) SetKeyword(v string) {
  145. if p.p == nil {
  146. p.p = make(map[string]interface{})
  147. }
  148. p.p["keyword"] = v
  149. return
  150. }
  151. func (p *ListAsyncJobsParams) SetListall(v bool) {
  152. if p.p == nil {
  153. p.p = make(map[string]interface{})
  154. }
  155. p.p["listall"] = v
  156. return
  157. }
  158. func (p *ListAsyncJobsParams) SetPage(v int) {
  159. if p.p == nil {
  160. p.p = make(map[string]interface{})
  161. }
  162. p.p["page"] = v
  163. return
  164. }
  165. func (p *ListAsyncJobsParams) SetPagesize(v int) {
  166. if p.p == nil {
  167. p.p = make(map[string]interface{})
  168. }
  169. p.p["pagesize"] = v
  170. return
  171. }
  172. func (p *ListAsyncJobsParams) SetStartdate(v string) {
  173. if p.p == nil {
  174. p.p = make(map[string]interface{})
  175. }
  176. p.p["startdate"] = v
  177. return
  178. }
  179. // You should always use this function to get a new ListAsyncJobsParams instance,
  180. // as then you are sure you have configured all required params
  181. func (s *AsyncjobService) NewListAsyncJobsParams() *ListAsyncJobsParams {
  182. p := &ListAsyncJobsParams{}
  183. p.p = make(map[string]interface{})
  184. return p
  185. }
  186. // Lists all pending asynchronous jobs for the account.
  187. func (s *AsyncjobService) ListAsyncJobs(p *ListAsyncJobsParams) (*ListAsyncJobsResponse, error) {
  188. resp, err := s.cs.newRequest("listAsyncJobs", p.toURLValues())
  189. if err != nil {
  190. return nil, err
  191. }
  192. var r ListAsyncJobsResponse
  193. if err := json.Unmarshal(resp, &r); err != nil {
  194. return nil, err
  195. }
  196. return &r, nil
  197. }
  198. type ListAsyncJobsResponse struct {
  199. Count int `json:"count"`
  200. AsyncJobs []*AsyncJob `json:"asyncjobs"`
  201. }
  202. type AsyncJob struct {
  203. Accountid string `json:"accountid,omitempty"`
  204. Cmd string `json:"cmd,omitempty"`
  205. Created string `json:"created,omitempty"`
  206. Jobinstanceid string `json:"jobinstanceid,omitempty"`
  207. Jobinstancetype string `json:"jobinstancetype,omitempty"`
  208. Jobprocstatus int `json:"jobprocstatus,omitempty"`
  209. Jobresult json.RawMessage `json:"jobresult,omitempty"`
  210. Jobresultcode int `json:"jobresultcode,omitempty"`
  211. Jobresulttype string `json:"jobresulttype,omitempty"`
  212. Jobstatus int `json:"jobstatus,omitempty"`
  213. Userid string `json:"userid,omitempty"`
  214. }