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

DXCDT-364: Add organization data source #475

Merged
merged 3 commits into from
Feb 10, 2023

Conversation

sergiught
Copy link
Contributor

@sergiught sergiught commented Feb 10, 2023

🔧 Changes

Adding support for organization data sources in this PR. They'll be available by quering based on organization_id or name. The data source will also additionally include connections compared to the organization resource.

📚 References

🔬 Testing

📝 Checklist

  • All new/changed/fixed functionality is covered by tests (or N/A)
  • I have added documentation for all new/changed functionality (or N/A)

@sergiught sergiught force-pushed the feature/DXCDT-364-org-data-source branch 3 times, most recently from e07db05 to b27042e Compare February 10, 2023 10:08
@sergiught sergiught marked this pull request as ready for review February 10, 2023 10:09
@sergiught sergiught requested a review from a team as a code owner February 10, 2023 10:09
@sergiught sergiught force-pushed the feature/DXCDT-364-org-data-source branch from b27042e to 926ca21 Compare February 10, 2023 10:17
Copy link
Contributor

@willvedd willvedd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally a good contribution but just two questions about performance regarding lookup by name and fetching all organization members.

Comment on lines 104 to 105
outerLoop:
for {
organizations, err := api.Organization.List(management.Page(page))
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)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it is ok to provide the ability to lookup by name. But given that some tenants have thousands of organizations, might we suggest to the end user to lookup by ID for most performant results? I can just see this loop taking a very long time and users not understanding why.

Comment on lines 139 to 157
foundConnections, err := fetchAllOrganizationConnections(api, foundOrganization.GetID())
if err != nil {
return diag.FromErr(err)
}

result = multierror.Append(
result,
data.Set("connections", flattenOrganizationConnections(foundConnections)),
)

foundMembers, err := fetchAllOrganizationMembers(api, foundOrganization.GetID())
if err != nil {
return diag.FromErr(err)
}

result = multierror.Append(
result,
data.Set("members", flattenOrganizationMembers(foundMembers)),
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any performance concerns about retrieving all connections and members? Connections should be a reasonable quality but it is possible for an organization to possess a large number of members.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe there's a limit on the API, they only return 1000 items at most.

@sergiught sergiught force-pushed the feature/DXCDT-364-org-data-source branch from 926ca21 to 7fc205c Compare February 10, 2023 15:19
Copy link
Contributor

@willvedd willvedd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm 👍

return diag.FromErr(err)
}
} else {
name := data.Get("name").(string)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to offer looking up the org by name, besides just by ID? Just curious.

Copy link
Contributor Author

@sergiught sergiught Feb 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily, we could consider removing it and allowing only lookup based on ID. But then it would be best to remove the functionality from all other data sources as well as there was a precedent set through the client data source.

Copy link
Contributor

@Widcket Widcket Feb 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this won't apply to all resources (only those that have unique names). Whereas in others it makes sense. If possible, consider having this for those resources only.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep indeed, for the resource server data source I'm using the identifier instead of the name, as that is not unique for those.

Copy link
Contributor

@Widcket Widcket Feb 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that if it turns out that customers actually need this functionality in other resources, it can always be added later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You raise a good point, I'll have to investigate the topic a bit deeper. For now I'll keep this as is in the PR however before we cut a release with all the data sources added we'll take a decision whether to only allow id's for the lookup or id's and names.

return diag.FromErr(result.ErrorOrNil())
}

func fetchAllOrganizationConnections(api *management.Management, organizationID string) ([]*management.OrganizationConnection, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future we can consider using a generic helper for the paging logic, which seems to be repeated in a number of places.

Copy link
Contributor Author

@sergiught sergiught Feb 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO a better place for this would be the Go SDK itself. It would be nice if it would provide functions like "ListAll()" to wrap that logic. Wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good idea!

@sergiught sergiught merged commit 3c6d3be into main Feb 10, 2023
@sergiught sergiught deleted the feature/DXCDT-364-org-data-source branch February 10, 2023 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants