Skip to content

Commit

Permalink
Update: enable debug logging in the provider
Browse files Browse the repository at this point in the history
 - when TF_LOG is debug/trace (and OS_DEBUG is not set)
 - enable this by default (person probably wants it)

Resolves: #1203
  • Loading branch information
till committed Jan 23, 2022
1 parent 4e51c3f commit 4e0e169
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions openstack/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package openstack

import (
"context"
"os"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/meta"
"github.com/hashicorp/terraform/helper/logging"

"github.com/gophercloud/utils/terraform/auth"
"github.com/gophercloud/utils/terraform/mutexkv"
Expand Down Expand Up @@ -245,6 +247,13 @@ func Provider() *schema.Provider {
Default: false,
Description: descriptions["disable_no_cache_header"],
},

"enable_logging": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: descriptions["enable_logging"],
},
},

DataSourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -487,10 +496,22 @@ func init() {
"automatically, if the initial auth token get expired. Defaults to `true`",

"max_retries": "How many times HTTP connection should be retried until giving up.",

"enable_logging": "Outputs very verbose logs with all calls made to and responses from OpenStack",
}
}

func configureProvider(d *schema.ResourceData, terraformVersion string) (interface{}, diag.Diagnostics) {
enableLogging := d.Get("enable_logging").(bool)
if !enableLogging {
// enforce logging (similar to OS_DEBUG) when TF_LOG is 'DEBUG' or 'TRACE'
if logLevel := logging.LogLevel(); logLevel != "" && os.Getenv("OS_DEBUG") == "" {
if logLevel == "DEBUG" || logLevel == "TRACE" {
enableLogging = true
}
}
}

config := Config{
auth.Config{
CACertFile: d.Get("cacert_file").(string),
Expand Down Expand Up @@ -526,6 +547,7 @@ func configureProvider(d *schema.ResourceData, terraformVersion string) (interfa
TerraformVersion: terraformVersion,
SDKVersion: meta.SDKVersionString(),
MutexKV: mutexkv.NewMutexKV(),
EnableLogger: enableLogging,
},
}

Expand Down

0 comments on commit 4e0e169

Please sign in to comment.