Skip to content

Commit dbd287c

Browse files
add ability to read an organization membership by org membership id
This will be useful in case you need to read all organization memberships and pull out information such as emails of all users so that it can be used to create downstream resources, example: ```terraform data "tfe_organization_members" "users" { organization = var.tfc_org } data "tfe_organization_membership" "test" { for_each = toset([ for index, member in data.tfe_organization_members.users.members : member.organization_membership_id ]) organization = var.tfc_org organization_membership_id = each.value } output "user_emails" { value = [ for index, member in data.tfe_organization_membership.test : member.email ] } ``` Related issue: #871
1 parent 01abda0 commit dbd287c

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

internal/provider/data_source_organization_membership.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ func dataSourceTFEOrganizationMembershipRead(d *schema.ResourceData, meta interf
6767

6868
d.SetId(orgMember.ID)
6969
} else {
70+
orgMember, err := fetchOrganizationMemberById(context.Background(), config.Client, orgMemberID)
71+
if err != nil {
72+
return fmt.Errorf(
73+
"could not find organization membership (%s) for organization %s: %w", orgMemberID, organization, err,
74+
)
75+
}
76+
77+
d.Set("email", orgMember.Email)
7078
d.SetId(orgMemberID)
7179
}
7280

internal/provider/organization_members_helpers.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,15 @@ func fetchOrganizationMemberByNameOrEmail(ctx context.Context, client *tfe.Clien
103103

104104
return nil, tfe.ErrResourceNotFound
105105
}
106+
107+
func fetchOrganizationMemberById(ctx context.Context, client *tfe.Client, orgMemberId string) (*tfe.OrganizationMembership, error) {
108+
orgMember, err := client.OrganizationMemberships.Read(ctx, orgMemberId)
109+
if err != nil {
110+
return nil, fmt.Errorf("failed to read organization membership %s: %w", orgMemberId, err)
111+
}
112+
if orgMember != nil {
113+
return orgMember, nil
114+
}
115+
116+
return nil, tfe.ErrResourceNotFound
117+
}

0 commit comments

Comments
 (0)