SwiftService.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 AddSwiftParams struct {
  24. p map[string]interface{}
  25. }
  26. func (p *AddSwiftParams) toURLValues() url.Values {
  27. u := url.Values{}
  28. if p.p == nil {
  29. return u
  30. }
  31. if v, found := p.p["account"]; found {
  32. u.Set("account", v.(string))
  33. }
  34. if v, found := p.p["key"]; found {
  35. u.Set("key", v.(string))
  36. }
  37. if v, found := p.p["url"]; found {
  38. u.Set("url", v.(string))
  39. }
  40. if v, found := p.p["username"]; found {
  41. u.Set("username", v.(string))
  42. }
  43. return u
  44. }
  45. func (p *AddSwiftParams) SetAccount(v string) {
  46. if p.p == nil {
  47. p.p = make(map[string]interface{})
  48. }
  49. p.p["account"] = v
  50. return
  51. }
  52. func (p *AddSwiftParams) SetKey(v string) {
  53. if p.p == nil {
  54. p.p = make(map[string]interface{})
  55. }
  56. p.p["key"] = v
  57. return
  58. }
  59. func (p *AddSwiftParams) SetUrl(v string) {
  60. if p.p == nil {
  61. p.p = make(map[string]interface{})
  62. }
  63. p.p["url"] = v
  64. return
  65. }
  66. func (p *AddSwiftParams) SetUsername(v string) {
  67. if p.p == nil {
  68. p.p = make(map[string]interface{})
  69. }
  70. p.p["username"] = v
  71. return
  72. }
  73. // You should always use this function to get a new AddSwiftParams instance,
  74. // as then you are sure you have configured all required params
  75. func (s *SwiftService) NewAddSwiftParams(url string) *AddSwiftParams {
  76. p := &AddSwiftParams{}
  77. p.p = make(map[string]interface{})
  78. p.p["url"] = url
  79. return p
  80. }
  81. // Adds Swift.
  82. func (s *SwiftService) AddSwift(p *AddSwiftParams) (*AddSwiftResponse, error) {
  83. resp, err := s.cs.newRequest("addSwift", p.toURLValues())
  84. if err != nil {
  85. return nil, err
  86. }
  87. var r AddSwiftResponse
  88. if err := json.Unmarshal(resp, &r); err != nil {
  89. return nil, err
  90. }
  91. return &r, nil
  92. }
  93. type AddSwiftResponse struct {
  94. Details []string `json:"details,omitempty"`
  95. Id string `json:"id,omitempty"`
  96. Name string `json:"name,omitempty"`
  97. Protocol string `json:"protocol,omitempty"`
  98. Providername string `json:"providername,omitempty"`
  99. Scope string `json:"scope,omitempty"`
  100. Url string `json:"url,omitempty"`
  101. Zoneid string `json:"zoneid,omitempty"`
  102. Zonename string `json:"zonename,omitempty"`
  103. }
  104. type ListSwiftsParams struct {
  105. p map[string]interface{}
  106. }
  107. func (p *ListSwiftsParams) toURLValues() url.Values {
  108. u := url.Values{}
  109. if p.p == nil {
  110. return u
  111. }
  112. if v, found := p.p["id"]; found {
  113. vv := strconv.FormatInt(v.(int64), 10)
  114. u.Set("id", vv)
  115. }
  116. if v, found := p.p["keyword"]; found {
  117. u.Set("keyword", v.(string))
  118. }
  119. if v, found := p.p["page"]; found {
  120. vv := strconv.Itoa(v.(int))
  121. u.Set("page", vv)
  122. }
  123. if v, found := p.p["pagesize"]; found {
  124. vv := strconv.Itoa(v.(int))
  125. u.Set("pagesize", vv)
  126. }
  127. return u
  128. }
  129. func (p *ListSwiftsParams) SetId(v int64) {
  130. if p.p == nil {
  131. p.p = make(map[string]interface{})
  132. }
  133. p.p["id"] = v
  134. return
  135. }
  136. func (p *ListSwiftsParams) SetKeyword(v string) {
  137. if p.p == nil {
  138. p.p = make(map[string]interface{})
  139. }
  140. p.p["keyword"] = v
  141. return
  142. }
  143. func (p *ListSwiftsParams) SetPage(v int) {
  144. if p.p == nil {
  145. p.p = make(map[string]interface{})
  146. }
  147. p.p["page"] = v
  148. return
  149. }
  150. func (p *ListSwiftsParams) SetPagesize(v int) {
  151. if p.p == nil {
  152. p.p = make(map[string]interface{})
  153. }
  154. p.p["pagesize"] = v
  155. return
  156. }
  157. // You should always use this function to get a new ListSwiftsParams instance,
  158. // as then you are sure you have configured all required params
  159. func (s *SwiftService) NewListSwiftsParams() *ListSwiftsParams {
  160. p := &ListSwiftsParams{}
  161. p.p = make(map[string]interface{})
  162. return p
  163. }
  164. // This is a courtesy helper function, which in some cases may not work as expected!
  165. func (s *SwiftService) GetSwiftID(keyword string, opts ...OptionFunc) (string, int, error) {
  166. p := &ListSwiftsParams{}
  167. p.p = make(map[string]interface{})
  168. p.p["keyword"] = keyword
  169. for _, fn := range opts {
  170. if err := fn(s.cs, p); err != nil {
  171. return "", -1, err
  172. }
  173. }
  174. l, err := s.ListSwifts(p)
  175. if err != nil {
  176. return "", -1, err
  177. }
  178. if l.Count == 0 {
  179. return "", l.Count, fmt.Errorf("No match found for %s: %+v", keyword, l)
  180. }
  181. if l.Count == 1 {
  182. return l.Swifts[0].Id, l.Count, nil
  183. }
  184. if l.Count > 1 {
  185. for _, v := range l.Swifts {
  186. if v.Name == keyword {
  187. return v.Id, l.Count, nil
  188. }
  189. }
  190. }
  191. return "", l.Count, fmt.Errorf("Could not find an exact match for %s: %+v", keyword, l)
  192. }
  193. // List Swift.
  194. func (s *SwiftService) ListSwifts(p *ListSwiftsParams) (*ListSwiftsResponse, error) {
  195. resp, err := s.cs.newRequest("listSwifts", p.toURLValues())
  196. if err != nil {
  197. return nil, err
  198. }
  199. var r ListSwiftsResponse
  200. if err := json.Unmarshal(resp, &r); err != nil {
  201. return nil, err
  202. }
  203. return &r, nil
  204. }
  205. type ListSwiftsResponse struct {
  206. Count int `json:"count"`
  207. Swifts []*Swift `json:"swift"`
  208. }
  209. type Swift struct {
  210. Details []string `json:"details,omitempty"`
  211. Id string `json:"id,omitempty"`
  212. Name string `json:"name,omitempty"`
  213. Protocol string `json:"protocol,omitempty"`
  214. Providername string `json:"providername,omitempty"`
  215. Scope string `json:"scope,omitempty"`
  216. Url string `json:"url,omitempty"`
  217. Zoneid string `json:"zoneid,omitempty"`
  218. Zonename string `json:"zonename,omitempty"`
  219. }