diff --git a/driver/netconf/commit.go b/driver/netconf/commit.go index a79d3ad..4e5fbe7 100644 --- a/driver/netconf/commit.go +++ b/driver/netconf/commit.go @@ -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) @@ -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 { diff --git a/driver/netconf/operation.go b/driver/netconf/operation.go index f77ad26..c8542ff 100644 --- a/driver/netconf/operation.go +++ b/driver/netconf/operation.go @@ -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 diff --git a/driver/opoptions/netconf.go b/driver/opoptions/netconf.go index 9855908..067ec9c 100644 --- a/driver/opoptions/netconf.go +++ b/driver/opoptions/netconf.go @@ -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 + } +}