/
service_security_group.go
132 lines (115 loc) · 3.11 KB
/
service_security_group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package tencentcloud
import (
"encoding/json"
"errors"
"fmt"
"log"
"strings"
"github.com/zqfan/tencentcloud-sdk-go/client"
)
var (
errSecurityGroupRuleNotFound = errors.New("security group rule index not found")
)
func describeSecurityGroupRuleIndex(client *client.Client, rule map[string]string) (index int, err error) {
if rule["sgId"] == "" {
err = fmt.Errorf("describeSecurityGroupRuleIndex, sgId empty")
return
}
params := map[string]string{
"Action": "DescribeSecurityGroupPolicys",
"sgId": rule["sgId"],
}
var response string
response, err = client.SendRequest("dfw", params)
if err != nil {
log.Printf("[ERROR] describeSecurityGroupRuleIndex client.SendRequest error:%v", err)
return
}
var jsonresp struct {
Code int `json:"code"`
Message string `json:"message"`
Data struct {
Ingress []struct {
Index int `json:"index"`
CidrIp string `json:"cidrIp"`
IpProtocol string `json:"ipProtocol"`
PortRange string `json:"portRange"`
Action string `json:"action"`
}
Egress []struct {
Index int `json:"index"`
CidrIp string `json:"cidrIp"`
IpProtocol string `json:"ipProtocol"`
PortRange string `json:"portRange"`
Action string `json:"action"`
}
}
}
err = json.Unmarshal([]byte(response), &jsonresp)
if err != nil {
log.Printf("[ERROR] describeSecurityGroupRules json.Unmarshal error:%v", err)
return
}
if jsonresp.Code != 0 {
log.Printf("[ERROR] describeSecurityGroupRuleIndex error, code:%v, message:%v", jsonresp.Code, jsonresp.Message)
err = fmt.Errorf(jsonresp.Message)
return
}
rules := jsonresp.Data
_rule := rules.Ingress
if rule["direction"] == "egress" {
_rule = rules.Egress
}
exist := false
for _, r := range _rule {
r.IpProtocol = strings.ToLower(r.IpProtocol)
r.PortRange = strings.ToLower(r.PortRange)
r.Action = strings.ToLower(r.Action)
rule["ipProtocol"] = strings.ToLower(rule["ipProtocol"])
rule["portRange"] = strings.ToLower(rule["portRange"])
rule["action"] = strings.ToLower(rule["action"])
if r.CidrIp == rule["cidrIp"] && r.IpProtocol == rule["ipProtocol"] && r.PortRange == rule["portRange"] && r.Action == rule["action"] {
exist = true
index = r.Index
break
}
}
if exist == false {
err = errSecurityGroupRuleNotFound
}
return
}
func getSecurityGroupAssociatedInstancesBySgId(client *client.Client, sgId string) (instanceIds []string, err error) {
params := map[string]string{
"Action": "DescribeInstancesOfSecurityGroup",
"sgId": sgId,
}
response, err := client.SendRequest("dfw", params)
if err != nil {
return
}
var jsonresp struct {
Code int `json:"code"`
Message string `json:"message"`
TotalCount int `json:"totalCount"`
Data []struct {
InstanceId string `json:"instanceId"`
}
}
err = json.Unmarshal([]byte(response), &jsonresp)
if err != nil {
return
}
if jsonresp.Code != 0 {
err = fmt.Errorf(jsonresp.Message)
return
}
if jsonresp.TotalCount == 0 {
return
}
items := jsonresp.Data
for _, item := range items {
instanceIds = append(instanceIds, item.InstanceId)
}
return
}