Skip to content

Commit 0bfdaee

Browse files
committed
Update resource ID to be <group-id>:<team-id>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
1 parent 636eff4 commit 0bfdaee

4 files changed

Lines changed: 106 additions & 17 deletions

github/resource_github_emu_group_mapping.go

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,18 @@ func resourceGithubEMUGroupMapping() *schema.Resource {
4949
Computed: true,
5050
},
5151
},
52-
SchemaVersion: 1,
52+
SchemaVersion: 2,
5353
StateUpgraders: []schema.StateUpgrader{
5454
{
5555
Type: resourceGithubEMUGroupMappingV0().CoreConfigSchema().ImpliedType(),
5656
Upgrade: resourceGithubEMUGroupMappingStateUpgradeV0,
5757
Version: 0,
5858
},
59+
{
60+
Type: resourceGithubEMUGroupMappingV1().CoreConfigSchema().ImpliedType(),
61+
Upgrade: resourceGithubEMUGroupMappingStateUpgradeV1,
62+
Version: 1,
63+
},
5964
},
6065
}
6166
}
@@ -94,7 +99,7 @@ func resourceGithubEMUGroupMappingCreate(ctx context.Context, d *schema.Resource
9499
return diag.FromErr(err)
95100
}
96101

97-
newResourceID, err := buildID(strconv.FormatInt(teamID, 10), teamSlug, strconv.FormatInt(groupID, 10))
102+
newResourceID, err := buildID(strconv.FormatInt(groupID, 10), strconv.FormatInt(teamID, 10))
98103
if err != nil {
99104
return diag.FromErr(err)
100105
}
@@ -226,7 +231,7 @@ func resourceGithubEMUGroupMappingUpdate(ctx context.Context, d *schema.Resource
226231
GroupID: new(groupID),
227232
}
228233

