Skip to content

Commit

Permalink
Add members to org data source
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Jun 7, 2023
1 parent 02ff387 commit ed70341
Show file tree
Hide file tree
Showing 8 changed files with 6,824 additions and 1,179 deletions.
1 change: 1 addition & 0 deletions docs/data-sources/organization.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ data "auth0_organization" "some-organization-by-id" {
- `connections` (Set of Object) (see [below for nested schema](#nestedatt--connections))
- `display_name` (String) Friendly name of this organization.
- `id` (String) The ID of this resource.
- `members` (Set of String) User ID(s) that are members of the organization.
- `metadata` (Map of String) Metadata associated with the organization. Maximum of 10 metadata properties allowed.

<a id="nestedatt--branding"></a>
Expand Down
53 changes: 44 additions & 9 deletions internal/auth0/organization/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ func dataSourceSchema() map[string]*schema.Schema {
},
}

dataSourceSchema["members"] = &schema.Schema{
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Computed: true,
Description: "User ID(s) that are members of the organization.",
}

return dataSourceSchema
}

Expand Down Expand Up @@ -108,21 +117,23 @@ func readOrganizationForDataSource(_ context.Context, data *schema.ResourceData,

data.SetId(foundOrganization.GetID())

result := multierror.Append(
data.Set("name", foundOrganization.GetName()),
data.Set("display_name", foundOrganization.GetDisplayName()),
data.Set("branding", flattenOrganizationBranding(foundOrganization.GetBranding())),
data.Set("metadata", foundOrganization.GetMetadata()),
)

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

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

result := multierror.Append(
data.Set("name", foundOrganization.GetName()),
data.Set("display_name", foundOrganization.GetDisplayName()),
data.Set("branding", flattenOrganizationBranding(foundOrganization.GetBranding())),
data.Set("metadata", foundOrganization.GetMetadata()),
data.Set("connections", flattenOrganizationConnections(foundConnections)),
data.Set("members", foundMembers),
)

return diag.FromErr(result.ErrorOrNil())
Expand Down Expand Up @@ -150,6 +161,30 @@ func fetchAllOrganizationConnections(api *management.Management, organizationID
return foundConnections, nil
}

func fetchAllOrganizationMembers(api *management.Management, organizationID string) ([]string, error) {
foundMembers := make([]string, 0)
var page int

for {
members, err := api.Organization.Members(organizationID, management.Page(page), management.PerPage(100))
if err != nil {
return nil, err
}

for _, member := range members.Members {
foundMembers = append(foundMembers, member.GetUserID())
}

if !members.HasNext() {
break
}

page++
}

return foundMembers, nil
}

func flattenOrganizationConnections(connections []*management.OrganizationConnection) []interface{} {
if connections == nil {
return nil
Expand Down
17 changes: 17 additions & 0 deletions internal/auth0/organization/data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ import (
)

const testAccGivenAnOrganizationWithConnectionsAndMembers = `
resource "auth0_user" "user" {
connection_name = "Username-Password-Authentication"
email = "{{.testName}}@auth0.com"
password = "MyPass123$"
}
resource "auth0_connection" "my_connection" {
depends_on = [ auth0_user.user ]
name = "Acceptance-Test-Connection-{{.testName}}"
strategy = "auth0"
}
Expand All @@ -30,6 +38,13 @@ resource "auth0_organization_connection" "my_org_conn" {
organization_id = auth0_organization.my_organization.id
connection_id = auth0_connection.my_connection.id
}
resource "auth0_organization_member" "org_member" {
depends_on = [ auth0_organization_connection.my_org_conn ]
organization_id = auth0_organization.my_organization.id
user_id = auth0_user.user.id
}
`

const testAccDataSourceOrganizationConfigByName = testAccGivenAnOrganizationWithConnectionsAndMembers + `
Expand Down Expand Up @@ -80,6 +95,7 @@ func TestAccDataSourceOrganizationByName(t *testing.T) {
resource.TestCheckResourceAttr("data.auth0_organization.test", "name", fmt.Sprintf("test-%s", testName)),
resource.TestCheckResourceAttr("data.auth0_organization.test", "connections.#", "1"),
resource.TestCheckResourceAttrSet("data.auth0_organization.test", "connections.0.connection_id"),
resource.TestCheckResourceAttr("data.auth0_organization.test", "members.#", "1"),
),
},
},
Expand Down Expand Up @@ -110,6 +126,7 @@ func TestAccDataSourceOrganizationByID(t *testing.T) {
resource.TestCheckResourceAttr("data.auth0_organization.test", "name", fmt.Sprintf("test-%s", testName)),
resource.TestCheckResourceAttr("data.auth0_organization.test", "connections.#", "1"),
resource.TestCheckResourceAttrSet("data.auth0_organization.test", "connections.0.connection_id"),
resource.TestCheckResourceAttr("data.auth0_organization.test", "members.#", "1"),
),
},
},
Expand Down
50 changes: 50 additions & 0 deletions internal/auth0/organization/resource_members_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ resource "auth0_organization_members" "my_members" {
organization_id = auth0_organization.my_org.id
members = [ auth0_user.user_1.id ]
}
data "auth0_organization" "my_org_data" {
depends_on = [ auth0_organization_members.my_members ]
organization_id = auth0_organization.my_org.id
}
`

const testAccOrganizationMembersWithTwoMembers = `
Expand Down Expand Up @@ -97,6 +103,12 @@ resource "auth0_organization_members" "my_members" {
organization_id = auth0_organization.my_org.id
members = [ auth0_user.user_1.id, auth0_user.user_2.id ]
}
data "auth0_organization" "my_org_data" {
depends_on = [ auth0_organization_members.my_members ]
organization_id = auth0_organization.my_org.id
}
`

const testAccOrganizationMembersRemoveOneMember = `
Expand All @@ -119,6 +131,12 @@ resource "auth0_organization_members" "my_members" {
organization_id = auth0_organization.my_org.id
members = [ auth0_user.user_2.id ]
}
data "auth0_organization" "my_org_data" {
depends_on = [ auth0_organization_members.my_members ]
organization_id = auth0_organization.my_org.id
}
`

const testAccOrganizationMembersRemoveAllMembers = `
Expand All @@ -133,6 +151,25 @@ resource "auth0_organization_members" "my_members" {
organization_id = auth0_organization.my_org.id
members = []
}
data "auth0_organization" "my_org_data" {
depends_on = [ auth0_organization_members.my_members ]
organization_id = auth0_organization.my_org.id
}
`

const testAccOrganizationMembersDeleteResource = `
resource "auth0_organization" "my_org" {
name = "some-org-{{.testName}}"
display_name = "{{.testName}}"
}
data "auth0_organization" "my_org_data" {
depends_on = [ auth0_organization.my_org ]
organization_id = auth0_organization.my_org.id
}
`

func TestAccOrganizationMembers(t *testing.T) {
Expand All @@ -148,24 +185,37 @@ func TestAccOrganizationMembers(t *testing.T) {
Config: acctest.ParseTestName(testAccOrganizationMembersWithOneMember, testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_organization_members.my_members", "members.#", "1"),
resource.TestCheckResourceAttr("data.auth0_organization.my_org_data", "members.#", "1"),
),
},
{
Config: acctest.ParseTestName(testAccOrganizationMembersWithTwoMembers, testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_organization_members.my_members", "members.#", "2"),
resource.TestCheckResourceAttr("data.auth0_organization.my_org_data", "members.#", "2"),
),
},
{
Config: acctest.ParseTestName(testAccOrganizationMembersRemoveOneMember, testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_organization_members.my_members", "members.#", "1"),
resource.TestCheckResourceAttr("data.auth0_organization.my_org_data", "members.#", "1"),
),
},
{
Config: acctest.ParseTestName(testAccOrganizationMembersRemoveAllMembers, testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_organization_members.my_members", "members.#", "0"),
resource.TestCheckResourceAttr("data.auth0_organization.my_org_data", "members.#", "0"),
),
},
{
Config: acctest.ParseTestName(testAccOrganizationMembersDeleteResource, testName),
},
{
RefreshState: true,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.auth0_organization.my_org_data", "members.#", "0"),
),
},
},
Expand Down
Loading

0 comments on commit ed70341

Please sign in to comment.