This repository has been archived by the owner on Aug 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_source_opentelekomcloud_vpc_subnet_ids_v1.go
103 lines (85 loc) · 2.54 KB
/
data_source_opentelekomcloud_vpc_subnet_ids_v1.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package opentelekomcloud
import (
"fmt"
"sort"
"github.com/huaweicloud/golangsdk/openstack/networking/v1/subnets"
"github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/networkipavailabilities"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func dataSourceVpcSubnetIdsV1() *schema.Resource {
return &schema.Resource{
Read: dataSourceVpcSubnetIdsV1Read,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"vpc_id": {
Type: schema.TypeString,
Required: true,
},
"ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
}
}
func dataSourceVpcSubnetIdsV1Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
subnetClient, err := config.networkingV1Client(GetRegion(d, config))
if err != nil {
return err
}
listOpts := subnets.ListOpts{
VPC_ID: d.Get("vpc_id").(string),
}
refinedSubnets, err := subnets.List(subnetClient, listOpts)
if err != nil {
return fmt.Errorf("Unable to retrieve subnets: %s", err)
}
if len(refinedSubnets) == 0 {
return fmt.Errorf("no matching subnet found for vpc with id %s", d.Get("vpc_id").(string))
}
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud networking client: %s", err)
}
sortedSubnets := make([]SubnetIP, 0)
for _, subnet := range refinedSubnets {
net, err := networkipavailabilities.Get(networkingClient, subnet.ID).Extract()
if err != nil {
return fmt.Errorf("Error retrieving network ip availabilities: %s", err)
}
subnetIPAvail := net.SubnetIPAvailabilities[0]
newSubnet := SubnetIP{
ID: subnet.ID,
IPs: subnetIPAvail.TotalIPs - subnetIPAvail.UsedIPs,
}
sortedSubnets = append(sortedSubnets, newSubnet)
}
// Returns the Subnet contains most available IPs out of a slice of subnets.
sort.Sort(sort.Reverse(subnetSort(sortedSubnets)))
Subnets := make([]string, 0)
for _, subnet := range sortedSubnets {
Subnets = append(Subnets, subnet.ID)
}
d.SetId(d.Get("vpc_id").(string))
d.Set("ids", Subnets)
d.Set("region", GetRegion(d, config))
return nil
}
type SubnetIP struct {
ID string
IPs int
}
type subnetSort []SubnetIP
func (a subnetSort) Len() int { return len(a) }
func (a subnetSort) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a subnetSort) Less(i, j int) bool {
return a[i].IPs < a[j].IPs
}