From 358600661dc4822028f9c1c59bbc272b18ce9b8e Mon Sep 17 00:00:00 2001 From: Krzysztof Klimonda Date: Mon, 23 Sep 2024 11:28:01 +0200 Subject: [PATCH] Configure SDK Logging from provider configuration --- cmd/codegen/config.yaml | 5 +---- pkg/translate/terraform_provider/template.go | 14 ++++++++++++++ .../terraform_provider/terraform_provider_file.go | 1 + 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/cmd/codegen/config.yaml b/cmd/codegen/config.yaml index e5158e00..593174d0 100644 --- a/cmd/codegen/config.yaml +++ b/cmd/codegen/config.yaml @@ -90,12 +90,9 @@ terraform_provider_config: description: "(Local inspection mode) The PAN-OS config file to load read in using `file()`" optional: true type: string - sdk_log_level: - description: "Log level configured for the PAN-OS SDK library" - 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 panos_version: diff --git a/pkg/translate/terraform_provider/template.go b/pkg/translate/terraform_provider/template.go index 5498e0c4..86e21492 100644 --- a/pkg/translate/terraform_provider/template.go +++ b/pkg/translate/terraform_provider/template.go @@ -1544,6 +1544,16 @@ 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 + logCategories, err = sdk.LogCategoryFromStrings(categories) + if err != nil { + resp.Diagnostics.AddError("Failed to configure Terraform provider", err.Error()) + return + } + } con = &sdk.Client{ Hostname: config.Hostname.ValueString(), Username: config.Username.ValueString(), @@ -1557,6 +1567,10 @@ func (p *PanosProvider) Configure(ctx context.Context, req provider.ConfigureReq SkipVerifyCertificate: config.SkipVerifyCertificate.ValueBool(), AuthFile: config.AuthFile.ValueString(), CheckEnvironment: true, + Logging: sdk.LoggingInfo{ + SLogHandler: &TerraformSlogHandler{}, + LogCategories: logCategories, + }, //Agent: fmt.Sprintf("Terraform/%s Provider/scm Version/%s", req.TerraformVersion, p.version), } diff --git a/pkg/translate/terraform_provider/terraform_provider_file.go b/pkg/translate/terraform_provider/terraform_provider_file.go index 30100a1b..cc78fb34 100644 --- a/pkg/translate/terraform_provider/terraform_provider_file.go +++ b/pkg/translate/terraform_provider/terraform_provider_file.go @@ -361,6 +361,7 @@ 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.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", "")