Skip to content

Commit cde397b

Browse files
authored
[TF-6330] add org membership ID attr to tfe_organization_membership (#997)
add attr to tfe_organization_membership datasource
1 parent 27309d8 commit cde397b

File tree

6 files changed

+66
-12
lines changed

6 files changed

+66
-12
lines changed

.github/pull_request_template.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ _Describe why you're making this change._
44

55
_Remember to:_
66

7-
- _Update the [Change Log](https://github.com/hashicorp/terraform-provider-tfe/blob/main/docs/changelog-process.md)_
8-
9-
- _Update the [Documentation](https://github.com/hashicorp/terraform-provider-tfe/blob/main/docs/changelog-process.md#updating-the-documentation)_
7+
- [ ] _Update the [Change Log](https://github.com/hashicorp/terraform-provider-tfe/blob/main/docs/changelog-process.md)_
8+
- [ ] _Update the [Documentation](https://github.com/hashicorp/terraform-provider-tfe/blob/main/docs/changelog-process.md#updating-the-documentation)_
109

1110
## Testing plan
1211

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55
pull_request:
66

77
concurrency:
8-
group: ${{ github.ref }}
8+
group: ${{ github.head_ref || github.ref }}
99
cancel-in-progress: true
1010

1111
jobs:

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
## Unreleased
22

33
FEATURES:
4+
* `d/tfe_organization_membership`: Add `organization_membership_id` attribute, by @laurenolivia [997](https://github.com/hashicorp/terraform-provider-tfe/pull/997)
45
* `d/tfe_variable_set`: Add `project_ids` attribute, by @Netra2104 [994](https://github.com/hashicorp/terraform-provider-tfe/pull/994)
56
* **New Data Source**: `d/tfe_teams` is a new data source to return names and IDs of Teams in an Organization, by @isaacmcollins [992](https://github.com/hashicorp/terraform-provider-tfe/pull/992)
7+
68
## v0.48.0 (August 7, 2023)
79

810
BUG FIXES:

tfe/data_source_organization_membership.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ func dataSourceTFEOrganizationMembership() *schema.Resource {
3535
Type: schema.TypeString,
3636
Computed: true,
3737
},
38+
39+
"organization_membership_id": {
40+
Type: schema.TypeString,
41+
Optional: true,
42+
Computed: true,
43+
AtLeastOneOf: []string{"email", "username"},
44+
},
3845
},
3946
}
4047
}
@@ -45,17 +52,23 @@ func dataSourceTFEOrganizationMembershipRead(d *schema.ResourceData, meta interf
4552
// Get the user email and organization.
4653
email := d.Get("email").(string)
4754
username := d.Get("username").(string)
55+
orgMemberID := d.Get("organization_membership_id").(string)
4856

4957
organization, err := config.schemaOrDefaultOrganization(d)
5058
if err != nil {
5159
return err
5260
}
5361

54-
orgMember, err := fetchOrganizationMemberByNameOrEmail(context.Background(), config.Client, organization, username, email)
55-
if err != nil {
56-
return fmt.Errorf("could not find organization membership for organization %s: %w", organization, err)
62+
if orgMemberID == "" {
63+
orgMember, err := fetchOrganizationMemberByNameOrEmail(context.Background(), config.Client, organization, username, email)
64+
if err != nil {
65+
return fmt.Errorf("could not find organization membership for organization %s: %w", organization, err)
66+
}
67+
68+
d.SetId(orgMember.ID)
69+
} else {
70+
d.SetId(orgMemberID)
5771
}
5872

59-
d.SetId(orgMember.ID)
6073
return resourceTFEOrganizationMembershipRead(d, meta)
6174
}

tfe/data_source_organization_membership_test.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ func TestAccTFEOrganizationMembershipDataSource_basic(t *testing.T) {
3434
resource.TestCheckResourceAttrSet("data.tfe_organization_membership.foobar", "user_id"),
3535
),
3636
},
37+
{
38+
Config: testAccTFEOrganizationMembershipDataSourceConfigWithOrgMemberID(rInt),
39+
Check: resource.ComposeAggregateTestCheckFunc(
40+
resource.TestCheckResourceAttr(
41+
"data.tfe_organization_membership.foobar", "email", "[email protected]"),
42+
resource.TestCheckResourceAttr(
43+
"data.tfe_organization_membership.foobar", "username", ""),
44+
resource.TestCheckResourceAttr(
45+
"data.tfe_organization_membership.foobar", "organization", orgName),
46+
resource.TestCheckResourceAttrSet("data.tfe_organization_membership.foobar", "user_id"),
47+
),
48+
},
3749
},
3850
})
3951
}
@@ -82,7 +94,7 @@ func TestAccTFEOrganizationMembershipDataSource_missingParams(t *testing.T) {
8294
Steps: []resource.TestStep{
8395
{
8496
Config: testAccTFEOrganizationMembershipDataSourceMissingParams(rInt),
85-
ExpectError: regexp.MustCompile("you must specify a username or email"),
97+
ExpectError: regexp.MustCompile("`email,organization_membership_id,username` must be specified"),
8698
},
8799
},
88100
})
@@ -130,3 +142,21 @@ data "tfe_organization_membership" "foobar" {
130142
organization = tfe_organization.foobar.name
131143
}`, rInt)
132144
}
145+
146+
func testAccTFEOrganizationMembershipDataSourceConfigWithOrgMemberID(rInt int) string {
147+
return fmt.Sprintf(`
148+
resource "tfe_organization" "foobar" {
149+
name = "tst-terraform-%d"
150+
151+
}
152+
153+
resource "tfe_organization_membership" "foobar" {
154+
155+
organization = tfe_organization.foobar.id
156+
}
157+
158+
data "tfe_organization_membership" "foobar" {
159+
organization = tfe_organization.foobar.name
160+
organization_membership_id = tfe_organization_membership.foobar.id
161+
}`, rInt)
162+
}

website/docs/d/organization_membership.html.markdown

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,32 @@ data "tfe_organization_membership" "test" {
2828

2929
### Fetch by username
3030

31-
```
31+
```hcl
3232
data "tfe_organization_membership" "test" {
3333
organization = "my-org-name"
3434
username = "my-username"
3535
}
3636
```
3737

38+
### Fetch by organization membership ID
39+
40+
```hcl
41+
data "tfe_organization_membership" "test" {
42+
organization = "my-org-name"
43+
organization_membership_id = "ou-xxxxxxxxxxx"
44+
}
45+
```
46+
3847
## Argument Reference
3948

4049
The following arguments are supported:
4150

42-
* `organization` - (Required) Name of the organization.
51+
* `organization` - (Optional) Name of the organization.
4352
* `email` - (Optional) Email of the user.
4453
* `username` - (Optional) The username of the user.
54+
* `organization_membership_id` - (Optional) ID belonging to the organziation membership.
4555

46-
~> **NOTE:** While `email` and `username` are optional arguments, one or the other is required.
56+
~> **NOTE:** While `email` and `username` are optional arguments, one or the other is required if `organization_membership_id` argument is not provided.
4757

4858
## Attributes Reference
4959

0 commit comments

Comments
 (0)