Skip to content

Commit b39c5e1

Browse files
committed
Update import function to support a single ID and a two part ID
Signed-off-by: Timo Sand <[email protected]>
1 parent 9593186 commit b39c5e1

3 files changed

Lines changed: 60 additions & 27 deletions

File tree

github/resource_github_emu_group_mapping.go

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"strconv"
7+
"strings"
78

89
"github.com/google/go-github/v67/github"
910
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -17,28 +18,11 @@ func resourceGithubEMUGroupMapping() *schema.Resource {
1718
Delete: resourceGithubEMUGroupMappingDelete,
1819
Importer: &schema.ResourceImporter{
1920
State: func(d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
20-
id, err := strconv.Atoi(d.Id())
21-
if err != nil {
22-
return nil, err
21+
isTwoPartID := strings.Contains(d.Id(), ":")
22+
if isTwoPartID {
23+
return importWithTwoPartID(d, meta)
2324
}
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", *group.Teams[0].TeamName))
41-
return []*schema.ResourceData{d}, nil
25+
return importWithIntegerID(d, meta)
4226
},
4327
},
4428
Schema: map[string]*schema.Schema{
@@ -185,3 +169,47 @@ func getInt64FromInterface(val any) (int64, error) {
185169
}
186170
return id64, nil
187171
}
172+
173+
func importWithTwoPartID(d *schema.ResourceData, _ any) ([]*schema.ResourceData, error) {
174+
groupIDString, teamSlug, err := parseTwoPartID(d.Id(), "group_id", "team_slug")
175+
if err != nil {
176+
return nil, err
177+
}
178+
groupID, err := strconv.Atoi(groupIDString)
179+
if err != nil {
180+
return nil, err
181+
}
182+
if err := d.Set("group_id", groupID); err != nil {
183+
return nil, err
184+
}
185+
if err := d.Set("team_slug", teamSlug); err != nil {
186+
return nil, err
187+
}
188+
d.SetId(fmt.Sprintf("teams/%s/external-groups", teamSlug))
189+
return []*schema.ResourceData{d}, nil
190+
}
191+
192+
func importWithIntegerID(d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
193+
groupID, err := strconv.Atoi(d.Id())
194+
if err != nil {
195+
return nil, err
196+
}
197+
if err := d.Set("group_id", groupID); err != nil {
198+
return nil, err
199+
}
200+
ctx := context.WithValue(context.Background(), ctxId, d.Id())
201+
client := meta.(*Owner).v3client
202+
orgName := meta.(*Owner).name
203+
group, _, err := client.Teams.GetExternalGroup(ctx, orgName, int64(groupID))
204+
if err != nil {
205+
return nil, err
206+
}
207+
if len(group.Teams) != 1 {
208+
return nil, fmt.Errorf("could not get team_slug from %v number of teams", len(group.Teams))
209+
}
210+
if err := d.Set("team_slug", group.Teams[0].TeamName); err != nil {
211+
return nil, err
212+
}
213+
d.SetId(fmt.Sprintf("teams/%s/external-groups", *group.Teams[0].TeamName))
214+
return []*schema.ResourceData{d}, nil
215+
}

github/resource_github_emu_group_mapping_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func testAccGithubEMUGroupMappingImportStateIdFunc(resourceName string) resource
167167
if !ok {
168168
return "", fmt.Errorf("not found: %s", resourceName)
169169
}
170-
return rs.Primary.Attributes["group_id"], nil
170+
return buildTwoPartID(rs.Primary.Attributes["group_id"], rs.Primary.Attributes["team_slug"]), nil
171171
}
172172
}
173173

website/docs/r/emu_group_mapping.html.markdown

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ description: |-
77

88
# github_emu_group_mapping
99

10-
This resource manages mappings between external groups for enterprise managed users and GitHub teams. It wraps the API detailed [here](https://docs.github.com/en/rest/reference/teams#external-groups). Note that this is a distinct resource from `github_team_sync_group_mapping`. `github_emu_group_mapping` is special to the Enterprise Managed User (EMU) external group feature, whereas `github_team_sync_group_mapping` is specific to Identity Provider Groups.
10+
This resource manages mappings between external groups for enterprise managed users and GitHub teams. It wraps the [Teams#ExternalGroups API](https://docs.github.com/en/rest/reference/teams#external-groups). Note that this is a distinct resource from `github_team_sync_group_mapping`. `github_emu_group_mapping` is special to the Enterprise Managed User (EMU) external group feature, whereas `github_team_sync_group_mapping` is specific to Identity Provider Groups.
1111

1212
## Example Usage
1313

@@ -16,20 +16,25 @@ resource "github_emu_group_mapping" "example_emu_group_mapping" {
1616
team_slug = "emu-test-team" # The GitHub team name to modify
1717
group_id = 28836 # The group ID of the external group to link
1818
}
19-
20-
# Note that here GITHUB_OWNER and GITHUB_TOKEN have been set in the environment.
2119
```
2220

2321
## Argument Reference
2422

2523
The following arguments are supported:
24+
2625
* `team_slug` - (Required) Slug of the GitHub team
2726
* `group_id` - (Required) Integer corresponding to the external group ID to be linked
2827

2928
## Import
3029

31-
GitHub EMU External Group Mappings can be imported using the external `group_id`, e.g.
30+
GitHub EMU External Group Mappings can be imported using the external `group_id`, when it maps to a single team, or the `group_id` and `team_slug` separated by a colon, when it maps to multiple teams, e.g.
3231

33-
```
32+
```sh
33+
# IdP Group with a single team
3434
$ terraform import github_emu_group_mapping.example_emu_group_mapping 28836
3535
```
36+
37+
```sh
38+
# IdP Group with multiple teams
39+
$ terraform import github_emu_group_mapping.example_emu_group_mapping 28836:emu-test-team
40+
```

0 commit comments

Comments
 (0)