-
Notifications
You must be signed in to change notification settings - Fork 670
/
data_source_ibm_is_subnet.go
262 lines (226 loc) · 6.8 KB
/
data_source_ibm_is_subnet.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"fmt"
"log"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceIBMISSubnet() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISSubnetRead,
Schema: map[string]*schema.Schema{
"identifier": {
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{isSubnetName, "identifier"},
ValidateFunc: validate.InvokeDataSourceValidator("ibm_is_subnet", "identifier"),
},
isSubnetIpv4CidrBlock: {
Type: schema.TypeString,
Computed: true,
},
isSubnetAvailableIpv4AddressCount: {
Type: schema.TypeInt,
Computed: true,
},
isSubnetTotalIpv4AddressCount: {
Type: schema.TypeInt,
Computed: true,
},
isSubnetName: {
Type: schema.TypeString,
Computed: true,
Optional: true,
ExactlyOneOf: []string{isSubnetName, "identifier"},
ValidateFunc: validate.InvokeDataSourceValidator("ibm_is_subnet", isSubnetName),
},
isSubnetTags: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: flex.ResourceIBMVPCHash,
Description: "List of tags",
},
isSubnetAccessTags: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: flex.ResourceIBMVPCHash,
Description: "List of access tags",
},
isSubnetCRN: {
Type: schema.TypeString,
Computed: true,
Description: "The crn of the resource",
},
isSubnetNetworkACL: {
Type: schema.TypeString,
Computed: true,
},
isSubnetPublicGateway: {
Type: schema.TypeString,
Computed: true,
},
isSubnetStatus: {
Type: schema.TypeString,
Computed: true,
},
isSubnetVPC: {
Type: schema.TypeString,
Computed: true,
},
isSubnetVPCName: {
Type: schema.TypeString,
Computed: true,
},
isSubnetZone: {
Type: schema.TypeString,
Computed: true,
},
isSubnetResourceGroup: {
Type: schema.TypeString,
Computed: true,
},
flex.ResourceControllerURL: {
Type: schema.TypeString,
Computed: true,
Description: "The URL of the IBM Cloud dashboard that can be used to explore and view details about this instance",
},
flex.ResourceName: {
Type: schema.TypeString,
Computed: true,
Description: "The name of the resource",
},
flex.ResourceCRN: {
Type: schema.TypeString,
Computed: true,
Description: "The crn of the resource",
},
flex.ResourceStatus: {
Type: schema.TypeString,
Computed: true,
Description: "The status of the resource",
},
flex.ResourceGroupName: {
Type: schema.TypeString,
Computed: true,
Description: "The resource group name in which resource is provisioned",
},
},
}
}
func DataSourceIBMISSubnetValidator() *validate.ResourceValidator {
validateSchema := make([]validate.ValidateSchema, 0)
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: "identifier",
ValidateFunctionIdentifier: validate.ValidateNoZeroValues,
Type: validate.TypeString})
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: isSubnetName,
ValidateFunctionIdentifier: validate.ValidateNoZeroValues,
Type: validate.TypeString})
ibmISSubnetDataSourceValidator := validate.ResourceValidator{ResourceName: "ibm_is_subnet", Schema: validateSchema}
return &ibmISSubnetDataSourceValidator
}
func dataSourceIBMISSubnetRead(d *schema.ResourceData, meta interface{}) error {
err := subnetGetByNameOrID(d, meta)
if err != nil {
return err
}
return nil
}
func subnetGetByNameOrID(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
var subnet *vpcv1.Subnet
if v, ok := d.GetOk("identifier"); ok {
id := v.(string)
getSubnetOptions := &vpcv1.GetSubnetOptions{
ID: &id,
}
subnetinfo, response, err := sess.GetSubnet(getSubnetOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Getting Subnet (%s): %s\n%s", id, err, response)
}
subnet = subnetinfo
} else if v, ok := d.GetOk(isSubnetName); ok {
name := v.(string)
start := ""
allrecs := []vpcv1.Subnet{}
getSubnetsListOptions := &vpcv1.ListSubnetsOptions{}
for {
if start != "" {
getSubnetsListOptions.Start = &start
}
subnetsCollection, response, err := sess.ListSubnets(getSubnetsListOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Fetching subnets List %s\n%s", err, response)
}
start = flex.GetNext(subnetsCollection.Next)
allrecs = append(allrecs, subnetsCollection.Subnets...)
if start == "" {
break
}
}
for _, subnetInfo := range allrecs {
if *subnetInfo.Name == name {
subnet = &subnetInfo
break
}
}
if subnet == nil {
return fmt.Errorf("[ERROR] No subnet found with name (%s)", name)
}
}
d.SetId(*subnet.ID)
d.Set(isSubnetName, *subnet.Name)
d.Set(isSubnetIpv4CidrBlock, *subnet.Ipv4CIDRBlock)
d.Set(isSubnetAvailableIpv4AddressCount, *subnet.AvailableIpv4AddressCount)
d.Set(isSubnetTotalIpv4AddressCount, *subnet.TotalIpv4AddressCount)
if subnet.NetworkACL != nil {
d.Set(isSubnetNetworkACL, *subnet.NetworkACL.ID)
}
if subnet.PublicGateway != nil {
d.Set(isSubnetPublicGateway, *subnet.PublicGateway.ID)
} else {
d.Set(isSubnetPublicGateway, nil)
}
d.Set(isSubnetStatus, *subnet.Status)
d.Set(isSubnetZone, *subnet.Zone.Name)
d.Set(isSubnetVPC, *subnet.VPC.ID)
d.Set(isSubnetVPCName, *subnet.VPC.Name)
controller, err := flex.GetBaseController(meta)
if err != nil {
return err
}
tags, err := flex.GetGlobalTagsUsingCRN(meta, *subnet.CRN, "", isUserTagType)
if err != nil {
log.Printf(
"An error occured during reading of subnet (%s) tags : %s", d.Id(), err)
}
accesstags, err := flex.GetGlobalTagsUsingCRN(meta, *subnet.CRN, "", isAccessTagType)
if err != nil {
log.Printf(
"Error on get of resource subnet (%s) access tags: %s", d.Id(), err)
}
d.Set(isSubnetTags, tags)
d.Set(isSubnetAccessTags, accesstags)
d.Set(isSubnetCRN, *subnet.CRN)
d.Set(flex.ResourceControllerURL, controller+"/vpc-ext/network/subnets")
d.Set(flex.ResourceName, *subnet.Name)
d.Set(flex.ResourceCRN, *subnet.CRN)
d.Set(flex.ResourceStatus, *subnet.Status)
if subnet.ResourceGroup != nil {
d.Set(isSubnetResourceGroup, *subnet.ResourceGroup.ID)
d.Set(flex.ResourceGroupName, *subnet.ResourceGroup.Name)
}
return nil
}