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

Restore use of tflog #344

Merged
merged 6 commits into from
Jan 26, 2022
Merged
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
4 changes: 4 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ updates:
ignore:
# terraform-plugin-go should only be updated via terraform-plugin-framework
- dependency-name: "github.com/hashicorp/terraform-plugin-go"
# terraform-plugin-log should only be updated via terraform-plugin-framework
- dependency-name: "github.com/hashicorp/terraform-plugin-log"
# go-hclog should only be updated via terraform-plugin-log
- dependency-name: "github.com/hashicorp/go-hclog"
schedule:
# Check for updates to Go modules every weekday
interval: "daily"
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ require (
github.com/google/go-cmp v0.5.7
github.com/hashicorp/aws-cloudformation-resource-schema-sdk-go v0.15.0
github.com/hashicorp/aws-sdk-go-base/v2 v2.0.0-beta.3
github.com/hashicorp/go-hclog v1.0.0
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/hcl/v2 v2.11.1
github.com/hashicorp/terraform-plugin-framework v0.5.1-0.20220105213120-5f18d804a22b
github.com/hashicorp/terraform-plugin-go v0.5.0
github.com/hashicorp/terraform-plugin-log v0.2.1
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
github.com/jinzhu/inflection v1.0.0
github.com/mattbaird/jsonpatch v0.0.0-20200820163806-098863c1fc24
Expand Down
10 changes: 8 additions & 2 deletions internal/acctest/testchecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"time"

"github.com/aws/aws-sdk-go-v2/service/cloudcontrol"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-log/tfsdklog"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
tfcloudcontrol "github.com/hashicorp/terraform-provider-awscc/internal/service/cloudcontrol"
Expand Down Expand Up @@ -60,7 +62,7 @@ func (td TestData) DeleteResource() resource.TestCheckFunc {
return fmt.Errorf("unable to convert %T to CloudControlApiProvider", td.provider)
}

ctx := context.TODO()
ctx := getTestContext()

return tfcloudcontrol.DeleteResource(ctx, provider.CloudControlApiClient(ctx), provider.RoleARN(ctx), td.CloudFormationResourceType, id, deleteResourceTimeout)
}
Expand All @@ -73,7 +75,7 @@ func (td TestData) checkExists(shouldExist bool) resource.TestCheckFunc {
return fmt.Errorf("unable to convert %T to CloudControlApiProvider", td.provider)
}

ctx := context.TODO()
ctx := getTestContext()

return existsFunc(shouldExist)(
ctx,
Expand Down Expand Up @@ -124,3 +126,7 @@ func existsFunc(shouldExist bool) func(context.Context, *cloudcontrol.Client, st
}
}
}

func getTestContext() context.Context {
return tfsdklog.NewRootProviderLogger(context.TODO(), tflog.WithLevelFromEnv("TF_LOG"), tflog.WithoutLocation())
}
20 changes: 20 additions & 0 deletions internal/generic/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package generic

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-log/tflog"
)

const (
LoggingKeyCFNType = "cfn_type"
)

func traceEntry(ctx context.Context, n string) {
tflog.Trace(ctx, fmt.Sprintf("%s entry", n))
}

func traceExit(ctx context.Context, n string) {
tflog.Trace(ctx, fmt.Sprintf("%s exit", n))
}
19 changes: 13 additions & 6 deletions internal/generic/plural_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package generic
import (
"context"
"fmt"
"log"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudcontrol"
cctypes "github.com/aws/aws-sdk-go-v2/service/cloudcontrol/types"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-plugin-log/tflog"
tfcloudcontrol "github.com/hashicorp/terraform-provider-awscc/internal/service/cloudcontrol"
)

Expand Down Expand Up @@ -65,10 +66,9 @@ func newGenericPluralDataSource(provider tfsdk.Provider, pluralDataSourceType *p
}

func (pd *pluralDataSource) Read(ctx context.Context, _ tfsdk.ReadDataSourceRequest, response *tfsdk.ReadDataSourceResponse) {
cfTypeName := pd.dataSourceType.cfTypeName
tfTypeName := pd.dataSourceType.tfTypeName
ctx = pd.cfnTypeContext(ctx)

log.Printf("[TRACE] DataSource.Read enter. cfTypeName: %s, tfTypeName: %s", cfTypeName, tfTypeName)
traceEntry(ctx, "PluralDataSource.Read")

conn := pd.provider.CloudControlApiClient(ctx)

Expand All @@ -87,16 +87,23 @@ func (pd *pluralDataSource) Read(ctx context.Context, _ tfsdk.ReadDataSourceRequ
Raw: val,
}

log.Printf("[DEBUG] Response.State.Raw. value: %v", response.State.Raw)
tflog.Debug(ctx, "Response.State.Raw", "value", hclog.Fmt("%v", response.State.Raw))

log.Printf("[TRACE] DataSource.Read exit. cfTypeName: %s, tfTypeName: %s", cfTypeName, tfTypeName)
traceExit(ctx, "PluralDataSource.Read")
}

