yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcs/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/pkg/errors"
    19  	"yunion.io/x/pkg/util/secrules"
    20  
    21  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    22  	"yunion.io/x/cloudmux/pkg/multicloud/hcs"
    23  	"yunion.io/x/onecloud/pkg/util/shellutils"
    24  )
    25  
    26  func init() {
    27  	type SecurityGroupListOptions struct {
    28  		VpcId string `help:"VPC ID"`
    29  		Name  string `help:"Secgroup name"`
    30  	}
    31  	shellutils.R(&SecurityGroupListOptions{}, "security-group-list", "List security group", func(cli *hcs.SRegion, args *SecurityGroupListOptions) error {
    32  		secgrps, e := cli.GetSecurityGroups(args.VpcId)
    33  		if e != nil {
    34  			return e
    35  		}
    36  		printList(secgrps, 0, 0, 0, nil)
    37  		return nil
    38  	})
    39  
    40  	type SecurityGroupShowOptions struct {
    41  		ID string `help:"ID or name of security group"`
    42  	}
    43  	shellutils.R(&SecurityGroupShowOptions{}, "security-group-show", "Show details of a security group", func(cli *hcs.SRegion, args *SecurityGroupShowOptions) error {
    44  		secgrp, err := cli.GetSecurityGroupDetails(args.ID)
    45  		if err != nil {
    46  			return err
    47  		}
    48  		printObject(secgrp)
    49  		return nil
    50  	})
    51  
    52  	type SecurityGroupCreateOptions struct {
    53  		NAME string `help:"secgroup name"`
    54  		VPC  string `help:"ID of VPC"`
    55  		Desc string `help:"description"`
    56  	}
    57  	shellutils.R(&SecurityGroupCreateOptions{}, "security-group-create", "Create security group", func(cli *hcs.SRegion, args *SecurityGroupCreateOptions) error {
    58  		result, err := cli.CreateSecurityGroup(args.VPC, args.NAME, args.Desc)
    59  		if err != nil {
    60  			return err
    61  		}
    62  		printObject(result)
    63  		return nil
    64  	})
    65  
    66  	type SecurityGroupRuleIdOptions struct {
    67  		ID string
    68  	}
    69  
    70  	shellutils.R(&SecurityGroupRuleIdOptions{}, "security-group-rule-delete", "Delete security group rule", func(cli *hcs.SRegion, args *SecurityGroupRuleIdOptions) error {
    71  		return cli.DeleteSecurityGroupRule(args.ID)
    72  	})
    73  
    74  	type SecurityGroupRuleCreateOptions struct {
    75  		SECGROUP_ID string
    76  		RULE        string
    77  	}
    78  
    79  	shellutils.R(&SecurityGroupRuleCreateOptions{}, "security-group-rule-create", "Create security group rule", func(cli *hcs.SRegion, args *SecurityGroupRuleCreateOptions) error {
    80  		_rule, err := secrules.ParseSecurityRule(args.RULE)
    81  		if err != nil {
    82  			return errors.Wrapf(err, "invalid rule %s", args.RULE)
    83  		}
    84  		rule := cloudprovider.SecurityRule{
    85  			SecurityRule: *_rule,
    86  		}
    87  		return cli.CreateSecurityGroupRule(args.SECGROUP_ID, rule)
    88  	})
    89  
    90  }