-
Notifications
You must be signed in to change notification settings - Fork 657
/
data_source_ibm_is_vpn_server_client.go
199 lines (175 loc) · 7.62 KB
/
data_source_ibm_is_vpn_server_client.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
// 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 DataSourceIBMIsVPNServerClient() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMIsVPNServerClientRead,
Schema: map[string]*schema.Schema{
"vpn_server": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The VPN server identifier.",
},
"identifier": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The VPN client identifier.",
},
"client_ip": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The IP address assigned to this VPN client from `client_ip_pool`.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"address": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The IP address. 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.",
},
},
},
},
"common_name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The common name of client certificate that the VPN client provided when connecting to the server.This property will be present only when the `certificate` client authentication method is enabled on the VPN server.",
},
"created_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The date and time that the VPN client was created.",
},
"disconnected_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The date and time that the VPN client was disconnected.",
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The URL for this VPN client.",
},
"remote_ip": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The remote IP address of this VPN client.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"address": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The IP address. 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.",
},
},
},
},
"remote_port": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The remote port of this VPN client.",
},
"resource_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
"status": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The status of the VPN client:- `connected`: the VPN client is `connected` to this VPN server.- `disconnected`: the VPN client is `disconnected` from this VPN server.The 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 client on which the unexpected property value was encountered.",
},
"username": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The username that this VPN client provided when connecting to the VPN server.This property will be present only when the`username` client authentication method is enabled on the VPN server.",
},
},
}
}
func dataSourceIBMIsVPNServerClientRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := vpcClient(meta)
if err != nil {
return diag.FromErr(err)
}
getVPNServerClientOptions := &vpcv1.GetVPNServerClientOptions{}
getVPNServerClientOptions.SetVPNServerID(d.Get("vpn_server").(string))
getVPNServerClientOptions.SetID(d.Get("identifier").(string))
vpnServerClient, response, err := sess.GetVPNServerClientWithContext(context, getVPNServerClientOptions)
if err != nil {
log.Printf("[DEBUG] GetVPNServerClientWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("[ERROR] GetVPNServerClientWithContext failed %s\n%s", err, response))
}
d.SetId(*vpnServerClient.ID)
if vpnServerClient.ClientIP != nil {
err = d.Set("client_ip", dataSourceVPNServerClientFlattenClientIP(*vpnServerClient.ClientIP))
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting client_ip %s", err))
}
}
if err = d.Set("common_name", vpnServerClient.CommonName); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting common_name: %s", err))
}
if err = d.Set("created_at", flex.DateTimeToString(vpnServerClient.CreatedAt)); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting created_at: %s", err))
}
if err = d.Set("disconnected_at", flex.DateTimeToString(vpnServerClient.DisconnectedAt)); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting disconnected_at: %s", err))
}
if err = d.Set("href", vpnServerClient.Href); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting href: %s", err))
}
if vpnServerClient.RemoteIP != nil {
err = d.Set("remote_ip", dataSourceVPNServerClientFlattenRemoteIP(*vpnServerClient.RemoteIP))
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting remote_ip %s", err))
}
}
if err = d.Set("remote_port", flex.IntValue(vpnServerClient.RemotePort)); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting remote_port: %s", err))
}
if err = d.Set("resource_type", vpnServerClient.ResourceType); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting resource_type: %s", err))
}
if err = d.Set("status", vpnServerClient.Status); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting status: %s", err))
}
if err = d.Set("username", vpnServerClient.Username); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting username: %s", err))
}
return nil
}
func dataSourceVPNServerClientFlattenClientIP(result vpcv1.IP) (finalList []map[string]interface{}) {
finalList = []map[string]interface{}{}
finalMap := dataSourceVPNServerClientClientIPToMap(result)
finalList = append(finalList, finalMap)
return finalList
}
func dataSourceVPNServerClientClientIPToMap(clientIPItem vpcv1.IP) (clientIPMap map[string]interface{}) {
clientIPMap = map[string]interface{}{}
if clientIPItem.Address != nil {
clientIPMap["address"] = clientIPItem.Address
}
return clientIPMap
}
func dataSourceVPNServerClientFlattenRemoteIP(result vpcv1.IP) (finalList []map[string]interface{}) {
finalList = []map[string]interface{}{}
finalMap := dataSourceVPNServerClientRemoteIPToMap(result)
finalList = append(finalList, finalMap)
return finalList
}
func dataSourceVPNServerClientRemoteIPToMap(remoteIPItem vpcv1.IP) (remoteIPMap map[string]interface{}) {
remoteIPMap = map[string]interface{}{}
if remoteIPItem.Address != nil {
remoteIPMap["address"] = remoteIPItem.Address
}
return remoteIPMap
}