Skip to content

Commit

Permalink
GH-863: Fix how we fetch organizations by name in data source (#877)
Browse files Browse the repository at this point in the history
Fix how we fetch organizations by name in data source
  • Loading branch information
sergiught authored Nov 9, 2023
1 parent 9ea6ac3 commit 0ebbe3f
Show file tree
Hide file tree
Showing 4 changed files with 245 additions and 303 deletions.
53 changes: 17 additions & 36 deletions internal/auth0/organization/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,43 +70,10 @@ func dataSourceSchema() map[string]*schema.Schema {

func readOrganizationForDataSource(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
api := meta.(*config.Config).GetAPI()
var foundOrganization *management.Organization
var err error

organizationID := data.Get("organization_id").(string)
if organizationID != "" {
foundOrganization, err = api.Organization.Read(ctx, organizationID)
if err != nil {
return diag.FromErr(err)
}
} else {
name := data.Get("name").(string)
page := 0

outerLoop:
for {
organizations, err := api.Organization.List(ctx, management.Page(page), management.PerPage(100))
if err != nil {
return diag.FromErr(err)
}

for _, organization := range organizations.Organizations {
if organization.GetName() == name {
foundOrganization = organization
break outerLoop
}
}

if !organizations.HasNext() {
break
}

page++
}

if foundOrganization == nil {
return diag.Errorf("No organization found with \"name\" = %q", name)
}
foundOrganization, err := findOrganizationByIDOrName(ctx, data, api)
if err != nil {
return diag.FromErr(err)
}

data.SetId(foundOrganization.GetID())
Expand All @@ -124,6 +91,20 @@ func readOrganizationForDataSource(ctx context.Context, data *schema.ResourceDat
return diag.FromErr(flattenOrganizationForDataSource(data, foundOrganization, foundConnections, foundMembers))
}

func findOrganizationByIDOrName(
ctx context.Context,
data *schema.ResourceData,
api *management.Management,
) (*management.Organization, error) {
organizationID := data.Get("organization_id").(string)
if organizationID != "" {
return api.Organization.Read(ctx, organizationID)
}

organizationName := data.Get("name").(string)
return api.Organization.ReadByName(ctx, organizationName)
}

func fetchAllOrganizationConnections(ctx context.Context, api *management.Management, organizationID string) ([]*management.OrganizationConnection, error) {
var foundConnections []*management.OrganizationConnection
var page int
Expand Down
4 changes: 2 additions & 2 deletions internal/auth0/organization/data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ data "auth0_organization" "test" {
}
`

const testAccDataSourceOrganizationNonexistentID = `
const testAccDataSourceOrganizationNonExistentID = `
data "auth0_organization" "test" {
organization_id = "org_XXXXXXXXXXXXXXXX"
}
Expand All @@ -89,7 +89,7 @@ func TestAccDataSourceOrganization(t *testing.T) {
PreventPostDestroyRefresh: true,
Steps: []resource.TestStep{
{
Config: acctest.ParseTestName(testAccDataSourceOrganizationNonexistentID, t.Name()),
Config: acctest.ParseTestName(testAccDataSourceOrganizationNonExistentID, t.Name()),
ExpectError: regexp.MustCompile(
"404 Not Found: No organization found by that id or name",
),
Expand Down
Loading

0 comments on commit 0ebbe3f

Please sign in to comment.