Skip to content

Commit ac1b510

Browse files
committed
Refactor to use tflog in resource_github_organization_ruleset.go
Signed-off-by: Timo Sand <[email protected]>
1 parent 903cd04 commit ac1b510

1 file changed

Lines changed: 119 additions & 12 deletions

File tree

github/resource_github_organization_ruleset.go

Lines changed: 119 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"log"
87
"net/http"
98
"strconv"
109

1110
"github.com/google/go-github/v81/github"
11+
"github.com/hashicorp/terraform-plugin-log/tflog"
1212
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1313
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1414
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
@@ -598,13 +598,23 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
598598

599599
func resourceGithubOrganizationRulesetCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
600600
client := meta.(*Owner).v3client
601-
602601
owner := meta.(*Owner).name
602+
name := d.Get("name").(string)
603+
604+
tflog.Debug(ctx, fmt.Sprintf("Creating organization ruleset: %s/%s", owner, name), map[string]any{
605+
"owner": owner,
606+
"name": name,
607+
})
603608

604609
rulesetReq := resourceGithubRulesetObject(d, owner)
605610

606611
ruleset, resp, err := client.Organizations.CreateRepositoryRuleset(ctx, owner, rulesetReq)
607612
if err != nil {
613+
tflog.Error(ctx, fmt.Sprintf("Failed to create organization ruleset: %s/%s", owner, name), map[string]any{
614+
"owner": owner,
615+
"name": name,
616+
"error": err.Error(),
617+
})
608618
return diag.FromErr(err)
609619
}
610620

@@ -613,16 +623,31 @@ func resourceGithubOrganizationRulesetCreate(ctx context.Context, d *schema.Reso
613623
_ = d.Set("node_id", ruleset.GetNodeID())
614624
_ = d.Set("etag", resp.Header.Get("ETag"))
615625

626+
tflog.Info(ctx, fmt.Sprintf("Created organization ruleset: %s/%s (ID: %d)", owner, name, *ruleset.ID), map[string]any{
627+
"owner": owner,
628+
"name": name,
629+
"ruleset_id": *ruleset.ID,
630+
})
631+
616632
return nil
617633
}
618634

619635
func resourceGithubOrganizationRulesetRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
620636
client := meta.(*Owner).v3client
621-
622637
owner := meta.(*Owner).name
623638

639+
tflog.Trace(ctx, fmt.Sprintf("Reading organization ruleset: %s", d.Id()), map[string]any{
640+
"owner": owner,
641+
"ruleset_id": d.Id(),
642+
})
643+
624644
rulesetID, err := strconv.ParseInt(d.Id(), 10, 64)
625645
if err != nil {
646+
tflog.Error(ctx, fmt.Sprintf("Could not convert ruleset ID '%s' to int64", d.Id()), map[string]any{
647+
"owner": owner,
648+
"ruleset_id": d.Id(),
649+
"error": err.Error(),
650+
})
626651
return diag.FromErr(unconvertibleIdErr(d.Id(), err))
627652
}
628653

@@ -635,15 +660,26 @@ func resourceGithubOrganizationRulesetRead(ctx context.Context, d *schema.Resour
635660
var ghErr *github.ErrorResponse
636661
if errors.As(err, &ghErr) {
637662
if ghErr.Response.StatusCode == http.StatusNotModified {
663+
tflog.Debug(ctx, "API responded with StatusNotModified, not refreshing state", map[string]any{
664+
"owner": owner,
665+
"ruleset_id": rulesetID,
666+
})
638667
return nil
639668
}
640669
if ghErr.Response.StatusCode == http.StatusNotFound {
641-
log.Printf("[INFO] Removing ruleset %s: %d from state because it no longer exists in GitHub",
642-
owner, rulesetID)
670+
tflog.Info(ctx, fmt.Sprintf("Removing ruleset %s/%d from state because it no longer exists in GitHub", owner, rulesetID), map[string]any{
671+
"owner": owner,
672+
"ruleset_id": rulesetID,
673+
})
643674
d.SetId("")
644675
return nil
645676
}
646677
}
678+
tflog.Error(ctx, fmt.Sprintf("Failed to read organization ruleset: %s/%d", owner, rulesetID), map[string]any{
679+
"owner": owner,
680+
"ruleset_id": rulesetID,
681+
"error": err.Error(),
682+
})
647683
return diag.FromErr(err)
648684
}
649685

@@ -657,23 +693,45 @@ func resourceGithubOrganizationRulesetRead(ctx context.Context, d *schema.Resour
657693
_ = d.Set("node_id", ruleset.GetNodeID())
658694
_ = d.Set("etag", resp.Header.Get("ETag"))
659695

696+
tflog.Trace(ctx, fmt.Sprintf("Successfully read organization ruleset: %s/%d", owner, rulesetID), map[string]any{
697+
"owner": owner,
698+
"ruleset_id": rulesetID,
699+
"name": ruleset.Name,
700+
})
701+
660702
return nil
661703
}
662704

663705
func resourceGithubOrganizationRulesetUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
664706
client := meta.(*Owner).v3client
665-
666707
owner := meta.(*Owner).name
667-
668-
rulesetReq := resourceGithubRulesetObject(d, owner)
708+
name := d.Get("name").(string)
669709

670710
rulesetID, err := strconv.ParseInt(d.Id(), 10, 64)
671711
if err != nil {
712+
tflog.Error(ctx, fmt.Sprintf("Could not convert ruleset ID '%s' to int64", d.Id()), map[string]any{
713+
"owner": owner,
714+
"ruleset_id": d.Id(),
715+
"error": err.Error(),
716+
})
672717
return diag.FromErr(unconvertibleIdErr(d.Id(), err))
673718
}
674719

