-
Notifications
You must be signed in to change notification settings - Fork 658
/
resource_ibm_is_vpn_server_client.go
179 lines (147 loc) · 6.15 KB
/
resource_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
// 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 ResourceIBMIsVPNServerClient() *schema.Resource {
return &schema.Resource{
CreateContext: resourceIBMIsVPNServerClientDisconnect,
ReadContext: resourceIBMIsVPNServerClientDisconnect,
UpdateContext: resourceIBMIsVPNServerClientDisconnect,
DeleteContext: resourceIBMIsVPNServerClientDelete,
Importer: &schema.ResourceImporter{},
Schema: map[string]*schema.Schema{
"vpn_server": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The VPN server identifier.",
},
"vpn_client": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The VPN Client identifier.",
},
"delete": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "The delete to use for this VPN client to be deleted or not, when false, client is disconneted and when set to true client is deleted.",
},
"status_code": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "status code of the result.",
},
"description": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "description of the result.",
},
},
}
}
func resourceIBMIsVPNServerClientDisconnect(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("vpn_client").(string))
_, response, err := sess.GetVPNServerClientWithContext(context, getVPNServerClientOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
return nil
}
log.Printf("[DEBUG] GetVPNServerClientWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("[ERROR] GetVPNServerClientWithContext failed %s\n%s", err, response))
}
var flag bool
if v, ok := d.GetOk("delete"); ok {
flag = v.(bool)
}
if flag == false {
disconnectVPNServerRouteOptions := &vpcv1.DisconnectVPNClientOptions{}
disconnectVPNServerRouteOptions.SetVPNServerID(d.Get("vpn_server").(string))
disconnectVPNServerRouteOptions.SetID(d.Get("vpn_client").(string))
response, err := sess.DisconnectVPNClientWithContext(context, disconnectVPNServerRouteOptions)
if err != nil {
log.Printf("[DEBUG] DisconnectVPNClientWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("[ERROR] DisconnectVPNClientWithContext failed %s\n%s", err, response))
}
if err = d.Set("status_code", response.StatusCode); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting status_code: %s", err))
}
if err = d.Set("description", "The VPN client disconnection request was accepted."); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting description: %s", err))
}
d.SetId(fmt.Sprintf("%s/%s/%v", d.Get("vpn_server").(string), d.Get("vpn_client").(string), response.StatusCode))
} else if flag == true {
deleteVPNServerClientOptions := &vpcv1.DeleteVPNServerClientOptions{}
deleteVPNServerClientOptions.SetVPNServerID(d.Get("vpn_server").(string))
deleteVPNServerClientOptions.SetID(d.Get("vpn_client").(string))
response, err := sess.DeleteVPNServerClientWithContext(context, deleteVPNServerClientOptions)
if err != nil {
log.Printf("[DEBUG] DeleteVPNServerClientWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("[ERROR] DeleteVPNServerClientWithContext failed %s\n%s", err, response))
}
if err = d.Set("status_code", response.StatusCode); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting status_code: %s", err))
}
if err = d.Set("description", "The VPN client disconnection request was accepted."); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting status_code: %s", err))
}
d.SetId(fmt.Sprintf("%s/%s", d.Get("vpn_server").(string), d.Get("vpn_client").(string)))
}
if err = d.Set("delete", d.Get("delete")); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting delete: %s", err))
}
return nil
}
func resourceIBMIsVPNServerClientDelete(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := vpcClient(meta)
if err != nil {
return diag.FromErr(err)
}
parts, err := flex.IdParts(d.Id())
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Failed %s\n%s", "false", err))
}
if len(parts) != 2 {
return diag.FromErr(fmt.Errorf("[ERROR] Incorrect ID %s: ID should be a combination of vpnServer/vpnClient", d.Id()))
}
vpnServer := parts[0]
vpnClient := parts[1]
getVPNServerClientOptions := &vpcv1.GetVPNServerClientOptions{}
getVPNServerClientOptions.SetVPNServerID(vpnServer)
getVPNServerClientOptions.SetID(vpnClient)
_, response, err := sess.GetVPNServerClientWithContext(context, getVPNServerClientOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
return nil
}
log.Printf("[DEBUG] GetVPNServerClientWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("[ERROR] GetVPNServerClientWithContext failed %s\n%s", err, response))
}
deleteVPNServerClientOptions := &vpcv1.DeleteVPNServerClientOptions{}
deleteVPNServerClientOptions.SetVPNServerID(d.Get("vpn_server").(string))
deleteVPNServerClientOptions.SetID(d.Get("vpn_client").(string))
response, err = sess.DeleteVPNServerClientWithContext(context, deleteVPNServerClientOptions)
if err != nil {
log.Printf("[DEBUG] DeleteVPNServerClientWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("[ERROR] DeleteVPNServerClientWithContext failed %s\n%s", err, response))
}
d.SetId("")
return nil
}