Skip to content

Commit

Permalink
provider: add commit_confirmed argument
Browse files Browse the repository at this point in the history
and commit_confirmed_wait_percent
Fix #585
  • Loading branch information
jeremmfr committed Dec 12, 2023
1 parent d72a791 commit 179acc4
Show file tree
Hide file tree
Showing 84 changed files with 665 additions and 362 deletions.
8 changes: 8 additions & 0 deletions .changes/issue-585.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!-- markdownlint-disable-file MD013 MD041 -->
FEATURES:

* add `commit_confirmed` and `commit_confirmed_wait_percent` provider argument to be able use `commit confirmed` feature to commit the resource actions (Fix [#585](https://github.com/jeremmfr/terraform-provider-junos/issues/585))

ENHANCEMENTS:

* display all errors when configuration commit generate multiple errors
21 changes: 21 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,27 @@ The following arguments are supported in the `provider` block:
It can also be sourced from the `JUNOS_SLEEP_LOCK` environment variable.
Defaults to `10`.

- **commit_confirmed** (Optional, Number)
Number of minutes until automatic rollback (1..65535).
It can also be sourced from the `JUNOS_COMMIT_CONFIRMED` environment variable.

**If this argument is specified**, for each resource action with commit,
the commit will take place in three steps:
- commit with the `confirmed` option and with the value of this argument as `confirm-timeout`.
- wait for `<commit_confirmed_wait_percent>`% of the minutes defined in the value of this argument.
- confirm commit to avoid rollback with the `commit check` command.

If a gracefully shutting down call with `Ctrl-c` is received by Terraform,
the wait step is stopped and provider returns an error.

- **commit_confirmed_wait_percent** (Optional, Number)
Percentage of `<commit_confirmed>` minute(s) to wait between
`commit confirmed` (commit with automatic rollback) and
`commit check` (confirmation) commands (0..99).
No effect if `<commit_confirmed>` is not used.
It can also be sourced from the `JUNOS_COMMIT_CONFIRMED_WAIT_PERCENT` environment variable.
Defaults to `90`.

---

### SSH options
Expand Down
100 changes: 61 additions & 39 deletions internal/junos/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,52 @@ const directoryPermission = 0o755

// Client information to connect on Junos Device and more.
type Client struct {
fakeUpdateAlso bool
fakeDeleteAlso bool
junosPort int
junosSSHTimeoutToEstab int
junosSSHRetryToEstab int
sleepLock int
sleepShort int
sleepSSHClosed int
filePermission int64
junosIP string
junosUserName string
junosPassword string
junosSSHKeyPEM string
junosSSHKeyFile string
junosSSHKeyPass string
groupIntDel string
logFileDst string
fakeCreateSetFile string
junosSSHCiphers []string
fakeUpdateAlso bool
fakeDeleteAlso bool
junosCommitConfirmed int
junosCommitConfirmedWaitPercent int
junosPort int
junosSSHTimeoutToEstab int
junosSSHRetryToEstab int
sleepLock int
sleepShort int
sleepSSHClosed int
filePermission int64
junosIP string
junosUserName string
junosPassword string
junosSSHKeyPEM string
junosSSHKeyFile string
junosSSHKeyPass string
groupIntDel string
logFileDst string
fakeCreateSetFile string
junosSSHCiphers []string
}

func NewClient(ip string) *Client {
return &Client{
junosIP: ip,
junosPort: 830,
junosUserName: "netconf",
junosPassword: "",
junosSSHKeyPEM: "",
junosSSHKeyFile: "",
junosSSHKeyPass: "",
groupIntDel: "",
sleepShort: 100,
sleepLock: 10,
sleepSSHClosed: 0,
junosSSHCiphers: DefaultSSHCiphers(),
junosSSHTimeoutToEstab: 0,
junosSSHRetryToEstab: 1,
filePermission: 0o644,
logFileDst: "",
fakeCreateSetFile: "",
fakeUpdateAlso: false,
fakeDeleteAlso: false,
junosIP: ip,
junosPort: 830,
junosUserName: "netconf",
junosPassword: "",
junosSSHKeyPEM: "",
junosSSHKeyFile: "",
junosSSHKeyPass: "",
groupIntDel: "",
sleepShort: 100,
sleepLock: 10,
junosCommitConfirmed: 0,
junosCommitConfirmedWaitPercent: 90,
sleepSSHClosed: 0,
junosSSHCiphers: DefaultSSHCiphers(),
junosSSHTimeoutToEstab: 0,
junosSSHRetryToEstab: 1,
filePermission: 0o644,
logFileDst: "",
fakeCreateSetFile: "",
fakeUpdateAlso: false,
fakeDeleteAlso: false,
}
}

Expand Down Expand Up @@ -105,6 +109,24 @@ func (clt *Client) WithSleepLock(sleep int) *Client {
return clt
}

func (clt *Client) WithCommitConfirmed(timeout int) (*Client, error) {
if timeout < 1 || timeout > 65535 {
return clt, fmt.Errorf("bad value for timeout of commit confirmed")
}
clt.junosCommitConfirmed = timeout

return clt, nil
}

func (clt *Client) WithCommitConfirmedWaitPercent(percent int) (*Client, error) {
if percent < 0 || percent > 99 {
return clt, fmt.Errorf("bad value for wait percent of timeout before the commit confirm")
}
clt.junosCommitConfirmedWaitPercent = percent

return clt, nil
}

func (clt *Client) WithSleepSSHClosed(sleep int) *Client {
clt.sleepSSHClosed = sleep

Expand All @@ -125,7 +147,7 @@ func (clt *Client) WithSSHTimeoutToEstablish(timeout int) *Client {

func (clt *Client) WithSSHRetryToEstablish(retry int) (*Client, error) {
if retry < 1 || retry > 10 {
return clt, fmt.Errorf("bad value to number of retry to establishing SSH connection")
return clt, fmt.Errorf("bad value for number of retry to establishing SSH connection")
}
clt.junosSSHRetryToEstab = retry

Expand Down
7 changes: 7 additions & 0 deletions internal/junos/client_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net"
"strconv"
"time"
)

func (clt *Client) StartNewSession(ctx context.Context) (*Session, error) {
Expand Down Expand Up @@ -43,6 +44,12 @@ func (clt *Client) StartNewSession(ctx context.Context) (*Session, error) {

return nil, err
}
sess.commitConfirmedTimeout = clt.junosCommitConfirmed
sess.commitConfirmedWait = time.Duration(
int(
(time.Duration(clt.junosCommitConfirmed)*time.Minute).Microseconds(),
)*clt.junosCommitConfirmedWaitPercent/100,
) * time.Microsecond
sess.logFile = func(message string) {
message = "[" + sess.localAddress + "->" + sess.remoteAddress + "]" + message
clt.logFile(message)
Expand Down
38 changes: 20 additions & 18 deletions internal/junos/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,26 @@ const (

CantReadValuesNotEnoughFields = "can't read values for %s in '%s': not enough fields"

EnvHost = "JUNOS_HOST"
EnvPort = "JUNOS_PORT"
EnvUsername = "JUNOS_USERNAME"
EnvPassword = "JUNOS_PASSWORD"
EnvKeyPem = "JUNOS_KEYPEM"
EnvKeyFile = "JUNOS_KEYFILE"
EnvKeyPass = "JUNOS_KEYPASS"
EnvGroupInterfaceDelete = "JUNOS_GROUP_INTERFACE_DELETE"
EnvSleepShort = "JUNOS_SLEEP_SHORT"
EnvSleepLock = "JUNOS_SLEEP_LOCK"
EnvSleepSSHClosed = "JUNOS_SLEEP_SSH_CLOSED"
EnvSSHTimeoutToEstablish = "JUNOS_SSH_TIMEOUT_TO_ESTABLISH"
EnvSSHRetryToEstablish = "JUNOS_SSH_RETRY_TO_ESTABLISH"
EnvFilePermission = "JUNOS_FILE_PERMISSION"
EnvLogPath = "JUNOS_LOG_PATH"
EnvFakecreateSetfile = "JUNOS_FAKECREATE_SETFILE"
EnvFakeupdateAlso = "JUNOS_FAKEUPDATE_ALSO"
EnvFakedeleteAlso = "JUNOS_FAKEDELETE_ALSO"
EnvHost = "JUNOS_HOST"
EnvPort = "JUNOS_PORT"
EnvUsername = "JUNOS_USERNAME"
EnvPassword = "JUNOS_PASSWORD"
EnvKeyPem = "JUNOS_KEYPEM"
EnvKeyFile = "JUNOS_KEYFILE"
EnvKeyPass = "JUNOS_KEYPASS"
EnvGroupInterfaceDelete = "JUNOS_GROUP_INTERFACE_DELETE"
EnvSleepShort = "JUNOS_SLEEP_SHORT"
EnvSleepLock = "JUNOS_SLEEP_LOCK"
EnvCommitConfirmed = "JUNOS_COMMIT_CONFIRMED"
EnvCommitConfirmedWaitPercent = "JUNOS_COMMIT_CONFIRMED_WAIT_PERCENT"
EnvSleepSSHClosed = "JUNOS_SLEEP_SSH_CLOSED"
EnvSSHTimeoutToEstablish = "JUNOS_SSH_TIMEOUT_TO_ESTABLISH"
EnvSSHRetryToEstablish = "JUNOS_SSH_RETRY_TO_ESTABLISH"
EnvFilePermission = "JUNOS_FILE_PERMISSION"
EnvLogPath = "JUNOS_LOG_PATH"
EnvFakecreateSetfile = "JUNOS_FAKECREATE_SETFILE"
EnvFakeupdateAlso = "JUNOS_FAKEUPDATE_ALSO"
EnvFakedeleteAlso = "JUNOS_FAKEDELETE_ALSO"

DefaultInterfaceTestAcc = "ge-0/0/3"
DefaultInterfaceTestAcc2 = "ge-0/0/4"
Expand Down
Loading

0 comments on commit 179acc4

Please sign in to comment.