This repository has been archived by the owner on Jan 5, 2021. It is now read-only.
/
resource_ksyun_security_group_entry.go
225 lines (219 loc) · 5.96 KB
/
resource_ksyun_security_group_entry.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package ksyun
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-ksyun/logger"
"strings"
)
func resourceKsyunSecurityGroupEntry() *schema.Resource {
return &schema.Resource{
Create: resourceKsyunSecurityGroupEntryCreate,
Read: resourceKsyunSecurityGroupEntryRead,
Delete: resourceKsyunSecurityGroupEntryDelete,
Schema: map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"security_group_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"cidr_block": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"direction": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"protocol": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"icmp_type": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"icmp_code": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"port_range_from": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"port_range_to": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"security_group_entry_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceKsyunSecurityGroupEntryCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*KsyunClient).vpcconn
creates := []string{
"description",
"security_group_id",
"cidr_block",
"direction",
"protocol",
"icmp_type",
"icmp_code",
"port_range_from",
"port_range_to",
}
createSecurityGroupEntry := make(map[string]interface{})
for _, v := range creates {
if v1, ok := d.GetOk(v); ok {
vv := Downline2Hump(v)
createSecurityGroupEntry[vv] = fmt.Sprintf("%v", v1)
}
}
var resp *map[string]interface{}
var err error
action := "AuthorizeSecurityGroupEntry"
logger.Debug(logger.ReqFormat, action, createSecurityGroupEntry)
resp, err = conn.AuthorizeSecurityGroupEntry(&createSecurityGroupEntry)
logger.Debug(logger.AllFormat, action, createSecurityGroupEntry, *resp, err)
if err != nil {
return fmt.Errorf("error on creating SecurityGroupEntry, %s", err)
}
idSet, ok := (*resp)["SecurityGroupEntryIdSet"]
if !ok {
return fmt.Errorf("AuthorizeSecurityGroupEntry Error : no ids found")
}
ids, ok := idSet.([]interface{})
if !ok || len(ids) == 0 {
return fmt.Errorf("AuthorizeSecurityGroupEntry Error : no ids found")
}
idres, ok := ids[0].(string)
if !ok {
return fmt.Errorf("AuthorizeSecurityGroupEntry Error : no id found")
}
if err := d.Set("security_group_entry_id", idres); err != nil {
return err
}
d.SetId(idres)
return nil
}
func resourceKsyunSecurityGroupEntryRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*KsyunClient).vpcconn
readSecurityGroup := make(map[string]interface{})
if _, ok := d.GetOk("security_group_id"); !ok {
return fmt.Errorf("security_group_id can't be null")
}
sgId := d.Get("security_group_id")
readSecurityGroup["SecurityGroupId.1"] = sgId
action := "DescribeSecurityGroups"
logger.Debug(logger.ReqFormat, action, readSecurityGroup)
resp, err := conn.DescribeSecurityGroups(&readSecurityGroup)
logger.Debug(logger.AllFormat, action, readSecurityGroup, *resp, err)
if err != nil {
return fmt.Errorf("error on reading SecurityGroup %q, %s", d.Id(), err)
}
itemset := (*resp)["SecurityGroupSet"]
items, ok := itemset.([]interface{})
if !ok || len(items) == 0 {
d.SetId("")
return nil
}
for _, v := range items {
item, ok := v.(map[string]interface{})
if !ok {
continue
}
if item["security_group_entry_id"] == d.Id() {
for sgk, sgv := range item {
if vpcSecurityGroupEntrySetKeys[sgk] {
if err := d.Set(Downline2Hump(sgk), sgv); err != nil {
return err
}
}
}
}
}
return nil
}
func resourceKsyunSecurityGroupEntryDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*KsyunClient).vpcconn
//delete
if _, ok := d.GetOk("security_group_id"); !ok {
return fmt.Errorf("security_group_id can't be null")
}
deleteSecurityGroupEntry := make(map[string]interface{})
deleteSecurityGroupEntry["SecurityGroupId"] = d.Get("security_group_id")
deleteSecurityGroupEntry["SecurityGroupEntryId"] = d.Id()
action := "RevokeSecurityGroupEntry"
logger.Debug(logger.ReqFormat, action, deleteSecurityGroupEntry)
resp, err0 := conn.RevokeSecurityGroupEntry(&deleteSecurityGroupEntry)
logger.Debug(logger.AllFormat, action, deleteSecurityGroupEntry, *resp, err0)
if err0 == nil || (err0 != nil && strings.Contains(err0.Error(), "NotFound")) {
return nil
}
//check
readSecurityGroup := make(map[string]interface{})
readSecurityGroup["SecurityGroupId.1"] = d.Get("security_group_id")
action = "DescribeSecurityGroups"
logger.Debug(logger.ReqFormat, action, readSecurityGroup)
resp, err1 := conn.DescribeSecurityGroups(&readSecurityGroup)
logger.Debug(logger.AllFormat, action, readSecurityGroup, *resp, err1)
if err1 != nil && strings.Contains(err1.Error(), "NotFound") {
return nil
}
if err1 != nil {
return nil
}
itemSet, ok := (*resp)["SecurityGroupSet"]
if !ok {
return nil
}
items, ok := itemSet.([]interface{})
if !ok || len(items) == 0 {
return nil
}
for _, v := range items {
item, ok := v.(map[string]interface{})
if !ok {
continue
}
sgeSet, ok := item["SecurityGroupEntrySet"]
if !ok {
continue
}
sges, ok := sgeSet.([]interface{})
if !ok {
continue
}
for k, sge := range sges {
sgeMap, ok := sge.(map[string]interface{})
if !ok {
continue
}
if sgeMap["SecurityGroupEntryId"] == d.Id() {
return fmt.Errorf("error on deleting SecurityGroups %q, %s", d.Id(), err0)
}
if k == len(sges)-1 {
return nil
}
}
}
return fmt.Errorf("error on deleting SecurityGroupEntrys %q, %s", d.Id(), err0)
}