/
regions.go
69 lines (55 loc) · 2.08 KB
/
regions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package selectel
import (
"errors"
"fmt"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"github.com/selectel/go-selvpcclient/selvpcclient/resell/v2/crossregionsubnets"
"github.com/selectel/go-selvpcclient/selvpcclient/resell/v2/subnets"
)
func expandVPCV2Regions(rawRegions *schema.Set) []string {
regions := rawRegions.List()
expandedRegions := make([]string, len(regions))
for i, region := range regions {
expandedRegions[i] = region.(string)
}
return expandedRegions
}
// expandVPCV2CrossRegionOpts converts the provided rawRegions structure to
// the slice of crossregionsubnets.CrossRegionOpt.
func expandVPCV2CrossRegionOpts(rawRegions *schema.Set) ([]crossregionsubnets.CrossRegionOpt, error) {
rawRegionsLen := rawRegions.Len()
if rawRegionsLen == 0 {
return nil, errors.New("got empty regions")
}
// Pre-allocate memory for expandedCrossRegionOpts slice.
expandedCrossRegionOpts := make([]crossregionsubnets.CrossRegionOpt, rawRegionsLen)
// Iterate over each value in rawRegions and add it to expandedCrossRegionOpts slice.
for i, rawRegion := range rawRegions.List() {
var region string
mapRegion := rawRegion.(map[string]interface{})
if value, ok := mapRegion["region"]; ok {
region = value.(string)
}
expandedCrossRegionOpts[i] = crossregionsubnets.CrossRegionOpt{
Region: region,
}
}
return expandedCrossRegionOpts, nil
}
// regionsMapsFromSubnetsStructs converts the provided subnets.Subnet to
// the slice of maps with associated regions correspondingly to the resource's schema.
func regionsMapsFromSubnetsStructs(subnetsStructs []subnets.Subnet) []map[string]interface{} {
associatedRegions := make([]map[string]interface{}, len(subnetsStructs))
for i, subnet := range subnetsStructs {
associatedRegions[i] = map[string]interface{}{
"region": subnet.Region,
}
}
return associatedRegions
}
// hashRegions is a hash function to use with the "regions" set.
func hashRegions(v interface{}) int {
m := v.(map[string]interface{})
return hashcode.String(fmt.Sprintf("%s-", m["region"].(string)))
}