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