/
helper.go
186 lines (156 loc) · 4.83 KB
/
helper.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package securitygrouprule
import (
"bytes"
"fmt"
"hash/crc32"
"sort"
"strconv"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/nifcloud/nifcloud-sdk-go/nifcloud"
"github.com/nifcloud/nifcloud-sdk-go/service/computing"
"github.com/nifcloud/nifcloud-sdk-go/service/computing/types"
"github.com/nifcloud/terraform-provider-nifcloud/nifcloud/internal/mutexkv"
)
var mutexKV = mutexkv.NewMutexKV()
type securityGroupNotFound struct {
name string
securityGroups []types.SecurityGroupInfo
}
func (err securityGroupNotFound) Error() string {
if err.securityGroups == nil {
return fmt.Sprintf("No security group with name %q", err.name)
}
return fmt.Sprintf("Expected to find one security group with name %q, got: %#v",
err.name, err.securityGroups)
}
func checkSecurityGroupExist(securityGroupInfo []types.SecurityGroupInfo, groupName string) error {
for _, s := range securityGroupInfo {
if nifcloud.ToString(s.GroupName) == groupName {
return nil
}
}
return securityGroupNotFound{name: groupName, securityGroups: securityGroupInfo}
}
func idHash(inputList []*computing.AuthorizeSecurityGroupIngressInput) string {
var buf bytes.Buffer
ip := inputList[0].IpPermissions[0]
if ip.FromPort != nil && nifcloud.ToInt32(ip.FromPort) > 0 {
buf.WriteString(fmt.Sprintf("%d-", nifcloud.ToInt32(ip.FromPort)))
}
if ip.ToPort != nil && nifcloud.ToInt32(ip.ToPort) > 0 {
buf.WriteString(fmt.Sprintf("%d-", nifcloud.ToInt32(ip.ToPort)))
}
buf.WriteString(fmt.Sprintf("%s-", ip.IpProtocol))
buf.WriteString(fmt.Sprintf("%s-", ip.InOut))
buf.WriteString(fmt.Sprintf("%s-", nifcloud.ToString(ip.Description)))
s := make([]string, len(inputList))
for i, input := range inputList {
s[i] = nifcloud.ToString(input.GroupName)
sort.Strings(s)
}
for _, v := range s {
buf.WriteString(fmt.Sprintf("%s-", v))
}
if ip.ListOfRequestIpRanges != nil && len(ip.ListOfRequestIpRanges) > 0 {
buf.WriteString(fmt.Sprintf("%s-", nifcloud.ToString(ip.ListOfRequestIpRanges[0].CidrIp)))
}
if ip.ListOfRequestGroups != nil && len(ip.ListOfRequestGroups) > 0 {
buf.WriteString(fmt.Sprintf("%s-", nifcloud.ToString(ip.ListOfRequestGroups[0].GroupName)))
}
hashcode := 0
v := int(crc32.ChecksumIEEE(buf.Bytes()))
if v >= 0 {
hashcode = v
}
if -v >= 0 {
hashcode = -v
}
return fmt.Sprintf("sgrule-%d", hashcode)
}
func validateSecurityGroupRuleImportString(importStr string) ([]string, error) {
// example: IN_TCP_8000_8000_10.0.3.0/24_example
importParts := strings.Split(importStr, "_")
errStr := "unexpected format of import string (%q), expected SECURITYGROUPNAME_TYPE_PROTOCOL_FROMPORT_TOPORT_SOURCE: %s"
if len(importParts) < 6 {
return nil, fmt.Errorf(errStr, importStr, "invalid parts")
}
ruleType := importParts[0]
protocol := importParts[1]
fromPort := importParts[2]
toPort := importParts[3]
source := importParts[4]
sgName := importParts[5:]
if len(sgName) == 0 {
return nil, fmt.Errorf(errStr, importStr, "security group name must be required")
}
if ruleType != "IN" && ruleType != "OUT" {
return nil, fmt.Errorf(errStr, importStr, "expecting 'IN' or 'OUT'")
}
if protocol != "ANY" &&
protocol != "TCP" &&
protocol != "UDP" &&
protocol != "ICMP" &&
protocol != "GRE" &&
protocol != "AH" &&
protocol != "VRRP" &&
protocol != "ICMPv6-all" {
return nil, fmt.Errorf(errStr, importStr, "protocol must be ANY/TCP/UDP/ICMP/GRE/AH/VRRP/ICMPv6-all")
}
if fromPort != "-" && toPort != "-" {
if p1, err := strconv.Atoi(fromPort); err != nil {
return nil, fmt.Errorf(errStr, importStr, "invalid port")
} else if p2, err := strconv.Atoi(toPort); err != nil || p2 < p1 {
return nil, fmt.Errorf(errStr, importStr, "invalid port")
}
}
if source == "" {
return nil, fmt.Errorf(errStr, importStr, "source must be required")
}
return importParts, nil
}
func populateSecurityGroupRuleFromImport(d *schema.ResourceData, importParts []string) error {
ruleType := importParts[0]
protocol := importParts[1]
fromPort := importParts[2]
toPort := importParts[3]
source := importParts[4]
sgName := importParts[5:]
if err := d.Set("type", ruleType); err != nil {
return err
}
if err := d.Set("protocol", protocol); err != nil {
return err
}
if fromPort != "-" {
p, err := strconv.Atoi(fromPort)
if err != nil {
return err
}
if err := d.Set("from_port", p); err != nil {
return err
}
}
if toPort != "-" {
p, err := strconv.Atoi(toPort)
if err != nil {
return err
}
if err := d.Set("to_port", p); err != nil {
return err
}
}
if strings.Contains(source, ".") || strings.Contains(source, ":") {
if err := d.Set("cidr_ip", source); err != nil {
return err
}
} else {
if err := d.Set("source_security_group_name", source); err != nil {
return err
}
}
if err := d.Set("security_group_names", sgName); err != nil {
return err
}
return nil
}