Skip to content

Commit c9df74c

Browse files
committed
fix: Add state migration for github_actions_organization_secret destroy_on_drift field
This addresses the regression introduced in v6.7.0 where existing github_actions_organization_secret resources would show invalid state for the new destroy_on_drift field. Adds schema migration from v0 to v1 that sets destroy_on_drift=true for existing resources that don't have this field. Fixes #2804
1 parent efd6d02 commit c9df74c

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 resourceGithubActionsOrganizationSecretMigrateState(v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
11+
switch v {
12+
case 0:
13+
log.Printf("[INFO] Found GitHub Actions Organization Secret State v0; migrating to v1")
14+
return migrateGithubActionsOrganizationSecretStateV0toV1(is)
15+
default:
16+
return is, fmt.Errorf("unexpected schema version: %d", v)
17+
}
18+
}
19+
20+
func migrateGithubActionsOrganizationSecretStateV0toV1(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 Organization 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 Organization 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 TestMigrateGithubActionsOrganizationSecretStateV0toV1(t *testing.T) {
11+
// Secret without destroy_on_drift should get default value
12+
oldAttributes := map[string]string{
13+
"id": "test-secret",
14+
"secret_name": "test-secret",
15+
"visibility": "private",
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 := migrateGithubActionsOrganizationSecretStateV0toV1(&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+
"secret_name": "test-secret",
32+
"visibility": "private",
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+
"secret_name": "test-secret",
47+
"visibility": "private",
48+
"destroy_on_drift": "false",
49+
}
50+
51+
newState2, err := migrateGithubActionsOrganizationSecretStateV0toV1(&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+
"secret_name": "test-secret",
62+
"visibility": "private",
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_organization_secret.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ func resourceGithubActionsOrganizationSecret() *schema.Resource {
2727
},
2828
},
2929

30+
// Schema migration added in v6.7.1 to handle the addition of destroy_on_drift field
31+
// Resources created before v6.7.0 need the field populated with default value
32+
SchemaVersion: 1,
33+
MigrateState: resourceGithubActionsOrganizationSecretMigrateState,
34+
3035
Schema: map[string]*schema.Schema{
3136
"secret_name": {
3237
Type: schema.TypeString,

0 commit comments

Comments
 (0)