/
http_configuration_resource.go
294 lines (258 loc) · 12.3 KB
/
http_configuration_resource.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
package httpconfiguration
import (
"context"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
client "github.com/pingidentity/pingdirectory-go-client/v10000/configurationapi"
"github.com/pingidentity/terraform-provider-pingdirectory/internal/operations"
"github.com/pingidentity/terraform-provider-pingdirectory/internal/resource/config"
internaltypes "github.com/pingidentity/terraform-provider-pingdirectory/internal/types"
"github.com/pingidentity/terraform-provider-pingdirectory/internal/version"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ resource.Resource = &httpConfigurationResource{}
_ resource.ResourceWithConfigure = &httpConfigurationResource{}
_ resource.ResourceWithImportState = &httpConfigurationResource{}
)
// Create a Http Configuration resource
func NewHttpConfigurationResource() resource.Resource {
return &httpConfigurationResource{}
}
// httpConfigurationResource is the resource implementation.
type httpConfigurationResource struct {
providerConfig internaltypes.ProviderConfiguration
apiClient *client.APIClient
}
// Metadata returns the resource type name.
func (r *httpConfigurationResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_default_http_configuration"
}
// Configure adds the provider configured client to the resource.
func (r *httpConfigurationResource) Configure(_ context.Context, req resource.ConfigureRequest, _ *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
providerCfg := req.ProviderData.(internaltypes.ResourceConfiguration)
r.providerConfig = providerCfg.ProviderConfig
r.apiClient = providerCfg.ApiClient
}
type httpConfigurationResourceModel struct {
Id types.String `tfsdk:"id"`
Notifications types.Set `tfsdk:"notifications"`
RequiredActions types.Set `tfsdk:"required_actions"`
Type types.String `tfsdk:"type"`
IncludeStackTracesInErrorPages types.Bool `tfsdk:"include_stack_traces_in_error_pages"`
IncludeServletInformationInErrorPages types.Bool `tfsdk:"include_servlet_information_in_error_pages"`
}
// GetSchema defines the schema for the resource.
func (r *httpConfigurationResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
schemaDef := schema.Schema{
Description: "Manages a Http Configuration.",
Attributes: map[string]schema.Attribute{
"type": schema.StringAttribute{
Description: "The type of HTTP Configuration resource. Options are ['http-configuration']",
Optional: false,
Required: false,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Validators: []validator.String{
stringvalidator.OneOf([]string{"http-configuration"}...),
},
},
"include_stack_traces_in_error_pages": schema.BoolAttribute{
Description: "Indicates whether exceptions thrown by servlet or web application extensions will be included in the resulting error page response. Stack traces can be helpful in diagnosing application errors, but in production they may reveal information that might be useful to a malicious attacker.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Bool{
boolplanmodifier.UseStateForUnknown(),
},
},
"include_servlet_information_in_error_pages": schema.BoolAttribute{
Description: "Supported in PingDirectory product version 9.3.0.0+. Indicates whether to expose servlet information in the error page response.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Bool{
boolplanmodifier.UseStateForUnknown(),
},
},
},
}
config.AddCommonResourceSchema(&schemaDef, false)
resp.Schema = schemaDef
}
// Validate that any restrictions are met in the plan and set any type-specific defaults
func (r *httpConfigurationResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
compare, err := version.Compare(r.providerConfig.ProductVersion, version.PingDirectory9300)
if err != nil {
resp.Diagnostics.AddError("Failed to compare PingDirectory versions", err.Error())
return
}
if compare >= 0 {
// Every remaining property is supported
return
}
var model httpConfigurationResourceModel
req.Plan.Get(ctx, &model)
if internaltypes.IsDefined(model.IncludeServletInformationInErrorPages) {
resp.Diagnostics.AddError("Attribute 'include_servlet_information_in_error_pages' not supported by PingDirectory version "+r.providerConfig.ProductVersion, "")
}
}
// Read a HttpConfigurationResponse object into the model struct
func readHttpConfigurationResponse(ctx context.Context, r *client.HttpConfigurationResponse, state *httpConfigurationResourceModel, diagnostics *diag.Diagnostics) {
state.Type = types.StringValue("http-configuration")
// Placeholder id value required by test framework
state.Id = types.StringValue("id")
state.IncludeStackTracesInErrorPages = internaltypes.BoolTypeOrNil(r.IncludeStackTracesInErrorPages)
state.IncludeServletInformationInErrorPages = internaltypes.BoolTypeOrNil(r.IncludeServletInformationInErrorPages)
state.Notifications, state.RequiredActions = config.ReadMessages(ctx, r.Urnpingidentityschemasconfigurationmessages20, diagnostics)
}
// Create any update operations necessary to make the state match the plan
func createHttpConfigurationOperations(plan httpConfigurationResourceModel, state httpConfigurationResourceModel) []client.Operation {
var ops []client.Operation
operations.AddBoolOperationIfNecessary(&ops, plan.IncludeStackTracesInErrorPages, state.IncludeStackTracesInErrorPages, "include-stack-traces-in-error-pages")
operations.AddBoolOperationIfNecessary(&ops, plan.IncludeServletInformationInErrorPages, state.IncludeServletInformationInErrorPages, "include-servlet-information-in-error-pages")
return ops
}
// Create a new resource
// For edit only resources like this, create doesn't actually "create" anything - it "adopts" the existing
// config object into management by terraform. This method reads the existing config object
// and makes any changes needed to make it match the plan - similar to the Update method.
func (r *httpConfigurationResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
// Retrieve values from plan
var plan httpConfigurationResourceModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
readResponse, httpResp, err := r.apiClient.HttpConfigurationAPI.GetHttpConfiguration(
config.ProviderBasicAuthContext(ctx, r.providerConfig)).Execute()
if err != nil {
config.ReportHttpError(ctx, &resp.Diagnostics, "An error occurred while getting the Http Configuration", err, httpResp)
return
}
// Log response JSON
responseJson, err := readResponse.MarshalJSON()
if err == nil {
tflog.Debug(ctx, "Read response: "+string(responseJson))
}
// Read the existing configuration
var state httpConfigurationResourceModel
readHttpConfigurationResponse(ctx, readResponse, &state, &resp.Diagnostics)
// Determine what changes are needed to match the plan
updateRequest := r.apiClient.HttpConfigurationAPI.UpdateHttpConfiguration(config.ProviderBasicAuthContext(ctx, r.providerConfig))
ops := createHttpConfigurationOperations(plan, state)
if len(ops) > 0 {
updateRequest = updateRequest.UpdateRequest(*client.NewUpdateRequest(ops))
// Log operations
operations.LogUpdateOperations(ctx, ops)
updateResponse, httpResp, err := r.apiClient.HttpConfigurationAPI.UpdateHttpConfigurationExecute(updateRequest)
if err != nil {
config.ReportHttpError(ctx, &resp.Diagnostics, "An error occurred while updating the Http Configuration", err, httpResp)
return
}
// Log response JSON
responseJson, err := updateResponse.MarshalJSON()
if err == nil {
tflog.Debug(ctx, "Update response: "+string(responseJson))
}
// Read the response
readHttpConfigurationResponse(ctx, updateResponse, &state, &resp.Diagnostics)
}
diags = resp.State.Set(ctx, state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
// Read resource information
func (r *httpConfigurationResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// Get current state
var state httpConfigurationResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
readResponse, httpResp, err := r.apiClient.HttpConfigurationAPI.GetHttpConfiguration(
config.ProviderBasicAuthContext(ctx, r.providerConfig)).Execute()
if err != nil {
config.ReportHttpError(ctx, &resp.Diagnostics, "An error occurred while getting the Http Configuration", err, httpResp)
return
}
// Log response JSON
responseJson, err := readResponse.MarshalJSON()
if err == nil {
tflog.Debug(ctx, "Read response: "+string(responseJson))
}
// Read the response into the state
readHttpConfigurationResponse(ctx, readResponse, &state, &resp.Diagnostics)
// Set refreshed state
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
}
// Update a resource
func (r *httpConfigurationResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Retrieve values from plan
var plan httpConfigurationResourceModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Get the current state to see how any attributes are changing
var state httpConfigurationResourceModel
req.State.Get(ctx, &state)
updateRequest := r.apiClient.HttpConfigurationAPI.UpdateHttpConfiguration(
config.ProviderBasicAuthContext(ctx, r.providerConfig))
// Determine what update operations are necessary
ops := createHttpConfigurationOperations(plan, state)
if len(ops) > 0 {
updateRequest = updateRequest.UpdateRequest(*client.NewUpdateRequest(ops))
// Log operations
operations.LogUpdateOperations(ctx, ops)
updateResponse, httpResp, err := r.apiClient.HttpConfigurationAPI.UpdateHttpConfigurationExecute(updateRequest)
if err != nil {
config.ReportHttpError(ctx, &resp.Diagnostics, "An error occurred while updating the Http Configuration", err, httpResp)
return
}
// Log response JSON
responseJson, err := updateResponse.MarshalJSON()
if err == nil {
tflog.Debug(ctx, "Update response: "+string(responseJson))
}
// Read the response
readHttpConfigurationResponse(ctx, updateResponse, &state, &resp.Diagnostics)
} else {
tflog.Warn(ctx, "No configuration API operations created for update")
}
diags = resp.State.Set(ctx, state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
// Delete deletes the resource and removes the Terraform state on success.
// This config object is edit-only, so Terraform can't delete it.
// After running a delete, Terraform will just "forget" about this object and it can be managed elsewhere.
func (r *httpConfigurationResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
// No implementation necessary
}
func (r *httpConfigurationResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
// Set a placeholder id value to appease terraform.
// The real attributes will be imported when terraform performs a read after the import.
// If no value is set here, Terraform will error out when importing.
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), "id")...)
}