yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/jdcloud/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 jdcloud
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/jdcloud-api/jdcloud-sdk-go/services/vpc/apis"
    21  	"github.com/jdcloud-api/jdcloud-sdk-go/services/vpc/client"
    22  	"github.com/jdcloud-api/jdcloud-sdk-go/services/vpc/models"
    23  
    24  	"yunion.io/x/pkg/errors"
    25  
    26  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    27  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    28  	"yunion.io/x/cloudmux/pkg/multicloud"
    29  )
    30  
    31  type SVpc struct {
    32  	multicloud.SVpc
    33  	JdcloudTags
    34  	models.Vpc
    35  
    36  	secgroups []cloudprovider.ICloudSecurityGroup
    37  	iwires    []cloudprovider.ICloudWire
    38  
    39  	region *SRegion
    40  }
    41  
    42  func (v *SVpc) GetId() string {
    43  	return v.VpcId
    44  }
    45  
    46  func (v *SVpc) GetName() string {
    47  	return v.VpcName
    48  }
    49  
    50  func (v *SVpc) GetGlobalId() string {
    51  	return v.GetId()
    52  }
    53  
    54  func (v *SVpc) GetStatus() string {
    55  	return api.VPC_STATUS_AVAILABLE
    56  }
    57  
    58  func (v *SVpc) Refresh() error {
    59  	return nil
    60  }
    61  
    62  func (v *SVpc) IsEmulated() bool {
    63  	return false
    64  }
    65  
    66  func (v *SVpc) GetRegion() cloudprovider.ICloudRegion {
    67  	return v.region
    68  }
    69  
    70  func (v *SVpc) GetIsDefault() bool {
    71  	return false
    72  }
    73  
    74  func (v *SVpc) GetCidrBlock() string {
    75  	return ""
    76  }
    77  
    78  func (v *SVpc) GetIWires() ([]cloudprovider.ICloudWire, error) {
    79  	if v.iwires != nil {
    80  		return v.iwires, nil
    81  	}
    82  	wire := SWire{
    83  		vpc: v,
    84  	}
    85  	v.iwires = []cloudprovider.ICloudWire{&wire}
    86  	return v.iwires, nil
    87  }
    88  
    89  func (v *SVpc) GetWire() *SWire {
    90  	return &SWire{
    91  		vpc: v,
    92  	}
    93  }
    94  
    95  func (v *SVpc) fetchSecurityGroups() error {
    96  	secgroups := make([]SSecurityGroup, 0)
    97  	n := 1
    98  	for {
    99  		parts, total, err := v.region.GetSecurityGroups(v.GetId(), []string{}, n, 100)
   100  		if err != nil {
   101  			return err
   102  		}
   103  		secgroups = append(secgroups, parts...)
   104  		if len(secgroups) >= total {
   105  			break
   106  		}
   107  		n++
   108  	}
   109  	v.secgroups = make([]cloudprovider.ICloudSecurityGroup, len(secgroups))
   110  	for i := 0; i < len(secgroups); i++ {
   111  		secgroups[i].vpc = v
   112  		v.secgroups[i] = &secgroups[i]
   113  	}
   114  	return nil
   115  }
   116  
   117  func (v *SVpc) GetISecurityGroups() ([]cloudprovider.ICloudSecurityGroup, error) {
   118  	if v.secgroups == nil {
   119  		err := v.fetchSecurityGroups()
   120  		if err != nil {
   121  			return nil, err
   122  		}
   123  	}
   124  	return v.secgroups, nil
   125  }
   126  
   127  func (v *SVpc) GetIRouteTables() ([]cloudprovider.ICloudRouteTable, error) {
   128  	return nil, cloudprovider.ErrNotImplemented
   129  }
   130  
   131  func (v *SVpc) GetIRouteTableById(routeTableId string) (cloudprovider.ICloudRouteTable, error) {
   132  	return nil, nil
   133  }
   134  
   135  func (v *SVpc) Delete() error {
   136  	return cloudprovider.ErrNotImplemented
   137  }
   138  
   139  func (v *SVpc) GetIWireById(wireId string) (cloudprovider.ICloudWire, error) {
   140  	iwires, err := v.GetIWires()
   141  	if err != nil {
   142  		return nil, err
   143  	}
   144  	for i := range iwires {
   145  		if iwires[i].GetGlobalId() == wireId {
   146  			return iwires[i], nil
   147  		}
   148  	}
   149  	return nil, cloudprovider.ErrNotFound
   150  }
   151  
   152  func (r *SRegion) GetVpcs(pageNumber, pageSize int) (vpcs []SVpc, total int, err error) {
   153  	req := apis.NewDescribeVpcsRequestWithAllParams(r.ID, &pageNumber, &pageSize, nil)
   154  	client := client.NewVpcClient(r.getCredential())
   155  	client.Logger = Logger{debug: r.client.debug}
   156  	resp, err := client.DescribeVpcs(req)
   157  	if err != nil {
   158  		return
   159  	}
   160  	if resp.Error.Code >= 400 {
   161  		err = errors.Error(resp.Error.Message)
   162  		return
   163  	}
   164  	total = resp.Result.TotalCount
   165  	vpcs = make([]SVpc, 0, len(resp.Result.Vpcs))
   166  	for i := range resp.Result.Vpcs {
   167  		vpcs = append(vpcs, SVpc{
   168  			Vpc:    resp.Result.Vpcs[i],
   169  			region: r,
   170  		})
   171  	}
   172  	return
   173  }
   174  
   175  func (r *SRegion) GetVpcById(id string) (*SVpc, error) {
   176  	req := apis.NewDescribeVpcRequest(r.ID, id)
   177  	client := client.NewVpcClient(r.getCredential())
   178  	client.Logger = Logger{debug: r.client.debug}
   179  	resp, err := client.DescribeVpc(req)
   180  	if err != nil {
   181  		return nil, err
   182  	}
   183  	if resp.Error.Code >= 400 {
   184  		return nil, fmt.Errorf(resp.Error.Message)
   185  	}
   186  	return &SVpc{
   187  		region: r,
   188  		Vpc:    resp.Result.Vpc,
   189  	}, nil
   190  }