| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- // 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 hcso
- import (
- "fmt"
- "net/url"
- "strings"
- "yunion.io/x/cloudmux/pkg/cloudprovider"
- "yunion.io/x/pkg/errors"
- "yunion.io/x/pkg/util/secrules"
- )
- type SecurityGroupRule struct {
- secgroup *SSecurityGroup
- Direction string
- Ethertype string
- Id string
- Description string
- PortRangeMax int64
- PortRangeMin int64
- Protocol string
- RemoteGroupId string
- RemoteIPPrefix string
- SecurityGroupId string
- TenantId string
- }
- func (self *SecurityGroupRule) GetGlobalId() string {
- return self.Id
- }
- func (self *SecurityGroupRule) GetDescription() string {
- return self.Description
- }
- func (self *SecurityGroupRule) GetDirection() secrules.TSecurityRuleDirection {
- if self.Direction == "egress" {
- return secrules.DIR_OUT
- }
- return secrules.DIR_IN
- }
- func (self *SecurityGroupRule) GetPriority() int {
- return 0
- }
- func (self *SecurityGroupRule) GetAction() secrules.TSecurityRuleAction {
- return secrules.SecurityRuleAllow
- }
- func (self *SecurityGroupRule) GetProtocol() string {
- if len(self.Protocol) == 0 {
- self.Protocol = secrules.PROTO_ANY
- }
- return strings.ToLower(self.Protocol)
- }
- func (self *SecurityGroupRule) GetPorts() string {
- if self.PortRangeMax > 0 && self.PortRangeMin > 0 {
- if self.PortRangeMax == self.PortRangeMin {
- return fmt.Sprintf("%d", self.PortRangeMax)
- }
- return fmt.Sprintf("%d-%d", self.PortRangeMin, self.PortRangeMax)
- }
- return ""
- }
- type SPageInfo struct {
- NextMarker string
- }
- func (self *SecurityGroupRule) GetCIDRs() []string {
- ret := []string{self.RemoteIPPrefix + self.RemoteGroupId}
- return ret
- }
- func (self *SecurityGroupRule) Delete() error {
- return self.secgroup.region.DeleteSecurityGroupRule(self.Id)
- }
- func (self *SRegion) DeleteSecurityGroupRule(id string) error {
- _, err := self.delete(SERVICE_VPC, "vpc/security-group-rules/"+id)
- return err
- }
- func (self *SRegion) GetSecurityGroupRules(groupId string) ([]SecurityGroupRule, error) {
- params := url.Values{}
- params.Set("security_group_id", groupId)
- ret := []SecurityGroupRule{}
- for {
- resp, err := self.list(SERVICE_VPC, "vpc/security-group-rules", params)
- if err != nil {
- return nil, err
- }
- part := struct {
- SecurityGroupRules []SecurityGroupRule
- PageInfo SPageInfo
- }{}
- err = resp.Unmarshal(&part)
- if err != nil {
- return nil, errors.Wrapf(err, "Unmarshal")
- }
- ret = append(ret, part.SecurityGroupRules...)
- if len(part.PageInfo.NextMarker) == 0 || len(part.SecurityGroupRules) == 0 {
- break
- }
- params.Set("marker", part.PageInfo.NextMarker)
- }
- return ret, nil
- }
- func (self *SRegion) CreateSecurityGroupRule(groupId string, opts *cloudprovider.SecurityGroupRuleCreateOptions) (*SecurityGroupRule, error) {
- rule := map[string]interface{}{
- "security_group_id": groupId,
- "description": opts.Desc,
- "direction": "ingress",
- "ethertype": "IPv4",
- "protocol": strings.ToLower(opts.Protocol),
- "action": "allow",
- "priority": opts.Priority,
- }
- if len(opts.CIDR) > 0 {
- rule["remote_ip_prefix"] = opts.CIDR
- }
- if opts.Action == secrules.SecurityRuleDeny {
- rule["action"] = "deny"
- }
- if opts.Protocol == secrules.PROTO_ANY {
- delete(rule, "protocol")
- }
- if len(opts.Ports) > 0 {
- rule["multiport"] = opts.Ports
- }
- if opts.Direction == secrules.DIR_OUT {
- rule["direction"] = "egress"
- }
- params := map[string]interface{}{
- "security_group_rule": rule,
- }
- resp, err := self.post(SERVICE_VPC, "vpc/security-group-rules", params)
- if err != nil {
- return nil, errors.Wrapf(err, "create rule")
- }
- ret := &SecurityGroupRule{}
- return ret, resp.Unmarshal(ret)
- }
- func (self *SecurityGroupRule) Update(opts *cloudprovider.SecurityGroupRuleUpdateOptions) error {
- return cloudprovider.ErrNotImplemented
- }
|