yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ctyun/secrules.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 ctyun 16 17 import ( 18 "yunion.io/x/jsonutils" 19 "yunion.io/x/pkg/errors" 20 ) 21 22 type SSecurityGroupRule struct { 23 secgroup *SSecurityGroup 24 25 PortRangeMax int64 `json:"port_range_max"` 26 SecurityGroupID string `json:"security_group_id"` 27 RemoteGroupId string `json:"remote_group_id"` 28 Description string `json:"description"` 29 RemoteIPPrefix string `json:"remote_ip_prefix"` 30 Protocol string `json:"protocol"` 31 Ethertype string `json:"ethertype"` 32 UpdatedAt string `json:"updated_at"` 33 Direction string `json:"direction"` 34 TenantID string `json:"tenant_id"` 35 ID string `json:"id"` 36 ProjectID string `json:"project_id"` 37 PortRangeMin int64 `json:"port_range_min"` 38 CreatedAt string `json:"created_at"` 39 } 40 41 func (self *SRegion) GetSecurityGroupRules(secgroupId string) ([]SSecurityGroupRule, error) { 42 params := map[string]string{ 43 "regionId": self.GetId(), 44 "securityGroupId": secgroupId, 45 } 46 47 resp, err := self.client.DoGet("/apiproxy/v3/getSecurityGroupRules", params) 48 if err != nil { 49 return nil, errors.Wrap(err, "SRegion.GetSecurityGroupRules.DoGet") 50 } 51 52 ret := make([]SSecurityGroupRule, 0) 53 err = resp.Unmarshal(&ret, "returnObj", "security_group_rules") 54 if err != nil { 55 return nil, errors.Wrap(err, "SRegion.GetSecurityGroupRules.Unmarshal") 56 } 57 58 secgroup, err := self.GetSecurityGroupDetails(secgroupId) 59 if err != nil { 60 return nil, errors.Wrap(err, "SRegion.GetSecurityGroupRules.GetSecurityGroupDetails") 61 } 62 63 for i := range ret { 64 ret[i].secgroup = secgroup 65 } 66 67 return ret, nil 68 } 69 70 func (self *SRegion) CreateSecurityGroupRule(groupId, direction, ethertype, protocol, remoteIpPrefix string, portRangeMin, portRangeMax int64) error { 71 ruleParams := jsonutils.NewDict() 72 ruleParams.Set("regionId", jsonutils.NewString(self.GetId())) 73 ruleParams.Set("securityGroupId", jsonutils.NewString(groupId)) 74 ruleParams.Set("direction", jsonutils.NewString(direction)) 75 ruleParams.Set("ethertype", jsonutils.NewString(ethertype)) 76 77 if len(protocol) > 0 { 78 ruleParams.Set("protocol", jsonutils.NewString(protocol)) 79 } 80 81 if len(remoteIpPrefix) > 0 { 82 ruleParams.Set("remoteIpPrefix", jsonutils.NewString(remoteIpPrefix)) 83 } 84 85 if portRangeMin > 0 { 86 ruleParams.Set("portRangeMin", jsonutils.NewInt(portRangeMin)) 87 } 88 89 if portRangeMax > 0 { 90 ruleParams.Set("portRangeMax", jsonutils.NewInt(portRangeMax)) 91 } 92 93 params := map[string]jsonutils.JSONObject{ 94 "jsonStr": ruleParams, 95 } 96 97 _, err := self.client.DoPost("/apiproxy/v3/createSecurityGroupRule", params) 98 if err != nil { 99 return errors.Wrap(err, "SRegion.CreateSecurityGroupRule.DoPost") 100 } 101 102 return err 103 }