Skip to content

Commit 891b0ea

Browse files
committed
Introduce reuse_connection provider flag
1 parent cb777e7 commit 891b0ea

File tree

7 files changed

+105
-27
lines changed

7 files changed

+105
-27
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Add `segment_routing_mpls` and `segment_routing_sr_prefer` attributes to `iosxr_router_ospf` resource and data source
77
- Add `v3_sha_encryption` and `v3_aes_encryption` attributes to `iosxr_snmp_server` resource and data source
88
- Add `fast_reroute_per_prefix` and `fast_reroute_per_prefix_ti_lfa` attributes to `iosxr_router_isis_interface_address_family` resource and data source
9+
- Add `reuse_connection` provider attribute
910

1011
## 0.3.1
1112

docs/guides/changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ description: |-
1515
- Add `segment_routing_mpls` and `segment_routing_sr_prefer` attributes to `iosxr_router_ospf` resource and data source
1616
- Add `v3_sha_encryption` and `v3_aes_encryption` attributes to `iosxr_snmp_server` resource and data source
1717
- Add `fast_reroute_per_prefix` and `fast_reroute_per_prefix_ti_lfa` attributes to `iosxr_router_isis_interface_address_family` resource and data source
18+
- Add `reuse_connection` provider attribute
1819

1920
## 0.3.1
2021

