Skip to content

Commit

Permalink
fix: List datasources throws error when list is empty (#103)
Browse files Browse the repository at this point in the history
* update domains datasource

* updating spaces data source

* updating org quotas

* updating org roles

* updating space roles

* updating space quotas

* updating stacks datasource

* updating list data sources

* Rerecorded data sources tests

* rerecorded space quotas fixture

* Rerecorded service_route_bindings

---------

Signed-off-by: Ray <[email protected]>
Co-authored-by: Debaditya <[email protected]>
  • Loading branch information
ANUGRAHG and Dray56 authored Jan 10, 2025
1 parent 0f275ef commit 5943ae9
Show file tree
Hide file tree
Showing 58 changed files with 6,437 additions and 7,584 deletions.
8 changes: 0 additions & 8 deletions internal/provider/datasource_apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,6 @@ func (d *appsDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
return
}

if len(apps) == 0 {
resp.Diagnostics.AddError(
"Unable to find any app in list",
"No app present with mentioned criteria",
)
return
}

appsList := []DatasourceAppType{}
for _, app := range apps {

Expand Down
8 changes: 5 additions & 3 deletions internal/provider/datasource_apps_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package provider

import (
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand All @@ -24,7 +23,7 @@ func TestAppsDataSource_Configure(t *testing.T) {
space = "02c0cc92-6ecc-44b1-b7b2-096ca19ee143"
}`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "apps.#", "8"),
resource.TestCheckResourceAttr(resourceName, "apps.#", "9"),
),
},
{
Expand All @@ -42,6 +41,7 @@ func TestAppsDataSource_Configure(t *testing.T) {
})
t.Run("error path - get unavailable apps", func(t *testing.T) {
cfg := getCFHomeConf()
resourceName := "data.cloudfoundry_apps.apps"
rec := cfg.SetupVCR(t, "fixtures/datasource_apps_invalid")
defer stopQuietly(rec)
resource.Test(t, resource.TestCase{
Expand All @@ -54,7 +54,9 @@ func TestAppsDataSource_Configure(t *testing.T) {
space = "02c0cc92-6ecc-44b1-b7b2-096ca19ee143"
name = "blah"
}`,
ExpectError: regexp.MustCompile(`Unable to find any app in list`),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "apps.#", "0"),
),
},
},
})
Expand Down
8 changes: 0 additions & 8 deletions internal/provider/datasource_buildpacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,6 @@ func (d *buildpacksDataSource) Read(ctx context.Context, req datasource.ReadRequ
return
}

if len(buildpacks) == 0 {
resp.Diagnostics.AddError(
"Unable to find any buildpack in the list",
"No buildpack present with mentioned criteria",
)
return
}

data.Buildpacks, diags = mapBuildpacksValuesToType(ctx, buildpacks)
resp.Diagnostics.Append(diags...)

Expand Down
5 changes: 3 additions & 2 deletions internal/provider/datasource_buildpacks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package provider

import (
"bytes"
"regexp"
"testing"
"text/template"

Expand Down Expand Up @@ -64,7 +63,9 @@ func TestBuildpacksDataSource_Configure(t *testing.T) {
HclObjectName: "ds",
Name: &invalidOrgGUID,
}),
ExpectError: regexp.MustCompile(`Unable to find any buildpack in the list`),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "buildpacks.#", "0"),
),
},
},
})
Expand Down
8 changes: 0 additions & 8 deletions internal/provider/datasource_domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,6 @@ func (d *DomainsDataSource) Read(ctx context.Context, req datasource.ReadRequest
return
}

if len(domains) == 0 {
resp.Diagnostics.AddError(
"Unable to find any domain in the list",
"No domain present with mentioned criteria",
)
return
}

data.Domains, diags = mapDomainsValuesToType(ctx, domains)
resp.Diagnostics.Append(diags...)

Expand Down
8 changes: 0 additions & 8 deletions internal/provider/datasource_isolation_segments.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,6 @@ func (d *IsolationSegmentsDataSource) Read(ctx context.Context, req datasource.R
return
}

if len(isolationSegments) == 0 {
resp.Diagnostics.AddError(
"Unable to find any Isolation Segment in given list",
"No isolation segment found with given criteria",
)
return
}

data.IsolationSegments, diags = mapIsolationSegmentsValuesToType(ctx, isolationSegments)
resp.Diagnostics.Append(diags...)

Expand Down
5 changes: 3 additions & 2 deletions internal/provider/datasource_isolation_segments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package provider

