Skip to content
676 changes: 676 additions & 0 deletions assets/terraform/test/resource_administrators_test.go

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ resource "panos_template_variable" "tmpl-var" {
}

resource "panos_template_variable" "stack-var" {
location = { template = { name = panos_template_stack.example.name } }
location = { template_stack = { name = panos_template_stack.example.name } }

name = format("$%s", var.prefix)
type = { interface = "ethernet1/1" }
Expand Down
22 changes: 20 additions & 2 deletions cmd/codegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package main
import (
"context"
"flag"
"fmt"
"log"
"log/slog"
"os"

"github.com/paloaltonetworks/pan-os-codegen/pkg/commands/codegen"
"github.com/paloaltonetworks/pan-os-codegen/pkg/properties"
Expand Down Expand Up @@ -40,6 +43,22 @@ func runCommand(ctx context.Context, cmdType properties.CommandType, cfg string)
}

func main() {
logLevel := os.Getenv("CODEGEN_LOG_LEVEL")
if logLevel == "" {
logLevel = "ERROR"
}
var level slog.Level
var err = level.UnmarshalText([]byte(logLevel))
if err != nil {
fmt.Print(err.Error())
return
}

logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: level,
}))
slog.SetDefault(logger)

cfg := parseFlags()

ctx := context.Background()
Expand All @@ -52,8 +71,7 @@ func main() {
} else {
opTypeMessage += cfg.OpType
}
log.Printf("Using configuration file: %s\n", cfg.ConfigFile)
log.Println(opTypeMessage)
slog.Debug("Parsed configuration file", "path", cfg.ConfigFile)

