-
Notifications
You must be signed in to change notification settings - Fork 2
/
embedded-profile.go
238 lines (200 loc) · 7.59 KB
/
embedded-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
package resources
import (
"context"
"github.com/CheckPointSW/terraform-provider-infinity-next/internal/api"
embeddedprofile "github.com/CheckPointSW/terraform-provider-infinity-next/internal/resources/embedded-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 ResourceEmbeddedProfile() *schema.Resource {
return &schema.Resource{
Description: "Embedded profile",
CreateContext: resourceEmbeddedProfileCreate,
ReadContext: resourceEmbeddedProfileRead,
UpdateContext: resourceEmbeddedProfileUpdate,
DeleteContext: resourceEmbeddedProfileDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
CustomizeDiff: func(ctx context.Context, diff *schema.ResourceDiff, meta interface{}) error {
if diff.HasChange("additional_settings") {
return diff.SetNewComputed("additional_settings_ids")
}
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,
},
"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,
},
},
"defined_applications_only": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"upgrade_mode": {
Type: schema.TypeString,
Description: "The upgrade mode of the profile: Automatic, Manual or Scheduled.\n" +
"The default is Automatic",
Optional: true,
Default: embeddedprofile.UpgradeModeAutomatic,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{embeddedprofile.UpgradeModeAutomatic, embeddedprofile.UpgradeModeManual, embeddedprofile.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,
},
},
"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 resourceEmbeddedProfileCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
var diags diag.Diagnostics
c := meta.(*api.Client)
createInput, err := embeddedprofile.CreateEmbeddedProfileInputFromResourceData(d)
if err != nil {
return utils.DiagError("unable to perform EmbeddedProfile Create", err, diags)
}
profile, err := embeddedprofile.NewEmbeddedProfile(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 EmbeddedProfile 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 EmbeddedProfile Create", err, diags)
}
if err = embeddedprofile.ReadEmbeddedProfileToResourceData(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 EmbeddedProfile to resource data", err, diags)
}
return diags
}
func resourceEmbeddedProfileRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
var diags diag.Diagnostics
c := meta.(*api.Client)
id := d.Id()
profile, err := embeddedprofile.GetEmbeddedProfile(c, id)
if err != nil {
return utils.DiagError("unable to perform EmbeddedProfile Read", err, diags)
}
if err := embeddedprofile.ReadEmbeddedProfileToResourceData(profile, d); err != nil {
return utils.DiagError("unable to perform EmbeddedProfile Read", err, diags)
}
return diags
}
func resourceEmbeddedProfileUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
var diags diag.Diagnostics
c := meta.(*api.Client)
updateInput, err := embeddedprofile.UpdateEmbeddedProfileInputFromResourceData(d)
if err != nil {
return utils.DiagError("unable to perform EmbeddedProfile Update", err, diags)
}
result, err := embeddedprofile.UpdateEmbeddedProfile(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 EmbeddedProfile 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 EmbeddedProfile Update", err, diags)
}
profile, err := embeddedprofile.GetEmbeddedProfile(c, d.Id())
if err != nil {
return utils.DiagError("failed get EmbeddedProfile after update", err, diags)
}
if err := embeddedprofile.ReadEmbeddedProfileToResourceData(profile, d); err != nil {
return utils.DiagError("unable to perform read EmbeddedProfile read after update", err, diags)
}
return diags
}
func resourceEmbeddedProfileDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
var diags diag.Diagnostics
c := meta.(*api.Client)
ID := d.Id()
result, err := embeddedprofile.DeleteEmbeddedProfile(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 EmbeddedProfile 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 EmbeddedProfile Delete", err, diags)
}
d.SetId("")
return diags
}