SecurityGroupService.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  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 CreateSecurityGroupParams struct {
  25. p map[string]interface{}
  26. }
  27. func (p *CreateSecurityGroupParams) toURLValues() url.Values {
  28. u := url.Values{}
  29. if p.p == nil {
  30. return u
  31. }
  32. if v, found := p.p["account"]; found {
  33. u.Set("account", v.(string))
  34. }
  35. if v, found := p.p["description"]; found {
  36. u.Set("description", v.(string))
  37. }
  38. if v, found := p.p["domainid"]; found {
  39. u.Set("domainid", v.(string))
  40. }
  41. if v, found := p.p["name"]; found {
  42. u.Set("name", v.(string))
  43. }
  44. if v, found := p.p["projectid"]; found {
  45. u.Set("projectid", v.(string))
  46. }
  47. return u
  48. }
  49. func (p *CreateSecurityGroupParams) SetAccount(v string) {
  50. if p.p == nil {
  51. p.p = make(map[string]interface{})
  52. }
  53. p.p["account"] = v
  54. return
  55. }
  56. func (p *CreateSecurityGroupParams) SetDescription(v string) {
  57. if p.p == nil {
  58. p.p = make(map[string]interface{})
  59. }
  60. p.p["description"] = v
  61. return
  62. }
  63. func (p *CreateSecurityGroupParams) SetDomainid(v string) {
  64. if p.p == nil {
  65. p.p = make(map[string]interface{})
  66. }
  67. p.p["domainid"] = v
  68. return
  69. }
  70. func (p *CreateSecurityGroupParams) SetName(v string) {
  71. if p.p == nil {
  72. p.p = make(map[string]interface{})
  73. }
  74. p.p["name"] = v
  75. return
  76. }
  77. func (p *CreateSecurityGroupParams) SetProjectid(v string) {
  78. if p.p == nil {
  79. p.p = make(map[string]interface{})
  80. }
  81. p.p["projectid"] = v
  82. return
  83. }
  84. // You should always use this function to get a new CreateSecurityGroupParams instance,
  85. // as then you are sure you have configured all required params
  86. func (s *SecurityGroupService) NewCreateSecurityGroupParams(name string) *CreateSecurityGroupParams {
  87. p := &CreateSecurityGroupParams{}
  88. p.p = make(map[string]interface{})
  89. p.p["name"] = name
  90. return p
  91. }
  92. // Creates a security group
  93. func (s *SecurityGroupService) CreateSecurityGroup(p *CreateSecurityGroupParams) (*CreateSecurityGroupResponse, error) {
  94. resp, err := s.cs.newRequest("createSecurityGroup", p.toURLValues())
  95. if err != nil {
  96. return nil, err
  97. }
  98. var r CreateSecurityGroupResponse
  99. if err := json.Unmarshal(resp, &r); err != nil {
  100. return nil, err
  101. }
  102. return &r, nil
  103. }
  104. type CreateSecurityGroupResponse struct {
  105. Account string `json:"account,omitempty"`
  106. Description string `json:"description,omitempty"`
  107. Domain string `json:"domain,omitempty"`
  108. Domainid string `json:"domainid,omitempty"`
  109. Egressrule []struct {
  110. Account string `json:"account,omitempty"`
  111. Cidr string `json:"cidr,omitempty"`
  112. Endport int `json:"endport,omitempty"`
  113. Icmpcode int `json:"icmpcode,omitempty"`
  114. Icmptype int `json:"icmptype,omitempty"`
  115. Protocol string `json:"protocol,omitempty"`
  116. Ruleid string `json:"ruleid,omitempty"`
  117. Securitygroupname string `json:"securitygroupname,omitempty"`
  118. Startport int `json:"startport,omitempty"`
  119. Tags []struct {
  120. Account string `json:"account,omitempty"`
  121. Customer string `json:"customer,omitempty"`
  122. Domain string `json:"domain,omitempty"`
  123. Domainid string `json:"domainid,omitempty"`
  124. Key string `json:"key,omitempty"`
  125. Project string `json:"project,omitempty"`
  126. Projectid string `json:"projectid,omitempty"`
  127. Resourceid string `json:"resourceid,omitempty"`
  128. Resourcetype string `json:"resourcetype,omitempty"`
  129. Value string `json:"value,omitempty"`
  130. } `json:"tags,omitempty"`
  131. } `json:"egressrule,omitempty"`
  132. Id string `json:"id,omitempty"`
  133. Ingressrule []struct {
  134. Account string `json:"account,omitempty"`
  135. Cidr string `json:"cidr,omitempty"`
  136. Endport int `json:"endport,omitempty"`
  137. Icmpcode int `json:"icmpcode,omitempty"`
  138. Icmptype int `json:"icmptype,omitempty"`
  139. Protocol string `json:"protocol,omitempty"`
  140. Ruleid string `json:"ruleid,omitempty"`
  141. Securitygroupname string `json:"securitygroupname,omitempty"`
  142. Startport int `json:"startport,omitempty"`
  143. Tags []struct {
  144. Account string `json:"account,omitempty"`
  145. Customer string `json:"customer,omitempty"`
  146. Domain string `json:"domain,omitempty"`
  147. Domainid string `json:"domainid,omitempty"`
  148. Key string `json:"key,omitempty"`
  149. Project string `json:"project,omitempty"`
  150. Projectid string `json:"projectid,omitempty"`
  151. Resourceid string `json:"resourceid,omitempty"`
  152. Resourcetype string `json:"resourcetype,omitempty"`
  153. Value string `json:"value,omitempty"`
  154. } `json:"tags,omitempty"`
  155. } `json:"ingressrule,omitempty"`
  156. Name string `json:"name,omitempty"`
  157. Project string `json:"project,omitempty"`
  158. Projectid string `json:"projectid,omitempty"`
  159. Tags []struct {
  160. Account string `json:"account,omitempty"`
  161. Customer string `json:"customer,omitempty"`
  162. Domain string `json:"domain,omitempty"`
  163. Domainid string `json:"domainid,omitempty"`
  164. Key string `json:"key,omitempty"`
  165. Project string `json:"project,omitempty"`
  166. Projectid string `json:"projectid,omitempty"`
  167. Resourceid string `json:"resourceid,omitempty"`
  168. Resourcetype string `json:"resourcetype,omitempty"`
  169. Value string `json:"value,omitempty"`
  170. } `json:"tags,omitempty"`
  171. Virtualmachinecount int `json:"virtualmachinecount,omitempty"`
  172. Virtualmachineids []string `json:"virtualmachineids,omitempty"`
  173. }
  174. type DeleteSecurityGroupParams struct {
  175. p map[string]interface{}
  176. }
  177. func (p *DeleteSecurityGroupParams) toURLValues() url.Values {
  178. u := url.Values{}
  179. if p.p == nil {
  180. return u
  181. }
  182. if v, found := p.p["account"]; found {
  183. u.Set("account", v.(string))
  184. }
  185. if v, found := p.p["domainid"]; found {
  186. u.Set("domainid", v.(string))
  187. }
  188. if v, found := p.p["id"]; found {
  189. u.Set("id", v.(string))
  190. }
  191. if v, found := p.p["name"]; found {
  192. u.Set("name", v.(string))
  193. }
  194. if v, found := p.p["projectid"]; found {
  195. u.Set("projectid", v.(string))
  196. }
  197. return u
  198. }
  199. func (p *DeleteSecurityGroupParams) SetAccount(v string) {
  200. if p.p == nil {
  201. p.p = make(map[string]interface{})
  202. }
  203. p.p["account"] = v
  204. return
  205. }
  206. func (p *DeleteSecurityGroupParams) SetDomainid(v string) {
  207. if p.p == nil {
  208. p.p = make(map[string]interface{})
  209. }
  210. p.p["domainid"] = v
  211. return
  212. }
  213. func (p *DeleteSecurityGroupParams) SetId(v string) {
  214. if p.p == nil {
  215. p.p = make(map[string]interface{})
  216. }
  217. p.p["id"] = v
  218. return
  219. }
  220. func (p *DeleteSecurityGroupParams) SetName(v string) {
  221. if p.p == nil {
  222. p.p = make(map[string]interface{})
  223. }
  224. p.p["name"] = v
  225. return
  226. }
  227. func (p *DeleteSecurityGroupParams) SetProjectid(v string) {
  228. if p.p == nil {
  229. p.p = make(map[string]interface{})
  230. }
  231. p.p["projectid"] = v
  232. return
  233. }
  234. // You should always use this function to get a new DeleteSecurityGroupParams instance,
  235. // as then you are sure you have configured all required params
  236. func (s *SecurityGroupService) NewDeleteSecurityGroupParams() *DeleteSecurityGroupParams {
  237. p := &DeleteSecurityGroupParams{}
  238. p.p = make(map[string]interface{})
  239. return p
  240. }
  241. // Deletes security group
  242. func (s *SecurityGroupService) DeleteSecurityGroup(p *DeleteSecurityGroupParams) (*DeleteSecurityGroupResponse, error) {
  243. resp, err := s.cs.newRequest("deleteSecurityGroup", p.toURLValues())
  244. if err != nil {
  245. return nil, err
  246. }
  247. var r DeleteSecurityGroupResponse
  248. if err := json.Unmarshal(resp, &r); err != nil {
  249. return nil, err
  250. }
  251. return &r, nil
  252. }
  253. type DeleteSecurityGroupResponse struct {
  254. Displaytext string `json:"displaytext,omitempty"`
  255. Success string `json:"success,omitempty"`
  256. }
  257. type AuthorizeSecurityGroupIngressParams struct {
  258. p map[string]interface{}
  259. }
  260. func (p *AuthorizeSecurityGroupIngressParams) toURLValues() url.Values {
  261. u := url.Values{}
  262. if p.p == nil {
  263. return u
  264. }
  265. if v, found := p.p["account"]; found {
  266. u.Set("account", v.(string))
  267. }
  268. if v, found := p.p["cidrlist"]; found {
  269. vv := strings.Join(v.([]string), ",")
  270. u.Set("cidrlist", vv)
  271. }
  272. if v, found := p.p["domainid"]; found {
  273. u.Set("domainid", v.(string))
  274. }
  275. if v, found := p.p["endport"]; found {
  276. vv := strconv.Itoa(v.(int))
  277. u.Set("endport", vv)
  278. }
  279. if v, found := p.p["icmpcode"]; found {
  280. vv := strconv.Itoa(v.(int))
  281. u.Set("icmpcode", vv)
  282. }
  283. if v, found := p.p["icmptype"]; found {
  284. vv := strconv.Itoa(v.(int))
  285. u.Set("icmptype", vv)
  286. }
  287. if v, found := p.p["projectid"]; found {
  288. u.Set("projectid", v.(string))
  289. }
  290. if v, found := p.p["protocol"]; found {
  291. u.Set("protocol", v.(string))
  292. }
  293. if v, found := p.p["securitygroupid"]; found {
  294. u.Set("securitygroupid", v.(string))
  295. }
  296. if v, found := p.p["securitygroupname"]; found {
  297. u.Set("securitygroupname", v.(string))
  298. }
  299. if v, found := p.p["startport"]; found {
  300. vv := strconv.Itoa(v.(int))
  301. u.Set("startport", vv)
  302. }
  303. if v, found := p.p["usersecuritygrouplist"]; found {
  304. i := 0
  305. for k, vv := range v.(map[string]string) {
  306. u.Set(fmt.Sprintf("usersecuritygrouplist[%d].key", i), k)
  307. u.Set(fmt.Sprintf("usersecuritygrouplist[%d].value", i), vv)
  308. i++
  309. }
  310. }
  311. return u
  312. }
  313. func (p *AuthorizeSecurityGroupIngressParams) SetAccount(v string) {
  314. if p.p == nil {
  315. p.p = make(map[string]interface{})
  316. }
  317. p.p["account"] = v
  318. return
  319. }
  320. func (p *AuthorizeSecurityGroupIngressParams) SetCidrlist(v []string) {
  321. if p.p == nil {
  322. p.p = make(map[string]interface{})
  323. }
  324. p.p["cidrlist"] = v
  325. return
  326. }
  327. func (p *AuthorizeSecurityGroupIngressParams) SetDomainid(v string) {
  328. if p.p == nil {
  329. p.p = make(map[string]interface{})
  330. }
  331. p.p["domainid"] = v
  332. return
  333. }
  334. func (p *AuthorizeSecurityGroupIngressParams) SetEndport(v int) {
  335. if p.p == nil {
  336. p.p = make(map[string]interface{})
  337. }
  338. p.p["endport"] = v
  339. return
  340. }
  341. func (p *AuthorizeSecurityGroupIngressParams) SetIcmpcode(v int) {
  342. if p.p == nil {
  343. p.p = make(map[string]interface{})
  344. }
  345. p.p["icmpcode"] = v
  346. return
  347. }
  348. func (p *AuthorizeSecurityGroupIngressParams) SetIcmptype(v int) {
  349. if p.p == nil {
  350. p.p = make(map[string]interface{})
  351. }
  352. p.p["icmptype"] = v
  353. return
  354. }
  355. func (p *AuthorizeSecurityGroupIngressParams) SetProjectid(v string) {
  356. if p.p == nil {
  357. p.p = make(map[string]interface{})
  358. }
  359. p.p["projectid"] = v
  360. return
  361. }
  362. func (p *AuthorizeSecurityGroupIngressParams) SetProtocol(v string) {
  363. if p.p == nil {
  364. p.p = make(map[string]interface{})
  365. }
  366. p.p["protocol"] = v
  367. return
  368. }
  369. func (p *AuthorizeSecurityGroupIngressParams) SetSecuritygroupid(v string) {
  370. if p.p == nil {
  371. p.p = make(map[string]interface{})
  372. }
  373. p.p["securitygroupid"] = v
  374. return
  375. }
  376. func (p *AuthorizeSecurityGroupIngressParams) SetSecuritygroupname(v string) {
  377. if p.p == nil {
  378. p.p = make(map[string]interface{})
  379. }
  380. p.p["securitygroupname"] = v
  381. return
  382. }
  383. func (p *AuthorizeSecurityGroupIngressParams) SetStartport(v int) {
  384. if p.p == nil {
  385. p.p = make(map[string]interface{})
  386. }
  387. p.p["startport"] = v
  388. return
  389. }
  390. func (p *AuthorizeSecurityGroupIngressParams) SetUsersecuritygrouplist(v map[string]string) {
  391. if p.p == nil {
  392. p.p = make(map[string]interface{})
  393. }
  394. p.p["usersecuritygrouplist"] = v
  395. return
  396. }
  397. // You should always use this function to get a new AuthorizeSecurityGroupIngressParams instance,
  398. // as then you are sure you have configured all required params
  399. func (s *SecurityGroupService) NewAuthorizeSecurityGroupIngressParams() *AuthorizeSecurityGroupIngressParams {
  400. p := &AuthorizeSecurityGroupIngressParams{}
  401. p.p = make(map[string]interface{})
  402. return p
  403. }
  404. // Authorizes a particular ingress rule for this security group
  405. func (s *SecurityGroupService) AuthorizeSecurityGroupIngress(p *AuthorizeSecurityGroupIngressParams) (*AuthorizeSecurityGroupIngressResponse, error) {
  406. resp, err := s.cs.newRequest("authorizeSecurityGroupIngress", p.toURLValues())
  407. if err != nil {
  408. return nil, err
  409. }
  410. var r AuthorizeSecurityGroupIngressResponse
  411. if err := json.Unmarshal(resp, &r); err != nil {
  412. return nil, err
  413. }
  414. // If we have a async client, we need to wait for the async result
  415. if s.cs.async {
  416. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  417. if err != nil {
  418. if err == AsyncTimeoutErr {
  419. return &r, err
  420. }
  421. return nil, err
  422. }
  423. b, err = getRawValue(b)
  424. if err != nil {
  425. return nil, err
  426. }
  427. if err := json.Unmarshal(b, &r); err != nil {
  428. return nil, err
  429. }
  430. }
  431. return &r, nil
  432. }
  433. type AuthorizeSecurityGroupIngressResponse struct {
  434. JobID string `json:"jobid,omitempty"`
  435. Account string `json:"account,omitempty"`
  436. Cidr string `json:"cidr,omitempty"`
  437. Endport int `json:"endport,omitempty"`
  438. Icmpcode int `json:"icmpcode,omitempty"`
  439. Icmptype int `json:"icmptype,omitempty"`
  440. Protocol string `json:"protocol,omitempty"`
  441. Ruleid string `json:"ruleid,omitempty"`
  442. Securitygroupname string `json:"securitygroupname,omitempty"`
  443. Startport int `json:"startport,omitempty"`
  444. Tags []struct {
  445. Account string `json:"account,omitempty"`
  446. Customer string `json:"customer,omitempty"`
  447. Domain string `json:"domain,omitempty"`
  448. Domainid string `json:"domainid,omitempty"`
  449. Key string `json:"key,omitempty"`
  450. Project string `json:"project,omitempty"`
  451. Projectid string `json:"projectid,omitempty"`
  452. Resourceid string `json:"resourceid,omitempty"`
  453. Resourcetype string `json:"resourcetype,omitempty"`
  454. Value string `json:"value,omitempty"`
  455. } `json:"tags,omitempty"`
  456. }
  457. type RevokeSecurityGroupIngressParams struct {
  458. p map[string]interface{}
  459. }
  460. func (p *RevokeSecurityGroupIngressParams) toURLValues() url.Values {
  461. u := url.Values{}
  462. if p.p == nil {
  463. return u
  464. }
  465. if v, found := p.p["id"]; found {
  466. u.Set("id", v.(string))
  467. }
  468. return u
  469. }
  470. func (p *RevokeSecurityGroupIngressParams) SetId(v string) {
  471. if p.p == nil {
  472. p.p = make(map[string]interface{})
  473. }
  474. p.p["id"] = v
  475. return
  476. }
  477. // You should always use this function to get a new RevokeSecurityGroupIngressParams instance,
  478. // as then you are sure you have configured all required params
  479. func (s *SecurityGroupService) NewRevokeSecurityGroupIngressParams(id string) *RevokeSecurityGroupIngressParams {
  480. p := &RevokeSecurityGroupIngressParams{}
  481. p.p = make(map[string]interface{})
  482. p.p["id"] = id
  483. return p
  484. }
  485. // Deletes a particular ingress rule from this security group
  486. func (s *SecurityGroupService) RevokeSecurityGroupIngress(p *RevokeSecurityGroupIngressParams) (*RevokeSecurityGroupIngressResponse, error) {
  487. resp, err := s.cs.newRequest("revokeSecurityGroupIngress", p.toURLValues())
  488. if err != nil {
  489. return nil, err
  490. }
  491. var r RevokeSecurityGroupIngressResponse
  492. if err := json.Unmarshal(resp, &r); err != nil {
  493. return nil, err
  494. }
  495. // If we have a async client, we need to wait for the async result
  496. if s.cs.async {
  497. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  498. if err != nil {
  499. if err == AsyncTimeoutErr {
  500. return &r, err
  501. }
  502. return nil, err
  503. }
  504. if err := json.Unmarshal(b, &r); err != nil {
  505. return nil, err
  506. }
  507. }
  508. return &r, nil
  509. }
  510. type RevokeSecurityGroupIngressResponse struct {
  511. JobID string `json:"jobid,omitempty"`
  512. Displaytext string `json:"displaytext,omitempty"`
  513. Success bool `json:"success,omitempty"`
  514. }
  515. type AuthorizeSecurityGroupEgressParams struct {
  516. p map[string]interface{}
  517. }
  518. func (p *AuthorizeSecurityGroupEgressParams) toURLValues() url.Values {
  519. u := url.Values{}
  520. if p.p == nil {
  521. return u
  522. }
  523. if v, found := p.p["account"]; found {
  524. u.Set("account", v.(string))
  525. }
  526. if v, found := p.p["cidrlist"]; found {
  527. vv := strings.Join(v.([]string), ",")
  528. u.Set("cidrlist", vv)
  529. }
  530. if v, found := p.p["domainid"]; found {
  531. u.Set("domainid", v.(string))
  532. }
  533. if v, found := p.p["endport"]; found {
  534. vv := strconv.Itoa(v.(int))
  535. u.Set("endport", vv)
  536. }
  537. if v, found := p.p["icmpcode"]; found {
  538. vv := strconv.Itoa(v.(int))
  539. u.Set("icmpcode", vv)
  540. }
  541. if v, found := p.p["icmptype"]; found {
  542. vv := strconv.Itoa(v.(int))
  543. u.Set("icmptype", vv)
  544. }
  545. if v, found := p.p["projectid"]; found {
  546. u.Set("projectid", v.(string))
  547. }
  548. if v, found := p.p["protocol"]; found {
  549. u.Set("protocol", v.(string))
  550. }
  551. if v, found := p.p["securitygroupid"]; found {
  552. u.Set("securitygroupid", v.(string))
  553. }
  554. if v, found := p.p["securitygroupname"]; found {
  555. u.Set("securitygroupname", v.(string))
  556. }
  557. if v, found := p.p["startport"]; found {
  558. vv := strconv.Itoa(v.(int))
  559. u.Set("startport", vv)
  560. }
  561. if v, found := p.p["usersecuritygrouplist"]; found {
  562. i := 0
  563. for k, vv := range v.(map[string]string) {
  564. u.Set(fmt.Sprintf("usersecuritygrouplist[%d].key", i), k)
  565. u.Set(fmt.Sprintf("usersecuritygrouplist[%d].value", i), vv)
  566. i++
  567. }
  568. }
  569. return u
  570. }
  571. func (p *AuthorizeSecurityGroupEgressParams) SetAccount(v string) {
  572. if p.p == nil {
  573. p.p = make(map[string]interface{})
  574. }
  575. p.p["account"] = v
  576. return
  577. }
  578. func (p *AuthorizeSecurityGroupEgressParams) SetCidrlist(v []string) {
  579. if p.p == nil {
  580. p.p = make(map[string]interface{})
  581. }
  582. p.p["cidrlist"] = v
  583. return
  584. }
  585. func (p *AuthorizeSecurityGroupEgressParams) SetDomainid(v string) {
  586. if p.p == nil {
  587. p.p = make(map[string]interface{})
  588. }
  589. p.p["domainid"] = v
  590. return
  591. }
  592. func (p *AuthorizeSecurityGroupEgressParams) SetEndport(v int) {
  593. if p.p == nil {
  594. p.p = make(map[string]interface{})
  595. }
  596. p.p["endport"] = v
  597. return
  598. }
  599. func (p *AuthorizeSecurityGroupEgressParams) SetIcmpcode(v int) {
  600. if p.p == nil {
  601. p.p = make(map[string]interface{})
  602. }
  603. p.p["icmpcode"] = v
  604. return
  605. }
  606. func (p *AuthorizeSecurityGroupEgressParams) SetIcmptype(v int) {
  607. if p.p == nil {
  608. p.p = make(map[string]interface{})
  609. }
  610. p.p["icmptype"] = v
  611. return
  612. }
  613. func (p *AuthorizeSecurityGroupEgressParams) SetProjectid(v string) {
  614. if p.p == nil {
  615. p.p = make(map[string]interface{})
  616. }
  617. p.p["projectid"] = v
  618. return
  619. }
  620. func (p *AuthorizeSecurityGroupEgressParams) SetProtocol(v string) {
  621. if p.p == nil {
  622. p.p = make(map[string]interface{})
  623. }
  624. p.p["protocol"] = v
  625. return
  626. }
  627. func (p *AuthorizeSecurityGroupEgressParams) SetSecuritygroupid(v string) {
  628. if p.p == nil {
  629. p.p = make(map[string]interface{})
  630. }
  631. p.p["securitygroupid"] = v
  632. return
  633. }
  634. func (p *AuthorizeSecurityGroupEgressParams) SetSecuritygroupname(v string) {
  635. if p.p == nil {
  636. p.p = make(map[string]interface{})
  637. }
  638. p.p["securitygroupname"] = v
  639. return
  640. }
  641. func (p *AuthorizeSecurityGroupEgressParams) SetStartport(v int) {
  642. if p.p == nil {
  643. p.p = make(map[string]interface{})
  644. }
  645. p.p["startport"] = v
  646. return
  647. }
  648. func (p *AuthorizeSecurityGroupEgressParams) SetUsersecuritygrouplist(v map[string]string) {
  649. if p.p == nil {
  650. p.p = make(map[string]interface{})
  651. }
  652. p.p["usersecuritygrouplist"] = v
  653. return
  654. }
  655. // You should always use this function to get a new AuthorizeSecurityGroupEgressParams instance,
  656. // as then you are sure you have configured all required params
  657. func (s *SecurityGroupService) NewAuthorizeSecurityGroupEgressParams() *AuthorizeSecurityGroupEgressParams {
  658. p := &AuthorizeSecurityGroupEgressParams{}
  659. p.p = make(map[string]interface{})
  660. return p
  661. }
  662. // Authorizes a particular egress rule for this security group
  663. func (s *SecurityGroupService) AuthorizeSecurityGroupEgress(p *AuthorizeSecurityGroupEgressParams) (*AuthorizeSecurityGroupEgressResponse, error) {
  664. resp, err := s.cs.newRequest("authorizeSecurityGroupEgress", p.toURLValues())
  665. if err != nil {
  666. return nil, err
  667. }
  668. var r AuthorizeSecurityGroupEgressResponse
  669. if err := json.Unmarshal(resp, &r); err != nil {
  670. return nil, err
  671. }
  672. // If we have a async client, we need to wait for the async result
  673. if s.cs.async {
  674. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  675. if err != nil {
  676. if err == AsyncTimeoutErr {
  677. return &r, err
  678. }
  679. return nil, err
  680. }
  681. b, err = getRawValue(b)
  682. if err != nil {
  683. return nil, err
  684. }
  685. if err := json.Unmarshal(b, &r); err != nil {
  686. return nil, err
  687. }
  688. }
  689. return &r, nil
  690. }
  691. type AuthorizeSecurityGroupEgressResponse struct {
  692. JobID string `json:"jobid,omitempty"`
  693. Account string `json:"account,omitempty"`
  694. Cidr string `json:"cidr,omitempty"`
  695. Endport int `json:"endport,omitempty"`
  696. Icmpcode int `json:"icmpcode,omitempty"`
  697. Icmptype int `json:"icmptype,omitempty"`
  698. Protocol string `json:"protocol,omitempty"`
  699. Ruleid string `json:"ruleid,omitempty"`
  700. Securitygroupname string `json:"securitygroupname,omitempty"`
  701. Startport int `json:"startport,omitempty"`
  702. Tags []struct {
  703. Account string `json:"account,omitempty"`
  704. Customer string `json:"customer,omitempty"`
  705. Domain string `json:"domain,omitempty"`
  706. Domainid string `json:"domainid,omitempty"`
  707. Key string `json:"key,omitempty"`
  708. Project string `json:"project,omitempty"`
  709. Projectid string `json:"projectid,omitempty"`
  710. Resourceid string `json:"resourceid,omitempty"`
  711. Resourcetype string `json:"resourcetype,omitempty"`
  712. Value string `json:"value,omitempty"`
  713. } `json:"tags,omitempty"`
  714. }
  715. type RevokeSecurityGroupEgressParams struct {
  716. p map[string]interface{}
  717. }
  718. func (p *RevokeSecurityGroupEgressParams) toURLValues() url.Values {
  719. u := url.Values{}
  720. if p.p == nil {
  721. return u
  722. }
  723. if v, found := p.p["id"]; found {
  724. u.Set("id", v.(string))
  725. }
  726. return u
  727. }
  728. func (p *RevokeSecurityGroupEgressParams) SetId(v string) {
  729. if p.p == nil {
  730. p.p = make(map[string]interface{})
  731. }
  732. p.p["id"] = v
  733. return
  734. }
  735. // You should always use this function to get a new RevokeSecurityGroupEgressParams instance,
  736. // as then you are sure you have configured all required params
  737. func (s *SecurityGroupService) NewRevokeSecurityGroupEgressParams(id string) *RevokeSecurityGroupEgressParams {
  738. p := &RevokeSecurityGroupEgressParams{}
  739. p.p = make(map[string]interface{})
  740. p.p["id"] = id
  741. return p
  742. }
  743. // Deletes a particular egress rule from this security group
  744. func (s *SecurityGroupService) RevokeSecurityGroupEgress(p *RevokeSecurityGroupEgressParams) (*RevokeSecurityGroupEgressResponse, error) {
  745. resp, err := s.cs.newRequest("revokeSecurityGroupEgress", p.toURLValues())
  746. if err != nil {
  747. return nil, err
  748. }
  749. var r RevokeSecurityGroupEgressResponse
  750. if err := json.Unmarshal(resp, &r); err != nil {
  751. return nil, err
  752. }
  753. // If we have a async client, we need to wait for the async result
  754. if s.cs.async {
  755. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  756. if err != nil {
  757. if err == AsyncTimeoutErr {
  758. return &r, err
  759. }
  760. return nil, err
  761. }
  762. if err := json.Unmarshal(b, &r); err != nil {
  763. return nil, err
  764. }
  765. }
  766. return &r, nil
  767. }
  768. type RevokeSecurityGroupEgressResponse struct {
  769. JobID string `json:"jobid,omitempty"`
  770. Displaytext string `json:"displaytext,omitempty"`
  771. Success bool `json:"success,omitempty"`
  772. }
  773. type ListSecurityGroupsParams struct {
  774. p map[string]interface{}
  775. }
  776. func (p *ListSecurityGroupsParams) toURLValues() url.Values {
  777. u := url.Values{}
  778. if p.p == nil {
  779. return u
  780. }
  781. if v, found := p.p["account"]; found {
  782. u.Set("account", v.(string))
  783. }
  784. if v, found := p.p["domainid"]; found {
  785. u.Set("domainid", v.(string))
  786. }
  787. if v, found := p.p["id"]; found {
  788. u.Set("id", v.(string))
  789. }
  790. if v, found := p.p["isrecursive"]; found {
  791. vv := strconv.FormatBool(v.(bool))
  792. u.Set("isrecursive", vv)
  793. }
  794. if v, found := p.p["keyword"]; found {
  795. u.Set("keyword", v.(string))
  796. }
  797. if v, found := p.p["listall"]; found {
  798. vv := strconv.FormatBool(v.(bool))
  799. u.Set("listall", vv)
  800. }
  801. if v, found := p.p["page"]; found {
  802. vv := strconv.Itoa(v.(int))
  803. u.Set("page", vv)
  804. }
  805. if v, found := p.p["pagesize"]; found {
  806. vv := strconv.Itoa(v.(int))
  807. u.Set("pagesize", vv)
  808. }
  809. if v, found := p.p["projectid"]; found {
  810. u.Set("projectid", v.(string))
  811. }
  812. if v, found := p.p["securitygroupname"]; found {
  813. u.Set("securitygroupname", v.(string))
  814. }
  815. if v, found := p.p["tags"]; found {
  816. i := 0
  817. for k, vv := range v.(map[string]string) {
  818. u.Set(fmt.Sprintf("tags[%d].key", i), k)
  819. u.Set(fmt.Sprintf("tags[%d].value", i), vv)
  820. i++
  821. }
  822. }
  823. if v, found := p.p["virtualmachineid"]; found {
  824. u.Set("virtualmachineid", v.(string))
  825. }
  826. return u
  827. }
  828. func (p *ListSecurityGroupsParams) SetAccount(v string) {
  829. if p.p == nil {
  830. p.p = make(map[string]interface{})
  831. }
  832. p.p["account"] = v
  833. return
  834. }
  835. func (p *ListSecurityGroupsParams) SetDomainid(v string) {
  836. if p.p == nil {
  837. p.p = make(map[string]interface{})
  838. }
  839. p.p["domainid"] = v
  840. return
  841. }
  842. func (p *ListSecurityGroupsParams) SetId(v string) {
  843. if p.p == nil {
  844. p.p = make(map[string]interface{})
  845. }
  846. p.p["id"] = v
  847. return
  848. }
  849. func (p *ListSecurityGroupsParams) SetIsrecursive(v bool) {
  850. if p.p == nil {
  851. p.p = make(map[string]interface{})
  852. }
  853. p.p["isrecursive"] = v
  854. return
  855. }
  856. func (p *ListSecurityGroupsParams) SetKeyword(v string) {
  857. if p.p == nil {
  858. p.p = make(map[string]interface{})
  859. }
  860. p.p["keyword"] = v
  861. return
  862. }
  863. func (p *ListSecurityGroupsParams) SetListall(v bool) {
  864. if p.p == nil {
  865. p.p = make(map[string]interface{})
  866. }
  867. p.p["listall"] = v
  868. return
  869. }
  870. func (p *ListSecurityGroupsParams) SetPage(v int) {
  871. if p.p == nil {
  872. p.p = make(map[string]interface{})
  873. }
  874. p.p["page"] = v
  875. return
  876. }
  877. func (p *ListSecurityGroupsParams) SetPagesize(v int) {
  878. if p.p == nil {
  879. p.p = make(map[string]interface{})
  880. }
  881. p.p["pagesize"] = v
  882. return
  883. }
  884. func (p *ListSecurityGroupsParams) SetProjectid(v string) {
  885. if p.p == nil {
  886. p.p = make(map[string]interface{})
  887. }
  888. p.p["projectid"] = v
  889. return
  890. }
  891. func (p *ListSecurityGroupsParams) SetSecuritygroupname(v string) {
  892. if p.p == nil {
  893. p.p = make(map[string]interface{})
  894. }
  895. p.p["securitygroupname"] = v
  896. return
  897. }
  898. func (p *ListSecurityGroupsParams) SetTags(v map[string]string) {
  899. if p.p == nil {
  900. p.p = make(map[string]interface{})
  901. }
  902. p.p["tags"] = v
  903. return
  904. }
  905. func (p *ListSecurityGroupsParams) SetVirtualmachineid(v string) {
  906. if p.p == nil {
  907. p.p = make(map[string]interface{})
  908. }
  909. p.p["virtualmachineid"] = v
  910. return
  911. }
  912. // You should always use this function to get a new ListSecurityGroupsParams instance,
  913. // as then you are sure you have configured all required params
  914. func (s *SecurityGroupService) NewListSecurityGroupsParams() *ListSecurityGroupsParams {
  915. p := &ListSecurityGroupsParams{}
  916. p.p = make(map[string]interface{})
  917. return p
  918. }
  919. // This is a courtesy helper function, which in some cases may not work as expected!
  920. func (s *SecurityGroupService) GetSecurityGroupID(keyword string, opts ...OptionFunc) (string, int, error) {
  921. p := &ListSecurityGroupsParams{}
  922. p.p = make(map[string]interface{})
  923. p.p["keyword"] = keyword
  924. for _, fn := range opts {
  925. if err := fn(s.cs, p); err != nil {
  926. return "", -1, err
  927. }
  928. }
  929. l, err := s.ListSecurityGroups(p)
  930. if err != nil {
  931. return "", -1, err
  932. }
  933. if l.Count == 0 {
  934. return "", l.Count, fmt.Errorf("No match found for %s: %+v", keyword, l)
  935. }
  936. if l.Count == 1 {
  937. return l.SecurityGroups[0].Id, l.Count, nil
  938. }
  939. if l.Count > 1 {
  940. for _, v := range l.SecurityGroups {
  941. if v.Name == keyword {
  942. return v.Id, l.Count, nil
  943. }
  944. }
  945. }
  946. return "", l.Count, fmt.Errorf("Could not find an exact match for %s: %+v", keyword, l)
  947. }
  948. // This is a courtesy helper function, which in some cases may not work as expected!
  949. func (s *SecurityGroupService) GetSecurityGroupByName(name string, opts ...OptionFunc) (*SecurityGroup, int, error) {
  950. id, count, err := s.GetSecurityGroupID(name, opts...)
  951. if err != nil {
  952. return nil, count, err
  953. }
  954. r, count, err := s.GetSecurityGroupByID(id, opts...)
  955. if err != nil {
  956. return nil, count, err
  957. }
  958. return r, count, nil
  959. }
  960. // This is a courtesy helper function, which in some cases may not work as expected!
  961. func (s *SecurityGroupService) GetSecurityGroupByID(id string, opts ...OptionFunc) (*SecurityGroup, int, error) {
  962. p := &ListSecurityGroupsParams{}
  963. p.p = make(map[string]interface{})
  964. p.p["id"] = id
  965. for _, fn := range opts {
  966. if err := fn(s.cs, p); err != nil {
  967. return nil, -1, err
  968. }
  969. }
  970. l, err := s.ListSecurityGroups(p)
  971. if err != nil {
  972. if strings.Contains(err.Error(), fmt.Sprintf(
  973. "Invalid parameter id value=%s due to incorrect long value format, "+
  974. "or entity does not exist", id)) {
  975. return nil, 0, fmt.Errorf("No match found for %s: %+v", id, l)
  976. }
  977. return nil, -1, err
  978. }
  979. if l.Count == 0 {
  980. return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l)
  981. }
  982. if l.Count == 1 {
  983. return l.SecurityGroups[0], l.Count, nil
  984. }
  985. return nil, l.Count, fmt.Errorf("There is more then one result for SecurityGroup UUID: %s!", id)
  986. }
  987. // Lists security groups
  988. func (s *SecurityGroupService) ListSecurityGroups(p *ListSecurityGroupsParams) (*ListSecurityGroupsResponse, error) {
  989. resp, err := s.cs.newRequest("listSecurityGroups", p.toURLValues())
  990. if err != nil {
  991. return nil, err
  992. }
  993. var r ListSecurityGroupsResponse
  994. if err := json.Unmarshal(resp, &r); err != nil {
  995. return nil, err
  996. }
  997. return &r, nil
  998. }
  999. type ListSecurityGroupsResponse struct {
  1000. Count int `json:"count"`
  1001. SecurityGroups []*SecurityGroup `json:"securitygroup"`
  1002. }
  1003. type SecurityGroup struct {
  1004. Account string `json:"account,omitempty"`
  1005. Description string `json:"description,omitempty"`
  1006. Domain string `json:"domain,omitempty"`
  1007. Domainid string `json:"domainid,omitempty"`
  1008. Egressrule []struct {
  1009. Account string `json:"account,omitempty"`
  1010. Cidr string `json:"cidr,omitempty"`
  1011. Endport int `json:"endport,omitempty"`
  1012. Icmpcode int `json:"icmpcode,omitempty"`
  1013. Icmptype int `json:"icmptype,omitempty"`
  1014. Protocol string `json:"protocol,omitempty"`
  1015. Ruleid string `json:"ruleid,omitempty"`
  1016. Securitygroupname string `json:"securitygroupname,omitempty"`
  1017. Startport int `json:"startport,omitempty"`
  1018. Tags []struct {
  1019. Account string `json:"account,omitempty"`
  1020. Customer string `json:"customer,omitempty"`
  1021. Domain string `json:"domain,omitempty"`
  1022. Domainid string `json:"domainid,omitempty"`
  1023. Key string `json:"key,omitempty"`
  1024. Project string `json:"project,omitempty"`
  1025. Projectid string `json:"projectid,omitempty"`
  1026. Resourceid string `json:"resourceid,omitempty"`
  1027. Resourcetype string `json:"resourcetype,omitempty"`
  1028. Value string `json:"value,omitempty"`
  1029. } `json:"tags,omitempty"`
  1030. } `json:"egressrule,omitempty"`
  1031. Id string `json:"id,omitempty"`
  1032. Ingressrule []struct {
  1033. Account string `json:"account,omitempty"`
  1034. Cidr string `json:"cidr,omitempty"`
  1035. Endport int `json:"endport,omitempty"`
  1036. Icmpcode int `json:"icmpcode,omitempty"`
  1037. Icmptype int `json:"icmptype,omitempty"`
  1038. Protocol string `json:"protocol,omitempty"`
  1039. Ruleid string `json:"ruleid,omitempty"`
  1040. Securitygroupname string `json:"securitygroupname,omitempty"`
  1041. Startport int `json:"startport,omitempty"`
  1042. Tags []struct {
  1043. Account string `json:"account,omitempty"`
  1044. Customer string `json:"customer,omitempty"`
  1045. Domain string `json:"domain,omitempty"`
  1046. Domainid string `json:"domainid,omitempty"`
  1047. Key string `json:"key,omitempty"`
  1048. Project string `json:"project,omitempty"`
  1049. Projectid string `json:"projectid,omitempty"`
  1050. Resourceid string `json:"resourceid,omitempty"`
  1051. Resourcetype string `json:"resourcetype,omitempty"`
  1052. Value string `json:"value,omitempty"`
  1053. } `json:"tags,omitempty"`
  1054. } `json:"ingressrule,omitempty"`
  1055. Name string `json:"name,omitempty"`
  1056. Project string `json:"project,omitempty"`
  1057. Projectid string `json:"projectid,omitempty"`
  1058. Tags []struct {
  1059. Account string `json:"account,omitempty"`
  1060. Customer string `json:"customer,omitempty"`
  1061. Domain string `json:"domain,omitempty"`
  1062. Domainid string `json:"domainid,omitempty"`
  1063. Key string `json:"key,omitempty"`
  1064. Project string `json:"project,omitempty"`
  1065. Projectid string `json:"projectid,omitempty"`
  1066. Resourceid string `json:"resourceid,omitempty"`
  1067. Resourcetype string `json:"resourcetype,omitempty"`
  1068. Value string `json:"value,omitempty"`
  1069. } `json:"tags,omitempty"`
  1070. Virtualmachinecount int `json:"virtualmachinecount,omitempty"`
  1071. Virtualmachineids []string `json:"virtualmachineids,omitempty"`
  1072. }