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"
1112 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1213)
1314
@@ -20,6 +21,9 @@ func resourceGithubEMUGroupMapping() *schema.Resource {
2021 Importer : & schema.ResourceImporter {
2122 StateContext : resourceGithubEMUGroupMappingImport ,
2223 },
24+ CustomizeDiff : customdiff .All (
25+ customdiff .ForceNewIf ("team_slug" , hasNewTeamID ),
26+ ),
2327 Description : "Manages the mapping of an external group to a GitHub team." ,
2428 Schema : map [string ]* schema.Schema {
2529 "team_id" : {
@@ -405,3 +409,45 @@ func matchTeamID(ctx context.Context, meta any, teamSlug string, groupTeams []*g
405409 }
406410 return teamID , nil
407411}
412+
413+ func hasNewTeamID (ctx context.Context , diff * schema.ResourceDiff , meta any ) bool {
414+ // Skip for new resources - no existing team_id to compare against
415+ if diff .Id () == "" {
416+ return false
417+ }
418+
419+ // Only check when team_slug changes
420+ if ! diff .HasChange ("team_slug" ) {
421+ return false
422+ }
423+
424+ // Get old team_id from state
425+ oldTeamID := toInt64 (diff .Get ("team_id" ))
426+ if oldTeamID == 0 {
427+ return false
428+ }
429+
430+ // Resolve new team_slug to team ID via API
431+ oldTeamSlug , newTeamSlug := diff .GetChange ("team_slug" )
432+ newTeamID , err := getTeamID (newTeamSlug .(string ), meta )
433+ if err != nil {
434+ // If team doesn't exist or API fails, skip ForceNew check and let Read handle it
435+ tflog .Debug (ctx , "Unable to resolve new team_slug to team ID, skipping ForceNew check" , map [string ]any {
436+ "new_team_slug" : newTeamSlug ,
437+ "error" : err .Error (),
438+ })
439+ return false
440+ }
441+
442+ if newTeamID != oldTeamID {
443+ tflog .Debug (ctx , "Team ID changed, forcing new resource" , map [string ]any {
444+ "old_team_id" : oldTeamID ,
445+ "new_team_id" : newTeamID ,
446+ "new_team_slug" : newTeamSlug ,
447+ "old_team_slug" : oldTeamSlug ,
448+ })
449+ return true
450+ }
451+
452+ return false
453+ }
0 commit comments