Skip to content

feat(lb): filter LBs with tags on datasource #2473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/data-sources/lbs.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@ data "scaleway_lbs" "my_key" {
name = "foobar"
zone = "fr-par-2"
}

# Find LBs that share the same tags
data "scaleway_lbs" "lbs_by_tags" {
tags = [ "a tag" ]
}
```

## Argument Reference

- `name` - (Optional) The load balancer name used as a filter. LBs with a name like it are listed.

- `tags` - (Optional) List of tags used as filter. LBs with these exact tags are listed.

- `zone` - (Defaults to [provider](../index.md#zone) `zone`) The [zone](../guides/regions_and_zones.md#zones) in which LBs exist.

## Attributes Reference
Expand Down
9 changes: 9 additions & 0 deletions scaleway/data_source_lbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ func DataSourceScalewayLbs() *schema.Resource {
Optional: true,
Description: "LBs with a name like it are listed.",
},
"tags": {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Description: "LBs with these exact tags are listed",
},
"lbs": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -162,6 +170,7 @@ func dataSourceScalewayLbsRead(ctx context.Context, d *schema.ResourceData, m in
Zone: zone,
Name: types.ExpandStringPtr(d.Get("name")),
ProjectID: types.ExpandStringPtr(d.Get("project_id")),
Tags: types.ExpandStrings(d.Get("tags")),
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
Expand Down
13 changes: 13 additions & 0 deletions scaleway/data_source_lbs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestAccScalewayDataSourceLbs_Basic(t *testing.T) {
name = "tf-lb-datasource0"
description = "a description"
type = "LB-S"
tags = [ "terraform-test", "data_scaleway_lbs", "basic" ]
}
`,
},
Expand All @@ -52,13 +53,15 @@ func TestAccScalewayDataSourceLbs_Basic(t *testing.T) {
name = "tf-lb-datasource0"
description = "a description"
type = "LB-S"
tags = [ "terraform-test", "data_scaleway_lbs", "basic" ]
}

resource scaleway_lb lb2 {
ip_id = scaleway_lb_ip.ip2.id
name = "tf-lb-datasource1"
description = "a description"
type = "LB-S"
tags = [ "terraform-test", "data_scaleway_lbs", "basic" ]
}
`,
},
Expand All @@ -75,20 +78,27 @@ func TestAccScalewayDataSourceLbs_Basic(t *testing.T) {
name = "tf-lb-datasource0"
description = "a description"
type = "LB-S"
tags = [ "terraform-test", "data_scaleway_lbs", "basic" ]
}

resource scaleway_lb lb2 {
ip_id = scaleway_lb_ip.ip2.id
name = "tf-lb-datasource1"
description = "a description"
type = "LB-S"
tags = [ "terraform-test", "data_scaleway_lbs", "basic" ]
}

data "scaleway_lbs" "lbs_by_name" {
name = "tf-lb-datasource"
depends_on = [scaleway_lb.lb1, scaleway_lb.lb2]
}

data "scaleway_lbs" "lbs_by_tags" {
tags = [ "terraform-test", "data_scaleway_lbs" ]
depends_on = [scaleway_lb.lb1, scaleway_lb.lb2]
}

data "scaleway_lbs" "lbs_by_name_other_zone" {
name = "tf-lb-datasource"
zone = "fr-par-2"
Expand All @@ -101,6 +111,9 @@ func TestAccScalewayDataSourceLbs_Basic(t *testing.T) {
resource.TestCheckResourceAttrSet("data.scaleway_lbs.lbs_by_name", "lbs.0.name"),
resource.TestCheckResourceAttrSet("data.scaleway_lbs.lbs_by_name", "lbs.1.name"),

resource.TestCheckResourceAttrSet("data.scaleway_lbs.lbs_by_tags", "lbs.0.id"),
resource.TestCheckResourceAttrSet("data.scaleway_lbs.lbs_by_tags", "lbs.1.id"),

resource.TestCheckNoResourceAttr("data.scaleway_lbs.lbs_by_name_other_zone", "lbs.0.id"),
),
},
Expand Down
Loading