Skip to content

Commit 36fb703

Browse files
committed
Implement the new location import API
1 parent fd1a1ab commit 36fb703

File tree

12 files changed

+431
-324
lines changed

12 files changed

+431
-324
lines changed

pkg/generate/generator.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ func (c *Creator) parseTemplate(templateName string) (*template.Template, error)
243243
templatePath := filepath.Join(c.TemplatesDir, templateName)
244244
funcMap := template.FuncMap{
245245
"renderImports": translate.RenderImports,
246+
"RenderEntryImportStructs": func() (string, error) { return translate.RenderEntryImportStructs(c.Spec) },
246247
"packageName": translate.PackageName,
247248
"locationType": translate.LocationType,
248249
"specParamType": translate.SpecParamType,

pkg/properties/normalized.go

Lines changed: 59 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,30 @@ type Normalization struct {
2222
XpathSuffix []string `json:"xpath_suffix" yaml:"xpath_suffix"`
2323
Locations map[string]*Location `json:"locations" yaml:"locations"`
2424
Entry *Entry `json:"entry" yaml:"entry"`
25-
Imports map[string]*Import `json:"imports" yaml:"imports"`
25+
Imports []Import `json:"imports" yaml:"imports"`
2626
Version string `json:"version" yaml:"version"`
2727
Spec *Spec `json:"spec" yaml:"spec"`
2828
Const map[string]*Const `json:"const" yaml:"const"`
2929
}
3030

31+
type Import struct {
32+
Variant *NameVariant
33+
Type *NameVariant
34+
Locations map[string]ImportLocation
35+
}
36+
37+
type ImportLocation struct {
38+
Name *NameVariant
39+
XpathElements []string
40+
XpathVariables map[string]ImportXpathVariable
41+
}
42+
43+
type ImportXpathVariable struct {
44+
Name *NameVariant
45+
Description string
46+
Default string
47+
}
48+
3149
type TerraformResourceType string
3250

3351
const (
@@ -90,19 +108,6 @@ type Entry struct {
90108
Name *EntryName `json:"name" yaml:"name"`
91109
}
92110

93-
type Import struct {
94-
Name *NameVariant
95-
Xpath []string `json:"xpath" yaml:"xpath"`
96-
Vars map[string]*ImportVar `json:"vars" yaml:"vars"`
97-
OnlyForParams []string `json:"only_for_params" yaml:"only_for_params"`
98-
}
99-
100-
type ImportVar struct {
101-
Name *NameVariant
102-
Description string `json:"description" yaml:"description"`
103-
Default string `json:"default" yaml:"default"`
104-
}
105-
106111
type EntryName struct {
107112
Description string `json:"description" yaml:"description"`
108113
Length *EntryNameLength `json:"length" yaml:"length"`
@@ -447,19 +452,6 @@ func generateXpathVariables(variables []xpathschema.Variable) map[string]*Locati
447452
return xpathVars
448453
}
449454

450-
func generateImportVariables(variables []xpathschema.Variable) map[string]*ImportVar {
451-
importVars := make(map[string]*ImportVar)
452-
for _, variable := range variables {
453-
entry := &ImportVar{
454-
Description: variable.Description,
455-
Default: variable.Default,
456-
}
457-
importVars[variable.Name] = entry
458-
}
459-
460-
return importVars
461-
}
462-
463455
func schemaToSpec(object object.Object) (*Normalization, error) {
464456
var resourceVariants []TerraformResourceVariant
465457
for _, elt := range object.TerraformConfig.ResourceVariants {
@@ -478,7 +470,6 @@ func schemaToSpec(object object.Object) (*Normalization, error) {
478470
PluralName: object.TerraformConfig.PluralName,
479471
},
480472
Locations: make(map[string]*Location),
481-
Imports: make(map[string]*Import),
482473
GoSdkPath: object.GoSdkConfig.Package,
483474
XpathSuffix: object.XpathSuffix,
484475
Version: object.Version,
@@ -560,39 +551,52 @@ func schemaToSpec(object object.Object) (*Normalization, error) {
560551

561552
}
562553

563-
imports := make(map[string]*Import, len(object.Imports))
554+
var imports []Import
564555
for _, elt := range object.Imports {
565-
var xpath []string
566-
567-
schemaXpathVars := make(map[string]xpathschema.Variable)
568-
for _, xpathVariable := range elt.Xpath.Variables {
569-
schemaXpathVars[xpathVariable.Name] = xpathVariable
570-
}
571-
572-
for _, element := range elt.Xpath.Elements {
573-
var eltEntry string
574-
if xpathVar, ok := schemaXpathVars[element[1:]]; ok {
575-
if xpathVar.Type == "entry" {
576-
eltEntry = fmt.Sprintf("{{ Entry %s }}", elt.Name)
577-
} else if xpathVar.Type == "object" {
578-
eltEntry = fmt.Sprintf("{{ Object %s }}", elt.Name)
556+
locations := make(map[string]ImportLocation, len(elt.Locations))
557+
for _, location := range elt.Locations {
558+
schemaXpathVars := make(map[string]xpathschema.Variable, len(location.Xpath.Variables))
559+
xpathVars := make(map[string]ImportXpathVariable, len(location.Xpath.Variables))
560+
for _, xpathVariable := range location.Xpath.Variables {
561+
schemaXpathVars[xpathVariable.Name] = xpathVariable
562+
xpathVars[xpathVariable.Name] = ImportXpathVariable{
563+
Name: &NameVariant{
564+
Underscore: naming.Underscore("", xpathVariable.Name, ""),
565+
CamelCase: naming.CamelCase("", xpathVariable.Name, "", true),
566+
LowerCamelCase: naming.CamelCase("", xpathVariable.Name, "", false),
567+
},
568+
Description: xpathVariable.Description,
569+
Default: xpathVariable.Default,
579570
}
580-
} else {
581-
eltEntry = element
582571
}
583-
xpath = append(xpath, eltEntry)
584-
}
585572

586-
importVariables := generateImportVariables(elt.Xpath.Variables)
587-
if len(importVariables) == 0 {
588-
importVariables = nil
589-
}
573+
var xpath []string
574+
xpath = append(xpath, location.Xpath.Elements...)
590575

591-
imports[elt.Name] = &Import{
592-
Xpath: xpath,
593-
Vars: importVariables,
594-
OnlyForParams: elt.OnlyForParams,
576+
locations[location.Name] = ImportLocation{
577+
Name: &NameVariant{
578+
Underscore: naming.Underscore("", location.Name, ""),
579+
CamelCase: naming.CamelCase("", location.Name, "", true),
580+
LowerCamelCase: naming.CamelCase("", location.Name, "", false),
581+
},
582+
XpathVariables: xpathVars,
583+
XpathElements: xpath,
584+
}
595585
}
586+
587+
imports = append(imports, Import{
588+
Type: &NameVariant{
589+
Underscore: naming.Underscore("", elt.Type, ""),
590+
CamelCase: naming.CamelCase("", elt.Type, "", true),
591+
LowerCamelCase: naming.CamelCase("", elt.Type, "", false),
592+
},
593+
Variant: &NameVariant{
594+
Underscore: naming.Underscore("", elt.Variant, ""),
595+
CamelCase: naming.CamelCase("", elt.Variant, "", true),
596+
LowerCamelCase: naming.CamelCase("", elt.Variant, "", false),
597+
},
598+
Locations: locations,
599+
})
596600
}
597601

598602
if len(imports) > 0 {
@@ -660,11 +664,6 @@ func ParseSpec(input []byte) (*Normalization, error) {
660664
return nil, err
661665
}
662666

663-
err = spec.AddNameVariantsForImports()
664-
if err != nil {
665-
return nil, err
666-
}
667-
668667
err = spec.AddNameVariantsForParams()
669668
if err != nil {
670669
return nil, err
@@ -704,27 +703,6 @@ func (spec *Normalization) AddNameVariantsForLocation() error {
704703
return nil
705704
}
706705

707-
// AddNameVariantsForImports add name variants for imports (under_score and CamelCase).
708-
func (spec *Normalization) AddNameVariantsForImports() error {
709-
for key, imp := range spec.Imports {
710-
imp.Name = &NameVariant{
711-
Underscore: naming.Underscore("", key, ""),
712-
CamelCase: naming.CamelCase("", key, "", true),
713-
LowerCamelCase: naming.CamelCase("", key, "", false),
714-
}
715-
716-
for subkey, variable := range imp.Vars {
717-
variable.Name = &NameVariant{
718-
Underscore: naming.Underscore("", subkey, ""),
719-
CamelCase: naming.CamelCase("", subkey, "", true),
720-
LowerCamelCase: naming.CamelCase("", subkey, "", false),
721-
}
722-
}
723-
}
724-
725-
return nil
726-
}
727-
728706
// AddNameVariantsForParams recursively add name variants for params for nested specs.
729707
func AddNameVariantsForParams(name string, param *SpecParam) error {
730708
param.Name = &NameVariant{

pkg/schema/imports/import.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package imports
22

3-
import "github.com/paloaltonetworks/pan-os-codegen/pkg/schema/xpath"
3+
import (
4+
"github.com/paloaltonetworks/pan-os-codegen/pkg/schema/location"
5+
)
46

57
type Import struct {
6-
Name string `yaml:"name"`
7-
Xpath xpathschema.Xpath `yaml:"xpath"`
8-
OnlyForParams []string `yaml:"only_for_params"`
8+
Variant string `yaml:"variant"`
9+
Type string `yaml:"type"`
10+
Locations []location.Location `yaml:"locations"`
911
}

pkg/schema/location/location.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const (
1414

1515
type Location struct {
1616
Name string `yaml:"name"`
17+
ReadOnly bool `yaml:"read_only"`
1718
Description string `yaml:"description"`
1819
Devices []Device `yaml:"devices"`
1920
Xpath xpathschema.Xpath `yaml:"xpath"`

pkg/translate/imports.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ func RenderImports(templateTypes ...string) (string, error) {
4747
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/version", "")
4848
case "template":
4949
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/panorama/template", "")
50+
case "imports":
51+
manager.AddStandardImport("encoding/xml", "")
5052
}
5153
}
5254

0 commit comments

Comments
 (0)