Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix tfproviderlint #804

Merged
merged 2 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 27 additions & 25 deletions scaleway/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

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

// zoneSchema returns a standard schema for a zone
func zoneSchema() *schema.Schema {
var allZones []string
for _, z := range scw.AllZones {
allZones = append(allZones, z.String())
}
return &schema.Schema{
Type: schema.TypeString,
Description: "The zone you want to attach the resource to",
Optional: true,
ForceNew: true,
Computed: true,
ValidateFunc: validationZone(),
Type: schema.TypeString,
Description: "The zone you want to attach the resource to",
Optional: true,
ForceNew: true,
Computed: true,
ValidateFunc: validation.StringInSlice(
allZones,
true,
),
}
}

// regionSchema returns a standard schema for a zone
func regionSchema() *schema.Schema {
var allRegions []string
for _, z := range scw.AllRegions {
allRegions = append(allRegions, z.String())
}
return &schema.Schema{
Type: schema.TypeString,
Description: "The region you want to attach the resource to",
Optional: true,
ForceNew: true,
Computed: true,
ValidateFunc: validationRegion(),
Type: schema.TypeString,
Description: "The region you want to attach the resource to",
Optional: true,
ForceNew: true,
Computed: true,
ValidateFunc: validation.StringInSlice(
allRegions,
true,
),
}
}

Expand Down Expand Up @@ -437,19 +452,6 @@ func validateDuration() schema.SchemaValidateFunc {
}
}

func validateHour() schema.SchemaValidateFunc {
return func(i interface{}, s string) (strings []string, errors []error) {
integer, isInteger := i.(int)
if !isInteger {
return nil, []error{fmt.Errorf("%v is not an int", i)}
}
if integer < 0 || integer > 23 {
return nil, []error{fmt.Errorf("int is outside range 0-23 for value %d", integer)}
}
return nil, nil
}
}

func diffSuppressFuncDuration(k, old, new string, d *schema.ResourceData) bool {
if old == new {
return true
Expand Down
14 changes: 2 additions & 12 deletions scaleway/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,8 @@ func Provider(config *ProviderConfig) plugin.ProviderFunc {
Description: "The Scaleway project ID.",
ValidateFunc: validationUUID(),
},
"region": {
Type: schema.TypeString,
Optional: true,
Description: "The Scaleway default region to use for your resources.",
ValidateFunc: validationRegion(),
},
"zone": {
Type: schema.TypeString,
Optional: true,
Description: "The Scaleway default zone to use for your resources.",
ValidateFunc: validationZone(),
},
"region": regionSchema(),
"zone": zoneSchema(),
"api_url": {
Type: schema.TypeString,
Optional: true,
Expand Down
2 changes: 1 addition & 1 deletion scaleway/resource_k8s_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func resourceScalewayK8SCluster() *schema.Resource {
Type: schema.TypeInt,
Required: true,
Description: "Start hour of the 2-hour maintenance window",
ValidateFunc: validateHour(),
ValidateFunc: validation.IntBetween(0, 23),
},
"maintenance_window_day": {
Type: schema.TypeString,
Expand Down
255 changes: 255 additions & 0 deletions scaleway/resource_lb_acl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
package scaleway

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/scaleway/scaleway-sdk-go/api/lb/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func TestAccScalewayLbAcl_Basic(t *testing.T) {
tt := NewTestTools(t)
defer tt.Cleanup()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: tt.ProviderFactories,
CheckDestroy: testAccCheckScalewayLbFrontendDestroy(tt),
Steps: []resource.TestStep{
{
Config: `
resource scaleway_lb_ip ip01 {}
resource scaleway_lb lb01 {
ip_id = scaleway_lb_ip.ip01.id
name = "test-lb-acl"
type = "lb-s"
}
resource scaleway_lb_backend bkd01 {
lb_id = scaleway_lb.lb01.id
forward_protocol = "http"
forward_port = 80
proxy_protocol = "none"
}
resource scaleway_lb_frontend frt01 {
lb_id = scaleway_lb.lb01.id
backend_id = scaleway_lb_backend.bkd01.id
name = "tf-test"
inbound_port = 80
timeout_client = "30s"
acl {
name = "test-acl"
action {
type = "allow"
}
match {
ip_subnet = ["192.168.0.1", "192.168.0.2", "192.168.10.0/24"]
http_filter = "acl_http_filter_none"
http_filter_value = []
invert = "true"
}
}
acl {
action {
type = "allow"
}
match {
ip_subnet = ["0.0.0.0/0"]
http_filter = "path_begin"
http_filter_value = ["criteria1","criteria2"]
invert = "true"
}
}
acl {
action {
type = "allow"
}
match {
ip_subnet = ["0.0.0.0/0"]
http_filter = "path_begin"
http_filter_value = ["criteria1","criteria2"]
}
}
acl {
action {
type = "allow"
}
match {
ip_subnet = ["0.0.0.0/0"]
http_filter = "acl_http_filter_none"
http_filter_value = []
}
}
acl {
match {
http_filter_value = []
ip_subnet = ["0.0.0.0/0"]
}
action {
type = "deny"
}
}
}
`,
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayACLAreCorrect(tt, "scaleway_lb_frontend.frt01", []*lb.ACL{
{
Name: "test-acl",
Match: &lb.ACLMatch{
IPSubnet: scw.StringSlicePtr([]string{"192.168.0.1", "192.168.0.2", "192.168.10.0/24"}),
HTTPFilter: lb.ACLHTTPFilterACLHTTPFilterNone,
HTTPFilterValue: []*string{},
Invert: true,
},
Action: &lb.ACLAction{Type: lb.ACLActionTypeAllow},
},
{
Match: &lb.ACLMatch{
IPSubnet: scw.StringSlicePtr([]string{"0.0.0.0/0"}),
HTTPFilter: lb.ACLHTTPFilterPathBegin,
HTTPFilterValue: scw.StringSlicePtr([]string{"criteria1", "criteria2"}),
Invert: true,
},
Action: &lb.ACLAction{Type: lb.ACLActionTypeAllow},
},
{
Match: &lb.ACLMatch{
IPSubnet: scw.StringSlicePtr([]string{"0.0.0.0/0"}),
HTTPFilter: lb.ACLHTTPFilterPathBegin,
HTTPFilterValue: scw.StringSlicePtr([]string{"criteria1", "criteria2"}),
Invert: false,
},
Action: &lb.ACLAction{Type: lb.ACLActionTypeAllow},
},
{
Match: &lb.ACLMatch{
IPSubnet: scw.StringSlicePtr([]string{"0.0.0.0/0"}),
HTTPFilter: lb.ACLHTTPFilterACLHTTPFilterNone,
HTTPFilterValue: []*string{},
Invert: false,
},
Action: &lb.ACLAction{Type: lb.ACLActionTypeAllow},
},
{
Match: &lb.ACLMatch{
IPSubnet: scw.StringSlicePtr([]string{"0.0.0.0/0"}),
HTTPFilter: lb.ACLHTTPFilterACLHTTPFilterNone,
HTTPFilterValue: []*string{},
},
Action: &lb.ACLAction{Type: lb.ACLActionTypeDeny},
},
}),
),
},
{
Config: `
resource scaleway_lb_ip ip01 {}
resource scaleway_lb lb01 {
ip_id = scaleway_lb_ip.ip01.id
name = "test-lb-acl"
type = "lb-s"
}
resource scaleway_lb_backend bkd01 {
lb_id = scaleway_lb.lb01.id
forward_protocol = "http"
forward_port = 80
proxy_protocol = "none"
}
resource scaleway_lb_frontend frt01 {
lb_id = scaleway_lb.lb01.id
backend_id = scaleway_lb_backend.bkd01.id
name = "tf-test"
inbound_port = 80
timeout_client = "30s"
acl {
action {
type = "allow"
}
match {
ip_subnet = ["10.0.0.10"]
http_filter = "path_begin"
http_filter_value = ["foo","bar"]
}
}
}
`,
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayACLAreCorrect(tt, "scaleway_lb_frontend.frt01", []*lb.ACL{
{
Match: &lb.ACLMatch{
IPSubnet: scw.StringSlicePtr([]string{"10.0.0.10"}),
HTTPFilter: lb.ACLHTTPFilterPathBegin,
HTTPFilterValue: scw.StringSlicePtr([]string{"foo", "bar"}),
Invert: false,
},
Action: &lb.ACLAction{Type: lb.ACLActionTypeAllow},
},
}),
),
},
},
})
}