720+
tflog.Debug(ctx, fmt.Sprintf("Updating organization ruleset: %s/%d", owner, rulesetID), map[string]any{
721+
"owner": owner,
722+
"ruleset_id": rulesetID,
723+
"name": name,
724+
})
725+
726+
rulesetReq := resourceGithubRulesetObject(d, owner)
727+
675728
ruleset, resp, err := client.Organizations.UpdateRepositoryRuleset(ctx, owner, rulesetID, rulesetReq)
676729
if err != nil {
730+
tflog.Error(ctx, fmt.Sprintf("Failed to update organization ruleset: %s/%d", owner, rulesetID), map[string]any{
731+
"owner": owner,
732+
"ruleset_id": rulesetID,
733+
"error": err.Error(),
734+
})
677735
return diag.FromErr(err)
678736
}
679737

@@ -682,6 +740,12 @@ func resourceGithubOrganizationRulesetUpdate(ctx context.Context, d *schema.Reso
682740
_ = d.Set("node_id", ruleset.GetNodeID())
683741
_ = d.Set("etag", resp.Header.Get("ETag"))
684742

743+
tflog.Info(ctx, fmt.Sprintf("Updated organization ruleset: %s/%d", owner, rulesetID), map[string]any{
744+
"owner": owner,
745+
"ruleset_id": rulesetID,
746+
"name": name,
747+
})
748+
685749
return nil
686750
}
687751

@@ -691,36 +755,79 @@ func resourceGithubOrganizationRulesetDelete(ctx context.Context, d *schema.Reso
691755

692756
rulesetID, err := strconv.ParseInt(d.Id(), 10, 64)
693757
if err != nil {
758+
tflog.Error(ctx, fmt.Sprintf("Could not convert ruleset ID '%s' to int64", d.Id()), map[string]any{
759+
"owner": owner,
760+
"ruleset_id": d.Id(),
761+
"error": err.Error(),
762+
})
694763
return diag.FromErr(unconvertibleIdErr(d.Id(), err))
695764
}
696765

697-
log.Printf("[DEBUG] Deleting organization ruleset: %s: %d", owner, rulesetID)
766+
tflog.Debug(ctx, fmt.Sprintf("Deleting organization ruleset: %s/%d", owner, rulesetID), map[string]any{
767+
"owner": owner,
768+
"ruleset_id": rulesetID,
769+
})
770+
698771
_, err = client.Organizations.DeleteRepositoryRuleset(ctx, owner, rulesetID)
699772
if err != nil {
773+
tflog.Error(ctx, fmt.Sprintf("Failed to delete organization ruleset: %s/%d", owner, rulesetID), map[string]any{
774+
"owner": owner,
775+
"ruleset_id": rulesetID,
776+
"error": err.Error(),
777+
})
700778
return diag.FromErr(err)
701779
}
702780

781+
tflog.Info(ctx, fmt.Sprintf("Deleted organization ruleset: %s/%d", owner, rulesetID), map[string]any{
782+
"owner": owner,
783+
"ruleset_id": rulesetID,
784+
})
785+
703786
return nil
704787
}
705788

706789
func resourceGithubOrganizationRulesetImport(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
790+
client := meta.(*Owner).v3client
791+
owner := meta.(*Owner).name
792+
707793
rulesetID, err := strconv.ParseInt(d.Id(), 10, 64)
708794
if err != nil {
795+
tflog.Error(ctx, fmt.Sprintf("Could not convert ruleset ID '%s' to int64", d.Id()), map[string]any{
796+
"owner": owner,
797+
"ruleset_id": d.Id(),
798+
"error": err.Error(),
799+
})
709800
return []*schema.ResourceData{d}, unconvertibleIdErr(d.Id(), err)
710801
}
711802
if rulesetID == 0 {
803+
tflog.Error(ctx, "ruleset_id must be present and non-zero", map[string]any{
804+
"owner": owner,
805+
"ruleset_id": rulesetID,
806+
})
712807
return []*schema.ResourceData{d}, fmt.Errorf("`ruleset_id` must be present")
713808
}
714-
log.Printf("[DEBUG] Importing organization ruleset with ID: %d", rulesetID)
715809

716-
client := meta.(*Owner).v3client
717-
owner := meta.(*Owner).name
810+
tflog.Debug(ctx, fmt.Sprintf("Importing organization ruleset: %s/%d", owner, rulesetID), map[string]any{
811+
"owner": owner,
812+
"ruleset_id": rulesetID,
813+
})
718814

719815
ruleset, _, err := client.Organizations.GetRepositoryRuleset(ctx, owner, rulesetID)
720816
if ruleset == nil || err != nil {
817+
tflog.Error(ctx, fmt.Sprintf("Failed to import organization ruleset: %s/%d", owner, rulesetID), map[string]any{
818+
"owner": owner,
819+
"ruleset_id": rulesetID,
820+
"error": err.Error(),
821+
})
721822
return []*schema.ResourceData{d}, err
722823
}
723824
d.SetId(strconv.FormatInt(ruleset.GetID(), 10))
724825

826+
tflog.Info(ctx, fmt.Sprintf("Imported organization ruleset: %s/%d (name: %s)", owner, rulesetID, ruleset.Name), map[string]any{
827+
"owner": owner,
828+
"ruleset_id": rulesetID,
829+
"name": ruleset.Name,
830+
})
831+
725832
return []*schema.ResourceData{d}, nil
726833
}

0 commit comments

Comments
 (0)