-
Notifications
You must be signed in to change notification settings - Fork 669
/
data_source_ibm_is_share_targets.go
325 lines (295 loc) · 9.86 KB
/
data_source_ibm_is_share_targets.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright IBM Corp. 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"context"
"fmt"
"log"
"time"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM/vpc-beta-go-sdk/vpcbetav1"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceIbmIsShareTargets() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIbmIsShareTargetsRead,
DeprecationMessage: "This resource is deprecated and will be removed in a future release. Please use ibm_is_share_mount_targets instead",
Schema: map[string]*schema.Schema{
"share": {
Type: schema.TypeString,
Required: true,
Description: "The file share identifier.",
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The user-defined name for this share target.",
},
"share_targets": {
Type: schema.TypeList,
Computed: true,
Description: "Collection of share targets.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this share target.",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "The date and time that the share target was created.",
},
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this share target.",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this share target.",
},
"lifecycle_state": {
Type: schema.TypeString,
Computed: true,
Description: "The lifecycle state of the mount target.",
},
"mount_path": {
Type: schema.TypeString,
Computed: true,
Description: "The mount path for the share.The IP addresses used in the mount path are currently within the IBM services IP range, but are expected to change to be within one of the VPC's subnets in the future.",
},
"resource_type": {
Type: schema.TypeString,
Computed: true,
Description: "The type of resource referenced.",
},
"subnet": {
Type: schema.TypeList,
Computed: true,
Description: "The subnet associated with this file share target.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"crn": {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this subnet.",
},
"deleted": {
Type: schema.TypeList,
Computed: true,
Description: "If present, this property indicates the referenced resource has been deleted and providessome supplementary information.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"more_info": {
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about deleted resources.",
},
},
},
},
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this subnet.",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this subnet.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this subnet.",
},
"resource_type": {
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
},
},
},
"vpc": {
Type: schema.TypeList,
Computed: true,
Description: "The VPC to which this share target is allowing to mount the file share.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"crn": {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this VPC.",
},
"deleted": {
Type: schema.TypeList,
Computed: true,
Description: "If present, this property indicates the referenced resource has been deleted and providessome supplementary information.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"more_info": {
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about deleted resources.",
},
},
},
},
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this VPC.",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this VPC.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The unique user-defined name for this VPC.",
},
"resource_type": {
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
},
},
},
},
},
},
},
}
}
func dataSourceIbmIsShareTargetsRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vpcClient, err := meta.(conns.ClientSession).VpcV1BetaAPI()
if err != nil {
return diag.FromErr(err)
}
listShareTargetsOptions := &vpcbetav1.ListShareMountTargetsOptions{}
listShareTargetsOptions.SetShareID(d.Get("share").(string))
if name, ok := d.GetOk("name"); ok {
listShareTargetsOptions.SetName(name.(string))
}
shareTargetCollection, response, err := vpcClient.ListShareMountTargetsWithContext(context, listShareTargetsOptions)
if err != nil {
log.Printf("[DEBUG] ListShareTargetsWithContext failed %s\n%s", err, response)
return diag.FromErr(err)
}
d.SetId(dataSourceIbmIsShareTargetsID(d))
if shareTargetCollection.MountTargets != nil {
err = d.Set("share_targets", dataSourceShareTargetCollectionFlattenTargets(shareTargetCollection.MountTargets))
if err != nil {
return diag.FromErr(fmt.Errorf("Error setting targets %s", err))
}
}
return nil
}
// dataSourceIbmIsShareTargetsID returns a reasonable ID for the list.
func dataSourceIbmIsShareTargetsID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}
func dataSourceShareTargetCollectionFlattenTargets(result []vpcbetav1.ShareMountTarget) (targets []map[string]interface{}) {
for _, targetsItem := range result {
targets = append(targets, dataSourceShareTargetCollectionTargetsToMap(targetsItem))
}
return targets
}
func dataSourceShareTargetCollectionTargetsToMap(targetsItem vpcbetav1.ShareMountTarget) (targetsMap map[string]interface{}) {
targetsMap = map[string]interface{}{}
if targetsItem.CreatedAt != nil {
targetsMap["created_at"] = targetsItem.CreatedAt.String()
}
if targetsItem.Href != nil {
targetsMap["href"] = targetsItem.Href
}
if targetsItem.ID != nil {
targetsMap["id"] = targetsItem.ID
}
if targetsItem.LifecycleState != nil {
targetsMap["lifecycle_state"] = targetsItem.LifecycleState
}
if targetsItem.MountPath != nil {
targetsMap["mount_path"] = targetsItem.MountPath
}
if targetsItem.Name != nil {
targetsMap["name"] = targetsItem.Name
}
if targetsItem.ResourceType != nil {
targetsMap["resource_type"] = targetsItem.ResourceType
}
if targetsItem.VPC.CRN != nil {
vpcList := []map[string]interface{}{}
vpcMap := dataSourceShareTargetCollectionTargetsVpcToMap(*targetsItem.VPC)
vpcList = append(vpcList, vpcMap)
targetsMap["vpc"] = vpcList
}
return targetsMap
}
func dataSourceShareTargetCollectionTargetsSubnetToMap(subnetItem vpcbetav1.SubnetReference) (subnetMap map[string]interface{}) {
subnetMap = map[string]interface{}{}
if subnetItem.CRN != nil {
subnetMap["crn"] = subnetItem.CRN
}
if subnetItem.Deleted != nil {
deletedList := []map[string]interface{}{}
deletedMap := dataSourceShareTargetCollectionSubnetDeletedToMap(*subnetItem.Deleted)
deletedList = append(deletedList, deletedMap)
subnetMap["deleted"] = deletedList
}
if subnetItem.Href != nil {
subnetMap["href"] = subnetItem.Href
}
if subnetItem.ID != nil {
subnetMap["id"] = subnetItem.ID
}
if subnetItem.Name != nil {
subnetMap["name"] = subnetItem.Name
}
return subnetMap
}
func dataSourceShareTargetCollectionSubnetDeletedToMap(deletedItem vpcbetav1.SubnetReferenceDeleted) (deletedMap map[string]interface{}) {
deletedMap = map[string]interface{}{}
if deletedItem.MoreInfo != nil {
deletedMap["more_info"] = deletedItem.MoreInfo
}
return deletedMap
}
func dataSourceShareTargetCollectionTargetsVpcToMap(vpcItem vpcbetav1.VPCReference) (vpcMap map[string]interface{}) {
vpcMap = map[string]interface{}{}
if vpcItem.CRN != nil {
vpcMap["crn"] = vpcItem.CRN
}
if vpcItem.Deleted != nil {
deletedList := []map[string]interface{}{}
deletedMap := dataSourceShareTargetCollectionVpcDeletedToMap(*vpcItem.Deleted)
deletedList = append(deletedList, deletedMap)
vpcMap["deleted"] = deletedList
}
if vpcItem.Href != nil {
vpcMap["href"] = vpcItem.Href
}
if vpcItem.ID != nil {
vpcMap["id"] = vpcItem.ID
}
if vpcItem.Name != nil {
vpcMap["name"] = vpcItem.Name
}
return vpcMap
}
func dataSourceShareTargetCollectionVpcDeletedToMap(deletedItem vpcbetav1.VPCReferenceDeleted) (deletedMap map[string]interface{}) {
deletedMap = map[string]interface{}{}
if deletedItem.MoreInfo != nil {
deletedMap["more_info"] = deletedItem.MoreInfo
}
return deletedMap
}