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

Feat terraform sdk logging #171

Open
wants to merge 2 commits 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
17 changes: 14 additions & 3 deletions assets/pango/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1006,8 +1006,19 @@ func (c *Client) GetTechSupportFile(ctx context.Context) (string, []byte, error)
func (c *Client) setupLogging(logging LoggingInfo) error {
var logger *slog.Logger

var logLevel slog.Level
var levelStr string
if levelStr = os.Getenv("PANOS_LOG_LEVEL"); c.CheckEnvironment && levelStr != "" {
err := logLevel.UnmarshalText([]byte(levelStr))
if err != nil {
return err
}
} else {
logLevel = slog.LevelInfo
}

if logging.SLogHandler == nil {
logger = slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: logging.LogLevel}))
logger = slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: logLevel}))
logger.Info("No slog handler provided, creating default os.Stderr handler.", "LogLevel", logging.LogLevel.Level())
} else {
logger = slog.New(logging.SLogHandler)
Expand All @@ -1019,7 +1030,7 @@ func (c *Client) setupLogging(logging LoggingInfo) error {
// 1. logging.LogCategories has the highest priority
// 2. If logging.LogCategories is not set, we check logging.LogSymbols
// 3. If logging.LogSymbols is empty and c.CheckEnvironment is true we consult
// PANOS_LOGGING environment variable.
// PANOS_LOG_CATEGORIES environment variable.
// 4. If no logging categories have been selected, default to basic library logging
// (i.e. "pango" category)
logMask := logging.LogCategories
Expand All @@ -1031,7 +1042,7 @@ func (c *Client) setupLogging(logging LoggingInfo) error {
}

if logMask == 0 {
if val := os.Getenv("PANOS_LOGGING"); c.CheckEnvironment && val != "" {
if val := os.Getenv("PANOS_LOG_CATEGORIES"); c.CheckEnvironment && val != "" {
symbols := strings.Split(val, ",")
logMask, err = LogCategoryFromStrings(symbols)
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion assets/pango/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func LogCategoryFromStrings(symbols []string) (LogCategory, error) {
}

logCategoriesMask |= category
slog.Info("logCategoriesMask", "equal", logCategoriesMask)
}
return logCategoriesMask, nil
}
Expand Down
11 changes: 11 additions & 0 deletions cmd/codegen/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ terraform_provider_config:
description: "(Local inspection mode) The PAN-OS config file to load read in using `file()`"
optional: true
type: string
sdk_log_categories:
description: "Log categories to configure for the PAN-OS SDK library"
env_name: "PANOS_LOG_CATEGORIES"
optional: true
type: string
sdk_log_level:
description: "SDK logging Level for categories"
env_name: "PANOS_LOG_LEVEL"
type: string
default_value: "INFO"
optional: true
panos_version:
description: "(Local inspection mode) The version of PAN-OS that exported the config file. This is only used if the root 'config' block does not contain the 'detail-version' attribute. Example: `10.2.3`."
optional: true
Expand Down
26 changes: 26 additions & 0 deletions pkg/translate/terraform_provider/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -1544,6 +1544,28 @@ func (p *PanosProvider) Configure(ctx context.Context, req provider.ConfigureReq
}
} else {
tflog.Info(ctx, "Configuring client for API mode")
var logCategories sdk.LogCategory
if !config.SdkLogCategories.IsNull() {
categories := strings.Split(config.SdkLogCategories.ValueString(), ",")
var err error
kklimonda-cl marked this conversation as resolved.
Show resolved Hide resolved
logCategories, err = sdk.LogCategoryFromStrings(categories)
if err != nil {
resp.Diagnostics.AddError("Failed to configure Terraform provider", err.Error())
return
}
}

var logLevel slog.Level
if !config.SdkLogLevel.IsNull() {
levelStr := config.SdkLogLevel.ValueString()
err := logLevel.UnmarshalText([]byte(levelStr))
if err != nil {
resp.Diagnostics.AddError("Failed to configure Terraform provider", fmt.Sprintf("Invalid Log Level: %s", levelStr))
}
} else {
logLevel = slog.LevelInfo
}

con = &sdk.Client{
Hostname: config.Hostname.ValueString(),
Username: config.Username.ValueString(),
Expand All @@ -1557,6 +1579,10 @@ func (p *PanosProvider) Configure(ctx context.Context, req provider.ConfigureReq
SkipVerifyCertificate: config.SkipVerifyCertificate.ValueBool(),
AuthFile: config.AuthFile.ValueString(),
CheckEnvironment: true,
Logging: sdk.LoggingInfo{
LogLevel: logLevel,
LogCategories: logCategories,
},
//Agent: fmt.Sprintf("Terraform/%s Provider/scm Version/%s", req.TerraformVersion, p.version),
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/translate/terraform_provider/terraform_provider_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,9 @@ func (g *GenerateTerraformProvider) GenerateTerraformProviderFile(spec *properti

func (g *GenerateTerraformProvider) GenerateTerraformProvider(terraformProvider *properties.TerraformProviderFile, spec *properties.Normalization, providerConfig properties.TerraformProvider) error {
terraformProvider.ImportManager.AddStandardImport("context", "")
terraformProvider.ImportManager.AddStandardImport("strings", "")
terraformProvider.ImportManager.AddStandardImport("fmt", "")
terraformProvider.ImportManager.AddStandardImport("log/slog", "")
terraformProvider.ImportManager.AddSdkImport("github.com/PaloAltoNetworks/pango", "sdk")
terraformProvider.ImportManager.AddHashicorpImport("github.com/hashicorp/terraform-plugin-framework/datasource", "")
terraformProvider.ImportManager.AddHashicorpImport("github.com/hashicorp/terraform-plugin-framework/function", "")
Expand Down