Skip to content

Commit 0995d7a

Browse files
committed
feat: add d/vra_load_balancer
Adds support for `d/vra_load_balancer`. Signed-off-by: Ryan Johnson <ryan@tenthirtyam.org>
1 parent 111df8d commit 0995d7a

File tree

7 files changed

+427
-11
lines changed

7 files changed

+427
-11
lines changed

terraform-provider-vra

52.5 MB
Binary file not shown.

vra/data_source_load_balancer.go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// © Broadcom. All Rights Reserved.
2+
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
3+
// SPDX-License-Identifier: MPL-2.0
4+
5+
package vra
6+
7+
import (
8+
"context"
9+
"log"
10+
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13+
"github.com/vmware/vra-sdk-go/pkg/client/load_balancer"
14+
)
15+
16+
func dataSourceLoadBalancer() *schema.Resource {
17+
return &schema.Resource{
18+
ReadContext: dataSourceLoadBalancerRead,
19+
20+
Schema: map[string]*schema.Schema{
21+
"id": {
22+
Type: schema.TypeString,
23+
Required: true,
24+
},
25+
"name": {
26+
Type: schema.TypeString,
27+
Computed: true,
28+
},
29+
"nics": nicsSchema(false),
30+
"project_id": {
31+
Type: schema.TypeString,
32+
Computed: true,
33+
},
34+
"routes": routesSchema(false),
35+
"custom_properties": {
36+
Type: schema.TypeMap,
37+
Computed: true,
38+
},
39+
"description": {
40+
Type: schema.TypeString,
41+
Computed: true,
42+
},
43+
"deployment_id": {
44+
Type: schema.TypeString,
45+
Computed: true,
46+
},
47+
"internet_facing": {
48+
Type: schema.TypeBool,
49+
Computed: true,
50+
},
51+
"tags": tagsSchema(),
52+
"targets": LoadBalancerTargetSchema(),
53+
"address": {
54+
Type: schema.TypeString,
55+
Computed: true,
56+
},
57+
"created_at": {
58+
Type: schema.TypeString,
59+
Computed: true,
60+
},
61+
"external_id": {
62+
Type: schema.TypeString,
63+
Computed: true,
64+
},
65+
"external_region_id": {
66+
Type: schema.TypeString,
67+
Computed: true,
68+
},
69+
"external_zone_id": {
70+
Type: schema.TypeString,
71+
Computed: true,
72+
},
73+
"links": linksSchema(),
74+
"organization_id": {
75+
Type: schema.TypeString,
76+
Computed: true,
77+
},
78+
"owner": {
79+
Type: schema.TypeString,
80+
Computed: true,
81+
},
82+
"updated_at": {
83+
Type: schema.TypeString,
84+
Computed: true,
85+
},
86+
},
87+
}
88+
}
89+
90+
func dataSourceLoadBalancerRead(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
91+
apiClient := m.(*Client).apiClient
92+
93+
id := d.Get("id").(string)
94+
resp, err := apiClient.LoadBalancer.GetLoadBalancer(load_balancer.NewGetLoadBalancerParams().WithID(id))
95+
if err != nil {
96+
return diag.FromErr(err)
97+
}
98+
99+
loadBalancer := *resp.Payload
100+
d.SetId(id)
101+
d.Set("address", loadBalancer.Address)
102+
d.Set("created_at", loadBalancer.CreatedAt)
103+
d.Set("custom_properties", loadBalancer.CustomProperties)
104+
d.Set("description", loadBalancer.Description)
105+
d.Set("deployment_id", loadBalancer.DeploymentID)
106+
d.Set("external_id", loadBalancer.ExternalID)
107+
d.Set("external_region_id", loadBalancer.ExternalRegionID)
108+
d.Set("external_zone_id", loadBalancer.ExternalZoneID)
109+
d.Set("name", loadBalancer.Name)
110+
d.Set("organization_id", loadBalancer.OrgID)
111+
d.Set("owner", loadBalancer.Owner)
112+
d.Set("project_id", loadBalancer.ProjectID)
113+
d.Set("updated_at", loadBalancer.UpdatedAt)
114+
115+
if err := d.Set("tags", flattenTags(loadBalancer.Tags)); err != nil {
116+
return diag.Errorf("error setting load balancer tags - error: %v", err)
117+
}
118+
if err := d.Set("routes", flattenRoutes(loadBalancer.Routes)); err != nil {
119+
return diag.Errorf("error setting load balancer routes - error: %v", err)
120+
}
121+
122+
if err := d.Set("links", flattenLinks(loadBalancer.Links)); err != nil {
123+
return diag.Errorf("error setting load balancer links - error: %#v", err)
124+
}
125+
126+
log.Printf("Finished reading the vra_load_balancer data source with id %s", d.Get("id"))
127+
return nil
128+
}
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
// © Broadcom. All Rights Reserved.
2+
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
3+
// SPDX-License-Identifier: MPL-2.0
4+
5+
package vra
6+
7+
import (
8+
"fmt"
9+
"os"
10+
"regexp"
11+
"testing"
12+
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
14+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
15+
)
16+
17+
func TestAccVRALoadBalancerDataSource(t *testing.T) {
18+
rInt := acctest.RandInt()
19+
20+
resource.Test(t, resource.TestCase{
21+
PreCheck: func() { testAccPreCheckLoadBalancerDataSource(t) },
22+
Providers: testAccProviders,
23+
Steps: []resource.TestStep{
24+
{
25+
Config: testAccCheckVRALoadBalancerDataSourceConfig(rInt),
26+
Check: resource.ComposeAggregateTestCheckFunc(
27+
resource.TestCheckResourceAttr("data.vra_load_balancer.test", "name", fmt.Sprintf("my-lb-%d", rInt)),
28+
resource.TestMatchResourceAttr("data.vra_load_balancer.test", "id", regexp.MustCompile("^lb-")),
29+
resource.TestCheckResourceAttr("data.vra_load_balancer.test", "routes.#", "1"),
30+
resource.TestCheckResourceAttr("data.vra_load_balancer.test", "nics.#", "1"),
31+
resource.TestCheckResourceAttr("data.vra_load_balancer.test", "targets.#", "1"),
32+
),
33+
},
34+
},
35+
})
36+
}
37+
38+
func testAccCheckVRALoadBalancerDataSourceConfig(rInt int) string {
39+
return testAccCheckVRALoadBalancerDataSource(rInt) + fmt.Sprintf(`
40+
resource "vra_load_balancer" "test" {
41+
name = "my-lb-%d"
42+
project_id = vra_project.my-project.id
43+
description = "load balancer description"
44+
45+
targets {
46+
machine_id = vra_machine.my_machine.id
47+
}
48+
49+
nics {
50+
network_id = data.vra_network.my-network.id
51+
}
52+
53+
routes {
54+
protocol = "TCP"
55+
port = "80"
56+
member_protocol = "TCP"
57+
member_port = "80"
58+
health_check_configuration = {
59+
protocol = "TCP"
60+
port = "80"
61+
interval_seconds = 30
62+
timeout_seconds = 10
63+
unhealthy_threshold = 2
64+
healthy_threshold = 10
65+
}
66+
}
67+
}
68+
69+
data "vra_load_balancer" "test" {
70+
id = vra_load_balancer.test.id
71+
}
72+
`, rInt)
73+
}
74+
75+
func testAccPreCheckLoadBalancerDataSource(t *testing.T) {
76+
if os.Getenv("VRA_AWS_CLOUD_ACCOUNT_NAME") == "" {
77+
t.Fatal("VRA_AWS_CLOUD_ACCOUNT_NAME must be set for acceptance tests")
78+
}
79+
if os.Getenv("VRA_FABRIC_NETWORK") == "" {
80+
t.Fatal("VRA_FABRIC_NETWORK must be set for acceptance tests")
81+
}
82+
if os.Getenv("VRA_REGION") == "" {
83+
t.Fatal("VRA_REGION must be set for acceptance tests")
84+
}
85+
if os.Getenv("VRA_IMAGE") == "" {
86+
t.Fatal("VRA_IMAGE must be set for acceptance tests")
87+
}
88+
if os.Getenv("VRA_FLAVOR") == "" {
89+
t.Fatal("VRA_FLAVOR must be set for acceptance tests")
90+
}
91+
}
92+
93+
func testAccCheckVRALoadBalancerDataSource(rInt int) string {
94+
name := os.Getenv("VRA_AWS_CLOUD_ACCOUNT_NAME")
95+
fabricNetwork := os.Getenv("VRA_FABRIC_NETWORK")
96+
region := os.Getenv("VRA_REGION")
97+
image := os.Getenv("VRA_IMAGE")
98+
flavor := os.Getenv("VRA_FLAVOR")
99+
return fmt.Sprintf(`
100+
101+
data "vra_cloud_account_aws" "my-cloud-account" {
102+
name = "%s"
103+
}
104+
105+
data "vra_region" "my-region" {
106+
cloud_account_id = data.vra_cloud_account_aws.my-cloud-account.id
107+
region = "%s"
108+
}
109+
110+
resource "vra_zone" "my-zone" {
111+
name = "my-zone-%d"
112+
description = "description my-vra-zone"
113+
region_id = data.vra_region.my-region.id
114+
}
115+
116+
resource "vra_project" "my-project" {
117+
name = "my-project-%d"
118+
description = "test project"
119+
zone_assignments {
120+
zone_id = vra_zone.my-zone.id
121+
priority = 1
122+
max_instances = 2
123+
}
124+
}
125+
126+
data "vra_fabric_network" "subnet" {
127+
filter = "name eq '%s'"
128+
}
129+
130+
resource "vra_network_profile" "my-network-profile" {
131+
name = "my-network-profile-%d"
132+
description = "test network profile"
133+
region_id = data.vra_region.my-region.id
134+
135+
fabric_network_ids = [
136+
data.vra_fabric_network.subnet.id,
137+
]
138+
139+
isolation_type = "NONE"
140+
141+
tags {
142+
key = "foo"
143+
value = "bar"
144+
}
145+
}
146+
147+
data "vra_network" "my-network" {
148+
name = data.vra_fabric_network.subnet.name
149+
depends_on = [vra_network_profile.my-network-profile]
150+
}
151+
152+
resource "vra_image_profile" "my-image-profile" {
153+
name = "my-image-profile-%d"
154+
description = "test image profile"
155+
region_id = data.vra_region.my-region.id
156+
157+
image_mapping {
158+
name = "image"
159+
image_name = "%s"
160+
}
161+
}
162+
163+
resource "vra_flavor_profile" "my-flavor-profile" {
164+
name = "my-flavor-profile-%d"
165+
description = "my flavor"
166+
region_id = data.vra_region.my-region.id
167+
flavor_mapping {
168+
name = "flavor"
169+
instance_type = "%s"
170+
}
171+
}
172+
173+
resource "vra_machine" "my-machine" {
174+
name = "my-machine-%d"
175+
description = "test machine updated"
176+
project_id = vra_project.my-project.id
177+
image = "image"
178+
flavor = "flavor"
179+
180+
tags {
181+
key = "foo"
182+
value = "bar"
183+
}
184+
}
185+
`, name, region, rInt, rInt, fabricNetwork, rInt, rInt, image, rInt, flavor, rInt)
186+
}

