|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "github.com/google/go-github/v43/github" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 10 | +) |
| 11 | + |
| 12 | +func resourceGithubEMUGroupMapping() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Create: resourceGithubEMUGroupMappingCreate, |
| 15 | + Read: resourceGithubEMUGroupMappingRead, |
| 16 | + Update: resourceGithubEMUGroupMappingUpdate, |
| 17 | + Delete: resourceGithubEMUGroupMappingDelete, |
| 18 | + Importer: &schema.ResourceImporter{ |
| 19 | + State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { |
| 20 | + id, err := strconv.Atoi(d.Id()) |
| 21 | + if err != nil { |
| 22 | + return nil, err |
| 23 | + } |
| 24 | + if err := d.Set("group_id", id); err != nil { |
| 25 | + return nil, err |
| 26 | + } |
| 27 | + ctx := context.WithValue(context.Background(), ctxId, d.Id()) |
| 28 | + client := meta.(*Owner).v3client |
| 29 | + orgName := meta.(*Owner).name |
| 30 | + group, _, err := client.Teams.GetExternalGroup(ctx, orgName, int64(id)) |
| 31 | + if err != nil { |
| 32 | + return nil, err |
| 33 | + } |
| 34 | + if len(group.Teams) != 1 { |
| 35 | + return nil, fmt.Errorf("could not get team_slug from %v number of teams", len(group.Teams)) |
| 36 | + } |
| 37 | + if err := d.Set("team_slug", group.Teams[0].TeamName); err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + d.SetId(fmt.Sprintf("teams/%s/external-groups", d.Id())) |
| 41 | + return []*schema.ResourceData{d}, nil |
| 42 | + }, |
| 43 | + }, |
| 44 | + Schema: map[string]*schema.Schema{ |
| 45 | + "team_slug": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Required: true, |
| 48 | + }, |
| 49 | + "group_id": { |
| 50 | + Type: schema.TypeInt, |
| 51 | + Required: true, |
| 52 | + }, |
| 53 | + "etag": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Computed: true, |
| 56 | + }, |
| 57 | + }, |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func resourceGithubEMUGroupMappingCreate(d *schema.ResourceData, meta interface{}) error { |
| 62 | + return resourceGithubEMUGroupMappingUpdate(d, meta) |
| 63 | +} |
| 64 | + |
| 65 | +func resourceGithubEMUGroupMappingRead(d *schema.ResourceData, meta interface{}) error { |
| 66 | + err := checkOrganization(meta) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + client := meta.(*Owner).v3client |
| 71 | + orgName := meta.(*Owner).name |
| 72 | + |
| 73 | + id, ok := d.GetOk("group_id") |
| 74 | + if !ok { |
| 75 | + return fmt.Errorf("could not get group id from provided value") |
| 76 | + } |
| 77 | + id64, err := getInt64FromInterface(id) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + ctx := context.WithValue(context.Background(), ctxId, d.Id()) |
| 83 | + |
| 84 | + group, resp, err := client.Teams.GetExternalGroup(ctx, orgName, id64) |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + |
| 89 | + d.Set("etag", resp.Header.Get("ETag")) |
| 90 | + d.Set("group", group) |
| 91 | + return nil |
| 92 | +} |
| 93 | + |
| 94 | +func resourceGithubEMUGroupMappingUpdate(d *schema.ResourceData, meta interface{}) error { |
| 95 | + err := checkOrganization(meta) |
| 96 | + if err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + client := meta.(*Owner).v3client |
| 100 | + orgName := meta.(*Owner).name |
| 101 | + ctx := context.WithValue(context.Background(), ctxId, d.Id()) |
| 102 | + |
| 103 | + teamSlug, ok := d.GetOk("team_slug") |
| 104 | + if !ok { |
| 105 | + return fmt.Errorf("could not get team slug from provided value") |
| 106 | + } |
| 107 | + |
| 108 | + id, ok := d.GetOk("group_id") |
| 109 | + if !ok { |
| 110 | + return fmt.Errorf("could not get group id from provided value") |
| 111 | + } |
| 112 | + id64, err := getInt64FromInterface(id) |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + |
| 117 | + eg := &github.ExternalGroup{ |
| 118 | + GroupID: &id64, |
| 119 | + } |
| 120 | + |
| 121 | + _, _, err = client.Teams.UpdateConnectedExternalGroup(ctx, orgName, teamSlug.(string), eg) |
| 122 | + if err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + |
| 126 | + d.SetId(fmt.Sprintf("teams/%s/external-groups", teamSlug)) |
| 127 | + return resourceGithubEMUGroupMappingRead(d, meta) |
| 128 | +} |
| 129 | + |
| 130 | +func resourceGithubEMUGroupMappingDelete(d *schema.ResourceData, meta interface{}) error { |
| 131 | + err := checkOrganization(meta) |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + client := meta.(*Owner).v3client |
| 136 | + orgName := meta.(*Owner).name |
| 137 | + |
| 138 | + teamSlug, ok := d.GetOk("team_slug") |
| 139 | + if !ok { |
| 140 | + return fmt.Errorf("could not parse team slug from provided value") |
| 141 | + } |
| 142 | + |
| 143 | + ctx := context.WithValue(context.Background(), ctxId, d.Id()) |
| 144 | + |
| 145 | + _, err = client.Teams.RemoveConnectedExternalGroup(ctx, orgName, teamSlug.(string)) |
| 146 | + if err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + return nil |
| 150 | +} |
| 151 | + |
| 152 | +func getInt64FromInterface(val interface{}) (int64, error) { |
| 153 | + var id64 int64 |
| 154 | + switch val := val.(type) { |
| 155 | + case int64: |
| 156 | + id64 = val |
| 157 | + case int: |
| 158 | + id64 = int64(val) |
| 159 | + case string: |
| 160 | + var err error |
| 161 | + id64, err = strconv.ParseInt(val, 10, 64) |
| 162 | + if err != nil { |
| 163 | + return 0, fmt.Errorf("could not parse id from string: %v", err) |
| 164 | + } |
| 165 | + default: |
| 166 | + return 0, fmt.Errorf("unexpected type converting to int64 from interface") |
| 167 | + } |
| 168 | + return id64, nil |
| 169 | +} |
0 commit comments