|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package fwserver |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/action" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/internal/logging" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/tfsdk" |
| 14 | +) |
| 15 | + |
| 16 | +// ValidateActionConfigRequest is the framework server request for the |
| 17 | +// ValidateActionConfig RPC. |
| 18 | +type ValidateActionConfigRequest struct { |
| 19 | + Config *tfsdk.Config |
| 20 | + Action action.Action |
| 21 | +} |
| 22 | + |
| 23 | +// ValidateActionConfigResponse is the framework server response for the |
| 24 | +// ValidateActionConfig RPC. |
| 25 | +type ValidateActionConfigResponse struct { |
| 26 | + Diagnostics diag.Diagnostics |
| 27 | +} |
| 28 | + |
| 29 | +// ValidateActionConfig implements the framework server ValidateActionConfig RPC. |
| 30 | +func (s *Server) ValidateActionConfig(ctx context.Context, req *ValidateActionConfigRequest, resp *ValidateActionConfigResponse) { |
| 31 | + if req == nil || req.Config == nil { |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + if actionWithConfigure, ok := req.Action.(action.ActionWithConfigure); ok { |
| 36 | + logging.FrameworkTrace(ctx, "Action implements ActionWithConfigure") |
| 37 | + |
| 38 | + configureReq := action.ConfigureRequest{ |
| 39 | + ProviderData: s.ActionConfigureData, |
| 40 | + } |
| 41 | + configureResp := action.ConfigureResponse{} |
| 42 | + |
| 43 | + logging.FrameworkTrace(ctx, "Calling provider defined Action Configure") |
| 44 | + actionWithConfigure.Configure(ctx, configureReq, &configureResp) |
| 45 | + logging.FrameworkTrace(ctx, "Called provider defined Action Configure") |
| 46 | + |
| 47 | + resp.Diagnostics.Append(configureResp.Diagnostics...) |
| 48 | + |
| 49 | + if resp.Diagnostics.HasError() { |
| 50 | + return |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + vdscReq := action.ValidateConfigRequest{ |
| 55 | + Config: *req.Config, |
| 56 | + } |
| 57 | + |
| 58 | + if actionWithConfigValidators, ok := req.Action.(action.ActionWithConfigValidators); ok { |
| 59 | + logging.FrameworkTrace(ctx, "Action implements ActionWithConfigValidators") |
| 60 | + |
| 61 | + for _, configValidator := range actionWithConfigValidators.ConfigValidators(ctx) { |
| 62 | + // Instantiate a new response for each request to prevent validators |
| 63 | + // from modifying or removing diagnostics. |
| 64 | + vdscResp := &action.ValidateConfigResponse{} |
| 65 | + |
| 66 | + logging.FrameworkTrace( |
| 67 | + ctx, |
| 68 | + "Calling provider defined ActionConfigValidator", |
| 69 | + map[string]interface{}{ |
| 70 | + logging.KeyDescription: configValidator.Description(ctx), |
| 71 | + }, |
| 72 | + ) |
| 73 | + configValidator.ValidateAction(ctx, vdscReq, vdscResp) |
| 74 | + logging.FrameworkTrace( |
| 75 | + ctx, |
| 76 | + "Called provider defined ActionConfigValidator", |
| 77 | + map[string]interface{}{ |
| 78 | + logging.KeyDescription: configValidator.Description(ctx), |
| 79 | + }, |
| 80 | + ) |
| 81 | + |
| 82 | + resp.Diagnostics.Append(vdscResp.Diagnostics...) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if actionWithValidateConfig, ok := req.Action.(action.ActionWithValidateConfig); ok { |
| 87 | + logging.FrameworkTrace(ctx, "Action implements ActionWithValidateConfig") |
| 88 | + |
| 89 | + // Instantiate a new response for each request to prevent validators |
| 90 | + // from modifying or removing diagnostics. |
| 91 | + vdscResp := &action.ValidateConfigResponse{} |
| 92 | + |
| 93 | + logging.FrameworkTrace(ctx, "Calling provider defined Action ValidateConfig") |
| 94 | + actionWithValidateConfig.ValidateConfig(ctx, vdscReq, vdscResp) |
| 95 | + logging.FrameworkTrace(ctx, "Called provider defined Action 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 action 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