Skip to content

Commit 6fc64f6

Browse files
committed
Convert resourceGithubRepository schema migration to use StateUpgraders
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
1 parent 3064bdc commit 6fc64f6

3 files changed

Lines changed: 221 additions & 38 deletions

File tree

Lines changed: 197 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,216 @@
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 resourceGithubRepositoryMigrateState(v int, is *terraform.InstanceState, meta any) (*terraform.InstanceState, error) {
12-
switch v {
13-
case 0:
14-
log.Printf("[INFO] Found GitHub Repository State v0; migrating to v1")
15-
return migrateGithubRepositoryStateV0toV1(is)
16-
default:
17-
return is, fmt.Errorf("unexpected schema version: %d", v)
18-
}
19-
}
11+
func resourceGithubRepositoryResourceV0() *schema.Resource {
12+
return &schema.Resource{
13+
Schema: map[string]*schema.Schema{
14+
"full_name": {
15+
Type: schema.TypeString,
16+
Optional: true,
17+
Computed: true,
18+
ConflictsWith: []string{"name"},
19+
},
20+
"name": {
21+
Type: schema.TypeString,
22+
Optional: true,
23+
Computed: true,
24+
ConflictsWith: []string{"full_name"},
25+
},
26+
"only_protected_branches": {
27+
Type: schema.TypeBool,
28+
Optional: true,
29+
Default: false,
30+
},
2031

21-
func migrateGithubRepositoryStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) {
22-
if is.Empty() {
23-
log.Printf("[DEBUG] Empty InstanceState; nothing to migrate.")
24-
return is, nil
32+
"description": {
33+
Type: schema.TypeString,
34+
Default: nil,
35+
Optional: true,
36+
},
37+
"homepage_url": {
38+
Type: schema.TypeString,
39+
Default: "",
40+
Optional: true,
41+
},
42+
"private": {
43+
Type: schema.TypeBool,
44+
Computed: true,
45+
},
46+
"visibility": {
47+
Type: schema.TypeString,
48+
Computed: true,
49+
},
50+
"has_issues": {
51+
Type: schema.TypeBool,
52+
Computed: true,
53+
},
54+
"has_projects": {
55+
Type: schema.TypeBool,
56+
Computed: true,
57+
},
58+
"has_downloads": {
59+
Type: schema.TypeBool,
60+
Computed: true,
61+
},
62+
"has_wiki": {
63+
Type: schema.TypeBool,
64+
Computed: true,
65+
},
66+
"allow_merge_commit": {
67+
Type: schema.TypeBool,
68+
Computed: true,
69+
},
70+
"allow_squash_merge": {
71+
Type: schema.TypeBool,
72+
Computed: true,
73+
},
74+
"allow_rebase_merge": {
75+
Type: schema.TypeBool,
76+
Computed: true,
77+
},
78+
"allow_auto_merge": {
79+
Type: schema.TypeBool,
80+
Computed: true,
81+
},
82+
"squash_merge_commit_title": {
83+
Type: schema.TypeString,
84+
Computed: true,
85+
},
86+
"squash_merge_commit_message": {
87+
Type: schema.TypeString,
88+
Computed: true,
89+
},
90+
"merge_commit_title": {
91+
Type: schema.TypeString,
92+
Computed: true,
93+
},
94+
"merge_commit_message": {
95+
Type: schema.TypeString,
96+
Computed: true,
97+
},
98+
"default_branch": {
99+
Type: schema.TypeString,
100+
Computed: true,
101+
},
102+
"archived": {
103+
Type: schema.TypeBool,
104+
Computed: true,
105+
},
106+
"branches": {
107+
Type: schema.TypeList,
108+
Computed: true,
109+
Elem: &schema.Resource{
110+
Schema: map[string]*schema.Schema{
111+
"name": {
112+
Type: schema.TypeString,
113+
Computed: true,
114+
},
115+
"protected": {
116+
Type: schema.TypeBool,
117+
Computed: true,
118+
},
119+
},
120+
},
121+
},
122+
"pages": {
123+
Type: schema.TypeList,
124+
Computed: true,
125+
Elem: &schema.Resource{
126+
Schema: map[string]*schema.Schema{
127+
"source": {
128+
Type: schema.TypeList,
129+
Computed: true,
130+
Elem: &schema.Resource{
131+
Schema: map[string]*schema.Schema{
132+
"branch": {
133+
Type: schema.TypeString,
134+
Computed: true,
135+
},
136+
"path": {
137+
Type: schema.TypeString,
138+
Computed: true,
139+
},
140+
},
141+
},
142+
},
143+
"cname": {
144+
Type: schema.TypeString,
145+
Computed: true,
146+
},
147+
"custom_404": {
148+
Type: schema.TypeBool,
149+
Computed: true,
150+
},
151+
"html_url": {
152+
Type: schema.TypeString,
153+
Computed: true,
154+
},
155+
"status": {
156+
Type: schema.TypeString,
157+
Computed: true,
158+
},
159+
"url": {
160+
Type: schema.TypeString,
161+
Computed: true,
162+
},
163+
},
164+
},
165+
},
166+
"topics": {
167+
Type: schema.TypeList,
168+
Elem: &schema.Schema{Type: schema.TypeString},
169+
Computed: true,
170+
},
171+
"html_url": {
172+
Type: schema.TypeString,
173+
Computed: true,
174+
},
175+
"ssh_clone_url": {
176+
Type: schema.TypeString,
177+
Computed: true,
178+
},
179+
"svn_url": {
180+
Type: schema.TypeString,
181+
Computed: true,
182+
},
183+
"git_clone_url": {
184+
Type: schema.TypeString,
185+
Computed: true,
186+
},
187+
"http_clone_url": {
188+
Type: schema.TypeString,
189+
Computed: true,
190+
},
191+
"node_id": {
192+
Type: schema.TypeString,
193+
Computed: true,
194+
},
195+
"repo_id": {
196+
Type: schema.TypeInt,
197+
Computed: true,
198+
},
199+
},
25200
}
201+
}
26202

27-
log.Printf("[DEBUG] GitHub Repository Attributes before migration: %#v", is.Attributes)
203+
func resourceGithubRepositoryInstanceStateUpgradeV0(ctx context.Context, rawState map[string]any, meta any) (map[string]any, error) {
204+
log.Printf("[DEBUG] GitHub Repository State before migration: %#v", rawState)
28205

29206
prefix := "branches."
30207

31-
for k := range is.Attributes {
208+
for k := range rawState {
32209
if strings.HasPrefix(k, prefix) {
33-
delete(is.Attributes, k)
210+
delete(rawState, k)
34211
}
35212
}
36213

37-
log.Printf("[DEBUG] GitHub Repository Attributes after State Migration: %#v", is.Attributes)
38-
39-
return is, nil
214+
log.Printf("[DEBUG] GitHub Repository State after migration: %#v", rawState)
215+
return rawState, nil
40216
}

github/migrate_github_repository_test.go

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

10-
func TestMigrateGithubRepositoryStateV0toV1(t *testing.T) {
11-
oldAttributes := map[string]string{
8+
func testResourceGithubRepositoryInstanceStateDataV0() map[string]any {
9+
return map[string]any{
1210
"branches.#": "1",
1311
"branches.0.name": "foobar",
1412
"branches.0.protected": "false",
1513
}
14+
}
1615

17-
newState, err := migrateGithubRepositoryStateV0toV1(&terraform.InstanceState{
18-
ID: "nonempty",
19-
Attributes: oldAttributes,
20-
})
21-
if err != nil {
22-
t.Fatal(err)
23-
}
16+
func testResourceGithubRepositoryInstanceStateDataV1() map[string]any {
17+
return map[string]any{}
18+
}
2419

25-
expectedAttributes := map[string]string{}
26-
if !reflect.DeepEqual(newState.Attributes, expectedAttributes) {
27-
t.Fatalf("Expected attributes:\n%#v\n\nGiven:\n%#v\n",
28-
expectedAttributes, newState.Attributes)
29-
}
20+
func TestGithub_MigrateRepositoryStateV0toV1(t *testing.T) {
21+
t.Run("migrates state without errors", func(t *testing.T) {
22+
expected := testResourceGithubRepositoryInstanceStateDataV1()
23+
actual, err := resourceGithubRepositoryInstanceStateUpgradeV0(t.Context(), testResourceGithubRepositoryInstanceStateDataV0(), nil)
24+
if err != nil {
25+
t.Fatalf("error migrating state: %s", err)
26+
}
27+
if !reflect.DeepEqual(expected, actual) {
28+
t.Fatalf("\n\nexpected:\n\n%#v\n\ngot:\n\n%#v\n\n", expected, actual)
29+
}
30+
})
3031
}

github/resource_github_repository.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ func resourceGithubRepository() *schema.Resource {
3232
},
3333

3434
SchemaVersion: 1,
35-
MigrateState: resourceGithubRepositoryMigrateState,
35+
StateUpgraders: []schema.StateUpgrader{
36+
{
37+
Type: resourceGithubRepositoryResourceV0().CoreConfigSchema().ImpliedType(),
38+
Upgrade: resourceGithubRepositoryInstanceStateUpgradeV0,
39+
Version: 0,
40+
},
41+
},
3642

3743
Schema: map[string]*schema.Schema{
3844
"name": {

0 commit comments

Comments
 (0)