-
Notifications
You must be signed in to change notification settings - Fork 131
/
data_iploadbalancing_vrack_networks.go
87 lines (73 loc) · 2.11 KB
/
data_iploadbalancing_vrack_networks.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
package ovh
import (
"fmt"
"net/url"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
)
func dataSourceIpLoadbalancingVrackNetworks() *schema.Resource {
return &schema.Resource{
Read: dataSourceIpLoadbalancingVrackNetworksRead,
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Description: "The internal name of your iploadbalancer.",
Required: true,
},
"subnet": {
Type: schema.TypeString,
Description: "Filters on subnet",
Optional: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
err := helpers.ValidateIpBlock(v.(string))
if err != nil {
errors = append(errors, err)
}
return
},
},
"vlan_id": {
Type: schema.TypeInt,
Description: "Filters on vlan id",
Optional: true,
},
"result": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeInt},
},
},
}
}
func dataSourceIpLoadbalancingVrackNetworksRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
result := make([]int64, 0)
serviceName := d.Get("service_name").(string)
vlanId := ""
subnet := ""
filters := ""
endpoint := fmt.Sprintf(
"/ipLoadbalancing/%s/vrack/network",
url.PathEscape(serviceName),
)
if val, ok := d.GetOkExists("vlan_id"); ok {
vlanId = strconv.Itoa(val.(int))
filters = fmt.Sprintf("%s&vlan=%s", filters, url.PathEscape(vlanId))
}
if val, ok := d.GetOkExists("subnet"); ok {
subnet = val.(string)
filters = fmt.Sprintf("%s&subnet=%s", filters, url.PathEscape(subnet))
}
// OVH IPLB API doens't parse the query string according to
// the RFC and throws a 400 error with empty query string
if filters != "" {
endpoint = fmt.Sprintf("%s?%s", endpoint, filters)
}
if err := config.OVHClient.Get(endpoint, &result); err != nil {
return fmt.Errorf("Error calling GET %s:\n\t %q", endpoint, err)
}
d.SetId(fmt.Sprintf("%s_%s_%s", serviceName, subnet, vlanId))
d.Set("result", result)
return nil
}