-
Notifications
You must be signed in to change notification settings - Fork 1
/
appsec-gateway-profile.go
268 lines (229 loc) · 9.12 KB
/
appsec-gateway-profile.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
package resources
import (
"context"
"github.com/CheckPointSW/terraform-provider-infinity-next/internal/api"
appsecgatewayprofile "github.com/CheckPointSW/terraform-provider-infinity-next/internal/resources/appsec-gateway-profile"
"github.com/CheckPointSW/terraform-provider-infinity-next/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func ResourceAppSecGatewayProfile() *schema.Resource {
return &schema.Resource{
Description: "CloudGuard Application Security Gateway profile is deployed as a VM that runs on a Check Point Gaia OS " +
"with a reverse proxy and Check Point Nano-Agent",
CreateContext: resourceAppSecGatewayProfileCreate,
ReadContext: resourceAppSecGatewayProfileRead,
UpdateContext: resourceAppSecGatewayProfileUpdate,
DeleteContext: resourceAppSecGatewayProfileDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
CustomizeDiff: func(ctx context.Context, diff *schema.ResourceDiff, meta interface{}) error {
if diff.HasChange("additional_settings") {
if err := diff.SetNewComputed("additional_settings_ids"); err != nil {
return err
}
}
if diff.HasChange("reverseproxy_additional_settings") {
if err := diff.SetNewComputed("reverseproxy_additional_settings_ids"); err != nil {
return err
}
}
return nil
},
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"name": {
Type: schema.TypeString,
Description: "The name of the resource, also acts as its unique ID",
Required: true,
},
"profile_type": {
Type: schema.TypeString,
Computed: true,
},
"profile_sub_type": {
Type: schema.TypeString,
Description: "The environment of deployment for the AppSec VM: Aws, Azure, VMware or HyperV",
Required: true,
ValidateFunc: validation.StringInSlice([]string{appsecgatewayprofile.ProfileSubTypeAws, appsecgatewayprofile.ProfileSubTypeAzure, appsecgatewayprofile.ProfileSubTypeVMware, appsecgatewayprofile.ProfileSubTypeHyperV}, false),
},
"additional_settings": {
Type: schema.TypeMap,
Description: "Controls the settings of the connected agents",
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"additional_settings_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"upgrade_mode": {
Type: schema.TypeString,
Description: "The upgrade mode of the profile: Automatic, Manual or Scheduled.\n" +
"The default is Automatic",
Optional: true,
Default: appsecgatewayprofile.UpgradeModeAutomatic,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{appsecgatewayprofile.UpgradeModeAutomatic, appsecgatewayprofile.UpgradeModeManual, appsecgatewayprofile.UpgradeModeScheduled}, false)),
},
"upgrade_time_schedule_type": {
Type: schema.TypeString,
Description: "The schedule type in case upgrade mode is scheduled: DaysInWeek",
Optional: true,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{"DaysInWeek"}, false)),
},
"upgrade_time_hour": {
Type: schema.TypeString,
Description: "The hour of the upgrade time start, for example: 10:00 or 20:00",
Optional: true,
},
"upgrade_time_duration": {
Type: schema.TypeInt,
Description: "The duration of the upgrade in hours",
Optional: true,
},
"upgrade_time_week_days": {
Type: schema.TypeSet,
Description: "The week days of the upgrade time schedule: Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday",
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"reverseproxy_upstream_timeout": {
Type: schema.TypeInt,
Description: "Sets the reverse proxy upstream timeout in seconds",
Optional: true,
},
"reverseproxy_additional_settings": {
Type: schema.TypeMap,
Description: "Sets the reverse proxy settings of linked assets",
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"reverseproxy_additional_settings_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"max_number_of_agents": {
Type: schema.TypeInt,
Description: "Sets the maximum number of agents that can be connected to this profile",
Optional: true,
ValidateDiagFunc: validation.ToDiagFunc(validation.IntAtMost(1000)),
},
"authentication_token": {
Type: schema.TypeString,
Description: "The token used to register an agent to the profile",
Computed: true,
},
},
}
}
func resourceAppSecGatewayProfileCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
var diags diag.Diagnostics
c := meta.(*api.Client)
createInput, err := appsecgatewayprofile.CreateCloudGuardAppSecGatewayProfileInputFromResourceData(d)
if err != nil {
return utils.DiagError("unable to perform AppSecGatewayProfile Create", err, diags)
}
profile, err := appsecgatewayprofile.NewAppSecGatewayProfile(ctx, c, createInput)
if err != nil {
if _, discardErr := c.DiscardChanges(); discardErr != nil {
diags = utils.DiagError("failed to discard changes", discardErr, diags)
}
return utils.DiagError("unable to perform AppSecGatewayProfile Create", err, diags)
}
isValid, err := c.PublishChanges()
if err != nil || !isValid {
if _, discardErr := c.DiscardChanges(); discardErr != nil {
diags = utils.DiagError("failed to discard changes", discardErr, diags)
}
return utils.DiagError("failed to Publish following AppSecGatewayProfile Create", err, diags)
}
if err = appsecgatewayprofile.ReadCloudGuardAppSecGatewayProfileToResourceData(profile, d); err != nil {
if _, discardErr := c.DiscardChanges(); discardErr != nil {
diags = utils.DiagError("failed to discard changes", discardErr, diags)
}
return utils.DiagError("Unable to read AppSecGatewayProfile to resource data", err, diags)
}
return diags
}
func resourceAppSecGatewayProfileRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
var diags diag.Diagnostics
c := meta.(*api.Client)
id := d.Id()
profile, err := appsecgatewayprofile.GetCloudGuardAppSecGatewayProfile(ctx, c, id)
if err != nil {
return utils.DiagError("unable to perform AppSecGatewayProfile Read", err, diags)
}
if err := appsecgatewayprofile.ReadCloudGuardAppSecGatewayProfileToResourceData(profile, d); err != nil {
return utils.DiagError("unable to perform AppSecGatewayProfile Read", err, diags)
}
return diags
}
func resourceAppSecGatewayProfileUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
var diags diag.Diagnostics
c := meta.(*api.Client)
updateInput, err := appsecgatewayprofile.UpdateCloudGuardAppSecGatewayProfileInputFromResourceData(d)
if err != nil {
return utils.DiagError("unable to perform AppSecGatewayProfile Update", err, diags)
}
result, err := appsecgatewayprofile.UpdateAppSecGatewayProfile(ctx, c, d.Id(), updateInput)
if err != nil || !result {
if _, discardErr := c.DiscardChanges(); discardErr != nil {
diags = utils.DiagError("failed to discard changes", discardErr, diags)
}
return utils.DiagError("unable to perform AppSecGatewayProfile Update", err, diags)
}
isValid, err := c.PublishChanges()
if err != nil || !isValid {
if _, discardErr := c.DiscardChanges(); discardErr != nil {
diags = utils.DiagError("failed to discard changes", discardErr, diags)
}
return utils.DiagError("failed to Publish following AppSecGatewayProfile Update", err, diags)
}
profile, err := appsecgatewayprofile.GetCloudGuardAppSecGatewayProfile(ctx, c, d.Id())
if err != nil {
return utils.DiagError("failed get AppSecGatewayProfile after update", err, diags)
}
if err := appsecgatewayprofile.ReadCloudGuardAppSecGatewayProfileToResourceData(profile, d); err != nil {
return utils.DiagError("unable to perform read AppSecGatewayProfile read after update", err, diags)
}
return diags
}
func resourceAppSecGatewayProfileDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
var diags diag.Diagnostics
c := meta.(*api.Client)
ID := d.Id()
result, err := appsecgatewayprofile.DeleteAppSecGatewayProfile(ctx, c, ID)
if err != nil || !result {
if _, discardErr := c.DiscardChanges(); discardErr != nil {
diags = utils.DiagError("failed to discard changes", discardErr, diags)
}
return utils.DiagError("unable to perform AppSecGatewayProfile Delete", err, diags)
}
isValid, err := c.PublishChanges()
if err != nil || !isValid {
if _, discardErr := c.DiscardChanges(); discardErr != nil {
diags = utils.DiagError("failed to discard changes", discardErr, diags)
}
return utils.DiagError("failed to Publish following AppSecGatewayProfile Delete", err, diags)
}
d.SetId("")
return diags
}