docs/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ provider "iosxr" {
4444
- `host` (String) IP or name of the Cisco IOS-XR device. Optionally a port can be added with `:12345`. The default port is `57400`. This can also be set as the IOSXR_HOST environment variable. If no `host` is provided, the `host` of the first device from the `devices` list is being used.
4545
- `key` (String) TLS private key content. This can also be set as the IOSXR_KEY environment variable.
4646
- `password` (String, Sensitive) Password for the IOS-XR device. This can also be set as the IOSXR_PASSWORD environment variable.
47+
- `reuse_connection` (Boolean) Reuse gNMI connection. This can also be set as the IOSXR_REUSE_CONNECTION environment variable. Defaults to `true`.
4748
- `tls` (Boolean) Use TLS. This can also be set as the IOSXR_TLS environment variable. Defaults to `true`.
4849
- `username` (String) Username for the IOS-XR device. This can also be set as the IOSXR_USERNAME environment variable.
4950
- `verify_certificate` (Boolean) Verify target certificate. This can also be set as the IOSXR_VERIFY_CERTIFICATE environment variable. Defaults to `false`.

gen/templates/provider.go

+27-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/provider/client/client.go

+47-25
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ const (
5252

5353
type Client struct {
5454
Devices map[string]*Device
55+
// Reuse connection
56+
ReuseConnection bool
5557
// Maximum number of retries
5658
MaxRetries int
5759
// Minimum delay between two retries
@@ -73,10 +75,11 @@ type SetOperation struct {
7375
Operation SetOperationType
7476
}
7577

76-
func NewClient() Client {
78+
func NewClient(reuseConnection bool) Client {
7779
devices := make(map[string]*Device)
7880
return Client{
7981
Devices: devices,
82+
ReuseConnection: reuseConnection,
8083
MaxRetries: DefaultMaxRetries,
8184
BackoffMinDelay: DefaultBackoffMinDelay,
8285
BackoffMaxDelay: DefaultBackoffMaxDelay,
@@ -110,6 +113,17 @@ func (c *Client) AddTarget(ctx context.Context, device, host, username, password
110113
return diags
111114
}
112115

116+
if c.ReuseConnection {
117+
err = t.CreateGNMIClient(ctx)
118+
if err != nil {
119+
diags.AddError(
120+
"Unable to create gNMI client",
121+
"Unable to create gNMI client:\n\n"+err.Error(),
122+
)
123+
return diags
124+
}
125+
}
126+
113127
c.Devices[device] = &Device{}
114128
c.Devices[device].Target = t
115129
c.Devices[device].SetMutex = &sync.Mutex{}
@@ -147,17 +161,19 @@ func (c *Client) Set(ctx context.Context, device string, operations ...SetOperat
147161
var setResp *gnmi.SetResponse
148162
for attempts := 0; ; attempts++ {
149163
c.Devices[device].SetMutex.Lock()
150-
err = target.CreateGNMIClient(ctx)
151-
if err != nil {
152-
if ok := c.Backoff(ctx, attempts); !ok {
153-
diags.AddError(
154-
"Unable to create gNMI client",
155-
"Unable to create gNMI client:\n\n"+err.Error(),
156-
)
157-
return nil, diags
158-
} else {
159-
tflog.Error(ctx, fmt.Sprintf("Unable to create gNMI client: %s, retries: %v", err.Error(), attempts))
160-
continue
164+
if !c.ReuseConnection {
165+
err = target.CreateGNMIClient(ctx)
166+
if err != nil {
167+
if ok := c.Backoff(ctx, attempts); !ok {
168+
diags.AddError(
169+
"Unable to create gNMI client",
170+
"Unable to create gNMI client:\n\n"+err.Error(),
171+
)
172+
return nil, diags
173+
} else {
174+
tflog.Error(ctx, fmt.Sprintf("Unable to create gNMI client: %s, retries: %v", err.Error(), attempts))
175+
continue
176+
}
161177
}
162178
}
163179
tCtx, cancel := context.WithTimeout(ctx, GnmiTimeout)
@@ -166,7 +182,9 @@ func (c *Client) Set(ctx context.Context, device string, operations ...SetOperat
166182
setResp, err = target.Set(tCtx, setReq)
167183
tflog.Debug(ctx, fmt.Sprintf("gNMI set response: %s", setResp.String()))
168184
c.Devices[device].SetMutex.Unlock()
169-
target.Close()
185+
if !c.ReuseConnection {
186+
target.Close()
187+
}
170188
if err != nil {
171189
if ok := c.Backoff(ctx, attempts); !ok {
172190
diags.AddError("Client Error", fmt.Sprintf("Set request failed, got error: %s", err))
@@ -201,23 +219,27 @@ func (c *Client) Get(ctx context.Context, device, path string) (*gnmi.GetRespons
201219
var getResp *gnmi.GetResponse
202220
for attempts := 0; ; attempts++ {
203221
tflog.Debug(ctx, fmt.Sprintf("gNMI get request: %s", getReq.String()))
204-
err = target.CreateGNMIClient(ctx)
205-
if err != nil {
206-
if ok := c.Backoff(ctx, attempts); !ok {
207-
diags.AddError(
208-
"Unable to create gNMI client",
209-
"Unable to create gNMI client:\n\n"+err.Error(),
210-
)
211-
return nil, diags
212-
} else {
213-
tflog.Error(ctx, fmt.Sprintf("Unable to create gNMI client: %s, retries: %v", err.Error(), attempts))
214-
continue
222+
if !c.ReuseConnection {
223+
err = target.CreateGNMIClient(ctx)
224+
if err != nil {
225+
if ok := c.Backoff(ctx, attempts); !ok {
226+
diags.AddError(
227+
"Unable to create gNMI client",
228+
"Unable to create gNMI client:\n\n"+err.Error(),
229+
)
230+
return nil, diags
231+
} else {
232+
tflog.Error(ctx, fmt.Sprintf("Unable to create gNMI client: %s, retries: %v", err.Error(), attempts))
233+
continue
234+
}
215235
}
216236
}
217237
tCtx, cancel := context.WithTimeout(ctx, GnmiTimeout)
218238
defer cancel()
219239
getResp, err = target.Get(tCtx, getReq)
220-
target.Close()
240+
if !c.ReuseConnection {
241+
target.Close()
242+
}
221243
tflog.Debug(ctx, fmt.Sprintf("gNMI get response: %s", getResp.String()))
222244
if err != nil {
223245
if ok := c.Backoff(ctx, attempts); !ok {

internal/provider/provider.go

+27-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

templates/guides/changelog.md.tmpl

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ description: |-
1515
- Add `segment_routing_mpls` and `segment_routing_sr_prefer` attributes to `iosxr_router_ospf` resource and data source
1616
- Add `v3_sha_encryption` and `v3_aes_encryption` attributes to `iosxr_snmp_server` resource and data source
1717
- Add `fast_reroute_per_prefix` and `fast_reroute_per_prefix_ti_lfa` attributes to `iosxr_router_isis_interface_address_family` resource and data source
18+
- Add `reuse_connection` provider attribute
1819

1920
## 0.3.1
2021

0 commit comments

Comments
 (0)