yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/cloudpods/vpc.go (about)

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