|
| 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