vpc.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 cloudpods
  15. import (
  16. "yunion.io/x/cloudmux/pkg/cloudprovider"
  17. "yunion.io/x/cloudmux/pkg/multicloud"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  22. )
  23. type SVpc struct {
  24. multicloud.SVpc
  25. CloudpodsTags
  26. region *SRegion
  27. api.VpcDetails
  28. }
  29. func (self *SVpc) GetName() string {
  30. return self.Name
  31. }
  32. func (self *SVpc) GetId() string {
  33. return self.Id
  34. }
  35. func (self *SVpc) GetGlobalId() string {
  36. return self.Id
  37. }
  38. func (self *SVpc) GetStatus() string {
  39. return self.Status
  40. }
  41. func (self *SVpc) Refresh() error {
  42. vpc, err := self.region.GetVpc(self.Id)
  43. if err != nil {
  44. return errors.Wrapf(err, "GetVpc(%s)", self.Id)
  45. }
  46. return jsonutils.Update(self, vpc)
  47. }
  48. func (self *SVpc) GetCidrBlock() string {
  49. return self.CidrBlock
  50. }
  51. func (self *SVpc) GetCidrBlock6() string {
  52. return self.CidrBlock6
  53. }
  54. func (self *SVpc) GetIRouteTableById(id string) (cloudprovider.ICloudRouteTable, error) {
  55. return nil, cloudprovider.ErrNotImplemented
  56. }
  57. func (self *SVpc) GetIRouteTables() ([]cloudprovider.ICloudRouteTable, error) {
  58. return nil, cloudprovider.ErrNotImplemented
  59. }
  60. func (self *SVpc) GetIsDefault() bool {
  61. return self.IsDefault
  62. }
  63. func (self *SVpc) GetRegion() cloudprovider.ICloudRegion {
  64. return self.region
  65. }
  66. func (self *SVpc) GetExternalAccessMode() string {
  67. return self.ExternalAccessMode
  68. }
  69. func (self *SVpc) Delete() error {
  70. return self.region.cli.delete(&modules.Vpcs, self.Id)
  71. }
  72. func (self *SRegion) GetIVpcs() ([]cloudprovider.ICloudVpc, error) {
  73. vpcs, err := self.GetVpcs()
  74. if err != nil {
  75. return nil, errors.Wrapf(err, "GetVpcs")
  76. }
  77. ret := []cloudprovider.ICloudVpc{}
  78. for i := range vpcs {
  79. vpcs[i].region = self
  80. ret = append(ret, &vpcs[i])
  81. }
  82. return ret, nil
  83. }
  84. func (self *SVpc) CreateIWire(opts *cloudprovider.SWireCreateOptions) (cloudprovider.ICloudWire, error) {
  85. wire, err := self.region.CreateWire(opts, self.Id, self.DomainId, self.PublicScope, self.IsPublic)
  86. if err != nil {
  87. return nil, err
  88. }
  89. wire.vpc = self
  90. return wire, nil
  91. }
  92. func (self *SRegion) CreateWire(opts *cloudprovider.SWireCreateOptions, vpcId, domainId, publicScope string, isPublic bool) (*SWire, error) {
  93. input := api.WireCreateInput{}
  94. input.GenerateName = opts.Name
  95. input.Mtu = opts.Mtu
  96. input.Bandwidth = opts.Bandwidth
  97. input.VpcId = vpcId
  98. input.DomainId = domainId
  99. input.PublicScope = publicScope
  100. input.IsPublic = &isPublic
  101. input.ZoneId = opts.ZoneId
  102. t := true
  103. input.IsEmulated = &t
  104. wire := &SWire{}
  105. return wire, self.create(&modules.Wires, input, wire)
  106. }
  107. func (self *SVpc) GetISecurityGroups() ([]cloudprovider.ICloudSecurityGroup, error) {
  108. return []cloudprovider.ICloudSecurityGroup{}, nil
  109. }
  110. func (self *SRegion) CreateIVpc(opts *cloudprovider.VpcCreateOptions) (cloudprovider.ICloudVpc, error) {
  111. input := api.VpcCreateInput{}
  112. input.Name = opts.NAME
  113. input.Description = opts.Desc
  114. input.CidrBlock = opts.CIDR
  115. input.CloudregionId = self.Id
  116. vpc := &SVpc{region: self}
  117. return vpc, self.create(&modules.Vpcs, input, vpc)
  118. }
  119. func (self *SRegion) GetIVpcById(id string) (cloudprovider.ICloudVpc, error) {
  120. vpc, err := self.GetVpc(id)
  121. if err != nil {
  122. return nil, errors.Wrapf(err, "GetVpc(%s)", id)
  123. }
  124. return vpc, nil
  125. }
  126. func (self *SRegion) GetVpcs() ([]SVpc, error) {
  127. vpcs := []SVpc{}
  128. return vpcs, self.list(&modules.Vpcs, nil, &vpcs)
  129. }
  130. func (self *SRegion) GetVpc(id string) (*SVpc, error) {
  131. vpc := &SVpc{region: self}
  132. return vpc, self.cli.get(&modules.Vpcs, id, nil, vpc)
  133. }