|
| 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 | +} |
0 commit comments