Skip to content

Commit c12065a

Browse files
committed
Use new reusable diffing pattern
Signed-off-by: Timo Sand <[email protected]>
1 parent 31f13b2 commit c12065a

2 files changed

Lines changed: 60 additions & 47 deletions

File tree

github/resource_github_emu_group_mapping.go

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/google/go-github/v82/github"
99
"github.com/hashicorp/terraform-plugin-log/tflog"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
1211
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1312
)
1413

@@ -21,10 +20,8 @@ func resourceGithubEMUGroupMapping() *schema.Resource {
2120
Importer: &schema.ResourceImporter{
2221
StateContext: resourceGithubEMUGroupMappingImport,
2322
},
24-
CustomizeDiff: customdiff.All(
25-
customdiff.ForceNewIf("team_slug", hasNewTeamID),
26-
),
27-
Description: "Manages the mapping of an external group to a GitHub team.",
23+
CustomizeDiff: diffTeam,
24+
Description: "Manages the mapping of an external group to a GitHub team.",
2825
Schema: map[string]*schema.Schema{
2926
"team_id": {
3027
Type: schema.TypeString,
@@ -361,45 +358,3 @@ func resourceGithubEMUGroupMappingImport(ctx context.Context, d *schema.Resource
361358

362359
return []*schema.ResourceData{d}, nil
363360
}
364-
365-
func hasNewTeamID(ctx context.Context, diff *schema.ResourceDiff, meta any) bool {
366-
// Skip for new resources - no existing team_id to compare against
367-
if diff.Id() == "" {
368-
return false
369-
}
370-
371-
// Only check when team_slug changes
372-
if !diff.HasChange("team_slug") {
373-
return false
374-
}
375-
376-
// Get old team_id from state
377-
oldTeamID := toInt64(diff.Get("team_id"))
378-
if oldTeamID == 0 {
379-
return false
380-
}
381-
382-
// Resolve new team_slug to team ID via API
383-
oldTeamSlug, newTeamSlug := diff.GetChange("team_slug")
384-
newTeamID, err := lookupTeamID(ctx, meta.(*Owner), newTeamSlug.(string))
385-
if err != nil {
386-
// If team doesn't exist or API fails, skip ForceNew check and let Read handle it
387-
tflog.Debug(ctx, "Unable to resolve new team_slug to team ID, skipping ForceNew check", map[string]any{
388-
"new_team_slug": newTeamSlug,
389-
"error": err.Error(),
390-
})
391-
return false
392-
}
393-
394-
if newTeamID != oldTeamID {
395-
tflog.Debug(ctx, "Team ID changed, forcing new resource", map[string]any{
396-
"old_team_id": oldTeamID,
397-
"new_team_id": newTeamID,
398-
"new_team_slug": newTeamSlug,
399-
"old_team_slug": oldTeamSlug,
400-
})
401-
return true
402-
}
403-
404-
return false
405-
}

github/util_diff.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package github
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-log/tflog"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
9+
10+
// diffTeam compares the team_id and team_slug fields to determine if the team has changed.
11+
func diffTeam(ctx context.Context, diff *schema.ResourceDiff, m any) error {
12+
// Skip for new resources - no existing team_id to compare against
13+
if len(diff.Id()) == 0 {
14+
return nil
15+
}
16+
17+
if diff.HasChange("team_slug") {
18+
if isNewTeamID(ctx, diff, m) {
19+
return diff.ForceNew("team_slug")
20+
}
21+
}
22+
23+
return nil
24+
}
25+
26+
// helper function to determine if the team has changed or was renamed.
27+
func isNewTeamID(ctx context.Context, diff *schema.ResourceDiff, m any) bool {
28+
// Get old team_id from state
29+
oldTeamID := toInt64(diff.Get("team_id"))
30+
if oldTeamID == 0 {
31+
return false
32+
}
33+
meta := m.(*Owner)
34+
35+
// Resolve new team_slug to team ID via API
36+
oldTeamSlug, newTeamSlug := diff.GetChange("team_slug")
37+
newTeamID, err := lookupTeamID(ctx, meta, newTeamSlug.(string))
38+
if err != nil {
39+
// If team doesn't exist or API fails, skip ForceNew check and let Read handle it
40+
tflog.Debug(ctx, "Unable to resolve new team_slug to team ID, skipping ForceNew check", map[string]any{
41+
"new_team_slug": newTeamSlug,
42+
"error": err.Error(),
43+
})
44+
return false
45+
}
46+
47+
if newTeamID != oldTeamID {
48+
tflog.Debug(ctx, "Team ID changed, forcing new resource", map[string]any{
49+
"old_team_id": oldTeamID,
50+
"new_team_id": newTeamID,
51+
"new_team_slug": newTeamSlug,
52+
"old_team_slug": oldTeamSlug,
53+
})
54+
return true
55+
}
56+
57+
return false
58+
}

0 commit comments

Comments
 (0)