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: Add support for entry-style resource lists (panos_address_objects) #128

Merged
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
2 changes: 0 additions & 2 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ linters:
#- err113 # disabled because of too many dynamic errors that don't wrap anything

linters-settings:
exhaustive:
default-signifies-exhaustive: true
gci:
sections:
- standard
Expand Down
56 changes: 43 additions & 13 deletions pkg/commands/codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,37 +92,67 @@ func (c *Command) Execute() error {
}

if c.commandType == properties.CommandTypeTerraform {
var singularVariant, pluralVariant bool
// For specs that are missing resource_variants, default to generating
// just singular variants of entry type.
if len(spec.TerraformProviderConfig.ResourceVariants) == 0 {
singularVariant = true
}
terraformResourceType := spec.TerraformProviderConfig.ResourceType
if terraformResourceType == "" {
terraformResourceType = properties.TerraformResourceEntry
}

_, uuid := spec.Spec.Params["uuid"]
if !uuid {
terraformGenerator := generate.NewCreator(config.Output.TerraformProvider, c.templatePath, spec)
dataSources, resources, err := terraformGenerator.RenderTerraformProviderFile(spec, properties.ResourceEntry)
if err != nil {
return fmt.Errorf("error rendering Terraform provider file for %s - %s", specPath, err)
for _, elt := range spec.TerraformProviderConfig.ResourceVariants {
switch elt {
case properties.TerraformResourceSingular:
singularVariant = true
case properties.TerraformResourcePlural:
pluralVariant = true
}
}

if singularVariant {
var resourceTyp properties.ResourceType
switch terraformResourceType {
case properties.TerraformResourceEntry:
resourceTyp = properties.ResourceEntry
case properties.TerraformResourceUuid:
resourceTyp = properties.ResourceUuid
case properties.TerraformResourceConfig:
panic("missing implementation for config type resources")
}

resourceList = append(resourceList, resources...)
dataSourceList = append(dataSourceList, dataSources...)
} else {
terraformGenerator := generate.NewCreator(config.Output.TerraformProvider, c.templatePath, spec)
dataSources, resources, err := terraformGenerator.RenderTerraformProviderFile(spec, properties.ResourceUuid)
dataSources, resources, err := terraformGenerator.RenderTerraformProviderFile(spec, resourceTyp)
if err != nil {
return fmt.Errorf("error rendering Terraform provider file for %s - %s", specPath, err)
}

resourceList = append(resourceList, resources...)
dataSourceList = append(dataSourceList, dataSources...)
}

terraformGenerator = generate.NewCreator(config.Output.TerraformProvider, c.templatePath, spec)
dataSources, resources, err = terraformGenerator.RenderTerraformProviderFile(spec, properties.ResourceUuidPlural)
if pluralVariant {
var resourceTyp properties.ResourceType
switch terraformResourceType {
case properties.TerraformResourceEntry:
resourceTyp = properties.ResourceEntryPlural
case properties.TerraformResourceUuid:
resourceTyp = properties.ResourceUuidPlural
case properties.TerraformResourceConfig:
panic("missing implementation for config type resources")
}

terraformGenerator := generate.NewCreator(config.Output.TerraformProvider, c.templatePath, spec)
dataSources, resources, err := terraformGenerator.RenderTerraformProviderFile(spec, resourceTyp)
if err != nil {
return fmt.Errorf("error rendering Terraform provider file for %s - %s", specPath, err)
}

resourceList = append(resourceList, resources...)
dataSourceList = append(dataSourceList, dataSources...)
}

} else if c.commandType == properties.CommandTypeSDK {
generator := generate.NewCreator(config.Output.GoSdk, c.templatePath, spec)
if err = generator.RenderTemplate(); err != nil {
Expand Down
15 changes: 11 additions & 4 deletions pkg/generate/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,12 @@
// RenderTerraformProviderFile generates a Go file for a Terraform provider based on the provided TerraformProviderFile and Normalization arguments.
func (c *Creator) RenderTerraformProviderFile(spec *properties.Normalization, typ properties.ResourceType) ([]string, []string, error) {
var name string
if typ == properties.ResourceUuidPlural {
switch typ {

Check failure on line 62 in pkg/generate/generator.go

View workflow job for this annotation

GitHub Actions / lint

missing cases in switch of type properties.ResourceType: properties.ResourceEntry, properties.ResourceUuid (exhaustive)
case properties.ResourceUuidPlural:
name = fmt.Sprintf("%s_%s", spec.TerraformProviderConfig.Suffix, spec.TerraformProviderConfig.PluralName)
} else {
case properties.ResourceEntryPlural:
name = spec.TerraformProviderConfig.PluralSuffix
default:
name = spec.Name
}

Expand All @@ -85,10 +88,14 @@
}

var filePath string
if typ == properties.ResourceUuidPlural {
switch typ {

Check failure on line 91 in pkg/generate/generator.go

View workflow job for this annotation

GitHub Actions / lint

missing cases in switch of type properties.ResourceType: properties.ResourceEntry, properties.ResourceUuid (exhaustive)
case properties.ResourceUuidPlural:
name = fmt.Sprintf("%s_%s", spec.TerraformProviderConfig.Suffix, spec.TerraformProviderConfig.PluralName)
filePath = c.createTerraformProviderFilePath(name)
} else {
case properties.ResourceEntryPlural:
name = spec.TerraformProviderConfig.PluralSuffix
filePath = c.createTerraformProviderFilePath(name)
default:
filePath = c.createTerraformProviderFilePath(spec.TerraformProviderConfig.Suffix)
}

Expand Down
35 changes: 30 additions & 5 deletions pkg/properties/normalized.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,30 @@ type Normalization struct {
Const map[string]*Const `json:"const" yaml:"const"`
}

type TerraformResourceType string

const (
TerraformResourceEntry TerraformResourceType = "entry"
TerraformResourceUuid TerraformResourceType = "uuid"
TerraformResourceConfig TerraformResourceType = "config"
)

type TerraformResourceVariant string

const (
TerraformResourceSingular TerraformResourceVariant = "singular"
TerraformResourcePlural TerraformResourceVariant = "plural"
)

type TerraformProviderConfig struct {
SkipResource bool `json:"skip_resource" yaml:"skip_resource"`
SkipDatasource bool `json:"skip_datasource" yaml:"skip_datasource"`
SkipDatasourceListing bool `json:"skip_datasource_listing" yaml:"skip_datasource_listing"`
Suffix string `json:"suffix" yaml:"suffix"`
PluralName string `json:"plural_name" yaml:"plural_name"`
SkipResource bool `json:"skip_resource" yaml:"skip_resource"`
SkipDatasource bool `json:"skip_datasource" yaml:"skip_datasource"`
SkipDatasourceListing bool `json:"skip_datasource_listing" yaml:"skip_datasource_listing"`
ResourceType TerraformResourceType `json:"resource_type" yaml:"resource_type"`
ResourceVariants []TerraformResourceVariant `json:"resource_variants" yaml:"resource_variants"`
Suffix string `json:"suffix" yaml:"suffix"`
PluralSuffix string `json:"plural_suffix" yaml:"plural_suffix"`
PluralName string `json:"plural_name" yaml:"plural_name"`
}

type NameVariant struct {
Expand Down Expand Up @@ -432,13 +450,20 @@ func generateImportVariables(variables []xpathschema.Variable) map[string]*Impor
}

func schemaToSpec(object object.Object) (*Normalization, error) {
var resourceVariants []TerraformResourceVariant
for _, elt := range object.TerraformConfig.ResourceVariants {
resourceVariants = append(resourceVariants, TerraformResourceVariant(elt))
}
spec := &Normalization{
Name: object.DisplayName,
TerraformProviderConfig: TerraformProviderConfig{
SkipResource: object.TerraformConfig.SkipResource,
SkipDatasource: object.TerraformConfig.SkipDatasource,
SkipDatasourceListing: object.TerraformConfig.SkipdatasourceListing,
ResourceType: TerraformResourceType(object.TerraformConfig.ResourceType),
ResourceVariants: resourceVariants,
Suffix: object.TerraformConfig.Suffix,
PluralSuffix: object.TerraformConfig.PluralSuffix,
PluralName: object.TerraformConfig.PluralName,
},
Locations: make(map[string]*Location),
Expand Down
7 changes: 4 additions & 3 deletions pkg/properties/resourcetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package properties
type ResourceType int

const (
ResourceEntry ResourceType = iota
ResourceUuid ResourceType = iota
ResourceUuidPlural ResourceType = iota
ResourceEntry ResourceType = iota
ResourceEntryPlural ResourceType = iota
ResourceUuid ResourceType = iota
ResourceUuidPlural ResourceType = iota
)
28 changes: 23 additions & 5 deletions pkg/schema/object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,30 @@ import (
"github.com/paloaltonetworks/pan-os-codegen/pkg/schema/validator"
)

type TerraformResourceType string

const (
TerraformResourceEntry TerraformResourceType = "entry"
TerraformResourceUuid TerraformResourceType = "uuid"
TerraformResourceConfig TerraformResourceType = "config"
)

type TerraformResourceVariant string

const (
TerraformResourceSingular TerraformResourceVariant = "singular"
TerraformResourcePlural TerraformResourceVariant = "plural"
)

type TerraformConfig struct {
SkipResource bool `yaml:"skip_resource"`
SkipDatasource bool `yaml:"skip_datasource"`
SkipdatasourceListing bool `yaml:"skip_datasource_listing"`
Suffix string `yaml:"suffix"`
PluralName string `yaml:"plural_name"`
SkipResource bool `yaml:"skip_resource"`
SkipDatasource bool `yaml:"skip_datasource"`
SkipdatasourceListing bool `yaml:"skip_datasource_listing"`
ResourceType TerraformResourceType `yaml:"resource_type"`
ResourceVariants []TerraformResourceVariant `yaml:"resource_variants"`
Suffix string `yaml:"suffix"`
PluralSuffix string `yaml:"plural_suffix"`
PluralName string `yaml:"plural_name"`
}

type GoSdkConfig struct {
Expand Down
Loading
Loading