229-
if d.HasChanges("group_id", "team_slug") {
234+
if d.HasChange("team_slug") {
230235

231236
tflog.Debug(ctx, "Updating connected external group via GitHub API")
232237

@@ -248,18 +253,6 @@ func resourceGithubEMUGroupMappingUpdate(ctx context.Context, d *schema.Resource
248253
if err := d.Set("group_name", group.GetGroupName()); err != nil {
249254
return diag.FromErr(err)
250255
}
251-
252-
teamID := toInt64(d.Get("team_id"))
253-
254-
newResourceID, err := buildID(strconv.FormatInt(teamID, 10), teamSlug, strconv.FormatInt(groupID, 10))
255-
if err != nil {
256-
return diag.FromErr(err)
257-
}
258-
259-
tflog.Trace(ctx, "Setting resource ID", map[string]any{
260-
"resource_id": newResourceID,
261-
})
262-
d.SetId(newResourceID)
263256
}
264257

265258
tflog.Trace(ctx, "Updated successfully", map[string]any{
@@ -350,7 +343,7 @@ func resourceGithubEMUGroupMappingImport(ctx context.Context, d *schema.Resource
350343
return nil, err
351344
}
352345

353-
resourceID, err := buildID(strconv.FormatInt(teamID, 10), teamSlug, groupIDString)
346+
resourceID, err := buildID(groupIDString, strconv.FormatInt(teamID, 10))
354347
if err != nil {
355348
return nil, err
356349
}

github/resource_github_emu_group_mapping_migration.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package github
22

33
import (
44
"context"
5+
"fmt"
56
"net/http"
67
"strconv"
78

@@ -65,3 +66,56 @@ func resourceGithubEMUGroupMappingStateUpgradeV0(ctx context.Context, rawState m
6566
tflog.Trace(ctx, "GitHub EMU Group Mapping State after migration", map[string]any{"state": rawState})
6667
return rawState, nil
6768
}
69+
70+
func resourceGithubEMUGroupMappingV1() *schema.Resource {
71+
return &schema.Resource{
72+
Schema: map[string]*schema.Schema{
73+
"team_id": {
74+
Type: schema.TypeInt,
75+
Computed: true,
76+
Description: "ID of the GitHub team.",
77+
},
78+
"team_slug": {
79+
Type: schema.TypeString,
80+
Required: true,
81+
Description: "Slug of the GitHub team.",
82+
},
83+
"group_id": {
84+
Type: schema.TypeInt,
85+
Required: true,
86+
ForceNew: true,
87+
Description: "Integer corresponding to the external group ID to be linked.",
88+
},
89+
"group_name": {
90+
Type: schema.TypeString,
91+
Computed: true,
92+
Description: "Name of the external group.",
93+
},
94+
"etag": {
95+
Type: schema.TypeString,
96+
Computed: true,
97+
},
98+
},
99+
}
100+
}
101+
102+
func resourceGithubEMUGroupMappingStateUpgradeV1(ctx context.Context, rawState map[string]any, meta any) (map[string]any, error) {
103+
tflog.Trace(ctx, "GitHub EMU Group Mapping State before migration v1 => v2", map[string]any{"state": rawState})
104+
105+
oldResourceID, ok := rawState["id"].(string)
106+
if !ok {
107+
return nil, fmt.Errorf("id is not a string")
108+
}
109+
teamID, _, groupID, err := parseID3(oldResourceID)
110+
if err != nil {
111+
return nil, fmt.Errorf("could not parse ID: %w", err)
112+
}
113+
resourceID, err := buildID(groupID, teamID)
114+
if err != nil {
115+
return nil, fmt.Errorf("could not build ID: %w", err)
116+
}
117+
rawState["id"] = resourceID
118+
119+
tflog.Trace(ctx, "GitHub EMU Group Mapping State after migration", map[string]any{"state": rawState})
120+
return rawState, nil
121+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package github
2+
3+
import (
4+
"testing"
5+
6+
"github.com/google/go-cmp/cmp"
7+
)
8+
9+
func Test_resourceGithubEMUGroupMappingStateUpgradeV1(t *testing.T) {
10+
t.Parallel()
11+
12+
for _, d := range []struct {
13+
testName string
14+
rawState map[string]any
15+
want map[string]any
16+
shouldError bool
17+
}{
18+
{
19+
testName: "migrates v1 to v2",
20+
rawState: map[string]any{
21+
"id": "123456:test-team:7765543",
22+
},
23+
want: map[string]any{
24+
"id": "7765543:123456",
25+
},
26+
shouldError: false,
27+
},
28+
} {
29+
t.Run(d.testName, func(t *testing.T) {
30+
t.Parallel()
31+
32+
got, err := resourceGithubEMUGroupMappingStateUpgradeV1(t.Context(), d.rawState, nil)
33+
if (err != nil) != d.shouldError {
34+
t.Fatalf("unexpected error state")
35+
}
36+
37+
if diff := cmp.Diff(got, d.want); !d.shouldError && diff != "" {
38+
t.Fatalf("got %+v, want %+v: %s", got, d.want, diff)
39+
}
40+
})
41+
}
42+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.26
44

55
require (
66
github.com/go-jose/go-jose/v3 v3.0.4
7+
github.com/google/go-cmp v0.7.0
78
github.com/google/go-github/v84 v84.0.0
89
github.com/google/uuid v1.6.0
910
github.com/hashicorp/go-cty v1.5.0
@@ -22,7 +23,6 @@ require (
2223
github.com/cloudflare/circl v1.6.1 // indirect
2324
github.com/fatih/color v1.18.0 // indirect
2425
github.com/golang/protobuf v1.5.4 // indirect
25-
github.com/google/go-cmp v0.7.0 // indirect
2626
github.com/google/go-querystring v1.2.0 // indirect
2727
github.com/hashicorp/errwrap v1.0.0 // indirect
2828
github.com/hashicorp/go-checkpoint v0.5.0 // indirect

0 commit comments

Comments
 (0)