Skip to content

Commit

Permalink
Shim remaining v6 methods
Browse files Browse the repository at this point in the history
  • Loading branch information
iwahbe committed May 25, 2024
1 parent 447e825 commit 34a2edf
Show file tree
Hide file tree
Showing 2 changed files with 266 additions and 18 deletions.
32 changes: 16 additions & 16 deletions dynamic/internal/shim/protov6/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,33 +129,33 @@ func (p shimProvider) ReadResource(ctx context.Context, req *tfprotov6.ReadResou
// calculate a plan for a resource. Terraform will suggest a proposed
// new state, which the provider can modify or return unmodified to
// influence Terraform's plan.
func (p shimProvider) PlanResourceChange(context.Context, *tfprotov6.PlanResourceChangeRequest) (*tfprotov6.PlanResourceChangeResponse, error) {
panic("UNIMPLIMENTED")
func (p shimProvider) PlanResourceChange(ctx context.Context, req *tfprotov6.PlanResourceChangeRequest) (*tfprotov6.PlanResourceChangeResponse, error) {
return translateGRPC(ctx, p.remote.PlanResourceChange, tfplugin6.PlanResourceChangeRequest(req), tfplugin6.PlanResourceChangeResponse)
}

// ApplyResourceChange is called when Terraform has detected a diff
// between the resource's state and the user's config, and the user has
// approved a planned change. The provider is to apply the changes
// contained in the plan, and return the resulting state.
func (p shimProvider) ApplyResourceChange(context.Context, *tfprotov6.ApplyResourceChangeRequest) (*tfprotov6.ApplyResourceChangeResponse, error) {
panic("UNIMPLIMENTED")
func (p shimProvider) ApplyResourceChange(ctx context.Context, req *tfprotov6.ApplyResourceChangeRequest) (*tfprotov6.ApplyResourceChangeResponse, error) {
return translateGRPC(ctx, p.remote.ApplyResourceChange, tfplugin6.ApplyResourceChangeRequest(req), tfplugin6.ApplyResourceChangeResponse)
}

// ImportResourceState is called when a user has requested Terraform
// import a resource. The provider should fetch the information
// specified by the passed ID and return it as one or more resource
// states for Terraform to assume control of.
func (p shimProvider) ImportResourceState(context.Context, *tfprotov6.ImportResourceStateRequest) (*tfprotov6.ImportResourceStateResponse, error) {
panic("UNIMPLIMENTED")
func (p shimProvider) ImportResourceState(ctx context.Context, req *tfprotov6.ImportResourceStateRequest) (*tfprotov6.ImportResourceStateResponse, error) {
return translateGRPC(ctx, p.remote.ImportResourceState, tfplugin6.ImportResourceStateRequest(req), tfplugin6.ImportResourceStateResponse)
}

// ValidateDataResourceConfig is called when Terraform is checking that a
// data source's configuration is valid. It is guaranteed to have types
// conforming to your schema, but it is not guaranteed that all values
// will be known. This is your opportunity to do custom or advanced
// validation prior to a plan being generated.
func (p shimProvider) ValidateDataResourceConfig(context.Context, *tfprotov6.ValidateDataResourceConfigRequest) (*tfprotov6.ValidateDataResourceConfigResponse, error) {
panic("UNIMPLIMENTED")
func (p shimProvider) ValidateDataResourceConfig(ctx context.Context, req *tfprotov6.ValidateDataResourceConfigRequest) (*tfprotov6.ValidateDataResourceConfigResponse, error) {
return translateGRPC(ctx, p.remote.ValidateDataResourceConfig, tfplugin6.ValidateDataResourceConfigRequest(req), tfplugin6.ValidateDataResourceConfigResponse)
}

// MoveResourceState is called when Terraform is asked to change a resource
Expand All @@ -167,24 +167,24 @@ func (p shimProvider) ValidateDataResourceConfig(context.Context, *tfprotov6.Val
// This functionality is only supported in Terraform 1.8 and later. The
// provider must have enabled the MoveResourceState server capability to
// enable these requests.
func (p shimProvider) MoveResourceState(context.Context, *tfprotov6.MoveResourceStateRequest) (*tfprotov6.MoveResourceStateResponse, error) {
panic("UNIMPLIMENTED")
func (p shimProvider) MoveResourceState(ctx context.Context, req *tfprotov6.MoveResourceStateRequest) (*tfprotov6.MoveResourceStateResponse, error) {
return translateGRPC(ctx, p.remote.MoveResourceState, tfplugin6.MoveResourceStateRequest(req), tfplugin6.MoveResourceStateResponse)
}

// ReadDataSource is called when Terraform is refreshing a data
// source's state.
func (p shimProvider) ReadDataSource(context.Context, *tfprotov6.ReadDataSourceRequest) (*tfprotov6.ReadDataSourceResponse, error) {
panic("UNIMPLIMENTED")
func (p shimProvider) ReadDataSource(ctx context.Context, req *tfprotov6.ReadDataSourceRequest) (*tfprotov6.ReadDataSourceResponse, error) {
return translateGRPC(ctx, p.remote.ReadDataSource, tfplugin6.ReadDataSourceRequest(req), tfplugin6.ReadDataSourceResponse)
}

// CallFunction is called when Terraform wants to execute the logic of a
// function referenced in the configuration.
func (p shimProvider) CallFunction(context.Context, *tfprotov6.CallFunctionRequest) (*tfprotov6.CallFunctionResponse, error) {
panic("UNIMPLIMENTED")
func (p shimProvider) CallFunction(ctx context.Context, req *tfprotov6.CallFunctionRequest) (*tfprotov6.CallFunctionResponse, error) {
return translateGRPC(ctx, p.remote.CallFunction, tfplugin6.CallFunctionRequest(req), tfplugin6.CallFunctionResponse)
}

// GetFunctions is called when Terraform wants to lookup which functions a
// provider supports when not calling GetProviderSchema.
func (p shimProvider) GetFunctions(context.Context, *tfprotov6.GetFunctionsRequest) (*tfprotov6.GetFunctionsResponse, error) {
panic("UNIMPLIMENTED")
func (p shimProvider) GetFunctions(ctx context.Context, req *tfprotov6.GetFunctionsRequest) (*tfprotov6.GetFunctionsResponse, error) {
return translateGRPC(ctx, p.remote.GetFunctions, tfplugin6.GetFunctionsRequest(req), tfplugin6.GetFunctionsResponse)
}
252 changes: 250 additions & 2 deletions dynamic/internal/shim/tfplugin6/tfplugin6.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/opentofu/opentofu/internal/tfplugin6"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
)

func GetMetadataRequest(i *tfprotov6.GetMetadataRequest) *tfplugin6.GetMetadata_Request {
Expand Down Expand Up @@ -77,7 +78,7 @@ func diagnostic(i *tfplugin6.Diagnostic) *tfprotov6.Diagnostic {
Severity: diagnosticSeverity(i.Severity),
Summary: i.Summary,
Detail: i.Detail,
Attribute: attributePath(i.Attribute),
Attribute: attributePathRequest(i.Attribute),
}
}
func diagnosticSeverity(i tfplugin6.Diagnostic_Severity) tfprotov6.DiagnosticSeverity {
Expand All @@ -91,7 +92,7 @@ func diagnosticSeverity(i tfplugin6.Diagnostic_Severity) tfprotov6.DiagnosticSev
}
}

func attributePath(i *tfplugin6.AttributePath) *tftypes.AttributePath {
func attributePathRequest(i *tfplugin6.AttributePath) *tftypes.AttributePath {
if i == nil {
return nil
}
Expand Down Expand Up @@ -446,3 +447,250 @@ func rawStateRequest(i *tfprotov6.RawState) *tfplugin6.RawState {
Flatmap: i.Flatmap,
}
}

func PlanResourceChangeRequest(i *tfprotov6.PlanResourceChangeRequest) *tfplugin6.PlanResourceChange_Request {
if i == nil {
return nil
}

return &tfplugin6.PlanResourceChange_Request{
TypeName: i.TypeName,
PriorState: dynamicValueRequest(i.PriorState),
ProposedNewState: dynamicValueRequest(i.ProposedNewState),
Config: dynamicValueRequest(i.Config),
PriorPrivate: i.PriorPrivate,
ProviderMeta: dynamicValueRequest(i.ProviderMeta),
}
}

func PlanResourceChangeResponse(i *tfplugin6.PlanResourceChange_Response) *tfprotov6.PlanResourceChangeResponse {
if i == nil {
return nil
}

return &tfprotov6.PlanResourceChangeResponse{
PlannedState: dynamicValueResponse(i.PlannedState),
RequiresReplace: attributePathsResponse(i.RequiresReplace),
PlannedPrivate: i.PlannedPrivate,
Diagnostics: diagnostics(i.Diagnostics),
UnsafeToUseLegacyTypeSystem: i.LegacyTypeSystem,
Deferred: nil, // tfplugin does not have a deferred concept
}
}

func attributePathsResponse(i []*tfplugin6.AttributePath) []*tftypes.AttributePath {
return applyArray(i, attributePathResponse)
}

func attributePathResponse(i *tfplugin6.AttributePath) *tftypes.AttributePath {
if i == nil {
return nil
}

steps := make([]tftypes.AttributePathStep, len(i.Steps))
for i, v := range i.Steps {
switch v := v.GetSelector().(type) {
case *tfplugin6.AttributePath_Step_AttributeName:
steps[i] = tftypes.AttributeName(v.AttributeName)
case *tfplugin6.AttributePath_Step_ElementKeyInt:
steps[i] = tftypes.ElementKeyInt(v.ElementKeyInt)
case *tfplugin6.AttributePath_Step_ElementKeyString:
steps[i] = tftypes.ElementKeyString(v.ElementKeyString)
default:
contract.Failf("%d: unknown attribute path of type %T", i, v)
}
}

return tftypes.NewAttributePathWithSteps(steps)
}

func ApplyResourceChangeRequest(i *tfprotov6.ApplyResourceChangeRequest) *tfplugin6.ApplyResourceChange_Request {
if i == nil {
return nil
}

return &tfplugin6.ApplyResourceChange_Request{
TypeName: i.TypeName,
PriorState: dynamicValueRequest(i.PriorState),
PlannedState: dynamicValueRequest(i.PlannedState),
Config: dynamicValueRequest(i.Config),
PlannedPrivate: i.PlannedPrivate,
ProviderMeta: dynamicValueRequest(i.ProviderMeta),
}
}

func ApplyResourceChangeResponse(i *tfplugin6.ApplyResourceChange_Response) *tfprotov6.ApplyResourceChangeResponse {
if i == nil {
return nil
}

return &tfprotov6.ApplyResourceChangeResponse{
NewState: dynamicValueResponse(i.NewState),
Private: i.Private,
Diagnostics: diagnostics(i.Diagnostics),
UnsafeToUseLegacyTypeSystem: i.LegacyTypeSystem,
}
}

func ImportResourceStateRequest(i *tfprotov6.ImportResourceStateRequest) *tfplugin6.ImportResourceState_Request {
if i == nil {
return nil
}

return &tfplugin6.ImportResourceState_Request{
TypeName: i.TypeName,
Id: i.ID,
}
}

func ImportResourceStateResponse(i *tfplugin6.ImportResourceState_Response) *tfprotov6.ImportResourceStateResponse {
if i == nil {
return nil
}

return &tfprotov6.ImportResourceStateResponse{
ImportedResources: importedResources(i.ImportedResources),
Diagnostics: diagnostics(i.Diagnostics),
Deferred: nil, // tfplugin6 does not support Deferred
}
}

func importedResources(i []*tfplugin6.ImportResourceState_ImportedResource) []*tfprotov6.ImportedResource {
return applyArray(i, importedResource)
}

func importedResource(i *tfplugin6.ImportResourceState_ImportedResource) *tfprotov6.ImportedResource {
if i == nil {
return nil
}

return &tfprotov6.ImportedResource{
TypeName: i.TypeName,
State: dynamicValueResponse(i.State),
Private: i.Private,
}
}

func ValidateDataResourceConfigRequest(i *tfprotov6.ValidateDataResourceConfigRequest) *tfplugin6.ValidateDataResourceConfig_Request {
if i == nil {
return nil
}

return &tfplugin6.ValidateDataResourceConfig_Request{
TypeName: i.TypeName,
Config: dynamicValueRequest(i.Config),
}
}

func ValidateDataResourceConfigResponse(i *tfplugin6.ValidateDataResourceConfig_Response) *tfprotov6.ValidateDataResourceConfigResponse {
if i == nil {
return nil
}

return &tfprotov6.ValidateDataResourceConfigResponse{
Diagnostics: diagnostics(i.Diagnostics),
}
}

func MoveResourceStateRequest(i *tfprotov6.MoveResourceStateRequest) *tfplugin6.MoveResourceState_Request {
if i == nil {
return nil
}

return &tfplugin6.MoveResourceState_Request{
SourceProviderAddress: i.SourceProviderAddress,
SourceTypeName: i.SourceTypeName,
SourceSchemaVersion: i.SourceSchemaVersion,
SourceState: rawStateRequest(i.SourceState),
TargetTypeName: i.TargetTypeName,
SourcePrivate: i.SourcePrivate,
}
}

func MoveResourceStateResponse(i *tfplugin6.MoveResourceState_Response) *tfprotov6.MoveResourceStateResponse {
if i == nil {
return nil
}

return &tfprotov6.MoveResourceStateResponse{
TargetPrivate: i.TargetPrivate,
TargetState: dynamicValueResponse(i.TargetState),
Diagnostics: diagnostics(i.Diagnostics),
}
}

func ReadDataSourceRequest(i *tfprotov6.ReadDataSourceRequest) *tfplugin6.ReadDataSource_Request {
if i == nil {
return nil
}

return &tfplugin6.ReadDataSource_Request{
TypeName: i.TypeName,
Config: dynamicValueRequest(i.Config),
ProviderMeta: dynamicValueRequest(i.ProviderMeta),
}
}

func ReadDataSourceResponse(i *tfplugin6.ReadDataSource_Response) *tfprotov6.ReadDataSourceResponse {
if i == nil {
return nil
}

return &tfprotov6.ReadDataSourceResponse{
State: dynamicValueResponse(i.State),
Diagnostics: diagnostics(i.Diagnostics),
Deferred: nil, // tfplugin6 does not support deferred
}
}

func CallFunctionRequest(i *tfprotov6.CallFunctionRequest) *tfplugin6.CallFunction_Request {
if i == nil {
return nil
}

return &tfplugin6.CallFunction_Request{
Name: i.Name,
Arguments: applyArray(i.Arguments, dynamicValueRequest),
}
}

func CallFunctionResponse(i *tfplugin6.CallFunction_Response) *tfprotov6.CallFunctionResponse {
if i == nil {
return nil
}

return &tfprotov6.CallFunctionResponse{
Error: functionError(i.Error),
Result: dynamicValueResponse(i.Result),
}
}

func functionError(i *tfplugin6.FunctionError) *tfprotov6.FunctionError {
if i == nil {
return nil
}

return &tfprotov6.FunctionError{
Text: i.Text,
FunctionArgument: i.FunctionArgument,
}
}

func GetFunctionsRequest(i *tfprotov6.GetFunctionsRequest) *tfplugin6.GetFunctions_Request {
if i == nil {
return nil
}

return &tfplugin6.GetFunctions_Request{}
}

func GetFunctionsResponse(i *tfplugin6.GetFunctions_Response) *tfprotov6.GetFunctionsResponse {
if i == nil {
return nil
}

return &tfprotov6.GetFunctionsResponse{
Diagnostics: diagnostics(i.Diagnostics),
Functions: applyMap(i.Functions, function),
}
}

0 comments on commit 34a2edf

Please sign in to comment.