|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package fromproto5 |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-go/tfprotov5" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/action" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/internal/fwschema" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/internal/fwserver" |
| 15 | +) |
| 16 | + |
| 17 | +// PlanActionRequest returns the *fwserver.PlanActionRequest equivalent of a *tfprotov5.PlanActionRequest. |
| 18 | +func PlanActionRequest(ctx context.Context, proto5 *tfprotov5.PlanActionRequest, reqAction action.Action, actionSchema fwschema.Schema) (*fwserver.PlanActionRequest, diag.Diagnostics) { |
| 19 | + if proto5 == nil { |
| 20 | + return nil, nil |
| 21 | + } |
| 22 | + |
| 23 | + var diags diag.Diagnostics |
| 24 | + |
| 25 | + // Panic prevention here to simplify the calling implementations. |
| 26 | + // This should not happen, but just in case. |
| 27 | + if actionSchema == nil { |
| 28 | + diags.AddError( |
| 29 | + "Missing Action Schema", |
| 30 | + "An unexpected error was encountered when handling the request. "+ |
| 31 | + "This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.\n\n"+ |
| 32 | + "Please report this to the provider developer:\n\n"+ |
| 33 | + "Missing schema.", |
| 34 | + ) |
| 35 | + |
| 36 | + return nil, diags |
| 37 | + } |
| 38 | + |
| 39 | + fw := &fwserver.PlanActionRequest{ |
| 40 | + ActionSchema: actionSchema, |
| 41 | + } |
| 42 | + |
| 43 | + config, configDiags := Config(ctx, proto5.Config, actionSchema) |
| 44 | + |
| 45 | + diags.Append(configDiags...) |
| 46 | + |
| 47 | + fw.Config = config |
| 48 | + |
| 49 | + // TODO:Actions: Here we need to retrieve client capabilities and linked resource data |
| 50 | + |
| 51 | + return fw, diags |
| 52 | +} |
0 commit comments