-
Notifications
You must be signed in to change notification settings - Fork 1
/
fw_group_v2.go
86 lines (70 loc) · 2.75 KB
/
fw_group_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
package viettelidc
import (
"context"
"fmt"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas_v2/groups"
)
func fwGroupV2RefreshFunc(networkingClient *gophercloud.ServiceClient, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
group, err := groups.Get(networkingClient, id).Extract()
if err != nil {
return nil, "", err
}
return group, group.Status, nil
}
}
func fwGroupV2DeleteFunc(networkingClient *gophercloud.ServiceClient, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
group, err := groups.Get(networkingClient, id).Extract()
if err != nil {
if _, ok := err.(gophercloud.ErrDefault404); ok {
return "", "DELETED", nil
}
return nil, "", fmt.Errorf("Unexpected error: %s", err)
}
return group, "DELETING", nil
}
}
func fwGroupV2IngressPolicyDeleteFunc(networkingClient *gophercloud.ServiceClient, d *schema.ResourceData, ctx context.Context, groupID string) diag.Diagnostics {
stateConf := &resource.StateChangeConf{
Pending: []string{"PENDING_CREATE", "PENDING_UPDATE"},
Target: []string{"ACTIVE", "INACTIVE", "DOWN"},
Refresh: fwGroupV2RefreshFunc(networkingClient, d.Id()),
Timeout: d.Timeout(schema.TimeoutUpdate),
Delay: 0,
MinTimeout: 2 * time.Second,
}
_, err := stateConf.WaitForStateContext(ctx)
if err != nil {
return diag.Errorf("Error waiting for openstack_fw_group_v2 %s to become active: %s", d.Id(), err)
}
_, err = groups.RemoveIngressPolicy(networkingClient, groupID).Extract()
if err != nil {
return diag.Errorf("Error removing ingress firewall policy from openstack_fw_group_v2 %s: %s", d.Id(), err)
}
return nil
}
func fwGroupV2EgressPolicyDeleteFunc(networkingClient *gophercloud.ServiceClient, d *schema.ResourceData, ctx context.Context, groupID string) diag.Diagnostics {
stateConf := &resource.StateChangeConf{
Pending: []string{"PENDING_CREATE", "PENDING_UPDATE"},
Target: []string{"ACTIVE", "INACTIVE", "DOWN"},
Refresh: fwGroupV2RefreshFunc(networkingClient, d.Id()),
Timeout: d.Timeout(schema.TimeoutUpdate),
Delay: 0,
MinTimeout: 2 * time.Second,
}
_, err := stateConf.WaitForStateContext(ctx)
if err != nil {
return diag.Errorf("Error waiting for openstack_fw_group_v2 %s to become active: %s", d.Id(), err)
}
_, err = groups.RemoveEgressPolicy(networkingClient, groupID).Extract()
if err != nil {
return diag.Errorf("Error removing egress firewall policy from openstack_fw_group_v2 %s: %s", d.Id(), err)
}
return nil
}