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

Bug Fix: GitHub Organization is not required for all datasources #242

Merged
merged 3 commits into from
Jun 27, 2019
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
12 changes: 11 additions & 1 deletion github/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package github
import (
"context"
"crypto/tls"
"fmt"
"net/http"
"net/url"

Expand All @@ -16,6 +17,7 @@ type Config struct {
Organization string
BaseURL string
Insecure bool
Individual bool
}

type Organization struct {
Expand All @@ -27,7 +29,7 @@ type Organization struct {
// 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},
)
Expand All @@ -39,6 +41,14 @@ func (c *Config) Client() (interface{}, error) {
ctx = context.WithValue(ctx, oauth2.HTTPClient, insecureClient)
}

if c.Individual {
org.name = ""
} else if c.Organization != "" {
org.name = c.Organization
} else {
return nil, fmt.Errorf("If `individual` is false, `organization` is required.")
}

tc := oauth2.NewClient(ctx, ts)

tc.Transport = NewEtagTransport(tc.Transport)
Expand Down
5 changes: 5 additions & 0 deletions github/data_source_github_repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ func dataSourceGithubRepositories() *schema.Resource {
}

func dataSourceGithubRepositoriesRead(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}

client := meta.(*Organization).client

query := d.Get("query").(string)
Expand Down
6 changes: 5 additions & 1 deletion github/data_source_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,12 @@ func dataSourceGithubRepository() *schema.Resource {
}

func dataSourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
err := checkOrganization(meta)
if err != nil {
return err
}

client := meta.(*Organization).client
orgName := meta.(*Organization).name
var repoName string

Expand Down
14 changes: 12 additions & 2 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func Provider() terraform.ResourceProvider {
},
"organization": {
Type: schema.TypeString,
Required: true,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("GITHUB_ORGANIZATION", nil),
Description: descriptions["organization"],
},
Expand All @@ -33,6 +33,12 @@ func Provider() terraform.ResourceProvider {
Default: false,
Description: descriptions["insecure"],
},
"individual": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: descriptions["individual"],
},
},

ResourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -77,12 +83,15 @@ func init() {
descriptions = map[string]string{
"token": "The OAuth token used to connect to GitHub.",

"organization": "The GitHub organization name to manage.",
"organization": "The GitHub organization name to manage. " +
"If `individual` is false, organization is required.",

"base_url": "The GitHub Base API URL",

"insecure": "Whether server should be accessed " +
"without verifying the TLS certificate.",

"individual": "Whether to run outside an organization.",
}
}

Expand All @@ -93,6 +102,7 @@ func providerConfigure(p *schema.Provider) schema.ConfigureFunc {
Organization: d.Get("organization").(string),
BaseURL: d.Get("base_url").(string),
Insecure: d.Get("insecure").(bool),
Individual: d.Get("individual").(bool),
}

meta, err := config.Client()
Expand Down
29 changes: 29 additions & 0 deletions github/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,35 @@ func testAccPreCheck(t *testing.T) {
}
}

func TestProvider_individual(t *testing.T) {
individualProviderConfig := `provider "github" {
organization = ""
individual = true
}
`
username := "hashibot"
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckGithubMembershipDestroy,
Steps: []resource.TestStep{
{
Config: individualProviderConfig + testAccCheckGithubUserDataSourceConfig(username),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.github_user.test", "name"),
resource.TestCheckResourceAttr("data.github_user.test", "name", "HashiBot"),
),
},
{
Config: individualProviderConfig + testAccGithubMembershipConfig(username),
ExpectError: regexp.MustCompile("This resource requires GitHub organization to be set on the provider."),
},
},
})
}

func TestProvider_insecure(t *testing.T) {
// Use ephemeral port range (49152–65535)
port := fmt.Sprintf("%d", 49152+rand.Intn(16382))
Expand Down
20 changes: 20 additions & 0 deletions github/resource_github_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ func resourceGithubBranchProtection() *schema.Resource {
}

func resourceGithubBranchProtectionCreate(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}

client := meta.(*Organization).client

orgName := meta.(*Organization).name
Expand Down Expand Up @@ -169,6 +174,11 @@ func resourceGithubBranchProtectionCreate(d *schema.ResourceData, meta interface
}

func resourceGithubBranchProtectionRead(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}

client := meta.(*Organization).client

repoName, branch, err := parseTwoPartID(d.Id())
Expand Down Expand Up @@ -223,6 +233,11 @@ func resourceGithubBranchProtectionRead(d *schema.ResourceData, meta interface{}
}

func resourceGithubBranchProtectionUpdate(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}

client := meta.(*Organization).client
repoName, branch, err := parseTwoPartID(d.Id())
if err != nil {
Expand Down Expand Up @@ -266,6 +281,11 @@ func resourceGithubBranchProtectionUpdate(d *schema.ResourceData, meta interface
}

func resourceGithubBranchProtectionDelete(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}

client := meta.(*Organization).client
repoName, branch, err := parseTwoPartID(d.Id())
if err != nil {
Expand Down
17 changes: 16 additions & 1 deletion github/resource_github_issue_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func resourceGithubIssueLabel() *schema.Resource {
// same function for two schema funcs.

func resourceGithubIssueLabelCreateOrUpdate(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}

client := meta.(*Organization).client
orgName := meta.(*Organization).name
repoName := d.Get("repository").(string)
Expand Down Expand Up @@ -130,6 +135,11 @@ func resourceGithubIssueLabelCreateOrUpdate(d *schema.ResourceData, meta interfa
}

func resourceGithubIssueLabelRead(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}

client := meta.(*Organization).client
repoName, name, err := parseTwoPartID(d.Id())
if err != nil {
Expand Down Expand Up @@ -171,6 +181,11 @@ func resourceGithubIssueLabelRead(d *schema.ResourceData, meta interface{}) erro
}

func resourceGithubIssueLabelDelete(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}

client := meta.(*Organization).client

orgName := meta.(*Organization).name
Expand All @@ -179,7 +194,7 @@ func resourceGithubIssueLabelDelete(d *schema.ResourceData, meta interface{}) er
ctx := context.WithValue(context.Background(), ctxId, d.Id())

log.Printf("[DEBUG] Deleting label: %s (%s/%s)", name, orgName, repoName)
_, err := client.Issues.DeleteLabel(ctx,
_, err = client.Issues.DeleteLabel(ctx,
orgName, repoName, name)
return err
}
17 changes: 16 additions & 1 deletion github/resource_github_membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ func resourceGithubMembership() *schema.Resource {
}

func resourceGithubMembershipCreateOrUpdate(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}

client := meta.(*Organization).client

orgName := meta.(*Organization).name
Expand Down Expand Up @@ -69,6 +74,11 @@ func resourceGithubMembershipCreateOrUpdate(d *schema.ResourceData, meta interfa
}

func resourceGithubMembershipRead(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}

client := meta.(*Organization).client

orgName := meta.(*Organization).name
Expand Down Expand Up @@ -107,12 +117,17 @@ func resourceGithubMembershipRead(d *schema.ResourceData, meta interface{}) erro
}

func resourceGithubMembershipDelete(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}

client := meta.(*Organization).client
orgName := meta.(*Organization).name
ctx := context.WithValue(context.Background(), ctxId, d.Id())

log.Printf("[DEBUG] Deleting membership: %s", d.Id())
_, err := client.Organizations.RemoveOrgMembership(ctx,
_, err = client.Organizations.RemoveOrgMembership(ctx,
d.Get("username").(string), orgName)

return err
Expand Down
21 changes: 20 additions & 1 deletion github/resource_github_organization_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ func resourceGithubOrganizationProject() *schema.Resource {
}

func resourceGithubOrganizationProjectCreate(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}

client := meta.(*Organization).client
orgName := meta.(*Organization).name
name := d.Get("name").(string)
Expand All @@ -66,6 +71,11 @@ func resourceGithubOrganizationProjectCreate(d *schema.ResourceData, meta interf
}

func resourceGithubOrganizationProjectRead(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}

client := meta.(*Organization).client
orgName := meta.(*Organization).name

Expand Down Expand Up @@ -105,6 +115,11 @@ func resourceGithubOrganizationProjectRead(d *schema.ResourceData, meta interfac
}

func resourceGithubOrganizationProjectUpdate(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}

client := meta.(*Organization).client
orgName := meta.(*Organization).name

Expand All @@ -131,8 +146,12 @@ func resourceGithubOrganizationProjectUpdate(d *schema.ResourceData, meta interf
}

func resourceGithubOrganizationProjectDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
err := checkOrganization(meta)
if err != nil {
return err
}

client := meta.(*Organization).client
orgName := meta.(*Organization).name
projectID, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
Expand Down
20 changes: 20 additions & 0 deletions github/resource_github_organization_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ func resourceGithubOrganizationWebhookObject(d *schema.ResourceData) *github.Hoo
}

func resourceGithubOrganizationWebhookCreate(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}

client := meta.(*Organization).client

orgName := meta.(*Organization).name
Expand All @@ -91,6 +96,11 @@ func resourceGithubOrganizationWebhookCreate(d *schema.ResourceData, meta interf
}

func resourceGithubOrganizationWebhookRead(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}

client := meta.(*Organization).client

orgName := meta.(*Organization).name
Expand Down Expand Up @@ -130,6 +140,11 @@ func resourceGithubOrganizationWebhookRead(d *schema.ResourceData, meta interfac
}

func resourceGithubOrganizationWebhookUpdate(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}

client := meta.(*Organization).client

orgName := meta.(*Organization).name
Expand All @@ -152,6 +167,11 @@ func resourceGithubOrganizationWebhookUpdate(d *schema.ResourceData, meta interf
}

func resourceGithubOrganizationWebhookDelete(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}

client := meta.(*Organization).client

orgName := meta.(*Organization).name
Expand Down
5 changes: 5 additions & 0 deletions github/resource_github_project_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ func resourceGithubProjectColumn() *schema.Resource {
}

func resourceGithubProjectColumnCreate(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}

client := meta.(*Organization).client

options := github.ProjectColumnOptions{
Expand Down
Loading