func testAccCheckScalewayACLAreCorrect(tt *TestTools, frontendName string, expectedAcls []*lb.ACL) resource.TestCheckFunc {
return func(s *terraform.State) error {
//define a wrapper for acl comparison
testCompareAcls := func(testAcl, apiAcl lb.ACL) bool {
//drop some values which are not part of the testing acl structure
apiAcl.ID = ""
apiAcl.Frontend = nil
//if we do not pass any name, then drop it from comparison
if testAcl.Name == "" {
testAcl.Name = apiAcl.Name
}
return aclEquals(&testAcl, &apiAcl)
}

rs, ok := s.RootModule().Resources[frontendName]
if !ok {
return fmt.Errorf("resource not found: %s", frontendName)
}

if rs.Primary.ID == "" {
return fmt.Errorf("resource id is not set")
}

lbAPI, region, ID, err := lbAPIWithRegionAndID(tt.Meta, rs.Primary.ID)
if err != nil {
return err
}

//fetch our acls from the scaleway
resACL, err := lbAPI.ListACLs(&lb.ListACLsRequest{
Region: region,
FrontendID: ID,
}, scw.WithAllPages())
if err != nil {
return fmt.Errorf("error on getting acl list [%s]", err)
}

//verify that the count of api acl is the same as we are expecting it to be
if len(expectedAcls) != len(resACL.ACLs) {
return fmt.Errorf("acl count is wrong")
}
//convert them to map indexed by the acl index
aclMap := make(map[int32]*lb.ACL)
for _, acl := range resACL.ACLs {
aclMap[acl.Index] = acl
}

//check that every index is set up correctly
for i := 1; i <= len(expectedAcls); i++ {
if _, found := aclMap[int32(i)]; !found {
return fmt.Errorf("cannot find an index set [%d]", i)
}
if !testCompareAcls(*expectedAcls[i-1], *aclMap[int32(i)]) {
return fmt.Errorf("two acls are not equal on stage %d", i)
}
}
//check the actual data

return nil
}
}
Loading