yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/jdcloud/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 jdcloud
    16  
    17  import (
    18  	"fmt"
    19  
    20  	commodels "github.com/jdcloud-api/jdcloud-sdk-go/services/common/models"
    21  	"github.com/jdcloud-api/jdcloud-sdk-go/services/vpc/apis"
    22  	"github.com/jdcloud-api/jdcloud-sdk-go/services/vpc/client"
    23  	"github.com/jdcloud-api/jdcloud-sdk-go/services/vpc/models"
    24  
    25  	"yunion.io/x/pkg/util/netutils"
    26  
    27  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    28  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    29  	"yunion.io/x/cloudmux/pkg/multicloud"
    30  	"yunion.io/x/onecloud/pkg/util/rbacutils"
    31  )
    32  
    33  type SNetwork struct {
    34  	multicloud.SResourceBase
    35  	JdcloudTags
    36  
    37  	wire *SWire
    38  
    39  	models.Subnet
    40  }
    41  
    42  func (n *SNetwork) GetId() string {
    43  	return n.SubnetId
    44  }
    45  
    46  func (n *SNetwork) GetName() string {
    47  	return n.SubnetName
    48  }
    49  
    50  func (n *SNetwork) GetGlobalId() string {
    51  	return n.GetId()
    52  }
    53  
    54  func (n *SNetwork) GetStatus() string {
    55  	return api.NETWORK_STATUS_AVAILABLE
    56  }
    57  
    58  func (n *SNetwork) Refresh() error {
    59  	return nil
    60  }
    61  
    62  func (n *SNetwork) IsEmulated() bool {
    63  	return false
    64  }
    65  
    66  func (n *SNetwork) GetProjectId() string {
    67  	return ""
    68  }
    69  
    70  func (n *SNetwork) GetIWire() cloudprovider.ICloudWire {
    71  	return n.wire
    72  }
    73  
    74  func (n *SNetwork) GetIpStart() string {
    75  	return n.StartIp
    76  }
    77  
    78  func (n *SNetwork) GetIpEnd() string {
    79  	return n.EndIp
    80  }
    81  
    82  func (n *SNetwork) Cidr() string {
    83  	return n.AddressPrefix
    84  }
    85  
    86  func (n *SNetwork) GetIpMask() int8 {
    87  	pref, _ := netutils.NewIPV4Prefix(n.Cidr())
    88  	return pref.MaskLen
    89  }
    90  
    91  func (n *SNetwork) GetGateway() string {
    92  	return ""
    93  }
    94  
    95  func (n *SNetwork) GetServerType() string {
    96  	return api.NETWORK_TYPE_GUEST
    97  }
    98  
    99  func (n *SNetwork) GetIsPublic() bool {
   100  	return true
   101  }
   102  
   103  func (n *SNetwork) GetPublicScope() rbacutils.TRbacScope {
   104  	return rbacutils.ScopeDomain
   105  }
   106  
   107  func (n *SNetwork) Delete() error {
   108  	return cloudprovider.ErrNotImplemented
   109  }
   110  
   111  func (n *SNetwork) GetAllocTimeoutSeconds() int {
   112  	return 120
   113  }
   114  
   115  func (r *SRegion) GetNetworks(vpcId string, pageNumber int, pageSize int) ([]SNetwork, int, error) {
   116  	filters := []commodels.Filter{}
   117  	if vpcId != "" {
   118  		filters = append(filters, commodels.Filter{
   119  			Name:   "vpcId",
   120  			Values: []string{vpcId},
   121  		})
   122  	}
   123  	req := apis.NewDescribeSubnetsRequestWithAllParams(r.ID, &pageNumber, &pageSize, filters)
   124  	client := client.NewVpcClient(r.getCredential())
   125  	client.Logger = Logger{debug: r.client.debug}
   126  	resp, err := client.DescribeSubnets(req)
   127  	if err != nil {
   128  		return nil, 0, err
   129  	}
   130  	if resp.Error.Code >= 400 {
   131  		return nil, 0, fmt.Errorf(resp.Error.Message)
   132  	}
   133  	nets := make([]SNetwork, len(resp.Result.Subnets))
   134  	for i := range nets {
   135  		nets[i] = SNetwork{
   136  			Subnet: resp.Result.Subnets[i],
   137  		}
   138  	}
   139  	return nets, resp.Result.TotalCount, nil
   140  }
   141  
   142  func (r *SRegion) GetNetworkById(id string) (*SNetwork, error) {
   143  	req := apis.NewDescribeSubnetRequest(r.ID, id)
   144  	client := client.NewVpcClient(r.getCredential())
   145  	client.Logger = Logger{debug: r.client.debug}
   146  	resp, err := client.DescribeSubnet(req)
   147  	if err != nil {
   148  		return nil, err
   149  	}
   150  	if resp.Error.Code >= 400 {
   151  		return nil, fmt.Errorf(resp.Error.Message)
   152  	}
   153  	return &SNetwork{
   154  		Subnet: resp.Result.Subnet,
   155  	}, nil
   156  }