Skip to content

Commit 11e5930

Browse files
committed
Convert resourceGithubActionsOrganizationSecret to use StateUpgraders for schema migrations
Signed-off-by: Timo Sand <[email protected]>
1 parent 665fb45 commit 11e5930

3 files changed

Lines changed: 106 additions & 71 deletions
Lines changed: 64 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,79 @@
11
package github
22

33
import (
4-
"fmt"
4+
"context"
55
"log"
66

7-
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
89
)
910

10-
func resourceGithubActionsOrganizationSecretMigrateState(v int, is *terraform.InstanceState, meta any) (*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)
11+
func resourceGithubActionsOrganizationSecretResourceV0() *schema.Resource {
12+
return &schema.Resource{
13+
Schema: map[string]*schema.Schema{
14+
"secret_name": {
15+
Type: schema.TypeString,
16+
Required: true,
17+
ForceNew: true,
18+
Description: "Name of the secret.",
19+
ValidateDiagFunc: validateSecretNameFunc,
20+
},
21+
"encrypted_value": {
22+
Type: schema.TypeString,
23+
ForceNew: true,
24+
Optional: true,
25+
Sensitive: true,
26+
ConflictsWith: []string{"plaintext_value"},
27+
Description: "Encrypted value of the secret using the GitHub public key in Base64 format.",
28+
ValidateDiagFunc: toDiagFunc(validation.StringIsBase64, "encrypted_value"),
29+
},
30+
"plaintext_value": {
31+
Type: schema.TypeString,
32+
ForceNew: true,
33+
Optional: true,
34+
Sensitive: true,
35+
ConflictsWith: []string{"encrypted_value"},
36+
Description: "Plaintext value of the secret to be encrypted.",
37+
},
38+
"visibility": {
39+
Type: schema.TypeString,
40+
Required: true,
41+
ForceNew: true,
42+
ValidateDiagFunc: validateValueFunc([]string{"all", "private", "selected"}),
43+
Description: "Configures the access that repositories have to the organization secret. Must be one of 'all', 'private', or 'selected'. 'selected_repository_ids' is required if set to 'selected'.",
44+
},
45+
"selected_repository_ids": {
46+
Type: schema.TypeSet,
47+
Elem: &schema.Schema{
48+
Type: schema.TypeInt,
49+
},
50+
Set: schema.HashInt,
51+
Optional: true,
52+
ForceNew: true,
53+
Description: "An array of repository ids that can access the organization secret.",
54+
},
55+
"created_at": {
56+
Type: schema.TypeString,
57+
Computed: true,
58+
Description: "Date of 'actions_secret' creation.",
59+
},
60+
"updated_at": {
61+
Type: schema.TypeString,
62+
Computed: true,
63+
Description: "Date of 'actions_secret' update.",
64+
},
65+
},
1766
}
1867
}
1968

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-
69+
func resourceGithubActionsOrganizationSecretInstanceStateUpgradeV0(ctx context.Context, rawState map[string]any, meta any) (map[string]any, error) {
70+
log.Printf("[DEBUG] GitHub Actions Organization Secret Attributes before migration: %#v", rawState)
2871
// 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"
72+
if _, ok := rawState["destroy_on_drift"]; !ok {
73+
rawState["destroy_on_drift"] = true
3174
}
3275

33-
log.Printf("[DEBUG] GitHub Actions Organization Secret Attributes after State Migration: %#v", is.Attributes)
76+
log.Printf("[DEBUG] GitHub Actions Organization Secret Attributes after migration: %#v", rawState)
3477

35-
return is, nil
78+
return rawState, nil
3679
}

github/migrate_github_actions_organization_secret_test.go

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,67 +3,53 @@ package github
33
import (
44
"reflect"
55
"testing"
6-
7-
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
86
)
97

10-
func TestMigrateGithubActionsOrganizationSecretStateV0toV1(t *testing.T) {
11-
// Secret without destroy_on_drift should get default value
12-
oldAttributes := map[string]string{
8+
func testResourceGithubActionsOrganizationSecretInstanceStateDataV0() map[string]any {
9+
return map[string]any{
1310
"id": "test-secret",
1411
"secret_name": "test-secret",
1512
"visibility": "private",
1613
"created_at": "2023-01-01T00:00:00Z",
1714
"updated_at": "2023-01-01T00:00:00Z",
1815
"plaintext_value": "secret-value",
1916
}
17+
}
2018

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-
}
19+
func testResourceGithubActionsOrganizationSecretInstanceStateDataV0_WithDrift() map[string]any {
20+
v0 := testResourceGithubActionsOrganizationSecretInstanceStateDataV0()
21+
v0["destroy_on_drift"] = false
22+
return v0
23+
}
4224

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-
}
25+
func testResourceGithubActionsOrganizationSecretInstanceStateDataV1() map[string]any {
26+
v0 := testResourceGithubActionsOrganizationSecretInstanceStateDataV0()
27+
v0["destroy_on_drift"] = true
28+
return v0
29+
}
5030

51-
newState2, err := migrateGithubActionsOrganizationSecretStateV0toV1(&terraform.InstanceState{
52-
ID: "test-secret",
53-
Attributes: oldAttributesWithDrift,
31+
func TestGithub_MigrateActionsOrganizationSecretStateV0toV1(t *testing.T) {
32+
t.Run("without destroy_on_drift", func(t *testing.T) {
33+
expected := testResourceGithubActionsOrganizationSecretInstanceStateDataV1()
34+
actual, err := resourceGithubActionsOrganizationSecretInstanceStateUpgradeV0(t.Context(), testResourceGithubActionsOrganizationSecretInstanceStateDataV0(), nil)
35+
if err != nil {
36+
t.Fatalf("error migrating state: %s", err)
37+
}
38+
39+
if !reflect.DeepEqual(expected, actual) {
40+
t.Fatalf("\n\nexpected:\n\n%#v\n\ngot:\n\n%#v\n\n", expected, actual)
41+
}
5442
})
55-
if err != nil {
56-
t.Fatal(err)
57-
}
5843

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-
}
44+
t.Run("with destroy_on_drift", func(t *testing.T) {
45+
expected := testResourceGithubActionsOrganizationSecretInstanceStateDataV0_WithDrift()
46+
actual, err := resourceGithubActionsOrganizationSecretInstanceStateUpgradeV0(t.Context(), testResourceGithubActionsOrganizationSecretInstanceStateDataV0_WithDrift(), nil)
47+
if err != nil {
48+
t.Fatalf("error migrating state: %s", err)
49+
}
50+
51+
if !reflect.DeepEqual(expected, actual) {
52+
t.Fatalf("\n\nexpected:\n\n%#v\n\ngot:\n\n%#v\n\n", expected, actual)
53+
}
54+
})
6955
}

github/resource_github_actions_organization_secret.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ func resourceGithubActionsOrganizationSecret() *schema.Resource {
3333
// Schema migration added in v6.7.1 to handle the addition of destroy_on_drift field
3434
// Resources created before v6.7.0 need the field populated with default value
3535
SchemaVersion: 1,
36-
MigrateState: resourceGithubActionsOrganizationSecretMigrateState,
36+
StateUpgraders: []schema.StateUpgrader{
37+
{
38+
Type: resourceGithubActionsOrganizationSecretResourceV0().CoreConfigSchema().ImpliedType(),
39+
Upgrade: resourceGithubActionsOrganizationSecretInstanceStateUpgradeV0,
40+
Version: 0,
41+
},
42+
},
3743

3844
Schema: map[string]*schema.Schema{
3945
"secret_name": {

0 commit comments

Comments
 (0)