yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ctyun/shell/secgroup.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/pkg/errors"
    19  	"yunion.io/x/pkg/util/secrules"
    20  
    21  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    22  	"yunion.io/x/cloudmux/pkg/multicloud/ctyun"
    23  	"yunion.io/x/onecloud/pkg/util/shellutils"
    24  )
    25  
    26  func init() {
    27  	type VSecurityGroupListOptions struct {
    28  		Vpc string `help:"Vpc ID"`
    29  	}
    30  	shellutils.R(&VSecurityGroupListOptions{}, "secgroup-list", "List secgroups", func(cli *ctyun.SRegion, args *VSecurityGroupListOptions) error {
    31  		secgroups, e := cli.GetSecurityGroups(args.Vpc)
    32  		if e != nil {
    33  			return e
    34  		}
    35  		printList(secgroups, 0, 0, 0, nil)
    36  		return nil
    37  	})
    38  
    39  	type VSecurityGroupRuleListOptions struct {
    40  		Group string `help:"Security Group ID"`
    41  	}
    42  	shellutils.R(&VSecurityGroupRuleListOptions{}, "secrule-list", "List secgroup rules", func(cli *ctyun.SRegion, args *VSecurityGroupRuleListOptions) error {
    43  		secrules, e := cli.GetSecurityGroupRules(args.Group)
    44  		if e != nil {
    45  			return e
    46  		}
    47  		printList(secrules, 0, 0, 0, nil)
    48  		return nil
    49  	})
    50  
    51  	type SecurityGroupCreateOptions struct {
    52  		VpcId string `help:"vpc id"`
    53  		Name  string `help:"secgroup name"`
    54  	}
    55  	shellutils.R(&SecurityGroupCreateOptions{}, "secgroup-create", "Create secgroup", func(cli *ctyun.SRegion, args *SecurityGroupCreateOptions) error {
    56  		vpc, e := cli.CreateSecurityGroup(args.VpcId, args.Name)
    57  		if e != nil {
    58  			return e
    59  		}
    60  		printObject(vpc)
    61  		return nil
    62  	})
    63  
    64  	type SecurityGroupRuleCreateOptions struct {
    65  		GROUP string `help:"secgroup id"`
    66  		RULE  string
    67  	}
    68  	shellutils.R(&SecurityGroupRuleCreateOptions{}, "secrule-create", "Create secgroup rule", func(cli *ctyun.SRegion, args *SecurityGroupRuleCreateOptions) error {
    69  		rule, err := secrules.ParseSecurityRule(args.RULE)
    70  		if err != nil {
    71  			return errors.Wrap(err, "ParseSecurityRule")
    72  		}
    73  		return cli.AddSecurityGroupRules(args.GROUP, cloudprovider.SecurityRule{SecurityRule: *rule})
    74  	})
    75  }