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

Allow tfe_organization_membership import by org name and user email #715

Merged
merged 3 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
FEATURES:

ENHANCEMENTS:
* r/tfe_organization_membership: Organization Memberships can now be imported using `<ORGANIZATION NAME>/<USER EMAIL>` ([#715](https://github.com/hashicorp/terraform-provider-tfe/pull/715))

BUG FIXES:

Expand Down
26 changes: 25 additions & 1 deletion tfe/resource_tfe_organization_membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
package tfe

import (
"context"
"fmt"
"log"
"strings"

tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -17,7 +19,7 @@ func resourceTFEOrganizationMembership() *schema.Resource {
Read: resourceTFEOrganizationMembershipRead,
Delete: resourceTFEOrganizationMembershipDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
StateContext: resourceTFEOrganizationMembershipImporter,
},

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -115,3 +117,25 @@ func resourceTFEOrganizationMembershipDelete(d *schema.ResourceData, meta interf

return nil
}

func resourceTFEOrganizationMembershipImporter(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(ConfiguredClient)

// Import formats:
// - <ORGANIZATION MEMBERSHIP ID>
// - <organization name>/<user email>
s := strings.SplitN(d.Id(), "/", 2)
if len(s) == 2 {
org := s[0]
email := s[1]
orgMembership, err := fetchOrganizationMemberByNameOrEmail(ctx, config.Client, org, "", email)
if err != nil {
return nil, fmt.Errorf(
"error retrieving user with email %s from organization %s: %w", email, org, err)
}

d.SetId(orgMembership.ID)
}

return []*schema.ResourceData{d}, nil
}
76 changes: 75 additions & 1 deletion tfe/resource_tfe_organization_membership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package tfe
import (
"fmt"
"math/rand"
"regexp"
"testing"
"time"

Expand Down Expand Up @@ -43,7 +44,7 @@ func TestAccTFEOrganizationMembership_basic(t *testing.T) {
})
}

func TestAccTFEOrganizationMembershipImport(t *testing.T) {
func TestAccTFEOrganizationMembershipImport_ByID(t *testing.T) {
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()

resource.Test(t, resource.TestCase{
Expand All @@ -63,6 +64,66 @@ func TestAccTFEOrganizationMembershipImport(t *testing.T) {
})
}

func TestAccTFEOrganizationMembershipImport_ByEmail(t *testing.T) {
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()

orgName := fmt.Sprintf("tst-terraform-%d", rInt)
email := "testuser@hashicorp.com"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTFEOrganizationMembershipDestroy,
Steps: []resource.TestStep{
{
Config: testAccTFEOrganizationMembership_nameAndEmail(orgName, email),
},
{
ResourceName: "tfe_organization_membership.foobar",
ImportState: true,
ImportStateId: fmt.Sprintf("%s/%s", orgName, email),
ImportStateVerify: true,
},
},
})
}

func TestAccTFEOrganizationMembershipImport_invalidImportId(t *testing.T) {
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()

orgName := fmt.Sprintf("tst-terraform-%d", rInt)
email := "testuser@hashicorp.com"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTFEOrganizationMembershipDestroy,
Steps: []resource.TestStep{
{
Config: testAccTFEOrganizationMembership_nameAndEmail(orgName, email),
},
{
ResourceName: "tfe_organization_membership.foobar",
ImportState: true,
ImportStateId: fmt.Sprintf("%s/%s/someOtherString", orgName, email),
ExpectError: regexp.MustCompile(fmt.Sprintf("error retrieving user with email %s/someOtherString from organization %s", email, orgName)),
},
{
ResourceName: "tfe_organization_membership.foobar",
ImportState: true,
ImportStateId: fmt.Sprintf("invalid-org-%d/%s", rInt, email),
ExpectError: regexp.MustCompile(fmt.Sprintf("error retrieving user with email %s from organization invalid-org-%d", email, rInt)),
},
{
ResourceName: "tfe_organization_membership.foobar",
ImportState: true,
ImportStateId: fmt.Sprintf("%s/invalidEmail", orgName),
ExpectError: regexp.MustCompile(fmt.Sprintf("error retrieving user with email invalidEmail from organization %s", orgName)),
},
},
})
}

func testAccCheckTFEOrganizationMembershipExists(
n string, membership *tfe.OrganizationMembership) resource.TestCheckFunc {
return func(s *terraform.State) error {
Expand Down Expand Up @@ -146,3 +207,16 @@ resource "tfe_organization_membership" "foobar" {
organization = tfe_organization.foobar.id
}`, rInt)
}

func testAccTFEOrganizationMembership_nameAndEmail(orgName string, email string) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "%s"
email = "admin@company.com"
}

resource "tfe_organization_membership" "foobar" {
email = "%s"
organization = tfe_organization.foobar.id
}`, orgName, email)
}
8 changes: 7 additions & 1 deletion website/docs/r/organization_membership.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,15 @@ In addition to all arguments above, the following attributes are exported:
* `user_id` - The ID of the user associated with the organization membership.
* `username` - The username of the user associated with the organization membership.

Organization memberships can be imported; use `<ORGANIZATION MEMBERSHIP ID>` as the import ID. For
## Import

Organization memberships can be imported using `<ORGANIZATION>/<USER EMAIL>` or `<ORGANIZATION MEMBERSHIP ID>` as the import ID. For
example:

```shell
terraform import tfe_organization_membership.test my-org-name/user@example.com
```

```shell
terraform import tfe_organization_membership.test ou-wAs3zYmWAhYK7peR
```