Skip to content

Commit f81dd66

Browse files
committed
refactor(github): migrate actions secret to context-aware CRUD
Migrate resource_github_actions_secret.go from legacy CRUD functions to Context-aware functions (CreateContext, ReadContext, DeleteContext). - Add diag import - Update function signatures to accept context.Context - Return diag.Diagnostics instead of error - Use StateContext for importer Ref: #2996
1 parent cb29e9a commit f81dd66

1 file changed

Lines changed: 24 additions & 26 deletions

File tree

github/resource_github_actions_secret.go

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@ import (
1010
"strings"
1111

1212
"github.com/google/go-github/v81/github"
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1314
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1415
"golang.org/x/crypto/nacl/box"
1516
)
1617

1718
func resourceGithubActionsSecret() *schema.Resource {
1819
return &schema.Resource{
19-
Create: resourceGithubActionsSecretCreateOrUpdate,
20-
Read: resourceGithubActionsSecretRead,
21-
Delete: resourceGithubActionsSecretDelete,
20+
CreateContext: resourceGithubActionsSecretCreateOrUpdate,
21+
ReadContext: resourceGithubActionsSecretRead,
22+
DeleteContext: resourceGithubActionsSecretDelete,
2223
Importer: &schema.ResourceImporter{
23-
State: resourceGithubActionsSecretImport,
24+
StateContext: resourceGithubActionsSecretImport,
2425
},
2526

2627
// Schema migration added to handle the addition of destroy_on_drift field
@@ -85,10 +86,9 @@ func resourceGithubActionsSecret() *schema.Resource {
8586
}
8687
}
8788

88-
func resourceGithubActionsSecretCreateOrUpdate(d *schema.ResourceData, meta any) error {
89+
func resourceGithubActionsSecretCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
8990
client := meta.(*Owner).v3client
9091
owner := meta.(*Owner).name
91-
ctx := context.Background()
9292

9393
repo := d.Get("repository").(string)
9494
secretName := d.Get("secret_name").(string)
@@ -97,15 +97,15 @@ func resourceGithubActionsSecretCreateOrUpdate(d *schema.ResourceData, meta any)
9797

9898
keyId, publicKey, err := getPublicKeyDetails(owner, repo, meta)
9999
if err != nil {
100-
return err
100+
return diag.FromErr(err)
101101
}
102102

103103
if encryptedText, ok := d.GetOk("encrypted_value"); ok {
104104
encryptedValue = encryptedText.(string)
105105
} else {
106106
encryptedBytes, err := encryptPlaintext(plaintextValue, publicKey)
107107
if err != nil {
108-
return err
108+
return diag.FromErr(err)
109109
}
110110
encryptedValue = base64.StdEncoding.EncodeToString(encryptedBytes)
111111
}
@@ -119,21 +119,20 @@ func resourceGithubActionsSecretCreateOrUpdate(d *schema.ResourceData, meta any)
119119

120120
_, err = client.Actions.CreateOrUpdateRepoSecret(ctx, owner, repo, eSecret)
121121
if err != nil {
122-
return err
122+
return diag.FromErr(err)
123123
}
124124

125125
d.SetId(buildTwoPartID(repo, secretName))
126-
return resourceGithubActionsSecretRead(d, meta)
126+
return resourceGithubActionsSecretRead(ctx, d, meta)
127127
}
128128

129-
func resourceGithubActionsSecretRead(d *schema.ResourceData, meta any) error {
129+
func resourceGithubActionsSecretRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
130130
client := meta.(*Owner).v3client
131131
owner := meta.(*Owner).name
132-
ctx := context.Background()
133132

134133
repoName, secretName, err := parseTwoPartID(d.Id(), "repository", "secret_name")
135134
if err != nil {
136-
return err
135+
return diag.FromErr(err)
137136
}
138137

139138
secret, _, err := client.Actions.GetRepoSecret(ctx, owner, repoName, secretName)
@@ -147,11 +146,11 @@ func resourceGithubActionsSecretRead(d *schema.ResourceData, meta any) error {
147146
return nil
148147
}
149148
}
150-
return err
149+
return diag.FromErr(err)
151150
}
152151

153152
if err = d.Set("created_at", secret.CreatedAt.String()); err != nil {
154-
return err
153+
return diag.FromErr(err)
155154
}
156155

157156
// This is a drift detection mechanism based on timestamps.
@@ -179,48 +178,47 @@ func resourceGithubActionsSecretRead(d *schema.ResourceData, meta any) error {
179178
// Alternative approach: set sensitive values to empty to trigger update plan
180179
// This tells Terraform that the current state is unknown and needs reconciliation
181180
if err = d.Set("encrypted_value", ""); err != nil {
182-
return err
181+
return diag.FromErr(err)
183182
}
184183
if err = d.Set("plaintext_value", ""); err != nil {
185-
return err
184+
return diag.FromErr(err)
186185
}
187186
log.Printf("[INFO] Detected drift but destroy_on_drift=false, clearing sensitive values to trigger update")
188187
}
189188
} else {
190189
// No drift detected, preserve the configured values in state
191190
if err = d.Set("encrypted_value", d.Get("encrypted_value")); err != nil {
192-
return err
191+
return diag.FromErr(err)
193192
}
194193
if err = d.Set("plaintext_value", d.Get("plaintext_value")); err != nil {
195-
return err
194+
return diag.FromErr(err)
196195
}
197196
} // Always update the timestamp to prevent repeated drift detection
198197
if err = d.Set("updated_at", secret.UpdatedAt.String()); err != nil {
199-
return err
198+
return diag.FromErr(err)
200199
}
201200

202201
return nil
203202
}
204203

205-
func resourceGithubActionsSecretDelete(d *schema.ResourceData, meta any) error {
204+
func resourceGithubActionsSecretDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
206205
client := meta.(*Owner).v3client
207206
orgName := meta.(*Owner).name
208-
ctx := context.WithValue(context.Background(), ctxId, d.Id())
207+
ctx = context.WithValue(ctx, ctxId, d.Id())
209208

210209
repoName, secretName, err := parseTwoPartID(d.Id(), "repository", "secret_name")
211210
if err != nil {
212-
return err
211+
return diag.FromErr(err)
213212
}
214213

215214
_, err = client.Actions.DeleteRepoSecret(ctx, orgName, repoName, secretName)
216215

217-
return err
216+
return diag.FromErr(err)
218217
}
219218

220-
func resourceGithubActionsSecretImport(d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
219+
func resourceGithubActionsSecretImport(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
221220
client := meta.(*Owner).v3client
222221
owner := meta.(*Owner).name
223-
ctx := context.Background()
224222

225223
parts := strings.Split(d.Id(), "/")
226224
if len(parts) != 2 {

0 commit comments

Comments
 (0)