vra/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ func Provider() *schema.Provider {
110110
"vra_fabric_storage_policy_vsphere": dataSourceFabricStoragePolicyVsphere(),
111111
"vra_image": dataSourceImage(),
112112
"vra_image_profile": dataSourceImageProfile(),
113+
"vra_load_balancer": dataSourceLoadBalancer(),
113114
"vra_machine": dataSourceMachine(),
114115
"vra_network": dataSourceNetwork(),
115116
"vra_network_domain": dataSourceNetworkDomain(),

vra/resource_load_balancer_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ func testAccCheckVRALoadBalancerConfig(rInt int) string {
252252

253253
return testAccCheckVRALoadBalancer(rInt) + fmt.Sprintf(`
254254
resource "vra_load_balancer" "my_load_balancer" {
255-
name = "my-lb-%d"
256-
project_id = vra_project.my-project.id
255+
name = "my-lb-%d"
256+
project_id = vra_project.my-project.id
257257
description = "load balancer description"
258258
259259
targets {
@@ -265,17 +265,17 @@ func testAccCheckVRALoadBalancerConfig(rInt int) string {
265265
}
266266
267267
routes {
268-
protocol = "TCP"
269-
port = "80"
268+
protocol = "TCP"
269+
port = "80"
270270
member_protocol = "TCP"
271-
member_port = "80"
271+
member_port = "80"
272272
health_check_configuration = {
273-
protocol = "TCP"
274-
port = "80"
275-
interval_seconds = 30
276-
timeout_seconds = 10
273+
protocol = "TCP"
274+
port = "80"
275+
interval_seconds = 30
276+
timeout_seconds = 10
277277
unhealthy_threshold = 2
278-
healthy_threshold = 10
278+
healthy_threshold = 10
279279
}
280280
}
281281
}`, rInt)

0 commit comments

Comments
 (0)