PortableIPService.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 CreatePortableIpRangeParams struct {
  25. p map[string]interface{}
  26. }
  27. func (p *CreatePortableIpRangeParams) toURLValues() url.Values {
  28. u := url.Values{}
  29. if p.p == nil {
  30. return u
  31. }
  32. if v, found := p.p["endip"]; found {
  33. u.Set("endip", v.(string))
  34. }
  35. if v, found := p.p["gateway"]; found {
  36. u.Set("gateway", v.(string))
  37. }
  38. if v, found := p.p["netmask"]; found {
  39. u.Set("netmask", v.(string))
  40. }
  41. if v, found := p.p["regionid"]; found {
  42. vv := strconv.Itoa(v.(int))
  43. u.Set("regionid", vv)
  44. }
  45. if v, found := p.p["startip"]; found {
  46. u.Set("startip", v.(string))
  47. }
  48. if v, found := p.p["vlan"]; found {
  49. u.Set("vlan", v.(string))
  50. }
  51. return u
  52. }
  53. func (p *CreatePortableIpRangeParams) SetEndip(v string) {
  54. if p.p == nil {
  55. p.p = make(map[string]interface{})
  56. }
  57. p.p["endip"] = v
  58. return
  59. }
  60. func (p *CreatePortableIpRangeParams) SetGateway(v string) {
  61. if p.p == nil {
  62. p.p = make(map[string]interface{})
  63. }
  64. p.p["gateway"] = v
  65. return
  66. }
  67. func (p *CreatePortableIpRangeParams) SetNetmask(v string) {
  68. if p.p == nil {
  69. p.p = make(map[string]interface{})
  70. }
  71. p.p["netmask"] = v
  72. return
  73. }
  74. func (p *CreatePortableIpRangeParams) SetRegionid(v int) {
  75. if p.p == nil {
  76. p.p = make(map[string]interface{})
  77. }
  78. p.p["regionid"] = v
  79. return
  80. }
  81. func (p *CreatePortableIpRangeParams) SetStartip(v string) {
  82. if p.p == nil {
  83. p.p = make(map[string]interface{})
  84. }
  85. p.p["startip"] = v
  86. return
  87. }
  88. func (p *CreatePortableIpRangeParams) SetVlan(v string) {
  89. if p.p == nil {
  90. p.p = make(map[string]interface{})
  91. }
  92. p.p["vlan"] = v
  93. return
  94. }
  95. // You should always use this function to get a new CreatePortableIpRangeParams instance,
  96. // as then you are sure you have configured all required params
  97. func (s *PortableIPService) NewCreatePortableIpRangeParams(endip string, gateway string, netmask string, regionid int, startip string) *CreatePortableIpRangeParams {
  98. p := &CreatePortableIpRangeParams{}
  99. p.p = make(map[string]interface{})
  100. p.p["endip"] = endip
  101. p.p["gateway"] = gateway
  102. p.p["netmask"] = netmask
  103. p.p["regionid"] = regionid
  104. p.p["startip"] = startip
  105. return p
  106. }
  107. // adds a range of portable public IP's to a region
  108. func (s *PortableIPService) CreatePortableIpRange(p *CreatePortableIpRangeParams) (*CreatePortableIpRangeResponse, error) {
  109. resp, err := s.cs.newRequest("createPortableIpRange", p.toURLValues())
  110. if err != nil {
  111. return nil, err
  112. }
  113. var r CreatePortableIpRangeResponse
  114. if err := json.Unmarshal(resp, &r); err != nil {
  115. return nil, err
  116. }
  117. // If we have a async client, we need to wait for the async result
  118. if s.cs.async {
  119. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  120. if err != nil {
  121. if err == AsyncTimeoutErr {
  122. return &r, err
  123. }
  124. return nil, err
  125. }
  126. b, err = getRawValue(b)
  127. if err != nil {
  128. return nil, err
  129. }
  130. if err := json.Unmarshal(b, &r); err != nil {
  131. return nil, err
  132. }
  133. }
  134. return &r, nil
  135. }
  136. type CreatePortableIpRangeResponse struct {
  137. JobID string `json:"jobid,omitempty"`
  138. Endip string `json:"endip,omitempty"`
  139. Gateway string `json:"gateway,omitempty"`
  140. Id string `json:"id,omitempty"`
  141. Netmask string `json:"netmask,omitempty"`
  142. Portableipaddress []struct {
  143. Accountid string `json:"accountid,omitempty"`
  144. Allocated string `json:"allocated,omitempty"`
  145. Domainid string `json:"domainid,omitempty"`
  146. Ipaddress string `json:"ipaddress,omitempty"`
  147. Networkid string `json:"networkid,omitempty"`
  148. Physicalnetworkid string `json:"physicalnetworkid,omitempty"`
  149. Regionid int `json:"regionid,omitempty"`
  150. State string `json:"state,omitempty"`
  151. Vpcid string `json:"vpcid,omitempty"`
  152. Zoneid string `json:"zoneid,omitempty"`
  153. } `json:"portableipaddress,omitempty"`
  154. Regionid int `json:"regionid,omitempty"`
  155. Startip string `json:"startip,omitempty"`
  156. Vlan string `json:"vlan,omitempty"`
  157. }
  158. type DeletePortableIpRangeParams struct {
  159. p map[string]interface{}
  160. }
  161. func (p *DeletePortableIpRangeParams) toURLValues() url.Values {
  162. u := url.Values{}
  163. if p.p == nil {
  164. return u
  165. }
  166. if v, found := p.p["id"]; found {
  167. u.Set("id", v.(string))
  168. }
  169. return u
  170. }
  171. func (p *DeletePortableIpRangeParams) SetId(v string) {
  172. if p.p == nil {
  173. p.p = make(map[string]interface{})
  174. }
  175. p.p["id"] = v
  176. return
  177. }
  178. // You should always use this function to get a new DeletePortableIpRangeParams instance,
  179. // as then you are sure you have configured all required params
  180. func (s *PortableIPService) NewDeletePortableIpRangeParams(id string) *DeletePortableIpRangeParams {
  181. p := &DeletePortableIpRangeParams{}
  182. p.p = make(map[string]interface{})
  183. p.p["id"] = id
  184. return p
  185. }
  186. // deletes a range of portable public IP's associated with a region
  187. func (s *PortableIPService) DeletePortableIpRange(p *DeletePortableIpRangeParams) (*DeletePortableIpRangeResponse, error) {
  188. resp, err := s.cs.newRequest("deletePortableIpRange", p.toURLValues())
  189. if err != nil {
  190. return nil, err
  191. }
  192. var r DeletePortableIpRangeResponse
  193. if err := json.Unmarshal(resp, &r); err != nil {
  194. return nil, err
  195. }
  196. // If we have a async client, we need to wait for the async result
  197. if s.cs.async {
  198. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  199. if err != nil {
  200. if err == AsyncTimeoutErr {
  201. return &r, err
  202. }
  203. return nil, err
  204. }
  205. if err := json.Unmarshal(b, &r); err != nil {
  206. return nil, err
  207. }
  208. }
  209. return &r, nil
  210. }
  211. type DeletePortableIpRangeResponse struct {
  212. JobID string `json:"jobid,omitempty"`
  213. Displaytext string `json:"displaytext,omitempty"`
  214. Success bool `json:"success,omitempty"`
  215. }
  216. type ListPortableIpRangesParams struct {
  217. p map[string]interface{}
  218. }
  219. func (p *ListPortableIpRangesParams) toURLValues() url.Values {
  220. u := url.Values{}
  221. if p.p == nil {
  222. return u
  223. }
  224. if v, found := p.p["id"]; found {
  225. u.Set("id", v.(string))
  226. }
  227. if v, found := p.p["keyword"]; found {
  228. u.Set("keyword", v.(string))
  229. }
  230. if v, found := p.p["page"]; found {
  231. vv := strconv.Itoa(v.(int))
  232. u.Set("page", vv)
  233. }
  234. if v, found := p.p["pagesize"]; found {
  235. vv := strconv.Itoa(v.(int))
  236. u.Set("pagesize", vv)
  237. }
  238. if v, found := p.p["regionid"]; found {
  239. vv := strconv.Itoa(v.(int))
  240. u.Set("regionid", vv)
  241. }
  242. return u
  243. }
  244. func (p *ListPortableIpRangesParams) SetId(v string) {
  245. if p.p == nil {
  246. p.p = make(map[string]interface{})
  247. }
  248. p.p["id"] = v
  249. return
  250. }
  251. func (p *ListPortableIpRangesParams) SetKeyword(v string) {
  252. if p.p == nil {
  253. p.p = make(map[string]interface{})
  254. }
  255. p.p["keyword"] = v
  256. return
  257. }
  258. func (p *ListPortableIpRangesParams) SetPage(v int) {
  259. if p.p == nil {
  260. p.p = make(map[string]interface{})
  261. }
  262. p.p["page"] = v
  263. return
  264. }
  265. func (p *ListPortableIpRangesParams) SetPagesize(v int) {
  266. if p.p == nil {
  267. p.p = make(map[string]interface{})
  268. }
  269. p.p["pagesize"] = v
  270. return
  271. }
  272. func (p *ListPortableIpRangesParams) SetRegionid(v int) {
  273. if p.p == nil {
  274. p.p = make(map[string]interface{})
  275. }
  276. p.p["regionid"] = v
  277. return
  278. }
  279. // You should always use this function to get a new ListPortableIpRangesParams instance,
  280. // as then you are sure you have configured all required params
  281. func (s *PortableIPService) NewListPortableIpRangesParams() *ListPortableIpRangesParams {
  282. p := &ListPortableIpRangesParams{}
  283. p.p = make(map[string]interface{})
  284. return p
  285. }
  286. // This is a courtesy helper function, which in some cases may not work as expected!
  287. func (s *PortableIPService) GetPortableIpRangeByID(id string, opts ...OptionFunc) (*PortableIpRange, int, error) {
  288. p := &ListPortableIpRangesParams{}
  289. p.p = make(map[string]interface{})
  290. p.p["id"] = id
  291. for _, fn := range opts {
  292. if err := fn(s.cs, p); err != nil {
  293. return nil, -1, err
  294. }
  295. }
  296. l, err := s.ListPortableIpRanges(p)
  297. if err != nil {
  298. if strings.Contains(err.Error(), fmt.Sprintf(
  299. "Invalid parameter id value=%s due to incorrect long value format, "+
  300. "or entity does not exist", id)) {
  301. return nil, 0, fmt.Errorf("No match found for %s: %+v", id, l)
  302. }
  303. return nil, -1, err
  304. }
  305. if l.Count == 0 {
  306. return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l)
  307. }
  308. if l.Count == 1 {
  309. return l.PortableIpRanges[0], l.Count, nil
  310. }
  311. return nil, l.Count, fmt.Errorf("There is more then one result for PortableIpRange UUID: %s!", id)
  312. }
  313. // list portable IP ranges
  314. func (s *PortableIPService) ListPortableIpRanges(p *ListPortableIpRangesParams) (*ListPortableIpRangesResponse, error) {
  315. resp, err := s.cs.newRequest("listPortableIpRanges", p.toURLValues())
  316. if err != nil {
  317. return nil, err
  318. }
  319. var r ListPortableIpRangesResponse
  320. if err := json.Unmarshal(resp, &r); err != nil {
  321. return nil, err
  322. }
  323. return &r, nil
  324. }
  325. type ListPortableIpRangesResponse struct {
  326. Count int `json:"count"`
  327. PortableIpRanges []*PortableIpRange `json:"portableiprange"`
  328. }
  329. type PortableIpRange struct {
  330. Endip string `json:"endip,omitempty"`
  331. Gateway string `json:"gateway,omitempty"`
  332. Id string `json:"id,omitempty"`
  333. Netmask string `json:"netmask,omitempty"`
  334. Portableipaddress []struct {
  335. Accountid string `json:"accountid,omitempty"`
  336. Allocated string `json:"allocated,omitempty"`
  337. Domainid string `json:"domainid,omitempty"`
  338. Ipaddress string `json:"ipaddress,omitempty"`
  339. Networkid string `json:"networkid,omitempty"`
  340. Physicalnetworkid string `json:"physicalnetworkid,omitempty"`
  341. Regionid int `json:"regionid,omitempty"`
  342. State string `json:"state,omitempty"`
  343. Vpcid string `json:"vpcid,omitempty"`
  344. Zoneid string `json:"zoneid,omitempty"`
  345. } `json:"portableipaddress,omitempty"`
  346. Regionid int `json:"regionid,omitempty"`
  347. Startip string `json:"startip,omitempty"`
  348. Vlan string `json:"vlan,omitempty"`
  349. }