// list returns the ResourceDescriptions of the specified CloudFormation type.
func (pd *pluralDataSource) list(ctx context.Context, conn *cloudcontrol.Client) ([]cctypes.ResourceDescription, error) {
return tfcloudcontrol.ListResourcesByTypeName(ctx, conn, pd.provider.RoleARN(ctx), pd.dataSourceType.cfTypeName)
}

// cfnTypeContext injects the CloudFormation type name into logger contexts.
func (pd *pluralDataSource) cfnTypeContext(ctx context.Context) context.Context {
ctx = tflog.With(ctx, LoggingKeyCFNType, pd.dataSourceType.cfTypeName)

return ctx
}

// GetCloudControlResourceDescriptionsValue returns the Terraform Value for the specified Cloud Control API ResourceDescriptions.
func GetCloudControlResourceDescriptionsValue(id string, descriptions []cctypes.ResourceDescription) tftypes.Value {
m := map[string]tftypes.Value{
Expand Down
65 changes: 34 additions & 31 deletions internal/generic/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"strings"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudcontrol"
cctypes "github.com/aws/aws-sdk-go-v2/service/cloudcontrol/types"
hclog "github.com/hashicorp/go-hclog"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-plugin-log/tflog"
tfcloudcontrol "github.com/hashicorp/terraform-provider-awscc/internal/service/cloudcontrol"
"github.com/hashicorp/terraform-provider-awscc/internal/tfresource"
"github.com/hashicorp/terraform-provider-awscc/internal/validate"
Expand Down Expand Up @@ -394,14 +395,13 @@ var (
)

func (r *resource) Create(ctx context.Context, request tfsdk.CreateResourceRequest, response *tfsdk.CreateResourceResponse) {
cfTypeName := r.resourceType.cfTypeName
tfTypeName := r.resourceType.tfTypeName
ctx = r.cfnTypeContext(ctx)

log.Printf("[TRACE] Resource.Create enter. cfTypeName: %s, tfTypeName: %s", cfTypeName, tfTypeName)
traceEntry(ctx, "Resource.Create")

conn := r.provider.CloudControlApiClient(ctx)

log.Printf("[DEBUG] Request.Plan.Raw. value: %v", request.Plan.Raw)
tflog.Debug(ctx, "Request.Plan.Raw", "value", hclog.Fmt("%v", request.Plan.Raw))

translator := toCloudControl{tfToCfNameMap: r.resourceType.tfToCfNameMap}
desiredState, err := translator.AsString(ctx, request.Plan.Raw)
Expand All @@ -412,12 +412,12 @@ func (r *resource) Create(ctx context.Context, request tfsdk.CreateResourceReque
return
}

log.Printf("[DEBUG] CloudControl DesiredState. value: %s", desiredState)
tflog.Debug(ctx, "CloudControl DesiredState", "value", desiredState)

input := &cloudcontrol.CreateResourceInput{
ClientToken: aws.String(tfresource.UniqueId()),
DesiredState: aws.String(desiredState),
TypeName: aws.String(cfTypeName),
TypeName: aws.String(r.resourceType.cfTypeName),
}

if roleARN := r.provider.RoleARN(ctx); roleARN != "" {
Expand Down Expand Up @@ -484,18 +484,17 @@ func (r *resource) Create(ctx context.Context, request tfsdk.CreateResourceReque
return
}

log.Printf("[DEBUG] Response.State.Raw. value: %v", response.State.Raw)
tflog.Debug(ctx, "Response.State.Raw", "value", hclog.Fmt("%v", response.State.Raw))

log.Printf("[TRACE] Resource.Create exit. cfTypeName: %s, tfTypeName: %s", cfTypeName, tfTypeName)
traceExit(ctx, "Resource.Create")
}

func (r *resource) Read(ctx context.Context, request tfsdk.ReadResourceRequest, response *tfsdk.ReadResourceResponse) {
cfTypeName := r.resourceType.cfTypeName
tfTypeName := r.resourceType.tfTypeName
ctx = r.cfnTypeContext(ctx)

log.Printf("[TRACE] Resource.Read enter. cfTypeName: %s, tfTypeName: %s", cfTypeName, tfTypeName)
traceEntry(ctx, "Resource.Read")

log.Printf("[DEBUG] Request.State.Raw. value: %v", request.State.Raw)
tflog.Debug(ctx, "Request.State.Raw", "value", hclog.Fmt("%v", request.State.Raw))

conn := r.provider.CloudControlApiClient(ctx)

Expand Down Expand Up @@ -567,16 +566,15 @@ func (r *resource) Read(ctx context.Context, request tfsdk.ReadResourceRequest,
}
}

log.Printf("[DEBUG] Response.State.Raw. value: %v", response.State.Raw)
tflog.Debug(ctx, "Response.State.Raw", "value", hclog.Fmt("%v", response.State.Raw))

log.Printf("[TRACE] Resource.Read exit. cfTypeName: %s, tfTypeName: %s", cfTypeName, tfTypeName)
traceExit(ctx, "Resource.Read")
}

func (r *resource) Update(ctx context.Context, request tfsdk.UpdateResourceRequest, response *tfsdk.UpdateResourceResponse) {
cfTypeName := r.resourceType.cfTypeName
tfTypeName := r.resourceType.tfTypeName
ctx = r.cfnTypeContext(ctx)

log.Printf("[TRACE] Resource.Update enter. cfTypeName: %s, tfTypeName: %s", cfTypeName, tfTypeName)
traceEntry(ctx, "Resource.Update")

conn := r.provider.CloudControlApiClient(ctx)

Expand Down Expand Up @@ -617,13 +615,13 @@ func (r *resource) Update(ctx context.Context, request tfsdk.UpdateResourceReque
return
}

log.Printf("[DEBUG] Cloud Control API PatchDocument: %s", patchDocument)
tflog.Debug(ctx, "Cloud Control API PatchDocument", "value", patchDocument)

input := &cloudcontrol.UpdateResourceInput{
ClientToken: aws.String(tfresource.UniqueId()),
Identifier: aws.String(id),
PatchDocument: aws.String(patchDocument),
TypeName: aws.String(cfTypeName),
TypeName: aws.String(r.resourceType.cfTypeName),
}

if roleARN := r.provider.RoleARN(ctx); roleARN != "" {
Expand Down Expand Up @@ -667,14 +665,13 @@ func (r *resource) Update(ctx context.Context, request tfsdk.UpdateResourceReque
return
}

log.Printf("[TRACE] Resource.Update exit. cfTypeName: %s, tfTypeName: %s", cfTypeName, tfTypeName)
traceExit(ctx, "Resource.Update")
}

func (r *resource) Delete(ctx context.Context, request tfsdk.DeleteResourceRequest, response *tfsdk.DeleteResourceResponse) {
cfTypeName := r.resourceType.cfTypeName
tfTypeName := r.resourceType.tfTypeName
ctx = r.cfnTypeContext(ctx)

log.Printf("[TRACE] Resource.Delete enter. cfTypeName: %s, tfTypeName: %s", cfTypeName, tfTypeName)
traceEntry(ctx, "Resource.Delete")

conn := r.provider.CloudControlApiClient(ctx)

Expand All @@ -686,7 +683,7 @@ func (r *resource) Delete(ctx context.Context, request tfsdk.DeleteResourceReque
return
}

err = tfcloudcontrol.DeleteResource(ctx, conn, r.provider.RoleARN(ctx), cfTypeName, id, r.resourceType.deleteTimeout)
err = tfcloudcontrol.DeleteResource(ctx, conn, r.provider.RoleARN(ctx), r.resourceType.cfTypeName, id, r.resourceType.deleteTimeout)

if err != nil {
response.Diagnostics = append(response.Diagnostics, ServiceOperationErrorDiag("Cloud Control API", "DeleteResource", err))
Expand All @@ -696,20 +693,19 @@ func (r *resource) Delete(ctx context.Context, request tfsdk.DeleteResourceReque

response.State.RemoveResource(ctx)

log.Printf("[TRACE] Resource.Delete exit. cfTypeName: %s, tfTypeName: %s", cfTypeName, tfTypeName)
traceExit(ctx, "Resource.Delete")
}

func (r *resource) ImportState(ctx context.Context, request tfsdk.ImportResourceStateRequest, response *tfsdk.ImportResourceStateResponse) {
cfTypeName := r.resourceType.cfTypeName
tfTypeName := r.resourceType.tfTypeName
ctx = r.cfnTypeContext(ctx)

log.Printf("[TRACE] Resource.ImportState enter. cfTypeName: %s, tfTypeName: %s", cfTypeName, tfTypeName)
traceEntry(ctx, "Resource.ImportState")

log.Printf("[DEBUG] Request.ID: %s", request.ID)
tflog.Debug(ctx, "Request.ID", "value", hclog.Fmt("%v", request.ID))

tfsdk.ResourceImportStatePassthroughID(ctx, idAttributePath, request, response)

log.Printf("[TRACE] Resource.ImportState exit. cfTypeName: %s, tfTypeName: %s", cfTypeName, tfTypeName)
traceExit(ctx, "Resource.ImportState")
}

// ConfigValidators returns a list of functions which will all be performed during validation.
Expand Down Expand Up @@ -804,6 +800,13 @@ func (r *resource) populateUnknownValues(ctx context.Context, id string, state *
return nil
}

// cfnTypeContext injects the CloudFormation type name into logger contexts.
func (r *resource) cfnTypeContext(ctx context.Context) context.Context {
ctx = tflog.With(ctx, LoggingKeyCFNType, r.resourceType.tfTypeName)

return ctx
}

// patchDocument returns a JSON Patch document describing the difference between `old` and `new`.
func patchDocument(old, new string) (string, error) {
patch, err := jsonpatch.CreatePatch([]byte(old), []byte(new))
Expand Down
19 changes: 13 additions & 6 deletions internal/generic/singular_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package generic
import (
"context"
"fmt"
"log"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudcontrol"
cctypes "github.com/aws/aws-sdk-go-v2/service/cloudcontrol/types"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-log/tflog"
tfcloudcontrol "github.com/hashicorp/terraform-provider-awscc/internal/service/cloudcontrol"
"github.com/hashicorp/terraform-provider-awscc/internal/tfresource"
)
Expand Down Expand Up @@ -65,10 +66,9 @@ func newGenericSingularDataSource(provider tfsdk.Provider, singularDataSourceTyp
}

func (sd *singularDataSource) Read(ctx context.Context, request tfsdk.ReadDataSourceRequest, response *tfsdk.ReadDataSourceResponse) {
cfTypeName := sd.dataSourceType.cfTypeName
tfTypeName := sd.dataSourceType.tfTypeName
ctx = sd.cfnTypeContext(ctx)

log.Printf("[TRACE] DataSource.Read enter. cfTypeName: %s, tfTypeName: %s", cfTypeName, tfTypeName)
traceEntry(ctx, "SingularDataSource.Read")

conn := sd.provider.CloudControlApiClient(ctx)

Expand Down Expand Up @@ -122,9 +122,9 @@ func (sd *singularDataSource) Read(ctx context.Context, request tfsdk.ReadDataSo
return
}

log.Printf("[DEBUG] Response.State.Raw. value: %v", response.State.Raw)
tflog.Debug(ctx, "Response.State.Raw", "value", hclog.Fmt("%v", response.State.Raw))

log.Printf("[TRACE] DataSource.Read exit. cfTypeName: %s, tfTypeName: %s", cfTypeName, tfTypeName)
traceExit(ctx, "SingularDataSource.Read")
}

// describe returns the live state of the specified resource.
Expand Down Expand Up @@ -154,3 +154,10 @@ func (sd *singularDataSource) setId(ctx context.Context, val string, state *tfsd

return nil
}

// cfnTypeContext injects the CloudFormation type name into logger contexts.
func (sd *singularDataSource) cfnTypeContext(ctx context.Context) context.Context {
ctx = tflog.With(ctx, LoggingKeyCFNType, sd.dataSourceType.cfTypeName)

return ctx
}
6 changes: 3 additions & 3 deletions internal/generic/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"math/big"

"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-plugin-log/tflog"
)

// Translates a Terraform Value to Cloud Control DesiredState.
Expand Down Expand Up @@ -235,7 +235,7 @@ func (t toTerraform) valueFromRaw(ctx context.Context, schema *tfsdk.Schema, pat
if isObject {
attributeName, ok := t.cfToTfNameMap[key]
if !ok {
log.Printf("[INFO] attribute name mapping not found. key: %s", key)
tflog.Info(ctx, "attribute name mapping not found", "key", key)
continue
}
path = path.WithAttributeName(attributeName)
Expand All @@ -245,7 +245,7 @@ func (t toTerraform) valueFromRaw(ctx context.Context, schema *tfsdk.Schema, pat
val, err := t.valueFromRaw(ctx, schema, path, v)
if err != nil {
if isObject {
log.Printf("[INFO] not found in Terraform schema. key: %s, path: %s, error: %s", key, path, err.Error())
tflog.Info(ctx, "not found in Terraform schema", "key", key, "path", path, "error", err.Error())
path = path.WithoutLastStep()
continue
}
Expand Down
Loading