-
Notifications
You must be signed in to change notification settings - Fork 658
/
data_source_ibm_is_vpn_server_route.go
168 lines (145 loc) · 5.93 KB
/
data_source_ibm_is_vpn_server_route.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
// Copyright IBM Corp. 2022 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"context"
"fmt"
"log"
"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 DataSourceIBMIsVPNServerRoute() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMIsVPNServerRouteRead,
Schema: map[string]*schema.Schema{
"vpn_server": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The VPN server identifier.",
},
"identifier": {
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"name", "identifier"},
Description: "The unique identifier for this VPN server route",
},
"name": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ExactlyOneOf: []string{"name", "identifier"},
Description: "The unique user-defined name for this VPN server route",
},
"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.",
},
"lifecycle_state": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The lifecycle state of the VPN route.",
},
"resource_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
},
}
}
func dataSourceIBMIsVPNServerRouteRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := vpcClient(meta)
if err != nil {
return diag.FromErr(err)
}
var vpnServerRoute *vpcv1.VPNServerRoute
if v, ok := d.GetOk("identifier"); ok {
getVPNServerRouteOptions := &vpcv1.GetVPNServerRouteOptions{}
getVPNServerRouteOptions.SetVPNServerID(d.Get("vpn_server").(string))
getVPNServerRouteOptions.SetID(v.(string))
vpnServerRouteInfo, response, err := sess.GetVPNServerRouteWithContext(context, getVPNServerRouteOptions)
if err != nil {
log.Printf("[DEBUG] GetVPNServerRouteWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("[ERROR] GetVPNServerRouteWithContext failed %s\n%s", err, response))
}
vpnServerRoute = vpnServerRouteInfo
} else if v, ok := d.GetOk("name"); ok {
name := v.(string)
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
}
}
for _, vpnServerRouteInfo := range allrecs {
if *vpnServerRouteInfo.Name == name {
vpnServerRoute = &vpnServerRouteInfo
break
}
}
if vpnServerRoute == nil {
log.Printf("[DEBUG] No vpnServer route found with name %s", name)
return diag.FromErr(fmt.Errorf("[ERROR] No vpn server route found with name %s", name))
}
}
d.SetId(fmt.Sprintf("%s/%s", d.Get("vpn_server").(string), *vpnServerRoute.ID))
if err = d.Set("vpn_server", d.Get("vpn_server").(string)); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting vpn_server: %s", err))
}
if err = d.Set("identifier", *vpnServerRoute.ID); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting identifier: %s", err))
}
if err = d.Set("action", vpnServerRoute.Action); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting action: %s", err))
}
if err = d.Set("created_at", flex.DateTimeToString(vpnServerRoute.CreatedAt)); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting created_at: %s", err))
}
if err = d.Set("destination", vpnServerRoute.Destination); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting destination: %s", err))
}
if err = d.Set("href", vpnServerRoute.Href); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting href: %s", err))
}
if err = d.Set("lifecycle_state", vpnServerRoute.LifecycleState); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting lifecycle_state: %s", err))
}
if err = d.Set("name", vpnServerRoute.Name); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting name: %s", err))
}
if err = d.Set("resource_type", vpnServerRoute.ResourceType); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting resource_type: %s", err))
}
return nil
}