|
8 | 8 | "github.com/aws/aws-sdk-go-v2/aws" |
9 | 9 | "github.com/aws/aws-sdk-go-v2/service/ec2" |
10 | 10 | "github.com/aws/aws-sdk-go-v2/service/ssm" |
11 | | - "github.com/gruntwork-io/terratest/modules/core/v2/collections" |
12 | 11 | "github.com/gruntwork-io/terratest/modules/core/v2/logger" |
13 | 12 | "github.com/gruntwork-io/terratest/modules/core/v2/random" |
14 | 13 | "github.com/gruntwork-io/terratest/modules/core/v2/testing" |
@@ -53,11 +52,11 @@ func GetRandomStableRegionContextE(t testing.TestingT, ctx context.Context, appr |
53 | 52 | regionsToPickFrom := stableRegions |
54 | 53 |
|
55 | 54 | if len(approvedRegions) > 0 { |
56 | | - regionsToPickFrom = collections.Intersection(regionsToPickFrom, approvedRegions) |
| 55 | + regionsToPickFrom = intersection(regionsToPickFrom, approvedRegions) |
57 | 56 | } |
58 | 57 |
|
59 | 58 | if len(forbiddenRegions) > 0 { |
60 | | - regionsToPickFrom = collections.Subtract(regionsToPickFrom, forbiddenRegions) |
| 59 | + regionsToPickFrom = subtract(regionsToPickFrom, forbiddenRegions) |
61 | 60 | } |
62 | 61 |
|
63 | 62 | return GetRandomRegionContextE(t, ctx, regionsToPickFrom, nil) |
@@ -100,7 +99,7 @@ func GetRandomRegionContextE(t testing.TestingT, ctx context.Context, approvedRe |
100 | 99 | regionsToPickFrom = allRegions |
101 | 100 | } |
102 | 101 |
|
103 | | - regionsToPickFrom = collections.Subtract(regionsToPickFrom, forbiddenRegions) |
| 102 | + regionsToPickFrom = subtract(regionsToPickFrom, forbiddenRegions) |
104 | 103 | region := random.RandomString(regionsToPickFrom) |
105 | 104 |
|
106 | 105 | logger.Default.Logf(t, "Using region %s", region) |
@@ -255,3 +254,39 @@ func GetRandomRegionForServiceContext(t testing.TestingT, ctx context.Context, s |
255 | 254 |
|
256 | 255 | return region |
257 | 256 | } |
| 257 | + |
| 258 | +// intersection returns the items present in both lists, de-duplicated, in the |
| 259 | +// order they appear in list1. |
| 260 | +func intersection[T comparable](list1, list2 []T) []T { |
| 261 | + lookups := make(map[T]struct{}, len(list2)) |
| 262 | + for _, item := range list2 { |
| 263 | + lookups[item] = struct{}{} |
| 264 | + } |
| 265 | + |
| 266 | + out := make([]T, 0, min(len(list1), len(list2))) |
| 267 | + for _, item := range list1 { |
| 268 | + if _, found := lookups[item]; found { |
| 269 | + out = append(out, item) |
| 270 | + delete(lookups, item) // delete so a repeated list1 item isn't emitted twice |
| 271 | + } |
| 272 | + } |
| 273 | + |
| 274 | + return out |
| 275 | +} |
| 276 | + |
| 277 | +// subtract returns the items in list1 that are not in list2. |
| 278 | +func subtract[T comparable](list1, list2 []T) []T { |
| 279 | + lookups := make(map[T]struct{}, len(list2)) |
| 280 | + for _, item := range list2 { |
| 281 | + lookups[item] = struct{}{} |
| 282 | + } |
| 283 | + |
| 284 | + out := make([]T, 0, len(list1)) |
| 285 | + for _, item := range list1 { |
| 286 | + if _, found := lookups[item]; !found { |
| 287 | + out = append(out, item) |
| 288 | + } |
| 289 | + } |
| 290 | + |
| 291 | + return out |
| 292 | +} |
0 commit comments