yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/cloudpods/network.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/pkg/errors"
    19  
    20  	api "yunion.io/x/onecloud/pkg/apis/compute"
    21  	modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
    22  	"yunion.io/x/onecloud/pkg/util/rbacutils"
    23  
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  	"yunion.io/x/cloudmux/pkg/multicloud"
    26  )
    27  
    28  type SNetwork struct {
    29  	multicloud.SResourceBase
    30  	CloudpodsTags
    31  	wire *SWire
    32  
    33  	api.NetworkDetails
    34  }
    35  
    36  func (self *SNetwork) GetName() string {
    37  	return self.Name
    38  }
    39  
    40  func (self *SNetwork) GetId() string {
    41  	return self.Id
    42  }
    43  
    44  func (self *SNetwork) GetGlobalId() string {
    45  	return self.Id
    46  }
    47  
    48  func (self *SNetwork) GetStatus() string {
    49  	return self.Status
    50  }
    51  
    52  func (self *SNetwork) Delete() error {
    53  	return self.wire.vpc.region.cli.delete(&modules.Networks, self.Id)
    54  }
    55  
    56  func (self *SNetwork) GetIWire() cloudprovider.ICloudWire {
    57  	return self.wire
    58  }
    59  
    60  func (self *SNetwork) GetIpStart() string {
    61  	return self.GuestIpStart
    62  }
    63  
    64  func (self *SNetwork) GetIpEnd() string {
    65  	return self.GuestIpEnd
    66  }
    67  
    68  func (self *SNetwork) GetIpMask() int8 {
    69  	return int8(self.GuestIpMask)
    70  }
    71  
    72  func (self *SNetwork) GetGateway() string {
    73  	return self.GuestGateway
    74  }
    75  
    76  func (self *SNetwork) GetServerType() string {
    77  	return self.ServerType
    78  }
    79  
    80  func (self *SNetwork) GetPublicScope() rbacutils.TRbacScope {
    81  	return rbacutils.TRbacScope(self.PublicScope)
    82  }
    83  
    84  func (self *SNetwork) GetAllocTimeoutSeconds() int {
    85  	return self.AllocTimoutSeconds
    86  }
    87  
    88  func (self *SNetwork) GetProjectId() string {
    89  	return self.TenantId
    90  }
    91  
    92  func (self *SWire) GetINetworks() ([]cloudprovider.ICloudNetwork, error) {
    93  	networks, err := self.vpc.region.GetNetworks(self.Id)
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  	ret := []cloudprovider.ICloudNetwork{}
    98  	for i := range networks {
    99  		networks[i].wire = self
   100  		ret = append(ret, &networks[i])
   101  	}
   102  	return ret, nil
   103  }
   104  
   105  func (self *SWire) GetINetworkById(id string) (cloudprovider.ICloudNetwork, error) {
   106  	net, err := self.vpc.region.GetNetwork(id)
   107  	if err != nil {
   108  		return nil, errors.Wrapf(err, "GetNetwork(%s)", id)
   109  	}
   110  	net.wire = self
   111  	return net, nil
   112  }
   113  
   114  func (self *SWire) CreateINetwork(opts *cloudprovider.SNetworkCreateOptions) (cloudprovider.ICloudNetwork, error) {
   115  	input := api.NetworkCreateInput{}
   116  	input.Name = opts.Name
   117  	input.Description = opts.Desc
   118  	input.GuestIpPrefix = opts.Cidr
   119  	input.WireId = self.Id
   120  	input.ProjectId = opts.ProjectId
   121  	network := &SNetwork{wire: self}
   122  	return network, self.vpc.region.create(&modules.Networks, input, network)
   123  }
   124  
   125  func (self *SRegion) GetNetworks(wireId string) ([]SNetwork, error) {
   126  	networks := []SNetwork{}
   127  	params := map[string]interface{}{}
   128  	if len(wireId) > 0 {
   129  		params["wire_id"] = wireId
   130  	}
   131  	return networks, self.list(&modules.Networks, params, &networks)
   132  }
   133  
   134  func (self *SRegion) GetNetwork(id string) (*SNetwork, error) {
   135  	network := &SNetwork{}
   136  	return network, self.cli.get(&modules.Networks, id, nil, network)
   137  }