Skip to content

Commit ee14a7c

Browse files
committed
wip
1 parent 14ba415 commit ee14a7c

File tree

5 files changed

+628
-88
lines changed

5 files changed

+628
-88
lines changed

internal/acctest/acctest_test.go

Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -187,74 +187,74 @@ var testBodyMatcherCases = []struct {
187187
shouldMatch bool
188188
}{
189189
// create bar compare with foo
190-
// {
191-
// requestBody: newRequest(http.MethodPost, "https://api.scaleway.com/iam/v1alpha1/users", strings.NewReader(barMemberCreationBody)),
192-
// cassetteBody: &cassette.Request{
193-
// URL: "https://api.scaleway.com/iam/v1alpha1/users",
194-
// Method: http.MethodPost,
195-
// Body: fooMemberCreationBody,
196-
// ContentLength: int64(len(fooMemberCreationBody)),
197-
// },
198-
// shouldMatch: false,
199-
// },
200-
// // create bar compare with bar
201-
// {
202-
// requestBody: newRequest(http.MethodPost, "https://api.scaleway.com/iam/v1alpha1/users", strings.NewReader(barMemberCreationBody)),
203-
// cassetteBody: &cassette.Request{
204-
// URL: "https://api.scaleway.com/iam/v1alpha1/users",
205-
// Method: http.MethodPost,
206-
// Body: barMemberCreationBody,
207-
// ContentLength: int64(len(barMemberCreationBody)),
208-
// },
209-
// shouldMatch: true,
210-
// },
211-
// // simple http get
212-
// {
213-
// requestBody: newRequest(http.MethodGet, "https://api.scaleway.com/iam/v1alpha1/users/6867048b-fe12-4e96-835e-41c79a39604b", nil),
214-
// cassetteBody: &cassette.Request{
215-
// URL: "https://api.scaleway.com/iam/v1alpha1/users/6867048b-fe12-4e96-835e-41c79a39604b",
216-
// Method: http.MethodGet,
217-
// Body: "",
218-
// ContentLength: 0,
219-
// },
220-
// shouldMatch: true,
221-
// },
222-
// // patch secret with nested slices of map[string]interface{} in different order
223-
// // we cannot user deep equal because the order of the slices is different although the values are the same
224-
// // it is not possible to sort them because they are not comparable (map[string]interface{})
225-
// {
226-
// requestBody: newRequest(http.MethodPatch, "https://api.scaleway.com/secrets/v1/secrets/123", strings.NewReader(secretPatchBodyRequest)),
227-
// cassetteBody: &cassette.Request{
228-
// URL: "https://api.scaleway.com/secrets/v1/secrets/123",
229-
// Method: http.MethodPatch,
230-
// Body: secretPatchBodyCassette,
231-
// ContentLength: int64(len(secretPatchBodyCassette)),
232-
// },
233-
// shouldMatch: true,
234-
// },
235-
// // compare nested slices of different integers
236-
// {
237-
// requestBody: newRequest(http.MethodPost, "https://api.scaleway.com/iam/v1alpha1/users", strings.NewReader(integertestBodyRequest)),
238-
// cassetteBody: &cassette.Request{
239-
// URL: "https://api.scaleway.com/iam/v1alpha1/users",
240-
// Method: http.MethodPost,
241-
// Body: integertestBodyCassette,
242-
// ContentLength: int64(len(integertestBodyCassette)),
243-
// },
244-
// shouldMatch: false,
245-
// },
246-
// // compare nested slices of same integers in different order
247-
// {
248-
// requestBody: newRequest(http.MethodPost, "https://api.scaleway.com/iam/v1alpha1/users", strings.NewReader(integerBodyRequestOutOfOrder)),
249-
// cassetteBody: &cassette.Request{
250-
// URL: "https://api.scaleway.com/iam/v1alpha1/users",
251-
// Method: http.MethodPost,
252-
// Body: integertestBodyRequest,
253-
// ContentLength: int64(len(integertestBodyRequest)),
254-
// },
255-
// shouldMatch: true,
256-
// },
257-
// // compare nested slices of slices of strings
190+
{
191+
requestBody: newRequest(http.MethodPost, "https://api.scaleway.com/iam/v1alpha1/users", strings.NewReader(barMemberCreationBody)),
192+
cassetteBody: &cassette.Request{
193+
URL: "https://api.scaleway.com/iam/v1alpha1/users",
194+
Method: http.MethodPost,
195+
Body: fooMemberCreationBody,
196+
ContentLength: int64(len(fooMemberCreationBody)),
197+
},
198+
shouldMatch: false,
199+
},
200+
// create bar compare with bar
201+
{
202+
requestBody: newRequest(http.MethodPost, "https://api.scaleway.com/iam/v1alpha1/users", strings.NewReader(barMemberCreationBody)),
203+
cassetteBody: &cassette.Request{
204+
URL: "https://api.scaleway.com/iam/v1alpha1/users",
205+
Method: http.MethodPost,
206+
Body: barMemberCreationBody,
207+
ContentLength: int64(len(barMemberCreationBody)),
208+
},
209+
shouldMatch: true,
210+
},
211+
// simple http get
212+
{
213+
requestBody: newRequest(http.MethodGet, "https://api.scaleway.com/iam/v1alpha1/users/6867048b-fe12-4e96-835e-41c79a39604b", nil),
214+
cassetteBody: &cassette.Request{
215+
URL: "https://api.scaleway.com/iam/v1alpha1/users/6867048b-fe12-4e96-835e-41c79a39604b",
216+
Method: http.MethodGet,
217+
Body: "",
218+
ContentLength: 0,
219+
},
220+
shouldMatch: true,
221+
},
222+
// patch secret with nested slices of map[string]interface{} in different order
223+
// we cannot user deep equal because the order of the slices is different although the values are the same
224+
// it is not possible to sort them because they are not comparable (map[string]interface{})
225+
{
226+
requestBody: newRequest(http.MethodPatch, "https://api.scaleway.com/secrets/v1/secrets/123", strings.NewReader(secretPatchBodyRequest)),
227+
cassetteBody: &cassette.Request{
228+
URL: "https://api.scaleway.com/secrets/v1/secrets/123",
229+
Method: http.MethodPatch,
230+
Body: secretPatchBodyCassette,
231+
ContentLength: int64(len(secretPatchBodyCassette)),
232+
},
233+
shouldMatch: true,
234+
},
235+
// compare nested slices of different integers
236+
{
237+
requestBody: newRequest(http.MethodPost, "https://api.scaleway.com/iam/v1alpha1/users", strings.NewReader(integertestBodyRequest)),
238+
cassetteBody: &cassette.Request{
239+
URL: "https://api.scaleway.com/iam/v1alpha1/users",
240+
Method: http.MethodPost,
241+
Body: integertestBodyCassette,
242+
ContentLength: int64(len(integertestBodyCassette)),
243+
},
244+
shouldMatch: false,
245+
},
246+
// compare nested slices of same integers in different order
247+
{
248+
requestBody: newRequest(http.MethodPost, "https://api.scaleway.com/iam/v1alpha1/users", strings.NewReader(integerBodyRequestOutOfOrder)),
249+
cassetteBody: &cassette.Request{
250+
URL: "https://api.scaleway.com/iam/v1alpha1/users",
251+
Method: http.MethodPost,
252+
Body: integertestBodyRequest,
253+
ContentLength: int64(len(integertestBodyRequest)),
254+
},
255+
shouldMatch: true,
256+
},
257+
// compare nested slices of slices of strings
258258
{
259259
requestBody: newRequest(http.MethodPost, "https://api.scaleway.com/iam/v1alpha1/users", strings.NewReader(nestedSliceOfSlicesRequest)),
260260
cassetteBody: &cassette.Request{

internal/acctest/compare.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package acctest
22

33
import (
4-
"fmt"
54
"net/url"
65
"reflect"
76
"sort"
@@ -10,26 +9,26 @@ import (
109

1110
// compareFormBodies compare two given url.Values
1211
// returns true if both url.Values are equivalent
13-
func compareFormBodies(expected, actual url.Values) bool {
12+
func compareFormBodies(request, cassette url.Values) bool {
1413
// Check for each key in actual requests
1514
// Compare its value to cassette content if marshal-able to string
16-
for key := range actual {
17-
expectedValue, exists := expected[key]
15+
for key := range cassette {
16+
requestValue, exists := request[key]
1817
if !exists {
1918
// Actual request may contain a field that does not exist in cassette
2019
// New fields can appear in requests with new api features
2120
// We do not want to generate new cassettes for each new features
2221
continue
2322
}
2423

25-
if !compareJSONFields(expectedValue, actual[key]) {
24+
if !compareJSONFields(requestValue, cassette[key]) {
2625
return false
2726
}
2827
}
2928

30-
for key := range expected {
31-
_, exists := actual[key]
32-
if !exists && expected[key] != nil {
29+
for key := range request {
30+
_, exists := cassette[key]
31+
if !exists && request[key] != nil {
3332
// Fails match if cassettes contains a field not in actual requests
3433
// Fields should not disappear from requests unless a sdk breaking change
3534
// We ignore if field is nil in cassette as it could be an old deprecated and unused field
@@ -52,6 +51,7 @@ func compareJSONFieldsStrings(expected, actual string) bool {
5251
if strings.HasPrefix(actual, snsPrefix) && strings.HasPrefix(expected, snsPrefix) {
5352
return true
5453
}
54+
5555
if strings.HasPrefix(actual, "arn:scw:sns:") && strings.HasPrefix(expected, "arn:scw:sns:") {
5656
return true
5757
}
@@ -76,6 +76,7 @@ func compareJSONFieldsStrings(expected, actual string) bool {
7676
expectedHandled = extractGeneratedNamePrefix(expected)
7777
actualHandled = extractGeneratedNamePrefix(actual)
7878
}
79+
7980
return expectedHandled == actualHandled
8081
}
8182

@@ -92,8 +93,8 @@ func compareStringSlices(request, cassette []string) bool {
9293
return false
9394
}
9495
}
95-
return true
9696

97+
return true
9798
}
9899

99100
// compareJSONSlices compares two slices of interface{}
@@ -115,10 +116,12 @@ func compareJSONSlices(request, cassette []interface{}) bool {
115116
for i, v := range cassette {
116117
cassetteString[i] = v.(string)
117118
}
119+
118120
requestString := make([]string, len(request))
119121
for i, v := range request {
120122
requestString[i] = v.(string)
121123
}
124+
122125
return compareStringSlices(requestString, cassetteString)
123126
case float64:
124127
sort.Slice(request, func(i, j int) bool {
@@ -133,6 +136,7 @@ func compareJSONSlices(request, cassette []interface{}) bool {
133136
return false
134137
}
135138
}
139+
136140
return true
137141
case []interface{}:
138142
return compareJSONSlices(request, cassette)
@@ -164,23 +168,21 @@ func compareJSONFields(requestValue, cassetteValue interface{}) bool {
164168
// compareJSONBodies compare two given maps that represent json bodies
165169
// returns true if both json are equivalent
166170
func compareJSONBodies(request, cassette map[string]interface{}) bool {
167-
fmt.Println("request", request)
168-
fmt.Println("cassette", cassette)
169171
for key, requestValue := range request {
170172
cassetteValue, ok := cassette[key]
171173
if !ok {
174+
if requestValue != nil {
175+
return false
176+
}
177+
172178
continue
173179
}
180+
174181
if reflect.TypeOf(cassetteValue) != reflect.TypeOf(requestValue) {
175182
return false
176183
}
177-
if !compareJSONFields(requestValue, cassetteValue) {
178-
return false
179-
}
180-
}
181184

182-
for key, cassetteValue := range cassette {
183-
if _, ok := request[key]; !ok && cassetteValue != nil {
185+
if !compareJSONFields(requestValue, cassetteValue) {
184186
return false
185187
}
186188
}

internal/acctest/vcr.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,18 @@ func cassetteBodyMatcher(request *http.Request, cassette cassette.Request) bool
109109
}
110110

111111
if !json.Valid(requestBody) {
112-
values, err := url.ParseQuery(string(requestBody))
112+
requestValues, err := url.ParseQuery(string(requestBody))
113113
if err != nil {
114114
panic(fmt.Errorf("cassette body matcher: failed to parse body as url values: %w", err)) // lintignore: R009
115115
}
116116

117117
// Remove keys that should be ignored during comparison
118118
for _, key := range BodyMatcherIgnore {
119-
values.Del(key)
119+
requestValues.Del(key)
120120
}
121121

122122
// Compare url values
123-
return compareFormBodies(values, cassette.Form)
123+
return compareFormBodies(requestValues, cassette.Form)
124124
}
125125

126126
err = json.Unmarshal(requestBody, &requestJSON)
@@ -145,6 +145,7 @@ func cassetteBodyMatcher(request *http.Request, cassette cassette.Request) bool
145145

146146
func removeKeyRecursive(m map[string]interface{}, key string) {
147147
delete(m, key)
148+
148149
for _, v := range m {
149150
if v, ok := v.(map[string]interface{}); ok {
150151
removeKeyRecursive(v, key)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package iam
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
9+
10+
func StateUpgraderV0(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
11+
groupID := rawState["group_id"].(string)
12+
userID := rawState["user_id"].(string)
13+
applicationID := rawState["application_id"].(string)
14+
15+
if groupID == "" {
16+
return nil, fmt.Errorf("group_id is required")
17+
}
18+
19+
if userID == "" && applicationID == "" {
20+
return nil, fmt.Errorf("user_id or application_id is required")
21+
}
22+
23+
return rawState, nil
24+
}
25+
26+
func resourceGroupMembershipV0() *schema.Resource {
27+
return &schema.Resource{
28+
SchemaVersion: 0,
29+
Schema: map[string]*schema.Schema{
30+
"group_id": {
31+
Type: schema.TypeString,
32+
Required: true,
33+
},
34+
"user_id": {
35+
Type: schema.TypeString,
36+
Optional: true,
37+
},
38+
"application_id": {
39+
Type: schema.TypeString,
40+
Optional: true,
41+
},
42+
},
43+
}
44+
}

0 commit comments

Comments
 (0)