Skip to content

Commit 2a64e3a

Browse files
committed
Add github_organization_block resource
1 parent 12efe79 commit 2a64e3a

File tree

3 files changed

+199
-0
lines changed

3 files changed

+199
-0
lines changed

github/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func Provider() terraform.ResourceProvider {
3939
"github_branch_protection": resourceGithubBranchProtection(),
4040
"github_issue_label": resourceGithubIssueLabel(),
4141
"github_membership": resourceGithubMembership(),
42+
"github_organization_block": resourceOrganizationBlock(),
4243
"github_organization_project": resourceGithubOrganizationProject(),
4344
"github_organization_webhook": resourceGithubOrganizationWebhook(),
4445
"github_project_column": resourceGithubProjectColumn(),

github/resource_organization_block.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"log"
6+
"net/http"
7+
8+
"github.com/google/go-github/v21/github"
9+
"github.com/hashicorp/terraform/helper/schema"
10+
)
11+
12+
func resourceOrganizationBlock() *schema.Resource {
13+
return &schema.Resource{
14+
Create: resourceOrganizationBlockCreate,
15+
Read: resourceOrganizationBlockRead,
16+
Delete: resourceOrganizationBlockDelete,
17+
Importer: &schema.ResourceImporter{
18+
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
19+
username := d.Id()
20+
d.Set("username", username)
21+
d.SetId(username)
22+
return []*schema.ResourceData{d}, nil
23+
},
24+
},
25+
26+
Schema: map[string]*schema.Schema{
27+
"username": {
28+
Type: schema.TypeString,
29+
Required: true,
30+
ForceNew: true,
31+
},
32+
33+
"etag": {
34+
Type: schema.TypeString,
35+
Computed: true,
36+
},
37+
},
38+
}
39+
}
40+
41+
func resourceOrganizationBlockCreate(d *schema.ResourceData, meta interface{}) error {
42+
client := meta.(*Organization).client
43+
orgName := meta.(*Organization).name
44+
ctx := context.Background()
45+
username := d.Get("username").(string)
46+
47+
log.Printf("[DEBUG] Creating organization block: %s (%s)", username, orgName)
48+
_, err := client.Organizations.BlockUser(ctx, orgName, username)
49+
if err != nil {
50+
return err
51+
}
52+
d.SetId(username)
53+
54+
return resourceOrganizationBlockRead(d, meta)
55+
}
56+
57+
func resourceOrganizationBlockRead(d *schema.ResourceData, meta interface{}) error {
58+
client := meta.(*Organization).client
59+
orgName := meta.(*Organization).name
60+
61+
username := d.Id()
62+
63+
ctx := context.WithValue(context.Background(), ctxId, d.Id())
64+
if !d.IsNewResource() {
65+
ctx = context.WithValue(ctx, ctxEtag, d.Get("etag").(string))
66+
}
67+
68+
log.Printf("[DEBUG] Reading organization block: %s (%s)", d.Id(), orgName)
69+
blocked, resp, err := client.Organizations.IsBlocked(ctx, orgName, username)
70+
if err != nil {
71+
if ghErr, ok := err.(*github.ErrorResponse); ok {
72+
if ghErr.Response.StatusCode == http.StatusNotModified {
73+
return nil
74+
}
75+
// not sure if this will ever be hit, I imagine just returns false?
76+
if ghErr.Response.StatusCode == http.StatusNotFound {
77+
log.Printf("[WARN] Removing organization block %s/%s from state because it no longer exists in GitHub",
78+
orgName, d.Id())
79+
d.SetId("")
80+
return nil
81+
}
82+
}
83+
return err
84+
}
85+
86+
if !blocked {
87+
d.SetId("")
88+
return nil
89+
}
90+
91+
d.Set("etag", resp.Header.Get("ETag"))
92+
93+
return nil
94+
}
95+
96+
func resourceOrganizationBlockDelete(d *schema.ResourceData, meta interface{}) error {
97+
client := meta.(*Organization).client
98+
99+
orgName := meta.(*Organization).name
100+
username := d.Id()
101+
ctx := context.WithValue(context.Background(), ctxId, d.Id())
102+
103+
log.Printf("[DEBUG] Deleting organization block: %s (%s)", d.Id(), orgName)
104+
_, err := client.Organizations.UnblockUser(ctx, orgName, username)
105+
return err
106+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform/helper/resource"
9+
"github.com/hashicorp/terraform/terraform"
10+
)
11+
12+
func TestAccOrganizationBlock_basic(t *testing.T) {
13+
resource.Test(t, resource.TestCase{
14+
PreCheck: func() { testAccPreCheck(t) },
15+
Providers: testAccProviders,
16+
CheckDestroy: testAccOrganizationBlockDestroy,
17+
Steps: []resource.TestStep{
18+
{
19+
Config: testAccOrganizationBlockConfig,
20+
Check: resource.ComposeTestCheckFunc(
21+
testAccCheckOrganizationBlockExists("github_organization_block.test"),
22+
),
23+
},
24+
},
25+
})
26+
}
27+
28+
func TestAccOrganizationBlock_importBasic(t *testing.T) {
29+
resource.Test(t, resource.TestCase{
30+
PreCheck: func() { testAccPreCheck(t) },
31+
Providers: testAccProviders,
32+
CheckDestroy: testAccOrganizationBlockDestroy,
33+
Steps: []resource.TestStep{
34+
{
35+
Config: testAccOrganizationBlockConfig,
36+
},
37+
{
38+
ResourceName: "github_organization_block.test",
39+
ImportState: true,
40+
ImportStateVerify: true,
41+
},
42+
},
43+
})
44+
}
45+
46+
func testAccOrganizationBlockDestroy(s *terraform.State) error {
47+
conn := testAccProvider.Meta().(*Organization).client
48+
orgName := testAccProvider.Meta().(*Organization).name
49+
50+
for _, rs := range s.RootModule().Resources {
51+
if rs.Type != "github_organization_block" {
52+
continue
53+
}
54+
55+
username := rs.Primary.ID
56+
57+
res, err := conn.Organizations.UnblockUser(context.TODO(), orgName, username)
58+
if res.StatusCode != 404 {
59+
return err
60+
}
61+
return nil
62+
}
63+
return nil
64+
}
65+
66+
func testAccCheckOrganizationBlockExists(n string) resource.TestCheckFunc {
67+
return func(s *terraform.State) error {
68+
rs, ok := s.RootModule().Resources[n]
69+
if !ok {
70+
return fmt.Errorf("Not Found: %s", n)
71+
}
72+
73+
username := rs.Primary.ID
74+
conn := testAccProvider.Meta().(*Organization).client
75+
orgName := testAccProvider.Meta().(*Organization).name
76+
77+
blocked, _, err := conn.Organizations.IsBlocked(context.TODO(), orgName, username)
78+
if err != nil {
79+
return err
80+
}
81+
if !blocked {
82+
return fmt.Errorf("not blocked: %s %s", orgName, username)
83+
}
84+
return nil
85+
}
86+
}
87+
88+
const testAccOrganizationBlockConfig = `
89+
resource "github_organization_block" "test" {
90+
username = "cgriggs01"
91+
}
92+
`

0 commit comments

Comments
 (0)