Skip to content

Commit bf4aafa

Browse files
committed
feat(codegen): Add support for child specs
1 parent 501201a commit bf4aafa

File tree

109 files changed

+5617
-5233
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+5617
-5233
lines changed

assets/pango/errors/panos.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88
)
99

10+
var ObjectExists = stderr.New("object already exists")
1011
var InvalidFilterError = stderr.New("filter is improperly formatted")
1112
var NameNotSpecifiedError = stderr.New("name is not specified")
1213
var NoLocationSpecifiedError = stderr.New("no location specified")
@@ -151,3 +152,17 @@ func (e *errorCheck) CodeError() string {
151152
return fmt.Sprintf("(%d) Unknown failure code, operation failed", e.Code)
152153
}
153154
}
155+
156+
type InvalidXpathComponentError struct {
157+
Message string
158+
}
159+
160+
func (o InvalidXpathComponentError) Error() string {
161+
return o.Message
162+
}
163+
164+
func NewInvalidXpathComponentError(message string) *InvalidXpathComponentError {
165+
return &InvalidXpathComponentError{
166+
Message: message,
167+
}
168+
}

assets/pango/example/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,8 +670,8 @@ func checkSecurityPolicyRules(c *pango.Client, ctx context.Context) {
670670
log.Printf("Security policy rule '%s:%s' with description '%s' read by id", *securityPolicyRuleReply.Uuid, securityPolicyRuleReply.Name, *securityPolicyRuleReply.Description)
671671

672672
// SECURITY POLICY RULE - UPDATE 2
673-
securityPolicyRuleEntry.Description = util.String("changed by id description")
674-
securityPolicyRuleReply, err = securityPolicyRuleApi.UpdateById(ctx, *securityPolicyRuleLocation, securityPolicyRuleEntry, *securityPolicyRuleReply.Uuid)
673+
securityPolicyRuleEntry.Description = util.String("changed by name description")
674+
securityPolicyRuleReply, err = securityPolicyRuleApi.Update(ctx, *securityPolicyRuleLocation, securityPolicyRuleEntry, securityPolicyRuleReply.Name)
675675
if err != nil {
676676
log.Printf("Failed to update security policy rule: %s", err)
677677
return

assets/pango/util/util.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func AsXpath(i interface{}) string {
109109
}
110110

111111
// AsEntryXpath returns the given values as an entry xpath segment.
112-
func AsEntryXpath(vals []string) string {
112+
func AsEntryXpath(vals ...string) string {
113113
if len(vals) == 0 || (len(vals) == 1 && vals[0] == "") {
114114
return "entry"
115115
}
@@ -160,18 +160,18 @@ func TemplateXpathPrefix(tmpl, ts string) []string {
160160
return []string{
161161
"config",
162162
"devices",
163-
AsEntryXpath([]string{"localhost.localdomain"}),
163+
AsEntryXpath("localhost.localdomain"),
164164
"template",
165-
AsEntryXpath([]string{tmpl}),
165+
AsEntryXpath(tmpl),
166166
}
167167
}
168168

169169
return []string{
170170
"config",
171171
"devices",
172-
AsEntryXpath([]string{"localhost.localdomain"}),
172+
AsEntryXpath("localhost.localdomain"),
173173
"template-stack",
174-
AsEntryXpath([]string{ts}),
174+
AsEntryXpath(ts),
175175
}
176176
}
177177

@@ -185,9 +185,9 @@ func DeviceGroupXpathPrefix(dg string) []string {
185185
return []string{
186186
"config",
187187
"devices",
188-
AsEntryXpath([]string{"localhost.localdomain"}),
188+
AsEntryXpath("localhost.localdomain"),
189189
"device-group",
190-
AsEntryXpath([]string{dg}),
190+
AsEntryXpath(dg),
191191
}
192192
}
193193

@@ -202,9 +202,9 @@ func VsysXpathPrefix(vsys string) []string {
202202
return []string{
203203
"config",
204204
"devices",
205-
AsEntryXpath([]string{"localhost.localdomain"}),
205+
AsEntryXpath("localhost.localdomain"),
206206
"vsys",
207-
AsEntryXpath([]string{vsys}),
207+
AsEntryXpath(vsys),
208208
}
209209
}
210210

assets/pango/util/util_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ func BenchmarkAsXpath(b *testing.B) {
106106
p := []string{
107107
"config",
108108
"devices",
109-
AsEntryXpath([]string{"localhost.localdomain"}),
109+
AsEntryXpath("localhost.localdomain"),
110110
"vsys",
111-
AsEntryXpath([]string{"vsys1"}),
111+
AsEntryXpath("vsys1"),
112112
"import",
113113
"network",
114114
}
@@ -124,7 +124,7 @@ func BenchmarkAsEntryXpathMultiple(b *testing.B) {
124124
b.ResetTimer()
125125

126126
for i := 0; i < b.N; i++ {
127-
_ = AsEntryXpath(v)
127+
_ = AsEntryXpath(v...)
128128
}
129129
}
130130

@@ -159,7 +159,7 @@ func TestAsEntryXpath(t *testing.T) {
159159

160160
for _, tc := range testCases {
161161
t.Run(tc.r, func(t *testing.T) {
162-
if AsEntryXpath(tc.v) != tc.r {
162+
if AsEntryXpath(tc.v...) != tc.r {
163163
t.Fail()
164164
}
165165
})

assets/terraform/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ require (
1313
github.com/hashicorp/terraform-plugin-testing v1.12.0
1414
github.com/onsi/ginkgo/v2 v2.22.2
1515
github.com/onsi/gomega v1.36.2
16-
golang.org/x/sync v0.13.0
1716
)
1817

1918
require (
@@ -63,6 +62,7 @@ require (
6362
golang.org/x/crypto v0.37.0 // indirect
6463
golang.org/x/mod v0.24.0 // indirect
6564
golang.org/x/net v0.37.0 // indirect
65+
golang.org/x/sync v0.13.0 // indirect
6666
golang.org/x/sys v0.32.0 // indirect
6767
golang.org/x/text v0.24.0 // indirect
6868
golang.org/x/tools v0.28.0 // indirect

assets/terraform/internal/manager/config.go

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
sdkerrors "github.com/PaloAltoNetworks/pango/errors"
1010
"github.com/PaloAltoNetworks/pango/util"
1111
"github.com/PaloAltoNetworks/pango/version"
12+
"github.com/PaloAltoNetworks/pango/xmlapi"
1213
)
1314

1415
type TFConfigObject[E any] interface {
@@ -18,13 +19,14 @@ type TFConfigObject[E any] interface {
1819

1920
type SDKConfigService[C any, L ConfigLocation] interface {
2021
Create(context.Context, L, C) (C, error)
21-
Update(context.Context, L, C) (C, error)
22-
Read(context.Context, L, string) (C, error)
22+
CreateWithXpath(context.Context, string, C) error
23+
UpdateWithXpath(context.Context, string, C) error
24+
ReadWithXpath(context.Context, string, string) (C, error)
2325
Delete(context.Context, L, C) error
2426
}
2527

2628
type ConfigLocation interface {
27-
Xpath(version.Number) ([]string, error)
29+
XpathWithComponents(version.Number, ...string) ([]string, error)
2830
}
2931

3032
type ConfigObjectManager[C any, L ConfigLocation, S SDKConfigService[C, L]] struct {
@@ -41,16 +43,41 @@ func NewConfigObjectManager[C any, L ConfigLocation, S SDKConfigService[C, L]](c
4143
}
4244
}
4345

44-
func (o *ConfigObjectManager[C, L, S]) Create(ctx context.Context, location L, config C) (C, error) {
45-
return o.service.Create(ctx, location, config)
46+
func (o *ConfigObjectManager[C, L, S]) Create(ctx context.Context, location L, components []string, config C) (C, error) {
47+
xpath, err := location.XpathWithComponents(o.client.Versioning(), components...)
48+
if err != nil {
49+
return *new(C), err
50+
}
51+
52+
err = o.service.CreateWithXpath(ctx, util.AsXpath(xpath[:len(xpath)-1]), config)
53+
if err != nil {
54+
return *new(C), err
55+
}
56+
57+
return o.service.ReadWithXpath(ctx, util.AsXpath(xpath), "get")
4658
}
4759

48-
func (o *ConfigObjectManager[C, L, S]) Update(ctx context.Context, location L, config C) (C, error) {
49-
return o.service.Update(ctx, location, config)
60+
func (o *ConfigObjectManager[C, L, S]) Update(ctx context.Context, location L, components []string, config C) (C, error) {
61+
xpath, err := location.XpathWithComponents(o.client.Versioning(), components...)
62+
if err != nil {
63+
return *new(C), err
64+
}
65+
66+
err = o.service.UpdateWithXpath(ctx, util.AsXpath(xpath), config)
67+
if err != nil {
68+
return *new(C), err
69+
}
70+
71+
return o.service.ReadWithXpath(ctx, util.AsXpath(xpath), "get")
5072
}
5173

52-
func (o *ConfigObjectManager[C, L, S]) Read(ctx context.Context, location L) (C, error) {
53-
obj, err := o.service.Read(ctx, location, "get")
74+
func (o *ConfigObjectManager[C, L, S]) Read(ctx context.Context, location L, components []string) (C, error) {
75+
xpath, err := location.XpathWithComponents(o.client.Versioning(), components...)
76+
if err != nil {
77+
return *new(C), err
78+
}
79+
80+
obj, err := o.service.ReadWithXpath(ctx, util.AsXpath(xpath), "get")
5481
if err != nil && sdkerrors.IsObjectNotFound(err) {
5582
return obj, ErrObjectNotFound
5683
}
@@ -59,5 +86,23 @@ func (o *ConfigObjectManager[C, L, S]) Read(ctx context.Context, location L) (C,
5986
}
6087

6188
func (o *ConfigObjectManager[C, L, S]) Delete(ctx context.Context, location L, config C) error {
62-
return o.service.Delete(ctx, location, config)
89+
deletes := xmlapi.NewChunkedMultiConfig(1, 1)
90+
91+
xpath, err := location.XpathWithComponents(o.client.Versioning())
92+
if err != nil {
93+
return err
94+
}
95+
96+
deletes.Add(&xmlapi.Config{
97+
Action: "delete",
98+
Xpath: util.AsXpath(xpath),
99+
Target: o.client.GetTarget(),
100+
})
101+
102+
_, _, _, err = o.client.MultiConfig(ctx, deletes, false, nil)
103+
if err != nil {
104+
return &Error{err: err, message: "sdk error while deleting"}
105+
}
106+
107+
return nil
63108
}

0 commit comments

Comments
 (0)