-
Notifications
You must be signed in to change notification settings - Fork 650
/
data_source_ibm_is_vpn_server_client_config.go
83 lines (70 loc) · 2.46 KB
/
data_source_ibm_is_vpn_server_client_config.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
// Copyright IBM Corp. 2022 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"context"
"fmt"
"log"
"os"
"strings"
"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 DataSourceIBMIsVPNServerClientConfiguration() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMIsVPNServerClientConfigurationRead,
Schema: map[string]*schema.Schema{
"vpn_server": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The VPN server identifier.",
},
"file_path": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "The File Path to store configuration.",
},
"vpn_server_client_configuration": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The VPN client configuration.",
},
},
}
}
func dataSourceIBMIsVPNServerClientConfigurationRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := vpcClient(meta)
if err != nil {
return diag.FromErr(err)
}
getVPNServerClientConfigurationOptions := &vpcv1.GetVPNServerClientConfigurationOptions{}
getVPNServerClientConfigurationOptions.SetID(d.Get("vpn_server").(string))
result, response, err := sess.GetVPNServerClientConfigurationWithContext(context, getVPNServerClientConfigurationOptions)
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(d.Get("vpn_server").(string))
configStr := *result
configStr = strings.Trim(configStr, "\n")
configStr = strings.Trim(configStr, `"`)
configStr = strings.Replace(configStr, `\n`, "\n", -1)
if v, ok := d.GetOk("file_path"); ok {
fileName := v.(string)
f, err := os.Create(fileName)
if err == nil {
_, err = f.WriteString(configStr)
}
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error Saving VPNServerClientConfiguration Result: %s", err))
}
log.Printf("OpenVPN client configuration was saved to {{.%s}}", fileName)
}
if err = d.Set("vpn_server_client_configuration", *result); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting VPNServerClientConfiguration Result: %s", err))
}
return nil
}