Skip to content

Commit 710d9b8

Browse files
committed
Restructure github_actions_secret state migration files
Signed-off-by: Timo Sand <[email protected]>
1 parent 9dc9f41 commit 710d9b8

4 files changed

Lines changed: 62 additions & 70 deletions

github/migrate_github_actions_secret_test.go

Lines changed: 0 additions & 55 deletions
This file was deleted.

github/resource_github_actions_secret.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,3 +292,15 @@ func encryptPlaintext(plaintext, publicKeyB64 string) ([]byte, error) {
292292

293293
return cipherText, nil
294294
}
295+
296+
func resourceGithubActionsSecretInstanceStateUpgradeV0(ctx context.Context, rawState map[string]any, meta any) (map[string]any, error) {
297+
log.Printf("[DEBUG] GitHub Actions Secret State before migration: %#v", rawState)
298+
// Add the destroy_on_drift field with default value true if it doesn't exist
299+
if _, ok := rawState["destroy_on_drift"]; !ok {
300+
rawState["destroy_on_drift"] = true
301+
}
302+
303+
log.Printf("[DEBUG] GitHub Actions Secret State after migration: %#v", rawState)
304+
305+
return rawState, nil
306+
}

github/migrate_github_actions_secret.go renamed to github/resource_github_actions_secret_schema_v0.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package github
22

33
import (
4-
"context"
5-
"log"
6-
74
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
85
)
96

@@ -52,15 +49,3 @@ func resourceGithubActionsSecretResourceV0() *schema.Resource {
5249
},
5350
}
5451
}
55-
56-
func resourceGithubActionsSecretInstanceStateUpgradeV0(ctx context.Context, rawState map[string]any, meta any) (map[string]any, error) {
57-
log.Printf("[DEBUG] GitHub Actions Secret State before migration: %#v", rawState)
58-
// Add the destroy_on_drift field with default value true if it doesn't exist
59-
if _, ok := rawState["destroy_on_drift"]; !ok {
60-
rawState["destroy_on_drift"] = true
61-
}
62-
63-
log.Printf("[DEBUG] GitHub Actions Secret State after migration: %#v", rawState)
64-
65-
return rawState, nil
66-
}

github/resource_github_actions_secret_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package github
33
import (
44
"encoding/base64"
55
"fmt"
6+
"reflect"
67
"strings"
78
"testing"
89

@@ -553,3 +554,52 @@ func TestGithubActionsSecretIssue964Solution(t *testing.T) {
553554
t.Logf("SUCCESS: Issue #964 solved - secret with destroy_on_drift=false does not get recreated on external changes")
554555
})
555556
}
557+
558+
func testResourceGithubActionsSecretInstanceStateDataV0() map[string]any {
559+
return map[string]any{
560+
"id": "test-secret",
561+
"repository": "test-repo",
562+
"secret_name": "test-secret",
563+
"created_at": "2023-01-01T00:00:00Z",
564+
"updated_at": "2023-01-01T00:00:00Z",
565+
"plaintext_value": "secret-value",
566+
}
567+
}
568+
569+
func testResourceGithubActionsSecretInstanceStateDataV0_WithDrift() map[string]any {
570+
v0 := testResourceGithubActionsSecretInstanceStateDataV0()
571+
v0["destroy_on_drift"] = false
572+
return v0
573+
}
574+
575+
func testResourceGithubActionsSecretInstanceStateDataV1() map[string]any {
576+
v0 := testResourceGithubActionsSecretInstanceStateDataV0()
577+
v0["destroy_on_drift"] = true
578+
return v0
579+
}
580+
581+
func TestGithub_MigrateActionsSecretStateV0toV1(t *testing.T) {
582+
t.Run("without destroy_on_drift", func(t *testing.T) {
583+
expected := testResourceGithubActionsSecretInstanceStateDataV1()
584+
actual, err := resourceGithubActionsSecretInstanceStateUpgradeV0(t.Context(), testResourceGithubActionsSecretInstanceStateDataV0(), nil)
585+
if err != nil {
586+
t.Fatalf("error migrating state: %s", err)
587+
}
588+
589+
if !reflect.DeepEqual(expected, actual) {
590+
t.Fatalf("\n\nexpected:\n\n%#v\n\ngot:\n\n%#v\n\n", expected, actual)
591+
}
592+
})
593+
594+
t.Run("with destroy_on_drift", func(t *testing.T) {
595+
expected := testResourceGithubActionsSecretInstanceStateDataV0_WithDrift()
596+
actual, err := resourceGithubActionsSecretInstanceStateUpgradeV0(t.Context(), testResourceGithubActionsSecretInstanceStateDataV0_WithDrift(), nil)
597+
if err != nil {
598+
t.Fatalf("error migrating state: %s", err)
599+
}
600+
601+
if !reflect.DeepEqual(expected, actual) {
602+
t.Fatalf("\n\nexpected:\n\n%#v\n\ngot:\n\n%#v\n\n", expected, actual)
603+
}
604+
})
605+
}

0 commit comments

Comments
 (0)