-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add github_organization_block resource
- Loading branch information
Showing
3 changed files
with
199 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
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,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 | ||
} |
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,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" | ||
} | ||
` |