-
Notifications
You must be signed in to change notification settings - Fork 769
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a github_membership data source.
This will allow querying whether a user is a member of the organization, as well as what role they have. Co-authored-by: Mann Patel <mann@autonomic.ai>
- Loading branch information
1 parent
55e9de1
commit 17dc329
Showing
5 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func dataSourceGithubMembership() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceGithubMembershipRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"username": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"role": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"etag": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceGithubMembershipRead(d *schema.ResourceData, meta interface{}) error { | ||
username := d.Get("username").(string) | ||
log.Printf("[INFO] Refreshing GitHub membership: %s", username) | ||
|
||
client := meta.(*Organization).client | ||
orgName := meta.(*Organization).name | ||
|
||
ctx := context.Background() | ||
|
||
membership, resp, err := client.Organizations.GetOrgMembership(ctx, | ||
username, orgName) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(buildTwoPartID(membership.Organization.Login, membership.User.Login)) | ||
|
||
d.Set("username", membership.User.Login) | ||
d.Set("role", membership.Role) | ||
d.Set("etag", resp.Header.Get("ETag")) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package github | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
) | ||
|
||
func TestAccGithubMembershipDataSource_noMatchReturnsError(t *testing.T) { | ||
username := "admin" | ||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccPreCheck(t) | ||
}, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckGithubMembershipDatasourceConfig(username), | ||
ExpectError: regexp.MustCompile(`Not Found`), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccGithubMembershipDataSource_existing(t *testing.T) { | ||
if testUser == "" { | ||
t.Skip("This test requires you to set the test user (set it by exporting GITHUB_TEST_USER)") | ||
} | ||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccPreCheck(t) | ||
}, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckGithubMembershipDatasourceConfig(testUser), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.github_membership.test", "username", testUser), | ||
resource.TestCheckResourceAttrSet("data.github_membership.test", "role"), | ||
resource.TestCheckResourceAttrSet("data.github_membership.test", "etag"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckGithubMembershipDatasourceConfig(username string) string { | ||
return fmt.Sprintf(` | ||
data "github_membership" "test" { | ||
username = "%s" | ||
} | ||
`, username) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--- | ||
layout: "github" | ||
page_title: "GitHub: github_membership" | ||
description: |- | ||
Get information on user membership in an organization. | ||
------------------------------------------- | ||
|
||
# github_membership | ||
|
||
Use this data source to find out if a user is a member of your organization, as well | ||
as what role they have within it. | ||
If the user's membership in the organization is pending their acceptance of an invite, | ||
the role they would have once they accept will be returned. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "github_membership" "membership_for_some_user" { | ||
username = "SomeUser" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
* `username` - (Required) The username to lookup in the organization. | ||
|
||
## Attributes Reference | ||
|
||
* `username` - The username. | ||
* `role` - `admin` or `member` -- the role the user has within the organization. | ||
* `etag` - An etag representing the membership object. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters