Skip to content

Commit

Permalink
Add github_organization_block resource
Browse files Browse the repository at this point in the history
  • Loading branch information
paultyng committed Jan 30, 2019
1 parent 12efe79 commit 2a64e3a
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 0 deletions.
1 change: 1 addition & 0 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func Provider() terraform.ResourceProvider {
"github_branch_protection": resourceGithubBranchProtection(),
"github_issue_label": resourceGithubIssueLabel(),
"github_membership": resourceGithubMembership(),
"github_organization_block": resourceOrganizationBlock(),
"github_organization_project": resourceGithubOrganizationProject(),
"github_organization_webhook": resourceGithubOrganizationWebhook(),
"github_project_column": resourceGithubProjectColumn(),
Expand Down
106 changes: 106 additions & 0 deletions github/resource_organization_block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package github

import (
"context"
"log"
"net/http"

"github.com/google/go-github/v21/github"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceOrganizationBlock() *schema.Resource {
return &schema.Resource{
Create: resourceOrganizationBlockCreate,
Read: resourceOrganizationBlockRead,
Delete: resourceOrganizationBlockDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
username := d.Id()
d.Set("username", username)
d.SetId(username)
return []*schema.ResourceData{d}, nil
},
},

Schema: map[string]*schema.Schema{
"username": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"etag": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceOrganizationBlockCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
orgName := meta.(*Organization).name
ctx := context.Background()
username := d.Get("username").(string)

log.Printf("[DEBUG] Creating organization block: %s (%s)", username, orgName)
_, err := client.Organizations.BlockUser(ctx, orgName, username)
if err != nil {
return err
}
d.SetId(username)

return resourceOrganizationBlockRead(d, meta)
}

func resourceOrganizationBlockRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
orgName := meta.(*Organization).name

username := d.Id()

ctx := context.WithValue(context.Background(), ctxId, d.Id())
if !d.IsNewResource() {
ctx = context.WithValue(ctx, ctxEtag, d.Get("etag").(string))
}

log.Printf("[DEBUG] Reading organization block: %s (%s)", d.Id(), orgName)
blocked, resp, err := client.Organizations.IsBlocked(ctx, orgName, username)
if err != nil {
if ghErr, ok := err.(*github.ErrorResponse); ok {
if ghErr.Response.StatusCode == http.StatusNotModified {
return nil
}
// not sure if this will ever be hit, I imagine just returns false?
if ghErr.Response.StatusCode == http.StatusNotFound {
log.Printf("[WARN] Removing organization block %s/%s from state because it no longer exists in GitHub",
orgName, d.Id())
d.SetId("")
return nil
}
}
return err
}

if !blocked {
d.SetId("")
return nil
}

d.Set("etag", resp.Header.Get("ETag"))

return nil
}

func resourceOrganizationBlockDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client

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

log.Printf("[DEBUG] Deleting organization block: %s (%s)", d.Id(), orgName)
_, err := client.Organizations.UnblockUser(ctx, orgName, username)
return err
}
92 changes: 92 additions & 0 deletions github/resource_organization_block_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package github

import (
"context"
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccOrganizationBlock_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccOrganizationBlockDestroy,
Steps: []resource.TestStep{
{
Config: testAccOrganizationBlockConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckOrganizationBlockExists("github_organization_block.test"),
),
},
},
})
}

func TestAccOrganizationBlock_importBasic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccOrganizationBlockDestroy,
Steps: []resource.TestStep{
{
Config: testAccOrganizationBlockConfig,
},
{
ResourceName: "github_organization_block.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccOrganizationBlockDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*Organization).client
orgName := testAccProvider.Meta().(*Organization).name

for _, rs := range s.RootModule().Resources {
if rs.Type != "github_organization_block" {
continue
}

username := rs.Primary.ID

res, err := conn.Organizations.UnblockUser(context.TODO(), orgName, username)
if res.StatusCode != 404 {
return err
}
return nil
}
return nil
}

func testAccCheckOrganizationBlockExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not Found: %s", n)
}

username := rs.Primary.ID
conn := testAccProvider.Meta().(*Organization).client
orgName := testAccProvider.Meta().(*Organization).name

blocked, _, err := conn.Organizations.IsBlocked(context.TODO(), orgName, username)
if err != nil {
return err
}
if !blocked {
return fmt.Errorf("not blocked: %s %s", orgName, username)
}
return nil
}
}

const testAccOrganizationBlockConfig = `
resource "github_organization_block" "test" {
username = "cgriggs01"
}
`

0 comments on commit 2a64e3a

Please sign in to comment.