vpc.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 qcloud
  15. import (
  16. "time"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. api "yunion.io/x/cloudmux/pkg/apis/compute"
  20. "yunion.io/x/cloudmux/pkg/cloudprovider"
  21. "yunion.io/x/cloudmux/pkg/multicloud"
  22. )
  23. type SVpc struct {
  24. multicloud.SVpc
  25. QcloudTags
  26. region *SRegion
  27. CidrBlock string
  28. Ipv6CidrBlock string
  29. CreatedTime time.Time
  30. DhcpOptionsId string
  31. DnsServerSet []string
  32. DomainName string
  33. EnableMulticast bool
  34. IsDefault bool
  35. VpcId string
  36. VpcName string
  37. }
  38. func (self *SVpc) GetId() string {
  39. return self.VpcId
  40. }
  41. func (self *SVpc) GetName() string {
  42. if len(self.VpcName) > 0 {
  43. return self.VpcName
  44. }
  45. return self.VpcId
  46. }
  47. func (self *SVpc) GetGlobalId() string {
  48. return self.VpcId
  49. }
  50. func (self *SVpc) GetIsDefault() bool {
  51. return self.IsDefault
  52. }
  53. func (self *SVpc) GetCidrBlock() string {
  54. return self.CidrBlock
  55. }
  56. func (self *SVpc) GetCidrBlock6() string {
  57. return self.Ipv6CidrBlock
  58. }
  59. func (self *SVpc) GetStatus() string {
  60. return api.VPC_STATUS_AVAILABLE
  61. }
  62. func (self *SVpc) Delete() error {
  63. return self.region.DeleteVpc(self.VpcId)
  64. }
  65. func (self *SVpc) SetTags(tags map[string]string, replace bool) error {
  66. return self.region.SetResourceTags("vpc", "vpc", []string{self.VpcId}, tags, replace)
  67. }
  68. func (self *SVpc) GetISecurityGroups() ([]cloudprovider.ICloudSecurityGroup, error) {
  69. return []cloudprovider.ICloudSecurityGroup{}, nil
  70. }
  71. func (self *SVpc) GetIRouteTables() ([]cloudprovider.ICloudRouteTable, error) {
  72. rts := []cloudprovider.ICloudRouteTable{}
  73. routetables, err := self.region.GetAllRouteTables(self.GetId(), []string{})
  74. if err != nil {
  75. return nil, errors.Wrapf(err, "self.region.GetAllRouteTables(%s, []string{})", self.GetId())
  76. }
  77. for i := range routetables {
  78. routetables[i].vpc = self
  79. rts = append(rts, &routetables[i])
  80. }
  81. return rts, nil
  82. }
  83. func (self *SVpc) GetIRouteTableById(routeTableId string) (cloudprovider.ICloudRouteTable, error) {
  84. routetables, err := self.region.GetAllRouteTables(self.GetId(), []string{routeTableId})
  85. if err != nil {
  86. return nil, errors.Wrapf(err, "self.region.GetAllRouteTables(%s, []string{})", self.GetId())
  87. }
  88. if len(routetables) == 0 {
  89. return nil, cloudprovider.ErrNotFound
  90. }
  91. if len(routetables) > 1 {
  92. return nil, cloudprovider.ErrDuplicateId
  93. }
  94. routetables[0].vpc = self
  95. return &routetables[0], nil
  96. }
  97. func (self *SVpc) GetINatGateways() ([]cloudprovider.ICloudNatGateway, error) {
  98. nats := []SNatGateway{}
  99. for {
  100. part, total, err := self.region.GetNatGateways(self.VpcId, len(nats), 50)
  101. if err != nil {
  102. return nil, err
  103. }
  104. nats = append(nats, part...)
  105. if len(nats) >= total {
  106. break
  107. }
  108. }
  109. inats := []cloudprovider.ICloudNatGateway{}
  110. for i := 0; i < len(nats); i++ {
  111. nats[i].vpc = self
  112. inats = append(inats, &nats[i])
  113. }
  114. return inats, nil
  115. }
  116. func (self *SVpc) GetIWireById(wireId string) (cloudprovider.ICloudWire, error) {
  117. wires, err := self.GetIWires()
  118. if err != nil {
  119. return nil, err
  120. }
  121. for i := 0; i < len(wires); i += 1 {
  122. if wires[i].GetGlobalId() == wireId {
  123. return wires[i], nil
  124. }
  125. }
  126. return nil, cloudprovider.ErrNotFound
  127. }
  128. func (self *SVpc) GetIWires() ([]cloudprovider.ICloudWire, error) {
  129. zones, err := self.region.GetZones()
  130. if err != nil {
  131. return nil, err
  132. }
  133. ret := []cloudprovider.ICloudWire{}
  134. for i := range zones {
  135. wire := &SWire{zone: &zones[i], vpc: self}
  136. ret = append(ret, wire)
  137. }
  138. return ret, nil
  139. }
  140. func (self *SVpc) GetRegion() cloudprovider.ICloudRegion {
  141. return self.region
  142. }
  143. func (self *SVpc) Refresh() error {
  144. vpc, err := self.region.GetVpc(self.VpcId)
  145. if err != nil {
  146. return err
  147. }
  148. return jsonutils.Update(self, vpc)
  149. }
  150. func (self *SVpc) GetAuthorityOwnerId() string {
  151. return self.region.client.ownerName
  152. }
  153. func (self *SVpc) ProposeJoinICloudInterVpcNetwork(opts *cloudprovider.SVpcJointInterVpcNetworkOption) error {
  154. instance := SCcnAttachInstanceInput{
  155. InstanceType: "VPC",
  156. InstanceId: self.GetId(),
  157. InstanceRegion: self.region.GetId(),
  158. }
  159. err := self.region.AttachCcnInstances(opts.InterVpcNetworkId, opts.NetworkAuthorityOwnerId, []SCcnAttachInstanceInput{instance})
  160. if err != nil {
  161. return errors.Wrapf(err, "self.region.AttachCcnInstance(%s,%s,%s)", jsonutils.Marshal(opts).String(), self.GetId(), self.region.GetId())
  162. }
  163. return nil
  164. }