Skip to content

Commit fe3ced8

Browse files
committed
Add schema migration
1 parent e3914b1 commit fe3ced8

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package github
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
8+
)
9+
10+
func resourceGithubActionsSecretMigrateState(v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
11+
switch v {
12+
case 0:
13+
log.Printf("[INFO] Found GitHub Actions Secret State v0; migrating to v1")
14+
return migrateGithubActionsSecretStateV0toV1(is)
15+
default:
16+
return is, fmt.Errorf("unexpected schema version: %d", v)
17+
}
18+
}
19+
20+
func migrateGithubActionsSecretStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) {
21+
if is.Empty() {
22+
log.Printf("[DEBUG] Empty InstanceState; nothing to migrate.")
23+
return is, nil
24+
}
25+
26+
log.Printf("[DEBUG] GitHub Actions Secret Attributes before migration: %#v", is.Attributes)
27+
28+
// Add the destroy_on_drift field with default value true if it doesn't exist
29+
if _, ok := is.Attributes["destroy_on_drift"]; !ok {
30+
is.Attributes["destroy_on_drift"] = "true"
31+
}
32+
33+
log.Printf("[DEBUG] GitHub Actions Secret Attributes after State Migration: %#v", is.Attributes)
34+
35+
return is, nil
36+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package github
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
8+
)
9+
10+
func TestMigrateGithubActionsSecretStateV0toV1(t *testing.T) {
11+
// Secret without destroy_on_drift should get default value
12+
oldAttributes := map[string]string{
13+
"id": "test-secret",
14+
"repository": "test-repo",
15+
"secret_name": "test-secret",
16+
"created_at": "2023-01-01T00:00:00Z",
17+
"updated_at": "2023-01-01T00:00:00Z",
18+
"plaintext_value": "secret-value",
19+
}
20+
21+
newState, err := migrateGithubActionsSecretStateV0toV1(&terraform.InstanceState{
22+
ID: "test-secret",
23+
Attributes: oldAttributes,
24+
})
25+
if err != nil {
26+
t.Fatal(err)
27+
}
28+
29+
expectedAttributes := map[string]string{
30+
"id": "test-secret",
31+
"repository": "test-repo",
32+
"secret_name": "test-secret",
33+
"created_at": "2023-01-01T00:00:00Z",
34+
"updated_at": "2023-01-01T00:00:00Z",
35+
"plaintext_value": "secret-value",
36+
"destroy_on_drift": "true",
37+
}
38+
if !reflect.DeepEqual(newState.Attributes, expectedAttributes) {
39+
t.Fatalf("Expected attributes:\n%#v\n\nGiven:\n%#v\n",
40+
expectedAttributes, newState.Attributes)
41+
}
42+
43+
// Secret with existing destroy_on_drift should be preserved
44+
oldAttributesWithDrift := map[string]string{
45+
"id": "test-secret",
46+
"repository": "test-repo",
47+
"secret_name": "test-secret",
48+
"destroy_on_drift": "false",
49+
}
50+
51+
newState2, err := migrateGithubActionsSecretStateV0toV1(&terraform.InstanceState{
52+
ID: "test-secret",
53+
Attributes: oldAttributesWithDrift,
54+
})
55+
if err != nil {
56+
t.Fatal(err)
57+
}
58+
59+
expectedAttributesWithDrift := map[string]string{
60+
"id": "test-secret",
61+
"repository": "test-repo",
62+
"secret_name": "test-secret",
63+
"destroy_on_drift": "false",
64+
}
65+
if !reflect.DeepEqual(newState2.Attributes, expectedAttributesWithDrift) {
66+
t.Fatalf("Expected attributes:\n%#v\n\nGiven:\n%#v\n",
67+
expectedAttributesWithDrift, newState2.Attributes)
68+
}
69+
}

github/resource_github_actions_secret.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ func resourceGithubActionsSecret() *schema.Resource {
2222
State: resourceGithubActionsSecretImport,
2323
},
2424

25+
// Schema migration added to handle the addition of destroy_on_drift field
26+
// Resources created before this field was added need it populated with default value
27+
SchemaVersion: 1,
28+
MigrateState: resourceGithubActionsSecretMigrateState,
29+
2530
Schema: map[string]*schema.Schema{
2631
"repository": {
2732
Type: schema.TypeString,

0 commit comments

Comments
 (0)