yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ecloud/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 ecloud
    16  
    17  import (
    18  	"yunion.io/x/jsonutils"
    19  	"yunion.io/x/pkg/errors"
    20  
    21  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    22  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    23  	"yunion.io/x/cloudmux/pkg/multicloud"
    24  )
    25  
    26  type SVpc struct {
    27  	multicloud.SVpc
    28  	EcloudTags
    29  
    30  	region *SRegion
    31  	iwires []cloudprovider.ICloudWire
    32  
    33  	// wires
    34  	// secgroups
    35  
    36  	Id       string
    37  	Name     string
    38  	Region   string
    39  	EcStatus string
    40  	RouterId string
    41  	Scale    string
    42  	UserId   string
    43  	UserName string
    44  }
    45  
    46  func (v *SVpc) GetId() string {
    47  	return v.Id
    48  }
    49  
    50  func (v *SVpc) GetName() string {
    51  	return v.Name
    52  }
    53  
    54  func (v *SVpc) GetGlobalId() string {
    55  	return v.GetId()
    56  }
    57  
    58  func (v *SVpc) GetStatus() string {
    59  	switch v.EcStatus {
    60  	case "ACTIVE":
    61  		return api.VPC_STATUS_AVAILABLE
    62  	case "DOWN", "BUILD", "ERROR":
    63  		return api.VPC_STATUS_UNAVAILABLE
    64  	case "PENDING_DELETE":
    65  		return api.VPC_STATUS_DELETING
    66  	case "PENDING_CREATE", "PENDING_UPDATE":
    67  		return api.VPC_STATUS_PENDING
    68  	default:
    69  		return api.VPC_STATUS_UNKNOWN
    70  	}
    71  }
    72  
    73  func (v *SVpc) Refresh() error {
    74  	n, err := v.region.getVpcById(v.Id)
    75  	if err != nil {
    76  		return err
    77  	}
    78  	return jsonutils.Update(v, n)
    79  	// TODO? v.fetchWires()
    80  }
    81  
    82  func (v *SVpc) IsEmulated() bool {
    83  	return false
    84  }
    85  
    86  func (v *SVpc) GetRegion() cloudprovider.ICloudRegion {
    87  	return v.region
    88  }
    89  
    90  func (v *SVpc) GetIsDefault() bool {
    91  	return false
    92  }
    93  
    94  func (v *SVpc) GetCidrBlock() string {
    95  	return ""
    96  }
    97  
    98  func (v *SVpc) GetIWires() ([]cloudprovider.ICloudWire, error) {
    99  	if v.iwires == nil {
   100  		err := v.fetchWires()
   101  		if err != nil {
   102  			return nil, err
   103  		}
   104  	}
   105  	return v.iwires, nil
   106  }
   107  
   108  func (v *SVpc) fetchWires() error {
   109  	networks, err := v.region.GetNetworks(v.RouterId, "")
   110  	if err != nil {
   111  		return err
   112  	}
   113  	izones, err := v.region.GetIZones()
   114  	if err != nil {
   115  		return errors.Wrap(err, "unable to GetZones")
   116  	}
   117  	findZone := func(zoneRegion string) *SZone {
   118  		for i := range izones {
   119  			zone := izones[i].(*SZone)
   120  			if zone.Region == zoneRegion {
   121  				return zone
   122  			}
   123  		}
   124  		return nil
   125  	}
   126  	zoneRegion2Wire := map[string]*SWire{}
   127  	for i := range networks {
   128  		zoneRegion := networks[i].Region
   129  		zone := findZone(zoneRegion)
   130  		var (
   131  			wire *SWire
   132  			ok   bool
   133  		)
   134  		if wire, ok = zoneRegion2Wire[zoneRegion]; !ok {
   135  			wire = &SWire{
   136  				vpc:  v,
   137  				zone: zone,
   138  			}
   139  			zoneRegion2Wire[zoneRegion] = wire
   140  		}
   141  		wire.inetworks = append(wire.inetworks, &networks[i])
   142  	}
   143  	iwires := make([]cloudprovider.ICloudWire, 0, len(zoneRegion2Wire))
   144  	for _, wire := range zoneRegion2Wire {
   145  		iwires = append(iwires, wire)
   146  	}
   147  	v.iwires = iwires
   148  	return nil
   149  }
   150  
   151  func (v *SVpc) GetISecurityGroups() ([]cloudprovider.ICloudSecurityGroup, error) {
   152  	return nil, nil
   153  }
   154  
   155  func (v *SVpc) GetIRouteTables() ([]cloudprovider.ICloudRouteTable, error) {
   156  	return nil, cloudprovider.ErrNotImplemented
   157  }
   158  
   159  func (v *SVpc) GetIRouteTableById(routeTableId string) (cloudprovider.ICloudRouteTable, error) {
   160  	return nil, nil
   161  }
   162  
   163  func (v *SVpc) Delete() error {
   164  	return cloudprovider.ErrNotImplemented
   165  }
   166  
   167  func (v *SVpc) GetIWireById(wireId string) (cloudprovider.ICloudWire, error) {
   168  	iwires, err := v.GetIWires()
   169  	if err != nil {
   170  		return nil, err
   171  	}
   172  	for i := range iwires {
   173  		if iwires[i].GetGlobalId() == wireId {
   174  			return iwires[i], nil
   175  		}
   176  	}
   177  	return nil, cloudprovider.ErrNotFound
   178  }