Skip to content

Commit 168b56b

Browse files
authored
chore: fix tfproviderlint (#804)
1 parent 6006159 commit 168b56b

7 files changed

+285
-417
lines changed

scaleway/helpers.go

+27-25
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1213
"github.com/scaleway/scaleway-sdk-go/namegenerator"
1314
"github.com/scaleway/scaleway-sdk-go/scw"
1415
"golang.org/x/xerrors"
@@ -250,25 +251,39 @@ func projectIDSchema() *schema.Schema {
250251

251252
// zoneSchema returns a standard schema for a zone
252253
func zoneSchema() *schema.Schema {
254+
var allZones []string
255+
for _, z := range scw.AllZones {
256+
allZones = append(allZones, z.String())
257+
}
253258
return &schema.Schema{
254-
Type: schema.TypeString,
255-
Description: "The zone you want to attach the resource to",
256-
Optional: true,
257-
ForceNew: true,
258-
Computed: true,
259-
ValidateFunc: validationZone(),
259+
Type: schema.TypeString,
260+
Description: "The zone you want to attach the resource to",
261+
Optional: true,
262+
ForceNew: true,
263+
Computed: true,
264+
ValidateFunc: validation.StringInSlice(
265+
allZones,
266+
true,
267+
),
260268
}
261269
}
262270

263271
// regionSchema returns a standard schema for a zone
264272
func regionSchema() *schema.Schema {
273+
var allRegions []string
274+
for _, z := range scw.AllRegions {
275+
allRegions = append(allRegions, z.String())
276+
}
265277
return &schema.Schema{
266-
Type: schema.TypeString,
267-
Description: "The region you want to attach the resource to",
268-
Optional: true,
269-
ForceNew: true,
270-
Computed: true,
271-
ValidateFunc: validationRegion(),
278+
Type: schema.TypeString,
279+
Description: "The region you want to attach the resource to",
280+
Optional: true,
281+
ForceNew: true,
282+
Computed: true,
283+
ValidateFunc: validation.StringInSlice(
284+
allRegions,
285+
true,
286+
),
272287
}
273288
}
274289

@@ -437,19 +452,6 @@ func validateDuration() schema.SchemaValidateFunc {
437452
}
438453
}
439454

440-
func validateHour() schema.SchemaValidateFunc {
441-
return func(i interface{}, s string) (strings []string, errors []error) {
442-
integer, isInteger := i.(int)
443-
if !isInteger {
444-
return nil, []error{fmt.Errorf("%v is not an int", i)}
445-
}
446-
if integer < 0 || integer > 23 {
447-
return nil, []error{fmt.Errorf("int is outside range 0-23 for value %d", integer)}
448-
}
449-
return nil, nil
450-
}
451-
}
452-
453455
func diffSuppressFuncDuration(k, old, new string, d *schema.ResourceData) bool {
454456
if old == new {
455457
return true

scaleway/provider.go

+2-12
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,8 @@ func Provider(config *ProviderConfig) plugin.ProviderFunc {
4545
Description: "The Scaleway project ID.",
4646
ValidateFunc: validationUUID(),
4747
},
48-
"region": {
49-
Type: schema.TypeString,
50-
Optional: true,
51-
Description: "The Scaleway default region to use for your resources.",
52-
ValidateFunc: validationRegion(),
53-
},
54-
"zone": {
55-
Type: schema.TypeString,
56-
Optional: true,
57-
Description: "The Scaleway default zone to use for your resources.",
58-
ValidateFunc: validationZone(),
59-
},
48+
"region": regionSchema(),
49+
"zone": zoneSchema(),
6050
"api_url": {
6151
Type: schema.TypeString,
6252
Optional: true,

scaleway/resource_k8s_cluster.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func resourceScalewayK8SCluster() *schema.Resource {
8888
Type: schema.TypeInt,
8989
Required: true,
9090
Description: "Start hour of the 2-hour maintenance window",
91-
ValidateFunc: validateHour(),
91+
ValidateFunc: validation.IntBetween(0, 23),
9292
},
9393
"maintenance_window_day": {
9494
Type: schema.TypeString,

scaleway/resource_lb_acl_test.go

+255
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
package scaleway
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
9+
"github.com/scaleway/scaleway-sdk-go/api/lb/v1"
10+
"github.com/scaleway/scaleway-sdk-go/scw"
11+
)
12+
13+
func TestAccScalewayLbAcl_Basic(t *testing.T) {
14+
tt := NewTestTools(t)
15+
defer tt.Cleanup()
16+
resource.ParallelTest(t, resource.TestCase{
17+
PreCheck: func() { testAccPreCheck(t) },
18+
ProviderFactories: tt.ProviderFactories,
19+
CheckDestroy: testAccCheckScalewayLbFrontendDestroy(tt),
20+
Steps: []resource.TestStep{
21+
{
22+
Config: `
23+
resource scaleway_lb_ip ip01 {}
24+
resource scaleway_lb lb01 {
25+
ip_id = scaleway_lb_ip.ip01.id
26+
name = "test-lb-acl"
27+
type = "lb-s"
28+
}
29+
resource scaleway_lb_backend bkd01 {
30+
lb_id = scaleway_lb.lb01.id
31+
forward_protocol = "http"
32+
forward_port = 80
33+
proxy_protocol = "none"
34+
}
35+
resource scaleway_lb_frontend frt01 {
36+
lb_id = scaleway_lb.lb01.id
37+
backend_id = scaleway_lb_backend.bkd01.id
38+
name = "tf-test"
39+
inbound_port = 80
40+
timeout_client = "30s"
41+
acl {
42+
name = "test-acl"
43+
action {
44+
type = "allow"
45+
}
46+
match {
47+
ip_subnet = ["192.168.0.1", "192.168.0.2", "192.168.10.0/24"]
48+
http_filter = "acl_http_filter_none"
49+
http_filter_value = []
50+
invert = "true"
51+
}
52+
}
53+
acl {
54+
action {
55+
type = "allow"
56+
}
57+
match {
58+
ip_subnet = ["0.0.0.0/0"]
59+
http_filter = "path_begin"
60+
http_filter_value = ["criteria1","criteria2"]
61+
invert = "true"
62+
}
63+
}
64+
acl {
65+
action {
66+
type = "allow"
67+
}
68+
match {
69+
ip_subnet = ["0.0.0.0/0"]
70+
http_filter = "path_begin"
71+
http_filter_value = ["criteria1","criteria2"]
72+
}
73+
}
74+
acl {
75+
action {
76+
type = "allow"
77+
}
78+
match {
79+
ip_subnet = ["0.0.0.0/0"]
80+
http_filter = "acl_http_filter_none"
81+
http_filter_value = []
82+
}
83+
}
84+
acl {
85+
match {
86+
http_filter_value = []
87+
ip_subnet = ["0.0.0.0/0"]
88+
}
89+
action {
90+
type = "deny"
91+
}
92+
}
93+
}
94+
`,
95+
Check: resource.ComposeTestCheckFunc(
96+
testAccCheckScalewayACLAreCorrect(tt, "scaleway_lb_frontend.frt01", []*lb.ACL{
97+
{
98+
Name: "test-acl",
99+
Match: &lb.ACLMatch{
100+
IPSubnet: scw.StringSlicePtr([]string{"192.168.0.1", "192.168.0.2", "192.168.10.0/24"}),
101+
HTTPFilter: lb.ACLHTTPFilterACLHTTPFilterNone,
102+
HTTPFilterValue: []*string{},
103+
Invert: true,
104+
},
105+
Action: &lb.ACLAction{Type: lb.ACLActionTypeAllow},
106+
},
107+
{
108+
Match: &lb.ACLMatch{
109+
IPSubnet: scw.StringSlicePtr([]string{"0.0.0.0/0"}),
110+
HTTPFilter: lb.ACLHTTPFilterPathBegin,
111+
HTTPFilterValue: scw.StringSlicePtr([]string{"criteria1", "criteria2"}),
112+
Invert: true,
113+
},
114+
Action: &lb.ACLAction{Type: lb.ACLActionTypeAllow},
115+
},
116+
{
117+
Match: &lb.ACLMatch{
118+
IPSubnet: scw.StringSlicePtr([]string{"0.0.0.0/0"}),
119+
HTTPFilter: lb.ACLHTTPFilterPathBegin,
120+
HTTPFilterValue: scw.StringSlicePtr([]string{"criteria1", "criteria2"}),
121+
Invert: false,
122+
},
123+
Action: &lb.ACLAction{Type: lb.ACLActionTypeAllow},
124+
},
125+
{
126+
Match: &lb.ACLMatch{
127+
IPSubnet: scw.StringSlicePtr([]string{"0.0.0.0/0"}),
128+
HTTPFilter: lb.ACLHTTPFilterACLHTTPFilterNone,
129+
HTTPFilterValue: []*string{},
130+
Invert: false,
131+
},
132+
Action: &lb.ACLAction{Type: lb.ACLActionTypeAllow},
133+
},
134+
{
135+
Match: &lb.ACLMatch{
136+
IPSubnet: scw.StringSlicePtr([]string{"0.0.0.0/0"}),
137+
HTTPFilter: lb.ACLHTTPFilterACLHTTPFilterNone,
138+
HTTPFilterValue: []*string{},
139+
},
140+
Action: &lb.ACLAction{Type: lb.ACLActionTypeDeny},
141+
},
142+
}),
143+
),
144+
},
145+
{
146+
Config: `
147+
resource scaleway_lb_ip ip01 {}
148+
resource scaleway_lb lb01 {
149+
ip_id = scaleway_lb_ip.ip01.id
150+
name = "test-lb-acl"
151+
type = "lb-s"
152+
}
153+
resource scaleway_lb_backend bkd01 {
154+
lb_id = scaleway_lb.lb01.id
155+
forward_protocol = "http"
156+
forward_port = 80
157+
proxy_protocol = "none"
158+
}
159+
resource scaleway_lb_frontend frt01 {
160+
lb_id = scaleway_lb.lb01.id
161+
backend_id = scaleway_lb_backend.bkd01.id
162+
name = "tf-test"
163+
inbound_port = 80
164+
timeout_client = "30s"
165+
acl {
166+
action {
167+
type = "allow"
168+
}
169+
match {
170+
ip_subnet = ["10.0.0.10"]
171+
http_filter = "path_begin"
172+
http_filter_value = ["foo","bar"]
173+
}
174+
}
175+
}
176+
`,
177+
Check: resource.ComposeTestCheckFunc(
178+
testAccCheckScalewayACLAreCorrect(tt, "scaleway_lb_frontend.frt01", []*lb.ACL{
179+
{
180+
Match: &lb.ACLMatch{
181+
IPSubnet: scw.StringSlicePtr([]string{"10.0.0.10"}),
182+
HTTPFilter: lb.ACLHTTPFilterPathBegin,
183+
HTTPFilterValue: scw.StringSlicePtr([]string{"foo", "bar"}),
184+
Invert: false,
185+
},
186+
Action: &lb.ACLAction{Type: lb.ACLActionTypeAllow},
187+
},
188+
}),
189+
),
190+
},
191+
},
192+
})
193+
}
194+
195+
func testAccCheckScalewayACLAreCorrect(tt *TestTools, frontendName string, expectedAcls []*lb.ACL) resource.TestCheckFunc {
196+
return func(s *terraform.State) error {
197+
//define a wrapper for acl comparison
198+
testCompareAcls := func(testAcl, apiAcl lb.ACL) bool {
199+
//drop some values which are not part of the testing acl structure
200+
apiAcl.ID = ""
201+
apiAcl.Frontend = nil
202+
//if we do not pass any name, then drop it from comparison
203+
if testAcl.Name == "" {
204+
testAcl.Name = apiAcl.Name
205+
}
206+
return aclEquals(&testAcl, &apiAcl)
207+
}
208+
209+
rs, ok := s.RootModule().Resources[frontendName]
210+
if !ok {
211+
return fmt.Errorf("resource not found: %s", frontendName)
212+
}
213+
214+
if rs.Primary.ID == "" {
215+
return fmt.Errorf("resource id is not set")
216+
}
217+
218+
lbAPI, region, ID, err := lbAPIWithRegionAndID(tt.Meta, rs.Primary.ID)
219+
if err != nil {
220+
return err
221+
}
222+
223+
//fetch our acls from the scaleway
224+
resACL, err := lbAPI.ListACLs(&lb.ListACLsRequest{
225+
Region: region,
226+
FrontendID: ID,
227+
}, scw.WithAllPages())
228+
if err != nil {
229+
return fmt.Errorf("error on getting acl list [%s]", err)
230+
}
231+
232+
//verify that the count of api acl is the same as we are expecting it to be
233+
if len(expectedAcls) != len(resACL.ACLs) {
234+
return fmt.Errorf("acl count is wrong")
235+
}
236+
//convert them to map indexed by the acl index
237+
aclMap := make(map[int32]*lb.ACL)
238+
for _, acl := range resACL.ACLs {
239+
aclMap[acl.Index] = acl
240+
}
241+
242+
//check that every index is set up correctly
243+
for i := 1; i <= len(expectedAcls); i++ {
244+
if _, found := aclMap[int32(i)]; !found {
245+
return fmt.Errorf("cannot find an index set [%d]", i)
246+
}
247+
if !testCompareAcls(*expectedAcls[i-1], *aclMap[int32(i)]) {
248+
return fmt.Errorf("two acls are not equal on stage %d", i)
249+
}
250+
}
251+
//check the actual data
252+
253+
return nil
254+
}
255+
}

0 commit comments

Comments
 (0)