import (
"bytes"
"regexp"
"testing"
"text/template"

Expand Down Expand Up @@ -85,7 +84,9 @@ func TestIsolationSegmentsDataSource_Configure(t *testing.T) {
HclObjectName: "ds",
Name: strtostrptr("testunavailable"),
}),
ExpectError: regexp.MustCompile(`Unable to find any Isolation Segment in given list`),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "isolation_segments.#", "0"),
),
},
},
})
Expand Down
7 changes: 0 additions & 7 deletions internal/provider/datasource_org_quotas.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,6 @@ func (d *OrgQuotasDataSource) Read(ctx context.Context, req datasource.ReadReque
)
return
}
if len(orgQuotas) == 0 {
resp.Diagnostics.AddError(
"Unable to find any org quota",
fmt.Sprintf("No org quota present under org %s with mentioned criteria", data.Org.ValueString()),
)
return
}

data.OrgQuotas, diags = mapOrgQuotasValuesToType(orgQuotas)
resp.Diagnostics.Append(diags...)
Expand Down
8 changes: 0 additions & 8 deletions internal/provider/datasource_org_roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,6 @@ func (d *OrgRolesDataSource) Read(ctx context.Context, req datasource.ReadReques
return
}

if len(roles) == 0 {
resp.Diagnostics.AddError(
"Unable to find any roles in list",
fmt.Sprintf("No roles present under org %s with mentioned criteria", data.Org.ValueString()),
)
return
}

data.Roles = mapOrgRolesValuesToType(roles)

tflog.Trace(ctx, "read a org roles data source")
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/datasource_org_roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestOrgRolesDataSource_Configure(t *testing.T) {
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "org", testOrgGuid),
resource.TestCheckResourceAttr(dataSourceName, "roles.#", "2")),
resource.TestCheckResourceAttr(dataSourceName, "roles.#", "5")),
},
{
Config: hclProvider(nil) + hclOrgRolesDataSource(&OrgRolesModelPtr{
Expand All @@ -81,7 +81,7 @@ func TestOrgRolesDataSource_Configure(t *testing.T) {
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "org", testOrgGuid),
resource.TestCheckResourceAttr(dataSourceName, "roles.#", "1")),
resource.TestCheckResourceAttr(dataSourceName, "roles.#", "2")),
},
},
})
Expand Down
8 changes: 0 additions & 8 deletions internal/provider/datasource_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,6 @@ func (d *RoutesDataSource) Read(ctx context.Context, req datasource.ReadRequest,
return
}

if len(routes) == 0 {
resp.Diagnostics.AddError(
"Unable to find route in list",
"Given criteria does not have any associated routes present",
)
return
}

data, diags = mapRoutesValuesToType(ctx, data, routes)
resp.Diagnostics.Append(diags...)

Expand Down
7 changes: 4 additions & 3 deletions internal/provider/datasource_routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package provider

import (
"bytes"
"regexp"
"testing"
"text/template"

Expand Down Expand Up @@ -69,7 +68,7 @@ func TestRoutesDataSource_Configure(t *testing.T) {
Space: strtostrptr(testSpaceRouteGUID),
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "routes.#", "23"),
resource.TestCheckResourceAttr(dataSourceName, "routes.#", "24"),
),
},
},
Expand All @@ -91,7 +90,9 @@ func TestRoutesDataSource_Configure(t *testing.T) {
Domain: strtostrptr(testDomainRouteGUID),
Space: strtostrptr(testSpaceRouteGUID),
}),
ExpectError: regexp.MustCompile(`Unable to find route in list`),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "routes.#", "0"),
),
},
},
})
Expand Down
8 changes: 0 additions & 8 deletions internal/provider/datasource_security_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,6 @@ func (d *SecurityGroupsDataSource) Read(ctx context.Context, req datasource.Read
return
}

if len(securityGroups) == 0 {
resp.Diagnostics.AddError(
"Unable to find any security group in list",
"Given criteria does not return any security group",
)
return
}

data.SecurityGroups, diags = mapDataSourceSecurityGroupsValuesToType(ctx, securityGroups)
resp.Diagnostics.Append(diags...)

Expand Down
5 changes: 3 additions & 2 deletions internal/provider/datasource_security_groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package provider

import (
"bytes"
"regexp"
"testing"
"text/template"

Expand Down Expand Up @@ -101,7 +100,9 @@ func TestSecurityGroupsDataSource_Configure(t *testing.T) {
HclObjectName: "ds",
Name: strtostrptr(invalidOrgGUID),
}),
ExpectError: regexp.MustCompile(`Unable to find any security group in list`),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "security_groups.#", "0"),
),
},
},
})
Expand Down
7 changes: 0 additions & 7 deletions internal/provider/datasource_service_brokers.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,6 @@ func (d *ServiceBrokersDataSource) Read(ctx context.Context, req datasource.Read
)
return
}
if len(svcBrokers) == 0 {
resp.Diagnostics.AddError(
"Unable to find any service broker in list",
"No service brokers present with mentioned criteria",
)
return
}

