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

Add a github_membership data source. #396

Merged
merged 1 commit into from
Apr 2, 2020
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
53 changes: 53 additions & 0 deletions github/data_source_github_membership.go
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
}
55 changes: 55 additions & 0 deletions github/data_source_github_membership_test.go
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)
}
1 change: 1 addition & 0 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func Provider() terraform.ResourceProvider {
DataSourcesMap: map[string]*schema.Resource{
"github_collaborators": dataSourceGithubCollaborators(),
"github_ip_ranges": dataSourceGithubIpRanges(),
"github_membership": dataSourceGithubMembership(),
"github_release": dataSourceGithubRelease(),
"github_repositories": dataSourceGithubRepositories(),
"github_repository": dataSourceGithubRepository(),
Expand Down
31 changes: 31 additions & 0 deletions website/docs/d/membership.html.markdown
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.
3 changes: 3 additions & 0 deletions website/github.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
<li>
<a href="/docs/providers/github/d/ip_ranges.html">github_ip_ranges</a>
</li>
<li>
<a href="/docs/providers/github/d/membership.html">github_membership</a>
</li>
<li>
<a href="/docs/providers/github/d/release.html">github_release</a>
</li>
Expand Down