Skip to content

Commit 1f4594b

Browse files
committed
refactor provider.go maint: migrate various data sources to tflog structured logging
1 parent e72c809 commit 1f4594b

1 file changed

Lines changed: 43 additions & 19 deletions

File tree

github/provider.go

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ package github
33
import (
44
"context"
55
"fmt"
6-
"log"
76
"net/url"
87
"os"
98
"os/exec"
109
"strings"
1110
"time"
1211

12+
"github.com/hashicorp/terraform-plugin-log/tflog"
1313
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1414
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1515
)
@@ -363,7 +363,7 @@ func providerConfigure(p *schema.Provider) schema.ConfigureContextFunc {
363363
// an explicitly set value in a provider block), but is necessary
364364
// for backwards compatibility. We could remove this backwards compatibility
365365
// code in a future major release.
366-
env := ownerOrOrgEnvDefaultFunc()
366+
env := ownerOrOrgEnvDefaultFunc(ctx)
367367
if env.(string) != "" {
368368
owner = env.(string)
369369
}
@@ -376,7 +376,9 @@ func providerConfigure(p *schema.Provider) schema.ConfigureContextFunc {
376376

377377
org := d.Get("organization").(string)
378378
if org != "" {
379-
log.Printf("[INFO] Selecting organization attribute as owner: %s", org)
379+
tflog.Info(ctx, "Selecting organization attribute as owner", map[string]any{
380+
"org_name": org,
381+
})
380382
owner = org
381383
}
382384

@@ -424,33 +426,44 @@ func providerConfigure(p *schema.Provider) schema.ConfigureContextFunc {
424426
}
425427

426428
if token == "" {
427-
log.Printf("[INFO] No token found, using GitHub CLI to get token from hostname %s", baseURL.Host)
428-
token = tokenFromGHCLI(baseURL)
429+
tflog.Info(ctx, "No token found, using GitHub CLI to get token from hostname", map[string]any{
430+
"hostname": baseURL.Host,
431+
})
432+
token = tokenFromGHCLI(ctx, baseURL)
429433
}
430434

431435
writeDelay := d.Get("write_delay_ms").(int)
432436
if writeDelay <= 0 {
433437
return nil, wrapErrors([]error{fmt.Errorf("write_delay_ms must be greater than 0ms")})
434438
}
435-
log.Printf("[INFO] Setting write_delay_ms to %d", writeDelay)
439+
tflog.Info(ctx, "Setting write_delay_ms", map[string]any{
440+
"write_delay_ms": writeDelay,
441+
})
436442

437443
readDelay := d.Get("read_delay_ms").(int)
438444
if readDelay < 0 {
439445
return nil, wrapErrors([]error{fmt.Errorf("read_delay_ms must be greater than or equal to 0ms")})
440446
}
441-
log.Printf("[DEBUG] Setting read_delay_ms to %d", readDelay)
447+
tflog.Debug(ctx, "Setting read_delay_ms", map[string]any{
448+
"read_delay_ms": readDelay,
449+
})
442450

443451
retryDelay := d.Get("read_delay_ms").(int)
444452
if retryDelay < 0 {
445453
return nil, diag.FromErr(fmt.Errorf("retry_delay_ms must be greater than or equal to 0ms"))
446454
}
447-
log.Printf("[DEBUG] Setting retry_delay_ms to %d", retryDelay)
455+
tflog.Debug(ctx, "Setting retry_delay_ms", map[string]any{
456+
"retry_delay_ms": retryDelay,
457+
})
448458

449459
maxRetries := d.Get("max_retries").(int)
450460
if maxRetries < 0 {
451461
return nil, diag.FromErr(fmt.Errorf("max_retries must be greater than or equal to 0"))
452462
}
453-
log.Printf("[DEBUG] Setting max_retries to %d", maxRetries)
463+
tflog.Debug(ctx, "Setting max_retries", map[string]any{
464+
"max_retries": maxRetries,
465+
})
466+
454467
retryableErrors := make(map[int]bool)
455468
if maxRetries > 0 {
456469
reParam := d.Get("retryable_errors").([]any)
@@ -462,19 +475,24 @@ func providerConfigure(p *schema.Provider) schema.ConfigureContextFunc {
462475
}
463476
}
464477

465-
log.Printf("[DEBUG] Setting retriableErrors to %v", retryableErrors)
478+
tflog.Debug(ctx, "Setting retriableErrors", map[string]any{
479+
"retryable_errors": retryableErrors,
480+
})
466481
}
467482

468483
_maxPerPage := d.Get("max_per_page").(int)
469484
if _maxPerPage <= 0 {
470485
return nil, diag.FromErr(fmt.Errorf("max_per_page must be greater than than 0"))
471486
}
472-
log.Printf("[DEBUG] Setting max_per_page to %d", _maxPerPage)
487+
tflog.Debug(ctx, "Setting max_per_page", map[string]any{
488+
"max_per_page": _maxPerPage,
489+
})
473490
maxPerPage = _maxPerPage
474491

475492
parallelRequests := d.Get("parallel_requests").(bool)
476-
477-
log.Printf("[DEBUG] Setting parallel_requests to %t", parallelRequests)
493+
tflog.Debug(ctx, "Setting parallel_requests", map[string]any{
494+
"parallel_requests": parallelRequests,
495+
})
478496

479497
config := Config{
480498
Token: token,
@@ -513,7 +531,7 @@ func ghCLIHostFromAPIHost(host string) string {
513531
}
514532

515533
// See https://github.com/integrations/terraform-provider-github/issues/1822
516-
func tokenFromGHCLI(u *url.URL) string {
534+
func tokenFromGHCLI(ctx context.Context, u *url.URL) string {
517535
ghCliPath := os.Getenv("GH_PATH")
518536
if ghCliPath == "" {
519537
ghCliPath = "gh"
@@ -523,22 +541,28 @@ func tokenFromGHCLI(u *url.URL) string {
523541

524542
out, err := exec.Command(ghCliPath, "auth", "token", "--hostname", host).Output()
525543
if err != nil {
526-
log.Printf("[DEBUG] Error getting token from GitHub CLI: %s", err.Error())
544+
tflog.Debug(ctx, "Error getting token from GitHub CLI", map[string]any{
545+
"error": err.Error(),
546+
})
527547
// GH CLI is either not installed or there was no `gh auth login` command issued,
528548
// which is fine. don't return the error to keep the flow going
529549
return ""
530550
}
531551

532-
log.Printf("[INFO] Using the token from GitHub CLI")
552+
tflog.Info(ctx, "Using the token from GitHub CLI")
533553
return strings.TrimSpace(string(out))
534554
}
535555

536-
func ownerOrOrgEnvDefaultFunc() any {
556+
func ownerOrOrgEnvDefaultFunc(ctx context.Context) any {
537557
if organization := os.Getenv("GITHUB_ORGANIZATION"); organization != "" {
538-
log.Printf("[INFO] Selecting owner %s from GITHUB_ORGANIZATION environment variable", organization)
558+
tflog.Info(ctx, "Selecting owner from GITHUB_ORGANIZATION environment variable", map[string]any{
559+
"owner": organization,
560+
})
539561
return organization
540562
}
541563
owner := os.Getenv("GITHUB_OWNER")
542-
log.Printf("[INFO] Selecting owner %s from GITHUB_OWNER environment variable", owner)
564+
tflog.Info(ctx, "Selecting owner from GITHUB_OWNER environment variable", map[string]any{
565+
"owner": owner,
566+
})
543567
return owner
544568
}

0 commit comments

Comments
 (0)