NATService.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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 EnableStaticNatParams struct {
  25. p map[string]interface{}
  26. }
  27. func (p *EnableStaticNatParams) toURLValues() url.Values {
  28. u := url.Values{}
  29. if p.p == nil {
  30. return u
  31. }
  32. if v, found := p.p["ipaddressid"]; found {
  33. u.Set("ipaddressid", v.(string))
  34. }
  35. if v, found := p.p["networkid"]; found {
  36. u.Set("networkid", v.(string))
  37. }
  38. if v, found := p.p["virtualmachineid"]; found {
  39. u.Set("virtualmachineid", v.(string))
  40. }
  41. if v, found := p.p["vmguestip"]; found {
  42. u.Set("vmguestip", v.(string))
  43. }
  44. return u
  45. }
  46. func (p *EnableStaticNatParams) SetIpaddressid(v string) {
  47. if p.p == nil {
  48. p.p = make(map[string]interface{})
  49. }
  50. p.p["ipaddressid"] = v
  51. return
  52. }
  53. func (p *EnableStaticNatParams) SetNetworkid(v string) {
  54. if p.p == nil {
  55. p.p = make(map[string]interface{})
  56. }
  57. p.p["networkid"] = v
  58. return
  59. }
  60. func (p *EnableStaticNatParams) SetVirtualmachineid(v string) {
  61. if p.p == nil {
  62. p.p = make(map[string]interface{})
  63. }
  64. p.p["virtualmachineid"] = v
  65. return
  66. }
  67. func (p *EnableStaticNatParams) SetVmguestip(v string) {
  68. if p.p == nil {
  69. p.p = make(map[string]interface{})
  70. }
  71. p.p["vmguestip"] = v
  72. return
  73. }
  74. // You should always use this function to get a new EnableStaticNatParams instance,
  75. // as then you are sure you have configured all required params
  76. func (s *NATService) NewEnableStaticNatParams(ipaddressid string, virtualmachineid string) *EnableStaticNatParams {
  77. p := &EnableStaticNatParams{}
  78. p.p = make(map[string]interface{})
  79. p.p["ipaddressid"] = ipaddressid
  80. p.p["virtualmachineid"] = virtualmachineid
  81. return p
  82. }
  83. // Enables static NAT for given IP address
  84. func (s *NATService) EnableStaticNat(p *EnableStaticNatParams) (*EnableStaticNatResponse, error) {
  85. resp, err := s.cs.newRequest("enableStaticNat", p.toURLValues())
  86. if err != nil {
  87. return nil, err
  88. }
  89. var r EnableStaticNatResponse
  90. if err := json.Unmarshal(resp, &r); err != nil {
  91. return nil, err
  92. }
  93. return &r, nil
  94. }
  95. type EnableStaticNatResponse struct {
  96. Displaytext string `json:"displaytext,omitempty"`
  97. Success string `json:"success,omitempty"`
  98. }
  99. type CreateIpForwardingRuleParams struct {
  100. p map[string]interface{}
  101. }
  102. func (p *CreateIpForwardingRuleParams) toURLValues() url.Values {
  103. u := url.Values{}
  104. if p.p == nil {
  105. return u
  106. }
  107. if v, found := p.p["cidrlist"]; found {
  108. vv := strings.Join(v.([]string), ",")
  109. u.Set("cidrlist", vv)
  110. }
  111. if v, found := p.p["endport"]; found {
  112. vv := strconv.Itoa(v.(int))
  113. u.Set("endport", vv)
  114. }
  115. if v, found := p.p["ipaddressid"]; found {
  116. u.Set("ipaddressid", v.(string))
  117. }
  118. if v, found := p.p["openfirewall"]; found {
  119. vv := strconv.FormatBool(v.(bool))
  120. u.Set("openfirewall", vv)
  121. }
  122. if v, found := p.p["protocol"]; found {
  123. u.Set("protocol", v.(string))
  124. }
  125. if v, found := p.p["startport"]; found {
  126. vv := strconv.Itoa(v.(int))
  127. u.Set("startport", vv)
  128. }
  129. return u
  130. }
  131. func (p *CreateIpForwardingRuleParams) SetCidrlist(v []string) {
  132. if p.p == nil {
  133. p.p = make(map[string]interface{})
  134. }
  135. p.p["cidrlist"] = v
  136. return
  137. }
  138. func (p *CreateIpForwardingRuleParams) SetEndport(v int) {
  139. if p.p == nil {
  140. p.p = make(map[string]interface{})
  141. }
  142. p.p["endport"] = v
  143. return
  144. }
  145. func (p *CreateIpForwardingRuleParams) SetIpaddressid(v string) {
  146. if p.p == nil {
  147. p.p = make(map[string]interface{})
  148. }
  149. p.p["ipaddressid"] = v
  150. return
  151. }
  152. func (p *CreateIpForwardingRuleParams) SetOpenfirewall(v bool) {
  153. if p.p == nil {
  154. p.p = make(map[string]interface{})
  155. }
  156. p.p["openfirewall"] = v
  157. return
  158. }
  159. func (p *CreateIpForwardingRuleParams) SetProtocol(v string) {
  160. if p.p == nil {
  161. p.p = make(map[string]interface{})
  162. }
  163. p.p["protocol"] = v
  164. return
  165. }
  166. func (p *CreateIpForwardingRuleParams) SetStartport(v int) {
  167. if p.p == nil {
  168. p.p = make(map[string]interface{})
  169. }
  170. p.p["startport"] = v
  171. return
  172. }
  173. // You should always use this function to get a new CreateIpForwardingRuleParams instance,
  174. // as then you are sure you have configured all required params
  175. func (s *NATService) NewCreateIpForwardingRuleParams(ipaddressid string, protocol string, startport int) *CreateIpForwardingRuleParams {
  176. p := &CreateIpForwardingRuleParams{}
  177. p.p = make(map[string]interface{})
  178. p.p["ipaddressid"] = ipaddressid
  179. p.p["protocol"] = protocol
  180. p.p["startport"] = startport
  181. return p
  182. }
  183. // Creates an IP forwarding rule
  184. func (s *NATService) CreateIpForwardingRule(p *CreateIpForwardingRuleParams) (*CreateIpForwardingRuleResponse, error) {
  185. resp, err := s.cs.newRequest("createIpForwardingRule", p.toURLValues())
  186. if err != nil {
  187. return nil, err
  188. }
  189. var r CreateIpForwardingRuleResponse
  190. if err := json.Unmarshal(resp, &r); err != nil {
  191. return nil, err
  192. }
  193. // If we have a async client, we need to wait for the async result
  194. if s.cs.async {
  195. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  196. if err != nil {
  197. if err == AsyncTimeoutErr {
  198. return &r, err
  199. }
  200. return nil, err
  201. }
  202. b, err = getRawValue(b)
  203. if err != nil {
  204. return nil, err
  205. }
  206. if err := json.Unmarshal(b, &r); err != nil {
  207. return nil, err
  208. }
  209. }
  210. return &r, nil
  211. }
  212. type CreateIpForwardingRuleResponse struct {
  213. JobID string `json:"jobid,omitempty"`
  214. Cidrlist string `json:"cidrlist,omitempty"`
  215. Fordisplay bool `json:"fordisplay,omitempty"`
  216. Id string `json:"id,omitempty"`
  217. Ipaddress string `json:"ipaddress,omitempty"`
  218. Ipaddressid string `json:"ipaddressid,omitempty"`
  219. Networkid string `json:"networkid,omitempty"`
  220. Privateendport string `json:"privateendport,omitempty"`
  221. Privateport string `json:"privateport,omitempty"`
  222. Protocol string `json:"protocol,omitempty"`
  223. Publicendport string `json:"publicendport,omitempty"`
  224. Publicport string `json:"publicport,omitempty"`
  225. State string `json:"state,omitempty"`
  226. Tags []struct {
  227. Account string `json:"account,omitempty"`
  228. Customer string `json:"customer,omitempty"`
  229. Domain string `json:"domain,omitempty"`
  230. Domainid string `json:"domainid,omitempty"`
  231. Key string `json:"key,omitempty"`
  232. Project string `json:"project,omitempty"`
  233. Projectid string `json:"projectid,omitempty"`
  234. Resourceid string `json:"resourceid,omitempty"`
  235. Resourcetype string `json:"resourcetype,omitempty"`
  236. Value string `json:"value,omitempty"`
  237. } `json:"tags,omitempty"`
  238. Virtualmachinedisplayname string `json:"virtualmachinedisplayname,omitempty"`
  239. Virtualmachineid string `json:"virtualmachineid,omitempty"`
  240. Virtualmachinename string `json:"virtualmachinename,omitempty"`
  241. Vmguestip string `json:"vmguestip,omitempty"`
  242. }
  243. type DeleteIpForwardingRuleParams struct {
  244. p map[string]interface{}
  245. }
  246. func (p *DeleteIpForwardingRuleParams) toURLValues() url.Values {
  247. u := url.Values{}
  248. if p.p == nil {
  249. return u
  250. }
  251. if v, found := p.p["id"]; found {
  252. u.Set("id", v.(string))
  253. }
  254. return u
  255. }
  256. func (p *DeleteIpForwardingRuleParams) SetId(v string) {
  257. if p.p == nil {
  258. p.p = make(map[string]interface{})
  259. }
  260. p.p["id"] = v
  261. return
  262. }
  263. // You should always use this function to get a new DeleteIpForwardingRuleParams instance,
  264. // as then you are sure you have configured all required params
  265. func (s *NATService) NewDeleteIpForwardingRuleParams(id string) *DeleteIpForwardingRuleParams {
  266. p := &DeleteIpForwardingRuleParams{}
  267. p.p = make(map[string]interface{})
  268. p.p["id"] = id
  269. return p
  270. }
  271. // Deletes an IP forwarding rule
  272. func (s *NATService) DeleteIpForwardingRule(p *DeleteIpForwardingRuleParams) (*DeleteIpForwardingRuleResponse, error) {
  273. resp, err := s.cs.newRequest("deleteIpForwardingRule", p.toURLValues())
  274. if err != nil {
  275. return nil, err
  276. }
  277. var r DeleteIpForwardingRuleResponse
  278. if err := json.Unmarshal(resp, &r); err != nil {
  279. return nil, err
  280. }
  281. // If we have a async client, we need to wait for the async result
  282. if s.cs.async {
  283. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  284. if err != nil {
  285. if err == AsyncTimeoutErr {
  286. return &r, err
  287. }
  288. return nil, err
  289. }
  290. if err := json.Unmarshal(b, &r); err != nil {
  291. return nil, err
  292. }
  293. }
  294. return &r, nil
  295. }
  296. type DeleteIpForwardingRuleResponse struct {
  297. JobID string `json:"jobid,omitempty"`
  298. Displaytext string `json:"displaytext,omitempty"`
  299. Success bool `json:"success,omitempty"`
  300. }
  301. type ListIpForwardingRulesParams struct {
  302. p map[string]interface{}
  303. }
  304. func (p *ListIpForwardingRulesParams) toURLValues() url.Values {
  305. u := url.Values{}
  306. if p.p == nil {
  307. return u
  308. }
  309. if v, found := p.p["account"]; found {
  310. u.Set("account", v.(string))
  311. }
  312. if v, found := p.p["domainid"]; found {
  313. u.Set("domainid", v.(string))
  314. }
  315. if v, found := p.p["id"]; found {
  316. u.Set("id", v.(string))
  317. }
  318. if v, found := p.p["ipaddressid"]; found {
  319. u.Set("ipaddressid", v.(string))
  320. }
  321. if v, found := p.p["isrecursive"]; found {
  322. vv := strconv.FormatBool(v.(bool))
  323. u.Set("isrecursive", vv)
  324. }
  325. if v, found := p.p["keyword"]; found {
  326. u.Set("keyword", v.(string))
  327. }
  328. if v, found := p.p["listall"]; found {
  329. vv := strconv.FormatBool(v.(bool))
  330. u.Set("listall", vv)
  331. }
  332. if v, found := p.p["page"]; found {
  333. vv := strconv.Itoa(v.(int))
  334. u.Set("page", vv)
  335. }
  336. if v, found := p.p["pagesize"]; found {
  337. vv := strconv.Itoa(v.(int))
  338. u.Set("pagesize", vv)
  339. }
  340. if v, found := p.p["projectid"]; found {
  341. u.Set("projectid", v.(string))
  342. }
  343. if v, found := p.p["virtualmachineid"]; found {
  344. u.Set("virtualmachineid", v.(string))
  345. }
  346. return u
  347. }
  348. func (p *ListIpForwardingRulesParams) SetAccount(v string) {
  349. if p.p == nil {
  350. p.p = make(map[string]interface{})
  351. }
  352. p.p["account"] = v
  353. return
  354. }
  355. func (p *ListIpForwardingRulesParams) SetDomainid(v string) {
  356. if p.p == nil {
  357. p.p = make(map[string]interface{})
  358. }
  359. p.p["domainid"] = v
  360. return
  361. }
  362. func (p *ListIpForwardingRulesParams) SetId(v string) {
  363. if p.p == nil {
  364. p.p = make(map[string]interface{})
  365. }
  366. p.p["id"] = v
  367. return
  368. }
  369. func (p *ListIpForwardingRulesParams) SetIpaddressid(v string) {
  370. if p.p == nil {
  371. p.p = make(map[string]interface{})
  372. }
  373. p.p["ipaddressid"] = v
  374. return
  375. }
  376. func (p *ListIpForwardingRulesParams) SetIsrecursive(v bool) {
  377. if p.p == nil {
  378. p.p = make(map[string]interface{})
  379. }
  380. p.p["isrecursive"] = v
  381. return
  382. }
  383. func (p *ListIpForwardingRulesParams) SetKeyword(v string) {
  384. if p.p == nil {
  385. p.p = make(map[string]interface{})
  386. }
  387. p.p["keyword"] = v
  388. return
  389. }
  390. func (p *ListIpForwardingRulesParams) SetListall(v bool) {
  391. if p.p == nil {
  392. p.p = make(map[string]interface{})
  393. }
  394. p.p["listall"] = v
  395. return
  396. }
  397. func (p *ListIpForwardingRulesParams) SetPage(v int) {
  398. if p.p == nil {
  399. p.p = make(map[string]interface{})
  400. }
  401. p.p["page"] = v
  402. return
  403. }
  404. func (p *ListIpForwardingRulesParams) SetPagesize(v int) {
  405. if p.p == nil {
  406. p.p = make(map[string]interface{})
  407. }
  408. p.p["pagesize"] = v
  409. return
  410. }
  411. func (p *ListIpForwardingRulesParams) SetProjectid(v string) {
  412. if p.p == nil {
  413. p.p = make(map[string]interface{})
  414. }
  415. p.p["projectid"] = v
  416. return
  417. }
  418. func (p *ListIpForwardingRulesParams) SetVirtualmachineid(v string) {
  419. if p.p == nil {
  420. p.p = make(map[string]interface{})
  421. }
  422. p.p["virtualmachineid"] = v
  423. return
  424. }
  425. // You should always use this function to get a new ListIpForwardingRulesParams instance,
  426. // as then you are sure you have configured all required params
  427. func (s *NATService) NewListIpForwardingRulesParams() *ListIpForwardingRulesParams {
  428. p := &ListIpForwardingRulesParams{}
  429. p.p = make(map[string]interface{})
  430. return p
  431. }
  432. // This is a courtesy helper function, which in some cases may not work as expected!
  433. func (s *NATService) GetIpForwardingRuleByID(id string, opts ...OptionFunc) (*IpForwardingRule, int, error) {
  434. p := &ListIpForwardingRulesParams{}
  435. p.p = make(map[string]interface{})
  436. p.p["id"] = id
  437. for _, fn := range opts {
  438. if err := fn(s.cs, p); err != nil {
  439. return nil, -1, err
  440. }
  441. }
  442. l, err := s.ListIpForwardingRules(p)
  443. if err != nil {
  444. if strings.Contains(err.Error(), fmt.Sprintf(
  445. "Invalid parameter id value=%s due to incorrect long value format, "+
  446. "or entity does not exist", id)) {
  447. return nil, 0, fmt.Errorf("No match found for %s: %+v", id, l)
  448. }
  449. return nil, -1, err
  450. }
  451. if l.Count == 0 {
  452. return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l)
  453. }
  454. if l.Count == 1 {
  455. return l.IpForwardingRules[0], l.Count, nil
  456. }
  457. return nil, l.Count, fmt.Errorf("There is more then one result for IpForwardingRule UUID: %s!", id)
  458. }
  459. // List the IP forwarding rules
  460. func (s *NATService) ListIpForwardingRules(p *ListIpForwardingRulesParams) (*ListIpForwardingRulesResponse, error) {
  461. resp, err := s.cs.newRequest("listIpForwardingRules", p.toURLValues())
  462. if err != nil {
  463. return nil, err
  464. }
  465. var r ListIpForwardingRulesResponse
  466. if err := json.Unmarshal(resp, &r); err != nil {
  467. return nil, err
  468. }
  469. return &r, nil
  470. }
  471. type ListIpForwardingRulesResponse struct {
  472. Count int `json:"count"`
  473. IpForwardingRules []*IpForwardingRule `json:"ipforwardingrule"`
  474. }
  475. type IpForwardingRule struct {
  476. Cidrlist string `json:"cidrlist,omitempty"`
  477. Fordisplay bool `json:"fordisplay,omitempty"`
  478. Id string `json:"id,omitempty"`
  479. Ipaddress string `json:"ipaddress,omitempty"`
  480. Ipaddressid string `json:"ipaddressid,omitempty"`
  481. Networkid string `json:"networkid,omitempty"`
  482. Privateendport string `json:"privateendport,omitempty"`
  483. Privateport string `json:"privateport,omitempty"`
  484. Protocol string `json:"protocol,omitempty"`
  485. Publicendport string `json:"publicendport,omitempty"`
  486. Publicport string `json:"publicport,omitempty"`
  487. State string `json:"state,omitempty"`
  488. Tags []struct {
  489. Account string `json:"account,omitempty"`
  490. Customer string `json:"customer,omitempty"`
  491. Domain string `json:"domain,omitempty"`
  492. Domainid string `json:"domainid,omitempty"`
  493. Key string `json:"key,omitempty"`
  494. Project string `json:"project,omitempty"`
  495. Projectid string `json:"projectid,omitempty"`
  496. Resourceid string `json:"resourceid,omitempty"`
  497. Resourcetype string `json:"resourcetype,omitempty"`
  498. Value string `json:"value,omitempty"`
  499. } `json:"tags,omitempty"`
  500. Virtualmachinedisplayname string `json:"virtualmachinedisplayname,omitempty"`
  501. Virtualmachineid string `json:"virtualmachineid,omitempty"`
  502. Virtualmachinename string `json:"virtualmachinename,omitempty"`
  503. Vmguestip string `json:"vmguestip,omitempty"`
  504. }
  505. type DisableStaticNatParams struct {
  506. p map[string]interface{}
  507. }
  508. func (p *DisableStaticNatParams) toURLValues() url.Values {
  509. u := url.Values{}
  510. if p.p == nil {
  511. return u
  512. }
  513. if v, found := p.p["ipaddressid"]; found {
  514. u.Set("ipaddressid", v.(string))
  515. }
  516. return u
  517. }
  518. func (p *DisableStaticNatParams) SetIpaddressid(v string) {
  519. if p.p == nil {
  520. p.p = make(map[string]interface{})
  521. }
  522. p.p["ipaddressid"] = v
  523. return
  524. }
  525. // You should always use this function to get a new DisableStaticNatParams instance,
  526. // as then you are sure you have configured all required params
  527. func (s *NATService) NewDisableStaticNatParams(ipaddressid string) *DisableStaticNatParams {
  528. p := &DisableStaticNatParams{}
  529. p.p = make(map[string]interface{})
  530. p.p["ipaddressid"] = ipaddressid
  531. return p
  532. }
  533. // Disables static rule for given IP address
  534. func (s *NATService) DisableStaticNat(p *DisableStaticNatParams) (*DisableStaticNatResponse, error) {
  535. resp, err := s.cs.newRequest("disableStaticNat", p.toURLValues())
  536. if err != nil {
  537. return nil, err
  538. }
  539. var r DisableStaticNatResponse
  540. if err := json.Unmarshal(resp, &r); err != nil {
  541. return nil, err
  542. }
  543. // If we have a async client, we need to wait for the async result
  544. if s.cs.async {
  545. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  546. if err != nil {
  547. if err == AsyncTimeoutErr {
  548. return &r, err
  549. }
  550. return nil, err
  551. }
  552. if err := json.Unmarshal(b, &r); err != nil {
  553. return nil, err
  554. }
  555. }
  556. return &r, nil
  557. }
  558. type DisableStaticNatResponse struct {
  559. JobID string `json:"jobid,omitempty"`
  560. Displaytext string `json:"displaytext,omitempty"`
  561. Success bool `json:"success,omitempty"`
  562. }