-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allows for managing organization membership, organization teams, team membership, and team repositories.
- Loading branch information
1 parent
803ef52
commit 7d83c3b
Showing
96 changed files
with
10,633 additions
and
2,372 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 |
---|---|---|
|
@@ -20,5 +20,6 @@ website/node_modules | |
*~ | ||
.*.swp | ||
.idea | ||
*.iml | ||
*.test | ||
*.iml |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,12 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/hashicorp/terraform/builtin/providers/github" | ||
"github.com/hashicorp/terraform/plugin" | ||
) | ||
|
||
func main() { | ||
plugin.Serve(&plugin.ServeOpts{ | ||
ProviderFunc: github.Provider, | ||
}) | ||
} |
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 @@ | ||
package main |
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,29 @@ | ||
package github | ||
|
||
import ( | ||
"github.com/google/go-github/github" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
type Config struct { | ||
Token string | ||
Organization string | ||
} | ||
|
||
type Organization struct { | ||
name string | ||
client *github.Client | ||
} | ||
|
||
// Client configures and returns a fully initialized GithubClient | ||
func (c *Config) Client() (interface{}, error) { | ||
var org Organization | ||
org.name = c.Organization | ||
ts := oauth2.StaticTokenSource( | ||
&oauth2.Token{AccessToken: c.Token}, | ||
) | ||
tc := oauth2.NewClient(oauth2.NoContext, ts) | ||
|
||
org.client = github.NewClient(tc) | ||
return &org, 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,56 @@ | ||
package github | ||
|
||
import ( | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
// Provider returns a terraform.ResourceProvider. | ||
func Provider() terraform.ResourceProvider { | ||
|
||
// The actual provider | ||
return &schema.Provider{ | ||
Schema: map[string]*schema.Schema{ | ||
"token": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
DefaultFunc: schema.EnvDefaultFunc("GITHUB_TOKEN", nil), | ||
Description: descriptions["token"], | ||
}, | ||
"organization": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
DefaultFunc: schema.EnvDefaultFunc("GITHUB_ORGANIZATION", nil), | ||
Description: descriptions["organization"], | ||
}, | ||
}, | ||
|
||
ResourcesMap: map[string]*schema.Resource{ | ||
"github_team": resourceGithubTeam(), | ||
"github_team_membership": resourceGithubTeamMembership(), | ||
"github_team_repository": resourceGithubTeamRepository(), | ||
"github_membership": resourceGithubMembership(), | ||
}, | ||
|
||
ConfigureFunc: providerConfigure, | ||
} | ||
} | ||
|
||
var descriptions map[string]string | ||
|
||
func init() { | ||
descriptions = map[string]string{ | ||
"token": "The OAuth token used to connect to GitHub.", | ||
|
||
"organization": "The GitHub organization name to manage.", | ||
} | ||
} | ||
|
||
func providerConfigure(d *schema.ResourceData) (interface{}, error) { | ||
config := Config{ | ||
Token: d.Get("token").(string), | ||
Organization: d.Get("organization").(string), | ||
} | ||
|
||
return config.Client() | ||
} |
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,38 @@ | ||
package github | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
var testAccProviders map[string]terraform.ResourceProvider | ||
var testAccProvider *schema.Provider | ||
|
||
func init() { | ||
testAccProvider = Provider().(*schema.Provider) | ||
testAccProviders = map[string]terraform.ResourceProvider{ | ||
"github": testAccProvider, | ||
} | ||
} | ||
|
||
func TestProvider(t *testing.T) { | ||
if err := Provider().(*schema.Provider).InternalValidate(); err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
} | ||
|
||
func TestProvider_impl(t *testing.T) { | ||
var _ terraform.ResourceProvider = Provider() | ||
} | ||
|
||
func testAccPreCheck(t *testing.T) { | ||
if v := os.Getenv("GITHUB_TOKEN"); v == "" { | ||
t.Fatal("GITHUB_TOKEN must be set for acceptance tests") | ||
} | ||
if v := os.Getenv("GITHUB_ORGANIZATION"); v == "" { | ||
t.Fatal("GITHUB_ORGANIZATION must be set for acceptance tests") | ||
} | ||
} |
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,85 @@ | ||
package github | ||
|
||
import ( | ||
"github.com/google/go-github/github" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceGithubMembership() *schema.Resource { | ||
|
||
return &schema.Resource{ | ||
Create: resourceGithubMembershipCreate, | ||
Read: resourceGithubMembershipRead, | ||
Update: resourceGithubMembershipUpdate, | ||
Delete: resourceGithubMembershipDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"username": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"role": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ValidateFunc: validateRoleValueFunc([]string{"member", "admin"}), | ||
Default: "member", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceGithubMembershipCreate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*Organization).client | ||
n := d.Get("username").(string) | ||
r := d.Get("role").(string) | ||
|
||
membership, _, err := client.Organizations.EditOrgMembership(n, meta.(*Organization).name, | ||
&github.Membership{Role: &r}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(buildTwoPartID(membership.Organization.Login, membership.User.Login)) | ||
|
||
return resourceGithubMembershipRead(d, meta) | ||
} | ||
|
||
func resourceGithubMembershipRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*Organization).client | ||
|
||
membership, _, err := client.Organizations.GetOrgMembership(d.Get("username").(string), meta.(*Organization).name) | ||
if err != nil { | ||
d.SetId("") | ||
return nil | ||
} | ||
username := membership.User.Login | ||
roleName := membership.Role | ||
|
||
d.Set("username", *username) | ||
d.Set("role", *roleName) | ||
return nil | ||
} | ||
|
||
func resourceGithubMembershipUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*Organization).client | ||
n := d.Get("username").(string) | ||
r := d.Get("role").(string) | ||
|
||
_, _, err := client.Organizations.EditOrgMembership(n, meta.(*Organization).name, &github.Membership{ | ||
Role: &r, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func resourceGithubMembershipDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*Organization).client | ||
n := d.Get("username").(string) | ||
|
||
_, err := client.Organizations.RemoveOrgMembership(n, meta.(*Organization).name) | ||
|
||
return err | ||
} |
Oops, something went wrong.