data.ServiceBrokers, diags = mapDataSourceServiceBrokersValuesToType(ctx, svcBrokers)
resp.Diagnostics.Append(diags...)
Expand Down
6 changes: 4 additions & 2 deletions internal/provider/datasource_service_brokers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package provider

import (
"bytes"
"regexp"
"testing"
"text/template"

Expand Down Expand Up @@ -81,6 +80,7 @@ func TestServiceBrokersDataSource(t *testing.T) {
t.Run("error path - get unavailable service brokers", func(t *testing.T) {
cfg := getCFHomeConf()
rec := cfg.SetupVCR(t, "fixtures/datasource_service_brokers_invalid")
dataSourceName := "data.cloudfoundry_service_brokers.ds"
defer stopQuietly(rec)
// Create a Terraform configuration that uses the data source
// and run `terraform apply`. The data source should not be found.
Expand All @@ -94,7 +94,9 @@ func TestServiceBrokersDataSource(t *testing.T) {
HclObjectName: "ds",
Name: strtostrptr("invalid-service-broker-name"),
}),
ExpectError: regexp.MustCompile(`Unable to find any service broker in list`),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "service_brokers.#", "0"),
),
},
},
})
Expand Down
7 changes: 0 additions & 7 deletions internal/provider/datasource_service_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,6 @@ func (d *ServiceInstancesDataSource) Read(ctx context.Context, req datasource.Re
)
return
}
if len(svcInstances) == 0 {
resp.Diagnostics.AddError(
"Unable to find any service instance in list",
"No service instance present with mentioned criteria",
)
return
}

data.ServiceInstances, diags = mapDataSourceServiceInstancesValuesToType(ctx, svcInstances)
resp.Diagnostics.Append(diags...)
Expand Down
9 changes: 6 additions & 3 deletions internal/provider/datasource_service_instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestServiceInstancesDataSource(t *testing.T) {
Space: &testSpaceGUID,
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "service_instances.#", "13"),
resource.TestCheckResourceAttr(dataSourceName, "service_instances.#", "14"),
),
},
{
Expand All @@ -83,7 +83,7 @@ func TestServiceInstancesDataSource(t *testing.T) {
Name: &testServiceInstance,
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "service_instances.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "service_instances.#", "2"),
),
},
},
Expand All @@ -92,6 +92,7 @@ func TestServiceInstancesDataSource(t *testing.T) {
t.Run("error path - get unavailable service instance", func(t *testing.T) {
cfg := getCFHomeConf()
rec := cfg.SetupVCR(t, "fixtures/datasource_service_instances_invalid")
dataSourceName := "data.cloudfoundry_service_instances.ds"
defer stopQuietly(rec)
// Create a Terraform configuration that uses the data source
// and run `terraform apply`. The data source should not be found.
Expand All @@ -114,7 +115,9 @@ func TestServiceInstancesDataSource(t *testing.T) {
Org: &testOrg2GUID,
Name: strtostrptr("hi"),
}),
ExpectError: regexp.MustCompile(`Unable to find any service instance in list`),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "service_instances.#", "0"),
),
},
},
})
Expand Down
7 changes: 0 additions & 7 deletions internal/provider/datasource_service_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,6 @@ func (d *ServicePlansDataSource) Read(ctx context.Context, req datasource.ReadRe
)
return
}
if len(svcPlans) == 0 {
resp.Diagnostics.AddError(
"Unable to find any service plans in the list",
"Check the filter criteria again",
)
return
}

data.ServicePlans, diags = mapServicePlansValuesToListType(ctx, svcPlans)
resp.Diagnostics.Append(diags...)
Expand Down
5 changes: 3 additions & 2 deletions internal/provider/datasource_service_plans_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package provider

import (
"bytes"
"regexp"
"testing"
"text/template"

Expand Down Expand Up @@ -69,7 +68,9 @@ func TestDatasourceServicePlans(t *testing.T) {
HclObjectName: "test",
Name: strtostrptr("invalid-service-name"),
}),
ExpectError: regexp.MustCompile(`Unable to find any service plans in the list`),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(datasourceName, "service_plans.#", "0"),
),
},
},
})
Expand Down
8 changes: 0 additions & 8 deletions internal/provider/datasource_service_route_bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,6 @@ func (d *ServiceRouteBindingsDataSource) Read(ctx context.Context, req datasourc
return
}

if len(svcRouteBindings) == 0 {
resp.Diagnostics.AddError(
"Unable to find any route bindings in list",
"Given input does not have any bindings present",
)
return
}

data.RouteBindings, diags = mapDataSourceServiceRouteBindingsValuesToType(ctx, svcRouteBindings)
resp.Diagnostics.Append(diags...)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
Expand Down
Loading

0 comments on commit 5943ae9

Please sign in to comment.