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

add commit confirmed options to the NETCONF driver #144

Merged
merged 1 commit into from
Jul 26, 2023
Merged
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
34 changes: 28 additions & 6 deletions driver/netconf/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,33 @@ package netconf

import (
"encoding/xml"
"strconv"

"github.com/scrapli/scrapligo/response"
"github.com/scrapli/scrapligo/util"
)

type commit struct {
XMLName xml.Name `xml:"commit"`
XMLName xml.Name `xml:"commit"`
Confirmed *targetElement `xml:"confirmed,omitempty"`
ConfirmedTimeout string `xml:"confirm-timeout,omitempty"`
Persist string `xml:"persist,omitempty"`
PersistID string `xml:"persist-id,omitempty"`
}

func (d *Driver) buildCommitElem() *message {
func (d *Driver) buildCommitElem(confirmed bool, timeout uint, persist, persistID string) *message {
commitElem := &commit{
XMLName: xml.Name{},
XMLName: xml.Name{},
Persist: persist,
PersistID: persistID,
}

if confirmed {
commitElem.Confirmed = &targetElement{}
}

if timeout > 0 {
commitElem.ConfirmedTimeout = strconv.Itoa(int(timeout))
}

netconfInput := d.buildPayload(commitElem)
Expand All @@ -21,15 +37,21 @@ func (d *Driver) buildCommitElem() *message {
}

// Commit executes a commit rpc against the NETCONF server.
func (d *Driver) Commit() (*response.NetconfResponse, error) {
func (d *Driver) Commit(opts ...util.Option) (*response.NetconfResponse, error) {
d.Logger.Info("Commit RPC requested")

op, err := NewOperation()
op, err := NewOperation(opts...)
if err != nil {
return nil, err
}

return d.sendRPC(d.buildCommitElem(), op)
m := d.buildCommitElem(
op.CommitConfirmed,
op.CommitConfirmTimeout,
op.CommitConfirmedPersist,
op.CommitConfirmedPersistID)

return d.sendRPC(m, op)
}

type discard struct {
Expand Down
5 changes: 5 additions & 0 deletions driver/netconf/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ type OperationOptions struct {
FilterType string
DefaultType string
Timeout time.Duration

CommitConfirmed bool
CommitConfirmTimeout uint
CommitConfirmedPersist string
CommitConfirmedPersistID string
}

// NewOperation returns a new OperationOptions object with the defaults set and any provided options
Expand Down
60 changes: 60 additions & 0 deletions driver/opoptions/netconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,63 @@ func WithFilter(s string) util.Option {
return util.ErrIgnoredOption
}
}

// WithCommitConfirmed allows setting the confirmed element in a commit operation.
func WithCommitConfirmed() util.Option {
return func(o interface{}) error {
c, ok := o.(*netconf.OperationOptions)

if ok {
c.CommitConfirmed = true

return nil
}

return util.ErrIgnoredOption
}
}

// WithCommitConfirmTimeout allows setting the confirm-timeout element in a commit operation.
func WithCommitConfirmTimeout(t uint) util.Option {
return func(o interface{}) error {
c, ok := o.(*netconf.OperationOptions)

if ok {
c.CommitConfirmTimeout = t

return nil
}

return util.ErrIgnoredOption
}
}

// WithCommitConfirmedPersist allows setting the persist element in a commit operation.
func WithCommitConfirmedPersist(label string) util.Option {
return func(o interface{}) error {
c, ok := o.(*netconf.OperationOptions)

if ok {
c.CommitConfirmedPersist = label

return nil
}

return util.ErrIgnoredOption
}
}

// WithCommitConfirmedPersistID allows setting the persist-id element in a commit operation.
func WithCommitConfirmedPersistID(id string) util.Option {
return func(o interface{}) error {
c, ok := o.(*netconf.OperationOptions)

if ok {
c.CommitConfirmedPersistID = id

return nil
}

return util.ErrIgnoredOption
}
}
Loading