Skip to content

Commit

Permalink
Update SDK signatures to align them with the generic code
Browse files Browse the repository at this point in the history
  • Loading branch information
kklimonda-cl committed Sep 3, 2024
1 parent a01a121 commit c3ab00a
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 51 deletions.
7 changes: 7 additions & 0 deletions assets/pango/util/location.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package util

import "github.com/PaloAltoNetworks/pango/version"

type ILocation interface {
XpathPrefix(version.Number) ([]string, error)
}
5 changes: 5 additions & 0 deletions pkg/properties/normalized.go
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,11 @@ func (spec *Normalization) HasEntryName() bool {
return spec.Entry != nil
}

func (spec *Normalization) HasEntryUuid() bool {
_, found := spec.Spec.Params["uuid"]
return found
}

func (spec *Normalization) HasEncryptedResources() bool {
for _, param := range spec.Spec.Params {
if param.HasEncryptedResources() {
Expand Down
12 changes: 4 additions & 8 deletions pkg/translate/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,8 @@ func addNameAsParamForNestedSpec(parent []string, nestedSpecs map[string]*proper
}

const importLocationStructTmpl = `
type ILocation interface {
xpath(version.Number) ([]string, error)
}
type ImportLocation interface {
XpathForLocation(version.Number, ILocation) ([]string, error)
XpathForLocation(version.Number, util.ILocation) ([]string, error)
MarshalPangoXML([]string) (string, error)
UnmarshalPangoXML([]byte) ([]string, error)
}
Expand Down Expand Up @@ -131,8 +127,8 @@ func New{{ $typeName }}(spec {{ $typeName }}Spec) *{{ $topType }} {
}
}
func (o *{{ $typeName }}) XpathForLocation(vn version.Number, loc ILocation) ([]string, error) {
ans, err := loc.xpath(vn)
func (o *{{ $typeName }}) XpathForLocation(vn version.Number, loc util.ILocation) ([]string, error) {
ans, err := loc.XpathPrefix(vn)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -218,7 +214,7 @@ func (o *{{ $topType }}) UnmarshalPangoXML(bytes []byte) ([]string, error) {
}
}
func (o *{{ $topType }}) XpathForLocation(vn version.Number, loc ILocation) ([]string, error) {
func (o *{{ $topType }}) XpathForLocation(vn version.Number, loc util.ILocation) ([]string, error) {
switch o.typ {
{{- range .Locations }}
case {{ $spec.Variant.LowerCamelCase }}{{ $spec.Type.CamelCase }}{{ .Name.CamelCase }}:
Expand Down
7 changes: 7 additions & 0 deletions pkg/translate/terraform_provider/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@ func New{{ resourceStructName }}() resource.Resource {
type {{ resourceStructName }} struct {
client *pango.Client
{{- if IsEntry }}
manager sdkmanager.EntryObjectManager[*{{ resourceSDKName }}.Entry, {{ resourceSDKName }}.Location, *{{ resourceSDKName }}.Service]
{{- else if IsUuid }}
manager sdkmanager.UuidObjectManager[*{{ resourceSDKName }}.Entry, {{ resourceSDKName }}.Location, *{{ resourceSDKName }}.Service]
{{- else if IsConfig }}
manager sdkmanager.ConfigObjectManager[*{{ resourceSDKName }}.Config, {{ resourceSDKName }}.Location, *{{ resourceSDKName }}.Service]
{{- end }}
}
{{- if not GoSDKSkipped }}
Expand Down
5 changes: 5 additions & 0 deletions pkg/translate/terraform_provider/terraform_provider_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ func (g *GenerateTerraformProvider) GenerateTerraformResource(resourceTyp proper

funcMap := template.FuncMap{
"GoSDKSkipped": func() bool { return spec.GoSdkSkip },
"IsEntry": func() bool { return spec.HasEntryName() && !spec.HasEntryUuid() },
"IsUuid": func() bool { return spec.HasEntryUuid() },
"IsConfig": func() bool { return !spec.HasEntryName() && !spec.HasEntryUuid() },
"resourceSDKName": func() string { return names.PackageName },
"HasPosition": func() bool { return hasPosition },
"metaName": func() string { return names.MetaName },
"structName": func() string { return names.StructName },
Expand Down Expand Up @@ -166,6 +170,7 @@ func (g *GenerateTerraformProvider) GenerateTerraformResource(resourceTyp proper
if !spec.TerraformProviderConfig.SkipResource {
terraformProvider.ImportManager.AddStandardImport("context", "")
terraformProvider.ImportManager.AddSdkImport("github.com/PaloAltoNetworks/pango", "")
terraformProvider.ImportManager.AddOtherImport("github.com/PaloAltoNetworks/terraform-provider-panos/internal/manager", "sdkmanager")

// entry or uuid style resource
if spec.Entry != nil {
Expand Down
8 changes: 4 additions & 4 deletions templates/sdk/config.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
}

{{- range $version := .SupportedVersions }}
func specifyConfig{{createGoSuffixFromVersion $version}}(o Config) (any, error) {
func specifyConfig{{createGoSuffixFromVersion $version}}(o *Config) (any, error) {
config := configXml{{createGoSuffixFromVersion $version}}{}

{{- range $_, $param := $.Spec.Params}}
Expand All @@ -133,10 +133,10 @@
{{- end}}

{{- range $version := .SupportedVersions }}
func (c *configXmlContainer{{createGoSuffixFromVersion $version}}) Normalize() ([]Config, error) {
configList := make([]Config, 0, len(c.Answer))
func (c *configXmlContainer{{createGoSuffixFromVersion $version}}) Normalize() ([]*Config, error) {
configList := make([]*Config, 0, len(c.Answer))
for _, o := range c.Answer {
config := Config{
config := &Config{
Misc: make(map[string][]generic.Xml),
}
{{- range $_, $param := $.Spec.Params}}
Expand Down
26 changes: 22 additions & 4 deletions templates/sdk/entry.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
}

{{ range $version := .SupportedVersions }}
func specifyEntry{{createGoSuffixFromVersion $version}}(o Entry) (any, error) {
func specifyEntry{{createGoSuffixFromVersion $version}}(o *Entry) (any, error) {
entry := entryXml{{createGoSuffixFromVersion $version}}{}

entry.Name = o.Name
Expand All @@ -182,10 +182,10 @@
{{- end}}

{{- range $version := .SupportedVersions }}
func (c *entryXmlContainer{{createGoSuffixFromVersion $version}}) Normalize() ([]Entry, error) {
entryList := make([]Entry, 0, len(c.Answer))
func (c *entryXmlContainer{{createGoSuffixFromVersion $version}}) Normalize() ([]*Entry, error) {
entryList := make([]*Entry, 0, len(c.Answer))
for _, o := range c.Answer {
entry := Entry{
entry := &Entry{
Misc: make(map[string][]generic.Xml),
}
entry.Name = o.Name
Expand Down Expand Up @@ -228,4 +228,22 @@
}

{{nestedSpecMatchesFunction $.Spec}}

func (o *Entry) EntryName() string {
return o.Name
}

func (o *Entry) SetEntryName(name string) {
o.Name = name
}

{{- if .Spec.Params.uuid}}
func (o *Entry) EntryUuid() *string {
return o.Uuid
}

func (o *Entry) SetEntryUuid(uuid *string) {
o.Uuid = uuid
}
{{- end }}
{{- end}}
8 changes: 4 additions & 4 deletions templates/sdk/interfaces.tmpl
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package {{packageName .GoSdkPath}}

{{- if .Entry}}
type Specifier func(Entry) (any, error)
type Specifier func(*Entry) (any, error)
{{- else}}
type Specifier func(Config) (any, error)
type Specifier func(*Config) (any, error)
{{- end}}

type Normalizer interface {
{{- if .Entry}}
Normalize() ([]Entry, error)
Normalize() ([]*Entry, error)
{{- else}}
Normalize() ([]Config, error)
Normalize() ([]*Config, error)
{{- end}}
}
6 changes: 3 additions & 3 deletions templates/sdk/location.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ return fmt.Errorf("multiple paths specified: only one should be specified")
return nil
}

func (o Location) xpath(vn version.Number) ([]string, error) {
func (o Location) XpathPrefix(vn version.Number) ([]string, error) {

var ans []string

Expand Down Expand Up @@ -108,7 +108,7 @@ return ans, nil
func (o Location) Xpath(vn version.Number) ([]string, error) {
{{- end}}

ans, err := o.xpath(vn)
ans, err := o.XpathPrefix(vn)
if err != nil {
return nil, err
}
Expand All @@ -124,7 +124,7 @@ return ans, nil
{{- if .Entry}}
func (o Location) XpathWithUuid(vn version.Number, uuid string) ([]string, error) {

ans, err := o.xpath(vn)
ans, err := o.XpathPrefix(vn)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit c3ab00a

Please sign in to comment.