Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: E2E example with static files required to execute it #46

Merged
merged 164 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 163 commits
Commits
Show all changes
164 commits
Select commit Hold shift + click to select a range
6ea5f9d
add EntryXml struct, render all missing functions for entry.go, exten…
sebastianczech Mar 11, 2024
4d70add
start work on service.go
sebastianczech Mar 11, 2024
401a470
refactor names in template for entry.go
sebastianczech Mar 11, 2024
53084e3
add not_present and from_version to SpecParamProfile
sebastianczech Mar 11, 2024
c5ff1b4
current service is only prepared for entry
sebastianczech Mar 11, 2024
5da312c
clear service template
sebastianczech Mar 11, 2024
2e011a7
render entry.go only for specs with entry
sebastianczech Mar 11, 2024
8e301c0
Merge branch 'main' into render-entry
sebastianczech Mar 11, 2024
48db70c
create structs for nested specs
sebastianczech Mar 11, 2024
07ccc14
generate content of function SpecifyEntry for specification with nest…
sebastianczech Mar 12, 2024
60af14e
generate content of function Normalize for specification with nested …
sebastianczech Mar 12, 2024
917a301
add function comments
sebastianczech Mar 12, 2024
2939d86
replace all string concatenation using "+" by function fmt.Sprintf()
sebastianczech Mar 13, 2024
b51801b
for item with type object, render []string, which will contain all me…
sebastianczech Mar 13, 2024
6461db6
refactor NormalizeAssignment(), SpecifyEntryAssignment(), fix some of…
sebastianczech Mar 13, 2024
acbb1a6
new template to generate config.go
sebastianczech Mar 13, 2024
23626f1
extend function NormalizeAssignment() and SpecifyEntryAssignment(), w…
sebastianczech Mar 13, 2024
117d60c
fix tests
sebastianczech Mar 13, 2024
30bcb0d
extend template functions after tests for service.yaml
sebastianczech Mar 13, 2024
15ebab6
merge changes from render-entry
sebastianczech Mar 13, 2024
b5806d2
extend template functions
sebastianczech Mar 13, 2024
743ac40
apply changes after review (#1)
sebastianczech Mar 14, 2024
f0e47db
remove comments
sebastianczech Mar 14, 2024
a0edfa2
use fmt.Errorf to return errors
sebastianczech Mar 14, 2024
e55a5be
apply changes after review (#2)
sebastianczech Mar 14, 2024
f6e4f92
refactor SpecMatchesFunction()
sebastianczech Mar 14, 2024
b22a504
merge changes from render-entry
sebastianczech Mar 14, 2024
4df52fa
refactor funcs.go
sebastianczech Mar 15, 2024
0ff2d59
refactor structs.go
sebastianczech Mar 15, 2024
cd02ede
Merge branch 'refs/heads/render-entry' into render-config
sebastianczech Mar 15, 2024
25bee3f
merge changes from render-entry branch
sebastianczech Mar 15, 2024
75ffc83
merge main
sebastianczech Mar 15, 2024
68f3557
remove nested variables declaration, use the directly in assignments
sebastianczech Mar 18, 2024
d6c8db1
fix problems while using entry.go with E2E tests for address object
sebastianczech Mar 19, 2024
099abaf
fix problems with default type for param, which has spec
sebastianczech Mar 19, 2024
55288fa
restore nested declaration (need to rebuild that mechanism)
sebastianczech Mar 20, 2024
0f95f5d
rebuild mechanism of generating nested objects
sebastianczech Mar 20, 2024
17f0093
fix location template
sebastianczech Mar 20, 2024
f04943a
render 'Misc []generic.Xml `xml:",any"' for child objects (not only f…
sebastianczech Mar 22, 2024
0bddff2
render 'Misc []generic.Xml `xml:",any"' for child objects (not only f…
sebastianczech Mar 22, 2024
e5bcd38
set value for nested Misc
sebastianczech Mar 22, 2024
55e281a
fix tests after changes with Misc
sebastianczech Mar 22, 2024
ee53918
struct ending with Xml are no more public from now
sebastianczech Mar 22, 2024
d836de2
introduce function to detecting versions
sebastianczech Mar 22, 2024
73717c2
return unique versions in format required in template
sebastianczech Mar 22, 2024
36db8b4
render different structs names for different versions
sebastianczech Mar 22, 2024
2b5ff1a
improve rendering different structs names for different versions
sebastianczech Mar 22, 2024
ead76f9
Merge branch 'main' into render-config
sebastianczech Mar 25, 2024
4858269
Introduce version struct
sebastianczech Mar 25, 2024
2d55354
Continue work on versioning
sebastianczech Mar 25, 2024
7d5a7ee
Remove version package
sebastianczech Mar 25, 2024
b51ee85
Restore version package
sebastianczech Mar 25, 2024
e66256a
Check if param is supported in specific PAN-OS version
sebastianczech Mar 25, 2024
c3a9042
Extend function for checking if param is supported in specific PAN-OS…
sebastianczech Mar 25, 2024
c935c26
Use function paramSupportedInVersion in templates
sebastianczech Mar 25, 2024
54794c2
Do not render assignment for not supported param in specific version
sebastianczech Mar 25, 2024
e6c6700
Update template to use newer versions of functions
sebastianczech Mar 25, 2024
fa24222
Refactor code
sebastianczech Mar 25, 2024
d534d1c
Fix config template after tests for NTP
sebastianczech Mar 26, 2024
b0ac86c
Fix config template after tests for DNS
sebastianczech Mar 26, 2024
ae0300f
Remove CopyMiscFrom, SpecMatches from config template
sebastianczech Mar 26, 2024
2544a4b
Add comment for MarshalXML in config template
sebastianczech Mar 26, 2024
3dac047
Working SpecMatches() (not refactored)
sebastianczech Mar 27, 2024
a361d44
Refactor code to render SpecMatches()
sebastianczech Mar 27, 2024
accd7db
Fix versioning after more tests
sebastianczech Mar 27, 2024
b79779c
Fix Misc map[string][]generic.Xml
sebastianczech Mar 28, 2024
09d149b
Make specify functions internal (we have multiple versions of specify…
sebastianczech Mar 28, 2024
a40bf23
Remove CopyMiscFrom(), fix issues with nested & versioned params
sebastianczech Mar 29, 2024
35a14d5
Fixes in location.tmpl after tests while building service.go
sebastianczech Apr 2, 2024
3083d8d
Render service.go for entry and config
sebastianczech Apr 2, 2024
342e3d6
Static files to execute generated code + example code using generated…
sebastianczech Apr 2, 2024
8e4ecb0
Rename function XmlPathSuffixes()
sebastianczech Apr 2, 2024
7807e08
Merge branch 'render-service' into e2e-example
sebastianczech Apr 2, 2024
0235c2a
Fix problem with nested specs and versions
sebastianczech Apr 2, 2024
7c59b6b
Merge branch 'render-config' into render-service
sebastianczech Apr 2, 2024
ff9af54
Merge branch 'render-service' into e2e-example
sebastianczech Apr 2, 2024
d1f99a0
Change path for example code
sebastianczech Apr 2, 2024
52d26ef
Fix specs (type of items in the list)
sebastianczech Apr 2, 2024
110147d
Merge branch 'refs/heads/render-config' into render-service
sebastianczech Apr 2, 2024
d393f0d
Merge branch 'refs/heads/render-service' into e2e-example
sebastianczech Apr 2, 2024
491b5e3
Merge branch 'refs/heads/main' into render-service
sebastianczech Apr 2, 2024
f34f035
Merge branch 'refs/heads/render-service' into e2e-example
sebastianczech Apr 2, 2024
f5f54d3
Update README
sebastianczech Apr 2, 2024
fadf567
Update README
sebastianczech Apr 2, 2024
bdf0f7e
go.mod for assets
sebastianczech Apr 2, 2024
9078a21
Simplify usage of example code
sebastianczech Apr 2, 2024
ad7b989
add EntryXml struct, render all missing functions for entry.go, exten…
sebastianczech Mar 11, 2024
948fe24
start work on service.go
sebastianczech Mar 11, 2024
024e86b
refactor names in template for entry.go
sebastianczech Mar 11, 2024
9b1a666
add not_present and from_version to SpecParamProfile
sebastianczech Mar 11, 2024
5c148fc
current service is only prepared for entry
sebastianczech Mar 11, 2024
dd3d1db
clear service template
sebastianczech Mar 11, 2024
2fb79ed
render entry.go only for specs with entry
sebastianczech Mar 11, 2024
8372889
create structs for nested specs
sebastianczech Mar 11, 2024
d305093
generate content of function SpecifyEntry for specification with nest…
sebastianczech Mar 12, 2024
9bbe3e1
generate content of function Normalize for specification with nested …
sebastianczech Mar 12, 2024
2180a57
add function comments
sebastianczech Mar 12, 2024
f1f9015
replace all string concatenation using "+" by function fmt.Sprintf()
sebastianczech Mar 13, 2024
5966f1b
for item with type object, render []string, which will contain all me…
sebastianczech Mar 13, 2024
f7bb754
refactor NormalizeAssignment(), SpecifyEntryAssignment(), fix some of…
sebastianczech Mar 13, 2024
3949bb7
new template to generate config.go
sebastianczech Mar 13, 2024
6ae47fc
extend function NormalizeAssignment() and SpecifyEntryAssignment(), w…
sebastianczech Mar 13, 2024
802698c
fix tests
sebastianczech Mar 13, 2024
1468446
extend template functions after tests for service.yaml
sebastianczech Mar 13, 2024
dbeedb4
extend template functions
sebastianczech Mar 13, 2024
9ccdce1
apply changes after review (#1)
sebastianczech Mar 14, 2024
c40fbfd
remove comments
sebastianczech Mar 14, 2024
f3e7d56
use fmt.Errorf to return errors
sebastianczech Mar 14, 2024
e7fa93a
apply changes after review (#2)
sebastianczech Mar 14, 2024
b5e271e
refactor SpecMatchesFunction()
sebastianczech Mar 14, 2024
633940c
refactor funcs.go
sebastianczech Mar 15, 2024
57bec84
refactor structs.go
sebastianczech Mar 15, 2024
c9640fe
merge changes from render-entry branch
sebastianczech Mar 15, 2024
ed7ed72
remove nested variables declaration, use the directly in assignments
sebastianczech Mar 18, 2024
24f1f51
fix problems while using entry.go with E2E tests for address object
sebastianczech Mar 19, 2024
44db76f
fix problems with default type for param, which has spec
sebastianczech Mar 19, 2024
5a64220
restore nested declaration (need to rebuild that mechanism)
sebastianczech Mar 20, 2024
5ec29bb
rebuild mechanism of generating nested objects
sebastianczech Mar 20, 2024
4fd868a
fix location template
sebastianczech Mar 20, 2024
3fedad1
render 'Misc []generic.Xml `xml:",any"' for child objects (not only f…
sebastianczech Mar 22, 2024
e964878
render 'Misc []generic.Xml `xml:",any"' for child objects (not only f…
sebastianczech Mar 22, 2024
b399cf1
set value for nested Misc
sebastianczech Mar 22, 2024
c267fff
fix tests after changes with Misc
sebastianczech Mar 22, 2024
e11fdca
struct ending with Xml are no more public from now
sebastianczech Mar 22, 2024
fe77494
introduce function to detecting versions
sebastianczech Mar 22, 2024
2f16fb8
return unique versions in format required in template
sebastianczech Mar 22, 2024
a9331b0
render different structs names for different versions
sebastianczech Mar 22, 2024
3ab0edd
improve rendering different structs names for different versions
sebastianczech Mar 22, 2024
279b05a
Introduce version struct
sebastianczech Mar 25, 2024
61a505d
Continue work on versioning
sebastianczech Mar 25, 2024
10fc1ec
Remove version package
sebastianczech Mar 25, 2024
9a9099b
Restore version package
sebastianczech Mar 25, 2024
68cb7c5
Check if param is supported in specific PAN-OS version
sebastianczech Mar 25, 2024
2f86bd6
Extend function for checking if param is supported in specific PAN-OS…
sebastianczech Mar 25, 2024
6ca7bbf
Use function paramSupportedInVersion in templates
sebastianczech Mar 25, 2024
2423da7
Do not render assignment for not supported param in specific version
sebastianczech Mar 25, 2024
e49158a
Update template to use newer versions of functions
sebastianczech Mar 25, 2024
9ac4470
Refactor code
sebastianczech Mar 25, 2024
862bd4a
Fix config template after tests for NTP
sebastianczech Mar 26, 2024
f284c78
Fix config template after tests for DNS
sebastianczech Mar 26, 2024
337c2ed
Remove CopyMiscFrom, SpecMatches from config template
sebastianczech Mar 26, 2024
644db51
Add comment for MarshalXML in config template
sebastianczech Mar 26, 2024
9377a74
Working SpecMatches() (not refactored)
sebastianczech Mar 27, 2024
0877c79
Refactor code to render SpecMatches()
sebastianczech Mar 27, 2024
b78ffd0
Fix versioning after more tests
sebastianczech Mar 27, 2024
782d829
Fix Misc map[string][]generic.Xml
sebastianczech Mar 28, 2024
583b48d
Make specify functions internal (we have multiple versions of specify…
sebastianczech Mar 28, 2024
b331865
Remove CopyMiscFrom(), fix issues with nested & versioned params
sebastianczech Mar 29, 2024
f86c660
Fixes in location.tmpl after tests while building service.go
sebastianczech Apr 2, 2024
47f7d07
Render service.go for entry and config
sebastianczech Apr 2, 2024
b272b01
Rename function XmlPathSuffixes()
sebastianczech Apr 2, 2024
ec3c02e
Fix problem with nested specs and versions
sebastianczech Apr 2, 2024
141cd9f
Merge branch 'refs/heads/render-service' into e2e-example
sebastianczech Apr 3, 2024
7abbcd2
Use imports manager
sebastianczech Apr 3, 2024
1399b18
Fix blank lines
sebastianczech Apr 3, 2024
2a7e485
Add missing Target: s.client.GetTarget()
sebastianczech Apr 3, 2024
04b5609
Merge branch 'refs/heads/render-service' into e2e-example
sebastianczech Apr 3, 2024
204efb6
Update assets from branch develop of pango repository
sebastianczech Apr 3, 2024
d31fe9c
Do not introduce short names for imports
sebastianczech Apr 3, 2024
03809ff
Do not introduce short names for imports
sebastianczech Apr 3, 2024
900b7d2
Merge branch 'render-service' into e2e-example
sebastianczech Apr 3, 2024
ce81843
Add missing Target: s.client.GetTarget(),
sebastianczech Apr 4, 2024
a7820de
Merge branch 'refs/heads/render-service' into e2e-example
sebastianczech Apr 4, 2024
ab60f70
Merge branch 'refs/heads/main' into e2e-example
sebastianczech Apr 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,14 @@ This command can be parametrizes using options:
- `mktp` - create only Terraform provider
- `mksdk` - create only PAN-OS SDK
- `config` - specify path for the config file, default is `cmd/codegen/config.yaml`


## Generate SDK

In order to use generated SDK code, go to directory defined in `config.yaml` e.g. `../generated/pango` and execute
example code:

```
go run cmd/codegen/main.go -t mksdk
cd ../generated/pango
PANOS_HOSTNAME='***' PANOS_USERNAME='***' PANOS_PASSWORD='***' go run example/main.go
```
142 changes: 142 additions & 0 deletions assets/errors/panos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package errors

import (
"encoding/xml"
stderr "errors"
"fmt"
"strings"

"github.com/PaloAltoNetworks/pango/util"
)

var InvalidFilterError = stderr.New("filter is improperly formatted")
var NameNotSpecifiedError = stderr.New("name is not specified")
var NoLocationSpecifiedError = stderr.New("no location specified")
var RelativePositionWithRemoveEverythingElseError = stderr.New("cannot do relative positioning when removing all other rules")
var UnrecognizedOperatorError = stderr.New("unsupported filter operator")
var UnsupportedFilterTypeError = stderr.New("unsupported type for filtering")
var UuidNotSpecifiedError = stderr.New("uuid is not specified")

// Panos is an error returned from PAN-OS.
//
// The error contains both the error message and the code returned from PAN-OS.
type Panos struct {
Msg string
Code int
}

// Error returns the error message.
func (e Panos) Error() string {
return e.Msg
}

// ObjectNotFound returns true if this is an object not found error.
func (e Panos) ObjectNotFound() bool {
return e.Code == 7
}

// ObjectNotFound returns an object not found error.
func ObjectNotFound() Panos {
return Panos{
Msg: "Object not found",
Code: 7,
}
}

// Parse attempts to parse an error from the given XML response.
func Parse(body []byte) error {
var e errorCheck

_ = xml.Unmarshal(body, &e)
if e.Failed() {
return Panos{
Msg: e.Message(),
Code: e.Code,
}
}

return nil
}

type errorCheck struct {
XMLName xml.Name `xml:"response"`
Status string `xml:"status,attr"`
Code int `xml:"code,attr"`
Msg *errorCheckMsg `xml:"msg"`
ResultMsg *string `xml:"result>msg"`
}

type errorCheckMsg struct {
Line []util.CdataText `xml:"line"`
Message string `xml:",chardata"`
}

func (e *errorCheck) Failed() bool {
if e.Status == "failed" || e.Status == "error" {
return true
} else if e.Code == 0 || e.Code == 19 || e.Code == 20 {
return false
}

return true
}

func (e *errorCheck) Message() string {
if e.Msg != nil {
if len(e.Msg.Line) > 0 {
var b strings.Builder
for i := range e.Msg.Line {
if i != 0 {
b.WriteString(" | ")
}
b.WriteString(strings.TrimSpace(e.Msg.Line[i].Text))
}
return b.String()
}

if e.Msg.Message != "" {
return e.Msg.Message
}
}

if e.ResultMsg != nil {
return *e.ResultMsg
}

return e.CodeError()
}

func (e *errorCheck) CodeError() string {
switch e.Code {
case 1:
return "Unknown command"
case 2, 3, 4, 5, 11:
return fmt.Sprintf("Internal error (%d) encountered", e.Code)
case 6:
return "Bad Xpath"
case 7:
return "Object not found"
case 8:
return "Object not unique"
case 10:
return "Reference count not zero"
case 12:
return "Invalid object"
case 14:
return "Operation not possible"
case 15:
return "Operation denied"
case 16:
return "Unauthorized"
case 17:
return "Invalid command"
case 18:
return "Malformed command"
case 0, 19, 20:
return ""
case 22:
return "Session timed out"
default:
return fmt.Sprintf("(%d) Unknown failure code, operation failed", e.Code)
}
}
73 changes: 73 additions & 0 deletions assets/errors/panos_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package errors

import (
"fmt"
"strings"
"testing"
)

func TestGetSingularMissingObjectIsError(t *testing.T) {
data := `<response status="success" code="7"><result/></response>`

err := Parse([]byte(data))
if err == nil {
t.Errorf("Error is nil")
} else {
e2, ok := err.(Panos)
if !ok {
t.Errorf("Not a panos error")
} else if !e2.ObjectNotFound() {
t.Errorf("Not an object not found error")
}
}
}

func TestShowSingularMissingObjectIsError(t *testing.T) {
data := `<response status="error"><msg><line>No such node</line></msg></response>`

err := Parse([]byte(data))
if err == nil {
t.Errorf("Error is nil")
} else {
e2, ok := err.(Panos)
if !ok {
t.Errorf("Not a panos error")
} else if e2.Msg != "No such node" {
t.Errorf("Incorrect msg: %s", e2.Msg)
}
}
}

func TestMultilineErrorMessage(t *testing.T) {
expected := "HTTP method must be GET"
data := fmt.Sprintf(`<response status="error" code="12"><msg><line><![CDATA[ tf123456 -> server -> first server is invalid. %s, Username/Password must not be empty when Tag Distribution is chosen]]></line><line><![CDATA[ tf123456 -> server is invalid]]></line></msg></response>`, expected)

err := Parse([]byte(data))
if err == nil {
t.Errorf("Error is nil")
} else {
e2, ok := err.(Panos)
if !ok {
t.Errorf("Not a panos error")
} else if !strings.Contains(e2.Msg, expected) {
t.Errorf("Does not contain the expected substring: %s", e2.Msg)
}
}
}

func TestFailedExportErrorMessage(t *testing.T) {
expected := `Parameter "format" is required while exporting certificate`
data := `<response status = 'error' code = '400'><result><msg>Parameter &quot;format&quot; is required while exporting certificate</msg></result></response>`

err := Parse([]byte(data))
if err == nil {
t.Errorf("Error is nil")
} else {
e2, ok := err.(Panos)
if !ok {
t.Errorf("Not a pnos error")
} else if !strings.Contains(e2.Msg, expected) {
t.Errorf("Does not contain the expected substring: %s", e2.Msg)
}
}
}
Loading