|
| 1 | +// Copyright IBM Corp. 2021, 2026 |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package fwserver |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/internal/logging" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/statestore" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/tfsdk" |
| 14 | +) |
| 15 | + |
| 16 | +// ValidateStateStoreConfigRequest is the framework server request for the |
| 17 | +// ValidateStateStoreConfig RPC. |
| 18 | +type ValidateStateStoreConfigRequest struct { |
| 19 | + Config *tfsdk.Config |
| 20 | + StateStore statestore.StateStore |
| 21 | +} |
| 22 | + |
| 23 | +// ValidateStateStoreConfigResponse is the framework server response for the |
| 24 | +// ValidateStateStoreConfig RPC. |
| 25 | +type ValidateStateStoreConfigResponse struct { |
| 26 | + Diagnostics diag.Diagnostics |
| 27 | +} |
| 28 | + |
| 29 | +// ValidateStateStoreConfig implements the framework server ValidateStateStoreConfig RPC. |
| 30 | +func (s *Server) ValidateStateStoreConfig(ctx context.Context, req *ValidateStateStoreConfigRequest, resp *ValidateStateStoreConfigResponse) { |
| 31 | + if req == nil || req.Config == nil { |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + if statestoreWithConfigure, ok := req.StateStore.(statestore.StateStoreWithConfigure); ok { |
| 36 | + logging.FrameworkTrace(ctx, "StateStore implements StateStoreWithConfigure") |
| 37 | + |
| 38 | + configureReq := statestore.ConfigureRequest{ |
| 39 | + StateStoreData: s.StateStoreConfigureData.StateStoreConfigureData, |
| 40 | + } |
| 41 | + configureResp := statestore.ConfigureResponse{} |
| 42 | + |
| 43 | + logging.FrameworkTrace(ctx, "Calling provider defined StateStore Configure") |
| 44 | + statestoreWithConfigure.Configure(ctx, configureReq, &configureResp) |
| 45 | + logging.FrameworkTrace(ctx, "Called provider defined StateStore Configure") |
| 46 | + |
| 47 | + resp.Diagnostics.Append(configureResp.Diagnostics...) |
| 48 | + |
| 49 | + if resp.Diagnostics.HasError() { |
| 50 | + return |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + vdscReq := statestore.ValidateConfigRequest{ |
| 55 | + Config: *req.Config, |
| 56 | + } |
| 57 | + |
| 58 | + if statestoreWithConfigValidators, ok := req.StateStore.(statestore.StateStoreWithConfigValidators); ok { |
| 59 | + logging.FrameworkTrace(ctx, "StateStore implements StateStoreWithConfigValidators") |
| 60 | + |
| 61 | + for _, configValidator := range statestoreWithConfigValidators.ConfigValidators(ctx) { |
| 62 | + // Instantiate a new response for each request to prevent validators |
| 63 | + // from modifying or removing diagnostics. |
| 64 | + vdscResp := &statestore.ValidateConfigResponse{} |
| 65 | + |
| 66 | + logging.FrameworkTrace( |
| 67 | + ctx, |
| 68 | + "Calling provider defined StateStoreConfigValidator", |
| 69 | + map[string]interface{}{ |
| 70 | + logging.KeyDescription: configValidator.Description(ctx), |
| 71 | + }, |
| 72 | + ) |
| 73 | + configValidator.ValidateStateStore(ctx, vdscReq, vdscResp) |
| 74 | + logging.FrameworkTrace( |
| 75 | + ctx, |
| 76 | + "Called provider defined StateStoreConfigValidator", |
| 77 | + map[string]interface{}{ |
| 78 | + logging.KeyDescription: configValidator.Description(ctx), |
| 79 | + }, |
| 80 | + ) |
| 81 | + |
| 82 | + resp.Diagnostics.Append(vdscResp.Diagnostics...) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if statestoreWithValidateConfig, ok := req.StateStore.(statestore.StateStoreWithValidateConfig); ok { |
| 87 | + logging.FrameworkTrace(ctx, "StateStore implements StateStoreWithValidateConfig") |
| 88 | + |
| 89 | + // Instantiate a new response for each request to prevent validators |
| 90 | + // from modifying or removing diagnostics. |
| 91 | + vdscResp := &statestore.ValidateConfigResponse{} |
| 92 | + |
| 93 | + logging.FrameworkTrace(ctx, "Calling provider defined StateStore ValidateConfig") |
| 94 | + statestoreWithValidateConfig.ValidateConfig(ctx, vdscReq, vdscResp) |
| 95 | + logging.FrameworkTrace(ctx, "Called provider defined StateStore ValidateConfig") |
| 96 | + |
| 97 | + resp.Diagnostics.Append(vdscResp.Diagnostics...) |
| 98 | + } |
| 99 | + |
| 100 | + schemaCapabilities := validator.ValidateSchemaClientCapabilities{ |
| 101 | + // The SchemaValidate function is shared between provider, resource, |
| 102 | + // data source, ephemeral resource, and statestore schemas; however, WriteOnlyAttributesAllowed |
| 103 | + // capability is only valid for resource schemas, so this is explicitly set to false |
| 104 | + // for all other schema types. |
| 105 | + WriteOnlyAttributesAllowed: false, |
| 106 | + } |
| 107 | + |
| 108 | + validateSchemaReq := ValidateSchemaRequest{ |
| 109 | + ClientCapabilities: schemaCapabilities, |
| 110 | + Config: *req.Config, |
| 111 | + } |
| 112 | + // Instantiate a new response for each request to prevent validators |
| 113 | + // from modifying or removing diagnostics. |
| 114 | + validateSchemaResp := ValidateSchemaResponse{} |
| 115 | + |
| 116 | + SchemaValidate(ctx, req.Config.Schema, validateSchemaReq, &validateSchemaResp) |
| 117 | + |
| 118 | + resp.Diagnostics.Append(validateSchemaResp.Diagnostics...) |
| 119 | +} |
0 commit comments