Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tfprovider.Provider adds a new method ImportManagedResourceState #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tfprovider/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type ManagedResourceType = common.ManagedResourceType

type DataResourceType = common.DataResourceType

type ImportedResource = common.ImportedResource

type ManagedResourceReadRequest = common.ManagedResourceReadRequest

type ManagedResourceReadResponse = common.ManagedResourceReadResponse
18 changes: 18 additions & 0 deletions tfprovider/internal/common/resource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package common

import (
"context"

"github.com/zclconf/go-cty/cty"
)

// ManagedResourceType represents a managed resource type belonging to a
Expand Down Expand Up @@ -36,3 +38,19 @@ type DataResourceType interface {
// plugin protocol features.
Sealed() Sealed
}

// ImportedResource represents an object being imported.
type ImportedResource struct {
// TypeName is the name of the resource type associated with the
// returned state.
TypeName string

// State is the state of the remote object being imported. This may not be
// complete, but must contain enough information to uniquely identify the
// resource.
State cty.Value

// Private is an opaque blob that will be stored in state along with the
// resource. It is intended only for interpretation by the provider itself.
OpaquePrivate []byte
}
47 changes: 43 additions & 4 deletions tfprovider/internal/protocol5/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package protocol5

import (
"context"
"fmt"
"sync"

"github.com/apparentlymart/terraform-provider/internal/tfplugin5"
Expand Down Expand Up @@ -33,10 +34,11 @@ func NewProvider(ctx context.Context, plugin *rpcplugin.Plugin, clientProxy inte
}

return &Provider{
client: client,
plugin: plugin,
schema: schema,
configured: false,
client: client,
plugin: plugin,
schema: schema,
configured: false,
configuredMu: &sync.Mutex{},
}, nil
}

Expand Down Expand Up @@ -150,6 +152,43 @@ func (p *Provider) ManagedResourceType(typeName string) common.ManagedResourceTy
}
}

func (p *Provider) ImportManagedResourceState(ctx context.Context, typeName string, id string) ([]common.ImportedResource, common.Diagnostics) {
p.configuredMu.Lock()
if !p.configured {
return nil, nil
}
p.configuredMu.Unlock()

var diags common.Diagnostics
resp, err := p.client.ImportResourceState(ctx, &tfplugin5.ImportResourceState_Request{
TypeName: typeName,
Id: id,
})
diags = append(diags, common.RPCErrorDiagnostics(err)...)
if err != nil {
return nil, diags
}
diags = append(diags, decodeDiagnostics(resp.Diagnostics)...)

var resources []common.ImportedResource
for _, raw := range resp.ImportedResources {
resource := common.ImportedResource{
TypeName: raw.TypeName,
OpaquePrivate: raw.Private,
}
schema, ok := p.schema.ManagedResourceTypes[raw.TypeName]
if !ok {
diags = append(diags, common.RPCErrorDiagnostics(fmt.Errorf("unknown resource type %q", raw.TypeName))...)
continue
}
state, moreDiags := decodeDynamicValue(raw.State, schema.Content)
resource.State = state
diags = append(diags, moreDiags...)
resources = append(resources, resource)
}
return resources, diags
}

func (p *Provider) Close() error {
return p.plugin.Close()
}
Expand Down
2 changes: 1 addition & 1 deletion tfprovider/internal/protocol5/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func decodeDynamicValue(raw *tfplugin5.DynamicValue, schema *tfschema.Block) (ct
}
return val, nil
case len(raw.Msgpack) > 0:
val, err := ctymsgpack.Unmarshal(raw.Json, ty)
val, err := ctymsgpack.Unmarshal(raw.Msgpack, ty)
if err != nil {
return cty.DynamicVal, common.ErrorDiagnostics(
"Provider returned invalid object",
Expand Down
41 changes: 37 additions & 4 deletions tfprovider/internal/protocol6/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package protocol6

import (
"context"
"fmt"
"sync"

"github.com/apparentlymart/terraform-provider/internal/tfplugin6"
Expand Down Expand Up @@ -33,10 +34,11 @@ func NewProvider(ctx context.Context, plugin *rpcplugin.Plugin, clientProxy inte
}

return &Provider{
client: client,
plugin: plugin,
schema: schema,
configured: false,
client: client,
plugin: plugin,
schema: schema,
configured: false,
configuredMu: &sync.Mutex{},
}, nil
}

Expand Down Expand Up @@ -141,6 +143,37 @@ func (p *Provider) ManagedResourceType(typeName string) common.ManagedResourceTy
}
}

func (p *Provider) ImportManagedResourceState(ctx context.Context, typeName string, id string) ([]common.ImportedResource, common.Diagnostics) {
var diags common.Diagnostics
resp, err := p.client.ImportResourceState(ctx, &tfplugin6.ImportResourceState_Request{
TypeName: typeName,
Id: id,
})
diags = append(diags, common.RPCErrorDiagnostics(err)...)
if err != nil {
return nil, diags
}
diags = append(diags, decodeDiagnostics(resp.Diagnostics)...)

var resources []common.ImportedResource
for _, raw := range resp.ImportedResources {
resource := common.ImportedResource{
TypeName: raw.TypeName,
OpaquePrivate: raw.Private,
}
schema, ok := p.schema.ManagedResourceTypes[raw.TypeName]
if !ok {
diags = append(diags, common.RPCErrorDiagnostics(fmt.Errorf("unknown resource type %q", raw.TypeName))...)
continue
}
state, moreDiags := decodeDynamicValue(raw.State, schema.Content)
resource.State = state
diags = append(diags, moreDiags...)
resources = append(resources, resource)
}
return resources, diags
}

func (p *Provider) Close() error {
return p.plugin.Close()
}
Expand Down
2 changes: 1 addition & 1 deletion tfprovider/internal/protocol6/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func decodeDynamicValue(raw *tfplugin6.DynamicValue, schema *tfschema.Block) (ct
}
return val, nil
case len(raw.Msgpack) > 0:
val, err := ctymsgpack.Unmarshal(raw.Json, ty)
val, err := ctymsgpack.Unmarshal(raw.Msgpack, ty)
if err != nil {
return cty.DynamicVal, common.ErrorDiagnostics(
"Provider returned invalid object",
Expand Down
5 changes: 5 additions & 0 deletions tfprovider/tfprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ type Provider interface {
// method. An unconfigured provider always returns nil.
ManagedResourceType(name string) ManagedResourceType

// ImportManagedResourceState requests that the given resource be imported.
// It's possible for providers to import multiple related types with a single
// import request.
ImportManagedResourceState(ctx context.Context, typeName string, id string) ([]ImportedResource, Diagnostics)

// Close kills the child process for this provider plugin, rendering the
// reciever unusable. Any further calls on the object after Close returns
// cause undefined behavior.
Expand Down