vpc.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package ksyun
  15. import (
  16. "fmt"
  17. api "yunion.io/x/cloudmux/pkg/apis/compute"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/cloudmux/pkg/multicloud"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/pkg/errors"
  22. )
  23. type SVpc struct {
  24. multicloud.SVpc
  25. SKsyunTags
  26. region *SRegion
  27. IsDefault bool `json:"IsDefault"`
  28. VpcId string `json:"VpcId"`
  29. CreateTime string `json:"CreateTime"`
  30. CidrBlock string `json:"CidrBlock"`
  31. VpcName string `json:"VpcName"`
  32. ProvidedIpv6CidrBlock bool `json:"ProvidedIpv6CidrBlock"`
  33. }
  34. func (region *SRegion) GetVpcs(ids []string) ([]SVpc, error) {
  35. param := map[string]interface{}{
  36. "MaxResults": "1000",
  37. }
  38. for i, vpcId := range ids {
  39. param[fmt.Sprintf("VpcId.%d", i+1)] = vpcId
  40. }
  41. vpcs := []SVpc{}
  42. for {
  43. resp, err := region.vpcRequest("DescribeVpcs", param)
  44. if err != nil {
  45. return nil, errors.Wrap(err, "list instance")
  46. }
  47. part := []SVpc{}
  48. err = resp.Unmarshal(&part, "VpcSet")
  49. if err != nil {
  50. return nil, errors.Wrap(err, "unmarshal instances")
  51. }
  52. vpcs = append(vpcs, part...)
  53. nextToken, err := resp.GetString("NextToken")
  54. if err != nil {
  55. break
  56. }
  57. param["NextToken"] = nextToken
  58. }
  59. return vpcs, nil
  60. }
  61. func (region *SRegion) GetVpc(id string) (*SVpc, error) {
  62. vpcs, err := region.GetVpcs([]string{id})
  63. if err != nil {
  64. return nil, errors.Wrap(err, "GetVpcs")
  65. }
  66. for _, vpc := range vpcs {
  67. if vpc.GetGlobalId() == id {
  68. return &vpc, nil
  69. }
  70. }
  71. return nil, errors.Wrapf(err, "vpc id:%s", id)
  72. }
  73. func (vpc *SVpc) GetId() string {
  74. return vpc.VpcId
  75. }
  76. func (vpc *SVpc) GetName() string {
  77. if len(vpc.VpcName) > 0 {
  78. return vpc.VpcName
  79. }
  80. return vpc.VpcId
  81. }
  82. func (vpc *SVpc) GetGlobalId() string {
  83. return vpc.VpcId
  84. }
  85. func (vpc *SVpc) GetStatus() string {
  86. return api.VPC_STATUS_AVAILABLE
  87. }
  88. func (vpc *SVpc) Refresh() error {
  89. extVpc, err := vpc.region.GetVpc(vpc.GetGlobalId())
  90. if err != nil {
  91. return errors.Wrap(err, "GetVpc")
  92. }
  93. return jsonutils.Update(vpc, extVpc)
  94. }
  95. func (vpc *SVpc) GetRegion() cloudprovider.ICloudRegion {
  96. return vpc.region
  97. }
  98. func (vpc *SVpc) GetIsDefault() bool {
  99. return vpc.IsDefault
  100. }
  101. func (vpc *SVpc) GetCidrBlock() string {
  102. return vpc.CidrBlock
  103. }
  104. func (vpc *SVpc) GetIWires() ([]cloudprovider.ICloudWire, error) {
  105. zones, err := vpc.region.GetZones()
  106. if err != nil {
  107. return nil, errors.Wrap(err, "GetZones")
  108. }
  109. for i := range zones {
  110. zones[i].region = vpc.region
  111. }
  112. wires := []cloudprovider.ICloudWire{}
  113. for i := 0; i < len(zones); i++ {
  114. wire := SWire{
  115. vpc: vpc,
  116. zone: &zones[i],
  117. }
  118. wires = append(wires, &wire)
  119. }
  120. return wires, nil
  121. }
  122. func (vpc *SVpc) GetISecurityGroups() ([]cloudprovider.ICloudSecurityGroup, error) {
  123. secgroups, err := vpc.region.GetSecurityGroups(vpc.VpcId, nil)
  124. if err != nil {
  125. return nil, errors.Wrap(err, "GetSecurityGroups")
  126. }
  127. isecgroups := []cloudprovider.ICloudSecurityGroup{}
  128. for i := range secgroups {
  129. secgroups[i].region = vpc.region
  130. isecgroups = append(isecgroups, &secgroups[i])
  131. }
  132. return isecgroups, nil
  133. }
  134. func (vpc *SVpc) GetIRouteTables() ([]cloudprovider.ICloudRouteTable, error) {
  135. return nil, cloudprovider.ErrNotImplemented
  136. }
  137. func (vpc *SVpc) GetIRouteTableById(routeTableId string) (cloudprovider.ICloudRouteTable, error) {
  138. return nil, cloudprovider.ErrNotImplemented
  139. }
  140. func (vpc *SVpc) Delete() error {
  141. return vpc.region.DeleteVpc(vpc.VpcId)
  142. }
  143. func (vpc *SVpc) GetTags() (map[string]string, error) {
  144. tags, err := vpc.region.ListTags("vpc", vpc.VpcId)
  145. if err != nil {
  146. return nil, err
  147. }
  148. return tags.GetTags(), nil
  149. }
  150. func (vpc *SVpc) GetIWireById(wireId string) (cloudprovider.ICloudWire, error) {
  151. wires, err := vpc.GetIWires()
  152. if err != nil {
  153. return nil, errors.Wrap(err, "vpc.GetIWires")
  154. }
  155. for _, wire := range wires {
  156. if wire.GetGlobalId() == wireId {
  157. return wire, nil
  158. }
  159. }
  160. return nil, errors.Wrapf(errors.ErrNotFound, "wire id:%s", wireId)
  161. }
  162. func (vpc *SRegion) DeleteVpc(vpcId string) error {
  163. params := map[string]interface{}{
  164. "VpcId": vpcId,
  165. }
  166. _, err := vpc.vpcRequest("DeleteVpc", params)
  167. return err
  168. }
  169. func (region *SRegion) CreateVpc(opts *cloudprovider.VpcCreateOptions) (*SVpc, error) {
  170. params := map[string]interface{}{
  171. "VpcName": opts.NAME,
  172. "CidrBlock": opts.CIDR,
  173. }
  174. body, err := region.vpcRequest("CreateVpc", params)
  175. if err != nil {
  176. return nil, err
  177. }
  178. ret := &SVpc{region: region}
  179. err = body.Unmarshal(ret, "Vpc")
  180. if err != nil {
  181. return nil, errors.Wrap(err, "Unmarshal")
  182. }
  183. return ret, nil
  184. }