-
Notifications
You must be signed in to change notification settings - Fork 657
/
data_source_ibm_is_vpn_server_routes.go
200 lines (171 loc) · 6.29 KB
/
data_source_ibm_is_vpn_server_routes.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
// Copyright IBM Corp. 2022 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/flex"
"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 DataSourceIBMIsVPNServerRoutes() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMIsVPNServerRoutesRead,
Schema: map[string]*schema.Schema{
"vpn_server": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The VPN server identifier.",
},
"routes": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Collection of VPN routes.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"action": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The action to perform with a packet matching the VPN route:- `translate`: translate the source IP address to one of the private IP addresses of the VPN server.- `deliver`: deliver the packet into the VPC.- `drop`: drop the packetThe enumerated values for this property are expected to expand in the future. When processing this property, check for and log unknown values. Optionally halt processing and surface the error, or bypass the VPN route on which the unexpected property value was encountered.",
},
"created_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The date and time that the VPN route was created.",
},
"destination": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The destination for this VPN route in the VPN server. If an incoming packet does not match any destination, it will be dropped.",
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The URL for this VPN route.",
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this VPN route.",
},
"lifecycle_state": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The lifecycle state of the VPN route.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this VPN route.",
},
"resource_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
},
},
},
},
}
}
func dataSourceIBMIsVPNServerRoutesRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := vpcClient(meta)
if err != nil {
return diag.FromErr(err)
}
start := ""
allrecs := []vpcv1.VPNServerRoute{}
for {
listVPNServerRoutesOptions := &vpcv1.ListVPNServerRoutesOptions{}
listVPNServerRoutesOptions.SetVPNServerID(d.Get("vpn_server").(string))
if start != "" {
listVPNServerRoutesOptions.Start = &start
}
vpnServerRouteCollection, response, err := sess.ListVPNServerRoutesWithContext(context, listVPNServerRoutesOptions)
if err != nil {
log.Printf("[DEBUG] ListVPNServerRoutesWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("[ERROR] ListVPNServerRoutesWithContext failed %s\n%s", err, response))
}
start = flex.GetNext(vpnServerRouteCollection.Next)
allrecs = append(allrecs, vpnServerRouteCollection.Routes...)
if start == "" {
break
}
}
d.SetId(dataSourceIBMIsVPNServerRoutesID(d))
if allrecs != nil {
err = d.Set("routes", dataSourceVPNServerRouteCollectionFlattenRoutes(allrecs))
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting routes %s", err))
}
}
return nil
}
// dataSourceIBMIsVPNServerRoutesID returns a reasonable ID for the list.
func dataSourceIBMIsVPNServerRoutesID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}
func dataSourceVPNServerRouteCollectionFlattenFirst(result vpcv1.VPNServerRouteCollectionFirst) (finalList []map[string]interface{}) {
finalList = []map[string]interface{}{}
finalMap := dataSourceVPNServerRouteCollectionFirstToMap(result)
finalList = append(finalList, finalMap)
return finalList
}
func dataSourceVPNServerRouteCollectionFirstToMap(firstItem vpcv1.VPNServerRouteCollectionFirst) (firstMap map[string]interface{}) {
firstMap = map[string]interface{}{}
if firstItem.Href != nil {
firstMap["href"] = firstItem.Href
}
return firstMap
}
func dataSourceVPNServerRouteCollectionFlattenNext(result vpcv1.VPNServerRouteCollectionNext) (finalList []map[string]interface{}) {
finalList = []map[string]interface{}{}
finalMap := dataSourceVPNServerRouteCollectionNextToMap(result)
finalList = append(finalList, finalMap)
return finalList
}
func dataSourceVPNServerRouteCollectionNextToMap(nextItem vpcv1.VPNServerRouteCollectionNext) (nextMap map[string]interface{}) {
nextMap = map[string]interface{}{}
if nextItem.Href != nil {
nextMap["href"] = nextItem.Href
}
return nextMap
}
func dataSourceVPNServerRouteCollectionFlattenRoutes(result []vpcv1.VPNServerRoute) (routes []map[string]interface{}) {
for _, routesItem := range result {
routes = append(routes, dataSourceVPNServerRouteCollectionRoutesToMap(routesItem))
}
return routes
}
func dataSourceVPNServerRouteCollectionRoutesToMap(routesItem vpcv1.VPNServerRoute) (routesMap map[string]interface{}) {
routesMap = map[string]interface{}{}
if routesItem.Action != nil {
routesMap["action"] = routesItem.Action
}
if routesItem.CreatedAt != nil {
routesMap["created_at"] = routesItem.CreatedAt.String()
}
if routesItem.Destination != nil {
routesMap["destination"] = routesItem.Destination
}
if routesItem.Href != nil {
routesMap["href"] = routesItem.Href
}
if routesItem.ID != nil {
routesMap["id"] = routesItem.ID
}
if routesItem.LifecycleState != nil {
routesMap["lifecycle_state"] = routesItem.LifecycleState
}
if routesItem.Name != nil {
routesMap["name"] = routesItem.Name
}
if routesItem.ResourceType != nil {
routesMap["resource_type"] = routesItem.ResourceType
}
return routesMap
}