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(specs): Initial panos_ethernet_layer3_subinterface spec #277

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
28 changes: 22 additions & 6 deletions assets/terraform/internal/manager/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ type TFConfigObject[E any] interface {

type SDKConfigService[C any, L ConfigLocation] interface {
Create(context.Context, L, C) (C, error)
CreateWithXpath(context.Context, string, C) error
Update(context.Context, L, C) (C, error)
Read(context.Context, L, string) (C, error)
ReadWithXpath(context.Context, string, string) (C, error)
Delete(context.Context, L, C) error
}

type ConfigLocation interface {
Xpath(version.Number) ([]string, error)
XpathWithComponents(version.Number, ...string) ([]string, error)
}

type ConfigObjectManager[C any, L ConfigLocation, S SDKConfigService[C, L]] struct {
Expand All @@ -41,16 +42,31 @@ func NewConfigObjectManager[C any, L ConfigLocation, S SDKConfigService[C, L]](c
}
}

func (o *ConfigObjectManager[C, L, S]) Create(ctx context.Context, location L, config C) (C, error) {
return o.service.Create(ctx, location, config)
func (o *ConfigObjectManager[C, L, S]) Create(ctx context.Context, location L, components []string, config C) (C, error) {
xpath, err := location.XpathWithComponents(o.client.Versioning(), components...)
if err != nil {
return *new(C), err
}

err = o.service.CreateWithXpath(ctx, util.AsXpath(xpath), config)
if err != nil {
return *new(C), err
}

return o.Read(ctx, location, components)
}

func (o *ConfigObjectManager[C, L, S]) Update(ctx context.Context, location L, config C) (C, error) {
return o.service.Update(ctx, location, config)
}

func (o *ConfigObjectManager[C, L, S]) Read(ctx context.Context, location L) (C, error) {
obj, err := o.service.Read(ctx, location, "get")
func (o *ConfigObjectManager[C, L, S]) Read(ctx context.Context, location L, components []string) (C, error) {
xpath, err := location.XpathWithComponents(o.client.Versioning(), components...)
if err != nil {
return *new(C), err
}

obj, err := o.service.ReadWithXpath(ctx, util.AsXpath(xpath), "get")
if err != nil && sdkerrors.IsObjectNotFound(err) {
return obj, ErrObjectNotFound
}
Expand Down
39 changes: 27 additions & 12 deletions assets/terraform/internal/manager/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package manager

import (
"context"
"errors"
"fmt"
"log/slog"

Expand Down Expand Up @@ -33,13 +34,15 @@ type EntryObject interface {
}

type EntryLocation interface {
XpathWithComponents(version.Number, ...string) ([]string, error)
XpathWithEntryName(version.Number, string) ([]string, error)
}

type SDKEntryService[E EntryObject, L EntryLocation] interface {
CreateWithXpath(context.Context, string, E) error
ReadWithXpath(context.Context, string, string) (E, error)
Create(context.Context, L, E) (E, error)
List(context.Context, L, string, string, string) ([]E, error)
Read(context.Context, L, string, string) (E, error)
Update(context.Context, L, E, string) (E, error)
Delete(context.Context, L, ...string) error
}
Expand Down Expand Up @@ -82,8 +85,13 @@ func (o *EntryObjectManager[E, L, S]) ReadMany(ctx context.Context, location L,
return filtered, nil
}

func (o *EntryObjectManager[E, L, S]) Read(ctx context.Context, location L, name string) (E, error) {
object, err := o.service.Read(ctx, location, name, "get")
func (o *EntryObjectManager[E, L, S]) Read(ctx context.Context, location L, components []string) (E, error) {
xpath, err := location.XpathWithComponents(o.client.Versioning(), components...)
if err != nil {
return *new(E), err
}

object, err := o.service.ReadWithXpath(ctx, util.AsXpath(xpath), "get")
if err != nil {
if sdkerrors.IsObjectNotFound(err) {
return *new(E), ErrObjectNotFound
Expand All @@ -94,20 +102,27 @@ func (o *EntryObjectManager[E, L, S]) Read(ctx context.Context, location L, name
return object, nil
}

func (o *EntryObjectManager[E, T, S]) Create(ctx context.Context, location T, entry E) (E, error) {
existing, err := o.service.List(ctx, location, "get", "", "")
if err != nil && !sdkerrors.IsObjectNotFound(err) {
func (o *EntryObjectManager[E, L, S]) Create(ctx context.Context, location L, components []string, entry E) (E, error) {
_, err := o.Read(ctx, location, components)
if err == nil {
return *new(E), &Error{err: ErrConflict, message: fmt.Sprintf("entry '%s' already exists", entry.EntryName())}
}

if err != nil && !errors.Is(err, ErrObjectNotFound) {
return *new(E), err
}

for _, elt := range existing {
if elt.EntryName() == entry.EntryName() {
return *new(E), ErrConflict
}
xpath, err := location.XpathWithComponents(o.client.Versioning(), components...)
if err != nil {
return *new(E), err
}

err = o.service.CreateWithXpath(ctx, util.AsXpath(xpath[:len(xpath)-1]), entry)
if err != nil {
return *new(E), err
}

obj, err := o.service.Create(ctx, location, entry)
return obj, err
return o.Read(ctx, location, components)
}

func (o *EntryObjectManager[E, L, S]) CreateMany(ctx context.Context, location L, entries []E) ([]E, error) {
Expand Down
54 changes: 42 additions & 12 deletions assets/terraform/internal/manager/entry_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@ package manager

import (
"context"
"errors"
"fmt"

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

type SDKImportableEntryService[E EntryObject, L EntryLocation, IL ImportLocation] interface {
Create(context.Context, L, []IL, E) (E, error)
CreateWithXpath(context.Context, string, E) error
Read(context.Context, L, string, string) (E, error)
ReadWithXpath(context.Context, string, string) (E, error)
List(context.Context, L, string, string, string) ([]E, error)
Update(context.Context, L, E, string) (E, error)
Delete(context.Context, L, []IL, ...string) error
ImportToLocations(context.Context, L, []IL, string) error
UnimportFromLocations(context.Context, L, []IL, []string) error
}

type ImportableEntryObjectManager[E EntryObject, L EntryLocation, IL ImportLocation, IS SDKImportableEntryService[E, L, IL]] struct {
Expand All @@ -34,29 +41,44 @@ func (o *ImportableEntryObjectManager[E, L, IL, IS]) ReadMany(ctx context.Contex
return nil, &Error{err: ErrInternal, message: "called ReadMany on an importable singular resource"}
}

func (o *ImportableEntryObjectManager[E, L, IL, IS]) Read(ctx context.Context, location L, name string) (E, error) {
object, err := o.service.Read(ctx, location, name, "get")
func (o *ImportableEntryObjectManager[E, L, IL, IS]) Read(ctx context.Context, location L, components []string) (E, error) {
xpath, err := location.XpathWithComponents(o.client.Versioning(), components...)
if err != nil {
return *new(E), ErrObjectNotFound
return *new(E), err
}

object, err := o.service.ReadWithXpath(ctx, util.AsXpath(xpath), "get")
if err != nil {
if sdkerrors.IsObjectNotFound(err) {
return *new(E), ErrObjectNotFound
}
return *new(E), &Error{err: err}
}

return object, nil
}

func (o *ImportableEntryObjectManager[E, L, IL, IS]) Create(ctx context.Context, location L, importLocs []IL, entry E) (E, error) {
existing, err := o.service.List(ctx, location, "get", "", "")
if err != nil && !sdkerrors.IsObjectNotFound(err) {
func (o *ImportableEntryObjectManager[E, L, IL, IS]) Create(ctx context.Context, location L, components []string, entry E) (E, error) {
_, err := o.Read(ctx, location, components)
if err == nil {
return *new(E), &Error{err: ErrConflict, message: fmt.Sprintf("entry '%s' already exists", entry.EntryName())}
}

if err != nil && !errors.Is(err, ErrObjectNotFound) {
return *new(E), err
}

for _, elt := range existing {
if elt.EntryName() == entry.EntryName() {
return *new(E), ErrConflict
}
xpath, err := location.XpathWithComponents(o.client.Versioning(), components...)
if err != nil {
return *new(E), err
}

err = o.service.CreateWithXpath(ctx, util.AsXpath(xpath[:len(xpath)-1]), entry)
if err != nil {
return *new(E), err
}

obj, err := o.service.Create(ctx, location, importLocs, entry)
return obj, err
return o.Read(ctx, location, components)
}

func (o *ImportableEntryObjectManager[E, L, IL, IS]) Update(ctx context.Context, location L, entry E, name string) (E, error) {
Expand All @@ -75,3 +97,11 @@ func (o *ImportableEntryObjectManager[E, L, IL, IS]) Delete(ctx context.Context,
}
return nil
}

func (o *ImportableEntryObjectManager[E, L, IL, IS]) ImportToLocations(ctx context.Context, location L, importLocs []IL, entry string) error {
return o.service.ImportToLocations(ctx, location, importLocs, entry)
}

func (o *ImportableEntryObjectManager[E, L, IL, IS]) UnimportFromLocations(ctx context.Context, location L, importLocs []IL, entry string) error {
return o.service.UnimportFromLocations(ctx, location, importLocs, []string{entry})
}
1 change: 1 addition & 0 deletions pkg/generate/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ func (c *Creator) parseTemplate(templateName string) (*template.Template, error)
"generateEntryXpath": translate.GenerateEntryXpath,
"nestedSpecs": translate.NestedSpecs,
"createGoSuffixFromVersion": translate.CreateGoSuffixFromVersionTmpl,
"paramNotSkipped": translate.ParamNotSkippedTmpl,
"paramSupportedInVersion": translate.ParamSupportedInVersionTmpl,
"xmlPathSuffixes": translate.XmlPathSuffixes,
"underscore": naming.Underscore,
Expand Down
Loading
Loading