yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/shell/securitygroup.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 SecurityGroupListOptions struct {
    25  		Ids    []string `help:"Secgroup Ids"`
    26  		VpcId  string   `help:"Vpc Id"`
    27  		Name   string   `help:"Secgroup Name"`
    28  		Limit  int      `help:"page size"`
    29  		Offset int      `help:"page offset"`
    30  	}
    31  	shellutils.R(&SecurityGroupListOptions{}, "security-group-list", "List SecurityGroup", func(cli *qcloud.SRegion, args *SecurityGroupListOptions) error {
    32  		secgrps, total, err := cli.GetSecurityGroups(args.Ids, args.VpcId, args.Name, args.Limit, args.Offset)
    33  		if err != nil {
    34  			return err
    35  		}
    36  		printList(secgrps, total, args.Offset, args.Limit, []string{})
    37  		return nil
    38  	})
    39  
    40  	type SecurityGroupOptions struct {
    41  		ID string `help:"SecurityGroup ID"`
    42  	}
    43  	shellutils.R(&SecurityGroupOptions{}, "security-group-show", "Show SecurityGroup", func(cli *qcloud.SRegion, args *SecurityGroupOptions) error {
    44  		secgroups, _, err := cli.GetSecurityGroups([]string{args.ID}, "", "", 0, 1)
    45  		if err != nil {
    46  			return err
    47  		}
    48  		if len(secgroups) == 1 {
    49  			printObject(secgroups[0])
    50  			return nil
    51  		}
    52  		return cloudprovider.ErrNotFound
    53  	})
    54  
    55  	shellutils.R(&SecurityGroupOptions{}, "security-group-references", "Show references of a security group", func(cli *qcloud.SRegion, args *SecurityGroupOptions) error {
    56  		references, err := cli.DescribeSecurityGroupReferences(args.ID)
    57  		if err != nil {
    58  			return err
    59  		}
    60  		printList(references, 0, 0, 0, nil)
    61  		return nil
    62  	})
    63  
    64  	shellutils.R(&SecurityGroupOptions{}, "security-group-delete", "Delete SecurityGroup", func(cli *qcloud.SRegion, args *SecurityGroupOptions) error {
    65  		return cli.DeleteSecurityGroup(args.ID)
    66  	})
    67  
    68  	type SecurityGroupCreateOptions struct {
    69  		NAME      string `help:"SecurityGroup Name"`
    70  		ProjectId string `help:"Project SecurityGroup belong to"`
    71  		Desc      string `help:"SecurityGroup Description"`
    72  	}
    73  
    74  	shellutils.R(&SecurityGroupCreateOptions{}, "security-group-create", "Create SecurityGroup", func(cli *qcloud.SRegion, args *SecurityGroupCreateOptions) error {
    75  		secgrp, err := cli.CreateSecurityGroup(args.NAME, args.ProjectId, args.Desc)
    76  		if err != nil {
    77  			return err
    78  		}
    79  		printObject(secgrp)
    80  		return nil
    81  	})
    82  
    83  	type AddressShowOptions struct {
    84  		Id     string `help:"IP address ID"`
    85  		Name   string `help:"IP address name"`
    86  		Limit  int    `help:"page size"`
    87  		Offset int    `help:"page offset"`
    88  	}
    89  	shellutils.R(&AddressShowOptions{}, "address-list", "Show address", func(cli *qcloud.SRegion, args *AddressShowOptions) error {
    90  		address, total, err := cli.AddressList(args.Id, args.Name, args.Offset, args.Limit)
    91  		if err != nil {
    92  			return err
    93  		}
    94  		printList(address, total, args.Offset, args.Limit, []string{})
    95  		return nil
    96  	})
    97  
    98  }