Files
cloudpods/pkg/multicloud/qcloud/shell/securitygroup.go
2020-10-31 23:08:21 +08:00

117 lines
3.8 KiB
Go

// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package shell
import (
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/util/secrules"
"yunion.io/x/onecloud/pkg/cloudprovider"
"yunion.io/x/onecloud/pkg/multicloud/qcloud"
"yunion.io/x/onecloud/pkg/util/shellutils"
)
func init() {
type SecurityGroupListOptions struct {
Ids []string `help:"Secgroup Ids"`
VpcId string `help:"Vpc Id"`
Name string `help:"Secgroup Name"`
Limit int `help:"page size"`
Offset int `help:"page offset"`
}
shellutils.R(&SecurityGroupListOptions{}, "security-group-list", "List SecurityGroup", func(cli *qcloud.SRegion, args *SecurityGroupListOptions) error {
secgrps, total, err := cli.GetSecurityGroups(args.Ids, args.VpcId, args.Name, args.Limit, args.Offset)
if err != nil {
return err
}
printList(secgrps, total, args.Offset, args.Limit, []string{})
return nil
})
type SecurityGroupOptions struct {
ID string `help:"SecurityGroup ID"`
}
shellutils.R(&SecurityGroupOptions{}, "security-group-show", "Show SecurityGroup", func(cli *qcloud.SRegion, args *SecurityGroupOptions) error {
secgroups, _, err := cli.GetSecurityGroups([]string{args.ID}, "", "", 0, 1)
if err != nil {
return err
}
if len(secgroups) == 1 {
printObject(secgroups[0])
return nil
}
return cloudprovider.ErrNotFound
})
shellutils.R(&SecurityGroupOptions{}, "security-group-delete", "Delete SecurityGroup", func(cli *qcloud.SRegion, args *SecurityGroupOptions) error {
return cli.DeleteSecurityGroup(args.ID)
})
type SecurityGroupCreateOptions struct {
NAME string `help:"SecurityGroup Name"`
ProjectId string `help:"Project SecurityGroup belong to"`
Desc string `help:"SecurityGroup Description"`
}
shellutils.R(&SecurityGroupCreateOptions{}, "security-group-create", "Create SecurityGroup", func(cli *qcloud.SRegion, args *SecurityGroupCreateOptions) error {
secgrp, err := cli.CreateSecurityGroup(args.NAME, args.ProjectId, args.Desc)
if err != nil {
return err
}
printObject(secgrp)
return nil
})
type AddressShowOptions struct {
Id string `help:"IP address ID"`
Name string `help:"IP address name"`
Limit int `help:"page size"`
Offset int `help:"page offset"`
}
shellutils.R(&AddressShowOptions{}, "address-list", "Show address", func(cli *qcloud.SRegion, args *AddressShowOptions) error {
address, total, err := cli.AddressList(args.Id, args.Name, args.Offset, args.Limit)
if err != nil {
return err
}
printList(address, total, args.Offset, args.Limit, []string{})
return nil
})
type RuleDeleteOptions struct {
SECGROUP_ID string
DIRECTION string `choices:"Egress|Ingress"`
IDS []string
}
shellutils.R(&RuleDeleteOptions{}, "security-group-rule-delete", "Delete rules", func(cli *qcloud.SRegion, args *RuleDeleteOptions) error {
return cli.DeleteRules(args.SECGROUP_ID, args.DIRECTION, args.IDS)
})
type RuleCreateOptions struct {
SECGROUP_ID string
INDEX int
RULE string
}
shellutils.R(&RuleCreateOptions{}, "security-group-rule-create", "Create rule", func(cli *qcloud.SRegion, args *RuleCreateOptions) error {
rule, err := secrules.ParseSecurityRule(args.RULE)
if err != nil {
return errors.Wrap(err, "secrules.ParseRuleString")
}
return cli.AddRule(args.SECGROUP_ID, args.INDEX, cloudprovider.SecurityRule{SecurityRule: *rule})
})
}