yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/shell/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 shell
    16  
    17  import (
    18  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    19  	"yunion.io/x/cloudmux/pkg/multicloud/qcloud"
    20  	"yunion.io/x/onecloud/pkg/util/shellutils"
    21  )
    22  
    23  func init() {
    24  	type VpcListOptions struct {
    25  		Limit  int `help:"page size"`
    26  		Offset int `help:"page offset"`
    27  	}
    28  	shellutils.R(&VpcListOptions{}, "vpc-list", "List vpcs", func(cli *qcloud.SRegion, args *VpcListOptions) error {
    29  		vpcs, total, err := cli.GetVpcs(nil, args.Offset, args.Limit)
    30  		if err != nil {
    31  			return err
    32  		}
    33  		printList(vpcs, total, args.Offset, args.Limit, []string{})
    34  		return nil
    35  	})
    36  
    37  	type VpcCreateOptions struct {
    38  		NAME string `help:"Name for vpc"`
    39  		CIDR string `help:"Cidr for vpc" choices:"10.0.0.0/16|172.16.0.0/12|192.168.0.0/16"`
    40  	}
    41  	shellutils.R(&VpcCreateOptions{}, "vpc-create", "Create vpc", func(cli *qcloud.SRegion, args *VpcCreateOptions) error {
    42  		opts := &cloudprovider.VpcCreateOptions{
    43  			NAME: args.NAME,
    44  			CIDR: args.CIDR,
    45  		}
    46  		vpc, err := cli.CreateIVpc(opts)
    47  		if err != nil {
    48  			return err
    49  		}
    50  		printObject(vpc)
    51  		return nil
    52  	})
    53  
    54  	type VpcDeleteOptions struct {
    55  		ID string `help:"VPC ID or Name"`
    56  	}
    57  	shellutils.R(&VpcDeleteOptions{}, "vpc-delete", "Delete vpc", func(cli *qcloud.SRegion, args *VpcDeleteOptions) error {
    58  		return cli.DeleteVpc(args.ID)
    59  	})
    60  
    61  	type VpcPCListOption struct {
    62  		VPCID  string
    63  		Limit  int `help:"page size"`
    64  		Offset int `help:"page offset"`
    65  	}
    66  	shellutils.R(&VpcPCListOption{}, "vpcPC-list", "List vpc peering connections", func(cli *qcloud.SRegion, args *VpcPCListOption) error {
    67  		vpcs, total, err := cli.DescribeVpcPeeringConnections(args.VPCID, "", args.Offset, args.Limit)
    68  		if err != nil {
    69  			return err
    70  		}
    71  		printList(vpcs, total, args.Offset, args.Limit, []string{})
    72  		return nil
    73  	})
    74  
    75  	type VpcPCShowOption struct {
    76  		VPCPCID string
    77  	}
    78  	shellutils.R(&VpcPCShowOption{}, "vpcPC-show", "show vpc peering connection", func(cli *qcloud.SRegion, args *VpcPCShowOption) error {
    79  		vpcPC, err := cli.GetVpcPeeringConnectionbyId(args.VPCPCID)
    80  		if err != nil {
    81  			return err
    82  		}
    83  		printObject(vpcPC)
    84  		return nil
    85  	})
    86  
    87  	type VpcPCCreateOPtion struct {
    88  		NAME         string
    89  		VPCID        string
    90  		PEERVPCID    string
    91  		PEERREGIONID string
    92  		PEEROWNERID  string
    93  		Bandwidth    int
    94  	}
    95  	shellutils.R(&VpcPCCreateOPtion{}, "vpcPC-create", "create vpc peering connection", func(cli *qcloud.SRegion, args *VpcPCCreateOPtion) error {
    96  		opts := cloudprovider.VpcPeeringConnectionCreateOptions{}
    97  		opts.Name = args.NAME
    98  		opts.PeerVpcId = args.PEERVPCID
    99  		opts.PeerAccountId = args.PEEROWNERID
   100  		opts.PeerRegionId = args.PEERREGIONID
   101  		vpcPCId, err := cli.CreateVpcPeeringConnection(args.VPCID, &opts)
   102  		if err != nil {
   103  			return err
   104  		}
   105  		printObject(vpcPCId)
   106  		return nil
   107  	})
   108  
   109  	shellutils.R(&VpcPCCreateOPtion{}, "vpcPC-createEx", "create Ex vpc peering connection", func(cli *qcloud.SRegion, args *VpcPCCreateOPtion) error {
   110  		opts := cloudprovider.VpcPeeringConnectionCreateOptions{}
   111  		opts.Name = args.NAME
   112  		opts.PeerVpcId = args.PEERVPCID
   113  		opts.PeerAccountId = args.PEEROWNERID
   114  		opts.PeerRegionId = args.PEERREGIONID
   115  		opts.Bandwidth = args.Bandwidth
   116  		taskId, err := cli.CreateVpcPeeringConnectionEx(args.VPCID, &opts)
   117  		if err != nil {
   118  			return err
   119  		}
   120  		printObject(taskId)
   121  		return nil
   122  	})
   123  
   124  	type VpcPeeringAcceptOPtion struct {
   125  		VPCPEERINGID string
   126  	}
   127  	shellutils.R(&VpcPeeringAcceptOPtion{}, "vpcPC-accept", "Accept vpcPeering", func(cli *qcloud.SRegion, args *VpcPeeringAcceptOPtion) error {
   128  		err := cli.AcceptVpcPeeringConnection(args.VPCPEERINGID)
   129  		if err != nil {
   130  			return err
   131  		}
   132  		return nil
   133  	})
   134  
   135  	shellutils.R(&VpcPeeringAcceptOPtion{}, "vpcPC-acceptEx", "Accept Ex vpcPeering", func(cli *qcloud.SRegion, args *VpcPeeringAcceptOPtion) error {
   136  		_, err := cli.AcceptVpcPeeringConnectionEx(args.VPCPEERINGID)
   137  		if err != nil {
   138  			return err
   139  		}
   140  		return nil
   141  	})
   142  
   143  	type VpcPeeringDeleteOPtion struct {
   144  		VPCPEERINGID string
   145  	}
   146  	shellutils.R(&VpcPeeringAcceptOPtion{}, "vpcPC-delete", "Delete vpcPeering", func(cli *qcloud.SRegion, args *VpcPeeringAcceptOPtion) error {
   147  		err := cli.DeleteVpcPeeringConnection(args.VPCPEERINGID)
   148  		if err != nil {
   149  			return err
   150  		}
   151  		return nil
   152  	})
   153  
   154  	shellutils.R(&VpcPeeringAcceptOPtion{}, "vpcPC-deleteEx", "Delete Ex vpcPeering", func(cli *qcloud.SRegion, args *VpcPeeringAcceptOPtion) error {
   155  		status, err := cli.DeleteVpcPeeringConnectionEx(args.VPCPEERINGID)
   156  		if err != nil {
   157  			return err
   158  		}
   159  		println(status)
   160  		return nil
   161  	})
   162  
   163  	type VpcTaskShowOption struct {
   164  		TASKID string
   165  	}
   166  	shellutils.R(&VpcTaskShowOption{}, "vpcTask-show", "show vpc task", func(cli *qcloud.SRegion, args *VpcTaskShowOption) error {
   167  		status, err := cli.DescribeVpcTaskResult(args.TASKID)
   168  		if err != nil {
   169  			return err
   170  		}
   171  		println(status)
   172  		return nil
   173  	})
   174  
   175  }