Skip to content

Commit 97447a7

Browse files
committed
Refactor to use tflog instead of log package
Signed-off-by: Timo Sand <[email protected]>
1 parent 4ec213d commit 97447a7

2 files changed

Lines changed: 28 additions & 9 deletions

File tree

github/resource_github_membership.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package github
33
import (
44
"context"
55
"errors"
6-
"log"
6+
"fmt"
77
"net/http"
88

99
"github.com/google/go-github/v81/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
)
@@ -108,8 +109,9 @@ func resourceGithubMembershipRead(ctx context.Context, d *schema.ResourceData, m
108109
return nil
109110
}
110111
if ghErr.Response.StatusCode == http.StatusNotFound {
111-
log.Printf("[INFO] Removing membership %s from state because it no longer exists in GitHub",
112-
d.Id())
112+
tflog.Info(ctx, fmt.Sprintf("Removing membership %s from state because it no longer exists in GitHub", d.Id()), map[string]any{
113+
"membership_id": d.Id(),
114+
})
113115
d.SetId("")
114116
return nil
115117
}
@@ -145,7 +147,11 @@ func resourceGithubMembershipDelete(ctx context.Context, d *schema.ResourceData,
145147
downgradeTo := "member"
146148

147149
if downgradeOnDestroy {
148-
log.Printf("[INFO] Downgrading '%s' membership for '%s' to '%s'", orgName, username, downgradeTo)
150+
tflog.Info(ctx, fmt.Sprintf("Downgrading '%s' membership for '%s' to '%s'", orgName, username, downgradeTo), map[string]any{
151+
"org_name": orgName,
152+
"username": username,
153+
"role": downgradeTo,
154+
})
149155

150156
// Check to make sure this member still has access to the organization before downgrading.
151157
// If we don't do this, the member would just be re-added to the organization.
@@ -155,7 +161,10 @@ func resourceGithubMembershipDelete(ctx context.Context, d *schema.ResourceData,
155161
var ghErr *github.ErrorResponse
156162
if errors.As(err, &ghErr) {
157163
if ghErr.Response.StatusCode == http.StatusNotFound {
158-
log.Printf("[INFO] Not downgrading '%s' membership for '%s' because they are not a member of the org anymore", orgName, username)
164+
tflog.Info(ctx, fmt.Sprintf("Not downgrading '%s' membership for '%s' because they are not a member of the org anymore", orgName, username), map[string]any{
165+
"org_name": orgName,
166+
"username": username,
167+
})
159168
return nil
160169
}
161170
}
@@ -164,21 +173,31 @@ func resourceGithubMembershipDelete(ctx context.Context, d *schema.ResourceData,
164173
}
165174

166175
if *membership.Role == downgradeTo {
167-
log.Printf("[INFO] Not downgrading '%s' membership for '%s' because they are already '%s'", orgName, username, downgradeTo)
176+
tflog.Info(ctx, fmt.Sprintf("Not downgrading '%s' membership for '%s' because they are already '%s'", orgName, username, downgradeTo), map[string]any{
177+
"org_name": orgName,
178+
"username": username,
179+
"role": downgradeTo,
180+
})
168181
return nil
169182
}
170183

171184
_, _, err = client.Organizations.EditOrgMembership(ctx, username, orgName, &github.Membership{
172185
Role: github.Ptr(downgradeTo),
173186
})
174187
} else {
175-
log.Printf("[INFO] Revoking '%s' membership for '%s'", orgName, username)
188+
tflog.Info(ctx, fmt.Sprintf("Revoking '%s' membership for '%s'", orgName, username), map[string]any{
189+
"org_name": orgName,
190+
"username": username,
191+
})
176192
_, err = client.Organizations.RemoveOrgMembership(ctx, username, orgName)
177193
if err != nil {
178194
var ghErr *github.ErrorResponse
179195
if errors.As(err, &ghErr) {
180196
if ghErr.Response.StatusCode == http.StatusNotFound {
181-
log.Printf("[INFO] Not removing '%s' membership for '%s' because they are not a member of the org anymore", orgName, username)
197+
tflog.Info(ctx, fmt.Sprintf("Not removing '%s' membership for '%s' because they are not a member of the org anymore", orgName, username), map[string]any{
198+
"org_name": orgName,
199+
"username": username,
200+
})
182201
return nil
183202
}
184203
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/google/go-github/v81 v81.0.0
88
github.com/google/uuid v1.6.0
99
github.com/hashicorp/go-cty v1.5.0
10+
github.com/hashicorp/terraform-plugin-log v0.9.0
1011
github.com/hashicorp/terraform-plugin-sdk/v2 v2.38.1
1112
github.com/shurcooL/githubv4 v0.0.0-20221126192849-0b5c4c7994eb
1213
github.com/stretchr/testify v1.11.1
@@ -39,7 +40,6 @@ require (
3940
github.com/hashicorp/terraform-exec v0.23.1 // indirect
4041
github.com/hashicorp/terraform-json v0.27.1 // indirect
4142
github.com/hashicorp/terraform-plugin-go v0.29.0 // indirect
42-
github.com/hashicorp/terraform-plugin-log v0.9.0 // indirect
4343
github.com/hashicorp/terraform-registry-address v0.4.0 // indirect
4444
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
4545
github.com/hashicorp/yamux v0.1.2 // indirect

0 commit comments

Comments
 (0)