/
data_source_ibm_is_subnets.go
132 lines (120 loc) · 3.13 KB
/
data_source_ibm_is_subnets.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package ibm
import (
"strconv"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.ibm.com/Bluemix/riaas-go-client/clients/network"
)
const (
isSubnets = "subnets"
)
func dataSourceIBMISSubnets() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISSubnetsRead,
Schema: map[string]*schema.Schema{
isSubnets: {
Type: schema.TypeList,
Description: "List of subnets",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"id": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"crn": {
Type: schema.TypeString,
Computed: true,
},
"ipv4_cidr_block": {
Type: schema.TypeString,
Computed: true,
},
"ipv6_cidr_block": {
Type: schema.TypeString,
Computed: true,
},
"available_ipv4_address_count": {
Type: schema.TypeString,
Computed: true,
},
"network_acl": {
Type: schema.TypeString,
Computed: true,
},
"public_gateway": {
Type: schema.TypeString,
Computed: true,
},
"resource_group": {
Type: schema.TypeString,
Computed: true,
},
"total_ipv4_address_count": {
Type: schema.TypeString,
Computed: true,
},
"vpc": {
Type: schema.TypeString,
Computed: true,
},
"zone": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceIBMISSubnetsRead(d *schema.ResourceData, meta interface{}) error {
sess, err := meta.(ClientSession).ISSession()
if err != nil {
return err
}
subnetC := network.NewSubnetClient(sess)
availableSubnets, _, err := subnetC.List("")
if err != nil {
return err
}
subnets := make([]map[string]string, len(availableSubnets))
for i, subnet := range availableSubnets {
var aac string = strconv.FormatInt(subnet.AvailableIPV4AddressCount, 10)
var tac string = strconv.FormatInt(subnet.TotalIPV4AddressCount, 10)
subn := make(map[string]string)
subn["name"] = subnet.Name
subn["id"] = string(subnet.ID)
subn["status"] = subnet.Status
subn["crn"] = subnet.Crn
subn["ipv4_cidr_block"] = subnet.IPV4CidrBlock
subn["ipv6_cidr_block"] = subnet.IPV6CidrBlock
subn["available_ipv4_address_count"] = aac
subn["network_acl"] = subnet.NetworkACL.Name
if subnet.PublicGateway != nil {
subn["public_gateway"] = string(subnet.PublicGateway.ID)
}
if subnet.ResourceGroup != nil {
subn["resource_group"] = string(subnet.ResourceGroup.ID)
}
subn["total_ipv4_address_count"] = tac
subn["vpc"] = string(subnet.Vpc.ID)
subn["zone"] = subnet.Zone.Name
subnets[i] = subn
}
d.SetId(dataSourceIBMISSubnetsID(d))
d.Set(isSubnets, subnets)
return nil
}
// dataSourceIBMISSubnetsId returns a reasonable ID for a subnet list.
func dataSourceIBMISSubnetsID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}