Skip to content

Commit 8326b6f

Browse files
authored
Merge pull request #127 from yanndegat/iplb/vrack_support
Iplb/vrack support
2 parents 9257aff + 5c203ef commit 8326b6f

29 files changed

+1451
-217
lines changed

ovh/data_source_ovh_iploadbalancing.go

-21
Original file line numberDiff line numberDiff line change
@@ -136,27 +136,6 @@ func dataSourceIpLoadbalancing() *schema.Resource {
136136
}
137137
}
138138

139-
type IpLoadbalancing struct {
140-
IPv6 string `json:"ipv6,omitempty"`
141-
IPv4 string `json:"ipv4,omitempty"`
142-
MetricsToken string `json:"metricsToken,omitempty"`
143-
Zone []string `json:"zone"`
144-
Offer string `json:"offer"`
145-
ServiceName string `json:"serviceName"`
146-
IpLoadbalancing string `json:"ipLoadbalancing"`
147-
State string `json:"state"`
148-
OrderableZones []*IpLoadbalancingOrderableZone `json:"orderableZone"`
149-
VrackEligibility bool `json:"vrackEligibility"`
150-
VrackName string `json:"vrackName"`
151-
SslConfiguration string `json:"sslConfiguration"`
152-
DisplayName string `json:"displayName"`
153-
}
154-
155-
type IpLoadbalancingOrderableZone struct {
156-
Name string `json:"name"`
157-
PlanCode string `json:"plan_code"`
158-
}
159-
160139
func dataSourceIpLoadbalancingRead(d *schema.ResourceData, meta interface{}) error {
161140
config := meta.(*Config)
162141
log.Printf("[DEBUG] Will list available iploadbalancing services")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"net/url"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
8+
)
9+
10+
func dataSourceIpLoadbalancingVrackNetwork() *schema.Resource {
11+
return &schema.Resource{
12+
Read: dataSourceIpLoadbalancingVrackNetworkRead,
13+
Schema: map[string]*schema.Schema{
14+
"service_name": {
15+
Type: schema.TypeString,
16+
Description: "The internal name of your IPloadbalancer",
17+
Required: true,
18+
},
19+
20+
"vrack_network_id": {
21+
Type: schema.TypeInt,
22+
Description: "Internal Load Balancer identifier of the vRack private network",
23+
Required: true,
24+
},
25+
26+
//Computed
27+
"farm_id": {
28+
Type: schema.TypeList,
29+
Description: "Farm id your vRack network is attached to and their type",
30+
Computed: true,
31+
Elem: &schema.Resource{
32+
Schema: map[string]*schema.Schema{
33+
"type": {
34+
Type: schema.TypeString,
35+
Computed: true,
36+
},
37+
"id": {
38+
Type: schema.TypeInt,
39+
Computed: true,
40+
},
41+
},
42+
},
43+
},
44+
"display_name": {
45+
Type: schema.TypeString,
46+
Description: "Human readable name for your vrack network",
47+
Computed: true,
48+
},
49+
"nat_ip": {
50+
Type: schema.TypeString,
51+
Description: "An IP block used as a pool of IPs by this Load Balancer to connect to the servers in this private network. The blck must be in the private network and reserved for the Load Balancer",
52+
Computed: true,
53+
},
54+
"subnet": {
55+
Type: schema.TypeString,
56+
Description: "IP block of the private network in the vRack",
57+
Computed: true,
58+
},
59+
"vlan": {
60+
Type: schema.TypeInt,
61+
Description: "VLAN of the private network in the vRack. 0 if the private network is not in a VLAN",
62+
Computed: true,
63+
},
64+
},
65+
}
66+
}
67+
68+
func dataSourceIpLoadbalancingVrackNetworkRead(d *schema.ResourceData, meta interface{}) error {
69+
config := meta.(*Config)
70+
endpoint := fmt.Sprintf(
71+
"/ipLoadbalancing/%s/vrack/network/%d",
72+
url.PathEscape(d.Get("service_name").(string)),
73+
d.Get("vrack_network_id").(int),
74+
)
75+
76+
vn := &IpLoadbalancingVrackNetwork{}
77+
if err := config.OVHClient.Get(endpoint, &vn); err != nil {
78+
return fmt.Errorf("Error calling GET %s:\n\t %q", endpoint, err)
79+
}
80+
81+
// set resource attributes
82+
for k, v := range vn.ToMap() {
83+
d.Set(k, v)
84+
}
85+
86+
d.SetId(fmt.Sprintf("%s/%d", d.Get("service_name").(string), d.Get("vrack_network_id").(int)))
87+
return nil
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9+
)
10+
11+
func TestAccIpLoadbalancingVrackNetworkDataSource_basic(t *testing.T) {
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() { testAccPreCheckIpLoadbalancing(t) },
14+
Providers: testAccProviders,
15+
Steps: []resource.TestStep{
16+
{
17+
Config: testAccIpLoadbalancingVrackNetworkDatasourceConfig_basic,
18+
Check: resource.ComposeTestCheckFunc(
19+
resource.TestCheckResourceAttr("data.ovh_iploadbalancing.iplb", "vrack_eligibility", "true"),
20+
resource.TestCheckResourceAttr("data.ovh_iploadbalancing_vrack_network.network", "subnet", ipLoadbalancingVrackNetworkSubnet),
21+
resource.TestCheckResourceAttr("data.ovh_iploadbalancing_vrack_network.network", "vlan", ipLoadbalancingVrackNetworkVlan),
22+
resource.TestCheckResourceAttrSet("data.ovh_iploadbalancing_vrack_network.network", "id"),
23+
resource.TestCheckResourceAttr("data.ovh_iploadbalancing_vrack_network.network", "nat_ip", ipLoadbalancingVrackNetworkNatIp),
24+
resource.TestCheckResourceAttrSet("data.ovh_iploadbalancing_vrack_network.network", "farm_id.#"),
25+
),
26+
},
27+
},
28+
})
29+
}
30+
31+
var testAccIpLoadbalancingVrackNetworkDatasourceConfig_basic = fmt.Sprintf(`
32+
data ovh_iploadbalancing "iplb" {
33+
service_name = "%s"
34+
}
35+
36+
resource "ovh_vrack_iploadbalancing" "viplb" {
37+
service_name = "%s"
38+
ip_loadbalancing = data.ovh_iploadbalancing.iplb.service_name
39+
}
40+
41+
resource ovh_iploadbalancing_vrack_network "network" {
42+
service_name = ovh_vrack_iploadbalancing.viplb.ip_loadbalancing
43+
subnet = "%s"
44+
vlan = %s
45+
nat_ip = "%s"
46+
display_name = "terraform_testacc"
47+
}
48+
49+
data ovh_iploadbalancing_vrack_network "network" {
50+
service_name = data.ovh_iploadbalancing.iplb.service_name
51+
vrack_network_id = ovh_iploadbalancing_vrack_network.network.vrack_network_id
52+
}
53+
`,
54+
os.Getenv("OVH_IPLB_SERVICE"),
55+
os.Getenv("OVH_VRACK"),
56+
ipLoadbalancingVrackNetworkSubnet,
57+
ipLoadbalancingVrackNetworkVlan,
58+
ipLoadbalancingVrackNetworkNatIp,
59+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"net/url"
6+
"strconv"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
9+
)
10+
11+
func dataSourceIpLoadbalancingVrackNetworks() *schema.Resource {
12+
return &schema.Resource{
13+
Read: dataSourceIpLoadbalancingVrackNetworksRead,
14+
Schema: map[string]*schema.Schema{
15+
"service_name": {
16+
Type: schema.TypeString,
17+
Description: "The internal name of your iploadbalancer.",
18+
Required: true,
19+
},
20+
21+
"subnet": {
22+
Type: schema.TypeString,
23+
Description: "Filters on subnet",
24+
Optional: true,
25+
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
26+
err := validateIpBlock(v.(string))
27+
if err != nil {
28+
errors = append(errors, err)
29+
}
30+
return
31+
},
32+
},
33+
"vlan_id": {
34+
Type: schema.TypeInt,
35+
Description: "Filters on vlan id",
36+
Optional: true,
37+
},
38+
39+
"result": {
40+
Type: schema.TypeList,
41+
Computed: true,
42+
Elem: &schema.Schema{Type: schema.TypeInt},
43+
},
44+
},
45+
}
46+
}
47+
48+
func dataSourceIpLoadbalancingVrackNetworksRead(d *schema.ResourceData, meta interface{}) error {
49+
config := meta.(*Config)
50+
51+
result := make([]int64, 0)
52+
53+
serviceName := d.Get("service_name").(string)
54+
vlanId := ""
55+
subnet := ""
56+
filters := ""
57+
58+
endpoint := fmt.Sprintf(
59+
"/ipLoadbalancing/%s/vrack/network",
60+
url.PathEscape(serviceName),
61+
)
62+
63+
if val, ok := d.GetOkExists("vlan_id"); ok {
64+
vlanId = strconv.Itoa(val.(int))
65+
filters = fmt.Sprintf("%s&vlan=%s", filters, url.PathEscape(vlanId))
66+
}
67+
68+
if val, ok := d.GetOkExists("subnet"); ok {
69+
subnet = val.(string)
70+
filters = fmt.Sprintf("%s&subnet=%s", filters, url.PathEscape(subnet))
71+
}
72+
73+
// OVH IPLB API doens't parse the query string according to
74+
// the RFC and throws a 400 error with empty query string
75+
if filters != "" {
76+
endpoint = fmt.Sprintf("%s?%s", endpoint, filters)
77+
}
78+
79+
if err := config.OVHClient.Get(endpoint, &result); err != nil {
80+
return fmt.Errorf("Error calling GET %s:\n\t %q", endpoint, err)
81+
}
82+
83+
d.SetId(fmt.Sprintf("%s_%s_%s", serviceName, subnet, vlanId))
84+
d.Set("result", result)
85+
return nil
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9+
)
10+
11+
func TestAccIpLoadbalancingVrackNetworksDataSource_basic(t *testing.T) {
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() { testAccPreCheckIpLoadbalancing(t) },
14+
Providers: testAccProviders,
15+
Steps: []resource.TestStep{
16+
{
17+
Config: testAccIpLoadbalancingVrackNetworksDatasourceConfig_basic,
18+
Check: resource.ComposeTestCheckFunc(
19+
resource.TestCheckResourceAttr("data.ovh_iploadbalancing.iplb", "vrack_eligibility", "true"),
20+
resource.TestCheckResourceAttrSet("data.ovh_iploadbalancing_vrack_networks.networks", "result.#"),
21+
),
22+
},
23+
},
24+
})
25+
}
26+
27+
func TestAccIpLoadbalancingVrackNetworksDataSource_withFilters(t *testing.T) {
28+
resource.Test(t, resource.TestCase{
29+
PreCheck: func() { testAccPreCheckIpLoadbalancing(t) },
30+
Providers: testAccProviders,
31+
Steps: []resource.TestStep{
32+
{
33+
Config: testAccIpLoadbalancingVrackNetworksDatasourceConfig_withFilters,
34+
Check: resource.ComposeTestCheckFunc(
35+
resource.TestCheckResourceAttr("data.ovh_iploadbalancing.iplb", "vrack_eligibility", "true"),
36+
resource.TestCheckResourceAttrSet("data.ovh_iploadbalancing_vrack_networks.networks", "result.#"),
37+
),
38+
},
39+
},
40+
})
41+
}
42+
43+
var testAccIpLoadbalancingVrackNetworksDatasourceConfig_basic = fmt.Sprintf(`
44+
data ovh_iploadbalancing "iplb" {
45+
service_name = "%s"
46+
}
47+
48+
data ovh_iploadbalancing_vrack_networks "networks" {
49+
service_name = data.ovh_iploadbalancing.iplb.service_name
50+
subnet = "10.0.0.0/24"
51+
}
52+
`, os.Getenv("OVH_IPLB_SERVICE"))
53+
54+
var testAccIpLoadbalancingVrackNetworksDatasourceConfig_withFilters = fmt.Sprintf(`
55+
data ovh_iploadbalancing "iplb" {
56+
service_name = "%s"
57+
}
58+
59+
data ovh_iploadbalancing_vrack_networks "networks" {
60+
service_name = data.ovh_iploadbalancing.iplb.service_name
61+
subnet = "10.0.0.0/24"
62+
vlan_id = 0
63+
}
64+
`, os.Getenv("OVH_IPLB_SERVICE"))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
8+
"github.com/hashicorp/terraform-plugin-sdk/terraform"
9+
)
10+
11+
func TestAccIpLoadbalancingVrackNetwork_importBasic(t *testing.T) {
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() { testAccCheckVrackIpLoadbalancingPreCheck(t) },
14+
Providers: testAccProviders,
15+
Steps: []resource.TestStep{
16+
{
17+
Config: testAccIpLoadbalancingVrackNetworkConfig_basic,
18+
},
19+
{
20+
ResourceName: "ovh_iploadbalancing_vrack_network.network",
21+
ImportState: true,
22+
ImportStateVerify: true,
23+
ImportStateIdFunc: testAccIpLoadbalancingVrackNetworkImportId("ovh_iploadbalancing_vrack_network.network"),
24+
},
25+
},
26+
})
27+
}
28+
29+
func testAccIpLoadbalancingVrackNetworkImportId(resourceName string) resource.ImportStateIdFunc {
30+
return func(s *terraform.State) (string, error) {
31+
subnet, ok := s.RootModule().Resources[resourceName]
32+
if !ok {
33+
return "", fmt.Errorf("ovh_ip_loadbalancing_vrack_network not found: %s", resourceName)
34+
}
35+
36+
return fmt.Sprintf(
37+
"%s/%s",
38+
subnet.Primary.Attributes["service_name"],
39+
subnet.Primary.Attributes["vrack_network_id"],
40+
), nil
41+
}
42+
}

0 commit comments

Comments
 (0)