Skip to content
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

enhancement(lb_profiles): added name filter to get only one profile #4516

Merged
merged 1 commit into from
Apr 18, 2023
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
41 changes: 29 additions & 12 deletions ibm/service/vpc/data_source_ibm_is_lb_profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ import (
)

const (
isLbsProfiles = "lb_profiles"
isLbsProfiles = "lb_profiles"
isLbsProfileName = "name"
)

func DataSourceIBMISLbProfiles() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISLbProfilesRead,

Schema: map[string]*schema.Schema{

isLbsProfileName: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The load balancer profile name.",
},
isLbsProfiles: {
Type: schema.TypeList,
Description: "Collection of load balancer profile collectors",
Expand Down Expand Up @@ -79,19 +84,31 @@ func dataSourceIBMISLbProfilesRead(d *schema.ResourceData, meta interface{}) err

start := ""
allrecs := []vpcv1.LoadBalancerProfile{}
for {
listOptions := &vpcv1.ListLoadBalancerProfilesOptions{}
if start != "" {
listOptions.Start = &start
if lbprofilenameok, ok := d.GetOk(isLbsProfileName); ok {
lbprofilename := lbprofilenameok.(string)
getLoadBalancerProfileOptions := &vpcv1.GetLoadBalancerProfileOptions{
Name: &lbprofilename,
}
profileCollectors, response, err := sess.ListLoadBalancerProfiles(listOptions)
lbProfile, response, err := sess.GetLoadBalancerProfile(getLoadBalancerProfileOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Fetching Load Balancer Profiles for VPC %s\n%s", err, response)
return fmt.Errorf("[ERROR] Error Fetching Load Balancer Profile(%s) for VPC %s\n%s", lbprofilename, err, response)
}
start = flex.GetNext(profileCollectors.Next)
allrecs = append(allrecs, profileCollectors.Profiles...)
if start == "" {
break
allrecs = append(allrecs, *lbProfile)
} else {
for {
listOptions := &vpcv1.ListLoadBalancerProfilesOptions{}
if start != "" {
listOptions.Start = &start
}
profileCollectors, response, err := sess.ListLoadBalancerProfiles(listOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Fetching Load Balancer Profiles for VPC %s\n%s", err, response)
}
start = flex.GetNext(profileCollectors.Next)
allrecs = append(allrecs, profileCollectors.Profiles...)
if start == "" {
break
}
}
}
lbprofilesInfo := make([]map[string]interface{}, 0)
Expand Down
25 changes: 25 additions & 0 deletions ibm/service/vpc/data_source_ibm_is_lb_profiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@ func TestAccIBMISLBProfilesDatasource_basic(t *testing.T) {
},
})
}
func TestAccIBMISLBProfilesDatasource_filter(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{

Config: testDSCheckIBMISLBProfilesFilterConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.ibm_is_lb_profiles.test_profiles", "lb_profiles.0.name", "network-fixed"),
resource.TestCheckResourceAttr("data.ibm_is_lb_profiles.test_profiles", "lb_profiles.0.family", "Network"),
resource.TestCheckResourceAttr("data.ibm_is_lb_profiles.test_profiles", "lb_profiles.0.route_mode_supported", "true"),
resource.TestCheckResourceAttrSet("data.ibm_is_lb_profiles.test_profiles", "lb_profiles.0.href"),
resource.TestCheckResourceAttrSet("data.ibm_is_lb_profiles.test_profiles", "lb_profiles.0.udp_supported"),
),
},
},
})
}
func testDSCheckIBMISLBProfilesConfig(vpcname, subnetname, zone, cidr, name string) string {
return fmt.Sprintf(`
resource "ibm_is_vpc" "testacc_vpc" {
Expand All @@ -51,3 +70,9 @@ resource "ibm_is_lb" "testacc_lb" {
data "ibm_is_lb_profiles" "test_profiles" {
} `, vpcname, subnetname, zone, cidr, name)
}
func testDSCheckIBMISLBProfilesFilterConfig() string {
return fmt.Sprintf(`
data "ibm_is_lb_profiles" "test_profiles" {
name = "network-fixed"
} `)
}
5 changes: 5 additions & 0 deletions website/docs/d/is_lb_profiles.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ data "ibm_is_lb_profiles" "example" {

```

## Argument reference
Review the argument references that you can specify for your data source.

- `name` - (Optional, String) The name of the load balancer profile. This will fetch only one profile if it exists with the `name` and profile can be accessed using `data.ibm_is_lb_profiles.profile.lb_profiles.0`

## Attribute reference
You can access the following attribute references after your data source is created.

Expand Down