forked from huaweicloud/terraform-provider-huaweicloud
/
resource_huaweicloud_networking_secgroup_v2.go
215 lines (179 loc) · 6.01 KB
/
resource_huaweicloud_networking_secgroup_v2.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
package huaweicloud
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/security/groups"
"github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/security/rules"
)
func resourceNetworkingSecGroupV2() *schema.Resource {
return &schema.Resource{
Create: resourceNetworkingSecGroupV2Create,
Read: resourceNetworkingSecGroupV2Read,
Update: resourceNetworkingSecGroupV2Update,
Delete: resourceNetworkingSecGroupV2Delete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Delete: schema.DefaultTimeout(10 * time.Minute),
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"tenant_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"delete_default_rules": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
},
}
}
func resourceNetworkingSecGroupV2Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
}
opts := groups.CreateOpts{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
TenantID: d.Get("tenant_id").(string),
}
log.Printf("[DEBUG] Create HuaweiCloud Neutron Security Group: %#v", opts)
security_group, err := groups.Create(networkingClient, opts).Extract()
if err != nil {
return err
}
// Delete the default security group rules if it has been requested.
deleteDefaultRules := d.Get("delete_default_rules").(bool)
if deleteDefaultRules {
security_group, err := groups.Get(networkingClient, security_group.ID).Extract()
if err != nil {
return err
}
for _, rule := range security_group.Rules {
if err := rules.Delete(networkingClient, rule.ID).ExtractErr(); err != nil {
return fmt.Errorf(
"There was a problem deleting a default security group rule: %s", err)
}
}
}
log.Printf("[DEBUG] HuaweiCloud Neutron Security Group created: %#v", security_group)
d.SetId(security_group.ID)
return resourceNetworkingSecGroupV2Read(d, meta)
}
func resourceNetworkingSecGroupV2Read(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Retrieve information about security group: %s", d.Id())
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
}
security_group, err := groups.Get(networkingClient, d.Id()).Extract()
if err != nil {
return CheckDeleted(d, err, "HuaweiCloud Neutron Security group")
}
d.Set("description", security_group.Description)
d.Set("tenant_id", security_group.TenantID)
d.Set("name", security_group.Name)
d.Set("region", GetRegion(d, config))
return nil
}
func resourceNetworkingSecGroupV2Update(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
}
var update bool
var updateOpts groups.UpdateOpts
if d.HasChange("name") {
update = true
updateOpts.Name = d.Get("name").(string)
}
if d.HasChange("description") {
update = true
updateOpts.Description = d.Get("description").(string)
}
if update {
log.Printf("[DEBUG] Updating SecGroup %s with options: %#v", d.Id(), updateOpts)
_, err = groups.Update(networkingClient, d.Id(), updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error updating HuaweiCloud SecGroup: %s", err)
}
}
return resourceNetworkingSecGroupV2Read(d, meta)
}
func resourceNetworkingSecGroupV2Delete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Destroy security group: %s", d.Id())
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
}
stateConf := &resource.StateChangeConf{
Pending: []string{"ACTIVE"},
Target: []string{"DELETED"},
Refresh: waitForSecGroupDelete(networkingClient, d.Id()),
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: 5 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("Error deleting HuaweiCloud Neutron Security Group: %s", err)
}
d.SetId("")
return err
}
func waitForSecGroupDelete(networkingClient *golangsdk.ServiceClient, secGroupId string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
log.Printf("[DEBUG] Attempting to delete HuaweiCloud Security Group %s.\n", secGroupId)
r, err := groups.Get(networkingClient, secGroupId).Extract()
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
log.Printf("[DEBUG] Successfully deleted HuaweiCloud Neutron Security Group %s", secGroupId)
return r, "DELETED", nil
}
return r, "ACTIVE", err
}
err = groups.Delete(networkingClient, secGroupId).ExtractErr()
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
log.Printf("[DEBUG] Successfully deleted HuaweiCloud Neutron Security Group %s", secGroupId)
return r, "DELETED", nil
}
if errCode, ok := err.(golangsdk.ErrUnexpectedResponseCode); ok {
if errCode.Actual == 409 {
return r, "ACTIVE", nil
}
}
return r, "ACTIVE", err
}
log.Printf("[DEBUG] HuaweiCloud Neutron Security Group %s still active.\n", secGroupId)
return r, "ACTIVE", nil
}
}