-
Notifications
You must be signed in to change notification settings - Fork 658
/
data_source_ibm_is_virtual_network_interface_ips.go
136 lines (118 loc) · 4.62 KB
/
data_source_ibm_is_virtual_network_interface_ips.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
// Copyright IBM Corp. 2023 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"context"
"fmt"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM/vpc-go-sdk/vpcv1"
)
func DataSourceIBMIsVirtualNetworkInterfaceIPs() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIsVirtualNetworkInterfaceIPsRead,
Schema: map[string]*schema.Schema{
"virtual_network_interface": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The virtual network interface identifier",
},
"reserved_ips": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Collection of reserved IPs for this virtual network interface.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"address": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The IP address.If the address has not yet been selected, the value will be `0.0.0.0`.This property may add support for IPv6 addresses in the future. When processing a value in this property, verify that the address is in an expected format. If it is not, log an error. Optionally halt processing and surface the error, or bypass the resource on which the unexpected IP address format was encountered.",
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The URL for this reserved IP.",
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this reserved IP.",
},
"reserved_ip": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The unique identifier for this reserved IP.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The name for this reserved IP. The name is unique across all reserved IPs in a subnet.",
},
"resource_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
},
},
},
},
}
}
func dataSourceIsVirtualNetworkInterfaceIPsRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vpcClient, err := vpcClient(meta)
if err != nil {
return diag.FromErr(err)
}
listVirtualNetworkInterfaceIpsOptions := &vpcv1.ListVirtualNetworkInterfaceIpsOptions{}
listVirtualNetworkInterfaceIpsOptions.SetVirtualNetworkInterfaceID(d.Get("virtual_network_interface").(string))
var pager *vpcv1.VirtualNetworkInterfaceIpsPager
pager, err = vpcClient.NewVirtualNetworkInterfaceIpsPager(listVirtualNetworkInterfaceIpsOptions)
if err != nil {
return diag.FromErr(err)
}
allItems, err := pager.GetAll()
if err != nil {
log.Printf("[DEBUG] VirtualNetworkInterfaceIpsPager.GetAll() failed %s", err)
return diag.FromErr(fmt.Errorf("VirtualNetworkInterfaceIpsPager.GetAll() failed %s", err))
}
d.SetId(dataSourceVirtualNetworkInterfaceIPsID(d))
mapSlice := []map[string]interface{}{}
for _, modelItem := range allItems {
modelMap, err := dataSourceIBMIsReservedIpsReservedIPToMap(&modelItem)
if err != nil {
return diag.FromErr(err)
}
mapSlice = append(mapSlice, modelMap)
}
if err = d.Set("reserved_ips", mapSlice); err != nil {
return diag.FromErr(fmt.Errorf("Error setting reserved_ips for virtual network interface datasource %s", err))
}
return nil
}
// dataSourceVirtualNetworkInterfaceIPsID returns a reasonable ID for the list.
func dataSourceVirtualNetworkInterfaceIPsID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}
func dataSourceIBMIsReservedIpsReservedIPCollectionFirstToMap(model *vpcv1.ReservedIPCollectionFirst) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["href"] = model.Href
return modelMap, nil
}
func dataSourceIBMIsReservedIpsReservedIPCollectionNextToMap(model *vpcv1.ReservedIPCollectionNext) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["href"] = model.Href
return modelMap, nil
}
func dataSourceIBMIsReservedIpsReservedIPToMap(model *vpcv1.ReservedIPReference) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["address"] = model.Address
modelMap["href"] = model.Href
modelMap["id"] = model.ID
modelMap["reserved_ip"] = model.ID
modelMap["name"] = model.Name
modelMap["resource_type"] = model.ResourceType
return modelMap, nil
}