Skip to content

Commit 3064bdc

Browse files
committed
Convert resourceGithubRepositoryWebhook and resourceGithubOrganizationWebhook schema migrations to use StateUpgraders
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
1 parent 85cd257 commit 3064bdc

6 files changed

Lines changed: 174 additions & 48 deletions
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"log"
6+
"strings"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
)
10+
11+
func resourceGithubOrganizationWebhookResourceV0() *schema.Resource {
12+
return &schema.Resource{
13+
Schema: map[string]*schema.Schema{
14+
"name": {
15+
Type: schema.TypeString,
16+
Required: true,
17+
ForceNew: true,
18+
},
19+
"events": {
20+
Type: schema.TypeSet,
21+
Required: true,
22+
Elem: &schema.Schema{Type: schema.TypeString},
23+
Set: schema.HashString,
24+
},
25+
"configuration": {
26+
Type: schema.TypeMap,
27+
Optional: true,
28+
},
29+
"url": {
30+
Type: schema.TypeString,
31+
Computed: true,
32+
},
33+
"active": {
34+
Type: schema.TypeBool,
35+
Optional: true,
36+
Default: true,
37+
},
38+
},
39+
}
40+
}
41+
42+
func resourceGithubOrganizationWebhookInstanceStateUpgradeV0(ctx context.Context, rawState map[string]any, meta any) (map[string]any, error) {
43+
log.Printf("[DEBUG] GitHub Organization Webhook State before migration: %#v", rawState)
44+
45+
prefix := "configuration."
46+
delete(rawState, prefix+"%")
47+
48+
// Read & delete old keys
49+
oldKeys := make(map[string]any)
50+
for k, v := range rawState {
51+
if strings.HasPrefix(k, prefix) {
52+
oldKeys[k] = v
53+
54+
// Delete old keys
55+
delete(rawState, k)
56+
}
57+
}
58+
59+
// Write new keys
60+
for k, v := range oldKeys {
61+
newKey := "configuration.0." + strings.TrimPrefix(k, prefix)
62+
rawState[newKey] = v
63+
}
64+
65+
rawState[prefix+"#"] = "1"
66+
67+
log.Printf("[DEBUG] GitHub Organization Webhook State after migration: %#v", rawState)
68+
69+
return rawState, nil
70+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package github
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestGithub_MigrateOrganizationWebhookStateV0toV1(t *testing.T) {
9+
t.Run("migrates state without errors", func(t *testing.T) {
10+
expected := testResourceGithubWebhookInstanceStateDataV1()
11+
actual, err := resourceGithubOrganizationWebhookInstanceStateUpgradeV0(t.Context(), testResourceGithubWebhookInstanceStateDataV0(), nil)
12+
if err != nil {
13+
t.Fatalf("error migrating state: %s", err)
14+
}
15+
16+
if !reflect.DeepEqual(expected, actual) {
17+
t.Fatalf("\n\nexpected:\n\n%#v\n\ngot:\n\n%#v\n\n", expected, actual)
18+
}
19+
})
20+
}
Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,75 @@
11
package github
22

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

8-
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
99
)
1010

