-
Notifications
You must be signed in to change notification settings - Fork 10
/
resource_appgate_global_settings.go
347 lines (331 loc) · 12.4 KB
/
resource_appgate_global_settings.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package appgate
import (
"context"
"fmt"
"log"
"net/http"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
// Global settings is as the name suggest global settings, and should be treated as a singleton object.
// we can never delete global_settings, but we can UPDATE and DELETE (reset) it.
// https://discuss.hashicorp.com/t/singleton-resource/9869
func resourceGlobalSettings() *schema.Resource {
return &schema.Resource{
CreateContext: resourceGlobalSettingsCreate,
ReadContext: resourceGlobalSettingsRead,
UpdateContext: resourceGlobalSettingsUpdate,
DeleteContext: resourceGlobalSettingsDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
SchemaVersion: 1,
Schema: map[string]*schema.Schema{
"profile_hostname": {
Type: schema.TypeString,
Description: "Client Connections, The hostname to use for generating profile URLs.",
Optional: true,
Computed: true,
},
"claims_token_expiration": {
Type: schema.TypeInt,
Description: "Number of minutes the Claims Token is valid both for administrators and clients.",
Optional: true,
Computed: true,
},
"entitlement_token_expiration": {
Type: schema.TypeInt,
Description: "Number of minutes the Entitlement Token is valid for clients.",
Optional: true,
Computed: true,
},
"administration_token_expiration": {
Type: schema.TypeInt,
Description: "Number of minutes the administration Token is valid for administrators.",
Optional: true,
Computed: true,
},
"vpn_certificate_expiration": {
Type: schema.TypeInt,
Description: "Number of minutes the VPN certificates is valid for clients.",
Optional: true,
Computed: true,
},
"login_banner_message": {
Type: schema.TypeString,
Description: "The configured message will be displayed on the login UI.",
Optional: true,
Computed: true,
},
"message_of_the_day": {
Type: schema.TypeString,
Description: "The configured message will be displayed after a successful logging.",
Optional: true,
Computed: true,
},
"backup_api_enabled": {
Type: schema.TypeBool,
Description: "Whether the backup API is enabled or not.",
Optional: true,
Computed: true,
},
"has_backup_passphrase": {
Type: schema.TypeBool,
Description: "Whether there is a backup passphrase set or not. Deprecated as of 5.0. Use backupApiEnabled instead.",
Optional: true,
Computed: true,
Deprecated: "Deprecated as of 5.0. Use backupApiEnabled instead.",
},
"backup_passphrase": {
Type: schema.TypeString,
Description: "The passphrase to encrypt Appliance Backups when backup API is used.",
Optional: true,
Computed: true,
Sensitive: true,
},
"fips": {
Type: schema.TypeBool,
Description: "FIPS 140-2 Compliant Tunneling.",
Optional: true,
Computed: true,
},
"geo_ip_updates": {
Type: schema.TypeBool,
Description: "Whether the automatic GeoIp updates are enabled or not.",
Optional: true,
Computed: true,
},
"audit_log_persistence_mode": {
Type: schema.TypeString,
Description: "Audit Log persistence mode.",
Optional: true,
Computed: true,
},
"app_discovery_domains": {
Type: schema.TypeSet,
Description: "Domains to monitor for for App Discovery feature.",
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"spa_mode": {
Type: schema.TypeString,
Description: "SPA Mode.",
Optional: true,
Computed: true,
ValidateFunc: func(v interface{}, name string) (warns []string, errs []error) {
// controller validation error message is wrong:
// spaMode FOO was not one of [TCP, UDP_TCP]
// should be
// spaMode FOO was not one of [TCP, UDP-TCP]
// so will validate here first.
s := v.(string)
list := []string{
"TCP",
"UDP-TCP",
}
for _, x := range list {
if s == x {
return
}
}
errs = append(errs, fmt.Errorf("type must be on of %v, got %s", list, s))
return
},
},
"spa_time_window_seconds": {
Type: schema.TypeInt,
Description: "Number of seconds the time skew SPA will allow.",
Optional: true,
Computed: true,
},
"collective_name": {
Type: schema.TypeString,
Description: "Friendly name for the Collective.",
Optional: true,
Computed: true,
},
"collective_id": {
Type: schema.TypeString,
Description: "A randomly generated ID during first installation to identify the Collective.",
Computed: true,
},
},
}
}
func resourceGlobalSettingsCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
return resourceGlobalSettingsUpdate(ctx, d, meta)
}
func resourceGlobalSettingsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// Warning or errors can be collected in a slice type
var diags diag.Diagnostics
log.Printf("[DEBUG] Reading Global settings id: %+v", d.Id())
token, err := meta.(*Client).GetToken()
if err != nil {
return diag.FromErr(err)
}
api := meta.(*Client).API.GlobalSettingsApi
currentVersion := meta.(*Client).ApplianceVersion
request := api.GlobalSettingsGet(ctx)
settings, res, err := request.Authorization(token).Execute()
if err != nil {
d.SetId("")
if res != nil && res.StatusCode == http.StatusNotFound {
return nil
}
return diag.FromErr(fmt.Errorf("Failed to read Global settings, %w", err))
}
d.SetId(settings.GetCollectiveId())
d.Set("claims_token_expiration", settings.GetClaimsTokenExpiration())
d.Set("entitlement_token_expiration", settings.GetEntitlementTokenExpiration())
d.Set("administration_token_expiration", settings.GetAdministrationTokenExpiration())
d.Set("vpn_certificate_expiration", settings.GetVpnCertificateExpiration())
d.Set("login_banner_message", settings.GetLoginBannerMessage())
d.Set("message_of_the_day", settings.GetMessageOfTheDay())
d.Set("backup_api_enabled", settings.GetBackupApiEnabled())
if val, ok := d.GetOk("backup_passphrase"); ok {
d.Set("backup_passphrase", val)
} else {
d.Set("backup_passphrase", settings.GetBackupPassphrase())
}
d.Set("fips", settings.GetFips())
d.Set("geo_ip_updates", settings.GetGeoIpUpdates())
d.Set("audit_log_persistence_mode", settings.GetAuditLogPersistenceMode())
d.Set("app_discovery_domains", settings.GetAppDiscoveryDomains())
d.Set("collective_id", settings.GetCollectiveId())
d.Set("collective_name", settings.GetCollectiveName())
d.Set("spa_time_window_seconds", settings.GetSpaTimeWindowSeconds())
d.Set("spa_mode", settings.GetSpaMode())
if currentVersion.GreaterThanOrEqual(Appliance54Version) {
ccAPI := meta.(*Client).API.ClientProfilesApi
request := ccAPI.ClientConnectionsGet(ctx)
clientConnections, _, err := request.Authorization(token).Execute()
if err != nil {
d.SetId("")
return diag.FromErr(fmt.Errorf("Failed to read Client Connections, %w", err))
}
d.Set("profile_hostname", clientConnections.GetProfileHostname())
}
return diags
}
func resourceGlobalSettingsUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf("[DEBUG] Updating Global settings")
token, err := meta.(*Client).GetToken()
if err != nil {
return diag.FromErr(err)
}
api := meta.(*Client).API.GlobalSettingsApi
currentVersion := meta.(*Client).ApplianceVersion
request := api.GlobalSettingsGet(ctx)
originalsettings, _, err := request.Authorization(token).Execute()
if err != nil {
return diag.FromErr(fmt.Errorf("Failed to read Global settings while updating, %w", err))
}
if d.HasChange("claims_token_expiration") {
originalsettings.SetClaimsTokenExpiration(float32(d.Get("claims_token_expiration").(int)))
}
if d.HasChange("entitlement_token_expiration") {
originalsettings.SetEntitlementTokenExpiration(float32(d.Get("entitlement_token_expiration").(int)))
}
if d.HasChange("administration_token_expiration") {
originalsettings.SetAdministrationTokenExpiration(float32(d.Get("administration_token_expiration").(int)))
}
if d.HasChange("vpn_certificate_expiration") {
originalsettings.SetVpnCertificateExpiration(float32(d.Get("vpn_certificate_expiration").(int)))
}
if d.HasChange("login_banner_message") {
originalsettings.SetLoginBannerMessage(d.Get("login_banner_message").(string))
}
if d.HasChange("message_of_the_day") {
originalsettings.SetMessageOfTheDay(d.Get("message_of_the_day").(string))
}
if d.HasChange("backup_api_enabled") {
originalsettings.SetBackupApiEnabled(d.Get("backup_api_enabled").(bool))
}
if d.HasChange("backup_passphrase") {
originalsettings.SetBackupPassphrase(d.Get("backup_passphrase").(string))
}
if d.HasChange("fips") {
originalsettings.SetFips(d.Get("fips").(bool))
}
if d.HasChange("geo_ip_updates") {
originalsettings.SetGeoIpUpdates(d.Get("geo_ip_updates").(bool))
}
if d.HasChange("audit_log_persistence_mode") {
originalsettings.SetAuditLogPersistenceMode(d.Get("audit_log_persistence_mode").(string))
}
if d.HasChange("app_discovery_domains") {
_, n := d.GetChange("app_discovery_domains")
domains, err := readArrayOfStringsFromConfig(n.(*schema.Set).List())
if err != nil {
return diag.FromErr(err)
}
originalsettings.SetAppDiscoveryDomains(domains)
}
if d.HasChange("spa_mode") {
if currentVersion.LessThan(Appliance55Version) {
return diag.Errorf("spa_mode is not supported on %s", currentVersion.String())
} else if currentVersion.GreaterThanOrEqual(Appliance55Version) {
originalsettings.SetSpaMode(d.Get("spa_mode").(string))
}
}
if d.HasChange("spa_time_window_seconds") {
if currentVersion.LessThan(Appliance60Version) {
return diag.Errorf("spa_time_window_seconds is not supported on %s", currentVersion.String())
} else if currentVersion.GreaterThanOrEqual(Appliance60Version) {
originalsettings.SetSpaTimeWindowSeconds(float32(d.Get("spa_time_window_seconds").(int)))
}
}
if d.HasChange("collective_name") {
if currentVersion.LessThan(Appliance60Version) {
return diag.Errorf("collective_name is not supported on %s", currentVersion.String())
} else if currentVersion.GreaterThanOrEqual(Appliance60Version) {
originalsettings.SetCollectiveName(d.Get("collective_name").(string))
}
}
log.Printf("[DEBUG] Updating Global settings %+v", originalsettings)
req := api.GlobalSettingsPut(ctx)
_, err = req.GlobalSettings(*originalsettings).Authorization(token).Execute()
if err != nil {
return diag.FromErr(fmt.Errorf("Could not update Global settings %w", prettyPrintAPIError(err)))
}
if currentVersion.GreaterThanOrEqual(Appliance54Version) && d.HasChange("profile_hostname") {
ccAPI := meta.(*Client).API.ClientProfilesApi
request := ccAPI.ClientConnectionsGet(ctx)
originalclientConnections, _, err := request.Authorization(token).Execute()
if err != nil {
d.SetId("")
return diag.FromErr(fmt.Errorf("Failed to read Client Connections, %w", err))
}
_, v := d.GetChange("profile_hostname")
originalclientConnections.SetProfileHostname(v.(string))
req := ccAPI.ClientConnectionsPut(ctx)
_, _, err = req.ClientConnections(*originalclientConnections).Authorization(token).Execute()
if err != nil {
return diag.FromErr(fmt.Errorf("Could not update Client Connections %w", prettyPrintAPIError(err)))
}
}
return resourceGlobalSettingsRead(ctx, d, meta)
}
func resourceGlobalSettingsDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// Warning or errors can be collected in a slice type
var diags diag.Diagnostics
log.Printf("[DEBUG] Delete Global settings")
token, err := meta.(*Client).GetToken()
if err != nil {
return diag.FromErr(err)
}
api := meta.(*Client).API.GlobalSettingsApi
if _, err := api.GlobalSettingsDelete(context.Background()).Authorization(token).Execute(); err != nil {
return diag.FromErr(fmt.Errorf("Could not reset Global settings %w", prettyPrintAPIError(err)))
}
d.SetId("")
// The API won't allow us to delete/remove/reset profile_hostname from client_connections so we will just leave it be.
return diags
}