mirror of
https://hubproxy.babadafafafafa.cn/https://github.com/yunionio/cloudpods.git
synced 2026-09-20 08:03:53 +08:00
118 lines
4.3 KiB
Go
118 lines
4.3 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 compute
|
||
|
||
import (
|
||
"yunion.io/x/jsonutils"
|
||
"yunion.io/x/pkg/errors"
|
||
"yunion.io/x/pkg/util/secrules"
|
||
|
||
"yunion.io/x/onecloud/pkg/mcclient/options"
|
||
)
|
||
|
||
type SecGroupRulesListOptions struct {
|
||
_ struct{} `mcp-desc:"列出安全组规则。建议传 secgroup(安全组 id/name);详情 climc_secgroup_rule_show,创建 climc_secgroup_rule_create,删除 climc_secgroup_rule_delete"`
|
||
|
||
options.BaseListOptions
|
||
Secgroup string `help:"Secgroup ID or Name" mcp:"true"`
|
||
SecgroupName string `help:"Search rules by fuzzy secgroup name" mcp:"true"`
|
||
Projects []string `help:"Filter rules by project" mcp:"true"`
|
||
Direction string `help:"filter Direction of rule" choices:"in|out" mcp:"true"`
|
||
Protocol string `help:"filter Protocol of rule" choices:"any|tcp|udp|icmp" mcp:"true"`
|
||
Action string `help:"filter Actin of rule" choices:"allow|deny" mcp:"true"`
|
||
Ports string `help:"filter Ports of rule" mcp:"true"`
|
||
Ip string `help:"filter cidr of rule" mcp:"true"`
|
||
}
|
||
|
||
func (opts *SecGroupRulesListOptions) Params() (jsonutils.JSONObject, error) {
|
||
return options.ListStructToParams(opts)
|
||
}
|
||
|
||
type SecGroupRulesCreateOptions struct {
|
||
_ struct{} `mcp-desc:"创建安全组规则。SECGROUP 为安全组 id/name,RULE 为规则字符串(如 in:allow tcp 22);可用 climc_secgroup_list 定位安全组"`
|
||
|
||
SECGROUP string `help:"Secgroup ID or Name" metavar:"Secgroup"`
|
||
RULE string `json:"-"`
|
||
Priority int64 `help:"priority of Rule" default:"50" mcp:"true"`
|
||
Desc string `help:"Description" json:"description" mcp:"true"`
|
||
}
|
||
|
||
// SecGroupRuleShowOptions / SecGroupRuleDeleteOptions 单独包装,避免复用 BaseShow/BaseId 误注册。
|
||
type SecGroupRuleShowOptions struct {
|
||
_ struct{} `mcp-desc:"查询单条安全组规则详情。ID 可用 climc_secgroup_rule_list 返回的 id"`
|
||
|
||
options.BaseShowOptions
|
||
}
|
||
|
||
type SecGroupRuleDeleteOptions struct {
|
||
_ struct{} `mcp-desc:"删除安全组规则。若尚不知 id,先用 climc_secgroup_rule_list(建议带 secgroup)定位"`
|
||
|
||
options.BaseIdOptions
|
||
}
|
||
|
||
func (opts *SecGroupRulesCreateOptions) Params() (jsonutils.JSONObject, error) {
|
||
rule, err := secrules.ParseSecurityRule(opts.RULE)
|
||
if err != nil {
|
||
return nil, errors.Wrapf(err, "invalid rule %s", opts.RULE)
|
||
}
|
||
return jsonutils.Marshal(map[string]interface{}{
|
||
"direction": rule.Direction,
|
||
"action": rule.Action,
|
||
"protocol": rule.Protocol,
|
||
"cidr": rule.IPNet.String(),
|
||
"ports": rule.GetPortsString(),
|
||
"priority": opts.Priority,
|
||
"description": opts.Desc,
|
||
"secgroup_id": opts.SECGROUP,
|
||
}), nil
|
||
}
|
||
|
||
type SecGroupRulesUpdateOptions struct {
|
||
options.BaseIdOptions
|
||
Name string `help:"New name of rule"`
|
||
Priority int64 `help:"priority of Rule"`
|
||
Protocol string `help:"Protocol of rule" choices:"any|tcp|udp|icmp"`
|
||
Ports string `help:"Ports of rule"`
|
||
Cidr string `help:"Cidr of rule"`
|
||
Action string `help:"filter Actin of rule" choices:"allow|deny"`
|
||
Desc string `help:"Description" metavar:"Description"`
|
||
}
|
||
|
||
func (opts *SecGroupRulesUpdateOptions) Params() (jsonutils.JSONObject, error) {
|
||
params := jsonutils.NewDict()
|
||
if len(opts.Name) > 0 {
|
||
params.Add(jsonutils.NewString(opts.Name), "name")
|
||
}
|
||
if len(opts.Desc) > 0 {
|
||
params.Add(jsonutils.NewString(opts.Desc), "description")
|
||
}
|
||
if opts.Priority > 0 {
|
||
params.Add(jsonutils.NewInt(opts.Priority), "priority")
|
||
}
|
||
if len(opts.Protocol) > 0 {
|
||
params.Add(jsonutils.NewString(opts.Protocol), "protocol")
|
||
}
|
||
if len(opts.Ports) > 0 {
|
||
params.Add(jsonutils.NewString(opts.Ports), "ports")
|
||
}
|
||
if len(opts.Cidr) > 0 {
|
||
params.Add(jsonutils.NewString(opts.Cidr), "cidr")
|
||
}
|
||
if len(opts.Action) > 0 {
|
||
params.Add(jsonutils.NewString(opts.Action), "action")
|
||
}
|
||
return params, nil
|
||
}
|