yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/shell/cloud_connect_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 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 CcnListOption struct {
    25  		Limit  int `help:"page size"`
    26  		Offset int `help:"page offset"`
    27  	}
    28  	shellutils.R(&CcnListOption{}, "ccn-list", "List cloud connect network", func(cli *qcloud.SRegion, args *CcnListOption) error {
    29  		vpcs, total, err := cli.DescribeCcns(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 CcnShowOption struct {
    38  		CCNID string
    39  	}
    40  	shellutils.R(&CcnShowOption{}, "ccn-show", "show cloud connect network", func(cli *qcloud.SRegion, args *CcnShowOption) error {
    41  		ccn, err := cli.GetCcnById(args.CCNID)
    42  		if err != nil {
    43  			return err
    44  		}
    45  		printObject(ccn)
    46  		return nil
    47  	})
    48  
    49  	type CcnChildListOption struct {
    50  		CCNID  string
    51  		Limit  int `help:"page size"`
    52  		Offset int `help:"page offset"`
    53  	}
    54  	shellutils.R(&CcnChildListOption{}, "ccn-child-list", "List cloud connect network attatched instance", func(cli *qcloud.SRegion, args *CcnChildListOption) error {
    55  		vpcs, total, err := cli.DescribeCcnAttachedInstances(args.CCNID, args.Offset, args.Limit)
    56  		if err != nil {
    57  			return err
    58  		}
    59  		printList(vpcs, total, args.Offset, args.Limit, []string{})
    60  		return nil
    61  	})
    62  
    63  	type CenCreateOptions struct {
    64  		Name        string
    65  		Description string
    66  	}
    67  	shellutils.R(&CenCreateOptions{}, "ccn-create", "create cloud connect network", func(cli *qcloud.SRegion, args *CenCreateOptions) error {
    68  		opts := cloudprovider.SInterVpcNetworkCreateOptions{}
    69  		opts.Name = args.Name
    70  		opts.Desc = args.Description
    71  		ccnId, err := cli.CreateCcn(&opts)
    72  		if err != nil {
    73  			return err
    74  		}
    75  		print(ccnId)
    76  		return nil
    77  	})
    78  
    79  	type CenDeleteOptions struct {
    80  		ID string
    81  	}
    82  	shellutils.R(&CenDeleteOptions{}, "ccn-delete", "delete cloud connect network", func(cli *qcloud.SRegion, args *CenDeleteOptions) error {
    83  		err := cli.DeleteCcn(args.ID)
    84  		if err != nil {
    85  			return err
    86  		}
    87  		return nil
    88  	})
    89  
    90  	type CenAddVpcOptions struct {
    91  		ID          string
    92  		OwnerId     string
    93  		VpcId       string
    94  		VpcRegionId string
    95  	}
    96  	shellutils.R(&CenAddVpcOptions{}, "ccn-add-vpc", "add vpc to cloud connect network attatched instance", func(cli *qcloud.SRegion, args *CenAddVpcOptions) error {
    97  
    98  		instance := qcloud.SCcnAttachInstanceInput{
    99  			InstanceType:   "VPC",
   100  			InstanceId:     args.VpcId,
   101  			InstanceRegion: args.VpcRegionId,
   102  		}
   103  		err := cli.AttachCcnInstances(args.ID, args.OwnerId, []qcloud.SCcnAttachInstanceInput{instance})
   104  		if err != nil {
   105  			return err
   106  		}
   107  		return nil
   108  	})
   109  
   110  	type CenRemoveVpcOptions struct {
   111  		ID          string
   112  		VpcId       string
   113  		VpcRegionId string
   114  	}
   115  	shellutils.R(&CenAddVpcOptions{}, "ccn-remove-vpc", "remove vpc to cloud connect network attatched instance", func(cli *qcloud.SRegion, args *CenAddVpcOptions) error {
   116  		instance := qcloud.SCcnAttachInstanceInput{
   117  			InstanceType:   "VPC",
   118  			InstanceId:     args.VpcId,
   119  			InstanceRegion: args.VpcRegionId,
   120  		}
   121  		err := cli.DetachCcnInstances(args.ID, []qcloud.SCcnAttachInstanceInput{instance})
   122  		if err != nil {
   123  			return err
   124  		}
   125  		return nil
   126  	})
   127  }