Skip to content

Commit 90c9c52

Browse files
authored
Add support for entry-style resource lists (panos_address_objects) (#128)
1 parent 6ab060d commit 90c9c52

File tree

13 files changed

+705
-67
lines changed

13 files changed

+705
-67
lines changed

.golangci.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ linters:
3333
#- err113 # disabled because of too many dynamic errors that don't wrap anything
3434

3535
linters-settings:
36-
exhaustive:
37-
default-signifies-exhaustive: true
3836
gci:
3937
sections:
4038
- standard

pkg/commands/codegen/codegen.go

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,37 +92,67 @@ func (c *Command) Execute() error {
9292
}
9393

9494
if c.commandType == properties.CommandTypeTerraform {
95+
var singularVariant, pluralVariant bool
96+
// For specs that are missing resource_variants, default to generating
97+
// just singular variants of entry type.
98+
if len(spec.TerraformProviderConfig.ResourceVariants) == 0 {
99+
singularVariant = true
100+
}
101+
terraformResourceType := spec.TerraformProviderConfig.ResourceType
102+
if terraformResourceType == "" {
103+
terraformResourceType = properties.TerraformResourceEntry
104+
}
95105

96-
_, uuid := spec.Spec.Params["uuid"]
97-
if !uuid {
98-
terraformGenerator := generate.NewCreator(config.Output.TerraformProvider, c.templatePath, spec)
99-
dataSources, resources, err := terraformGenerator.RenderTerraformProviderFile(spec, properties.ResourceEntry)
100-
if err != nil {
101-
return fmt.Errorf("error rendering Terraform provider file for %s - %s", specPath, err)
106+
for _, elt := range spec.TerraformProviderConfig.ResourceVariants {
107+
switch elt {
108+
case properties.TerraformResourceSingular:
109+
singularVariant = true
110+
case properties.TerraformResourcePlural:
111+
pluralVariant = true
112+
}
113+
}
114+
115+
if singularVariant {
116+
var resourceTyp properties.ResourceType
117+
switch terraformResourceType {
118+
case properties.TerraformResourceEntry:
119+
resourceTyp = properties.ResourceEntry
120+
case properties.TerraformResourceUuid:
121+
resourceTyp = properties.ResourceUuid
122+
case properties.TerraformResourceConfig:
123+
panic("missing implementation for config type resources")
102124
}
103125

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

113132
resourceList = append(resourceList, resources...)
114133
dataSourceList = append(dataSourceList, dataSources...)
134+
}
115135

116-
terraformGenerator = generate.NewCreator(config.Output.TerraformProvider, c.templatePath, spec)
117-
dataSources, resources, err = terraformGenerator.RenderTerraformProviderFile(spec, properties.ResourceUuidPlural)
136+
if pluralVariant {
137+
var resourceTyp properties.ResourceType
138+
switch terraformResourceType {
139+
case properties.TerraformResourceEntry:
140+
resourceTyp = properties.ResourceEntryPlural
141+
case properties.TerraformResourceUuid:
142+
resourceTyp = properties.ResourceUuidPlural
143+
case properties.TerraformResourceConfig:
144+
panic("missing implementation for config type resources")
145+
}
146+
147+
terraformGenerator := generate.NewCreator(config.Output.TerraformProvider, c.templatePath, spec)
148+
dataSources, resources, err := terraformGenerator.RenderTerraformProviderFile(spec, resourceTyp)
118149
if err != nil {
119150
return fmt.Errorf("error rendering Terraform provider file for %s - %s", specPath, err)
120151
}
121152

122153
resourceList = append(resourceList, resources...)
123154
dataSourceList = append(dataSourceList, dataSources...)
124155
}
125-
126156
} else if c.commandType == properties.CommandTypeSDK {
127157
generator := generate.NewCreator(config.Output.GoSdk, c.templatePath, spec)
128158
if err = generator.RenderTemplate(); err != nil {

pkg/generate/generator.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,12 @@ func (c *Creator) RenderTemplate() error {
5959
// RenderTerraformProviderFile generates a Go file for a Terraform provider based on the provided TerraformProviderFile and Normalization arguments.
6060
func (c *Creator) RenderTerraformProviderFile(spec *properties.Normalization, typ properties.ResourceType) ([]string, []string, error) {
6161
var name string
62-
if typ == properties.ResourceUuidPlural {
62+
switch typ {
63+
case properties.ResourceUuidPlural:
6364
name = fmt.Sprintf("%s_%s", spec.TerraformProviderConfig.Suffix, spec.TerraformProviderConfig.PluralName)
64-
} else {
65+
case properties.ResourceEntryPlural:
66+
name = spec.TerraformProviderConfig.PluralSuffix
67+
default:
6568
name = spec.Name
6669
}
6770

@@ -85,10 +88,14 @@ func (c *Creator) RenderTerraformProviderFile(spec *properties.Normalization, ty
8588
}
8689

8790
var filePath string
88-
if typ == properties.ResourceUuidPlural {
91+
switch typ {
92+
case properties.ResourceUuidPlural:
8993
name = fmt.Sprintf("%s_%s", spec.TerraformProviderConfig.Suffix, spec.TerraformProviderConfig.PluralName)
9094
filePath = c.createTerraformProviderFilePath(name)
91-
} else {
95+
case properties.ResourceEntryPlural:
96+
name = spec.TerraformProviderConfig.PluralSuffix
97+
filePath = c.createTerraformProviderFilePath(name)
98+
default:
9299
filePath = c.createTerraformProviderFilePath(spec.TerraformProviderConfig.Suffix)
93100
}
94101

pkg/properties/normalized.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,30 @@ type Normalization struct {
2828
Const map[string]*Const `json:"const" yaml:"const"`
2929
}
3030

31+
type TerraformResourceType string
32+
33+
const (
34+
TerraformResourceEntry TerraformResourceType = "entry"
35+
TerraformResourceUuid TerraformResourceType = "uuid"
36+
TerraformResourceConfig TerraformResourceType = "config"
37+
)
38+
39+
type TerraformResourceVariant string
40+
41+
const (
42+
TerraformResourceSingular TerraformResourceVariant = "singular"
43+
TerraformResourcePlural TerraformResourceVariant = "plural"
44+
)
45+
3146
type TerraformProviderConfig struct {
32-
SkipResource bool `json:"skip_resource" yaml:"skip_resource"`
33-
SkipDatasource bool `json:"skip_datasource" yaml:"skip_datasource"`
34-
SkipDatasourceListing bool `json:"skip_datasource_listing" yaml:"skip_datasource_listing"`
35-
Suffix string `json:"suffix" yaml:"suffix"`
36-
PluralName string `json:"plural_name" yaml:"plural_name"`
47+
SkipResource bool `json:"skip_resource" yaml:"skip_resource"`
48+
SkipDatasource bool `json:"skip_datasource" yaml:"skip_datasource"`
49+
SkipDatasourceListing bool `json:"skip_datasource_listing" yaml:"skip_datasource_listing"`
50+
ResourceType TerraformResourceType `json:"resource_type" yaml:"resource_type"`
51+
ResourceVariants []TerraformResourceVariant `json:"resource_variants" yaml:"resource_variants"`
52+
Suffix string `json:"suffix" yaml:"suffix"`
53+
PluralSuffix string `json:"plural_suffix" yaml:"plural_suffix"`
54+
PluralName string `json:"plural_name" yaml:"plural_name"`
3755
}
3856

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

434452
func schemaToSpec(object object.Object) (*Normalization, error) {
453+
var resourceVariants []TerraformResourceVariant
454+
for _, elt := range object.TerraformConfig.ResourceVariants {
455+
resourceVariants = append(resourceVariants, TerraformResourceVariant(elt))
456+
}
435457
spec := &Normalization{
436458
Name: object.DisplayName,
437459
TerraformProviderConfig: TerraformProviderConfig{
438460
SkipResource: object.TerraformConfig.SkipResource,
439461
SkipDatasource: object.TerraformConfig.SkipDatasource,
440462
SkipDatasourceListing: object.TerraformConfig.SkipdatasourceListing,
463+
ResourceType: TerraformResourceType(object.TerraformConfig.ResourceType),
464+
ResourceVariants: resourceVariants,
441465
Suffix: object.TerraformConfig.Suffix,
466+
PluralSuffix: object.TerraformConfig.PluralSuffix,
442467
PluralName: object.TerraformConfig.PluralName,
443468
},
444469
Locations: make(map[string]*Location),

pkg/properties/resourcetype.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package properties
33
type ResourceType int
44

55
const (
6-
ResourceEntry ResourceType = iota
7-
ResourceUuid ResourceType = iota
8-
ResourceUuidPlural ResourceType = iota
6+
ResourceEntry ResourceType = iota
7+
ResourceEntryPlural ResourceType = iota
8+
ResourceUuid ResourceType = iota
9+
ResourceUuidPlural ResourceType = iota
910
)

pkg/schema/object/object.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,30 @@ import (
99
"github.com/paloaltonetworks/pan-os-codegen/pkg/schema/validator"
1010
)
1111

12+
type TerraformResourceType string
13+
14+
const (
15+
TerraformResourceEntry TerraformResourceType = "entry"
16+
TerraformResourceUuid TerraformResourceType = "uuid"
17+
TerraformResourceConfig TerraformResourceType = "config"
18+
)
19+
20+
type TerraformResourceVariant string
21+
22+
const (
23+
TerraformResourceSingular TerraformResourceVariant = "singular"
24+
TerraformResourcePlural TerraformResourceVariant = "plural"
25+
)
26+
1227
type TerraformConfig struct {
13-
SkipResource bool `yaml:"skip_resource"`
14-
SkipDatasource bool `yaml:"skip_datasource"`
15-
SkipdatasourceListing bool `yaml:"skip_datasource_listing"`
16-
Suffix string `yaml:"suffix"`
17-
PluralName string `yaml:"plural_name"`
28+
SkipResource bool `yaml:"skip_resource"`
29+
SkipDatasource bool `yaml:"skip_datasource"`
30+
SkipdatasourceListing bool `yaml:"skip_datasource_listing"`
31+
ResourceType TerraformResourceType `yaml:"resource_type"`
32+
ResourceVariants []TerraformResourceVariant `yaml:"resource_variants"`
33+
Suffix string `yaml:"suffix"`
34+
PluralSuffix string `yaml:"plural_suffix"`
35+
PluralName string `yaml:"plural_name"`
1836
}
1937

2038
type GoSdkConfig struct {

0 commit comments

Comments
 (0)