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

Add logging using terraform-plugin-log. #837

Merged
merged 2 commits into from Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Expand Up @@ -13,7 +13,7 @@ require (
github.com/golang/mock v1.4.3
github.com/google/go-cmp v0.5.6
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/go-hclog v0.16.1
github.com/hashicorp/go-hclog v1.0.0
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/go-plugin v1.4.1
github.com/hashicorp/go-uuid v1.0.2
Expand All @@ -24,7 +24,7 @@ require (
github.com/hashicorp/terraform-exec v0.15.0
github.com/hashicorp/terraform-json v0.13.0
github.com/hashicorp/terraform-plugin-go v0.5.0
github.com/hashicorp/terraform-plugin-log v0.2.0
github.com/hashicorp/terraform-plugin-log v0.2.1
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mitchellh/copystructure v1.2.0
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Expand Up @@ -167,8 +167,9 @@ github.com/hashicorp/go-getter v1.5.3 h1:NF5+zOlQegim+w/EUhSLh6QhXHmZMEeHLQzllkQ
github.com/hashicorp/go-getter v1.5.3/go.mod h1:BrrV/1clo8cCYu6mxvboYg+KutTiFnXjMEgDD8+i7ZI=
github.com/hashicorp/go-hclog v0.0.0-20180709165350-ff2cf002a8dd/go.mod h1:9bjs9uLqI8l75knNv3lV1kA55veR+WUPSiKIWcQHudI=
github.com/hashicorp/go-hclog v0.14.1/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
github.com/hashicorp/go-hclog v0.16.1 h1:IVQwpTGNRRIHafnTs2dQLIk4ENtneRIEEJWOVDqz99o=
github.com/hashicorp/go-hclog v0.16.1/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
github.com/hashicorp/go-hclog v1.0.0 h1:bkKf0BeBXcSYa7f5Fyi9gMuQ8gNsxeiNpZjR6VxNZeo=
github.com/hashicorp/go-hclog v1.0.0/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
Expand Down Expand Up @@ -198,8 +199,9 @@ github.com/hashicorp/terraform-json v0.13.0 h1:Li9L+lKD1FO5RVFRM1mMMIBDoUHslOniy
github.com/hashicorp/terraform-json v0.13.0/go.mod h1:y5OdLBCT+rxbwnpxZs9kGL7R9ExU76+cpdY8zHwoazk=
github.com/hashicorp/terraform-plugin-go v0.5.0 h1:+gCDdF0hcYCm0YBTxrP4+K1NGIS5ZKZBKDORBewLJmg=
github.com/hashicorp/terraform-plugin-go v0.5.0/go.mod h1:PAVN26PNGpkkmsvva1qfriae5Arky3xl3NfzKa8XFVM=
github.com/hashicorp/terraform-plugin-log v0.2.0 h1:rjflRuBqCnSk3UHOR25MP1G5BDLKktTA6lNjjcAnBfI=
github.com/hashicorp/terraform-plugin-log v0.2.0/go.mod h1:E1kJmapEHzqu1x6M++gjvhzM2yMQNXPVWZRCB8sgYjg=
github.com/hashicorp/terraform-plugin-log v0.2.1 h1:hl0G6ctSx7DRTE62VNsPWrq7d+JWy1kjk9ApOFrCq3I=
github.com/hashicorp/terraform-plugin-log v0.2.1/go.mod h1:RW/n0x4dyITmenuirZ1ViPQGP5JQdPTZ4Wwc0rLKi94=
github.com/hashicorp/terraform-registry-address v0.0.0-20210412075316-9b2996cce896 h1:1FGtlkJw87UsTMg5s8jrekrHmUPUJaMcu6ELiVhQrNw=
github.com/hashicorp/terraform-registry-address v0.0.0-20210412075316-9b2996cce896/go.mod h1:bzBPnUIkI0RxauU8Dqo+2KrZZ28Cf48s8V6IHt3p4co=
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 h1:HKLsbzeOsfXmKNpr3GiT18XAblV0BjCbzL8KQAMZGa0=
Expand Down
54 changes: 38 additions & 16 deletions helper/schema/grpc_provider.go
Expand Up @@ -4,13 +4,14 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"strconv"
"sync"

"github.com/hashicorp/go-cty/cty"
ctyconvert "github.com/hashicorp/go-cty/cty/convert"
"github.com/hashicorp/go-cty/cty/msgpack"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-log/tfsdklog"

"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tftypes"
Expand All @@ -21,7 +22,15 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

const newExtraKey = "_new_extra_shim"
const (
newExtraKey = "_new_extra_shim"
tflogSubsystemName = "sdkv2"
bflad marked this conversation as resolved.
Show resolved Hide resolved
)

func withLogger(ctx context.Context) context.Context {
return tfsdklog.NewSubsystem(ctx, tflogSubsystemName,
tfsdklog.WithLevelFromEnv("TF_LOG_SDK_V2"))
bflad marked this conversation as resolved.
Show resolved Hide resolved
}

func NewGRPCProviderServer(p *Provider) *GRPCProviderServer {
return &GRPCProviderServer{
Expand Down Expand Up @@ -53,6 +62,7 @@ func mergeStop(ctx context.Context, cancel context.CancelFunc, stopCh chan struc
// It creates a goroutine to wait for the server stop and propagates
// cancellation to the derived grpc context.
func (s *GRPCProviderServer) StopContext(ctx context.Context) context.Context {
ctx = withLogger(ctx)
s.stopMu.Lock()
defer s.stopMu.Unlock()

Expand All @@ -61,7 +71,8 @@ func (s *GRPCProviderServer) StopContext(ctx context.Context) context.Context {
return stoppable
}

func (s *GRPCProviderServer) GetProviderSchema(_ context.Context, req *tfprotov5.GetProviderSchemaRequest) (*tfprotov5.GetProviderSchemaResponse, error) {
func (s *GRPCProviderServer) GetProviderSchema(ctx context.Context, req *tfprotov5.GetProviderSchemaRequest) (*tfprotov5.GetProviderSchemaResponse, error) {
ctx = withLogger(ctx)

resp := &tfprotov5.GetProviderSchemaResponse{
ResourceSchemas: make(map[string]*tfprotov5.Schema),
Expand Down Expand Up @@ -111,7 +122,8 @@ func (s *GRPCProviderServer) getDatasourceSchemaBlock(name string) *configschema
return dat.CoreConfigSchema()
}

func (s *GRPCProviderServer) PrepareProviderConfig(_ context.Context, req *tfprotov5.PrepareProviderConfigRequest) (*tfprotov5.PrepareProviderConfigResponse, error) {
func (s *GRPCProviderServer) PrepareProviderConfig(ctx context.Context, req *tfprotov5.PrepareProviderConfigRequest) (*tfprotov5.PrepareProviderConfigResponse, error) {
ctx = withLogger(ctx)
resp := &tfprotov5.PrepareProviderConfigResponse{}

schemaBlock := s.getProviderSchemaBlock()
Expand Down Expand Up @@ -214,7 +226,8 @@ func (s *GRPCProviderServer) PrepareProviderConfig(_ context.Context, req *tfpro
return resp, nil
}

func (s *GRPCProviderServer) ValidateResourceTypeConfig(_ context.Context, req *tfprotov5.ValidateResourceTypeConfigRequest) (*tfprotov5.ValidateResourceTypeConfigResponse, error) {
func (s *GRPCProviderServer) ValidateResourceTypeConfig(ctx context.Context, req *tfprotov5.ValidateResourceTypeConfigRequest) (*tfprotov5.ValidateResourceTypeConfigResponse, error) {
ctx = withLogger(ctx)
resp := &tfprotov5.ValidateResourceTypeConfigResponse{}

schemaBlock := s.getResourceSchemaBlock(req.TypeName)
Expand All @@ -232,7 +245,8 @@ func (s *GRPCProviderServer) ValidateResourceTypeConfig(_ context.Context, req *
return resp, nil
}

func (s *GRPCProviderServer) ValidateDataSourceConfig(_ context.Context, req *tfprotov5.ValidateDataSourceConfigRequest) (*tfprotov5.ValidateDataSourceConfigResponse, error) {
func (s *GRPCProviderServer) ValidateDataSourceConfig(ctx context.Context, req *tfprotov5.ValidateDataSourceConfigRequest) (*tfprotov5.ValidateDataSourceConfigResponse, error) {
ctx = withLogger(ctx)
resp := &tfprotov5.ValidateDataSourceConfigResponse{}

schemaBlock := s.getDatasourceSchemaBlock(req.TypeName)
Expand All @@ -257,6 +271,7 @@ func (s *GRPCProviderServer) ValidateDataSourceConfig(_ context.Context, req *tf
}

func (s *GRPCProviderServer) UpgradeResourceState(ctx context.Context, req *tfprotov5.UpgradeResourceStateRequest) (*tfprotov5.UpgradeResourceStateResponse, error) {
ctx = withLogger(ctx)
resp := &tfprotov5.UpgradeResourceStateResponse{}

res, ok := s.provider.ResourcesMap[req.TypeName]
Expand Down Expand Up @@ -292,7 +307,7 @@ func (s *GRPCProviderServer) UpgradeResourceState(ctx context.Context, req *tfpr
return resp, nil
}
default:
log.Println("[DEBUG] no state provided to upgrade")
tfsdklog.SubsystemDebug(ctx, tflogSubsystemName, "no state provided to upgrade")
return resp, nil
}

Expand All @@ -304,7 +319,7 @@ func (s *GRPCProviderServer) UpgradeResourceState(ctx context.Context, req *tfpr
}

// The provider isn't required to clean out removed fields
s.removeAttributes(jsonMap, schemaBlock.ImpliedType())
s.removeAttributes(ctx, jsonMap, schemaBlock.ImpliedType())

// now we need to turn the state into the default json representation, so
// that it can be re-decoded using the actual schema.
Expand Down Expand Up @@ -433,7 +448,7 @@ func (s *GRPCProviderServer) upgradeJSONState(ctx context.Context, version int,

// Remove any attributes no longer present in the schema, so that the json can
// be correctly decoded.
func (s *GRPCProviderServer) removeAttributes(v interface{}, ty cty.Type) {
func (s *GRPCProviderServer) removeAttributes(ctx context.Context, v interface{}, ty cty.Type) {
// we're only concerned with finding maps that corespond to object
// attributes
switch v := v.(type) {
Expand All @@ -442,7 +457,7 @@ func (s *GRPCProviderServer) removeAttributes(v interface{}, ty cty.Type) {
if ty.IsListType() || ty.IsSetType() {
eTy := ty.ElementType()
for _, eV := range v {
s.removeAttributes(eV, eTy)
s.removeAttributes(ctx, eV, eTy)
}
}
return
Expand All @@ -451,38 +466,39 @@ func (s *GRPCProviderServer) removeAttributes(v interface{}, ty cty.Type) {
if ty.IsMapType() {
eTy := ty.ElementType()
for _, eV := range v {
s.removeAttributes(eV, eTy)
s.removeAttributes(ctx, eV, eTy)
}
return
}

if ty == cty.DynamicPseudoType {
log.Printf("[DEBUG] ignoring dynamic block: %#v\n", v)
tflog.SubsystemDebug(ctx, tflogSubsystemName, "ignoring dynamic block", "block", v)
return
}

if !ty.IsObjectType() {
// This shouldn't happen, and will fail to decode further on, so
// there's no need to handle it here.
log.Printf("[WARN] unexpected type %#v for map in json state", ty)
tflog.SubsystemWarn(ctx, tflogSubsystemName, "unexpected type for map in JSON state", "type", ty)
return
}

attrTypes := ty.AttributeTypes()
for attr, attrV := range v {
attrTy, ok := attrTypes[attr]
if !ok {
log.Printf("[DEBUG] attribute %q no longer present in schema", attr)
tflog.SubsystemDebug(ctx, tflogSubsystemName, "attribute no longer present in schema", "attribute", attr)
delete(v, attr)
continue
}

s.removeAttributes(attrV, attrTy)
s.removeAttributes(ctx, attrV, attrTy)
}
}
}

func (s *GRPCProviderServer) StopProvider(_ context.Context, _ *tfprotov5.StopProviderRequest) (*tfprotov5.StopProviderResponse, error) {
func (s *GRPCProviderServer) StopProvider(ctx context.Context, _ *tfprotov5.StopProviderRequest) (*tfprotov5.StopProviderResponse, error) {
ctx = withLogger(ctx)
s.stopMu.Lock()
defer s.stopMu.Unlock()

Expand All @@ -495,6 +511,7 @@ func (s *GRPCProviderServer) StopProvider(_ context.Context, _ *tfprotov5.StopPr
}

func (s *GRPCProviderServer) ConfigureProvider(ctx context.Context, req *tfprotov5.ConfigureProviderRequest) (*tfprotov5.ConfigureProviderResponse, error) {
ctx = withLogger(ctx)
resp := &tfprotov5.ConfigureProviderResponse{}

schemaBlock := s.getProviderSchemaBlock()
Expand Down Expand Up @@ -527,6 +544,7 @@ func (s *GRPCProviderServer) ConfigureProvider(ctx context.Context, req *tfproto
}

func (s *GRPCProviderServer) ReadResource(ctx context.Context, req *tfprotov5.ReadResourceRequest) (*tfprotov5.ReadResourceResponse, error) {
ctx = withLogger(ctx)
resp := &tfprotov5.ReadResourceResponse{
// helper/schema did previously handle private data during refresh, but
// core is now going to expect this to be maintained in order to
Expand Down Expand Up @@ -619,6 +637,7 @@ func (s *GRPCProviderServer) ReadResource(ctx context.Context, req *tfprotov5.Re
}

func (s *GRPCProviderServer) PlanResourceChange(ctx context.Context, req *tfprotov5.PlanResourceChangeRequest) (*tfprotov5.PlanResourceChangeResponse, error) {
ctx = withLogger(ctx)
resp := &tfprotov5.PlanResourceChangeResponse{}

// This is a signal to Terraform Core that we're doing the best we can to
Expand Down Expand Up @@ -855,6 +874,7 @@ func (s *GRPCProviderServer) PlanResourceChange(ctx context.Context, req *tfprot
}

func (s *GRPCProviderServer) ApplyResourceChange(ctx context.Context, req *tfprotov5.ApplyResourceChangeRequest) (*tfprotov5.ApplyResourceChangeResponse, error) {
ctx = withLogger(ctx)
resp := &tfprotov5.ApplyResourceChangeResponse{
// Start with the existing state as a fallback
NewState: req.PriorState,
Expand Down Expand Up @@ -1034,6 +1054,7 @@ func (s *GRPCProviderServer) ApplyResourceChange(ctx context.Context, req *tfpro
}

func (s *GRPCProviderServer) ImportResourceState(ctx context.Context, req *tfprotov5.ImportResourceStateRequest) (*tfprotov5.ImportResourceStateResponse, error) {
ctx = withLogger(ctx)
resp := &tfprotov5.ImportResourceStateResponse{}

info := &terraform.InstanceInfo{
Expand Down Expand Up @@ -1092,6 +1113,7 @@ func (s *GRPCProviderServer) ImportResourceState(ctx context.Context, req *tfpro
}

func (s *GRPCProviderServer) ReadDataSource(ctx context.Context, req *tfprotov5.ReadDataSourceRequest) (*tfprotov5.ReadDataSourceResponse, error) {
ctx = withLogger(ctx)
resp := &tfprotov5.ReadDataSourceResponse{}

schemaBlock := s.getDatasourceSchemaBlock(req.TypeName)
Expand Down
12 changes: 6 additions & 6 deletions helper/schema/grpc_provider_test.go
Expand Up @@ -83,7 +83,7 @@ func TestUpgradeState_jsonState(t *testing.T) {
},
}

resp, err := server.UpgradeResourceState(nil, req)
resp, err := server.UpgradeResourceState(context.Background(), req)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -136,7 +136,7 @@ func TestUpgradeState_jsonStateBigInt(t *testing.T) {
},
}

resp, err := server.UpgradeResourceState(nil, req)
resp, err := server.UpgradeResourceState(context.Background(), req)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -276,7 +276,7 @@ func TestUpgradeState_removedAttr(t *testing.T) {
JSON: []byte(tc.raw),
},
}
resp, err := server.UpgradeResourceState(nil, req)
resp, err := server.UpgradeResourceState(context.Background(), req)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -463,7 +463,7 @@ func TestUpgradeState_flatmapState(t *testing.T) {

for i, req := range testReqs {
t.Run(fmt.Sprintf("%d-%d", i, req.Version), func(t *testing.T) {
resp, err := server.UpgradeResourceState(nil, req)
resp, err := server.UpgradeResourceState(context.Background(), req)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -542,7 +542,7 @@ func TestUpgradeState_flatmapStateMissingMigrateState(t *testing.T) {

for i, req := range testReqs {
t.Run(fmt.Sprintf("%d-%d", i, req.Version), func(t *testing.T) {
resp, err := server.UpgradeResourceState(nil, req)
resp, err := server.UpgradeResourceState(context.Background(), req)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1542,7 +1542,7 @@ func TestPrepareProviderConfig(t *testing.T) {
},
}

resp, err := server.PrepareProviderConfig(nil, testReq)
resp, err := server.PrepareProviderConfig(context.Background(), testReq)
if err != nil {
t.Fatal(err)
}
Expand Down