jeremmfr / terraform-provider-junos

Terraform provider for Junos devices
https://registry.terraform.io/providers/jeremmfr/junos
MIT License
61 stars 22 forks source link

Feature request: add commit confirm feature #585

Closed NikitaPuglachenko closed 9 months ago

NikitaPuglachenko commented 9 months ago

Description

It would be great to keep the JunOS feature like commit confirm to avoid an outage if something wrong with config.

New or Affected Resource(s)

junos internal/junos/netconf.go ...

Potential Terraform Configuration

junos:

provider "junos" {
  ...
  commitconfirm = 5 # minutes
}

internal/junos/netconf.go:

// netconfCommit commits the configuration.
func (sess *Session) netconfCommit(logMessage string, confirmCommit *int) (_warn []error, _err error) {
    if confirmCommit != nil {
        confirmReply, confirmErr := sess.netconf.Exec(netconf.RawMethod(fmt.Sprintf(rpcCommitConfirm, confirmCommit, logMessage)))
        if confirmErr != nil {
            return []error{}, fmt.Errorf("executing netconf confirm commit: %w", confirmErr)
        }

        // If there are errors in the confirm commit, return now.
        if confirmReply.Errors != nil {
            warnings := make([]error, 0)
            for _, m := range confirmReply.Errors {
                if m.Severity == errorSeverity {
                    return warnings, errors.New(m.Error())
                }
                warnings = append(warnings, errors.New(m.Error()))
            }
            return warnings, nil
        }

        delay := *confirmCommit - 1
        if delay < 0 {
            delay = 0
        }

        // Wait for confirmCommit - 1 minutes before committing.
        time.Sleep(time.Duration(delay) * time.Minute)
    }
...

References

https://www.juniper.net/documentation/us/en/software/junos/junos-xml-protocol/topics/ref/tag/junos-xml-protocol-commit-configuration.html

jeremmfr commented 9 months ago

Hi đź‘‹

After a few tests, the feature is feasible. I'll take care of adding it.