Skip to content

Commit b584add

Browse files
committed
Add device group parent specification and custom codegen
1 parent 93c8878 commit b584add

File tree

11 files changed

+292
-23
lines changed

11 files changed

+292
-23
lines changed

pkg/commands/codegen/codegen.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ func (c *Command) Execute() error {
119119
resourceTyp = properties.ResourceEntry
120120
case properties.TerraformResourceUuid:
121121
resourceTyp = properties.ResourceUuid
122+
case properties.TerraformResourceCustom:
123+
resourceTyp = properties.ResourceCustom
122124
case properties.TerraformResourceConfig:
123125
panic("missing implementation for config type resources")
124126
}
@@ -140,6 +142,8 @@ func (c *Command) Execute() error {
140142
resourceTyp = properties.ResourceEntryPlural
141143
case properties.TerraformResourceUuid:
142144
resourceTyp = properties.ResourceUuidPlural
145+
case properties.TerraformResourceCustom:
146+
resourceTyp = properties.ResourceCustom
143147
case properties.TerraformResourceConfig:
144148
panic("missing implementation for config type resources")
145149
}
@@ -153,7 +157,7 @@ func (c *Command) Execute() error {
153157
resourceList = append(resourceList, resources...)
154158
dataSourceList = append(dataSourceList, dataSources...)
155159
}
156-
} else if c.commandType == properties.CommandTypeSDK {
160+
} else if c.commandType == properties.CommandTypeSDK && !spec.GoSdkSkip {
157161
generator := generate.NewCreator(config.Output.GoSdk, c.templatePath, spec)
158162
if err = generator.RenderTemplate(); err != nil {
159163
return fmt.Errorf("error rendering %s - %s", specPath, err)

pkg/generate/generator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (c *Creator) RenderTerraformProviderFile(spec *properties.Normalization, ty
6464
name = fmt.Sprintf("%s_%s", spec.TerraformProviderConfig.Suffix, spec.TerraformProviderConfig.PluralName)
6565
case properties.ResourceEntryPlural:
6666
name = spec.TerraformProviderConfig.PluralSuffix
67-
case properties.ResourceEntry, properties.ResourceUuid:
67+
case properties.ResourceEntry, properties.ResourceUuid, properties.ResourceCustom:
6868
name = spec.Name
6969
}
7070

@@ -95,7 +95,7 @@ func (c *Creator) RenderTerraformProviderFile(spec *properties.Normalization, ty
9595
case properties.ResourceEntryPlural:
9696
name = spec.TerraformProviderConfig.PluralSuffix
9797
filePath = c.createTerraformProviderFilePath(name)
98-
case properties.ResourceEntry, properties.ResourceUuid:
98+
case properties.ResourceEntry, properties.ResourceUuid, properties.ResourceCustom:
9999
filePath = c.createTerraformProviderFilePath(spec.TerraformProviderConfig.Suffix)
100100
}
101101

pkg/properties/normalized.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
type Normalization struct {
1919
Name string `json:"name" yaml:"name"`
2020
TerraformProviderConfig TerraformProviderConfig `json:"terraform_provider_config" yaml:"terraform_provider_config"`
21+
GoSdkSkip bool `json:"go_sdk_skip" yaml:"go_sdk_skip"`
2122
GoSdkPath []string `json:"go_sdk_path" yaml:"go_sdk_path"`
2223
XpathSuffix []string `json:"xpath_suffix" yaml:"xpath_suffix"`
2324
Locations map[string]*Location `json:"locations" yaml:"locations"`
@@ -53,6 +54,7 @@ const (
5354
TerraformResourceEntry TerraformResourceType = "entry"
5455
TerraformResourceUuid TerraformResourceType = "uuid"
5556
TerraformResourceConfig TerraformResourceType = "config"
57+
TerraformResourceCustom TerraformResourceType = "custom"
5658
)
5759

5860
type TerraformResourceVariant string
@@ -67,6 +69,7 @@ type TerraformProviderConfig struct {
6769
SkipDatasource bool `json:"skip_datasource" yaml:"skip_datasource"`
6870
SkipDatasourceListing bool `json:"skip_datasource_listing" yaml:"skip_datasource_listing"`
6971
ResourceType TerraformResourceType `json:"resource_type" yaml:"resource_type"`
72+
CustomFuncs map[string]string `json:"custom_functions" yaml:"custom_functions"`
7073
ResourceVariants []TerraformResourceVariant `json:"resource_variants" yaml:"resource_variants"`
7174
Suffix string `json:"suffix" yaml:"suffix"`
7275
PluralSuffix string `json:"plural_suffix" yaml:"plural_suffix"`
@@ -473,12 +476,14 @@ func schemaToSpec(object object.Object) (*Normalization, error) {
473476
SkipDatasource: object.TerraformConfig.SkipDatasource,
474477
SkipDatasourceListing: object.TerraformConfig.SkipdatasourceListing,
475478
ResourceType: TerraformResourceType(object.TerraformConfig.ResourceType),
479+
CustomFuncs: object.TerraformConfig.CustomFunctions,
476480
ResourceVariants: resourceVariants,
477481
Suffix: object.TerraformConfig.Suffix,
478482
PluralSuffix: object.TerraformConfig.PluralSuffix,
479483
PluralName: object.TerraformConfig.PluralName,
480484
},
481485
Locations: make(map[string]*Location),
486+
GoSdkSkip: object.GoSdkConfig.Skip,
482487
GoSdkPath: object.GoSdkConfig.Package,
483488
XpathSuffix: object.XpathSuffix,
484489
Version: object.Version,

pkg/properties/resourcetype.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ type ResourceType int
44

55
const (
66
ResourceEntry ResourceType = iota
7+
ResourceCustom ResourceType = iota
78
ResourceEntryPlural ResourceType = iota
89
ResourceUuid ResourceType = iota
910
ResourceUuidPlural ResourceType = iota

pkg/schema/object/object.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const (
1515
TerraformResourceEntry TerraformResourceType = "entry"
1616
TerraformResourceUuid TerraformResourceType = "uuid"
1717
TerraformResourceConfig TerraformResourceType = "config"
18+
TerraformResourceCustom TerraformResourceType = "custom"
1819
)
1920

2021
type TerraformResourceVariant string
@@ -29,14 +30,16 @@ type TerraformConfig struct {
2930
SkipDatasource bool `yaml:"skip_datasource"`
3031
SkipdatasourceListing bool `yaml:"skip_datasource_listing"`
3132
ResourceType TerraformResourceType `yaml:"resource_type"`
33+
CustomFunctions map[string]string `yaml:"custom_functions"`
3234
ResourceVariants []TerraformResourceVariant `yaml:"resource_variants"`
3335
Suffix string `yaml:"suffix"`
3436
PluralSuffix string `yaml:"plural_suffix"`
3537
PluralName string `yaml:"plural_name"`
3638
}
3739

3840
type GoSdkConfig struct {
39-
Package []string
41+
Skip bool `yaml:"skip"`
42+
Package []string `yaml:"package"`
4043
}
4144

4245
type Entry struct {
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package terraform_provider
2+
3+
const deviceGroupParentImports = `
4+
import (
5+
"encoding/xml"
6+
"github.com/PaloAltoNetworks/pango/xmlapi"
7+
)
8+
`
9+
10+
const deviceGroupParentCommon = `
11+
var _ = tflog.Warn
12+
type _ = diag.Diagnostics
13+
type dgpReq struct {
14+
XMLName xml.Name ` + "`" + `xml:"show"` + "`" + `
15+
Cmd string ` + "`" + `xml:"dg-hierarchy"` + "`" + `
16+
}
17+
18+
type dgpResp struct {
19+
Result *dgHierarchy ` + "`" + `xml:"result>dg-hierarchy"` + "`" + `
20+
}
21+
22+
func (o *dgpResp) results() map[string]string {
23+
ans := make(map[string]string)
24+
25+
if o.Result != nil {
26+
for _, v := range o.Result.Info {
27+
ans[v.Name] = ""
28+
v.results(ans)
29+
}
30+
}
31+
32+
return ans
33+
}
34+
35+
type dgHierarchy struct {
36+
Info []dghInfo ` + "`" + `xml:"dg"` + "`" + `
37+
}
38+
39+
type dghInfo struct {
40+
Name string ` + "`" + `xml:"name,attr"` + "`" + `
41+
Children []dghInfo ` + "`" + `xml:"dg"` + "`" + `
42+
}
43+
44+
func (o *dghInfo) results(ans map[string]string) {
45+
for _, v := range o.Children {
46+
ans[v.Name] = o.Name
47+
v.results(ans)
48+
}
49+
}
50+
`
51+
52+
const deviceGroupParentDataSourceRead = `
53+
var state DeviceGroupParentResourceModel
54+
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
55+
if resp.Diagnostics.HasError() {
56+
return
57+
}
58+
59+
cmd := &xmlapi.Op{
60+
Command: dgpReq{},
61+
}
62+
63+
var ans dgpResp
64+
if _, _, err := o.client.Communicate(ctx, cmd, false, &ans); err != nil {
65+
resp.Diagnostics.AddError("Failed to query for device group parents", err.Error())
66+
return
67+
}
68+
69+
70+
hierarchy := ans.results()
71+
72+
name := state.DeviceGroup.ValueString()
73+
parent, ok := hierarchy[name]
74+
if !ok {
75+
state.Parent = types.StringValue("")
76+
} else {
77+
state.Parent = types.StringValue(parent)
78+
}
79+
80+
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
81+
`
82+
83+
const deviceGroupParentResourceRead = `
84+
var state DeviceGroupParentResourceModel
85+
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
86+
if resp.Diagnostics.HasError() {
87+
return
88+
}
89+
90+
cmd := &xmlapi.Op{
91+
Command: dgpReq{},
92+
}
93+
94+
var ans dgpResp
95+
if _, _, err := o.client.Communicate(ctx, cmd, false, &ans); err != nil {
96+
resp.Diagnostics.AddError("Failed to query for device group parents", err.Error())
97+
return
98+
}
99+
100+
hierarchy := ans.results()
101+
102+
name := state.DeviceGroup.ValueString()
103+
parent, ok := hierarchy[name]
104+
if !ok {
105+
state.Parent = types.StringValue("")
106+
} else {
107+
state.Parent = types.StringValue(parent)
108+
}
109+
110+
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
111+
`
112+
113+
const deviceGroupParentResourceCreate = ``
114+
const deviceGroupParentResourceUpdate = ``
115+
const deviceGroupParentResourceDelete = ``

0 commit comments

Comments
 (0)