OvsElementService.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 ConfigureOvsElementParams struct {
  25. p map[string]interface{}
  26. }
  27. func (p *ConfigureOvsElementParams) toURLValues() url.Values {
  28. u := url.Values{}
  29. if p.p == nil {
  30. return u
  31. }
  32. if v, found := p.p["enabled"]; found {
  33. vv := strconv.FormatBool(v.(bool))
  34. u.Set("enabled", vv)
  35. }
  36. if v, found := p.p["id"]; found {
  37. u.Set("id", v.(string))
  38. }
  39. return u
  40. }
  41. func (p *ConfigureOvsElementParams) SetEnabled(v bool) {
  42. if p.p == nil {
  43. p.p = make(map[string]interface{})
  44. }
  45. p.p["enabled"] = v
  46. return
  47. }
  48. func (p *ConfigureOvsElementParams) SetId(v string) {
  49. if p.p == nil {
  50. p.p = make(map[string]interface{})
  51. }
  52. p.p["id"] = v
  53. return
  54. }
  55. // You should always use this function to get a new ConfigureOvsElementParams instance,
  56. // as then you are sure you have configured all required params
  57. func (s *OvsElementService) NewConfigureOvsElementParams(enabled bool, id string) *ConfigureOvsElementParams {
  58. p := &ConfigureOvsElementParams{}
  59. p.p = make(map[string]interface{})
  60. p.p["enabled"] = enabled
  61. p.p["id"] = id
  62. return p
  63. }
  64. // Configures an ovs element.
  65. func (s *OvsElementService) ConfigureOvsElement(p *ConfigureOvsElementParams) (*ConfigureOvsElementResponse, error) {
  66. resp, err := s.cs.newRequest("configureOvsElement", p.toURLValues())
  67. if err != nil {
  68. return nil, err
  69. }
  70. var r ConfigureOvsElementResponse
  71. if err := json.Unmarshal(resp, &r); err != nil {
  72. return nil, err
  73. }
  74. // If we have a async client, we need to wait for the async result
  75. if s.cs.async {
  76. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  77. if err != nil {
  78. if err == AsyncTimeoutErr {
  79. return &r, err
  80. }
  81. return nil, err
  82. }
  83. b, err = getRawValue(b)
  84. if err != nil {
  85. return nil, err
  86. }
  87. if err := json.Unmarshal(b, &r); err != nil {
  88. return nil, err
  89. }
  90. }
  91. return &r, nil
  92. }
  93. type ConfigureOvsElementResponse struct {
  94. JobID string `json:"jobid,omitempty"`
  95. Account string `json:"account,omitempty"`
  96. Domain string `json:"domain,omitempty"`
  97. Domainid string `json:"domainid,omitempty"`
  98. Enabled bool `json:"enabled,omitempty"`
  99. Id string `json:"id,omitempty"`
  100. Nspid string `json:"nspid,omitempty"`
  101. Project string `json:"project,omitempty"`
  102. Projectid string `json:"projectid,omitempty"`
  103. }
  104. type ListOvsElementsParams struct {
  105. p map[string]interface{}
  106. }
  107. func (p *ListOvsElementsParams) toURLValues() url.Values {
  108. u := url.Values{}
  109. if p.p == nil {
  110. return u
  111. }
  112. if v, found := p.p["enabled"]; found {
  113. vv := strconv.FormatBool(v.(bool))
  114. u.Set("enabled", vv)
  115. }
  116. if v, found := p.p["id"]; found {
  117. u.Set("id", v.(string))
  118. }
  119. if v, found := p.p["keyword"]; found {
  120. u.Set("keyword", v.(string))
  121. }
  122. if v, found := p.p["nspid"]; found {
  123. u.Set("nspid", v.(string))
  124. }
  125. if v, found := p.p["page"]; found {
  126. vv := strconv.Itoa(v.(int))
  127. u.Set("page", vv)
  128. }
  129. if v, found := p.p["pagesize"]; found {
  130. vv := strconv.Itoa(v.(int))
  131. u.Set("pagesize", vv)
  132. }
  133. return u
  134. }
  135. func (p *ListOvsElementsParams) SetEnabled(v bool) {
  136. if p.p == nil {
  137. p.p = make(map[string]interface{})
  138. }
  139. p.p["enabled"] = v
  140. return
  141. }
  142. func (p *ListOvsElementsParams) SetId(v string) {
  143. if p.p == nil {
  144. p.p = make(map[string]interface{})
  145. }
  146. p.p["id"] = v
  147. return
  148. }
  149. func (p *ListOvsElementsParams) SetKeyword(v string) {
  150. if p.p == nil {
  151. p.p = make(map[string]interface{})
  152. }
  153. p.p["keyword"] = v
  154. return
  155. }
  156. func (p *ListOvsElementsParams) SetNspid(v string) {
  157. if p.p == nil {
  158. p.p = make(map[string]interface{})
  159. }
  160. p.p["nspid"] = v
  161. return
  162. }
  163. func (p *ListOvsElementsParams) SetPage(v int) {
  164. if p.p == nil {
  165. p.p = make(map[string]interface{})
  166. }
  167. p.p["page"] = v
  168. return
  169. }
  170. func (p *ListOvsElementsParams) SetPagesize(v int) {
  171. if p.p == nil {
  172. p.p = make(map[string]interface{})
  173. }
  174. p.p["pagesize"] = v
  175. return
  176. }
  177. // You should always use this function to get a new ListOvsElementsParams instance,
  178. // as then you are sure you have configured all required params
  179. func (s *OvsElementService) NewListOvsElementsParams() *ListOvsElementsParams {
  180. p := &ListOvsElementsParams{}
  181. p.p = make(map[string]interface{})
  182. return p
  183. }
  184. // This is a courtesy helper function, which in some cases may not work as expected!
  185. func (s *OvsElementService) GetOvsElementByID(id string, opts ...OptionFunc) (*OvsElement, int, error) {
  186. p := &ListOvsElementsParams{}
  187. p.p = make(map[string]interface{})
  188. p.p["id"] = id
  189. for _, fn := range opts {
  190. if err := fn(s.cs, p); err != nil {
  191. return nil, -1, err
  192. }
  193. }
  194. l, err := s.ListOvsElements(p)
  195. if err != nil {
  196. if strings.Contains(err.Error(), fmt.Sprintf(
  197. "Invalid parameter id value=%s due to incorrect long value format, "+
  198. "or entity does not exist", id)) {
  199. return nil, 0, fmt.Errorf("No match found for %s: %+v", id, l)
  200. }
  201. return nil, -1, err
  202. }
  203. if l.Count == 0 {
  204. return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l)
  205. }
  206. if l.Count == 1 {
  207. return l.OvsElements[0], l.Count, nil
  208. }
  209. return nil, l.Count, fmt.Errorf("There is more then one result for OvsElement UUID: %s!", id)
  210. }
  211. // Lists all available ovs elements.
  212. func (s *OvsElementService) ListOvsElements(p *ListOvsElementsParams) (*ListOvsElementsResponse, error) {
  213. resp, err := s.cs.newRequest("listOvsElements", p.toURLValues())
  214. if err != nil {
  215. return nil, err
  216. }
  217. var r ListOvsElementsResponse
  218. if err := json.Unmarshal(resp, &r); err != nil {
  219. return nil, err
  220. }
  221. return &r, nil
  222. }
  223. type ListOvsElementsResponse struct {
  224. Count int `json:"count"`
  225. OvsElements []*OvsElement `json:"ovselement"`
  226. }
  227. type OvsElement struct {
  228. Account string `json:"account,omitempty"`
  229. Domain string `json:"domain,omitempty"`
  230. Domainid string `json:"domainid,omitempty"`
  231. Enabled bool `json:"enabled,omitempty"`
  232. Id string `json:"id,omitempty"`
  233. Nspid string `json:"nspid,omitempty"`
  234. Project string `json:"project,omitempty"`
  235. Projectid string `json:"projectid,omitempty"`
  236. }