11-
func resourceGithubWebhookMigrateState(v int, is *terraform.InstanceState, meta any) (*terraform.InstanceState, error) {
12-
switch v {
13-
case 0:
14-
log.Printf("[INFO] Found GitHub Webhook State v0; migrating to v1")
15-
return migrateGithubWebhookStateV0toV1(is)
16-
default:
17-
return is, fmt.Errorf("unexpected schema version: %d", v)
11+
func resourceGithubRepositoryWebhookResourceV0() *schema.Resource {
12+
return &schema.Resource{
13+
Schema: map[string]*schema.Schema{
14+
"name": {
15+
Type: schema.TypeString,
16+
Required: true,
17+
ForceNew: true,
18+
},
19+
"repository": {
20+
Type: schema.TypeString,
21+
Required: true,
22+
ForceNew: true,
23+
},
24+
"events": {
25+
Type: schema.TypeSet,
26+
Required: true,
27+
Elem: &schema.Schema{Type: schema.TypeString},
28+
Set: schema.HashString,
29+
},
30+
"configuration": {
31+
Type: schema.TypeMap,
32+
Optional: true,
33+
},
34+
"url": {
35+
Type: schema.TypeString,
36+
Computed: true,
37+
},
38+
"active": {
39+
Type: schema.TypeBool,
40+
Optional: true,
41+
Default: true,
42+
},
43+
},
1844
}
1945
}
2046

21-
func migrateGithubWebhookStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) {
22-
if is.Empty() {
23-
log.Printf("[DEBUG] Empty InstanceState; nothing to migrate.")
24-
return is, nil
25-
}
26-
27-
log.Printf("[DEBUG] GitHub Webhook Attributes before migration: %#v", is.Attributes)
47+
func resourceGithubRepositoryWebhookInstanceStateUpgradeV0(ctx context.Context, rawState map[string]any, meta any) (map[string]any, error) {
48+
log.Printf("[DEBUG] GitHub Repository Webhook State before migration: %#v", rawState)
2849

2950
prefix := "configuration."
30-
31-
delete(is.Attributes, prefix+"%")
51+
delete(rawState, prefix+"%")
3252

3353
// Read & delete old keys
34-
oldKeys := make(map[string]string)
35-
for k, v := range is.Attributes {
54+
oldKeys := make(map[string]any)
55+
for k, v := range rawState {
3656
if strings.HasPrefix(k, prefix) {
3757
oldKeys[k] = v
3858

3959
// Delete old keys
40-
delete(is.Attributes, k)
60+
delete(rawState, k)
4161
}
4262
}
4363

4464
// Write new keys
4565
for k, v := range oldKeys {
4666
newKey := "configuration.0." + strings.TrimPrefix(k, prefix)
47-
is.Attributes[newKey] = v
67+
rawState[newKey] = v
4868
}
4969

50-
is.Attributes[prefix+"#"] = "1"
51-
log.Printf("[DEBUG] GitHub Webhook Attributes after State Migration: %#v", is.Attributes)
70+
rawState[prefix+"#"] = "1"
71+
72+
log.Printf("[DEBUG] GitHub Repository Webhook State after migration: %#v", rawState)
5273

53-
return is, nil
74+
return rawState, nil
5475
}

github/migrate_github_repository_webhook_test.go

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

10-
func TestMigrateGithubWebhookStateV0toV1(t *testing.T) {
11-
oldAttributes := map[string]string{
8+
func testResourceGithubWebhookInstanceStateDataV0() map[string]any {
9+
return map[string]any{
1210
"configuration.%": "4",
1311
"configuration.content_type": "form",
1412
"configuration.insecure_ssl": "0",
1513
"configuration.secret": "blablah",
1614
"configuration.url": "https://google.co.uk/",
1715
}
16+
}
1817

19-
newState, err := migrateGithubWebhookStateV0toV1(&terraform.InstanceState{
20-
ID: "nonempty",
21-
Attributes: oldAttributes,
22-
})
23-
if err != nil {
24-
t.Fatal(err)
25-
}
26-
27-
expectedAttributes := map[string]string{
18+
func testResourceGithubWebhookInstanceStateDataV1() map[string]any {
19+
v0 := testResourceGithubWebhookInstanceStateDataV0()
20+
return map[string]any{
2821
"configuration.#": "1",
29-
"configuration.0.content_type": "form",
30-
"configuration.0.insecure_ssl": "0",
31-
"configuration.0.secret": "blablah",
32-
"configuration.0.url": "https://google.co.uk/",
33-
}
34-
if !reflect.DeepEqual(newState.Attributes, expectedAttributes) {
35-
t.Fatalf("Expected attributes:\n%#v\n\nGiven:\n%#v\n",
36-
expectedAttributes, newState.Attributes)
22+
"configuration.0.content_type": v0["configuration.content_type"],
23+
"configuration.0.insecure_ssl": v0["configuration.insecure_ssl"],
24+
"configuration.0.secret": v0["configuration.secret"],
25+
"configuration.0.url": v0["configuration.url"],
3726
}
3827
}
28+
29+
func TestGithub_MigrateRepositoryWebhookStateV0toV1(t *testing.T) {
30+
t.Run("migrates state without errors", func(t *testing.T) {
31+
expected := testResourceGithubWebhookInstanceStateDataV1()
32+
actual, err := resourceGithubRepositoryWebhookInstanceStateUpgradeV0(t.Context(), testResourceGithubWebhookInstanceStateDataV0(), nil)
33+
if err != nil {
34+
t.Fatalf("error migrating state: %s", err)
35+
}
36+
37+
if !reflect.DeepEqual(expected, actual) {
38+
t.Fatalf("\n\nexpected:\n\n%#v\n\ngot:\n\n%#v\n\n", expected, actual)
39+
}
40+
})
41+
}

github/resource_github_organization_webhook.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ func resourceGithubOrganizationWebhook() *schema.Resource {
2222
},
2323

2424
SchemaVersion: 1,
25-
MigrateState: resourceGithubWebhookMigrateState,
25+
StateUpgraders: []schema.StateUpgrader{
26+
{
27+
Type: resourceGithubOrganizationWebhookResourceV0().CoreConfigSchema().ImpliedType(),
28+
Upgrade: resourceGithubOrganizationWebhookInstanceStateUpgradeV0,
29+
Version: 0,
30+
},
31+
},
2632

2733
Schema: map[string]*schema.Schema{
2834
"events": {

github/resource_github_repository_webhook.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ func resourceGithubRepositoryWebhook() *schema.Resource {
3434
},
3535

3636
SchemaVersion: 1,
37-
MigrateState: resourceGithubWebhookMigrateState,
37+
StateUpgraders: []schema.StateUpgrader{
38+
{
39+
Type: resourceGithubRepositoryWebhookResourceV0().CoreConfigSchema().ImpliedType(),
40+
Upgrade: resourceGithubRepositoryWebhookInstanceStateUpgradeV0,
41+
Version: 0,
42+
},
43+
},
3844

3945
Schema: map[string]*schema.Schema{
4046
"repository": {

0 commit comments

Comments
 (0)