77 "strings"
88
99 "github.com/google/go-github/v67/github"
10+ "github.com/hashicorp/terraform-plugin-log/tflog"
1011 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1112 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1213)
@@ -50,6 +51,10 @@ func resourceGithubEMUGroupMappingCreate(ctx context.Context, d *schema.Resource
5051}
5152
5253func resourceGithubEMUGroupMappingRead (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
54+ tflog .Trace (ctx , "Reading EMU group mapping" , map [string ]any {
55+ "resource_id" : d .Id (),
56+ })
57+
5358 err := checkOrganization (meta )
5459 if err != nil {
5560 return diag .FromErr (err )
@@ -68,33 +73,69 @@ func resourceGithubEMUGroupMappingRead(ctx context.Context, d *schema.ResourceDa
6873
6974 ctx = context .WithValue (ctx , ctxId , d .Id ())
7075
76+ tflog .Debug (ctx , "Querying external group from GitHub API" , map [string ]any {
77+ "org_name" : orgName ,
78+ "group_id" : id64 ,
79+ })
80+
7181 group , resp , err := client .Teams .GetExternalGroup (ctx , orgName , id64 )
7282 if err != nil {
7383 if resp != nil && resp .StatusCode == 404 {
7484 // If the group is not found, remove it from state
85+ tflog .Info (ctx , "Removing EMU group mapping from state because it no longer exists in GitHub" , map [string ]any {
86+ "org_name" : orgName ,
87+ "group_id" : id64 ,
88+ "resource_id" : d .Id (),
89+ "status_code" : resp .StatusCode ,
90+ })
7591 d .SetId ("" )
7692 return nil
7793 }
7894 return diag .FromErr (err )
7995 }
8096
97+ tflog .Debug (ctx , "Successfully retrieved external group from GitHub API" , map [string ]any {
98+ "org_name" : orgName ,
99+ "group_id" : id64 ,
100+ "team_count" : len (group .Teams ),
101+ })
102+
81103 if len (group .Teams ) < 1 {
82104 // if there's not a team linked, that means it was removed outside of terraform
83105 // and we should remove it from our state
106+ tflog .Info (ctx , "Removing EMU group mapping from state because no teams are linked" , map [string ]any {
107+ "org_name" : orgName ,
108+ "group_id" : id64 ,
109+ "resource_id" : d .Id (),
110+ })
84111 d .SetId ("" )
85112 return nil
86113 }
87114
88- if err = d .Set ("etag" , resp .Header .Get ("ETag" )); err != nil {
115+ etag := resp .Header .Get ("ETag" )
116+ tflog .Trace (ctx , "Setting state attribute: etag" , map [string ]any {
117+ "etag" : etag ,
118+ })
119+ if err = d .Set ("etag" , etag ); err != nil {
89120 return diag .FromErr (err )
90121 }
91- if err = d .Set ("group_id" , int (* group .GroupID )); err != nil {
122+
123+ groupIDInt := int (* group .GroupID )
124+ tflog .Trace (ctx , "Setting state attribute: group_id" , map [string ]any {
125+ "group_id" : groupIDInt ,
126+ })
127+ if err = d .Set ("group_id" , groupIDInt ); err != nil {
92128 return diag .FromErr (err )
93129 }
94130 return nil
95131}
96132
97133func resourceGithubEMUGroupMappingUpdate (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
134+ resourceID := d .Id ()
135+ tflog .Trace (ctx , "Updating EMU group mapping" , map [string ]any {
136+ "resource_id" : resourceID ,
137+ })
138+
98139 err := checkOrganization (meta )
99140 if err != nil {
100141 return diag .FromErr (err )
@@ -117,20 +158,46 @@ func resourceGithubEMUGroupMappingUpdate(ctx context.Context, d *schema.Resource
117158 return diag .FromErr (err )
118159 }
119160
161+ teamSlugStr := teamSlug .(string )
162+
120163 eg := & github.ExternalGroup {
121164 GroupID : & id64 ,
122165 }
123166
124- _ , _ , err = client .Teams .UpdateConnectedExternalGroup (ctx , orgName , teamSlug .(string ), eg )
167+ tflog .Debug (ctx , "Updating connected external group via GitHub API" , map [string ]any {
168+ "org_name" : orgName ,
169+ "team_slug" : teamSlugStr ,
170+ "group_id" : id64 ,
171+ })
172+
173+ _ , _ , err = client .Teams .UpdateConnectedExternalGroup (ctx , orgName , teamSlugStr , eg )
125174 if err != nil {
126175 return diag .FromErr (err )
127176 }
128177
129- d .SetId (fmt .Sprintf ("teams/%s/external-groups" , teamSlug ))
178+ tflog .Debug (ctx , "Successfully updated connected external group" , map [string ]any {
179+ "org_name" : orgName ,
180+ "team_slug" : teamSlugStr ,
181+ "group_id" : id64 ,
182+ })
183+
184+ newResourceID := fmt .Sprintf ("teams/%s/external-groups" , teamSlugStr )
185+ tflog .Trace (ctx , "Setting resource ID" , map [string ]any {
186+ "resource_id" : newResourceID ,
187+ })
188+ d .SetId (newResourceID )
189+
190+ tflog .Trace (ctx , "Transitioning to read operation" , map [string ]any {
191+ "resource_id" : newResourceID ,
192+ })
130193 return resourceGithubEMUGroupMappingRead (ctx , d , meta )
131194}
132195
133196func resourceGithubEMUGroupMappingDelete (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
197+ tflog .Trace (ctx , "Deleting EMU group mapping" , map [string ]any {
198+ "resource_id" : d .Id (),
199+ })
200+
134201 err := checkOrganization (meta )
135202 if err != nil {
136203 return diag .FromErr (err )
@@ -145,10 +212,23 @@ func resourceGithubEMUGroupMappingDelete(ctx context.Context, d *schema.Resource
145212
146213 ctx = context .WithValue (ctx , ctxId , d .Id ())
147214
148- _ , err = client .Teams .RemoveConnectedExternalGroup (ctx , orgName , teamSlug .(string ))
215+ teamSlugStr := teamSlug .(string )
216+ tflog .Debug (ctx , "Removing connected external group from team via GitHub API" , map [string ]any {
217+ "org_name" : orgName ,
218+ "team_slug" : teamSlugStr ,
219+ "resource_id" : d .Id (),
220+ })
221+
222+ _ , err = client .Teams .RemoveConnectedExternalGroup (ctx , orgName , teamSlugStr )
149223 if err != nil {
150224 return diag .FromErr (err )
151225 }
226+
227+ tflog .Debug (ctx , "Successfully removed connected external group from team" , map [string ]any {
228+ "org_name" : orgName ,
229+ "team_slug" : teamSlugStr ,
230+ "resource_id" : d .Id (),
231+ })
152232 return nil
153233}
154234
@@ -171,7 +251,13 @@ func getInt64FromInterface(val any) (int64, error) {
171251 return id64 , nil
172252}
173253
174- func importWithTwoPartID (_ context.Context , d * schema.ResourceData , _ any ) ([]* schema.ResourceData , error ) {
254+ func importWithTwoPartID (ctx context.Context , d * schema.ResourceData , _ any ) ([]* schema.ResourceData , error ) {
255+ importID := d .Id ()
256+ tflog .Trace (ctx , "Importing EMU group mapping with two-part ID" , map [string ]any {
257+ "import_id" : importID ,
258+ "strategy" : "two_part_id" ,
259+ })
260+
175261 groupIDString , teamSlug , err := parseTwoPartID (d .Id (), "group_id" , "team_slug" )
176262 if err != nil {
177263 return nil , err
@@ -180,37 +266,101 @@ func importWithTwoPartID(_ context.Context, d *schema.ResourceData, _ any) ([]*s
180266 if err != nil {
181267 return nil , err
182268 }
269+
270+ tflog .Debug (ctx , "Parsed two-part import ID" , map [string ]any {
271+ "import_id" : importID ,
272+ "group_id" : groupID ,
273+ "team_slug" : teamSlug ,
274+ })
275+
276+ tflog .Trace (ctx , "Setting state attribute: group_id" , map [string ]any {
277+ "group_id" : groupID ,
278+ })
183279 if err := d .Set ("group_id" , groupID ); err != nil {
184280 return nil , err
185281 }
282+
283+ tflog .Trace (ctx , "Setting state attribute: team_slug" , map [string ]any {
284+ "team_slug" : teamSlug ,
285+ })
186286 if err := d .Set ("team_slug" , teamSlug ); err != nil {
187287 return nil , err
188288 }
189- d .SetId (fmt .Sprintf ("teams/%s/external-groups" , teamSlug ))
289+
290+ resourceID := fmt .Sprintf ("teams/%s/external-groups" , teamSlug )
291+ tflog .Trace (ctx , "Setting resource ID" , map [string ]any {
292+ "resource_id" : resourceID ,
293+ })
294+ d .SetId (resourceID )
190295 return []* schema.ResourceData {d }, nil
191296}
192297
193298func importWithIntegerID (ctx context.Context , d * schema.ResourceData , meta any ) ([]* schema.ResourceData , error ) {
299+ importID := d .Id ()
300+ tflog .Trace (ctx , "Importing EMU group mapping with integer ID" , map [string ]any {
301+ "import_id" : importID ,
302+ "strategy" : "integer_id" ,
303+ })
304+
194305 groupID , err := strconv .Atoi (d .Id ())
195306 if err != nil {
196307 return nil , err
197308 }
309+
310+ tflog .Debug (ctx , "Parsed integer import ID" , map [string ]any {
311+ "import_id" : importID ,
312+ "group_id" : groupID ,
313+ })
314+
315+ tflog .Trace (ctx , "Setting state attribute: group_id" , map [string ]any {
316+ "group_id" : groupID ,
317+ })
198318 if err := d .Set ("group_id" , groupID ); err != nil {
199319 return nil , err
200320 }
201321 ctx = context .WithValue (ctx , ctxId , d .Id ())
202322 client := meta .(* Owner ).v3client
203323 orgName := meta .(* Owner ).name
324+
325+ tflog .Debug (ctx , "Querying external group from GitHub API for import" , map [string ]any {
326+ "org_name" : orgName ,
327+ "group_id" : groupID ,
328+ "import_id" : importID ,
329+ })
330+
204331 group , _ , err := client .Teams .GetExternalGroup (ctx , orgName , int64 (groupID ))
205332 if err != nil {
206333 return nil , err
207334 }
335+
336+ tflog .Debug (ctx , "Successfully retrieved external group from GitHub API for import" , map [string ]any {
337+ "org_name" : orgName ,
338+ "group_id" : groupID ,
339+ "team_count" : len (group .Teams ),
340+ })
341+
208342 if len (group .Teams ) != 1 {
343+ tflog .Info (ctx , "Multiple teams found for external group during import" , map [string ]any {
344+ "org_name" : orgName ,
345+ "group_id" : groupID ,
346+ "team_count" : len (group .Teams ),
347+ "import_id" : importID ,
348+ })
209349 return nil , fmt .Errorf ("could not get team_slug from %v number of teams" , len (group .Teams ))
210350 }
211- if err := d .Set ("team_slug" , group .Teams [0 ].TeamName ); err != nil {
351+
352+ teamSlug := * group .Teams [0 ].TeamName
353+ tflog .Trace (ctx , "Setting state attribute: team_slug" , map [string ]any {
354+ "team_slug" : teamSlug ,
355+ })
356+ if err := d .Set ("team_slug" , teamSlug ); err != nil {
212357 return nil , err
213358 }
214- d .SetId (fmt .Sprintf ("teams/%s/external-groups" , * group .Teams [0 ].TeamName ))
359+
360+ resourceID := fmt .Sprintf ("teams/%s/external-groups" , teamSlug )
361+ tflog .Trace (ctx , "Setting resource ID" , map [string ]any {
362+ "resource_id" : resourceID ,
363+ })
364+ d .SetId (resourceID )
215365 return []* schema.ResourceData {d }, nil
216366}
0 commit comments