-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_source_viettelidc_networking_qos_bandwidth_limit_rule_v2.go
105 lines (84 loc) · 2.7 KB
/
data_source_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
package viettelidc
import (
"context"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/qos/rules"
)
func dataSourceNetworkingQoSBandwidthLimitRuleV2() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceNetworkingQoSBandwidthLimitRuleV2Read,
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,
Optional: true,
Computed: true,
ForceNew: false,
},
"max_burst_kbps": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: false,
},
"direction": {
Type: schema.TypeString,
Computed: true,
ForceNew: false,
},
},
}
}
func dataSourceNetworkingQoSBandwidthLimitRuleV2Read(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)
}
listOpts := rules.BandwidthLimitRulesListOpts{}
if v, ok := d.GetOk("max_kbps"); ok {
listOpts.MaxKBps = v.(int)
}
if v, ok := d.GetOk("max_burst_kbps"); ok {
listOpts.MaxBurstKBps = v.(int)
}
qosPolicyID := d.Get("qos_policy_id").(string)
pages, err := rules.ListBandwidthLimitRules(networkingClient, qosPolicyID, listOpts).AllPages()
if err != nil {
return diag.Errorf("Unable to retrieve openstack_networking_qos_bandwidth_limit_rule_v2: %s", err)
}
allRules, err := rules.ExtractBandwidthLimitRules(pages)
if err != nil {
return diag.Errorf("Unable to extract openstack_networking_qos_bandwidth_limit_rule_v2: %s", err)
}
if len(allRules) < 1 {
return diag.Errorf("Your query returned no openstack_networking_qos_bandwidth_limit_rule_v2. " +
"Please change your search criteria and try again.")
}
if len(allRules) > 1 {
return diag.Errorf("Your query returned more than one openstack_networking_qos_bandwidth_limit_rule_v2." +
" Please try a more specific search criteria")
}
rule := allRules[0]
id := resourceNetworkingQoSRuleV2BuildID(qosPolicyID, rule.ID)
log.Printf("[DEBUG] Retrieved openstack_networking_qos_bandwidth_limit_rule_v2 %s: %+v", id, rule)
d.SetId(id)
d.Set("qos_policy_id", qosPolicyID)
d.Set("max_kbps", rule.MaxKBps)
d.Set("max_burst_kbps", rule.MaxBurstKBps)
d.Set("direction", rule.Direction)
d.Set("region", GetRegion(d, config))
return nil
}