Skip to content

Commit edd0100

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

2 files changed

Lines changed: 53 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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99

1010
"github.com/google/go-github/v82/github"
11+
"github.com/hashicorp/terraform-plugin-log/tflog"
1112
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1213
)
1314

@@ -109,3 +110,53 @@ func diffSecretVariableVisibility(ctx context.Context, d *schema.ResourceDiff, _
109110

110111
return nil
111112
}
113+
114+
// diffTeam compares the team_id and team_slug fields to determine if the team has changed.
115+
func diffTeam(ctx context.Context, diff *schema.ResourceDiff, m any) error {
116+
// Skip for new resources - no existing team_id to compare against
117+
if len(diff.Id()) == 0 {
118+
return nil
119+
}
120+
121+
if diff.HasChange("team_slug") {
122+
if isNewTeamID(ctx, diff, m) {
123+
return diff.ForceNew("team_slug")
124+
}
125+
}
126+
127+
return nil
128+
}
129+
130+
// helper function to determine if the team has changed or was renamed.
131+
func isNewTeamID(ctx context.Context, diff *schema.ResourceDiff, m any) bool {
132+
// Get old team_id from state
133+
oldTeamID := toInt64(diff.Get("team_id"))
134+
if oldTeamID == 0 {
135+
return false
136+
}
137+
meta := m.(*Owner)
138+
139+
// Resolve new team_slug to team ID via API
140+
oldTeamSlug, newTeamSlug := diff.GetChange("team_slug")
141+
newTeamID, err := lookupTeamID(ctx, meta, newTeamSlug.(string))
142+
if err != nil {
143+
// If team doesn't exist or API fails, skip ForceNew check and let Read handle it
144+
tflog.Debug(ctx, "Unable to resolve new team_slug to team ID, skipping ForceNew check", map[string]any{
145+
"new_team_slug": newTeamSlug,
146+
"error": err.Error(),
147+
})
148+
return false
149+
}
150+
151+
if newTeamID != oldTeamID {
152+
tflog.Debug(ctx, "Team ID changed, forcing new resource", map[string]any{
153+
"old_team_id": oldTeamID,
154+
"new_team_id": newTeamID,
155+
"new_team_slug": newTeamSlug,
156+
"old_team_slug": oldTeamSlug,
157+
})
158+
return true
159+
}
160+
161+
return false
162+
}

0 commit comments

Comments
 (0)