cmdType := properties.CommandTypeSDK // Default command type
if cfg.OpType == "mktp" {
Expand Down
10 changes: 5 additions & 5 deletions pkg/commands/codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package codegen
import (
"context"
"fmt"
"log"
"log/slog"

"github.com/paloaltonetworks/pan-os-codegen/pkg/generate"
"github.com/paloaltonetworks/pan-os-codegen/pkg/load"
Expand Down Expand Up @@ -56,13 +56,13 @@ func (c *Command) Setup() error {
}

func (c *Command) Execute() error {
log.Printf("Generating %s\n", c.commandType)

if len(c.args) == 0 {
return fmt.Errorf("path to configuration file is required")
}
configPath := c.args[0]

slog.Info("Generating code", "type", c.commandType)

content, err := load.File(configPath)
if err != nil {
return fmt.Errorf("error loading %s - %s", configPath, err)
Expand All @@ -78,7 +78,7 @@ func (c *Command) Execute() error {
specMetadata := make(map[string]properties.TerraformProviderSpecMetadata)

for _, specPath := range c.specs {
log.Printf("Parsing %s...\n", specPath)
slog.Info("Parsing YAML spec", "spec", specPath)
content, err := load.File(specPath)
if err != nil {
return fmt.Errorf("error loading %s - %s", specPath, err)
Expand Down Expand Up @@ -194,7 +194,7 @@ func (c *Command) Execute() error {
if err != nil {
return fmt.Errorf("error generating terraform code: %w", err)
}
log.Printf("Generated Terraform resources: %s\n Generated dataSources: %s", resourceList, dataSourceList)
slog.Debug("Generated Terraform resources", "resources", resourceList, "dataSources", dataSourceList)
}

if err = generate.CopyAssets(config, c.commandType); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/generate/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"io"
"io/fs"
"log"
"log/slog"
"os"
"path/filepath"
"strings"
Expand All @@ -20,7 +20,7 @@ func CopyAssets(config *properties.Config, commandType properties.CommandType) e
return err
}

log.Printf("%v", asset)
slog.Debug("Copying static assets", "source", asset)
if asset.Target.GoSdk && commandType == properties.CommandTypeSDK {
if err = copyAsset(config.Output.GoSdk, asset, files); err != nil {
return err
Expand Down Expand Up @@ -80,7 +80,7 @@ func copyAsset(target string, asset *properties.Asset, files []string) error {
}

destinationFilePath := filepath.Join(destinationDir, sourceFileDirRelative, filepath.Base(sourceFilePath))
log.Printf("Copy file from %s to %s\n", sourceFilePath, destinationFilePath)
slog.Debug("Copying rendered file", "source", sourceFilePath, "destination", destinationFilePath)

// Read the contents of the source files
data, err := os.ReadFile(sourceFilePath)
Expand Down
5 changes: 2 additions & 3 deletions pkg/generate/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"go/format"
"io"
"log"
"log/slog"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -37,16 +38,14 @@ func NewCreator(goOutputDir, templatesDir string, spec *properties.Normalization

// RenderTemplate loops through all templates, parses them, and renders content, which is saved to the output file.
func (c *Creator) RenderTemplate() error {
log.Println("Start rendering templates")

templates, err := c.listOfTemplates()
if err != nil {
return fmt.Errorf("error listing templates: %w", err)
}

for _, templateName := range templates {
filePath := c.createFullFilePath(templateName)
log.Printf("Creating file: %s\n", filePath)
slog.Debug("Creating target file", "path", filePath)

if err := c.makeAllDirs(filePath); err != nil {
return fmt.Errorf("error creating directories for %s: %w", filePath, err)
Expand Down
11 changes: 10 additions & 1 deletion pkg/properties/normalized.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ type SpecParamTerraformProviderConfig struct {
Type *string `json:"type" yaml:"type"`
Private *bool `json:"ignored" yaml:"private"`
Sensitive *bool `json:"sensitive" yaml:"sensitive"`
Optional *bool `json:"optional" yaml:"optional"`
Computed *bool `json:"computed" yaml:"computed"`
Required *bool `json:"required" yaml:"required"`
VariantCheck *string `json:"variant_check" yaml:"variant_check"`
Expand Down Expand Up @@ -321,6 +322,14 @@ func (o *SpecParam) FinalSensitive() bool {
return false
}

func (o *SpecParam) FinalOptional() bool {
if o.TerraformProviderConfig != nil && o.TerraformProviderConfig.Optional != nil {
return *o.TerraformProviderConfig.Optional
}

return !o.FinalRequired()
}

func (o *SpecParam) FinalComputed() bool {
if o.TerraformProviderConfig != nil && o.TerraformProviderConfig.Computed != nil {
return *o.TerraformProviderConfig.Computed
Expand Down Expand Up @@ -590,6 +599,7 @@ func schemaParameterToSpecParameter(schemaSpec *parameter.Parameter) (*SpecParam
Name: schemaSpec.CodegenOverrides.Terraform.Name,
Type: schemaSpec.CodegenOverrides.Terraform.Type,
Private: schemaSpec.CodegenOverrides.Terraform.Private,
Optional: schemaSpec.CodegenOverrides.Terraform.Optional,
Sensitive: schemaSpec.CodegenOverrides.Terraform.Sensitive,
Computed: schemaSpec.CodegenOverrides.Terraform.Computed,
Required: schemaSpec.CodegenOverrides.Terraform.Required,
Expand Down Expand Up @@ -1578,7 +1588,6 @@ func (spec *Normalization) HasPrivateParameters() bool {
}

func resolveXpath(xpath []string, spec *Spec) (*SpecParam, error) {
fmt.Printf("xpath[0]: %s\n", xpath[0])
elt := xpath[0]
if elt == "spec" {
elt = xpath[1]
Expand Down
2 changes: 2 additions & 0 deletions pkg/schema/parameter/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type EnumSpecValue struct {
type VariantCheckType string

const (
VariantCheckDisabled VariantCheckType = "Disabled"
VariantCheckConflictsWith VariantCheckType = "ConflictsWith"
VariantCheckExactlyOneOf VariantCheckType = "ExactlyOneOf"
)
Expand All @@ -60,6 +61,7 @@ type CodegenOverridesTerraform struct {
Name *string `yaml:"name"`
Type *string `yaml:"type"`
Private *bool `yaml:"private"`
Optional *bool `yaml:"optional"`
Sensitive *bool `yaml:"sensitive"`
Computed *bool `yaml:"computed"`
Required *bool `yaml:"required"`
Expand Down
3 changes: 0 additions & 3 deletions pkg/translate/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ func RenderImports(spec *properties.Normalization, templateTypes ...string) (str
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/util", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/version", "")

if spec.Name == "global-protect-portal" && !spec.HasParametersWithStrconv() {
panic("WTF?")
}
if spec.HasParametersWithStrconv() {
manager.AddStandardImport("errors", "")
}
Expand Down
3 changes: 0 additions & 3 deletions pkg/translate/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package translate
import (
"bytes"
"fmt"
"log"
"strings"
"text/template"

Expand Down Expand Up @@ -486,8 +485,6 @@ func checkIfDeviceVersionSupportedByProfile(param *properties.SpecParam, deviceV
return true
}

log.Printf("Param: %s, deviceVersion: %s, MinVersion: %s, MaxVersion: %s", param.Name.CamelCase, deviceVersion, profile.MinVersion.String(), profile.MaxVersion.String())

if deviceVersion.GreaterThanOrEqualTo(*profile.MinVersion) && deviceVersion.LesserThan(*profile.MaxVersion) {
return true
}
Expand Down
Loading