-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource_viettelidc_networking_qos_bandwidth_limit_rule_v2.go
211 lines (172 loc) · 6.9 KB
/
resource_viettelidc_networking_qos_bandwidth_limit_rule_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
package viettelidc
import (
"context"
"log"
"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/openstack/networking/v2/extensions/qos/rules"
)
func resourceNetworkingQoSBandwidthLimitRuleV2() *schema.Resource {
return &schema.Resource{
CreateContext: resourceNetworkingQoSBandwidthLimitRuleV2Create,
ReadContext: resourceNetworkingQoSBandwidthLimitRuleV2Read,
UpdateContext: resourceNetworkingQoSBandwidthLimitRuleV2Update,
DeleteContext: resourceNetworkingQoSBandwidthLimitRuleV2Delete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"qos_policy_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"max_kbps": {
Type: schema.TypeInt,
Required: true,
ForceNew: false,
},
"max_burst_kbps": {
Type: schema.TypeInt,
Optional: true,
ForceNew: false,
},
"direction": {
Type: schema.TypeString,
Default: "egress",
Optional: true,
ForceNew: false,
},
},
}
}
func resourceNetworkingQoSBandwidthLimitRuleV2Create(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
networkingClient, err := config.NetworkingV2Client(GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating OpenStack networking client: %s", err)
}
createOpts := rules.CreateBandwidthLimitRuleOpts{
MaxKBps: d.Get("max_kbps").(int),
MaxBurstKBps: d.Get("max_burst_kbps").(int),
Direction: d.Get("direction").(string),
}
qosPolicyID := d.Get("qos_policy_id").(string)
log.Printf("[DEBUG] openstack_networking_qos_bandwidth_limit_rule_v2 create options: %#v", createOpts)
r, err := rules.CreateBandwidthLimitRule(networkingClient, qosPolicyID, createOpts).ExtractBandwidthLimitRule()
if err != nil {
return diag.Errorf("Error creating openstack_networking_qos_bandwidth_limit_rule_v2: %s", err)
}
log.Printf("[DEBUG] Waiting for openstack_networking_qos_bandwidth_limit_rule_v2 %s to become available.", r.ID)
stateConf := &resource.StateChangeConf{
Target: []string{"ACTIVE"},
Refresh: networkingQoSBandwidthLimitRuleV2StateRefreshFunc(networkingClient, qosPolicyID, r.ID),
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: 5 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForStateContext(ctx)
if err != nil {
return diag.Errorf("Error waiting for openstack_networking_qos_bandwidth_limit_rule_v2 %s to become available: %s", r.ID, err)
}
id := resourceNetworkingQoSRuleV2BuildID(qosPolicyID, r.ID)
d.SetId(id)
log.Printf("[DEBUG] Created openstack_networking_qos_bandwidth_limit_rule_v2 %s: %#v", id, r)
return resourceNetworkingQoSBandwidthLimitRuleV2Read(ctx, d, meta)
}
func resourceNetworkingQoSBandwidthLimitRuleV2Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
networkingClient, err := config.NetworkingV2Client(GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating OpenStack networking client: %s", err)
}
qosPolicyID, qosRuleID, err := resourceNetworkingQoSRuleV2ParseID(d.Id())
if err != nil {
return diag.Errorf("Error reading openstack_networking_qos_bandwidth_limit_rule_v2 ID %s: %s", d.Id(), err)
}
r, err := rules.GetBandwidthLimitRule(networkingClient, qosPolicyID, qosRuleID).ExtractBandwidthLimitRule()
if err != nil {
return diag.FromErr(CheckDeleted(d, err, "Error getting openstack_networking_qos_bandwidth_limit_rule_v2"))
}
log.Printf("[DEBUG] Retrieved openstack_networking_qos_bandwidth_limit_rule_v2 %s: %#v", d.Id(), r)
d.Set("qos_policy_id", qosPolicyID)
d.Set("max_kbps", r.MaxKBps)
d.Set("max_burst_kbps", r.MaxBurstKBps)
d.Set("direction", r.Direction)
d.Set("region", GetRegion(d, config))
return nil
}
func resourceNetworkingQoSBandwidthLimitRuleV2Update(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
networkingClient, err := config.NetworkingV2Client(GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating OpenStack networking client: %s", err)
}
qosPolicyID, qosRuleID, err := resourceNetworkingQoSRuleV2ParseID(d.Id())
if err != nil {
return diag.Errorf("Error reading openstack_networking_qos_bandwidth_limit_rule_v2 ID %s: %s", d.Id(), err)
}
var hasChange bool
var updateOpts rules.UpdateBandwidthLimitRuleOpts
if d.HasChange("max_kbps") {
hasChange = true
maxKBps := d.Get("max_kbps").(int)
updateOpts.MaxKBps = &maxKBps
}
if d.HasChange("max_burst_kbps") {
hasChange = true
maxBurstKBps := d.Get("max_burst_kbps").(int)
updateOpts.MaxBurstKBps = &maxBurstKBps
}
if d.HasChange("direction") {
hasChange = true
updateOpts.Direction = d.Get("direction").(string)
}
if hasChange {
log.Printf("[DEBUG] openstack_networking_qos_bandwidth_limit_rule_v2 %s update options: %#v", d.Id(), updateOpts)
_, err = rules.UpdateBandwidthLimitRule(networkingClient, qosPolicyID, qosRuleID, updateOpts).ExtractBandwidthLimitRule()
if err != nil {
return diag.Errorf("Error updating openstack_networking_qos_bandwidth_limit_rule_v2 %s: %s", d.Id(), err)
}
}
return resourceNetworkingQoSBandwidthLimitRuleV2Read(ctx, d, meta)
}
func resourceNetworkingQoSBandwidthLimitRuleV2Delete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
networkingClient, err := config.NetworkingV2Client(GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating OpenStack networking client: %s", err)
}
qosPolicyID, qosRuleID, err := resourceNetworkingQoSRuleV2ParseID(d.Id())
if err != nil {
return diag.Errorf("Error reading openstack_networking_qos_bandwidth_limit_rule_v2 ID %s: %s", d.Id(), err)
}
if err := rules.DeleteBandwidthLimitRule(networkingClient, qosPolicyID, qosRuleID).ExtractErr(); err != nil {
return diag.FromErr(CheckDeleted(d, err, "Error getting openstack_networking_qos_bandwidth_limit_rule_v2"))
}
stateConf := &resource.StateChangeConf{
Pending: []string{"ACTIVE"},
Target: []string{"DELETED"},
Refresh: networkingQoSBandwidthLimitRuleV2StateRefreshFunc(networkingClient, qosPolicyID, d.Id()),
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: 5 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForStateContext(ctx)
if err != nil {
return diag.Errorf("Error waiting for openstack_networking_qos_bandwidth_limit_rule_v2 %s to Delete: %s", d.Id(), err)
}
return nil
}