vswitches.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package ecs
  2. import (
  3. "time"
  4. "github.com/denverdino/aliyungo/common"
  5. "github.com/denverdino/aliyungo/util"
  6. )
  7. type CreateVSwitchArgs struct {
  8. ZoneId string
  9. CidrBlock string
  10. VpcId string
  11. VSwitchName string
  12. Description string
  13. ClientToken string
  14. }
  15. type CreateVSwitchResponse struct {
  16. common.Response
  17. VSwitchId string
  18. }
  19. // CreateVSwitch creates Virtual Switch
  20. //
  21. // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/vswitch&createvswitch
  22. func (client *Client) CreateVSwitch(args *CreateVSwitchArgs) (vswitchId string, err error) {
  23. response := CreateVSwitchResponse{}
  24. err = client.Invoke("CreateVSwitch", args, &response)
  25. if err != nil {
  26. return "", err
  27. }
  28. return response.VSwitchId, err
  29. }
  30. type DeleteVSwitchArgs struct {
  31. VSwitchId string
  32. }
  33. type DeleteVSwitchResponse struct {
  34. common.Response
  35. }
  36. // DeleteVSwitch deletes Virtual Switch
  37. //
  38. // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/vswitch&deletevswitch
  39. func (client *Client) DeleteVSwitch(VSwitchId string) error {
  40. args := DeleteVSwitchArgs{
  41. VSwitchId: VSwitchId,
  42. }
  43. response := DeleteVSwitchResponse{}
  44. return client.Invoke("DeleteVSwitch", &args, &response)
  45. }
  46. type DescribeVSwitchesArgs struct {
  47. VpcId string
  48. VSwitchId string
  49. ZoneId string
  50. common.Pagination
  51. }
  52. type VSwitchStatus string
  53. const (
  54. VSwitchStatusPending = VSwitchStatus("Pending")
  55. VSwitchStatusAvailable = VSwitchStatus("Available")
  56. )
  57. //
  58. // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/datatype&vswitchsettype
  59. type VSwitchSetType struct {
  60. VSwitchId string
  61. VpcId string
  62. Status VSwitchStatus // enum Pending | Available
  63. CidrBlock string
  64. ZoneId string
  65. AvailableIpAddressCount int
  66. Description string
  67. VSwitchName string
  68. CreationTime util.ISO6801Time
  69. }
  70. type DescribeVSwitchesResponse struct {
  71. common.Response
  72. common.PaginationResult
  73. VSwitches struct {
  74. VSwitch []VSwitchSetType
  75. }
  76. }
  77. // DescribeVSwitches describes Virtual Switches
  78. //
  79. // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/vswitch&describevswitches
  80. func (client *Client) DescribeVSwitches(args *DescribeVSwitchesArgs) (vswitches []VSwitchSetType, pagination *common.PaginationResult, err error) {
  81. args.Validate()
  82. response := DescribeVSwitchesResponse{}
  83. err = client.Invoke("DescribeVSwitches", args, &response)
  84. if err == nil {
  85. return response.VSwitches.VSwitch, &response.PaginationResult, nil
  86. }
  87. return nil, nil, err
  88. }
  89. type ModifyVSwitchAttributeArgs struct {
  90. VSwitchId string
  91. VSwitchName string
  92. Description string
  93. }
  94. type ModifyVSwitchAttributeResponse struct {
  95. common.Response
  96. }
  97. // ModifyVSwitchAttribute modifies attribute of Virtual Private Cloud
  98. //
  99. // You can read doc at http://docs.aliyun.com/#/pub/ecs/open-api/vswitch&modifyvswitchattribute
  100. func (client *Client) ModifyVSwitchAttribute(args *ModifyVSwitchAttributeArgs) error {
  101. response := ModifyVSwitchAttributeResponse{}
  102. return client.Invoke("ModifyVSwitchAttribute", args, &response)
  103. }
  104. // WaitForVSwitchAvailable waits for VSwitch to given status
  105. func (client *Client) WaitForVSwitchAvailable(vpcId string, vswitchId string, timeout int) error {
  106. if timeout <= 0 {
  107. timeout = DefaultTimeout
  108. }
  109. args := DescribeVSwitchesArgs{
  110. VpcId: vpcId,
  111. VSwitchId: vswitchId,
  112. }
  113. for {
  114. vswitches, _, err := client.DescribeVSwitches(&args)
  115. if err != nil {
  116. return err
  117. }
  118. if len(vswitches) == 0 {
  119. return common.GetClientErrorFromString("Not found")
  120. }
  121. if vswitches[0].Status == VSwitchStatusAvailable {
  122. break
  123. }
  124. timeout = timeout - DefaultWaitForInterval
  125. if timeout <= 0 {
  126. return common.GetClientErrorFromString("Timeout")
  127. }
  128. time.Sleep(DefaultWaitForInterval * time.Second)
  129. }
  130. return nil
  131. }