diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 00000000..26efc716 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,2 @@ +# These are supported funding model platforms +custom: "https://www.buymeacoffee.com/jeremmfr" \ No newline at end of file diff --git a/.github/workflows/linters-main.yml b/.github/workflows/linters-main.yml index 2c49c750..11c72d16 100644 --- a/.github/workflows/linters-main.yml +++ b/.github/workflows/linters-main.yml @@ -20,7 +20,7 @@ jobs: - name: Check out code uses: actions/checkout@v3 - name: golangci-lint - uses: golangci/golangci-lint-action@v3.1.0 + uses: golangci/golangci-lint-action@v3.2.0 with: version: latest args: -c .golangci.yml -v diff --git a/.github/workflows/linters.yml b/.github/workflows/linters.yml index 34a73d10..a928c3fe 100644 --- a/.github/workflows/linters.yml +++ b/.github/workflows/linters.yml @@ -17,9 +17,9 @@ jobs: - name: Check out code uses: actions/checkout@v3 - name: golangci-lint - uses: golangci/golangci-lint-action@v3.1.0 + uses: golangci/golangci-lint-action@v3.2.0 with: - version: v1.45 + version: v1.46 args: -c .golangci.yml -v markdown-lint: diff --git a/.golangci.yml b/.golangci.yml index 469ef107..f1cd1ef9 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -7,20 +7,23 @@ linters: - interfacer # deprecated 1.38 - scopelint # deprecated 1.39 - golint # deprecated 1.41 + - exhaustivestruct # deprecated 1.46 - funlen - dupl - wsl - gomnd - goerr113 - nestif - - exhaustivestruct + - exhaustruct - paralleltest - gci - cyclop - forcetypeassert - gomoddirectives - varnamelen + - nonamedreturns - maintidx + - execinquery linters-settings: gocognit: # minimal code complexity to report, 30 by default diff --git a/CHANGELOG.md b/CHANGELOG.md index 2533b167..cead0838 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,28 @@ ENHANCEMENTS: BUG FIXES: +## 1.28.0 (May 30, 2022) + +FEATURES: + +* add `junos_interface_logical_info` data source (Fixes [#393](https://github.com/jeremmfr/terraform-provider-junos/issues/393)) +* add `junos_rip_group` resource +* add `junos_rip_neighbor` resource + +ENHANCEMENTS: + +* resource/`junos_interface_logical`: add `disable` argument (Fixes [#395](https://github.com/jeremmfr/terraform-provider-junos/issues/395)) +* data-source/`junos_interface_logical`: add `disable` attribute (like resource) +* resource/`junos_interface_physical`: add `disable` argument +* data-source/`junos_interface_physical`: add `disable` attribute (like resource) +* resource/`junos_ospf_area`: add `realm` argument to add a OSPFv3 realm area. (Fixes [#392](https://github.com/jeremmfr/terraform-provider-junos/issues/392)), + add new `area_range`, `context_identifier`, `inter_area_prefix_export`, `inter_area_prefix_import`, `network_summary_export`, `network_summary_import`, `no_context_identifier_advertisement`, `nssa`, `stub` and `virtual_link` arguments, + add validation of `area_id` + +BUG FIXES: + +* provider: correctly set address to connect to Junos host when an IPv6 is used for provider `ip` argument (enclose IPv6 in square brackets) + ## 1.27.0 (May 06, 2022) ENHANCEMENTS: diff --git a/README.md b/README.md index a4b15690..49a177dc 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ [![Release](https://img.shields.io/github/v/release/jeremmfr/terraform-provider-junos)](https://github.com/jeremmfr/terraform-provider-junos/releases) -[![Installs](https://img.shields.io/badge/dynamic/json?logo=terraform&label=installs&query=$.data.attributes.downloads&url=https%3A%2F%2Fregistry.terraform.io%2Fv2%2Fproviders%2F713)](https://registry.terraform.io/providers/jeremmfr/junos) +[![Installs](https://img.shields.io/badge/dynamic/json?logo=terraform&label=installs&query=$.data.attributes.total&url=https%3A%2F%2Fregistry.terraform.io%2Fv2%2Fproviders%2F713%2Fdownloads%2Fsummary)](https://registry.terraform.io/providers/jeremmfr/junos) [![Registry](https://img.shields.io/badge/registry-doc%40latest-lightgrey?logo=terraform)](https://registry.terraform.io/providers/jeremmfr/junos/latest/docs) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/jeremmfr/terraform-provider-junos/blob/main/LICENSE) [![Go Status](https://github.com/jeremmfr/terraform-provider-junos/actions/workflows/go.yml/badge.svg)](https://github.com/jeremmfr/terraform-provider-junos/actions/workflows/go.yml) diff --git a/docs/data-sources/interface_logical.md b/docs/data-sources/interface_logical.md index 60a04ae1..09511a06 100644 --- a/docs/data-sources/interface_logical.md +++ b/docs/data-sources/interface_logical.md @@ -5,7 +5,7 @@ page_title: "Junos: junos_interface_logical" # junos_interface_logical Get information on a logical interface -(as with an junos_interface_logical resource import). +(as with a junos_interface_logical resource import). ## Example Usage @@ -42,6 +42,8 @@ The following attributes are exported: Name of unit interface (with dot). - **description** (String) Description for interface. +- **disable** (Boolean) + Interface disabled. - **family_inet** (Block) Family inet enabled and possible configuration. - **address** (Block List) diff --git a/docs/data-sources/interface_logical_info.md b/docs/data-sources/interface_logical_info.md new file mode 100644 index 00000000..c6af1989 --- /dev/null +++ b/docs/data-sources/interface_logical_info.md @@ -0,0 +1,45 @@ +--- +page_title: "Junos: junos_interface_logical_info" +--- + +# junos_interface_logical_info + +Get summary information about a logical interface (like its admin/operational statuses and IP addresses). + +## Example Usage + +```hcl +# Read statuses and addresses of ge-0/0/3.0 +data "junos_interface_logical_info" "ge003_0" { + name = "ge-0/0/3.0" +} +output "ge003_0" { + value = data.junos_interface_logical_info.ge003_0 +} +``` + +## Argument Reference + +The following arguments are supported: + +- **name** (Required, String) + Name of unit interface (with dot). + +## Attributes Reference + +The following attributes are exported: + +- **id** (String) + The name of interface read. +- **admin_status** (String) + Admin status. +- **oper_status** (String) + Operational status. +- **family_inet** (Block) + Family inet enabled. + - **address_cidr** (List of String) + List of addresses in CIDR format. +- **family_inet6** (Block) + Family inet6 enabled. + - **address_cidr** (List of String) + List of addresses in CIDR format. diff --git a/docs/data-sources/interface_physical.md b/docs/data-sources/interface_physical.md index 8a36e66f..a713b77a 100644 --- a/docs/data-sources/interface_physical.md +++ b/docs/data-sources/interface_physical.md @@ -5,7 +5,7 @@ page_title: "Junos: junos_interface_physical" # junos_interface_physical Get information on a physical interface -(as with an junos_interface_physical resource import). +(as with a junos_interface_physical resource import). ## Example Usage @@ -44,6 +44,8 @@ The following attributes are exported: Minimum number of aggregated links. - **description** (String) Description for interface. +- **disable** (Boolean) + Interface disabled. - **esi** (Block) ESI Config parameters. See [below for nested schema](#esi-attributes). diff --git a/docs/data-sources/interfaces_physical_present.md b/docs/data-sources/interfaces_physical_present.md index d3369b17..a78b8a20 100644 --- a/docs/data-sources/interfaces_physical_present.md +++ b/docs/data-sources/interfaces_physical_present.md @@ -5,7 +5,7 @@ page_title: "Junos: junos_interfaces_physical_present" # junos_interfaces_physical_present Get list of all of filtered physical interfaces present on the Junos device and their -admin/operational status. +admin/operational statuses. ## Example Usage diff --git a/docs/resources/interface_logical.md b/docs/resources/interface_logical.md index 660d5d36..9497824b 100644 --- a/docs/resources/interface_logical.md +++ b/docs/resources/interface_logical.md @@ -34,6 +34,8 @@ The following arguments are supported: the dependency, but only if st0.x interface is empty or disable. - **description** (Optional, String) Description for interface. +- **disable** (Optional, Boolean) + Disable this logical interface. - **family_inet** (Optional, Block) Enable family inet and add configurations if specified. - **address** (Optional, Block List) diff --git a/docs/resources/interface_physical.md b/docs/resources/interface_physical.md index 95af4ec2..d1ef930a 100644 --- a/docs/resources/interface_physical.md +++ b/docs/resources/interface_physical.md @@ -49,6 +49,8 @@ The following arguments are supported: Deprecated argument, use the `parent_ether_opts` argument instead. - **description** (Optional, String) Description for interface. +- **disable** (Optional, Boolean) + Disable this interface. - **esi** (Optional, Block) Define ESI Config parameters. See [below for nested schema](#esi-arguments). diff --git a/docs/resources/ospf.md b/docs/resources/ospf.md index 1d76ec19..e2f2ac49 100644 --- a/docs/resources/ospf.md +++ b/docs/resources/ospf.md @@ -24,7 +24,7 @@ resource "junos_ospf" "ospf" { The following arguments are supported: -- **routing_instance** (Optional, String) +- **routing_instance** (Optional, String, Forces new resource) Routing instance. Need to be `default` (for root level) or the name of routing instance. Defaults to `default`. diff --git a/docs/resources/ospf_area.md b/docs/resources/ospf_area.md index 2293b446..83aa1f0e 100644 --- a/docs/resources/ospf_area.md +++ b/docs/resources/ospf_area.md @@ -20,17 +20,21 @@ resource "junos_ospf_area" "demo_area" { ## Argument Reference --> **Note** Some arguments are not compatible with version `v3` of ospf. +-> **Note** Some arguments are only compatible with one version (`v2` or `v3`) of ospf. The following arguments are supported: - **area_id** (Required, String, Forces new resource) The id of ospf area. -- **routing_instance** (Optional, String) +- **routing_instance** (Optional, String, Forces new resource) Routing instance for area. Need to be `default` or name of routing instance. Defaults to `default`. -- **version** (Optional, String) +- **realm** (Optional, String, Forces new resource) + OSPFv3 realm configuration. + Need to be `ipv4-unicast`, `ipv4-multicast` or `ipv6-multicast`. + `version` need to be `v3`. +- **version** (Optional, String, Forces new resource) Version of ospf. Need to be `v2` or `v3`. Defaults to `v2`. @@ -43,14 +47,14 @@ The following arguments are supported: Conflict with `authentication_md5`. - **authentication_md5** (Optional, Block List) For each key_id, MD5 authentication key. - See [below for nested schema](#authentication_md5-arguments). + See [below for nested schema](#authentication_md5-arguments-for-interface). Conflict with `authentication_simple_password`. - **bandwidth_based_metrics** (Optional, Block Set) For each bandwidth, configure bandwidth based metrics. - See [below for nested schema](#bandwidth_based_metrics-arguments). + See [below for nested schema](#bandwidth_based_metrics-arguments-for-interface). - **bfd_liveness_detection** (Optional, Block) Bidirectional Forwarding Detection options. - See [below for nested schema](#bfd_liveness_detection-arguments). + See [below for nested schema](#bfd_liveness_detection-arguments-for-interface). - **dead_interval** (Optional, Number) Dead interval (seconds). - **demand_circuit** (Optional, Boolean) @@ -85,7 +89,7 @@ The following arguments are supported: Maximum OSPF packet size (128..65535). - **neighbor** (Optional, Block Set) For each address, configure NBMA neighbor. - See [below for nested schema](#neighbor-arguments). + See [below for nested schema](#neighbor-arguments-for-interface). - **no_advertise_adjacency_segment** (Optional, Boolean) Do not advertise an adjacency segment for this interface. - **no_eligible_backup** (Optional, Boolean) @@ -118,10 +122,75 @@ The following arguments are supported: Traffic engineering metric (1..4294967295). - **transit_delay** (Optional, Number) Transit delay (seconds) (1..65535). +- **area_range** (Optional, Block Set) + For each `range`, configure area range. + See [below for nested schema](#area_range-arguments). +- **context_identifier** (Optional, Set of String) + Configure context identifier in support of edge protection. + Conflict with `no_context_identifier_advertisement`. +- **inter_area_prefix_export** (Optional, List of String) + Export policy for Inter Area Prefix LSAs. +- **inter_area_prefix_import** (Optional, List of String) + Import policy for Inter Area Prefix LSAs. +- **network_summary_export** (Optional, List of String) + Export policy for Type 3 Summary LSAs. +- **network_summary_import** (Optional, List of String) + Import policy for Type 3 Summary LSAs. +- **no_context_identifier_advertisement** (Optional, Boolean) + Disable context identifier advertisments in this area. + Conflict with `context_identifier`. +- **nssa** (Optional, Block) + Configure a not-so-stubby area. + Conflict with `stub`. + - **area_range** (Optional, Block Set) + For each `range`, configure area range. + See [below for nested schema](#area_range-arguments). + - **default_lsa** (Optional, Block) + Configure a default LSA. + See [below for nested schema](#default_lsa-arguments-for-nssa). + - **no_summaries** (Optional, Boolean) + Don't flood summary LSAs into this NSSA area. + - **summaries** (Optional, Boolean) + Flood summary LSAs into this NSSA area. +- **stub** (Optional, Block) + Configure a stub area. + Conflict with `nssa`. + - **default_metric** (Optional, Number) + Metric for the default route in this stub area (1..16777215). + - **no_summaries** (Optional, Boolean) + Don't flood summary LSAs into this stub area. + - **summaries** (Optional, Boolean) + Flood summary LSAs into this stub area. +- **virtual_link** (Optional, Block Set) + For each combination of `neighbor_id` and `transit_area`, configure virtual link. + - **neighbor_id** (Required, String) + Router ID of a virtual neighbor. + Need to be a IPv4 address. + - **transit_area** (Required, String) + Transit area in common with virtual neighbor. + Need to be in IPv4 format. + - **dead_interval** (Optional, Number) + Dead interval (seconds) (1..65535). + - **demand_circuit** (Optional, Boolean) + Interface functions as a demand circuit. + - **disable** (Optional, Boolean) + Disable this virtual link. + - **flood_reduction** (Optional, Boolean) + Enable flood reduction. + - **hello_interval** (Optional, Number) + Hello interval (seconds) (1..255). + - **ipsec_sa** (Optional, String) + IPSec security association name. + - **mtu** (Optional, Number) + Maximum OSPF packet size (128..65535). + - **retransmit_interval** (Optional, Number) + Retransmission interval (seconds) (1..65535). + - **transit_delay** (Optional, Number) + Transit delay (seconds) (1..65535). --- -### authentication_md5 arguments +### authentication_md5 arguments for interface - **key_id** (Required, Number) Key ID for MD5 authentication (0..255). @@ -132,7 +201,7 @@ The following arguments are supported: --- -### bandwidth_based_metrics arguments +### bandwidth_based_metrics arguments for interface - **bandwidth** (Required, String) Bandwidth threshold. @@ -142,7 +211,7 @@ The following arguments are supported: --- -### bfd_liveness_detection arguments +### bfd_liveness_detection arguments for interface - **authentication_algorithm** (Optional, String) Authentication algorithm name. @@ -173,25 +242,53 @@ The following arguments are supported: --- -### neighbor arguments +### neighbor arguments for interface - **address** (Required, String) Address of neighbor. - **eligible** (Optional, Boolean) Eligible to be DR on an NBMA network. +--- + +### area_range arguments + +- **range** (Required, String) + Range to summarize routes in this area. +- **exact** (Optional, Boolean) + Enforce exact match for advertisement of this area range. +- **override_metric** (Optional, Number) + Override the dynamic metric for this area-range (1..16777215). +- **restrict** (Optional, Boolean) + Restrict advertisement of this area range. + +--- + +### default_lsa arguments for nssa + +- **default_metric** (Optional, Number) + Metric for the default route in this area (1..16777215). +- **metric_type** (Optional, Number) + External metric type for the default type 7 LSA (1..2). +- **type_7** (Optional, Boolean) + Flood type 7 default LSA if no-summaries is configured. + ## Attributes Reference The following attributes are exported: - **id** (String) - An identifier for the resource with format `_-__-_`. + An identifier for the resource with format + `_-__-_` + or `_-__-__-_` if realm is set. ## Import -Junos ospf area can be imported using an id made up of -`_-__-_`, e.g. +Junos ospf area can be imported using an id made up of +`_-__-_` or +`_-__-__-_`, e.g. ```shell $ terraform import junos_ospf_area.demo_area 0.0.0.0_-_v2_-_default +$ terraform import junos_ospf_area.demo_area2 0.0.0.0_-_v3_-_ipv4-unicast_-_default ``` diff --git a/docs/resources/rip_group.md b/docs/resources/rip_group.md new file mode 100644 index 00000000..2f182b0c --- /dev/null +++ b/docs/resources/rip_group.md @@ -0,0 +1,90 @@ +--- +page_title: "Junos: junos_rip_group" +--- + +# junos_rip_group + +Provides a rip or ripng group resource. + +## Example Usage + +```hcl +# Add a ripng group +resource "junos_rip_group" "demo_rip" { + name = "group1" + ng = true +} +``` + +## Argument Reference + +The following arguments are supported: + +- **name** (Required, String, Forces new resource) + The name of group. +- **routing_instance** (Optional, String, Forces new resource) + Routing instance for group. + Need to be `default` or name of routing instance. + Defaults to `default`. +- **ng** (Optional, Boolean, Forces new resource) + Protocol `ripng` instead of `rip`. +- **bfd_liveness_detection** (Optional, Block) + Bidirectional Forwarding Detection options. + Conflict with `ng`. + - **authentication_algorithm** (Optional, String) + Authentication algorithm name. + - **authentication_key_chain** (Optional, String) + Authentication key chain name. + - **authentication_loose_check** (Optional, Boolean) + Verify authentication only if authentication is negotiated. + - **detection_time_threshold** (Optional, Number) + High detection-time triggering a trap (milliseconds). + - **minimum_interval** (Optional, Number) + Minimum transmit and receive interval (1..255000 milliseconds). + - **minimum_receive_interval** (Optional, Number) + Minimum receive interval (1..255000 milliseconds). + - **multiplier** (Optional, Number) + Detection time multiplier (1..255). + - **no_adaptation** (Optional, Boolean) + Disable adaptation. + - **transmit_interval_minimum_interval** (Optional, Number) + Minimum transmit interval (1..255000 milliseconds). + - **transmit_interval_threshold** (Optional, Number) + High transmit interval triggering a trap (milliseconds). + - **version** (Optional, String) + BFD protocol version number. +- **demand_circuit** (Optional, Boolean) + Enable demand circuit. + Conflict with `ng`. +- **export** (Optional, List of String) + Export policy. +- **import** (Optional, List of String) + Import policy. +- **max_retrans_time** (Optional, Number) + Maximum time to re-transmit a message in demand-circuit (5..180). + Conflict with `ng`. +- **metric_out** (Optional, Number) + Default metric of exported routes (1..15). +- **preference** (Optional, Number) + Preference of routes learned by this group. +- **route_timeout** (Optional, Number) + Delay before routes time out (30..360 seconds). +- **update_interval** (Optional, Number) + Interval between regular route updates (10..60 seconds) + +## Attributes Reference + +The following attributes are exported: + +- **id** (String) + An identifier for the resource with format `_-_` + or `_-_ng_-_` if `ng` is set to `true`. + +## Import + +Junos rip group can be imported using an id made up of +`_-_` or `_-_ng_-_`, e.g. + +```shell +$ terraform import junos_rip_group.demo_rip group1_-_ng_-_default +``` diff --git a/docs/resources/rip_neighbor.md b/docs/resources/rip_neighbor.md new file mode 100644 index 00000000..d7f7b7a5 --- /dev/null +++ b/docs/resources/rip_neighbor.md @@ -0,0 +1,131 @@ +--- +page_title: "Junos: junos_rip_neighbor" +--- + +# junos_rip_neighbor + +Provides a rip or ripng neighbor resource. + +## Example Usage + +```hcl +# Add a ripng neighbor +resource "junos_rip_neighbor" "demo_rip" { + name = "ae0.0" + group = "group1" + ng = true +} +``` + +## Argument Reference + +The following arguments are supported: + +- **name** (Required, String, Forces new resource) + Interface name. +- **group** (Required, String, Forces new resource) + Name of RIP or RIPng group for this neighbor. +- **routing_instance** (Optional, String, Forces new resource) + Routing instance for neighbor. + Need to be `default` or name of routing instance. + Defaults to `default`. +- **ng** (Optional, Boolean, Forces new resource) + Protocol `ripng` instead of `rip`. +- **any_sender** (Optional, Boolean) + Disable strict checks on sender address. + Conflict with `ng`. +- **authentication_key** (Optional, String, Sensitive) + Authentication key (password). + Conflict with `authentication_selective_md5`, `ng`. +- **authentication_selective_md5** (Optional, Block List) + For each key_id, MD5 authentication key. + Conflict with `authentication_key`, `authentication_type`, `ng`. + - **key_id** (Required, Number) + Key ID for MD5 authentication (0..255). + - **key** (Required, String, Sensitive) + MD5 authentication key value. + - **start_time** (Optional, String) + Start time for key transmission (YYYY-MM-DD.HH:MM:SS). +- **authentication_type** (Optional, String) + Authentication type. + Conflict with `authentication_selective_md5`, `ng`. +- **bfd_liveness_detection** (Optional, Block) + Bidirectional Forwarding Detection options. + Conflict with `ng`. + - **authentication_algorithm** (Optional, String) + Authentication algorithm name. + - **authentication_key_chain** (Optional, String) + Authentication key chain name. + - **authentication_loose_check** (Optional, Boolean) + Verify authentication only if authentication is negotiated. + - **detection_time_threshold** (Optional, Number) + High detection-time triggering a trap (milliseconds). + - **minimum_interval** (Optional, Number) + Minimum transmit and receive interval (1..255000 milliseconds). + - **minimum_receive_interval** (Optional, Number) + Minimum receive interval (1..255000 milliseconds). + - **multiplier** (Optional, Number) + Detection time multiplier (1..255). + - **no_adaptation** (Optional, Boolean) + Disable adaptation. + - **transmit_interval_minimum_interval** (Optional, Number) + Minimum transmit interval (1..255000 milliseconds). + - **transmit_interval_threshold** (Optional, Number) + High transmit interval triggering a trap (milliseconds). + - **version** (Optional, String) + BFD protocol version number. +- **check_zero** (Optional, Boolean) + Check reserved fields on incoming RIPv1 packets. + Conflict with `no_check_zero`, `ng`. +- **demand_circuit** (Optional, Boolean) + Enable demand circuit. + Conflict with `ng`. +- **dynamic_peers** (Optional, Boolean) + Learn peers dynamically on a p2mp interface. + Conflict with `ng`. + `interface_type_p2mp` need to be true. +- **import** (Optional, List of String) + Import policy. +- **max_retrans_time** (Optional, Number) + Maximum time to re-transmit a message in demand-circuit (5..180). + Conflict with `ng`. +- **message_size** (Optional, Number) + Number of route entries per update message (25..255). + Conflict with `ng`. +- **metric_in** (Optional, Number) + Metric value to add to incoming routes (1..15). +- **no_check_zero** (Optional, Booelan) + Don't check reserved fields on incoming RIPv1 packets. + Conflict with `check_zero`, `ng`. +- **peer** (Optional, Set of String) + P2MP peer. + Conflict with `ng`. + `interface_type_p2mp` need to be true. + Need to be valid IP addresses. +- **receive** (Optional, String) + Configure RIP receive options. + Need to be `both`, `none`, `version-1` or `version-2`. +- **route_timeout** (Optional, Number) + Delay before routes time out (30..360 seconds). +- **send** (Optional, String) + Configure RIP send options. + Need to be `broadcast`, `multicast`, `none` or `version-1`. +- **update_interval** (Optional, Number) + Interval between regular route updates (10..60 seconds) + +## Attributes Reference + +The following attributes are exported: + +- **id** (String) + An identifier for the resource with format `_-__-_` + or `_-__-_ng_-_` if `ng` is set to `true`. + +## Import + +Junos rip group can be imported using an id made up of +`_-__-_` or `_-__-_ng_-_`, e.g. + +```shell +$ terraform import junos_rip_neighbor.demo_rip ae0.0_-_group1_-_ng_-_default +``` diff --git a/docs/resources/rstp.md b/docs/resources/rstp.md index 72f43cc5..43e92a9f 100644 --- a/docs/resources/rstp.md +++ b/docs/resources/rstp.md @@ -23,7 +23,7 @@ resource "junos_rstp" "rstp" { The following arguments are supported: -- **routing_instance** (Optional, String) +- **routing_instance** (Optional, String, Forces new resource) Routing instance. Need to be `default` (for root level) or the name of routing instance. Defaults to `default`. diff --git a/docs/resources/vstp.md b/docs/resources/vstp.md index 1630ada6..4e55b9ed 100644 --- a/docs/resources/vstp.md +++ b/docs/resources/vstp.md @@ -23,7 +23,7 @@ resource "junos_vstp" "vstp" { The following arguments are supported: -- **routing_instance** (Optional, String) +- **routing_instance** (Optional, String, Forces new resource) Routing instance. Need to be `default` (for root level) or the name of routing instance. Defaults to `default`. diff --git a/go.mod b/go.mod index 5b297475..489cb59d 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,8 @@ go 1.17 require ( github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 - github.com/hashicorp/terraform-plugin-sdk/v2 v2.14.0 - github.com/jeremmfr/go-netconf v0.4.5 + github.com/hashicorp/terraform-plugin-sdk/v2 v2.16.0 + github.com/jeremmfr/go-netconf v0.4.6 github.com/jeremmfr/go-utils v0.4.1 github.com/jeremmfr/junosdecode v1.1.0 golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd @@ -26,13 +26,13 @@ require ( github.com/hashicorp/go-plugin v1.4.3 // indirect github.com/hashicorp/go-uuid v1.0.3 // indirect github.com/hashicorp/go-version v1.4.0 // indirect - github.com/hashicorp/hc-install v0.3.1 // indirect - github.com/hashicorp/hcl/v2 v2.11.1 // indirect + github.com/hashicorp/hc-install v0.3.2 // indirect + github.com/hashicorp/hcl/v2 v2.12.0 // indirect github.com/hashicorp/logutils v1.0.0 // indirect github.com/hashicorp/terraform-exec v0.16.1 // indirect github.com/hashicorp/terraform-json v0.13.0 // indirect github.com/hashicorp/terraform-plugin-go v0.9.0 // indirect - github.com/hashicorp/terraform-plugin-log v0.3.0 // indirect + github.com/hashicorp/terraform-plugin-log v0.4.0 // indirect github.com/hashicorp/terraform-registry-address v0.0.0-20210412075316-9b2996cce896 // indirect github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect @@ -41,7 +41,7 @@ require ( github.com/mitchellh/copystructure v1.2.0 // indirect github.com/mitchellh/go-testing-interface v1.14.1 // indirect github.com/mitchellh/go-wordwrap v1.0.0 // indirect - github.com/mitchellh/mapstructure v1.4.3 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/mitchellh/reflectwalk v1.0.2 // indirect github.com/oklog/run v1.0.0 // indirect github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect @@ -57,4 +57,4 @@ require ( google.golang.org/protobuf v1.28.0 // indirect ) -replace github.com/hashicorp/terraform-plugin-sdk/v2 => github.com/jeremmfr/terraform-plugin-sdk/v2 v2.14.1-0.20220504103323-e9ac986dd061 +replace github.com/hashicorp/terraform-plugin-sdk/v2 => github.com/jeremmfr/terraform-plugin-sdk/v2 v2.16.1-0.20220517115548-d52b104279ff diff --git a/go.sum b/go.sum index efb0c179..e44cd303 100644 --- a/go.sum +++ b/go.sum @@ -114,10 +114,11 @@ github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09 github.com/hashicorp/go-version v1.3.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/go-version v1.4.0 h1:aAQzgqIrRKRa7w75CKpbBxYsmUoPjzVm1W59ca1L0J4= github.com/hashicorp/go-version v1.4.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= -github.com/hashicorp/hc-install v0.3.1 h1:VIjllE6KyAI1A244G8kTaHXy+TL5/XYzvrtFi8po/Yk= github.com/hashicorp/hc-install v0.3.1/go.mod h1:3LCdWcCDS1gaHC9mhHCGbkYfoY6vdsKohGjugbZdZak= -github.com/hashicorp/hcl/v2 v2.11.1 h1:yTyWcXcm9XB0TEkyU/JCRU6rYy4K+mgLtzn2wlrJbcc= -github.com/hashicorp/hcl/v2 v2.11.1/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg= +github.com/hashicorp/hc-install v0.3.2 h1:oiQdJZvXmkNcRcEOOfM5n+VTsvNjWQeOjfAoO6dKSH8= +github.com/hashicorp/hc-install v0.3.2/go.mod h1:xMG6Tr8Fw1WFjlxH0A9v61cW15pFwgEGqEz0V4jisHs= +github.com/hashicorp/hcl/v2 v2.12.0 h1:PsYxySWpMD4KPaoJLnsHwtK5Qptvj/4Q6s0t4sUxZf4= +github.com/hashicorp/hcl/v2 v2.12.0/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg= github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y= github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= github.com/hashicorp/terraform-exec v0.16.1 h1:NAwZFJW2L2SaCBVZoVaH8LPImLOGbPLkSHy0IYbs2uE= @@ -126,8 +127,9 @@ github.com/hashicorp/terraform-json v0.13.0 h1:Li9L+lKD1FO5RVFRM1mMMIBDoUHslOniy github.com/hashicorp/terraform-json v0.13.0/go.mod h1:y5OdLBCT+rxbwnpxZs9kGL7R9ExU76+cpdY8zHwoazk= github.com/hashicorp/terraform-plugin-go v0.9.0 h1:FvLY/3z4SNVatPZdoFcyrlNbCar+WyyOTv5X4Tp+WZc= github.com/hashicorp/terraform-plugin-go v0.9.0/go.mod h1:EawBkgjBWNf7jiKnVoyDyF39OSV+u6KUX+Y73EPj3oM= -github.com/hashicorp/terraform-plugin-log v0.3.0 h1:NPENNOjaJSVX0f7JJTl4f/2JKRPQ7S2ZN9B4NSqq5kA= github.com/hashicorp/terraform-plugin-log v0.3.0/go.mod h1:EjueSP/HjlyFAsDqt+okpCPjkT4NDynAe32AeDC4vps= +github.com/hashicorp/terraform-plugin-log v0.4.0 h1:F3eVnm8r2EfQCe2k9blPIiF/r2TT01SHijXnS7bujvc= +github.com/hashicorp/terraform-plugin-log v0.4.0/go.mod h1:9KclxdunFownr4pIm1jdmwKRmE4d6HVG2c9XDq47rpg= github.com/hashicorp/terraform-registry-address v0.0.0-20210412075316-9b2996cce896 h1:1FGtlkJw87UsTMg5s8jrekrHmUPUJaMcu6ELiVhQrNw= github.com/hashicorp/terraform-registry-address v0.0.0-20210412075316-9b2996cce896/go.mod h1:bzBPnUIkI0RxauU8Dqo+2KrZZ28Cf48s8V6IHt3p4co= github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 h1:HKLsbzeOsfXmKNpr3GiT18XAblV0BjCbzL8KQAMZGa0= @@ -139,14 +141,14 @@ github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU= github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= -github.com/jeremmfr/go-netconf v0.4.5 h1:QWbcM3TFAAYPV8g3Nz76scBKKlEZ9IgTeeXfI7w3wAY= -github.com/jeremmfr/go-netconf v0.4.5/go.mod h1:WzgoZT7lUkqjcUWsf6U9icQTuCmouShHFcszfNO8E3k= +github.com/jeremmfr/go-netconf v0.4.6 h1:QPeWHpAFT9wRPJzNh91rjSLeNCokrYBMuPWlCQBRo+c= +github.com/jeremmfr/go-netconf v0.4.6/go.mod h1:WzgoZT7lUkqjcUWsf6U9icQTuCmouShHFcszfNO8E3k= github.com/jeremmfr/go-utils v0.4.1 h1:/Jhy9vmFW5uPNycVA/KqNxgifw1gEHFIn+Clik6gBJM= github.com/jeremmfr/go-utils v0.4.1/go.mod h1:K0lGadiSvg9OKGJnW4Bs3t18/VApp/6x2+BV93Sts2M= github.com/jeremmfr/junosdecode v1.1.0 h1:Os8QeOzyL+BPuDZJMjyJgz4QPOgA8EChgKB2Ih5wwCc= github.com/jeremmfr/junosdecode v1.1.0/go.mod h1:nTY0XbZC2ePbZdV0wuUboSMtGrJxtpwWVYfHjrS2Oqw= -github.com/jeremmfr/terraform-plugin-sdk/v2 v2.14.1-0.20220504103323-e9ac986dd061 h1:ExoniBzo8c4mE8PykDdsUkkm/QfZ6ygdlgpfrTeiVrM= -github.com/jeremmfr/terraform-plugin-sdk/v2 v2.14.1-0.20220504103323-e9ac986dd061/go.mod h1:+m4FDQ8h1ulz7zpWtqmZn2JSZQDXUVibhUShbkQVId4= +github.com/jeremmfr/terraform-plugin-sdk/v2 v2.16.1-0.20220517115548-d52b104279ff h1:y6XSfEIc6TMi0tASuAF2xrPOyd5clWRKGqPa+Gqmq+k= +github.com/jeremmfr/terraform-plugin-sdk/v2 v2.16.1-0.20220517115548-d52b104279ff/go.mod h1:hLa0sTiySU/AWEgV2GxJh0/pQIqcCmm30IPja9N9lTg= github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= github.com/jhump/protoreflect v1.6.0 h1:h5jfMVslIg6l29nsMs0D8Wj17RDVdNYti0vDN/PZZoE= github.com/jhump/protoreflect v1.6.0/go.mod h1:eaTn3RZAmMBcV0fifFvlm6VHNz3wSkYyXYWUh7ymB74= @@ -179,8 +181,8 @@ github.com/mitchellh/go-testing-interface v1.14.1/go.mod h1:gfgS7OtZj6MA4U1UrDRp github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= github.com/mitchellh/go-wordwrap v1.0.0 h1:6GlHJ/LTGMrIJbwgdqdl2eEH8o+Exx/0m8ir9Gns0u4= github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= -github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs= -github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ= github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= diff --git a/junos/client.go b/junos/client.go new file mode 100644 index 00000000..040e6c0c --- /dev/null +++ b/junos/client.go @@ -0,0 +1,222 @@ +package junos + +import ( + "context" + "fmt" + "log" + "net" + "os" + "path" + "strconv" + "time" +) + +const directoryPermission = 0o755 + +// Client information to connect on Junos Device and more. +type Client struct { + fakeUpdateAlso bool + fakeDeleteAlso bool + junosPort int + sleepLock int + sleepShort int + sleepSSHClosed int + junosSSHTimeoutToEstab int + filePermission int64 + junosIP string + junosUserName string + junosPassword string + junosSSHKeyPEM string + junosSSHKeyFile string + junosSSHKeyPass string + groupIntDel string + logFileDst string + fakeCreateSetFile string + junosSSHCiphers []string +} + +func (clt *Client) startNewSession(ctx context.Context) (*junosSession, error) { + var auth netconfAuthMethod + auth.Username = clt.junosUserName + auth.Ciphers = clt.junosSSHCiphers + if clt.junosSSHKeyPEM != "" { + auth.PrivateKeyPEM = clt.junosSSHKeyPEM + if clt.junosSSHKeyPass != "" { + auth.Passphrase = clt.junosSSHKeyPass + } + } + if clt.junosSSHKeyFile != "" { + auth.PrivateKeyFile = clt.junosSSHKeyFile + if clt.junosSSHKeyPass != "" { + auth.Passphrase = clt.junosSSHKeyPass + } + } + if clt.junosPassword != "" { + auth.Password = clt.junosPassword + } + auth.Timeout = clt.junosSSHTimeoutToEstab + junSess, err := netconfNewSession(ctx, net.JoinHostPort(clt.junosIP, strconv.Itoa(clt.junosPort)), &auth) + if err != nil { + return nil, err + } + if junSess.SystemInformation.HardwareModel == "" { + return junSess, fmt.Errorf("can't read model of device with netconf command") + } + clt.logFile("[startNewSession] started") + + return junSess, nil +} + +func (clt *Client) closeSession(junSess *junosSession) { + err := junSess.close(clt.sleepSSHClosed) + if err != nil { + clt.logFile(fmt.Sprintf("[closeSession] err: %q", err)) + } else { + clt.logFile("[closeSession] closed") + } +} + +func (clt *Client) command(cmd string, junSess *junosSession) (string, error) { + read, err := junSess.netconfCommand(cmd) + clt.logFile(fmt.Sprintf("[command] cmd: %q", cmd)) + clt.logFile(fmt.Sprintf("[command] read: %q", read)) + sleepShort(clt.sleepShort) + if err != nil && read != emptyW { + clt.logFile(fmt.Sprintf("[command] err: %q", err)) + + return "", err + } + + return read, nil +} + +func (clt *Client) commandXML(cmd string, junSess *junosSession) (string, error) { + read, err := junSess.netconfCommandXML(cmd) + clt.logFile(fmt.Sprintf("[commandXML] cmd: %q", cmd)) + clt.logFile(fmt.Sprintf("[commandXML] read: %q", read)) + sleepShort(clt.sleepShort) + if err != nil { + clt.logFile(fmt.Sprintf("[commandXML] err: %q", err)) + + return "", err + } + + return read, nil +} + +func (clt *Client) configSet(cmd []string, junSess *junosSession) error { + if junSess != nil { + message, err := junSess.netconfConfigSet(cmd) + sleepShort(clt.sleepShort) + clt.logFile(fmt.Sprintf("[configSet] cmd: %q", cmd)) + clt.logFile(fmt.Sprintf("[configSet] message: %q", message)) + if err != nil { + clt.logFile(fmt.Sprintf("[configSet] err: %q", err)) + + return err + } + + return nil + } else if clt.fakeCreateSetFile != "" { + return clt.appendFakeCreateSetFile(cmd) + } + + return fmt.Errorf("internal error: Client.configSet call without connection on device") +} + +func (clt *Client) appendFakeCreateSetFile(lines []string) error { + dirSetFile := path.Dir(clt.fakeCreateSetFile) + if _, err := os.Stat(dirSetFile); err != nil { + if err := os.MkdirAll(dirSetFile, os.FileMode(directoryPermission)); err != nil { + return fmt.Errorf("failed to create parent directory of '%s': %w", clt.fakeCreateSetFile, err) + } + } + f, err := os.OpenFile(clt.fakeCreateSetFile, + os.O_APPEND|os.O_CREATE|os.O_WRONLY, os.FileMode(clt.filePermission)) + if err != nil { + return fmt.Errorf("failed to open file '%s': %w", clt.fakeCreateSetFile, err) + } + defer f.Close() + for _, v := range lines { + if _, err := f.WriteString(v + "\n"); err != nil { + return fmt.Errorf("failed to write in file '%s': %w", clt.fakeCreateSetFile, err) + } + } + + return nil +} + +func (clt *Client) commitConf(logMessage string, junSess *junosSession) (_warnings []error, _err error) { + clt.logFile(fmt.Sprintf("[commitConf] commit %q", logMessage)) + warns, err := junSess.netconfCommit(logMessage) + sleepShort(clt.sleepShort) + if len(warns) > 0 { + for _, w := range warns { + clt.logFile(fmt.Sprintf("[commitConf] commit warning: %q", w)) + } + } + if err != nil { + clt.logFile(fmt.Sprintf("[commitConf] commit error: %q", err)) + + return warns, err + } + + return warns, nil +} + +func (clt *Client) configLock(ctx context.Context, junSess *junosSession) error { + for { + select { + case <-ctx.Done(): + clt.logFile("[configLock] aborted") + + return fmt.Errorf("candidate configuration lock attempt aborted") + default: + if junSess.netconfConfigLock() { + clt.logFile("[configLock] locked") + sleepShort(clt.sleepShort) + + return nil + } + clt.logFile("[configLock] sleep to wait the lock") + sleep(clt.sleepLock) + } + } +} + +func (clt *Client) configClear(junSess *junosSession) (errs []error) { + errs = append(errs, junSess.netconfConfigClear()...) + sleepShort(clt.sleepShort) + clt.logFile("[configClear] config clear") + + errs = append(errs, junSess.netconfConfigUnlock()...) + sleepShort(clt.sleepShort) + clt.logFile("[configClear] config unlock") + + return +} + +// log message in junosLogFile. +func (clt *Client) logFile(message string) { + if clt.logFileDst != "" { + f, err := os.OpenFile(clt.logFileDst, + os.O_WRONLY|os.O_CREATE|os.O_APPEND, os.FileMode(clt.filePermission)) + if err != nil { + log.Fatal(err) + } + defer f.Close() + + log.SetOutput(f) + log.SetPrefix(time.Now().Format("2006-01-02 15:04:05")) + + log.Printf("%s", message) + } +} + +func sleep(timeSleep int) { + time.Sleep(time.Duration(timeSleep) * time.Second) +} + +func sleepShort(timeSleep int) { + time.Sleep(time.Duration(timeSleep) * time.Millisecond) +} diff --git a/junos/config.go b/junos/config.go index 846b3208..1a26abea 100644 --- a/junos/config.go +++ b/junos/config.go @@ -29,52 +29,52 @@ type configProvider struct { junosSSHCiphers []string } -// prepareSession : prepare information to connect to Junos Device and more. -func (c *configProvider) prepareSession() (*Session, diag.Diagnostics) { - sess := &Session{ +// newClient : prepare information to connect to Junos Device and more. +func (c *configProvider) newClient() (*Client, diag.Diagnostics) { + clt := &Client{ junosIP: c.junosIP, junosPort: c.junosPort, junosUserName: c.junosUserName, junosPassword: c.junosPassword, junosSSHKeyPEM: c.junosSSHKeyPEM, - junosKeyPass: c.junosKeyPass, - junosGroupIntDel: c.junosGroupIntDel, - junosSleepLock: c.junosCmdSleepLock, - junosSleepShort: c.junosCmdSleepShort, - junosSleepSSHClosed: c.junosSSHSleepClosed, + junosSSHKeyPass: c.junosKeyPass, + groupIntDel: c.junosGroupIntDel, + sleepLock: c.junosCmdSleepLock, + sleepShort: c.junosCmdSleepShort, + sleepSSHClosed: c.junosSSHSleepClosed, junosSSHCiphers: c.junosSSHCiphers, junosSSHTimeoutToEstab: c.junosSSHTimeoutToEstab, - junosFakeUpdateAlso: c.junosFakeUpdateAlso, - junosFakeDeleteAlso: c.junosFakeDeleteAlso, + fakeUpdateAlso: c.junosFakeUpdateAlso, + fakeDeleteAlso: c.junosFakeDeleteAlso, } // junosSSHKeyFile sshKeyFile := c.junosSSHKeyFile if err := replaceTildeToHomeDir(&sshKeyFile); err != nil { - return sess, diag.FromErr(err) + return clt, diag.FromErr(err) } - sess.junosSSHKeyFile = sshKeyFile + clt.junosSSHKeyFile = sshKeyFile // junosFilePermission filePermission, err := strconv.ParseInt(c.junosFilePermission, 8, 64) if err != nil { - return sess, diag.FromErr(fmt.Errorf("failed to convert value from '%s' to int64: %w", + return clt, diag.FromErr(fmt.Errorf("failed to convert value from '%s' to int64: %w", c.junosFilePermission, err)) } - sess.junosFilePermission = filePermission + clt.filePermission = filePermission // junosLogFile junosLogFile := c.junosDebugNetconfLogPath if err := replaceTildeToHomeDir(&junosLogFile); err != nil { - return sess, diag.FromErr(err) + return clt, diag.FromErr(err) } - sess.junosLogFile = junosLogFile + clt.logFileDst = junosLogFile // junosFakeCreateSetFile junosFakeCreateSetFile := c.junosFakeCreateSetFile if err := replaceTildeToHomeDir(&junosFakeCreateSetFile); err != nil { - return sess, diag.FromErr(err) + return clt, diag.FromErr(err) } - sess.junosFakeCreateSetFile = junosFakeCreateSetFile + clt.fakeCreateSetFile = junosFakeCreateSetFile - return sess, nil + return clt, nil } diff --git a/junos/data_source_interface.go b/junos/data_source_interface.go index fb0797b9..3f8ae1f8 100644 --- a/junos/data_source_interface.go +++ b/junos/data_source_interface.go @@ -353,14 +353,14 @@ func dataSourceInterfaceRead(ctx context.Context, d *schema.ResourceData, m inte if d.Get("config_interface").(string) == "" && d.Get("match").(string) == "" { return diag.FromErr(fmt.Errorf("no arguments provided, 'config_interface' and 'match' empty")) } - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) mutex.Lock() - nameFound, err := searchInterfaceID(d.Get("config_interface").(string), d.Get("match").(string), m, jnprSess) + nameFound, err := searchInterfaceID(d.Get("config_interface").(string), d.Get("match").(string), clt, junSess) if err != nil { mutex.Unlock() @@ -371,7 +371,7 @@ func dataSourceInterfaceRead(ctx context.Context, d *schema.ResourceData, m inte return diag.FromErr(fmt.Errorf("no interface found with arguments provided")) } - interfaceOpt, err := readInterface(nameFound, m, jnprSess) + interfaceOpt, err := readInterface(nameFound, clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -385,10 +385,9 @@ func dataSourceInterfaceRead(ctx context.Context, d *schema.ResourceData, m inte return nil } -func searchInterfaceID(configInterface, match string, m interface{}, jnprSess *NetconfObject) (string, error) { - sess := m.(*Session) +func searchInterfaceID(configInterface, match string, clt *Client, junSess *junosSession) (string, error) { intConfigList := make([]string, 0) - showConfig, err := sess.command(cmdShowConfig+"interfaces "+configInterface+pipeDisplaySet, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"interfaces "+configInterface+pipeDisplaySet, junSess) if err != nil { return "", err } diff --git a/junos/data_source_interface_logical.go b/junos/data_source_interface_logical.go index 2c594bf2..fe2b5317 100644 --- a/junos/data_source_interface_logical.go +++ b/junos/data_source_interface_logical.go @@ -41,6 +41,10 @@ func dataSourceInterfaceLogical() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "disable": { + Type: schema.TypeBool, + Computed: true, + }, "family_inet": { Type: schema.TypeList, Computed: true, @@ -564,14 +568,14 @@ func dataSourceInterfaceLogicalRead(ctx context.Context, d *schema.ResourceData, if d.Get("config_interface").(string) == "" && d.Get("match").(string) == "" { return diag.FromErr(fmt.Errorf("no arguments provided, 'config_interface' and 'match' empty")) } - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) mutex.Lock() - nameFound, err := searchInterfaceLogicalID(d.Get("config_interface").(string), d.Get("match").(string), m, jnprSess) + nameFound, err := searchInterfaceLogicalID(d.Get("config_interface").(string), d.Get("match").(string), clt, junSess) if err != nil { mutex.Unlock() @@ -582,7 +586,7 @@ func dataSourceInterfaceLogicalRead(ctx context.Context, d *schema.ResourceData, return diag.FromErr(fmt.Errorf("no logical interface found with arguments provided")) } - interfaceOpt, err := readInterfaceLogical(nameFound, m, jnprSess) + interfaceOpt, err := readInterfaceLogical(nameFound, clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -596,11 +600,10 @@ func dataSourceInterfaceLogicalRead(ctx context.Context, d *schema.ResourceData, return nil } -func searchInterfaceLogicalID(configInterface, match string, m interface{}, jnprSess *NetconfObject, +func searchInterfaceLogicalID(configInterface, match string, clt *Client, junSess *junosSession, ) (string, error) { - sess := m.(*Session) intConfigList := make([]string, 0) - showConfig, err := sess.command(cmdShowConfig+"interfaces "+configInterface+pipeDisplaySet, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"interfaces "+configInterface+pipeDisplaySet, junSess) if err != nil { return "", err } diff --git a/junos/data_source_interface_logical_info.go b/junos/data_source_interface_logical_info.go new file mode 100644 index 00000000..fbda1dbc --- /dev/null +++ b/junos/data_source_interface_logical_info.go @@ -0,0 +1,160 @@ +package junos + +import ( + "context" + "encoding/xml" + "fmt" + "strings" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +type interfaceLogicalInfo struct { + adminStatus string + name string + operStatus string + familyInet []map[string]interface{} + familyInet6 []map[string]interface{} +} + +func dataSourceInterfaceLogicalInfo() *schema.Resource { + return &schema.Resource{ + ReadWithoutTimeout: dataSourceInterfaceLogicalInfoRead, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { + value := v.(string) + if strings.Count(value, ".") != 1 { + errors = append(errors, fmt.Errorf( + "%q in %q need to have 1 dot", value, k)) + } + + return + }, + }, + "admin_status": { + Type: schema.TypeString, + Computed: true, + }, + "oper_status": { + Type: schema.TypeString, + Computed: true, + }, + "family_inet": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "address_cidr": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + }, + }, + }, + "family_inet6": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "address_cidr": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + }, + }, + }, + }, + } +} + +func dataSourceInterfaceLogicalInfoRead(ctx context.Context, d *schema.ResourceData, m interface{}, +) diag.Diagnostics { + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) + if err != nil { + return diag.FromErr(err) + } + defer clt.closeSession(junSess) + mutex.Lock() + ifaceInfo, err := readInterfaceLogicalInfo(d, clt, junSess) + mutex.Unlock() + if err != nil { + return diag.FromErr(err) + } + fillInterfaceLogicalInfo(d, ifaceInfo) + d.SetId(ifaceInfo.name) + + return nil +} + +func readInterfaceLogicalInfo(d *schema.ResourceData, clt *Client, junSess *junosSession, +) (interfaceLogicalInfo, error) { + var result interfaceLogicalInfo + replyData, err := clt.commandXML(fmt.Sprintf(rpcGetInterfaceInformationTerse, d.Get("name").(string)), junSess) + if err != nil { + return result, err + } + var iface getLogicalInterfaceTerseReply + err = xml.Unmarshal([]byte(replyData), &iface.InterfaceInfo) + if err != nil { + return result, fmt.Errorf("failed to xml unmarshal reply data '%s': %w", replyData, err) + } + if len(iface.InterfaceInfo.LogicalInterface) == 0 { + return result, fmt.Errorf("logical-interface not found in xml: %v", replyData) + } + ifaceInfo := iface.InterfaceInfo.LogicalInterface[0] + result.name = strings.TrimSpace(ifaceInfo.Name) + result.adminStatus = strings.TrimSpace(ifaceInfo.AdminStatus) + result.operStatus = strings.TrimSpace(ifaceInfo.OperStatus) + for _, family := range ifaceInfo.AddressFamily { + switch strings.TrimSpace(family.Name) { + case inetW: + if len(result.familyInet) == 0 { + result.familyInet = append(result.familyInet, map[string]interface{}{ + "address_cidr": make([]string, 0, len(family.Address)), + }) + } + for _, address := range family.Address { + result.familyInet[0]["address_cidr"] = append( + result.familyInet[0]["address_cidr"].([]string), + strings.TrimSpace(address.Local), + ) + } + case inet6W: + if len(result.familyInet6) == 0 { + result.familyInet6 = append(result.familyInet6, map[string]interface{}{ + "address_cidr": make([]string, 0, len(family.Address)), + }) + } + for _, address := range family.Address { + result.familyInet6[0]["address_cidr"] = append( + result.familyInet6[0]["address_cidr"].([]string), + strings.TrimSpace(address.Local), + ) + } + } + } + + return result, nil +} + +func fillInterfaceLogicalInfo(d *schema.ResourceData, ifaceInfo interfaceLogicalInfo) { + if tfErr := d.Set("admin_status", ifaceInfo.adminStatus); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("oper_status", ifaceInfo.operStatus); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("family_inet", ifaceInfo.familyInet); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("family_inet6", ifaceInfo.familyInet6); tfErr != nil { + panic(tfErr) + } +} diff --git a/junos/data_source_interface_logical_info_test.go b/junos/data_source_interface_logical_info_test.go new file mode 100644 index 00000000..d56f5deb --- /dev/null +++ b/junos/data_source_interface_logical_info_test.go @@ -0,0 +1,100 @@ +package junos_test + +import ( + "fmt" + "os" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +// export TESTACC_INTERFACE= for choose interface available else it's ge-0/0/3 or xe-0/0/3. +func TestAccDataSourceInterfaceLogicalInfo_basic(t *testing.T) { + testaccInterface := defaultInterfaceTestAcc + if iface := os.Getenv("TESTACC_INTERFACE"); iface != "" { + testaccInterface = iface + } + if os.Getenv("TESTACC_SWITCH") == "" { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceInterfaceLogicalInfoPre(testaccInterface), + }, + { + Config: testAccDataSourceInterfaceLogicalInfoConfig(testaccInterface), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.junos_interface_logical_info.testacc_dataIfaceLogInfo", + "admin_status", "up"), + resource.TestCheckResourceAttr("data.junos_interface_logical_info.testacc_dataIfaceLogInfo", + "family_inet.#", "1"), + resource.TestCheckResourceAttr("data.junos_interface_logical_info.testacc_dataIfaceLogInfo", + "family_inet.0.address_cidr.#", "2"), + resource.TestCheckResourceAttr("data.junos_interface_logical_info.testacc_dataIfaceLogInfo", + "family_inet6.#", "1"), + resource.TestCheckTypeSetElemAttr("data.junos_interface_logical_info.testacc_dataIfaceLogInfo", + "family_inet6.0.address_cidr.*", "2001:db8::1/64"), + ), + }, + }, + }) + } +} + +func testAccDataSourceInterfaceLogicalInfoPre(interFace string) string { + return fmt.Sprintf(` +resource "junos_interface_physical" "testacc_dataIfaceLogInfo" { + name = "%s" + description = "testacc_dataIfaceLogInfo" + vlan_tagging = true +} +resource "junos_interface_logical" "testacc_dataIfaceLogInfo" { + name = "${junos_interface_physical.testacc_dataIfaceLogInfo.name}.10" + description = "testacc_dataIfaceLogInfo" + family_inet { + address { + cidr_ip = "192.0.2.1/25" + } + address { + cidr_ip = "192.0.2.2/25" + } + } + family_inet6 { + address { + cidr_ip = "2001:db8::1/64" + } + } +} +`, interFace) +} + +func testAccDataSourceInterfaceLogicalInfoConfig(interFace string) string { + return fmt.Sprintf(` +resource "junos_interface_physical" "testacc_dataIfaceLogInfo" { + name = "%s" + description = "testacc_dataIfaceLogInfo" + vlan_tagging = true +} +resource "junos_interface_logical" "testacc_dataIfaceLogInfo" { + name = "${junos_interface_physical.testacc_dataIfaceLogInfo.name}.10" + description = "testacc_dataIfaceLogInfo" + family_inet { + address { + cidr_ip = "192.0.2.1/25" + } + address { + cidr_ip = "192.0.2.2/25" + } + } + family_inet6 { + address { + cidr_ip = "2001:db8::1/64" + } + } +} +data "junos_interface_logical_info" "testacc_dataIfaceLogInfo" { + name = junos_interface_logical.testacc_dataIfaceLogInfo.name +} +`, interFace) +} diff --git a/junos/data_source_interface_physical.go b/junos/data_source_interface_physical.go index 972272a1..75eba168 100644 --- a/junos/data_source_interface_physical.go +++ b/junos/data_source_interface_physical.go @@ -40,6 +40,10 @@ func dataSourceInterfacePhysical() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "disable": { + Type: schema.TypeBool, + Computed: true, + }, "ae_lacp": { Type: schema.TypeString, Computed: true, @@ -338,14 +342,17 @@ func dataSourceInterfacePhysicalRead(ctx context.Context, d *schema.ResourceData if d.Get("config_interface").(string) == "" && d.Get("match").(string) == "" { return diag.FromErr(fmt.Errorf("no arguments provided, 'config_interface' and 'match' empty")) } - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) mutex.Lock() - nameFound, err := searchInterfacePhysicalID(d.Get("config_interface").(string), d.Get("match").(string), m, jnprSess) + nameFound, err := searchInterfacePhysicalID( + d.Get("config_interface").(string), + d.Get("match").(string), + clt, junSess) if err != nil { mutex.Unlock() @@ -356,7 +363,7 @@ func dataSourceInterfacePhysicalRead(ctx context.Context, d *schema.ResourceData return diag.FromErr(fmt.Errorf("no physical interface found with arguments provided")) } - interfaceOpt, err := readInterfacePhysical(nameFound, m, jnprSess) + interfaceOpt, err := readInterfacePhysical(nameFound, clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -370,10 +377,9 @@ func dataSourceInterfacePhysicalRead(ctx context.Context, d *schema.ResourceData return nil } -func searchInterfacePhysicalID(configInterface, match string, m interface{}, jnprSess *NetconfObject) (string, error) { - sess := m.(*Session) +func searchInterfacePhysicalID(configInterface, match string, clt *Client, junSess *junosSession) (string, error) { intConfigList := make([]string, 0) - showConfig, err := sess.command(cmdShowConfig+"interfaces "+configInterface+pipeDisplaySet, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"interfaces "+configInterface+pipeDisplaySet, junSess) if err != nil { return "", err } diff --git a/junos/data_source_interfaces_physical_present.go b/junos/data_source_interfaces_physical_present.go index e1e6ab73..2d87017a 100644 --- a/junos/data_source_interfaces_physical_present.go +++ b/junos/data_source_interfaces_physical_present.go @@ -72,14 +72,14 @@ func dataSourceInterfacesPhysicalPresent() *schema.Resource { func dataSourceInterfacesPhysicalPresentRead(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) mutex.Lock() - iPresent, err := searchInterfacesPhysicalPresent(d, m, jnprSess) + iPresent, err := searchInterfacesPhysicalPresent(d, clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -102,22 +102,21 @@ func dataSourceInterfacesPhysicalPresentRead(ctx context.Context, d *schema.Reso return nil } -func searchInterfacesPhysicalPresent(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func searchInterfacesPhysicalPresent(d *schema.ResourceData, clt *Client, junSess *junosSession, ) (interfacesPresentOpts, error) { - sess := m.(*Session) var result interfacesPresentOpts - replyData, err := sess.commandXML(rpcGetInterfaceInformationTerse, jnprSess) + replyData, err := clt.commandXML(rpcGetInterfacesInformationTerse, junSess) if err != nil { return result, err } - var iface getInterfaceTerseReply + var iface getPhysicalInterfaceTerseReply err = xml.Unmarshal([]byte(replyData), &iface.InterfaceInfo) if err != nil { return result, fmt.Errorf("failed to xml unmarshal reply data '%s': %w", replyData, err) } for _, iFace := range iface.InterfaceInfo.PhysicalInterface { if mName := d.Get("match_name").(string); mName != "" { - matched, err := regexp.MatchString(mName, strings.Trim(iFace.Name, " \n\t")) + matched, err := regexp.MatchString(mName, strings.TrimSpace(iFace.Name)) if err != nil { return result, fmt.Errorf("failed to regexp with '%s': %w", mName, err) } @@ -125,17 +124,17 @@ func searchInterfacesPhysicalPresent(d *schema.ResourceData, m interface{}, jnpr continue } } - if d.Get("match_admin_up").(bool) && strings.Trim(iFace.AdminStatus, " \n\t") != "up" { + if d.Get("match_admin_up").(bool) && strings.TrimSpace(iFace.AdminStatus) != "up" { continue } - if d.Get("match_oper_up").(bool) && strings.Trim(iFace.OperStatus, " \n\t") != "up" { + if d.Get("match_oper_up").(bool) && strings.TrimSpace(iFace.OperStatus) != "up" { continue } - result.interfaceNames = append(result.interfaceNames, strings.Trim(iFace.Name, " \n\t")) + result.interfaceNames = append(result.interfaceNames, strings.TrimSpace(iFace.Name)) result.interfaceStatuses = append(result.interfaceStatuses, map[string]interface{}{ - "name": strings.Trim(iFace.Name, " \n\t"), - "admin_status": strings.Trim(iFace.AdminStatus, " \n\t"), - "oper_status": strings.Trim(iFace.OperStatus, " \n\t"), + "name": strings.TrimSpace(iFace.Name), + "admin_status": strings.TrimSpace(iFace.AdminStatus), + "oper_status": strings.TrimSpace(iFace.OperStatus), }) } diff --git a/junos/data_source_routing_instance.go b/junos/data_source_routing_instance.go index 82547379..608b23da 100644 --- a/junos/data_source_routing_instance.go +++ b/junos/data_source_routing_instance.go @@ -83,14 +83,14 @@ func dataSourceRoutingInstance() *schema.Resource { } func dataSourceRoutingInstanceRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) mutex.Lock() - instanceOptions, err := readRoutingInstance(d.Get("name").(string), m, jnprSess) + instanceOptions, err := readRoutingInstance(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) diff --git a/junos/data_source_security_zone.go b/junos/data_source_security_zone.go index ebd27627..a206bafc 100644 --- a/junos/data_source_security_zone.go +++ b/junos/data_source_security_zone.go @@ -200,14 +200,14 @@ func dataSourceSecurityZone() *schema.Resource { } func dataSourceSecurityZoneRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) mutex.Lock() - zoneOptions, err := readSecurityZone(d.Get("name").(string), m, jnprSess) + zoneOptions, err := readSecurityZone(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) diff --git a/junos/data_source_system_information.go b/junos/data_source_system_information.go index 95409a86..cdaf58d7 100644 --- a/junos/data_source_system_information.go +++ b/junos/data_source_system_information.go @@ -37,36 +37,36 @@ func dataSourceSystemInformation() *schema.Resource { } func dataSourceSystemInformationRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - j, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(j) + defer clt.closeSession(junSess) // Catches case where hostname is not set - if j.SystemInformation.HostName != "" { - d.SetId(j.SystemInformation.HostName) + if junSess.SystemInformation.HostName != "" { + d.SetId(junSess.SystemInformation.HostName) } else { d.SetId("Null-Hostname") } - if tfErr := d.Set("hardware_model", j.SystemInformation.HardwareModel); tfErr != nil { + if tfErr := d.Set("hardware_model", junSess.SystemInformation.HardwareModel); tfErr != nil { panic(tfErr) } - if tfErr := d.Set("os_name", j.SystemInformation.OsName); tfErr != nil { + if tfErr := d.Set("os_name", junSess.SystemInformation.OsName); tfErr != nil { panic(tfErr) } - if tfErr := d.Set("os_version", j.SystemInformation.OsVersion); tfErr != nil { + if tfErr := d.Set("os_version", junSess.SystemInformation.OsVersion); tfErr != nil { panic(tfErr) } - if tfErr := d.Set("serial_number", j.SystemInformation.SerialNumber); tfErr != nil { + if tfErr := d.Set("serial_number", junSess.SystemInformation.SerialNumber); tfErr != nil { panic(tfErr) } // Fix recommended in https://stackoverflow.com/a/23725010 due ot the lack of being able to Unmarshal self-closing // xml tags. This issue is tracked here - https://github.com/golang/go/issues/21399 // Pointer to bool in sysInfo struct will be nil if the tag does not exist - if j.SystemInformation.ClusterNode != nil { + if junSess.SystemInformation.ClusterNode != nil { if tfErr := d.Set("cluster_node", true); tfErr != nil { panic(tfErr) } diff --git a/junos/func_common.go b/junos/func_common.go index 4d93344e..862c875f 100644 --- a/junos/func_common.go +++ b/junos/func_common.go @@ -332,25 +332,25 @@ func copyAndRemoveItemMapList2( return list } -func checkCompatibilitySecurity(jnprSess *NetconfObject) bool { - if strings.HasPrefix(strings.ToLower(jnprSess.SystemInformation.HardwareModel), "srx") { +func checkCompatibilitySecurity(junSess *junosSession) bool { + if strings.HasPrefix(strings.ToLower(junSess.SystemInformation.HardwareModel), "srx") { return true } - if strings.HasPrefix(strings.ToLower(jnprSess.SystemInformation.HardwareModel), "vsrx") { + if strings.HasPrefix(strings.ToLower(junSess.SystemInformation.HardwareModel), "vsrx") { return true } - if strings.HasPrefix(strings.ToLower(jnprSess.SystemInformation.HardwareModel), "j") { + if strings.HasPrefix(strings.ToLower(junSess.SystemInformation.HardwareModel), "j") { return true } return false } -func checkCompatibilityRouter(jnprSess *NetconfObject) bool { - if strings.HasPrefix(strings.ToLower(jnprSess.SystemInformation.HardwareModel), "mx") { +func checkCompatibilityRouter(junSess *junosSession) bool { + if strings.HasPrefix(strings.ToLower(junSess.SystemInformation.HardwareModel), "mx") { return true } - if strings.HasPrefix(strings.ToLower(jnprSess.SystemInformation.HardwareModel), "vmx") { + if strings.HasPrefix(strings.ToLower(junSess.SystemInformation.HardwareModel), "vmx") { return true } diff --git a/junos/func_resource_bgp.go b/junos/func_resource_bgp.go index 9d9882bc..abd59ca4 100644 --- a/junos/func_resource_bgp.go +++ b/junos/func_resource_bgp.go @@ -63,8 +63,7 @@ type bgpOptions struct { gracefulRestart []map[string]interface{} } -func delBgpOpts(d *schema.ResourceData, typebgp string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delBgpOpts(d *schema.ResourceData, typebgp string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) delPrefix := deleteLS if d.Get("routing_instance").(string) != defaultW { @@ -115,11 +114,10 @@ func delBgpOpts(d *schema.ResourceData, typebgp string, m interface{}, jnprSess delPrefix+"type", ) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func setBgpOptsSimple(setPrefix string, d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setBgpOptsSimple(setPrefix string, d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := []string{setPrefix} if d.Get("accept_remote_nexthop").(bool) { configSet = append(configSet, setPrefix+"accept-remote-nexthop") @@ -270,7 +268,7 @@ func setBgpOptsSimple(setPrefix string, d *schema.ResourceData, m interface{}, j configSet = append(configSet, setPrefix+"remove-private") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func readBgpOptsSimple(item string, confRead *bgpOptions) error { @@ -422,9 +420,8 @@ func readBgpOptsSimple(item string, confRead *bgpOptions) error { return nil } -func setBgpOptsBfd(setPrefix string, bfdLivenessDetection []interface{}, m interface{}, jnprSess *NetconfObject, +func setBgpOptsBfd(setPrefix string, bfdLivenessDetection []interface{}, clt *Client, junSess *junosSession, ) error { - sess := m.(*Session) configSet := make([]string, 0) setPrefixBfd := setPrefix + "bfd-liveness-detection " @@ -477,7 +474,7 @@ func setBgpOptsBfd(setPrefix string, bfdLivenessDetection []interface{}, m inter } } if len(configSet) > 0 { - err := sess.configSet(configSet, jnprSess) + err := clt.configSet(configSet, junSess) if err != nil { return err } @@ -549,9 +546,8 @@ func readBgpOptsBfd(item string, bfdRead map[string]interface{}) error { } func setBgpOptsFamily( - setPrefix, familyType string, familyOptsList []interface{}, m interface{}, jnprSess *NetconfObject, + setPrefix, familyType string, familyOptsList []interface{}, clt *Client, junSess *junosSession, ) error { - sess := m.(*Session) configSet := make([]string, 0) setPrefixFamily := setPrefix + "family " switch familyType { @@ -597,7 +593,7 @@ func setBgpOptsFamily( } } if len(configSet) > 0 { - err := sess.configSet(configSet, jnprSess) + err := clt.configSet(configSet, junSess) if err != nil { return err } @@ -726,9 +722,8 @@ func readBgpOptsFamily(item, familyType string, opts []map[string]interface{}) ( return append(opts, readOpts), nil } -func setBgpOptsGrafefulRestart(setPrefix string, gracefulRestarts []interface{}, m interface{}, jnprSess *NetconfObject, +func setBgpOptsGrafefulRestart(setPrefix string, gracefulRestarts []interface{}, clt *Client, junSess *junosSession, ) error { - sess := m.(*Session) configSet := make([]string, 0) for _, v := range gracefulRestarts { @@ -748,7 +743,7 @@ func setBgpOptsGrafefulRestart(setPrefix string, gracefulRestarts []interface{}, } } if len(configSet) > 0 { - err := sess.configSet(configSet, jnprSess) + err := clt.configSet(configSet, junSess) if err != nil { return err } diff --git a/junos/netconf.go b/junos/netconf.go index ff438bf1..4f92a12f 100644 --- a/junos/netconf.go +++ b/junos/netconf.go @@ -29,15 +29,16 @@ const ( rpcClearCandidate = "" rpcClose = "" - rpcGetInterfaceInformationTerse = `` + rpcGetInterfacesInformationTerse = `` + rpcGetInterfaceInformationTerse = `%s` xmlStartTagConfigOut = "" xmlEndTagConfigOut = "" ) -// NetconfObject : store Junos device info and session. -type NetconfObject struct { - Session *netconf.Session +// junosSession : store Junos device info and session. +type junosSession struct { + session *netconf.Session SystemInformation sysInfo `xml:"system-information"` } @@ -69,7 +70,7 @@ type commitResults struct { Errors []netconf.RPCError `xml:"rpc-error"` } -type getInterfaceTerseReply struct { +type getPhysicalInterfaceTerseReply struct { InterfaceInfo struct { PhysicalInterface []struct { Name string `xml:"name"` @@ -79,11 +80,27 @@ type getInterfaceTerseReply struct { } `xml:"interface-information"` } -// netconfNewSession establishes a new connection to a NetconfObject device that we will use +type getLogicalInterfaceTerseReply struct { + InterfaceInfo struct { + LogicalInterface []struct { + Name string `xml:"name"` + AdminStatus string `xml:"admin-status"` + OperStatus string `xml:"oper-status"` + AddressFamily []struct { + Name string `xml:"address-family-name"` + Address []struct { + Local string `xml:"ifa-local"` + } `xml:"interface-address"` + } `xml:"address-family"` + } `xml:"logical-interface"` + } `xml:"interface-information"` +} + +// netconfNewSession establishes a new connection to a Junos device that we will use // to run our commands against. // Authentication methods are defined using the netconfAuthMethod struct, and are as follows: // username and password, SSH private key (with or without passphrase). -func netconfNewSession(ctx context.Context, host string, auth *netconfAuthMethod) (*NetconfObject, error) { +func netconfNewSession(ctx context.Context, host string, auth *netconfAuthMethod) (*junosSession, error) { clientConfig, err := genSSHClientConfig(auth) if err != nil { return nil, err @@ -92,10 +109,10 @@ func netconfNewSession(ctx context.Context, host string, auth *netconfAuthMethod return netconfNewSessionWithConfig(ctx, host, clientConfig) } -// netconfNewSessionWithConfig establishes a new connection to a NetconfObject device that we will use +// netconfNewSessionWithConfig establishes a new connection to a Junos device that we will use // to run our commands against. func netconfNewSessionWithConfig(ctx context.Context, host string, clientConfig *ssh.ClientConfig, -) (*NetconfObject, error) { +) (*junosSession, error) { netDialer := net.Dialer{ Timeout: clientConfig.Timeout, } @@ -112,9 +129,9 @@ func netconfNewSessionWithConfig(ctx context.Context, host string, clientConfig } // newSessionFromNetconf uses an existing netconf.Session to run our commands against. -func newSessionFromNetconf(s *netconf.Session) (*NetconfObject, error) { - n := &NetconfObject{ - Session: s, +func newSessionFromNetconf(s *netconf.Session) (*junosSession, error) { + n := &junosSession{ + session: s, } return n, n.gatherFacts() @@ -178,13 +195,9 @@ func defaultSSHCiphers() schema.SchemaDefaultFunc { } // gatherFacts gathers basic information about the device. -func (j *NetconfObject) gatherFacts() error { - if j == nil { - return errors.New("attempt to call GatherFacts on nil NetconfObject object") - } - s := j.Session +func (j *junosSession) gatherFacts() error { // Get info for get-system-information and populate SystemInformation Struct - val, err := s.Exec(netconf.RawMethod(rpcSystemInfo)) + val, err := j.session.Exec(netconf.RawMethod(rpcSystemInfo)) if err != nil { return fmt.Errorf("failed to netconf get-system-information: %w", err) } @@ -206,9 +219,9 @@ func (j *NetconfObject) gatherFacts() error { } // netconfCommand (show, execute) on Junos device. -func (j *NetconfObject) netconfCommand(cmd string) (string, error) { +func (j *junosSession) netconfCommand(cmd string) (string, error) { command := fmt.Sprintf(rpcCommand, cmd) - reply, err := j.Session.Exec(netconf.RawMethod(command)) + reply, err := j.session.Exec(netconf.RawMethod(command)) if err != nil { return "", fmt.Errorf("failed to netconf command exec: %w", err) } @@ -228,8 +241,8 @@ func (j *NetconfObject) netconfCommand(cmd string) (string, error) { return output.Config, nil } -func (j *NetconfObject) netconfCommandXML(cmd string) (string, error) { - reply, err := j.Session.Exec(netconf.RawMethod(cmd)) +func (j *junosSession) netconfCommandXML(cmd string) (string, error) { + reply, err := j.session.Exec(netconf.RawMethod(cmd)) if err != nil { return "", fmt.Errorf("failed to netconf xml command exec: %w", err) } @@ -242,9 +255,9 @@ func (j *NetconfObject) netconfCommandXML(cmd string) (string, error) { return reply.Data, nil } -func (j *NetconfObject) netconfConfigSet(cmd []string) (string, error) { +func (j *junosSession) netconfConfigSet(cmd []string) (string, error) { command := fmt.Sprintf(rpcConfigStringSet, strings.Join(cmd, "\n")) - reply, err := j.Session.Exec(netconf.RawMethod(command)) + reply, err := j.session.Exec(netconf.RawMethod(command)) if err != nil { return "", fmt.Errorf("failed to netconf set/delete command exec: %w", err) } @@ -262,8 +275,8 @@ func (j *NetconfObject) netconfConfigSet(cmd []string) (string, error) { } // netConfConfigLock locks the candidate configuration. -func (j *NetconfObject) netconfConfigLock() bool { - reply, err := j.Session.Exec(netconf.RawMethod(rpcCandidateLock)) +func (j *junosSession) netconfConfigLock() bool { + reply, err := j.session.Exec(netconf.RawMethod(rpcCandidateLock)) if err != nil { return false } @@ -275,8 +288,8 @@ func (j *NetconfObject) netconfConfigLock() bool { } // Unlock unlocks the candidate configuration. -func (j *NetconfObject) netconfConfigUnlock() []error { - reply, err := j.Session.Exec(netconf.RawMethod(rpcCandidateUnlock)) +func (j *junosSession) netconfConfigUnlock() []error { + reply, err := j.session.Exec(netconf.RawMethod(rpcCandidateUnlock)) if err != nil { return []error{fmt.Errorf("failed to netconf config unlock: %w", err)} } @@ -292,8 +305,8 @@ func (j *NetconfObject) netconfConfigUnlock() []error { return []error{} } -func (j *NetconfObject) netconfConfigClear() []error { - reply, err := j.Session.Exec(netconf.RawMethod(rpcClearCandidate)) +func (j *junosSession) netconfConfigClear() []error { + reply, err := j.session.Exec(netconf.RawMethod(rpcClearCandidate)) if err != nil { return []error{fmt.Errorf("failed to netconf config clear: %w", err)} } @@ -310,8 +323,8 @@ func (j *NetconfObject) netconfConfigClear() []error { } // netconfCommit commits the configuration. -func (j *NetconfObject) netconfCommit(logMessage string) (_warn []error, _err error) { - reply, err := j.Session.Exec(netconf.RawMethod(fmt.Sprintf(rpcCommit, logMessage))) +func (j *junosSession) netconfCommit(logMessage string) (_warn []error, _err error) { + reply, err := j.session.Exec(netconf.RawMethod(fmt.Sprintf(rpcCommit, logMessage))) if err != nil { return []error{}, fmt.Errorf("failed to netconf commit: %w", err) } @@ -352,9 +365,9 @@ func (j *NetconfObject) netconfCommit(logMessage string) (_warn []error, _err er } // Close disconnects our session to the device. -func (j *NetconfObject) close(sleepClosed int) error { - _, err := j.Session.Exec(netconf.RawMethod(rpcClose)) - j.Session.Transport.Close() +func (j *junosSession) close(sleepClosed int) error { + _, err := j.session.Exec(netconf.RawMethod(rpcClose)) + j.session.Transport.Close() if err != nil { sleep(sleepClosed) diff --git a/junos/provider.go b/junos/provider.go index 47d2474f..a6707a30 100644 --- a/junos/provider.go +++ b/junos/provider.go @@ -147,6 +147,8 @@ func Provider() *schema.Provider { "junos_policyoptions_policy_statement": resourcePolicyoptionsPolicyStatement(), "junos_policyoptions_prefix_list": resourcePolicyoptionsPrefixList(), "junos_rib_group": resourceRibGroup(), + "junos_rip_group": resourceRipGroup(), + "junos_rip_neighbor": resourceRipNeighbor(), "junos_routing_instance": resourceRoutingInstance(), "junos_routing_options": resourceRoutingOptions(), "junos_rstp": resourceRstp(), @@ -223,6 +225,7 @@ func Provider() *schema.Provider { DataSourcesMap: map[string]*schema.Resource{ "junos_interface": dataSourceInterface(), "junos_interface_logical": dataSourceInterfaceLogical(), + "junos_interface_logical_info": dataSourceInterfaceLogicalInfo(), "junos_interface_physical": dataSourceInterfacePhysical(), "junos_interfaces_physical_present": dataSourceInterfacesPhysicalPresent(), "junos_routing_instance": dataSourceRoutingInstance(), @@ -263,5 +266,5 @@ func configureProvider(ctx context.Context, d *schema.ResourceData) (interface{} c.junosSSHCiphers = append(c.junosSSHCiphers, v.(string)) } - return c.prepareSession() + return c.newClient() } diff --git a/junos/resource_access_address_assignment_pool.go b/junos/resource_access_address_assignment_pool.go index d7b6432e..69b55915 100644 --- a/junos/resource_access_address_assignment_pool.go +++ b/junos/resource_access_address_assignment_pool.go @@ -475,66 +475,70 @@ func resourceAccessAddressAssignPool() *schema.Resource { func resourceAccessAddressAssignPoolCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setAccessAddressAssignPool(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setAccessAddressAssignPool(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string) + idSeparator + d.Get("routing_instance").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if d.Get("routing_instance").(string) != defaultW { - instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !instanceExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("routing instance %v doesn't exist", d.Get("routing_instance").(string)))...) } } accessAddressAssignPoolExists, err := checkAccessAddressAssignPoolExists( - d.Get("name").(string), d.Get("routing_instance").(string), m, jnprSess) + d.Get("name").(string), + d.Get("routing_instance").(string), + clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if accessAddressAssignPoolExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr( fmt.Errorf("access address-assignment pool %v already exists in routing-instance %s", d.Get("name").(string), d.Get("routing_instance").(string)))...) } - if err := setAccessAddressAssignPool(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setAccessAddressAssignPool(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_access_address_assignment_pool", jnprSess) + warns, err := clt.commitConf("create resource junos_access_address_assignment_pool", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } accessAddressAssignPoolExists, err = checkAccessAddressAssignPoolExists( - d.Get("name").(string), d.Get("routing_instance").(string), m, jnprSess) + d.Get("name").(string), + d.Get("routing_instance").(string), + clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -546,25 +550,27 @@ func resourceAccessAddressAssignPoolCreate(ctx context.Context, d *schema.Resour "=> check your config", d.Get("name").(string), d.Get("routing_instance").(string)))...) } - return append(diagWarns, resourceAccessAddressAssignPoolReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceAccessAddressAssignPoolReadWJunSess(d, clt, junSess)...) } func resourceAccessAddressAssignPoolRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceAccessAddressAssignPoolReadWJnprSess(d, m, jnprSess) + return resourceAccessAddressAssignPoolReadWJunSess(d, clt, junSess) } -func resourceAccessAddressAssignPoolReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceAccessAddressAssignPoolReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() accessAddressAssignPoolOptions, err := readAccessAddressAssignPool( - d.Get("name").(string), d.Get("routing_instance").(string), m, jnprSess) + d.Get("name").(string), + d.Get("routing_instance").(string), + clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -581,81 +587,93 @@ func resourceAccessAddressAssignPoolReadWJnprSess(d *schema.ResourceData, m inte func resourceAccessAddressAssignPoolUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { + clt := m.(*Client) + if clt.fakeUpdateAlso { if err := delAccessAddressAssignPool( - d.Get("name").(string), d.Get("routing_instance").(string), m, nil); err != nil { + d.Get("name").(string), + d.Get("routing_instance").(string), + clt, nil, + ); err != nil { return diag.FromErr(err) } - if err := setAccessAddressAssignPool(d, m, nil); err != nil { + if err := setAccessAddressAssignPool(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delAccessAddressAssignPool(d.Get("name").(string), d.Get("routing_instance").(string), - m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delAccessAddressAssignPool( + d.Get("name").(string), + d.Get("routing_instance").(string), + clt, junSess, + ); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setAccessAddressAssignPool(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setAccessAddressAssignPool(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_access_address_assignment_pool", jnprSess) + warns, err := clt.commitConf("update resource junos_access_address_assignment_pool", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceAccessAddressAssignPoolReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceAccessAddressAssignPoolReadWJunSess(d, clt, junSess)...) } func resourceAccessAddressAssignPoolDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { + clt := m.(*Client) + if clt.fakeDeleteAlso { if err := delAccessAddressAssignPool( - d.Get("name").(string), d.Get("routing_instance").(string), m, nil); err != nil { + d.Get("name").(string), + d.Get("routing_instance").(string), + clt, nil, + ); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delAccessAddressAssignPool(d.Get("name").(string), d.Get("routing_instance").(string), - m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delAccessAddressAssignPool( + d.Get("name").(string), + d.Get("routing_instance").(string), + clt, junSess, + ); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_access_address_assignment_pool", jnprSess) + warns, err := clt.commitConf("delete resource junos_access_address_assignment_pool", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -665,18 +683,18 @@ func resourceAccessAddressAssignPoolDelete(ctx context.Context, d *schema.Resour func resourceAccessAddressAssignPoolImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) idSplit := strings.Split(d.Id(), idSeparator) if len(idSplit) < 2 { return nil, fmt.Errorf("missing element(s) in id with separator %v", idSeparator) } - accessAddressAssignPoolExists, err := checkAccessAddressAssignPoolExists(idSplit[0], idSplit[1], m, jnprSess) + accessAddressAssignPoolExists, err := checkAccessAddressAssignPoolExists(idSplit[0], idSplit[1], clt, junSess) if err != nil { return nil, err } @@ -684,7 +702,7 @@ func resourceAccessAddressAssignPoolImport(ctx context.Context, d *schema.Resour return nil, fmt.Errorf("don't find access address-assignment pool with id '%v' (id must be "+ ""+idSeparator+")", d.Id()) } - accessAddressAssignPoolOptions, err := readAccessAddressAssignPool(idSplit[0], idSplit[1], m, jnprSess) + accessAddressAssignPoolOptions, err := readAccessAddressAssignPool(idSplit[0], idSplit[1], clt, junSess) if err != nil { return nil, err } @@ -695,19 +713,18 @@ func resourceAccessAddressAssignPoolImport(ctx context.Context, d *schema.Resour return result, nil } -func checkAccessAddressAssignPoolExists(name, instance string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) +func checkAccessAddressAssignPoolExists(name, instance string, clt *Client, junSess *junosSession) (bool, error) { var showConfig string var err error if instance == defaultW { - showConfig, err = sess.command(cmdShowConfig+ - "access address-assignment pool "+name+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "access address-assignment pool "+name+pipeDisplaySet, junSess) if err != nil { return false, err } } else { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+instance+" "+ - "access address-assignment pool "+name+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+instance+" "+ + "access address-assignment pool "+name+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -720,8 +737,7 @@ func checkAccessAddressAssignPoolExists(name, instance string, m interface{}, jn return true, nil } -func setAccessAddressAssignPool(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setAccessAddressAssignPool(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := setLS @@ -749,7 +765,7 @@ func setAccessAddressAssignPool(d *schema.ResourceData, m interface{}, jnprSess configSet = append(configSet, setPrefix+"link "+v) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func setAccessAddressAssignPoolFamily(family map[string]interface{}, setPrefix string) ([]string, error) { @@ -1050,19 +1066,18 @@ func setAccessAddressAssignPoolFamilyDhcpAttributes(dhcpAttr map[string]interfac return configSet, nil } -func readAccessAddressAssignPool(name, instance string, m interface{}, jnprSess *NetconfObject, +func readAccessAddressAssignPool(name, instance string, clt *Client, junSess *junosSession, ) (accessAddressAssignPoolOptions, error) { - sess := m.(*Session) var confRead accessAddressAssignPoolOptions var showConfig string var err error if instance == defaultW { - showConfig, err = sess.command(cmdShowConfig+ - "access address-assignment pool "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "access address-assignment pool "+name+pipeDisplaySetRelative, junSess) } else { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+instance+" "+ - "access address-assignment pool "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+instance+" "+ + "access address-assignment pool "+name+pipeDisplaySetRelative, junSess) } if err != nil { return confRead, err @@ -1348,8 +1363,7 @@ func readAccessAddressAssignPoolFamily(itemTrim string, family map[string]interf return nil } -func delAccessAddressAssignPool(name, instance string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delAccessAddressAssignPool(name, instance string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) if instance == defaultW { configSet = append(configSet, "delete access address-assignment pool "+name) @@ -1357,7 +1371,7 @@ func delAccessAddressAssignPool(name, instance string, m interface{}, jnprSess * configSet = append(configSet, delRoutingInstances+instance+" access address-assignment pool "+name) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillAccessAddressAssignPoolData( diff --git a/junos/resource_aggregate_route.go b/junos/resource_aggregate_route.go index 3fcf7f00..6be71d10 100644 --- a/junos/resource_aggregate_route.go +++ b/junos/resource_aggregate_route.go @@ -127,65 +127,69 @@ func resourceAggregateRoute() *schema.Resource { } func resourceAggregateRouteCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setAggregateRoute(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setAggregateRoute(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("destination").(string) + idSeparator + d.Get("routing_instance").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if d.Get("routing_instance").(string) != defaultW { - instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !instanceExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("routing instance %v doesn't exist", d.Get("routing_instance").(string)))...) } } aggregateRouteExists, err := checkAggregateRouteExists( - d.Get("destination").(string), d.Get("routing_instance").(string), m, jnprSess) + d.Get("destination").(string), + d.Get("routing_instance").(string), + clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if aggregateRouteExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("aggregate route %v already exists on table %s", d.Get("destination").(string), d.Get("routing_instance").(string)))...) } - if err := setAggregateRoute(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setAggregateRoute(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_aggregate_route", jnprSess) + warns, err := clt.commitConf("create resource junos_aggregate_route", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } aggregateRouteExists, err = checkAggregateRouteExists( - d.Get("destination").(string), d.Get("routing_instance").(string), m, jnprSess) + d.Get("destination").(string), + d.Get("routing_instance").(string), + clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -197,25 +201,27 @@ func resourceAggregateRouteCreate(ctx context.Context, d *schema.ResourceData, m "=> check your config", d.Get("destination").(string), d.Get("routing_instance").(string)))...) } - return append(diagWarns, resourceAggregateRouteReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceAggregateRouteReadWJunSess(d, clt, junSess)...) } func resourceAggregateRouteRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceAggregateRouteReadWJnprSess(d, m, jnprSess) + return resourceAggregateRouteReadWJunSess(d, clt, junSess) } -func resourceAggregateRouteReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceAggregateRouteReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - aggregateRouteOptions, err := readAggregateRoute(d.Get("destination").(string), d.Get("routing_instance").(string), - m, jnprSess) + aggregateRouteOptions, err := readAggregateRoute( + d.Get("destination").(string), + d.Get("routing_instance").(string), + clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -231,80 +237,92 @@ func resourceAggregateRouteReadWJnprSess(d *schema.ResourceData, m interface{}, func resourceAggregateRouteUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { + clt := m.(*Client) + if clt.fakeUpdateAlso { if err := delAggregateRoute( - d.Get("destination").(string), d.Get("routing_instance").(string), m, nil); err != nil { + d.Get("destination").(string), + d.Get("routing_instance").(string), + clt, nil, + ); err != nil { return diag.FromErr(err) } - if err := setAggregateRoute(d, m, nil); err != nil { + if err := setAggregateRoute(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delAggregateRoute(d.Get("destination").(string), d.Get("routing_instance").(string), - m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delAggregateRoute( + d.Get("destination").(string), + d.Get("routing_instance").(string), + clt, junSess, + ); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setAggregateRoute(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setAggregateRoute(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_aggregate_route", jnprSess) + warns, err := clt.commitConf("update resource junos_aggregate_route", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceAggregateRouteReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceAggregateRouteReadWJunSess(d, clt, junSess)...) } func resourceAggregateRouteDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { + clt := m.(*Client) + if clt.fakeDeleteAlso { if err := delAggregateRoute( - d.Get("destination").(string), d.Get("routing_instance").(string), m, nil); err != nil { + d.Get("destination").(string), + d.Get("routing_instance").(string), + clt, nil, + ); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delAggregateRoute(d.Get("destination").(string), d.Get("routing_instance").(string), - m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delAggregateRoute( + d.Get("destination").(string), + d.Get("routing_instance").(string), + clt, junSess, + ); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_aggregate_route", jnprSess) + warns, err := clt.commitConf("delete resource junos_aggregate_route", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -314,18 +332,18 @@ func resourceAggregateRouteDelete(ctx context.Context, d *schema.ResourceData, m func resourceAggregateRouteImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) idSplit := strings.Split(d.Id(), idSeparator) if len(idSplit) < 2 { return nil, fmt.Errorf("missing element(s) in id with separator %v", idSeparator) } - aggregateRouteExists, err := checkAggregateRouteExists(idSplit[0], idSplit[1], m, jnprSess) + aggregateRouteExists, err := checkAggregateRouteExists(idSplit[0], idSplit[1], clt, junSess) if err != nil { return nil, err } @@ -333,7 +351,7 @@ func resourceAggregateRouteImport(ctx context.Context, d *schema.ResourceData, m return nil, fmt.Errorf("don't find aggregate route with id '%v' (id must be "+ ""+idSeparator+")", d.Id()) } - aggregateRouteOptions, err := readAggregateRoute(idSplit[0], idSplit[1], m, jnprSess) + aggregateRouteOptions, err := readAggregateRoute(idSplit[0], idSplit[1], clt, junSess) if err != nil { return nil, err } @@ -344,34 +362,33 @@ func resourceAggregateRouteImport(ctx context.Context, d *schema.ResourceData, m return result, nil } -func checkAggregateRouteExists(destination, instance string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) +func checkAggregateRouteExists(destination, instance string, clt *Client, junSess *junosSession) (bool, error) { var showConfig string var err error if instance == defaultW { if !strings.Contains(destination, ":") { - showConfig, err = sess.command(cmdShowConfig+ - "routing-options aggregate route "+destination+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "routing-options aggregate route "+destination+pipeDisplaySet, junSess) if err != nil { return false, err } } else { - showConfig, err = sess.command(cmdShowConfig+ - "routing-options rib inet6.0 aggregate route "+destination+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "routing-options rib inet6.0 aggregate route "+destination+pipeDisplaySet, junSess) if err != nil { return false, err } } } else { if !strings.Contains(destination, ":") { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+instance+" "+ - "routing-options aggregate route "+destination+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+instance+" "+ + "routing-options aggregate route "+destination+pipeDisplaySet, junSess) if err != nil { return false, err } } else { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+instance+" "+ - "routing-options rib "+instance+".inet6.0 aggregate route "+destination+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+instance+" "+ + "routing-options rib "+instance+".inet6.0 aggregate route "+destination+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -385,8 +402,7 @@ func checkAggregateRouteExists(destination, instance string, m interface{}, jnpr return true, nil } -func setAggregateRoute(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setAggregateRoute(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) var setPrefix string @@ -450,31 +466,30 @@ func setAggregateRoute(d *schema.ResourceData, m interface{}, jnprSess *NetconfO configSet = append(configSet, setPrefix+" preference "+strconv.Itoa(d.Get("preference").(int))) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readAggregateRoute(destination, instance string, m interface{}, jnprSess *NetconfObject, +func readAggregateRoute(destination, instance string, clt *Client, junSess *junosSession, ) (aggregateRouteOptions, error) { - sess := m.(*Session) var confRead aggregateRouteOptions var showConfig string var err error if instance == defaultW { if !strings.Contains(destination, ":") { - showConfig, err = sess.command(cmdShowConfig+ - "routing-options aggregate route "+destination+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "routing-options aggregate route "+destination+pipeDisplaySetRelative, junSess) } else { - showConfig, err = sess.command(cmdShowConfig+ - "routing-options rib inet6.0 aggregate route "+destination+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "routing-options rib inet6.0 aggregate route "+destination+pipeDisplaySetRelative, junSess) } } else { if !strings.Contains(destination, ":") { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+instance+" "+ - "routing-options aggregate route "+destination+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+instance+" "+ + "routing-options aggregate route "+destination+pipeDisplaySetRelative, junSess) } else { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+instance+" "+ - "routing-options rib "+instance+".inet6.0 aggregate route "+destination+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+instance+" "+ + "routing-options rib "+instance+".inet6.0 aggregate route "+destination+pipeDisplaySetRelative, junSess) } } if err != nil { @@ -534,8 +549,7 @@ func readAggregateRoute(destination, instance string, m interface{}, jnprSess *N return confRead, nil } -func delAggregateRoute(destination, instance string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delAggregateRoute(destination, instance string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) if instance == defaultW { if !strings.Contains(destination, ":") { @@ -553,7 +567,7 @@ func delAggregateRoute(destination, instance string, m interface{}, jnprSess *Ne } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillAggregateRouteData(d *schema.ResourceData, aggregateRouteOptions aggregateRouteOptions) { diff --git a/junos/resource_application.go b/junos/resource_application.go index 31da65e0..6eb90387 100644 --- a/junos/resource_application.go +++ b/junos/resource_application.go @@ -176,48 +176,48 @@ func resourceApplication() *schema.Resource { } func resourceApplicationCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setApplication(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setApplication(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - appExists, err := checkApplicationExists(d.Get("name").(string), m, jnprSess) + appExists, err := checkApplicationExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if appExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("application %v already exists", d.Get("name").(string)))...) } - if err := setApplication(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setApplication(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_application", jnprSess) + warns, err := clt.commitConf("create resource junos_application", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - appExists, err = checkApplicationExists(d.Get("name").(string), m, jnprSess) + appExists, err = checkApplicationExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -228,23 +228,23 @@ func resourceApplicationCreate(ctx context.Context, d *schema.ResourceData, m in "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceApplicationReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceApplicationReadWJunSess(d, clt, junSess)...) } func resourceApplicationRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceApplicationReadWJnprSess(d, m, jnprSess) + return resourceApplicationReadWJunSess(d, clt, junSess) } -func resourceApplicationReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) diag.Diagnostics { +func resourceApplicationReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession) diag.Diagnostics { mutex.Lock() - applicationOptions, err := readApplication(d.Get("name").(string), m, jnprSess) + applicationOptions, err := readApplication(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -260,76 +260,76 @@ func resourceApplicationReadWJnprSess(d *schema.ResourceData, m interface{}, jnp func resourceApplicationUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delApplication(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delApplication(d, clt, nil); err != nil { return diag.FromErr(err) } - if err := setApplication(d, m, nil); err != nil { + if err := setApplication(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delApplication(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delApplication(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setApplication(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setApplication(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_application", jnprSess) + warns, err := clt.commitConf("update resource junos_application", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceApplicationReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceApplicationReadWJunSess(d, clt, junSess)...) } func resourceApplicationDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delApplication(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delApplication(d, clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delApplication(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delApplication(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_application", jnprSess) + warns, err := clt.commitConf("delete resource junos_application", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -339,21 +339,21 @@ func resourceApplicationDelete(ctx context.Context, d *schema.ResourceData, m in func resourceApplicationImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - appExists, err := checkApplicationExists(d.Id(), m, jnprSess) + appExists, err := checkApplicationExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !appExists { return nil, fmt.Errorf("don't find application with id '%v' (id must be )", d.Id()) } - applicationOptions, err := readApplication(d.Id(), m, jnprSess) + applicationOptions, err := readApplication(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -363,10 +363,9 @@ func resourceApplicationImport(ctx context.Context, d *schema.ResourceData, m in return result, nil } -func checkApplicationExists(application string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "applications application "+application+pipeDisplaySet, jnprSess) +func checkApplicationExists(application string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "applications application "+application+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -377,8 +376,7 @@ func checkApplicationExists(application string, m interface{}, jnprSess *Netconf return true, nil } -func setApplication(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setApplication(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set applications application " + d.Get("name").(string) + " " @@ -415,7 +413,7 @@ func setApplication(d *schema.ResourceData, m interface{}, jnprSess *NetconfObje return fmt.Errorf("multiple blocks term with the same name %s", term["name"].(string)) } termName = append(termName, term["name"].(string)) - if err := setApplicationTerm(setPrefix, term, sess, jnprSess); err != nil { + if err := setApplicationTerm(setPrefix, term, clt, junSess); err != nil { return err } } @@ -423,10 +421,10 @@ func setApplication(d *schema.ResourceData, m interface{}, jnprSess *NetconfObje configSet = append(configSet, setPrefix+"uuid "+v) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func setApplicationTerm(setApp string, term map[string]interface{}, sess *Session, jnprSess *NetconfObject) error { +func setApplicationTerm(setApp string, term map[string]interface{}, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := setApp + "term " + term["name"].(string) + " " @@ -469,15 +467,14 @@ func setApplicationTerm(setApp string, term map[string]interface{}, sess *Sessio configSet = append(configSet, setPrefix+"uuid "+v) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readApplication(application string, m interface{}, jnprSess *NetconfObject) (applicationOptions, error) { - sess := m.(*Session) +func readApplication(application string, clt *Client, junSess *junosSession) (applicationOptions, error) { var confRead applicationOptions - showConfig, err := sess.command(cmdShowConfig+ - "applications application "+application+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "applications application "+application+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -580,12 +577,11 @@ func readApplicationTerm(itemTrim string, term map[string]interface{}) error { return nil } -func delApplication(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delApplication(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete applications application "+d.Get("name").(string)) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillApplicationData(d *schema.ResourceData, applicationOptions applicationOptions) { diff --git a/junos/resource_application_set.go b/junos/resource_application_set.go index 06475079..800c7fc1 100644 --- a/junos/resource_application_set.go +++ b/junos/resource_application_set.go @@ -44,48 +44,48 @@ func resourceApplicationSet() *schema.Resource { } func resourceApplicationSetCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setApplicationSet(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setApplicationSet(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - appSetExists, err := checkApplicationSetExists(d.Get("name").(string), m, jnprSess) + appSetExists, err := checkApplicationSetExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if appSetExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("application-set %v already exists", d.Get("name").(string)))...) } - if err := setApplicationSet(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setApplicationSet(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_application_set", jnprSess) + warns, err := clt.commitConf("create resource junos_application_set", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - appSetExists, err = checkApplicationSetExists(d.Get("name").(string), m, jnprSess) + appSetExists, err = checkApplicationSetExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -96,24 +96,24 @@ func resourceApplicationSetCreate(ctx context.Context, d *schema.ResourceData, m "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceApplicationSetReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceApplicationSetReadWJunSess(d, clt, junSess)...) } func resourceApplicationSetRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceApplicationSetReadWJnprSess(d, m, jnprSess) + return resourceApplicationSetReadWJunSess(d, clt, junSess) } -func resourceApplicationSetReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceApplicationSetReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - applicationSetOptions, err := readApplicationSet(d.Get("name").(string), m, jnprSess) + applicationSetOptions, err := readApplicationSet(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -129,76 +129,76 @@ func resourceApplicationSetReadWJnprSess(d *schema.ResourceData, m interface{}, func resourceApplicationSetUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delApplicationSet(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delApplicationSet(d, clt, nil); err != nil { return diag.FromErr(err) } - if err := setApplicationSet(d, m, nil); err != nil { + if err := setApplicationSet(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delApplicationSet(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delApplicationSet(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setApplicationSet(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setApplicationSet(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_application_set", jnprSess) + warns, err := clt.commitConf("update resource junos_application_set", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceApplicationSetReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceApplicationSetReadWJunSess(d, clt, junSess)...) } func resourceApplicationSetDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delApplicationSet(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delApplicationSet(d, clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delApplicationSet(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delApplicationSet(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_application_set", jnprSess) + warns, err := clt.commitConf("delete resource junos_application_set", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -208,21 +208,21 @@ func resourceApplicationSetDelete(ctx context.Context, d *schema.ResourceData, m func resourceApplicationSetImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - appSetExists, err := checkApplicationSetExists(d.Id(), m, jnprSess) + appSetExists, err := checkApplicationSetExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !appSetExists { return nil, fmt.Errorf("don't find application-set with id '%v' (id must be )", d.Id()) } - applicationSetOptions, err := readApplicationSet(d.Id(), m, jnprSess) + applicationSetOptions, err := readApplicationSet(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -232,10 +232,9 @@ func resourceApplicationSetImport(ctx context.Context, d *schema.ResourceData, m return result, nil } -func checkApplicationSetExists(applicationSet string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "applications application-set "+applicationSet+pipeDisplaySet, jnprSess) +func checkApplicationSetExists(applicationSet string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "applications application-set "+applicationSet+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -246,8 +245,7 @@ func checkApplicationSetExists(applicationSet string, m interface{}, jnprSess *N return true, nil } -func setApplicationSet(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setApplicationSet(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set applications application-set " + d.Get("name").(string) @@ -255,15 +253,14 @@ func setApplicationSet(d *schema.ResourceData, m interface{}, jnprSess *NetconfO configSet = append(configSet, setPrefix+" application "+v.(string)) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readApplicationSet(applicationSet string, m interface{}, jnprSess *NetconfObject) (applicationSetOptions, error) { - sess := m.(*Session) +func readApplicationSet(applicationSet string, clt *Client, junSess *junosSession) (applicationSetOptions, error) { var confRead applicationSetOptions - showConfig, err := sess.command(cmdShowConfig+ - "applications application-set "+applicationSet+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "applications application-set "+applicationSet+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -286,12 +283,11 @@ func readApplicationSet(applicationSet string, m interface{}, jnprSess *NetconfO return confRead, nil } -func delApplicationSet(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delApplicationSet(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete applications application-set "+d.Get("name").(string)) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillApplicationSetData(d *schema.ResourceData, applicationSetOptions applicationSetOptions) { diff --git a/junos/resource_bgp_group.go b/junos/resource_bgp_group.go index d84e26f7..d05efd74 100644 --- a/junos/resource_bgp_group.go +++ b/junos/resource_bgp_group.go @@ -604,63 +604,63 @@ func resourceBgpGroup() *schema.Resource { } func resourceBgpGroupCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setBgpGroup(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setBgpGroup(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string) + idSeparator + d.Get("routing_instance").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if d.Get("routing_instance").(string) != defaultW { - instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !instanceExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("routing instance %v doesn't exist", d.Get("routing_instance").(string)))...) } } - bgpGroupxists, err := checkBgpGroupExists(d.Get("name").(string), d.Get("routing_instance").(string), m, jnprSess) + bgpGroupxists, err := checkBgpGroupExists(d.Get("name").(string), d.Get("routing_instance").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if bgpGroupxists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("bgp group %v already exists in routing-instance %v", d.Get("name").(string), d.Get("routing_instance").(string)))...) } - if err := setBgpGroup(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setBgpGroup(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_bgp_group", jnprSess) + warns, err := clt.commitConf("create resource junos_bgp_group", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - bgpGroupxists, err = checkBgpGroupExists(d.Get("name").(string), d.Get("routing_instance").(string), m, jnprSess) + bgpGroupxists, err = checkBgpGroupExists(d.Get("name").(string), d.Get("routing_instance").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -671,23 +671,23 @@ func resourceBgpGroupCreate(ctx context.Context, d *schema.ResourceData, m inter "=> check your config", d.Get("name").(string), d.Get("routing_instance").(string)))...) } - return append(diagWarns, resourceBgpGroupReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceBgpGroupReadWJunSess(d, clt, junSess)...) } func resourceBgpGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceBgpGroupReadWJnprSess(d, m, jnprSess) + return resourceBgpGroupReadWJunSess(d, clt, junSess) } -func resourceBgpGroupReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) diag.Diagnostics { +func resourceBgpGroupReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession) diag.Diagnostics { mutex.Lock() - bgpGroupOptions, err := readBgpGroup(d.Get("name").(string), d.Get("routing_instance").(string), m, jnprSess) + bgpGroupOptions, err := readBgpGroup(d.Get("name").(string), d.Get("routing_instance").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -703,76 +703,76 @@ func resourceBgpGroupReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSe func resourceBgpGroupUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delBgpOpts(d, "group", m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delBgpOpts(d, "group", clt, nil); err != nil { return diag.FromErr(err) } - if err := setBgpGroup(d, m, nil); err != nil { + if err := setBgpGroup(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delBgpOpts(d, "group", m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delBgpOpts(d, "group", clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setBgpGroup(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setBgpGroup(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_bgp_group", jnprSess) + warns, err := clt.commitConf("update resource junos_bgp_group", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceBgpGroupReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceBgpGroupReadWJunSess(d, clt, junSess)...) } func resourceBgpGroupDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delBgpGroup(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delBgpGroup(d, clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delBgpGroup(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delBgpGroup(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_bgp_group", jnprSess) + warns, err := clt.commitConf("delete resource junos_bgp_group", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -782,18 +782,18 @@ func resourceBgpGroupDelete(ctx context.Context, d *schema.ResourceData, m inter func resourceBgpGroupImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) idSplit := strings.Split(d.Id(), idSeparator) if len(idSplit) < 2 { return nil, fmt.Errorf("missing element(s) in id with separator %v", idSeparator) } - bgpGroupxists, err := checkBgpGroupExists(idSplit[0], idSplit[1], m, jnprSess) + bgpGroupxists, err := checkBgpGroupExists(idSplit[0], idSplit[1], clt, junSess) if err != nil { return nil, err } @@ -801,7 +801,7 @@ func resourceBgpGroupImport(ctx context.Context, d *schema.ResourceData, m inter return nil, fmt.Errorf("don't find bgp group with id '%v' "+ "(id must be "+idSeparator+")", d.Id()) } - bgpGroupOptions, err := readBgpGroup(idSplit[0], idSplit[1], m, jnprSess) + bgpGroupOptions, err := readBgpGroup(idSplit[0], idSplit[1], clt, junSess) if err != nil { return nil, err } @@ -811,19 +811,18 @@ func resourceBgpGroupImport(ctx context.Context, d *schema.ResourceData, m inter return result, nil } -func checkBgpGroupExists(bgpGroup, instance string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) +func checkBgpGroupExists(bgpGroup, instance string, clt *Client, junSess *junosSession) (bool, error) { var showConfig string var err error if instance == defaultW { - showConfig, err = sess.command(cmdShowConfig+ - "protocols bgp group "+bgpGroup+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "protocols bgp group "+bgpGroup+pipeDisplaySet, junSess) if err != nil { return false, err } } else { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+instance+" "+ - "protocols bgp group "+bgpGroup+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+instance+" "+ + "protocols bgp group "+bgpGroup+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -835,16 +834,14 @@ func checkBgpGroupExists(bgpGroup, instance string, m interface{}, jnprSess *Net return true, nil } -func setBgpGroup(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) - +func setBgpGroup(d *schema.ResourceData, clt *Client, junSess *junosSession) error { setPrefix := setLS if d.Get("routing_instance").(string) != defaultW { setPrefix = setRoutingInstances + d.Get("routing_instance").(string) + " " } setPrefix += "protocols bgp group " + d.Get("name").(string) + " " - if err := sess.configSet([]string{setPrefix + "type " + d.Get("type").(string)}, jnprSess); err != nil { + if err := clt.configSet([]string{setPrefix + "type " + d.Get("type").(string)}, junSess); err != nil { return err } if d.Get("type").(string) == "external" { @@ -855,27 +852,26 @@ func setBgpGroup(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) return fmt.Errorf("conflict between type=external and accept_remote_nexthop + multihop") } } - if err := setBgpOptsSimple(setPrefix, d, m, jnprSess); err != nil { + if err := setBgpOptsSimple(setPrefix, d, clt, junSess); err != nil { return err } - if err := setBgpOptsBfd(setPrefix, d.Get("bfd_liveness_detection").([]interface{}), m, jnprSess); err != nil { + if err := setBgpOptsBfd(setPrefix, d.Get("bfd_liveness_detection").([]interface{}), clt, junSess); err != nil { return err } - if err := setBgpOptsFamily(setPrefix, "evpn", d.Get("family_evpn").([]interface{}), m, jnprSess); err != nil { + if err := setBgpOptsFamily(setPrefix, "evpn", d.Get("family_evpn").([]interface{}), clt, junSess); err != nil { return err } - if err := setBgpOptsFamily(setPrefix, inetW, d.Get("family_inet").([]interface{}), m, jnprSess); err != nil { + if err := setBgpOptsFamily(setPrefix, inetW, d.Get("family_inet").([]interface{}), clt, junSess); err != nil { return err } - if err := setBgpOptsFamily(setPrefix, inet6W, d.Get("family_inet6").([]interface{}), m, jnprSess); err != nil { + if err := setBgpOptsFamily(setPrefix, inet6W, d.Get("family_inet6").([]interface{}), clt, junSess); err != nil { return err } - return setBgpOptsGrafefulRestart(setPrefix, d.Get("graceful_restart").([]interface{}), m, jnprSess) + return setBgpOptsGrafefulRestart(setPrefix, d.Get("graceful_restart").([]interface{}), clt, junSess) } -func readBgpGroup(bgpGroup, instance string, m interface{}, jnprSess *NetconfObject) (bgpOptions, error) { - sess := m.(*Session) +func readBgpGroup(bgpGroup, instance string, clt *Client, junSess *junosSession) (bgpOptions, error) { var confRead bgpOptions var showConfig string var err error @@ -885,14 +881,14 @@ func readBgpGroup(bgpGroup, instance string, m interface{}, jnprSess *NetconfObj confRead.preference = -1 if instance == defaultW { - showConfig, err = sess.command(cmdShowConfig+ - "protocols bgp group "+bgpGroup+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "protocols bgp group "+bgpGroup+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } } else { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+instance+" "+ - "protocols bgp group "+bgpGroup+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+instance+" "+ + "protocols bgp group "+bgpGroup+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -968,8 +964,7 @@ func readBgpGroup(bgpGroup, instance string, m interface{}, jnprSess *NetconfObj return confRead, nil } -func delBgpGroup(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delBgpGroup(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) if d.Get("routing_instance").(string) == defaultW { configSet = append(configSet, "delete protocols bgp group "+d.Get("name").(string)) @@ -978,7 +973,7 @@ func delBgpGroup(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) " protocols bgp group "+d.Get("name").(string)) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillBgpGroupData(d *schema.ResourceData, bgpGroupOptions bgpOptions) { diff --git a/junos/resource_bgp_neighbor.go b/junos/resource_bgp_neighbor.go index 42bc559a..7aa6dabf 100644 --- a/junos/resource_bgp_neighbor.go +++ b/junos/resource_bgp_neighbor.go @@ -596,9 +596,9 @@ func resourceBgpNeighbor() *schema.Resource { } func resourceBgpNeighborCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setBgpNeighbor(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setBgpNeighbor(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("ip").(string) + @@ -607,67 +607,73 @@ func resourceBgpNeighborCreate(ctx context.Context, d *schema.ResourceData, m in return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if d.Get("routing_instance").(string) != defaultW { - instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !instanceExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("routing instance %v doesn't exist", d.Get("routing_instance").(string)))...) } } - bgpGroupExists, err := checkBgpGroupExists(d.Get("group").(string), d.Get("routing_instance").(string), m, jnprSess) + bgpGroupExists, err := checkBgpGroupExists(d.Get("group").(string), d.Get("routing_instance").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !bgpGroupExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("bgp group %v doesn't exist", d.Get("group").(string)))...) } - bgpNeighborxists, err := checkBgpNeighborExists(d.Get("ip").(string), - d.Get("routing_instance").(string), d.Get("group").(string), m, jnprSess) + bgpNeighborxists, err := checkBgpNeighborExists( + d.Get("ip").(string), + d.Get("routing_instance").(string), + d.Get("group").(string), + clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if bgpNeighborxists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("bgp neighbor %v already exists in group %v (routing-instance %v)", d.Get("ip").(string), d.Get("group").(string), d.Get("routing_instance").(string)))...) } - if err := setBgpNeighbor(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setBgpNeighbor(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_bgp_neighbor", jnprSess) + warns, err := clt.commitConf("create resource junos_bgp_neighbor", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - bgpNeighborxists, err = checkBgpNeighborExists(d.Get("ip").(string), - d.Get("routing_instance").(string), d.Get("group").(string), m, jnprSess) + bgpNeighborxists, err = checkBgpNeighborExists( + d.Get("ip").(string), + d.Get("routing_instance").(string), + d.Get("group").(string), + clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -681,24 +687,27 @@ func resourceBgpNeighborCreate(ctx context.Context, d *schema.ResourceData, m in "=> check your config", d.Get("ip").(string), d.Get("group").(string), d.Get("routing_instance").(string)))...) } - return append(diagWarns, resourceBgpNeighborReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceBgpNeighborReadWJunSess(d, clt, junSess)...) } func resourceBgpNeighborRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceBgpNeighborReadWJnprSess(d, m, jnprSess) + return resourceBgpNeighborReadWJunSess(d, clt, junSess) } -func resourceBgpNeighborReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) diag.Diagnostics { +func resourceBgpNeighborReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession) diag.Diagnostics { mutex.Lock() - bgpNeighborOptions, err := readBgpNeighbor(d.Get("ip").(string), - d.Get("routing_instance").(string), d.Get("group").(string), m, jnprSess) + bgpNeighborOptions, err := readBgpNeighbor( + d.Get("ip").(string), + d.Get("routing_instance").(string), + d.Get("group").(string), + clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -714,76 +723,76 @@ func resourceBgpNeighborReadWJnprSess(d *schema.ResourceData, m interface{}, jnp func resourceBgpNeighborUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delBgpOpts(d, "neighbor", m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delBgpOpts(d, "neighbor", clt, nil); err != nil { return diag.FromErr(err) } - if err := setBgpNeighbor(d, m, nil); err != nil { + if err := setBgpNeighbor(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delBgpOpts(d, "neighbor", m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delBgpOpts(d, "neighbor", clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setBgpNeighbor(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setBgpNeighbor(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_bgp_neighbor", jnprSess) + warns, err := clt.commitConf("update resource junos_bgp_neighbor", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceBgpNeighborReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceBgpNeighborReadWJunSess(d, clt, junSess)...) } func resourceBgpNeighborDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delBgpNeighbor(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delBgpNeighbor(d, clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delBgpNeighbor(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delBgpNeighbor(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_bgp_neighbor", jnprSess) + warns, err := clt.commitConf("delete resource junos_bgp_neighbor", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -793,18 +802,18 @@ func resourceBgpNeighborDelete(ctx context.Context, d *schema.ResourceData, m in func resourceBgpNeighborImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) idSplit := strings.Split(d.Id(), idSeparator) if len(idSplit) < 3 { return nil, fmt.Errorf("missing element(s) in id with separator %v", idSeparator) } - bgpNeighborxists, err := checkBgpNeighborExists(idSplit[0], idSplit[1], idSplit[2], m, jnprSess) + bgpNeighborxists, err := checkBgpNeighborExists(idSplit[0], idSplit[1], idSplit[2], clt, junSess) if err != nil { return nil, err } @@ -812,7 +821,7 @@ func resourceBgpNeighborImport(ctx context.Context, d *schema.ResourceData, m in return nil, fmt.Errorf("don't find bgp neighbor with id '%v' "+ "(id must be "+idSeparator+""+idSeparator+")", d.Id()) } - bgpNeighborOptions, err := readBgpNeighbor(idSplit[0], idSplit[1], idSplit[2], m, jnprSess) + bgpNeighborOptions, err := readBgpNeighbor(idSplit[0], idSplit[1], idSplit[2], clt, junSess) if err != nil { return nil, err } @@ -822,19 +831,18 @@ func resourceBgpNeighborImport(ctx context.Context, d *schema.ResourceData, m in return result, nil } -func checkBgpNeighborExists(ip, instance, group string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) +func checkBgpNeighborExists(ip, instance, group string, clt *Client, junSess *junosSession) (bool, error) { var showConfig string var err error if instance == defaultW { - showConfig, err = sess.command(cmdShowConfig+ - "protocols bgp group "+group+" neighbor "+ip+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "protocols bgp group "+group+" neighbor "+ip+pipeDisplaySet, junSess) if err != nil { return false, err } } else { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+instance+" "+ - "protocols bgp group "+group+" neighbor "+ip+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+instance+" "+ + "protocols bgp group "+group+" neighbor "+ip+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -846,34 +854,33 @@ func checkBgpNeighborExists(ip, instance, group string, m interface{}, jnprSess return true, nil } -func setBgpNeighbor(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { +func setBgpNeighbor(d *schema.ResourceData, clt *Client, junSess *junosSession) error { setPrefix := setLS if d.Get("routing_instance").(string) != defaultW { setPrefix = setRoutingInstances + d.Get("routing_instance").(string) + " " } setPrefix += "protocols bgp group " + d.Get("group").(string) + " neighbor " + d.Get("ip").(string) + " " - if err := setBgpOptsSimple(setPrefix, d, m, jnprSess); err != nil { + if err := setBgpOptsSimple(setPrefix, d, clt, junSess); err != nil { return err } - if err := setBgpOptsBfd(setPrefix, d.Get("bfd_liveness_detection").([]interface{}), m, jnprSess); err != nil { + if err := setBgpOptsBfd(setPrefix, d.Get("bfd_liveness_detection").([]interface{}), clt, junSess); err != nil { return err } - if err := setBgpOptsFamily(setPrefix, "evpn", d.Get("family_evpn").([]interface{}), m, jnprSess); err != nil { + if err := setBgpOptsFamily(setPrefix, "evpn", d.Get("family_evpn").([]interface{}), clt, junSess); err != nil { return err } - if err := setBgpOptsFamily(setPrefix, inetW, d.Get("family_inet").([]interface{}), m, jnprSess); err != nil { + if err := setBgpOptsFamily(setPrefix, inetW, d.Get("family_inet").([]interface{}), clt, junSess); err != nil { return err } - if err := setBgpOptsFamily(setPrefix, inet6W, d.Get("family_inet6").([]interface{}), m, jnprSess); err != nil { + if err := setBgpOptsFamily(setPrefix, inet6W, d.Get("family_inet6").([]interface{}), clt, junSess); err != nil { return err } - return setBgpOptsGrafefulRestart(setPrefix, d.Get("graceful_restart").([]interface{}), m, jnprSess) + return setBgpOptsGrafefulRestart(setPrefix, d.Get("graceful_restart").([]interface{}), clt, junSess) } -func readBgpNeighbor(ip, instance, group string, m interface{}, jnprSess *NetconfObject) (bgpOptions, error) { - sess := m.(*Session) +func readBgpNeighbor(ip, instance, group string, clt *Client, junSess *junosSession) (bgpOptions, error) { var confRead bgpOptions var showConfig string var err error @@ -883,14 +890,14 @@ func readBgpNeighbor(ip, instance, group string, m interface{}, jnprSess *Netcon confRead.preference = -1 if instance == defaultW { - showConfig, err = sess.command(cmdShowConfig+ - "protocols bgp group "+group+" neighbor "+ip+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "protocols bgp group "+group+" neighbor "+ip+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } } else { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+instance+" "+ - "protocols bgp group "+group+" neighbor "+ip+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+instance+" "+ + "protocols bgp group "+group+" neighbor "+ip+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -967,8 +974,7 @@ func readBgpNeighbor(ip, instance, group string, m interface{}, jnprSess *Netcon return confRead, nil } -func delBgpNeighbor(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delBgpNeighbor(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) if d.Get("routing_instance").(string) == defaultW { configSet = append(configSet, "delete protocols bgp"+ @@ -980,7 +986,7 @@ func delBgpNeighbor(d *schema.ResourceData, m interface{}, jnprSess *NetconfObje " neighbor "+d.Get("ip").(string)) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillBgpNeighborData(d *schema.ResourceData, bgpNeighborOptions bgpOptions) { diff --git a/junos/resource_bridge_domain.go b/junos/resource_bridge_domain.go index 43512418..517316d3 100644 --- a/junos/resource_bridge_domain.go +++ b/junos/resource_bridge_domain.go @@ -150,69 +150,73 @@ func resourceBridgeDomain() *schema.Resource { } func resourceBridgeDomainCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setBridgeDomain(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setBridgeDomain(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string) + idSeparator + d.Get("routing_instance").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilityRouter(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilityRouter(junSess) { return diag.FromErr(fmt.Errorf("bridge domain "+ - "not compatible with Junos device %s", jnprSess.SystemInformation.HardwareModel)) + "not compatible with Junos device %s", junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if d.Get("routing_instance").(string) != defaultW { - instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !instanceExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("routing instance %v doesn't exist", d.Get("routing_instance").(string)))...) } } bridgeDomainExists, err := checkBridgeDomainExists( - d.Get("name").(string), d.Get("routing_instance").(string), m, jnprSess) + d.Get("name").(string), + d.Get("routing_instance").(string), + clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if bridgeDomainExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("bridge domain %v already exists in routing_instance %s", d.Get("name").(string), d.Get("routing_instance").(string)))...) } - if err := setBridgeDomain(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setBridgeDomain(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_bridge_domain", jnprSess) + warns, err := clt.commitConf("create resource junos_bridge_domain", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } bridgeDomainExists, err = checkBridgeDomainExists( - d.Get("name").(string), d.Get("routing_instance").(string), m, jnprSess) + d.Get("name").(string), + d.Get("routing_instance").(string), + clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -224,25 +228,27 @@ func resourceBridgeDomainCreate(ctx context.Context, d *schema.ResourceData, m i "=> check your config", d.Get("name").(string), d.Get("routing_instance").(string)))...) } - return append(diagWarns, resourceBridgeDomainReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceBridgeDomainReadWJunSess(d, clt, junSess)...) } func resourceBridgeDomainRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceBridgeDomainReadWJnprSess(d, m, jnprSess) + return resourceBridgeDomainReadWJunSess(d, clt, junSess) } -func resourceBridgeDomainReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceBridgeDomainReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - bridgeDomainOptions, err := readBridgeDomain(d.Get("name").(string), d.Get("routing_instance").(string), - m, jnprSess) + bridgeDomainOptions, err := readBridgeDomain( + d.Get("name").(string), + d.Get("routing_instance").(string), + clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -258,94 +264,118 @@ func resourceBridgeDomainReadWJnprSess(d *schema.ResourceData, m interface{}, jn func resourceBridgeDomainUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { + clt := m.(*Client) + if clt.fakeUpdateAlso { if d.HasChange("vxlan") { oldVxlan, _ := d.GetChange("vxlan") - if err := delBridgeDomainOpts(d.Get("name").(string), d.Get("routing_instance").(string), oldVxlan.([]interface{}), - m, nil); err != nil { + if err := delBridgeDomainOpts( + d.Get("name").(string), + d.Get("routing_instance").(string), + oldVxlan.([]interface{}), + clt, nil, + ); err != nil { return diag.FromErr(err) } } else if err := delBridgeDomainOpts( - d.Get("name").(string), d.Get("routing_instance").(string), d.Get("vxlan").([]interface{}), m, nil); err != nil { + d.Get("name").(string), + d.Get("routing_instance").(string), + d.Get("vxlan").([]interface{}), + clt, nil, + ); err != nil { return diag.FromErr(err) } - if err := setBridgeDomain(d, m, nil); err != nil { + if err := setBridgeDomain(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if d.HasChange("vxlan") { oldVxlan, _ := d.GetChange("vxlan") - if err := delBridgeDomainOpts(d.Get("name").(string), d.Get("routing_instance").(string), oldVxlan.([]interface{}), - m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delBridgeDomainOpts( + d.Get("name").(string), + d.Get("routing_instance").(string), + oldVxlan.([]interface{}), + clt, junSess, + ); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } } else if err := delBridgeDomainOpts( - d.Get("name").(string), d.Get("routing_instance").(string), d.Get("vxlan").([]interface{}), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + d.Get("name").(string), + d.Get("routing_instance").(string), + d.Get("vxlan").([]interface{}), + clt, junSess, + ); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setBridgeDomain(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setBridgeDomain(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_bridge_domain", jnprSess) + warns, err := clt.commitConf("update resource junos_bridge_domain", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceBridgeDomainReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceBridgeDomainReadWJunSess(d, clt, junSess)...) } func resourceBridgeDomainDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delBridgeDomain(d.Get("name").(string), d.Get("routing_instance").(string), d.Get("vxlan").([]interface{}), - m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delBridgeDomain( + d.Get("name").(string), + d.Get("routing_instance").(string), + d.Get("vxlan").([]interface{}), + clt, nil, + ); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delBridgeDomain(d.Get("name").(string), d.Get("routing_instance").(string), d.Get("vxlan").([]interface{}), - m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delBridgeDomain( + d.Get("name").(string), + d.Get("routing_instance").(string), + d.Get("vxlan").([]interface{}), + clt, junSess, + ); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_bridge_domain", jnprSess) + warns, err := clt.commitConf("delete resource junos_bridge_domain", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -355,18 +385,18 @@ func resourceBridgeDomainDelete(ctx context.Context, d *schema.ResourceData, m i func resourceBridgeDomainImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) idSplit := strings.Split(d.Id(), idSeparator) if len(idSplit) < 2 { return nil, fmt.Errorf("missing element(s) in id with separator %v", idSeparator) } - bridgeDomainExists, err := checkBridgeDomainExists(idSplit[0], idSplit[1], m, jnprSess) + bridgeDomainExists, err := checkBridgeDomainExists(idSplit[0], idSplit[1], clt, junSess) if err != nil { return nil, err } @@ -374,7 +404,7 @@ func resourceBridgeDomainImport(ctx context.Context, d *schema.ResourceData, m i return nil, fmt.Errorf("don't find bridge domain with id '%v' (id must be "+ ""+idSeparator+")", d.Id()) } - bridgeDomainOptions, err := readBridgeDomain(idSplit[0], idSplit[1], m, jnprSess) + bridgeDomainOptions, err := readBridgeDomain(idSplit[0], idSplit[1], clt, junSess) if err != nil { return nil, err } @@ -385,19 +415,18 @@ func resourceBridgeDomainImport(ctx context.Context, d *schema.ResourceData, m i return result, nil } -func checkBridgeDomainExists(name, instance string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) +func checkBridgeDomainExists(name, instance string, clt *Client, junSess *junosSession) (bool, error) { var showConfig string var err error if instance == defaultW { - showConfig, err = sess.command(cmdShowConfig+ - "bridge-domains \""+name+"\""+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "bridge-domains \""+name+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } } else { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+instance+" "+ - "bridge-domains \""+name+"\""+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+instance+" "+ + "bridge-domains \""+name+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -410,8 +439,7 @@ func checkBridgeDomainExists(name, instance string, m interface{}, jnprSess *Net return true, nil } -func setBridgeDomain(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setBridgeDomain(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := setLS @@ -480,21 +508,20 @@ func setBridgeDomain(d *schema.ResourceData, m interface{}, jnprSess *NetconfObj } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readBridgeDomain(name, instance string, m interface{}, jnprSess *NetconfObject) (bridgeDomainOptions, error) { - sess := m.(*Session) +func readBridgeDomain(name, instance string, clt *Client, junSess *junosSession) (bridgeDomainOptions, error) { var confRead bridgeDomainOptions var showConfig string var err error if instance == defaultW { - showConfig, err = sess.command(cmdShowConfig+ - "bridge-domains \""+name+"\""+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "bridge-domains \""+name+"\""+pipeDisplaySetRelative, junSess) } else { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+instance+" "+ - "bridge-domains \""+name+"\""+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+instance+" "+ + "bridge-domains \""+name+"\""+pipeDisplaySetRelative, junSess) } if err != nil { return confRead, err @@ -570,13 +597,13 @@ func readBridgeDomain(name, instance string, m interface{}, jnprSess *NetconfObj var showConfigEvpn string var err error if confRead.routingInstance == defaultW { - showConfigEvpn, err = sess.command(cmdShowConfig+"protocols evpn"+pipeDisplaySetRelative, jnprSess) + showConfigEvpn, err = clt.command(cmdShowConfig+"protocols evpn"+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } } else { - showConfigEvpn, err = sess.command(cmdShowConfig+routingInstancesWS+instance+" "+ - "protocols evpn"+pipeDisplaySetRelative, jnprSess) + showConfigEvpn, err = clt.command(cmdShowConfig+routingInstancesWS+instance+" "+ + "protocols evpn"+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -620,8 +647,7 @@ func readBridgeDomain(name, instance string, m interface{}, jnprSess *NetconfObj return confRead, nil } -func delBridgeDomainOpts(name, instance string, vxlan []interface{}, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delBridgeDomainOpts(name, instance string, vxlan []interface{}, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) delPrefix := deleteLS if instance != defaultW { @@ -654,11 +680,10 @@ func delBridgeDomainOpts(name, instance string, vxlan []interface{}, m interface } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func delBridgeDomain(name, instance string, vxlan []interface{}, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delBridgeDomain(name, instance string, vxlan []interface{}, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) if instance == defaultW { configSet = append(configSet, "delete bridge-domains \""+name+"\"") @@ -678,7 +703,7 @@ func delBridgeDomain(name, instance string, vxlan []interface{}, m interface{}, } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillBridgeDomainData(d *schema.ResourceData, bridgeDomainOptions bridgeDomainOptions) { diff --git a/junos/resource_chassis_cluster.go b/junos/resource_chassis_cluster.go index 210ced8a..ecfff2f5 100644 --- a/junos/resource_chassis_cluster.go +++ b/junos/resource_chassis_cluster.go @@ -205,14 +205,14 @@ func resourceChassisCluster() *schema.Resource { } } -func checkCompatibilityChassisCluster(jnprSess *NetconfObject) bool { - if strings.HasPrefix(strings.ToLower(jnprSess.SystemInformation.HardwareModel), "srx") { +func checkCompatibilityChassisCluster(junSess *junosSession) bool { + if strings.HasPrefix(strings.ToLower(junSess.SystemInformation.HardwareModel), "srx") { return true } - if strings.HasPrefix(strings.ToLower(jnprSess.SystemInformation.HardwareModel), "vsrx") { + if strings.HasPrefix(strings.ToLower(junSess.SystemInformation.HardwareModel), "vsrx") { return true } - if strings.HasPrefix(strings.ToLower(jnprSess.SystemInformation.HardwareModel), "j") { + if strings.HasPrefix(strings.ToLower(junSess.SystemInformation.HardwareModel), "j") { return true } @@ -220,60 +220,60 @@ func checkCompatibilityChassisCluster(jnprSess *NetconfObject) bool { } func resourceChassisClusterCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setChassisCluster(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setChassisCluster(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId("cluster") return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilityChassisCluster(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilityChassisCluster(junSess) { return diag.FromErr(fmt.Errorf("chassis cluster "+ - "not compatible with Junos device %s", jnprSess.SystemInformation.HardwareModel)) + "not compatible with Junos device %s", junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := setChassisCluster(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setChassisCluster(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_chassis_cluster", jnprSess) + warns, err := clt.commitConf("create resource junos_chassis_cluster", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.SetId("cluster") - return append(diagWarns, resourceChassisClusterReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceChassisClusterReadWJunSess(d, clt, junSess)...) } func resourceChassisClusterRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceChassisClusterReadWJnprSess(d, m, jnprSess) + return resourceChassisClusterReadWJunSess(d, clt, junSess) } -func resourceChassisClusterReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceChassisClusterReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - clusterOptions, err := readChassisCluster(m, jnprSess) + clusterOptions, err := readChassisCluster(clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -285,76 +285,76 @@ func resourceChassisClusterReadWJnprSess(d *schema.ResourceData, m interface{}, func resourceChassisClusterUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delChassisCluster(m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delChassisCluster(clt, nil); err != nil { return diag.FromErr(err) } - if err := setChassisCluster(d, m, nil); err != nil { + if err := setChassisCluster(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delChassisCluster(m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delChassisCluster(clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setChassisCluster(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setChassisCluster(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_chassis_cluster", jnprSess) + warns, err := clt.commitConf("update resource junos_chassis_cluster", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceChassisClusterReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceChassisClusterReadWJunSess(d, clt, junSess)...) } func resourceChassisClusterDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delChassisCluster(m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delChassisCluster(clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delChassisCluster(m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delChassisCluster(clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_chassis_cluster", jnprSess) + warns, err := clt.commitConf("delete resource junos_chassis_cluster", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -364,14 +364,14 @@ func resourceChassisClusterDelete(ctx context.Context, d *schema.ResourceData, m func resourceChassisClusterImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - clusterOptions, err := readChassisCluster(m, jnprSess) + clusterOptions, err := readChassisCluster(clt, junSess) if err != nil { return nil, err } @@ -382,8 +382,7 @@ func resourceChassisClusterImport(ctx context.Context, d *schema.ResourceData, m return result, nil } -func setChassisCluster(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setChassisCluster(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setIntPrefix := "set interfaces " @@ -481,28 +480,26 @@ func setChassisCluster(d *schema.ResourceData, m interface{}, jnprSess *NetconfO strconv.Itoa(v)) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func delChassisCluster(m interface{}, jnprSess *NetconfObject) error { +func delChassisCluster(clt *Client, junSess *junosSession) error { listLinesToDelete := make([]string, 0) listLinesToDelete = append(listLinesToDelete, "chassis cluster") listLinesToDelete = append(listLinesToDelete, "interfaces fab0") listLinesToDelete = append(listLinesToDelete, "interfaces fab1") - sess := m.(*Session) configSet := make([]string, 0) for _, line := range listLinesToDelete { configSet = append(configSet, deleteLS+line) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readChassisCluster(m interface{}, jnprSess *NetconfObject) (chassisClusterOptions, error) { - sess := m.(*Session) +func readChassisCluster(clt *Client, junSess *junosSession) (chassisClusterOptions, error) { var confRead chassisClusterOptions - showConfig, err := sess.command(cmdShowConfig+"chassis cluster"+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"chassis cluster"+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -647,7 +644,7 @@ func readChassisCluster(m interface{}, jnprSess *NetconfObject) (chassisClusterO } } } - showConfigFab0, err := sess.command(cmdShowConfig+"interfaces fab0"+pipeDisplaySetRelative, jnprSess) + showConfigFab0, err := clt.command(cmdShowConfig+"interfaces fab0"+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -675,7 +672,7 @@ func readChassisCluster(m interface{}, jnprSess *NetconfObject) (chassisClusterO } } } - showConfigFab1, err := sess.command(cmdShowConfig+"interfaces fab1"+pipeDisplaySetRelative, jnprSess) + showConfigFab1, err := clt.command(cmdShowConfig+"interfaces fab1"+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } diff --git a/junos/resource_chassis_redundancy.go b/junos/resource_chassis_redundancy.go index 58896682..bd0803be 100644 --- a/junos/resource_chassis_redundancy.go +++ b/junos/resource_chassis_redundancy.go @@ -88,56 +88,56 @@ func resourceChassisRedundancy() *schema.Resource { } func resourceChassisRedundancyCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setChassisRedundancy(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setChassisRedundancy(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId("redundancy") return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := setChassisRedundancy(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setChassisRedundancy(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_chassis_redundancy", jnprSess) + warns, err := clt.commitConf("create resource junos_chassis_redundancy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.SetId("redundancy") - return append(diagWarns, resourceChassisRedundancyReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceChassisRedundancyReadWJunSess(d, clt, junSess)...) } func resourceChassisRedundancyRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceChassisRedundancyReadWJnprSess(d, m, jnprSess) + return resourceChassisRedundancyReadWJunSess(d, clt, junSess) } -func resourceChassisRedundancyReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceChassisRedundancyReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - redundancyOptions, err := readChassisRedundancy(m, jnprSess) + redundancyOptions, err := readChassisRedundancy(clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -149,76 +149,76 @@ func resourceChassisRedundancyReadWJnprSess(d *schema.ResourceData, m interface{ func resourceChassisRedundancyUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delChassisRedundancy(m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delChassisRedundancy(clt, nil); err != nil { return diag.FromErr(err) } - if err := setChassisRedundancy(d, m, nil); err != nil { + if err := setChassisRedundancy(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delChassisRedundancy(m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delChassisRedundancy(clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setChassisRedundancy(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setChassisRedundancy(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_chassis_redundancy", jnprSess) + warns, err := clt.commitConf("update resource junos_chassis_redundancy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceChassisRedundancyReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceChassisRedundancyReadWJunSess(d, clt, junSess)...) } func resourceChassisRedundancyDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delChassisRedundancy(m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delChassisRedundancy(clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delChassisRedundancy(m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delChassisRedundancy(clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_chassis_redundancy", jnprSess) + warns, err := clt.commitConf("delete resource junos_chassis_redundancy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -228,14 +228,14 @@ func resourceChassisRedundancyDelete(ctx context.Context, d *schema.ResourceData func resourceChassisRedundancyImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - redundancyOptions, err := readChassisRedundancy(m, jnprSess) + redundancyOptions, err := readChassisRedundancy(clt, junSess) if err != nil { return nil, err } @@ -246,8 +246,7 @@ func resourceChassisRedundancyImport(ctx context.Context, d *schema.ResourceData return result, nil } -func setChassisRedundancy(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setChassisRedundancy(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set chassis redundancy " @@ -285,21 +284,19 @@ func setChassisRedundancy(d *schema.ResourceData, m interface{}, jnprSess *Netco " "+routingEngine["role"].(string)) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func delChassisRedundancy(m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delChassisRedundancy(clt *Client, junSess *junosSession) error { configSet := []string{"delete chassis redundancy"} - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readChassisRedundancy(m interface{}, jnprSess *NetconfObject) (chassisRedundancyOptions, error) { - sess := m.(*Session) +func readChassisRedundancy(clt *Client, junSess *junosSession) (chassisRedundancyOptions, error) { var confRead chassisRedundancyOptions - showConfig, err := sess.command(cmdShowConfig+"chassis redundancy"+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"chassis redundancy"+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } diff --git a/junos/resource_eventoptions_destination.go b/junos/resource_eventoptions_destination.go index bd514f7f..036c4220 100644 --- a/junos/resource_eventoptions_destination.go +++ b/junos/resource_eventoptions_destination.go @@ -64,50 +64,50 @@ func resourceEventoptionsDestination() *schema.Resource { func resourceEventoptionsDestinationCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setEventoptionsDestination(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setEventoptionsDestination(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - eventoptionsDestinationExists, err := checkEventoptionsDestinationExists(d.Get("name").(string), m, jnprSess) + eventoptionsDestinationExists, err := checkEventoptionsDestinationExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if eventoptionsDestinationExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("event-options destinations %v already exists", d.Get("name").(string)))...) } - if err := setEventoptionsDestination(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setEventoptionsDestination(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_eventoptions_destination", jnprSess) + warns, err := clt.commitConf("create resource junos_eventoptions_destination", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - eventoptionsDestinationExists, err = checkEventoptionsDestinationExists(d.Get("name").(string), m, jnprSess) + eventoptionsDestinationExists, err = checkEventoptionsDestinationExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -118,24 +118,24 @@ func resourceEventoptionsDestinationCreate(ctx context.Context, d *schema.Resour "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceEventoptionsDestinationReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceEventoptionsDestinationReadWJunSess(d, clt, junSess)...) } func resourceEventoptionsDestinationRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceEventoptionsDestinationReadWJnprSess(d, m, jnprSess) + return resourceEventoptionsDestinationReadWJunSess(d, clt, junSess) } -func resourceEventoptionsDestinationReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceEventoptionsDestinationReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - eventoptionsDestinationOptions, err := readEventoptionsDestination(d.Get("name").(string), m, jnprSess) + eventoptionsDestinationOptions, err := readEventoptionsDestination(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -152,77 +152,77 @@ func resourceEventoptionsDestinationReadWJnprSess(d *schema.ResourceData, m inte func resourceEventoptionsDestinationUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delEventoptionsDestination(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delEventoptionsDestination(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setEventoptionsDestination(d, m, nil); err != nil { + if err := setEventoptionsDestination(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delEventoptionsDestination(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delEventoptionsDestination(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setEventoptionsDestination(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setEventoptionsDestination(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_eventoptions_destination", jnprSess) + warns, err := clt.commitConf("update resource junos_eventoptions_destination", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceEventoptionsDestinationReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceEventoptionsDestinationReadWJunSess(d, clt, junSess)...) } func resourceEventoptionsDestinationDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delEventoptionsDestination(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delEventoptionsDestination(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delEventoptionsDestination(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delEventoptionsDestination(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_eventoptions_destination", jnprSess) + warns, err := clt.commitConf("delete resource junos_eventoptions_destination", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -232,22 +232,22 @@ func resourceEventoptionsDestinationDelete(ctx context.Context, d *schema.Resour func resourceEventoptionsDestinationImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - eventoptionsDestinationExists, err := checkEventoptionsDestinationExists(d.Id(), m, jnprSess) + eventoptionsDestinationExists, err := checkEventoptionsDestinationExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !eventoptionsDestinationExists { return nil, fmt.Errorf("don't find event-options destinations with id '%v' (id must be )", d.Id()) } - eventoptionsDestinationOptions, err := readEventoptionsDestination(d.Id(), m, jnprSess) + eventoptionsDestinationOptions, err := readEventoptionsDestination(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -258,9 +258,8 @@ func resourceEventoptionsDestinationImport(ctx context.Context, d *schema.Resour return result, nil } -func checkEventoptionsDestinationExists(name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"event-options destinations \""+name+"\""+pipeDisplaySet, jnprSess) +func checkEventoptionsDestinationExists(name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"event-options destinations \""+name+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -271,8 +270,7 @@ func checkEventoptionsDestinationExists(name string, m interface{}, jnprSess *Ne return true, nil } -func setEventoptionsDestination(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setEventoptionsDestination(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set event-options destinations \"" + d.Get("name").(string) + "\" " @@ -292,17 +290,16 @@ func setEventoptionsDestination(d *schema.ResourceData, m interface{}, jnprSess configSet = append(configSet, setPrefix+"transfer-delay "+strconv.Itoa(v)) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readEventoptionsDestination(name string, m interface{}, jnprSess *NetconfObject, +func readEventoptionsDestination(name string, clt *Client, junSess *junosSession, ) (eventoptionsDestinationOptions, error) { - sess := m.(*Session) var confRead eventoptionsDestinationOptions confRead.transferDelay = -1 // default value - showConfig, err := sess.command(cmdShowConfig+ - "event-options destinations \""+name+"\""+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "event-options destinations \""+name+"\""+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -347,12 +344,11 @@ func readEventoptionsDestination(name string, m interface{}, jnprSess *NetconfOb return confRead, nil } -func delEventoptionsDestination(destination string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delEventoptionsDestination(destination string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete event-options destinations \""+destination+"\"") - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillEventoptionsDestinationData( diff --git a/junos/resource_eventoptions_generate_event.go b/junos/resource_eventoptions_generate_event.go index 4f16d314..7103e71b 100644 --- a/junos/resource_eventoptions_generate_event.go +++ b/junos/resource_eventoptions_generate_event.go @@ -57,50 +57,50 @@ func resourceEventoptionsGenerateEvent() *schema.Resource { func resourceEventoptionsGenerateEventCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setEventoptionsGenerateEvent(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setEventoptionsGenerateEvent(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - eventoptionsGenerateEventExists, err := checkEventoptionsGenerateEventExists(d.Get("name").(string), m, jnprSess) + eventoptionsGenerateEventExists, err := checkEventoptionsGenerateEventExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if eventoptionsGenerateEventExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("event-options generate-event %v already exists", d.Get("name").(string)))...) } - if err := setEventoptionsGenerateEvent(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setEventoptionsGenerateEvent(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_eventoptions_generate_event", jnprSess) + warns, err := clt.commitConf("create resource junos_eventoptions_generate_event", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - eventoptionsGenerateEventExists, err = checkEventoptionsGenerateEventExists(d.Get("name").(string), m, jnprSess) + eventoptionsGenerateEventExists, err = checkEventoptionsGenerateEventExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -111,25 +111,25 @@ func resourceEventoptionsGenerateEventCreate(ctx context.Context, d *schema.Reso "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceEventoptionsGenerateEventReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceEventoptionsGenerateEventReadWJunSess(d, clt, junSess)...) } func resourceEventoptionsGenerateEventRead(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceEventoptionsGenerateEventReadWJnprSess(d, m, jnprSess) + return resourceEventoptionsGenerateEventReadWJunSess(d, clt, junSess) } -func resourceEventoptionsGenerateEventReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceEventoptionsGenerateEventReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - eventoptionsGenerateEventOptions, err := readEventoptionsGenerateEvent(d.Get("name").(string), m, jnprSess) + eventoptionsGenerateEventOptions, err := readEventoptionsGenerateEvent(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -146,77 +146,77 @@ func resourceEventoptionsGenerateEventReadWJnprSess(d *schema.ResourceData, m in func resourceEventoptionsGenerateEventUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delEventoptionsGenerateEvent(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delEventoptionsGenerateEvent(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setEventoptionsGenerateEvent(d, m, nil); err != nil { + if err := setEventoptionsGenerateEvent(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delEventoptionsGenerateEvent(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delEventoptionsGenerateEvent(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setEventoptionsGenerateEvent(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setEventoptionsGenerateEvent(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_eventoptions_generate_event", jnprSess) + warns, err := clt.commitConf("update resource junos_eventoptions_generate_event", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceEventoptionsGenerateEventReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceEventoptionsGenerateEventReadWJunSess(d, clt, junSess)...) } func resourceEventoptionsGenerateEventDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delEventoptionsGenerateEvent(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delEventoptionsGenerateEvent(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delEventoptionsGenerateEvent(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delEventoptionsGenerateEvent(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_eventoptions_generate_event", jnprSess) + warns, err := clt.commitConf("delete resource junos_eventoptions_generate_event", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -226,22 +226,22 @@ func resourceEventoptionsGenerateEventDelete(ctx context.Context, d *schema.Reso func resourceEventoptionsGenerateEventImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - eventoptionsGenerateEventExists, err := checkEventoptionsGenerateEventExists(d.Id(), m, jnprSess) + eventoptionsGenerateEventExists, err := checkEventoptionsGenerateEventExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !eventoptionsGenerateEventExists { return nil, fmt.Errorf("don't find event-options generate-event with id '%v' (id must be )", d.Id()) } - eventoptionsGenerateEventOptions, err := readEventoptionsGenerateEvent(d.Id(), m, jnprSess) + eventoptionsGenerateEventOptions, err := readEventoptionsGenerateEvent(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -252,9 +252,8 @@ func resourceEventoptionsGenerateEventImport(ctx context.Context, d *schema.Reso return result, nil } -func checkEventoptionsGenerateEventExists(name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"event-options generate-event \""+name+"\""+pipeDisplaySet, jnprSess) +func checkEventoptionsGenerateEventExists(name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"event-options generate-event \""+name+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -265,8 +264,7 @@ func checkEventoptionsGenerateEventExists(name string, m interface{}, jnprSess * return true, nil } -func setEventoptionsGenerateEvent(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setEventoptionsGenerateEvent(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set event-options generate-event \"" + d.Get("name").(string) + "\" " @@ -280,16 +278,15 @@ func setEventoptionsGenerateEvent(d *schema.ResourceData, m interface{}, jnprSes configSet = append(configSet, setPrefix+"no-drift") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readEventoptionsGenerateEvent(name string, m interface{}, jnprSess *NetconfObject, +func readEventoptionsGenerateEvent(name string, clt *Client, junSess *junosSession, ) (eventoptionsGenerateEventOptions, error) { - sess := m.(*Session) var confRead eventoptionsGenerateEventOptions - showConfig, err := sess.command(cmdShowConfig+ - "event-options generate-event \""+name+"\""+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "event-options generate-event \""+name+"\""+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -321,12 +318,11 @@ func readEventoptionsGenerateEvent(name string, m interface{}, jnprSess *Netconf return confRead, nil } -func delEventoptionsGenerateEvent(event string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delEventoptionsGenerateEvent(event string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete event-options generate-event \""+event+"\"") - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillEventoptionsGenerateEventData( diff --git a/junos/resource_eventoptions_policy.go b/junos/resource_eventoptions_policy.go index 02c12623..6abb46f7 100644 --- a/junos/resource_eventoptions_policy.go +++ b/junos/resource_eventoptions_policy.go @@ -467,50 +467,50 @@ func resourceEventoptionsPolicy() *schema.Resource { } func resourceEventoptionsPolicyCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setEventoptionsPolicy(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setEventoptionsPolicy(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - eventoptionsPolicyExists, err := checkEventoptionsPolicyExists(d.Get("name").(string), m, jnprSess) + eventoptionsPolicyExists, err := checkEventoptionsPolicyExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if eventoptionsPolicyExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("event-options policy %v already exists", d.Get("name").(string)))...) } - if err := setEventoptionsPolicy(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setEventoptionsPolicy(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_eventoptions_policy", jnprSess) + warns, err := clt.commitConf("create resource junos_eventoptions_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - eventoptionsPolicyExists, err = checkEventoptionsPolicyExists(d.Get("name").(string), m, jnprSess) + eventoptionsPolicyExists, err = checkEventoptionsPolicyExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -521,24 +521,24 @@ func resourceEventoptionsPolicyCreate(ctx context.Context, d *schema.ResourceDat "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceEventoptionsPolicyReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceEventoptionsPolicyReadWJunSess(d, clt, junSess)...) } func resourceEventoptionsPolicyRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceEventoptionsPolicyReadWJnprSess(d, m, jnprSess) + return resourceEventoptionsPolicyReadWJunSess(d, clt, junSess) } -func resourceEventoptionsPolicyReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceEventoptionsPolicyReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - eventoptionsPolicyOptions, err := readEventoptionsPolicy(d.Get("name").(string), m, jnprSess) + eventoptionsPolicyOptions, err := readEventoptionsPolicy(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -554,76 +554,76 @@ func resourceEventoptionsPolicyReadWJnprSess(d *schema.ResourceData, m interface func resourceEventoptionsPolicyUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delEventoptionsPolicy(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delEventoptionsPolicy(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setEventoptionsPolicy(d, m, nil); err != nil { + if err := setEventoptionsPolicy(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delEventoptionsPolicy(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delEventoptionsPolicy(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setEventoptionsPolicy(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setEventoptionsPolicy(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_eventoptions_policy", jnprSess) + warns, err := clt.commitConf("update resource junos_eventoptions_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceEventoptionsPolicyReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceEventoptionsPolicyReadWJunSess(d, clt, junSess)...) } func resourceEventoptionsPolicyDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delEventoptionsPolicy(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delEventoptionsPolicy(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delEventoptionsPolicy(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delEventoptionsPolicy(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_eventoptions_policy", jnprSess) + warns, err := clt.commitConf("delete resource junos_eventoptions_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -633,22 +633,22 @@ func resourceEventoptionsPolicyDelete(ctx context.Context, d *schema.ResourceDat func resourceEventoptionsPolicyImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - eventoptionsPolicyExists, err := checkEventoptionsPolicyExists(d.Id(), m, jnprSess) + eventoptionsPolicyExists, err := checkEventoptionsPolicyExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !eventoptionsPolicyExists { return nil, fmt.Errorf("don't find event-options policy with id '%v' (id must be )", d.Id()) } - eventoptionsPolicyOptions, err := readEventoptionsPolicy(d.Id(), m, jnprSess) + eventoptionsPolicyOptions, err := readEventoptionsPolicy(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -659,9 +659,8 @@ func resourceEventoptionsPolicyImport(ctx context.Context, d *schema.ResourceDat return result, nil } -func checkEventoptionsPolicyExists(name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"event-options policy \""+name+"\""+pipeDisplaySet, jnprSess) +func checkEventoptionsPolicyExists(name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"event-options policy \""+name+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -672,8 +671,7 @@ func checkEventoptionsPolicyExists(name string, m interface{}, jnprSess *Netconf return true, nil } -func setEventoptionsPolicy(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setEventoptionsPolicy(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set event-options policy \"" + d.Get("name").(string) + "\" " @@ -873,15 +871,14 @@ func setEventoptionsPolicy(d *schema.ResourceData, m interface{}, jnprSess *Netc } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readEventoptionsPolicy(name string, m interface{}, jnprSess *NetconfObject) (eventoptionsPolicyOptions, error) { - sess := m.(*Session) +func readEventoptionsPolicy(name string, clt *Client, junSess *junosSession) (eventoptionsPolicyOptions, error) { var confRead eventoptionsPolicyOptions - showConfig, err := sess.command(cmdShowConfig+ - "event-options policy \""+name+"\""+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "event-options policy \""+name+"\""+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -1182,12 +1179,11 @@ func readEventoptionsPolicyThen(then map[string]interface{}, itemTrim string) er return nil } -func delEventoptionsPolicy(policy string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delEventoptionsPolicy(policy string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete event-options policy \""+policy+"\"") - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillEventoptionsPolicyData(d *schema.ResourceData, eventoptionsPolicyOptions eventoptionsPolicyOptions) { diff --git a/junos/resource_evpn.go b/junos/resource_evpn.go index 2ccca194..8868f719 100644 --- a/junos/resource_evpn.go +++ b/junos/resource_evpn.go @@ -117,70 +117,70 @@ func resourceEvpn() *schema.Resource { } func resourceEvpnCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setEvpn(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setEvpn(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("routing_instance").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if d.Get("routing_instance").(string) != defaultW { - instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !instanceExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("routing instance %v doesn't exist", d.Get("routing_instance").(string)))...) } } - if err := setEvpn(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setEvpn(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_evpn", jnprSess) + warns, err := clt.commitConf("create resource junos_evpn", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.SetId(d.Get("routing_instance").(string)) - return append(diagWarns, resourceEvpnReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceEvpnReadWJunSess(d, clt, junSess)...) } func resourceEvpnRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceEvpnReadWJnprSess(d, m, jnprSess) + return resourceEvpnReadWJunSess(d, clt, junSess) } -func resourceEvpnReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) diag.Diagnostics { +func resourceEvpnReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession) diag.Diagnostics { mutex.Lock() if d.Get("routing_instance").(string) != defaultW { - instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), clt, junSess) if err != nil { mutex.Unlock() @@ -194,7 +194,7 @@ func resourceEvpnReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess * return nil } } - evpnOptions, err := readEvpn(d.Get("routing_instance").(string), m, jnprSess) + evpnOptions, err := readEvpn(d.Get("routing_instance").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -210,76 +210,76 @@ func resourceEvpnReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess * func resourceEvpnUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delEvpn(false, d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delEvpn(false, d, clt, nil); err != nil { return diag.FromErr(err) } - if err := setEvpn(d, m, nil); err != nil { + if err := setEvpn(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delEvpn(false, d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delEvpn(false, d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setEvpn(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setEvpn(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_evpn", jnprSess) + warns, err := clt.commitConf("update resource junos_evpn", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceEvpnReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceEvpnReadWJunSess(d, clt, junSess)...) } func resourceEvpnDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delEvpn(true, d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delEvpn(true, d, clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delEvpn(true, d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delEvpn(true, d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_evpn", jnprSess) + warns, err := clt.commitConf("delete resource junos_evpn", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -289,16 +289,16 @@ func resourceEvpnDelete(ctx context.Context, d *schema.ResourceData, m interface func resourceEvpnImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) idList := strings.Split(d.Id(), idSeparator) if idList[0] != defaultW { - instanceExists, err := checkRoutingInstanceExists(idList[0], m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(idList[0], clt, junSess) if err != nil { return nil, err } @@ -306,7 +306,7 @@ func resourceEvpnImport(ctx context.Context, d *schema.ResourceData, m interface return nil, fmt.Errorf("routing instance %v doesn't exist", idList[0]) } } - evpnOptions, err := readEvpn(idList[0], m, jnprSess) + evpnOptions, err := readEvpn(idList[0], clt, junSess) if err != nil { return nil, err } @@ -325,8 +325,7 @@ func resourceEvpnImport(ctx context.Context, d *schema.ResourceData, m interface return result, nil } -func setEvpn(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setEvpn(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := setLS setPrefixSwitchRIVRF := setLS @@ -385,35 +384,34 @@ func setEvpn(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) err } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readEvpn(routingInstance string, m interface{}, jnprSess *NetconfObject, +func readEvpn(routingInstance string, clt *Client, junSess *junosSession, ) (evpnOptions, error) { - sess := m.(*Session) var confRead evpnOptions var showConfig string var showConfigSwitchRI string if routingInstance == defaultW { var err error - showConfig, err = sess.command(cmdShowConfig+"protocols evpn"+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+"protocols evpn"+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } - showConfigSwitchRI, err = sess.command(cmdShowConfig+"switch-options"+pipeDisplaySetRelative, jnprSess) + showConfigSwitchRI, err = clt.command(cmdShowConfig+"switch-options"+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } } else { var err error - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ - "protocols evpn"+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ + "protocols evpn"+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } - showConfigSwitchRI, err = sess.command(cmdShowConfig+routingInstancesWS+routingInstance+ - pipeDisplaySetRelative, jnprSess) + showConfigSwitchRI, err = clt.command(cmdShowConfig+routingInstancesWS+routingInstance+ + pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -491,8 +489,7 @@ func readEvpn(routingInstance string, m interface{}, jnprSess *NetconfObject, return confRead, nil } -func delEvpn(destroy bool, d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delEvpn(destroy bool, d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) delPrefix := "delete protocols evpn " @@ -531,7 +528,7 @@ func delEvpn(destroy bool, d *schema.ResourceData, m interface{}, jnprSess *Netc configSet = append(configSet, delPrefixSwitchRIVRF+line) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillEvpnData(d *schema.ResourceData, evpnOptions evpnOptions) { diff --git a/junos/resource_firewall_filter.go b/junos/resource_firewall_filter.go index d4744532..7c0e0c29 100644 --- a/junos/resource_firewall_filter.go +++ b/junos/resource_firewall_filter.go @@ -310,49 +310,52 @@ func resourceFirewallFilter() *schema.Resource { } func resourceFirewallFilterCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setFirewallFilter(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setFirewallFilter(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string) + idSeparator + d.Get("family").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - firewallFilterExists, err := checkFirewallFilterExists(d.Get("name").(string), d.Get("family").(string), m, jnprSess) + firewallFilterExists, err := checkFirewallFilterExists( + d.Get("name").(string), + d.Get("family").(string), + clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if firewallFilterExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("firewall filter %v already exists", d.Get("name").(string)))...) } - if err := setFirewallFilter(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setFirewallFilter(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_firewall_filter", jnprSess) + warns, err := clt.commitConf("create resource junos_firewall_filter", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - firewallFilterExists, err = checkFirewallFilterExists(d.Get("name").(string), d.Get("family").(string), m, jnprSess) + firewallFilterExists, err = checkFirewallFilterExists(d.Get("name").(string), d.Get("family").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -363,24 +366,24 @@ func resourceFirewallFilterCreate(ctx context.Context, d *schema.ResourceData, m "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceFirewallFilterReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceFirewallFilterReadWJunSess(d, clt, junSess)...) } func resourceFirewallFilterRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceFirewallFilterReadWJnprSess(d, m, jnprSess) + return resourceFirewallFilterReadWJunSess(d, clt, junSess) } -func resourceFirewallFilterReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceFirewallFilterReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - filterOptions, err := readFirewallFilter(d.Get("name").(string), d.Get("family").(string), m, jnprSess) + filterOptions, err := readFirewallFilter(d.Get("name").(string), d.Get("family").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -396,76 +399,76 @@ func resourceFirewallFilterReadWJnprSess(d *schema.ResourceData, m interface{}, func resourceFirewallFilterUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delFirewallFilter(d.Get("name").(string), d.Get("family").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delFirewallFilter(d.Get("name").(string), d.Get("family").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setFirewallFilter(d, m, nil); err != nil { + if err := setFirewallFilter(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delFirewallFilter(d.Get("name").(string), d.Get("family").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delFirewallFilter(d.Get("name").(string), d.Get("family").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setFirewallFilter(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setFirewallFilter(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_firewall_filter", jnprSess) + warns, err := clt.commitConf("update resource junos_firewall_filter", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceFirewallFilterReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceFirewallFilterReadWJunSess(d, clt, junSess)...) } func resourceFirewallFilterDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delFirewallFilter(d.Get("name").(string), d.Get("family").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delFirewallFilter(d.Get("name").(string), d.Get("family").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delFirewallFilter(d.Get("name").(string), d.Get("family").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delFirewallFilter(d.Get("name").(string), d.Get("family").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_firewall_filter", jnprSess) + warns, err := clt.commitConf("delete resource junos_firewall_filter", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -475,25 +478,25 @@ func resourceFirewallFilterDelete(ctx context.Context, d *schema.ResourceData, m func resourceFirewallFilterImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) idList := strings.Split(d.Id(), idSeparator) if len(idList) < 2 { return nil, fmt.Errorf("missing element(s) in id with separator %v", idSeparator) } - firewallFilterExists, err := checkFirewallFilterExists(idList[0], idList[1], m, jnprSess) + firewallFilterExists, err := checkFirewallFilterExists(idList[0], idList[1], clt, junSess) if err != nil { return nil, err } if !firewallFilterExists { return nil, fmt.Errorf("don't find firewall filter with id '%v' (id must be "+idSeparator+")", d.Id()) } - filterOptions, err := readFirewallFilter(idList[0], idList[1], m, jnprSess) + filterOptions, err := readFirewallFilter(idList[0], idList[1], clt, junSess) if err != nil { return nil, err } @@ -504,10 +507,9 @@ func resourceFirewallFilterImport(ctx context.Context, d *schema.ResourceData, m return result, nil } -func checkFirewallFilterExists(name, family string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "firewall family "+family+" filter "+name+pipeDisplaySet, jnprSess) +func checkFirewallFilterExists(name, family string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "firewall family "+family+" filter "+name+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -518,8 +520,7 @@ func checkFirewallFilterExists(name, family string, m interface{}, jnprSess *Net return true, nil } -func setFirewallFilter(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setFirewallFilter(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) var err error setPrefix := "set firewall family " + d.Get("family").(string) + " filter " + d.Get("name").(string) @@ -550,15 +551,14 @@ func setFirewallFilter(d *schema.ResourceData, m interface{}, jnprSess *NetconfO } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readFirewallFilter(filter, family string, m interface{}, jnprSess *NetconfObject) (filterOptions, error) { - sess := m.(*Session) +func readFirewallFilter(filter, family string, clt *Client, junSess *junosSession) (filterOptions, error) { var confRead filterOptions - showConfig, err := sess.command(cmdShowConfig+ - "firewall family "+family+" filter "+filter+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "firewall family "+family+" filter "+filter+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -614,12 +614,11 @@ func readFirewallFilter(filter, family string, m interface{}, jnprSess *NetconfO return confRead, nil } -func delFirewallFilter(filter, family string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delFirewallFilter(filter, family string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete firewall family "+family+" filter "+filter) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillFirewallFilterData(d *schema.ResourceData, filterOptions filterOptions) { diff --git a/junos/resource_firewall_policer.go b/junos/resource_firewall_policer.go index 60791278..710eb617 100644 --- a/junos/resource_firewall_policer.go +++ b/junos/resource_firewall_policer.go @@ -96,49 +96,49 @@ func resourceFirewallPolicer() *schema.Resource { } func resourceFirewallPolicerCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setFirewallPolicer(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setFirewallPolicer(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - firewallPolicerExists, err := checkFirewallPolicerExists(d.Get("name").(string), m, jnprSess) + firewallPolicerExists, err := checkFirewallPolicerExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if firewallPolicerExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("firewall policer %v already exists", d.Get("name").(string)))...) } - if err := setFirewallPolicer(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setFirewallPolicer(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_firewall_policer", jnprSess) + warns, err := clt.commitConf("create resource junos_firewall_policer", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - firewallPolicerExists, err = checkFirewallPolicerExists(d.Get("name").(string), m, jnprSess) + firewallPolicerExists, err = checkFirewallPolicerExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -149,24 +149,24 @@ func resourceFirewallPolicerCreate(ctx context.Context, d *schema.ResourceData, "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceFirewallPolicerReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceFirewallPolicerReadWJunSess(d, clt, junSess)...) } func resourceFirewallPolicerRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceFirewallPolicerReadWJnprSess(d, m, jnprSess) + return resourceFirewallPolicerReadWJunSess(d, clt, junSess) } -func resourceFirewallPolicerReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceFirewallPolicerReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - policerOptions, err := readFirewallPolicer(d.Get("name").(string), m, jnprSess) + policerOptions, err := readFirewallPolicer(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -182,76 +182,76 @@ func resourceFirewallPolicerReadWJnprSess(d *schema.ResourceData, m interface{}, func resourceFirewallPolicerUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delFirewallPolicer(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delFirewallPolicer(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setFirewallPolicer(d, m, nil); err != nil { + if err := setFirewallPolicer(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delFirewallPolicer(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delFirewallPolicer(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setFirewallPolicer(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setFirewallPolicer(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_firewall_policer", jnprSess) + warns, err := clt.commitConf("update resource junos_firewall_policer", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceFirewallPolicerReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceFirewallPolicerReadWJunSess(d, clt, junSess)...) } func resourceFirewallPolicerDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delFirewallPolicer(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delFirewallPolicer(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delFirewallPolicer(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delFirewallPolicer(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_firewall_policer", jnprSess) + warns, err := clt.commitConf("delete resource junos_firewall_policer", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -261,22 +261,22 @@ func resourceFirewallPolicerDelete(ctx context.Context, d *schema.ResourceData, func resourceFirewallPolicerImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - firewallPolicerExists, err := checkFirewallPolicerExists(d.Id(), m, jnprSess) + firewallPolicerExists, err := checkFirewallPolicerExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !firewallPolicerExists { return nil, fmt.Errorf("don't find firewall policer with id '%v' (id must be )", d.Id()) } - policerOptions, err := readFirewallPolicer(d.Id(), m, jnprSess) + policerOptions, err := readFirewallPolicer(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -287,9 +287,8 @@ func resourceFirewallPolicerImport(ctx context.Context, d *schema.ResourceData, return result, nil } -func checkFirewallPolicerExists(name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"firewall policer "+name+pipeDisplaySet, jnprSess) +func checkFirewallPolicerExists(name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"firewall policer "+name+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -300,8 +299,7 @@ func checkFirewallPolicerExists(name string, m interface{}, jnprSess *NetconfObj return true, nil } -func setFirewallPolicer(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setFirewallPolicer(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set firewall policer " + d.Get("name").(string) @@ -343,14 +341,13 @@ func setFirewallPolicer(d *schema.ResourceData, m interface{}, jnprSess *Netconf } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readFirewallPolicer(name string, m interface{}, jnprSess *NetconfObject) (policerOptions, error) { - sess := m.(*Session) +func readFirewallPolicer(name string, clt *Client, junSess *junosSession) (policerOptions, error) { var confRead policerOptions - showConfig, err := sess.command(cmdShowConfig+"firewall policer "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"firewall policer "+name+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -413,12 +410,11 @@ func readFirewallPolicer(name string, m interface{}, jnprSess *NetconfObject) (p return confRead, nil } -func delFirewallPolicer(policer string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delFirewallPolicer(policer string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete firewall policer "+policer) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillFirewallPolicerData(d *schema.ResourceData, policerOptions policerOptions) { diff --git a/junos/resource_forwardingoptions_sampling_instance.go b/junos/resource_forwardingoptions_sampling_instance.go index 8402ff2e..0f643c39 100644 --- a/junos/resource_forwardingoptions_sampling_instance.go +++ b/junos/resource_forwardingoptions_sampling_instance.go @@ -629,51 +629,55 @@ func resourceForwardingoptionsSamplingInstance() *schema.Resource { func resourceForwardingoptionsSamplingInstanceCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setForwardingoptionsSamplingInstance(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setForwardingoptionsSamplingInstance(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - fwdoptsSamplingInstanceExists, err := checkForwardingoptionsSamplingInstanceExists(d.Get("name").(string), m, jnprSess) + fwdoptsSamplingInstanceExists, err := checkForwardingoptionsSamplingInstanceExists( + d.Get("name").(string), + clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if fwdoptsSamplingInstanceExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("forwarding-options sampling instance %v already exists", d.Get("name").(string)))...) } - if err := setForwardingoptionsSamplingInstance(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setForwardingoptionsSamplingInstance(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_forwardingoptions_sampling_instance", jnprSess) + warns, err := clt.commitConf("create resource junos_forwardingoptions_sampling_instance", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - fwdoptsSamplingInstanceExists, err = checkForwardingoptionsSamplingInstanceExists(d.Get("name").(string), m, jnprSess) + fwdoptsSamplingInstanceExists, err = checkForwardingoptionsSamplingInstanceExists( + d.Get("name").(string), + clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -684,26 +688,26 @@ func resourceForwardingoptionsSamplingInstanceCreate(ctx context.Context, d *sch "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceForwardingoptionsSamplingInstanceReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceForwardingoptionsSamplingInstanceReadWJunSess(d, clt, junSess)...) } func resourceForwardingoptionsSamplingInstanceRead(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceForwardingoptionsSamplingInstanceReadWJnprSess(d, m, jnprSess) + return resourceForwardingoptionsSamplingInstanceReadWJunSess(d, clt, junSess) } -func resourceForwardingoptionsSamplingInstanceReadWJnprSess( - d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceForwardingoptionsSamplingInstanceReadWJunSess( + d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - samplingInstanceOptions, err := readForwardingoptionsSamplingInstance(d.Get("name").(string), m, jnprSess) + samplingInstanceOptions, err := readForwardingoptionsSamplingInstance(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -720,78 +724,78 @@ func resourceForwardingoptionsSamplingInstanceReadWJnprSess( func resourceForwardingoptionsSamplingInstanceUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delForwardingoptionsSamplingInstance(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delForwardingoptionsSamplingInstance(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setForwardingoptionsSamplingInstance(d, m, nil); err != nil { + if err := setForwardingoptionsSamplingInstance(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delForwardingoptionsSamplingInstance(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delForwardingoptionsSamplingInstance(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setForwardingoptionsSamplingInstance(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setForwardingoptionsSamplingInstance(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_forwardingoptions_sampling_instance", jnprSess) + warns, err := clt.commitConf("update resource junos_forwardingoptions_sampling_instance", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceForwardingoptionsSamplingInstanceReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceForwardingoptionsSamplingInstanceReadWJunSess(d, clt, junSess)...) } func resourceForwardingoptionsSamplingInstanceDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delForwardingoptionsSamplingInstance(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delForwardingoptionsSamplingInstance(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delForwardingoptionsSamplingInstance(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delForwardingoptionsSamplingInstance(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_forwardingoptions_sampling_instance", jnprSess) + warns, err := clt.commitConf("delete resource junos_forwardingoptions_sampling_instance", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -801,22 +805,22 @@ func resourceForwardingoptionsSamplingInstanceDelete(ctx context.Context, d *sch func resourceForwardingoptionsSamplingInstanceImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - fwdoptsSamplingInstanceExists, err := checkForwardingoptionsSamplingInstanceExists(d.Id(), m, jnprSess) + fwdoptsSamplingInstanceExists, err := checkForwardingoptionsSamplingInstanceExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !fwdoptsSamplingInstanceExists { return nil, fmt.Errorf("don't find forwarding-options sampling instance with id '%v' (id must be )", d.Id()) } - samplingInstanceOptions, err := readForwardingoptionsSamplingInstance(d.Id(), m, jnprSess) + samplingInstanceOptions, err := readForwardingoptionsSamplingInstance(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -827,11 +831,10 @@ func resourceForwardingoptionsSamplingInstanceImport(ctx context.Context, d *sch return result, nil } -func checkForwardingoptionsSamplingInstanceExists(name string, m interface{}, jnprSess *NetconfObject, +func checkForwardingoptionsSamplingInstanceExists(name string, clt *Client, junSess *junosSession, ) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "forwarding-options sampling instance \""+name+"\""+pipeDisplaySet, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "forwarding-options sampling instance \""+name+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -842,8 +845,7 @@ func checkForwardingoptionsSamplingInstanceExists(name string, m interface{}, jn return true, nil } -func setForwardingoptionsSamplingInstance(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setForwardingoptionsSamplingInstance(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set forwarding-options sampling instance \"" + d.Get("name").(string) + "\" " @@ -852,7 +854,7 @@ func setForwardingoptionsSamplingInstance(d *schema.ResourceData, m interface{}, } for _, v := range d.Get("family_inet_input").([]interface{}) { if err := setForwardingoptionsSamplingInstanceInput(setPrefix, - v.(map[string]interface{}), inetW, sess, jnprSess); err != nil { + v.(map[string]interface{}), inetW, clt, junSess); err != nil { return err } } @@ -861,13 +863,13 @@ func setForwardingoptionsSamplingInstance(d *schema.ResourceData, m interface{}, return fmt.Errorf("family_inet_output block is empty") } if err := setForwardingoptionsSamplingInstanceOutput(setPrefix, - v.(map[string]interface{}), inetW, sess, jnprSess); err != nil { + v.(map[string]interface{}), inetW, clt, junSess); err != nil { return err } } for _, v := range d.Get("family_inet6_input").([]interface{}) { if err := setForwardingoptionsSamplingInstanceInput(setPrefix, - v.(map[string]interface{}), inet6W, sess, jnprSess); err != nil { + v.(map[string]interface{}), inet6W, clt, junSess); err != nil { return err } } @@ -876,13 +878,13 @@ func setForwardingoptionsSamplingInstance(d *schema.ResourceData, m interface{}, return fmt.Errorf("family_inet6_output block is empty") } if err := setForwardingoptionsSamplingInstanceOutput(setPrefix, - v.(map[string]interface{}), inet6W, sess, jnprSess); err != nil { + v.(map[string]interface{}), inet6W, clt, junSess); err != nil { return err } } for _, v := range d.Get("family_mpls_input").([]interface{}) { if err := setForwardingoptionsSamplingInstanceInput(setPrefix, - v.(map[string]interface{}), mplsW, sess, jnprSess); err != nil { + v.(map[string]interface{}), mplsW, clt, junSess); err != nil { return err } } @@ -891,22 +893,22 @@ func setForwardingoptionsSamplingInstance(d *schema.ResourceData, m interface{}, return fmt.Errorf("family_mpls_output block is empty") } if err := setForwardingoptionsSamplingInstanceOutput(setPrefix, - v.(map[string]interface{}), mplsW, sess, jnprSess); err != nil { + v.(map[string]interface{}), mplsW, clt, junSess); err != nil { return err } } for _, v := range d.Get("input").([]interface{}) { if err := setForwardingoptionsSamplingInstanceInput(setPrefix, - v.(map[string]interface{}), "", sess, jnprSess); err != nil { + v.(map[string]interface{}), "", clt, junSess); err != nil { return err } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func setForwardingoptionsSamplingInstanceInput( - setPrefix string, input map[string]interface{}, family string, sess *Session, jnprSess *NetconfObject, + setPrefix string, input map[string]interface{}, family string, clt *Client, junSess *junosSession, ) error { configSet := make([]string, 0) switch family { @@ -944,11 +946,11 @@ func setForwardingoptionsSamplingInstanceInput( } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func setForwardingoptionsSamplingInstanceOutput( - setPrefix string, output map[string]interface{}, family string, sess *Session, jnprSess *NetconfObject, + setPrefix string, output map[string]interface{}, family string, clt *Client, junSess *junosSession, ) error { configSet := make([]string, 0) switch family { @@ -1064,16 +1066,15 @@ func setForwardingoptionsSamplingInstanceOutput( } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readForwardingoptionsSamplingInstance(name string, m interface{}, jnprSess *NetconfObject, +func readForwardingoptionsSamplingInstance(name string, clt *Client, junSess *junosSession, ) (samplingInstanceOptions, error) { - sess := m.(*Session) var confRead samplingInstanceOptions - showConfig, err := sess.command(cmdShowConfig+ - "forwarding-options sampling instance \""+name+"\""+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "forwarding-options sampling instance \""+name+"\""+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -1374,11 +1375,10 @@ func readForwardingoptionsSamplingInstanceOutput(outputRead map[string]interface return nil } -func delForwardingoptionsSamplingInstance(samplingInstance string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delForwardingoptionsSamplingInstance(samplingInstance string, clt *Client, junSess *junosSession) error { configSet := []string{"delete forwarding-options sampling instance \"" + samplingInstance + "\""} - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillForwardingoptionsSamplingInstanceData( diff --git a/junos/resource_generate_route.go b/junos/resource_generate_route.go index b2227f46..e2152254 100644 --- a/junos/resource_generate_route.go +++ b/junos/resource_generate_route.go @@ -134,65 +134,69 @@ func resourceGenerateRoute() *schema.Resource { } func resourceGenerateRouteCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setGenerateRoute(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setGenerateRoute(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("destination").(string) + idSeparator + d.Get("routing_instance").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if d.Get("routing_instance").(string) != defaultW { - instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !instanceExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("routing instance %v doesn't exist", d.Get("routing_instance").(string)))...) } } - generateRouteExists, err := checkGenerateRouteExists(d.Get("destination").(string), d.Get("routing_instance").(string), - m, jnprSess) + generateRouteExists, err := checkGenerateRouteExists( + d.Get("destination").(string), + d.Get("routing_instance").(string), + clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if generateRouteExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("generate route %v already exists on table %s", d.Get("destination").(string), d.Get("routing_instance").(string)))...) } - if err := setGenerateRoute(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setGenerateRoute(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_generate_route", jnprSess) + warns, err := clt.commitConf("create resource junos_generate_route", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - generateRouteExists, err = checkGenerateRouteExists(d.Get("destination").(string), d.Get("routing_instance").(string), - m, jnprSess) + generateRouteExists, err = checkGenerateRouteExists( + d.Get("destination").(string), + d.Get("routing_instance").(string), + clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -203,25 +207,27 @@ func resourceGenerateRouteCreate(ctx context.Context, d *schema.ResourceData, m "=> check your config", d.Get("destination").(string), d.Get("routing_instance").(string)))...) } - return append(diagWarns, resourceGenerateRouteReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceGenerateRouteReadWJunSess(d, clt, junSess)...) } func resourceGenerateRouteRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceGenerateRouteReadWJnprSess(d, m, jnprSess) + return resourceGenerateRouteReadWJunSess(d, clt, junSess) } -func resourceGenerateRouteReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceGenerateRouteReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - generateRouteOptions, err := readGenerateRoute(d.Get("destination").(string), d.Get("routing_instance").(string), - m, jnprSess) + generateRouteOptions, err := readGenerateRoute( + d.Get("destination").(string), + d.Get("routing_instance").(string), + clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -237,81 +243,93 @@ func resourceGenerateRouteReadWJnprSess(d *schema.ResourceData, m interface{}, j func resourceGenerateRouteUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { + clt := m.(*Client) + if clt.fakeUpdateAlso { if err := delGenerateRoute( - d.Get("destination").(string), d.Get("routing_instance").(string), m, nil); err != nil { + d.Get("destination").(string), + d.Get("routing_instance").(string), + clt, nil, + ); err != nil { return diag.FromErr(err) } - if err := setGenerateRoute(d, m, nil); err != nil { + if err := setGenerateRoute(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if err := delGenerateRoute( - d.Get("destination").(string), d.Get("routing_instance").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + d.Get("destination").(string), + d.Get("routing_instance").(string), + clt, junSess, + ); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setGenerateRoute(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setGenerateRoute(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_generate_route", jnprSess) + warns, err := clt.commitConf("update resource junos_generate_route", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceGenerateRouteReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceGenerateRouteReadWJunSess(d, clt, junSess)...) } func resourceGenerateRouteDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { + clt := m.(*Client) + if clt.fakeDeleteAlso { if err := delGenerateRoute( - d.Get("destination").(string), d.Get("routing_instance").(string), m, nil); err != nil { + d.Get("destination").(string), + d.Get("routing_instance").(string), + clt, nil, + ); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if err := delGenerateRoute( - d.Get("destination").(string), d.Get("routing_instance").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + d.Get("destination").(string), + d.Get("routing_instance").(string), + clt, junSess, + ); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_generate_route", jnprSess) + warns, err := clt.commitConf("delete resource junos_generate_route", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -321,18 +339,18 @@ func resourceGenerateRouteDelete(ctx context.Context, d *schema.ResourceData, m func resourceGenerateRouteImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) idSplit := strings.Split(d.Id(), idSeparator) if len(idSplit) < 2 { return nil, fmt.Errorf("missing element(s) in id with separator %v", idSeparator) } - generateRouteExists, err := checkGenerateRouteExists(idSplit[0], idSplit[1], m, jnprSess) + generateRouteExists, err := checkGenerateRouteExists(idSplit[0], idSplit[1], clt, junSess) if err != nil { return nil, err } @@ -340,7 +358,7 @@ func resourceGenerateRouteImport(ctx context.Context, d *schema.ResourceData, m return nil, fmt.Errorf("don't find generate route with id '%v' (id must be "+ ""+idSeparator+")", d.Id()) } - generateRouteOptions, err := readGenerateRoute(idSplit[0], idSplit[1], m, jnprSess) + generateRouteOptions, err := readGenerateRoute(idSplit[0], idSplit[1], clt, junSess) if err != nil { return nil, err } @@ -351,34 +369,33 @@ func resourceGenerateRouteImport(ctx context.Context, d *schema.ResourceData, m return result, nil } -func checkGenerateRouteExists(destination, instance string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) +func checkGenerateRouteExists(destination, instance string, clt *Client, junSess *junosSession) (bool, error) { var showConfig string var err error if instance == defaultW { if !strings.Contains(destination, ":") { - showConfig, err = sess.command(cmdShowConfig+ - "routing-options generate route "+destination+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "routing-options generate route "+destination+pipeDisplaySet, junSess) if err != nil { return false, err } } else { - showConfig, err = sess.command(cmdShowConfig+ - "routing-options rib inet6.0 "+"generate route "+destination+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "routing-options rib inet6.0 "+"generate route "+destination+pipeDisplaySet, junSess) if err != nil { return false, err } } } else { if !strings.Contains(destination, ":") { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+instance+" "+ - "routing-options generate route "+destination+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+instance+" "+ + "routing-options generate route "+destination+pipeDisplaySet, junSess) if err != nil { return false, err } } else { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+instance+" "+ - "routing-options rib "+instance+".inet6.0 generate route "+destination+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+instance+" "+ + "routing-options rib "+instance+".inet6.0 generate route "+destination+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -392,8 +409,7 @@ func checkGenerateRouteExists(destination, instance string, m interface{}, jnprS return true, nil } -func setGenerateRoute(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setGenerateRoute(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) var setPrefix string @@ -459,31 +475,30 @@ func setGenerateRoute(d *schema.ResourceData, m interface{}, jnprSess *NetconfOb configSet = append(configSet, setPrefix+"preference "+strconv.Itoa(d.Get("preference").(int))) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readGenerateRoute(destination, instance string, m interface{}, jnprSess *NetconfObject, +func readGenerateRoute(destination, instance string, clt *Client, junSess *junosSession, ) (generateRouteOptions, error) { - sess := m.(*Session) var confRead generateRouteOptions var showConfig string var err error if instance == defaultW { if !strings.Contains(destination, ":") { - showConfig, err = sess.command(cmdShowConfig+ - "routing-options generate route "+destination+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "routing-options generate route "+destination+pipeDisplaySetRelative, junSess) } else { - showConfig, err = sess.command(cmdShowConfig+ - "routing-options rib inet6.0 generate route "+destination+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "routing-options rib inet6.0 generate route "+destination+pipeDisplaySetRelative, junSess) } } else { if !strings.Contains(destination, ":") { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+instance+" "+ - "routing-options generate route "+destination+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+instance+" "+ + "routing-options generate route "+destination+pipeDisplaySetRelative, junSess) } else { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+instance+" "+ - "routing-options rib "+instance+".inet6.0 generate route "+destination+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+instance+" "+ + "routing-options rib "+instance+".inet6.0 generate route "+destination+pipeDisplaySetRelative, junSess) } } if err != nil { @@ -545,8 +560,7 @@ func readGenerateRoute(destination, instance string, m interface{}, jnprSess *Ne return confRead, nil } -func delGenerateRoute(destination, instance string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delGenerateRoute(destination, instance string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) if instance == defaultW { if !strings.Contains(destination, ":") { @@ -564,7 +578,7 @@ func delGenerateRoute(destination, instance string, m interface{}, jnprSess *Net } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillGenerateRouteData(d *schema.ResourceData, generateRouteOptions generateRouteOptions) { diff --git a/junos/resource_group_dual_system.go b/junos/resource_group_dual_system.go index 5db178c0..43addbe0 100644 --- a/junos/resource_group_dual_system.go +++ b/junos/resource_group_dual_system.go @@ -182,49 +182,49 @@ func resourceGroupDualSystem() *schema.Resource { } func resourceGroupDualSystemCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setGroupDualSystem(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setGroupDualSystem(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - groupDualSystemExists, err := checkGroupDualSystemExists(d.Get("name").(string), m, jnprSess) + groupDualSystemExists, err := checkGroupDualSystemExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if groupDualSystemExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("group %v already exists", d.Get("name").(string)))...) } - if err := setGroupDualSystem(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setGroupDualSystem(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_group_dual_system", jnprSess) + warns, err := clt.commitConf("create resource junos_group_dual_system", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - groupDualSystemExists, err = checkGroupDualSystemExists(d.Get("name").(string), m, jnprSess) + groupDualSystemExists, err = checkGroupDualSystemExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -235,24 +235,24 @@ func resourceGroupDualSystemCreate(ctx context.Context, d *schema.ResourceData, "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceGroupDualSystemReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceGroupDualSystemReadWJunSess(d, clt, junSess)...) } func resourceGroupDualSystemRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceGroupDualSystemReadWJnprSess(d, m, jnprSess) + return resourceGroupDualSystemReadWJunSess(d, clt, junSess) } -func resourceGroupDualSystemReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceGroupDualSystemReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - groupDualSystemOpts, err := readGroupDualSystem(d.Get("name").(string), m, jnprSess) + groupDualSystemOpts, err := readGroupDualSystem(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -268,112 +268,112 @@ func resourceGroupDualSystemReadWJnprSess(d *schema.ResourceData, m interface{}, func resourceGroupDualSystemUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delGroupDualSystem(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delGroupDualSystem(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } if strings.HasPrefix(d.Get("name").(string), "node") { - if err := sess.configSet([]string{"delete apply-groups \"${node}\""}, nil); err != nil { + if err := clt.configSet([]string{"delete apply-groups \"${node}\""}, nil); err != nil { return diag.FromErr(err) } - } else if err := sess.configSet([]string{"delete apply-groups " + d.Get("name").(string)}, nil); err != nil { + } else if err := clt.configSet([]string{"delete apply-groups " + d.Get("name").(string)}, nil); err != nil { return diag.FromErr(err) } - if err := setGroupDualSystem(d, m, nil); err != nil { + if err := setGroupDualSystem(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delGroupDualSystem(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delGroupDualSystem(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if strings.HasPrefix(d.Get("name").(string), "node") { - if err := sess.configSet([]string{"delete apply-groups \"${node}\""}, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := clt.configSet([]string{"delete apply-groups \"${node}\""}, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - } else if err := sess.configSet([]string{"delete apply-groups " + d.Get("name").(string)}, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + } else if err := clt.configSet([]string{"delete apply-groups " + d.Get("name").(string)}, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setGroupDualSystem(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setGroupDualSystem(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_group_dual_system", jnprSess) + warns, err := clt.commitConf("update resource junos_group_dual_system", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceGroupDualSystemReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceGroupDualSystemReadWJunSess(d, clt, junSess)...) } func resourceGroupDualSystemDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delGroupDualSystem(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delGroupDualSystem(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } if strings.HasPrefix(d.Get("name").(string), "node") { - if err := sess.configSet([]string{"delete apply-groups \"${node}\""}, nil); err != nil { + if err := clt.configSet([]string{"delete apply-groups \"${node}\""}, nil); err != nil { return diag.FromErr(err) } - } else if err := sess.configSet([]string{"delete apply-groups " + d.Get("name").(string)}, nil); err != nil { + } else if err := clt.configSet([]string{"delete apply-groups " + d.Get("name").(string)}, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delGroupDualSystem(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delGroupDualSystem(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if strings.HasPrefix(d.Get("name").(string), "node") { - if err := sess.configSet([]string{"delete apply-groups \"${node}\""}, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := clt.configSet([]string{"delete apply-groups \"${node}\""}, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - } else if err := sess.configSet([]string{"delete apply-groups " + d.Get("name").(string)}, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + } else if err := clt.configSet([]string{"delete apply-groups " + d.Get("name").(string)}, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_group_dual_system", jnprSess) + warns, err := clt.commitConf("delete resource junos_group_dual_system", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -383,25 +383,25 @@ func resourceGroupDualSystemDelete(ctx context.Context, d *schema.ResourceData, func resourceGroupDualSystemImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) if !bchk.StringInSlice(d.Id(), []string{"node0", "node1", "re0", "re1"}) { return nil, fmt.Errorf("invalid group id '%v' (id must be )", d.Id()) } - groupDualSystemExists, err := checkGroupDualSystemExists(d.Id(), m, jnprSess) + groupDualSystemExists, err := checkGroupDualSystemExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !groupDualSystemExists { return nil, fmt.Errorf("don't find group with id '%v' (id must be )", d.Id()) } - groupDualSystemOptions, err := readGroupDualSystem(d.Id(), m, jnprSess) + groupDualSystemOptions, err := readGroupDualSystem(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -412,9 +412,8 @@ func resourceGroupDualSystemImport(ctx context.Context, d *schema.ResourceData, return result, nil } -func checkGroupDualSystemExists(name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"groups "+name+pipeDisplaySet, jnprSess) +func checkGroupDualSystemExists(name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"groups "+name+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -425,8 +424,7 @@ func checkGroupDualSystemExists(name string, m interface{}, jnprSess *NetconfObj return true, nil } -func setGroupDualSystem(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setGroupDualSystem(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) if d.Get("apply_groups").(bool) { @@ -535,14 +533,13 @@ func setGroupDualSystem(d *schema.ResourceData, m interface{}, jnprSess *Netconf } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readGroupDualSystem(group string, m interface{}, jnprSess *NetconfObject) (groupDualSystemOptions, error) { - sess := m.(*Session) +func readGroupDualSystem(group string, clt *Client, junSess *junosSession) (groupDualSystemOptions, error) { var confRead groupDualSystemOptions - showConfig, err := sess.command(cmdShowConfig+"groups "+group+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"groups "+group+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -668,7 +665,7 @@ func readGroupDualSystem(group string, m interface{}, jnprSess *NetconfObject) ( } } } - showConfigApplyGroups, err := sess.command(cmdShowConfig+"apply-groups"+pipeDisplaySetRelative, jnprSess) + showConfigApplyGroups, err := clt.command(cmdShowConfig+"apply-groups"+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -693,12 +690,11 @@ func readGroupDualSystem(group string, m interface{}, jnprSess *NetconfObject) ( return confRead, nil } -func delGroupDualSystem(group string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delGroupDualSystem(group string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete groups "+group) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillGroupDualSystemData(d *schema.ResourceData, groupDualSystemOptions groupDualSystemOptions) { diff --git a/junos/resource_igmp_snooping_vlan.go b/junos/resource_igmp_snooping_vlan.go index 4e0e9faa..451484db 100644 --- a/junos/resource_igmp_snooping_vlan.go +++ b/junos/resource_igmp_snooping_vlan.go @@ -153,47 +153,49 @@ func resourceIgmpSnoopingVlan() *schema.Resource { } func resourceIgmpSnoopingVlanCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setIgmpSnoopingVlan(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setIgmpSnoopingVlan(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string) + idSeparator + d.Get("routing_instance").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if d.Get("routing_instance").(string) != defaultW { - instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !instanceExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("routing instance %v doesn't exist", d.Get("routing_instance").(string)))...) } } igmpSnoopingVlanExists, err := checkIgmpSnoopingVlanExists( - d.Get("name").(string), d.Get("routing_instance").(string), m, jnprSess) + d.Get("name").(string), + d.Get("routing_instance").(string), + clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if igmpSnoopingVlanExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) if d.Get("routing_instance").(string) == defaultW { return append(diagWarns, diag.FromErr(fmt.Errorf("protocols igmp-snooping vlan %v already exists", d.Get("name").(string)))...) @@ -204,20 +206,22 @@ func resourceIgmpSnoopingVlanCreate(ctx context.Context, d *schema.ResourceData, d.Get("name").(string), d.Get("routing_instance").(string)))...) } - if err := setIgmpSnoopingVlan(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setIgmpSnoopingVlan(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_igmp_snooping_vlan", jnprSess) + warns, err := clt.commitConf("create resource junos_igmp_snooping_vlan", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } igmpSnoopingVlanExists, err = checkIgmpSnoopingVlanExists( - d.Get("name").(string), d.Get("routing_instance").(string), m, jnprSess) + d.Get("name").(string), + d.Get("routing_instance").(string), + clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -234,25 +238,27 @@ func resourceIgmpSnoopingVlanCreate(ctx context.Context, d *schema.ResourceData, "=> check your config", d.Get("name").(string), d.Get("routing_instance").(string)))...) } - return append(diagWarns, resourceIgmpSnoopingVlanReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceIgmpSnoopingVlanReadWJunSess(d, clt, junSess)...) } func resourceIgmpSnoopingVlanRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceIgmpSnoopingVlanReadWJnprSess(d, m, jnprSess) + return resourceIgmpSnoopingVlanReadWJunSess(d, clt, junSess) } -func resourceIgmpSnoopingVlanReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceIgmpSnoopingVlanReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() igmpSnoopingVlanOptions, err := readIgmpSnoopingVlan( - d.Get("name").(string), d.Get("routing_instance").(string), m, jnprSess) + d.Get("name").(string), + d.Get("routing_instance").(string), + clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -268,76 +274,76 @@ func resourceIgmpSnoopingVlanReadWJnprSess(d *schema.ResourceData, m interface{} func resourceIgmpSnoopingVlanUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delIgmpSnoopingVlan(d.Get("name").(string), d.Get("routing_instance").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delIgmpSnoopingVlan(d.Get("name").(string), d.Get("routing_instance").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setIgmpSnoopingVlan(d, m, nil); err != nil { + if err := setIgmpSnoopingVlan(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delIgmpSnoopingVlan(d.Get("name").(string), d.Get("routing_instance").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delIgmpSnoopingVlan(d.Get("name").(string), d.Get("routing_instance").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setIgmpSnoopingVlan(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setIgmpSnoopingVlan(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_igmp_snooping_vlan", jnprSess) + warns, err := clt.commitConf("update resource junos_igmp_snooping_vlan", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceIgmpSnoopingVlanReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceIgmpSnoopingVlanReadWJunSess(d, clt, junSess)...) } func resourceIgmpSnoopingVlanDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delIgmpSnoopingVlan(d.Get("name").(string), d.Get("routing_instance").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delIgmpSnoopingVlan(d.Get("name").(string), d.Get("routing_instance").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delIgmpSnoopingVlan(d.Get("name").(string), d.Get("routing_instance").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delIgmpSnoopingVlan(d.Get("name").(string), d.Get("routing_instance").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_igmp_snooping_vlan", jnprSess) + warns, err := clt.commitConf("delete resource junos_igmp_snooping_vlan", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -347,18 +353,18 @@ func resourceIgmpSnoopingVlanDelete(ctx context.Context, d *schema.ResourceData, func resourceIgmpSnoopingVlanImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) idSplit := strings.Split(d.Id(), idSeparator) if len(idSplit) < 2 { return nil, fmt.Errorf("missing element(s) in id with separator %v", idSeparator) } - igmpSnoopingVlanExists, err := checkIgmpSnoopingVlanExists(idSplit[0], idSplit[1], m, jnprSess) + igmpSnoopingVlanExists, err := checkIgmpSnoopingVlanExists(idSplit[0], idSplit[1], clt, junSess) if err != nil { return nil, err } @@ -366,7 +372,7 @@ func resourceIgmpSnoopingVlanImport(ctx context.Context, d *schema.ResourceData, return nil, fmt.Errorf("don't find protocols igmp-snooping vlan with id '%v' "+ "(id must be %s)", d.Id(), idSeparator) } - igmpSnoopingVlanOptions, err := readIgmpSnoopingVlan(idSplit[0], idSplit[1], m, jnprSess) + igmpSnoopingVlanOptions, err := readIgmpSnoopingVlan(idSplit[0], idSplit[1], clt, junSess) if err != nil { return nil, err } @@ -377,16 +383,15 @@ func resourceIgmpSnoopingVlanImport(ctx context.Context, d *schema.ResourceData, return result, nil } -func checkIgmpSnoopingVlanExists(name, routingInstance string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) +func checkIgmpSnoopingVlanExists(name, routingInstance string, clt *Client, junSess *junosSession) (bool, error) { var showConfig string var err error if routingInstance == defaultW { - showConfig, err = sess.command(cmdShowConfig+ - "protocols igmp-snooping vlan "+name+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "protocols igmp-snooping vlan "+name+pipeDisplaySet, junSess) } else { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ - "protocols igmp-snooping vlan "+name+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ + "protocols igmp-snooping vlan "+name+pipeDisplaySet, junSess) } if err != nil { return false, err @@ -398,8 +403,7 @@ func checkIgmpSnoopingVlanExists(name, routingInstance string, m interface{}, jn return true, nil } -func setIgmpSnoopingVlan(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setIgmpSnoopingVlan(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := setLS @@ -470,21 +474,20 @@ func setIgmpSnoopingVlan(d *schema.ResourceData, m interface{}, jnprSess *Netcon configSet = append(configSet, setPrefix+"robust-count "+strconv.Itoa(v)) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readIgmpSnoopingVlan(name, routingInstance string, m interface{}, jnprSess *NetconfObject, +func readIgmpSnoopingVlan(name, routingInstance string, clt *Client, junSess *junosSession, ) (igmpSnoopingVlanOptions, error) { - sess := m.(*Session) var confRead igmpSnoopingVlanOptions var showConfig string var err error if routingInstance == defaultW { - showConfig, err = sess.command(cmdShowConfig+ - "protocols igmp-snooping vlan "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "protocols igmp-snooping vlan "+name+pipeDisplaySetRelative, junSess) } else { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ - "protocols igmp-snooping vlan "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ + "protocols igmp-snooping vlan "+name+pipeDisplaySetRelative, junSess) } if err != nil { return confRead, err @@ -572,8 +575,7 @@ func readIgmpSnoopingVlan(name, routingInstance string, m interface{}, jnprSess return confRead, nil } -func delIgmpSnoopingVlan(name, routingInstance string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delIgmpSnoopingVlan(name, routingInstance string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) if routingInstance == defaultW { @@ -582,7 +584,7 @@ func delIgmpSnoopingVlan(name, routingInstance string, m interface{}, jnprSess * configSet = append(configSet, delRoutingInstances+routingInstance+" protocols igmp-snooping vlan "+name) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillIgmpSnoopingVlanData(d *schema.ResourceData, igmpSnoopingVlanOptions igmpSnoopingVlanOptions) { diff --git a/junos/resource_interface.go b/junos/resource_interface.go index fecce5fb..9d6a3ba5 100644 --- a/junos/resource_interface.go +++ b/junos/resource_interface.go @@ -431,126 +431,126 @@ func resourceInterface() *schema.Resource { } func resourceInterfaceCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if sess.junosGroupIntDel != "" { - if err := delInterfaceElement("apply-groups "+sess.junosGroupIntDel, d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if clt.groupIntDel != "" { + if err := delInterfaceElement("apply-groups "+clt.groupIntDel, d, clt, nil); err != nil { return diag.FromErr(err) } } else { - if err := delInterfaceElement("disable", d, m, nil); err != nil { + if err := delInterfaceElement("disable", d, clt, nil); err != nil { return diag.FromErr(err) } - if err := delInterfaceElement("description", d, m, nil); err != nil { + if err := delInterfaceElement("description", d, clt, nil); err != nil { return diag.FromErr(err) } } - if err := setInterface(d, m, nil); err != nil { + if err := setInterface(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - intExists, err := checkInterfaceExistsOld(d.Get("name").(string), m, jnprSess) + defer clt.closeSession(junSess) + intExists, err := checkInterfaceExistsOld(d.Get("name").(string), clt, junSess) if err != nil { return diag.FromErr(err) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if intExists { - ncInt, emptyInt, err := checkInterfaceNC(d.Get("name").(string), m, jnprSess) + ncInt, emptyInt, err := checkInterfaceNC(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !ncInt && !emptyInt { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("interface %s already configured", d.Get("name").(string)))...) } - if sess.junosGroupIntDel != "" { - err = delInterfaceElement("apply-groups "+sess.junosGroupIntDel, d, m, jnprSess) + if clt.groupIntDel != "" { + err = delInterfaceElement("apply-groups "+clt.groupIntDel, d, clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } } else { - err = delInterfaceElement("disable", d, m, jnprSess) + err = delInterfaceElement("disable", d, clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - err = delInterfaceElement("description", d, m, jnprSess) + err = delInterfaceElement("description", d, clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } } } if d.Get("security_zone").(string) != "" { - if !checkCompatibilitySecurity(jnprSess) { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if !checkCompatibilitySecurity(junSess) { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security zone not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel))...) + junSess.SystemInformation.HardwareModel))...) } - zonesExists, err := checkSecurityZonesExists(d.Get("security_zone").(string), m, jnprSess) + zonesExists, err := checkSecurityZonesExists(d.Get("security_zone").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !zonesExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security zone %v doesn't exist", d.Get("security_zone").(string)))...) } } if d.Get("routing_instance").(string) != "" { - instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !instanceExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("routing instance %v doesn't exist", d.Get("routing_instance").(string)))...) } } - if err := setInterface(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setInterface(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_interface", jnprSess) + warns, err := clt.commitConf("create resource junos_interface", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - intExists, err = checkInterfaceExistsOld(d.Get("name").(string), m, jnprSess) + intExists, err = checkInterfaceExistsOld(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } if intExists { - ncInt, _, err := checkInterfaceNC(d.Get("name").(string), m, jnprSess) + ncInt, _, err := checkInterfaceNC(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -565,23 +565,23 @@ func resourceInterfaceCreate(ctx context.Context, d *schema.ResourceData, m inte "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceInterfaceReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceInterfaceReadWJunSess(d, clt, junSess)...) } func resourceInterfaceRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceInterfaceReadWJnprSess(d, m, jnprSess) + return resourceInterfaceReadWJunSess(d, clt, junSess) } -func resourceInterfaceReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) diag.Diagnostics { +func resourceInterfaceReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession) diag.Diagnostics { mutex.Lock() - intExists, err := checkInterfaceExistsOld(d.Get("name").(string), m, jnprSess) + intExists, err := checkInterfaceExistsOld(d.Get("name").(string), clt, junSess) if err != nil { mutex.Unlock() @@ -593,7 +593,7 @@ func resourceInterfaceReadWJnprSess(d *schema.ResourceData, m interface{}, jnprS return nil } - ncInt, _, err := checkInterfaceNC(d.Get("name").(string), m, jnprSess) + ncInt, _, err := checkInterfaceNC(d.Get("name").(string), clt, junSess) if err != nil { mutex.Unlock() @@ -605,7 +605,7 @@ func resourceInterfaceReadWJnprSess(d *schema.ResourceData, m interface{}, jnprS return nil } - interfaceOpt, err := readInterface(d.Get("name").(string), m, jnprSess) + interfaceOpt, err := readInterface(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -617,18 +617,18 @@ func resourceInterfaceReadWJnprSess(d *schema.ResourceData, m interface{}, jnprS func resourceInterfaceUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delInterfaceOpts(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delInterfaceOpts(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -639,36 +639,36 @@ func resourceInterfaceUpdate(ctx context.Context, d *schema.ResourceData, m inte if nAE.(string) != "" { newAE = nAE.(string) } - lastAEchild, err := aggregatedLastChild(oAE.(string), d.Get("name").(string), m, jnprSess) + lastAEchild, err := aggregatedLastChild(oAE.(string), d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if lastAEchild { - aggregatedCount, err := aggregatedCountSearchMax(newAE, oAE.(string), d.Get("name").(string), m, jnprSess) + aggregatedCount, err := aggregatedCountSearchMax(newAE, oAE.(string), d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if aggregatedCount == "0" { - err = sess.configSet([]string{"delete chassis aggregated-devices ethernet device-count"}, jnprSess) + err = clt.configSet([]string{"delete chassis aggregated-devices ethernet device-count"}, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - oAEintNC, oAEintEmpty, err := checkInterfaceNC(oAE.(string), m, jnprSess) + oAEintNC, oAEintEmpty, err := checkInterfaceNC(oAE.(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if oAEintNC || oAEintEmpty { - err = sess.configSet([]string{"delete interfaces " + oAE.(string)}, jnprSess) + err = clt.configSet([]string{"delete interfaces " + oAE.(string)}, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -676,27 +676,27 @@ func resourceInterfaceUpdate(ctx context.Context, d *schema.ResourceData, m inte } else { oldAEInt, err := strconv.Atoi(strings.TrimPrefix(oAE.(string), "ae")) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } aggregatedCountInt, err := strconv.Atoi(aggregatedCount) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if aggregatedCountInt < oldAEInt+1 { - oAEintNC, oAEintEmpty, err := checkInterfaceNC(oAE.(string), m, jnprSess) + oAEintNC, oAEintEmpty, err := checkInterfaceNC(oAE.(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if oAEintNC || oAEintEmpty { - err = sess.configSet([]string{"delete interfaces " + oAE.(string)}, jnprSess) + err = clt.configSet([]string{"delete interfaces " + oAE.(string)}, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -709,28 +709,28 @@ func resourceInterfaceUpdate(ctx context.Context, d *schema.ResourceData, m inte if d.HasChange("security_zone") { oSecurityZone, nSecurityZone := d.GetChange("security_zone") if nSecurityZone.(string) != "" { - if !checkCompatibilitySecurity(jnprSess) { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if !checkCompatibilitySecurity(junSess) { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security zone not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel))...) + junSess.SystemInformation.HardwareModel))...) } - zonesExists, err := checkSecurityZonesExists(nSecurityZone.(string), m, jnprSess) + zonesExists, err := checkSecurityZonesExists(nSecurityZone.(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !zonesExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security zone %v doesn't exist", nSecurityZone.(string)))...) } } if oSecurityZone.(string) != "" { - err = delZoneInterface(oSecurityZone.(string), d, m, jnprSess) + err = delZoneInterface(oSecurityZone.(string), d, clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -739,83 +739,83 @@ func resourceInterfaceUpdate(ctx context.Context, d *schema.ResourceData, m inte if d.HasChange("routing_instance") { oRoutingInstance, nRoutingInstance := d.GetChange("routing_instance") if nRoutingInstance.(string) != "" { - instanceExists, err := checkRoutingInstanceExists(nRoutingInstance.(string), m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(nRoutingInstance.(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !instanceExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("routing instance %v doesn't exist", nRoutingInstance.(string)))...) } } if oRoutingInstance.(string) != "" { - err = delRoutingInstanceInterface(oRoutingInstance.(string), d, m, jnprSess) + err = delRoutingInstanceInterface(oRoutingInstance.(string), d, clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } } } - if err := setInterface(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setInterface(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_interface", jnprSess) + warns, err := clt.commitConf("update resource junos_interface", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceInterfaceReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceInterfaceReadWJunSess(d, clt, junSess)...) } func resourceInterfaceDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delInterface(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delInterface(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_interface", jnprSess) + warns, err := clt.commitConf("delete resource junos_interface", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !d.Get("complete_destroy").(bool) { - intExists, err := checkInterfaceExistsOld(d.Get("name").(string), m, jnprSess) + intExists, err := checkInterfaceExistsOld(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } if intExists { - err = addInterfaceNC(d.Get("name").(string), m, jnprSess) + err = addInterfaceNC(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - _, err = sess.commitConf("disable(NC) resource junos_interface", jnprSess) + _, err = clt.commitConf("disable(NC) resource junos_interface", junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -827,21 +827,21 @@ func resourceInterfaceDelete(ctx context.Context, d *schema.ResourceData, m inte func resourceInterfaceImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - intExists, err := checkInterfaceExistsOld(d.Id(), m, jnprSess) + intExists, err := checkInterfaceExistsOld(d.Id(), clt, junSess) if err != nil { return nil, err } if !intExists { return nil, fmt.Errorf("don't find interface with id '%v' (id must be )", d.Id()) } - interfaceOpt, err := readInterface(d.Id(), m, jnprSess) + interfaceOpt, err := readInterface(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -855,9 +855,8 @@ func resourceInterfaceImport(ctx context.Context, d *schema.ResourceData, m inte return result, nil } -func checkInterfaceNC(interFace string, m interface{}, jnprSess *NetconfObject) (ncInt, emtyInt bool, errFunc error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"interfaces "+interFace+pipeDisplaySetRelative, jnprSess) +func checkInterfaceNC(interFace string, clt *Client, junSess *junosSession) (ncInt, emtyInt bool, errFunc error) { + showConfig, err := clt.command(cmdShowConfig+"interfaces "+interFace+pipeDisplaySetRelative, junSess) if err != nil { return false, false, err } @@ -884,8 +883,8 @@ func checkInterfaceNC(interFace string, m interface{}, jnprSess *NetconfObject) return false, true, nil } showConfig = strings.Join(showConfigLines, "\n") - if sess.junosGroupIntDel != "" { - if showConfig == "set apply-groups "+sess.junosGroupIntDel { + if clt.groupIntDel != "" { + if showConfig == "set apply-groups "+clt.groupIntDel { return true, false, nil } } @@ -901,8 +900,7 @@ func checkInterfaceNC(interFace string, m interface{}, jnprSess *NetconfObject) return false, false, nil } -func addInterfaceNC(interFace string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func addInterfaceNC(interFace string, clt *Client, junSess *junosSession) error { intCut := make([]string, 0, 2) var setName string var err error @@ -919,11 +917,11 @@ func addInterfaceNC(interFace string, m interface{}, jnprSess *NetconfObject) er default: return fmt.Errorf("the name %s contains too dots", interFace) } - if intCut[0] == st0Word || sess.junosGroupIntDel == "" { - err = sess.configSet([]string{"set interfaces " + setName + " disable description NC"}, jnprSess) + if intCut[0] == st0Word || clt.groupIntDel == "" { + err = clt.configSet([]string{"set interfaces " + setName + " disable description NC"}, junSess) } else { - err = sess.configSet([]string{"set interfaces " + setName + - " apply-groups " + sess.junosGroupIntDel}, jnprSess) + err = clt.configSet([]string{"set interfaces " + setName + + " apply-groups " + clt.groupIntDel}, junSess) } if err != nil { return err @@ -932,11 +930,10 @@ func addInterfaceNC(interFace string, m interface{}, jnprSess *NetconfObject) er return nil } -func checkInterfaceExistsOld(interFace string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) +func checkInterfaceExistsOld(interFace string, clt *Client, junSess *junosSession) (bool, error) { rpcIntName := "" + interFace + "" - reply, err := sess.commandXML(rpcIntName, jnprSess) + reply, err := clt.commandXML(rpcIntName, junSess) if err != nil { if strings.Contains(err.Error(), " not found\n") || strings.HasSuffix(err.Error(), " not found") { @@ -952,8 +949,7 @@ func checkInterfaceExistsOld(interFace string, m interface{}, jnprSess *NetconfO return true, nil } -func setInterface(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setInterface(d *schema.ResourceData, clt *Client, junSess *junosSession) error { var setName string intCut := make([]string, 0, 2) configSet := make([]string, 0) @@ -1068,9 +1064,12 @@ func setInterface(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject oldAE = oldAEtf.(string) } } - if jnprSess != nil { - aggregatedCount, err := aggregatedCountSearchMax(d.Get("ether802_3ad").(string), oldAE, - d.Get("name").(string), m, jnprSess) + if junSess != nil { + aggregatedCount, err := aggregatedCountSearchMax( + d.Get("ether802_3ad").(string), + oldAE, + d.Get("name").(string), + clt, junSess) if err != nil { return err } @@ -1117,14 +1116,13 @@ func setInterface(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject " interface "+d.Get("name").(string)) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readInterface(interFace string, m interface{}, jnprSess *NetconfObject) (interfaceOptions, error) { - sess := m.(*Session) +func readInterface(interFace string, clt *Client, junSess *junosSession) (interfaceOptions, error) { var confRead interfaceOptions - showConfig, err := sess.command(cmdShowConfig+"interfaces "+interFace+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"interfaces "+interFace+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -1251,8 +1249,8 @@ func readInterface(interFace string, m interface{}, jnprSess *NetconfObject) (in confRead.inetAddress = inetAddress confRead.inet6Address = inet6Address } - if checkCompatibilitySecurity(jnprSess) { - showConfigSecurityZones, err := sess.command(cmdShowConfig+"security zones"+pipeDisplaySetRelative, jnprSess) + if checkCompatibilitySecurity(junSess) { + showConfigSecurityZones, err := clt.command(cmdShowConfig+"security zones"+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -1267,7 +1265,7 @@ func readInterface(interFace string, m interface{}, jnprSess *NetconfObject) (in } } } - showConfigRoutingInstances, err := sess.command(cmdShowConfig+"routing-instances"+pipeDisplaySetRelative, jnprSess) + showConfigRoutingInstances, err := clt.command(cmdShowConfig+"routing-instances"+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -1284,8 +1282,7 @@ func readInterface(interFace string, m interface{}, jnprSess *NetconfObject) (in return confRead, nil } -func delInterface(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delInterface(d *schema.ResourceData, clt *Client, junSess *junosSession) error { intCut := make([]string, 0, 2) var setName string if strings.Contains(d.Get("name").(string), ".") { @@ -1298,7 +1295,7 @@ func delInterface(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject setName = intCut[0] + " unit " + intCut[1] case 1: setName = intCut[0] - err := checkInterfaceContainsUnit(setName, m, jnprSess) + err := checkInterfaceContainsUnit(setName, clt, junSess) if err != nil { return err } @@ -1306,7 +1303,7 @@ func delInterface(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject return fmt.Errorf("the name %s contains too dots", d.Get("name").(string)) } - if err := sess.configSet([]string{"delete interfaces " + setName}, jnprSess); err != nil { + if err := clt.configSet([]string{"delete interfaces " + setName}, junSess); err != nil { return err } if strings.Contains(d.Get("name").(string), "st0.") && !d.Get("complete_destroy").(bool) { @@ -1315,30 +1312,33 @@ func delInterface(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject // or by // - junos_interface_st0_unit resource // else there is an interface st0.x empty - err := sess.configSet([]string{"set interfaces " + setName}, jnprSess) + err := clt.configSet([]string{"set interfaces " + setName}, junSess) if err != nil { return err } } if d.Get("ether802_3ad").(string) != "" { - lastAEchild, err := aggregatedLastChild(d.Get("ether802_3ad").(string), d.Get("name").(string), m, jnprSess) + lastAEchild, err := aggregatedLastChild(d.Get("ether802_3ad").(string), d.Get("name").(string), clt, junSess) if err != nil { return err } if lastAEchild { - aggregatedCount, err := aggregatedCountSearchMax("ae-1", d.Get("ether802_3ad").(string), - d.Get("name").(string), m, jnprSess) + aggregatedCount, err := aggregatedCountSearchMax( + "ae-1", + d.Get("ether802_3ad").(string), + d.Get("name").(string), + clt, junSess) if err != nil { return err } if aggregatedCount == "0" { - err = sess.configSet([]string{"delete chassis aggregated-devices ethernet device-count"}, jnprSess) + err = clt.configSet([]string{"delete chassis aggregated-devices ethernet device-count"}, junSess) if err != nil { return err } } else { - err = sess.configSet([]string{"set chassis aggregated-devices ethernet device-count " + - aggregatedCount}, jnprSess) + err = clt.configSet([]string{"set chassis aggregated-devices ethernet device-count " + + aggregatedCount}, junSess) if err != nil { return err } @@ -1353,12 +1353,12 @@ func delInterface(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject return fmt.Errorf("failed to convert internal variable aggregatedCountInt in integer: %w", err) } if aggregatedCountInt < aeInt+1 { - oAEintNC, oAEintEmpty, err := checkInterfaceNC(d.Get("ether802_3ad").(string), m, jnprSess) + oAEintNC, oAEintEmpty, err := checkInterfaceNC(d.Get("ether802_3ad").(string), clt, junSess) if err != nil { return err } if oAEintNC || oAEintEmpty { - err = sess.configSet([]string{"delete interfaces " + d.Get("ether802_3ad").(string)}, jnprSess) + err = clt.configSet([]string{"delete interfaces " + d.Get("ether802_3ad").(string)}, junSess) if err != nil { return err } @@ -1366,13 +1366,13 @@ func delInterface(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject } } } - if checkCompatibilitySecurity(jnprSess) && d.Get("security_zone").(string) != "" { - if err := delZoneInterface(d.Get("security_zone").(string), d, m, jnprSess); err != nil { + if checkCompatibilitySecurity(junSess) && d.Get("security_zone").(string) != "" { + if err := delZoneInterface(d.Get("security_zone").(string), d, clt, junSess); err != nil { return err } } if d.Get("routing_instance").(string) != "" { - if err := delRoutingInstanceInterface(d.Get("routing_instance").(string), d, m, jnprSess); err != nil { + if err := delRoutingInstanceInterface(d.Get("routing_instance").(string), d, clt, junSess); err != nil { return err } } @@ -1380,9 +1380,8 @@ func delInterface(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject return nil } -func checkInterfaceContainsUnit(interFace string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"interfaces "+interFace+pipeDisplaySetRelative, jnprSess) +func checkInterfaceContainsUnit(interFace string, clt *Client, junSess *junosSession) error { + showConfig, err := clt.command(cmdShowConfig+"interfaces "+interFace+pipeDisplaySetRelative, junSess) if err != nil { return err } @@ -1405,8 +1404,7 @@ func checkInterfaceContainsUnit(interFace string, m interface{}, jnprSess *Netco return nil } -func delInterfaceElement(element string, d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delInterfaceElement(element string, d *schema.ResourceData, clt *Client, junSess *junosSession) error { intCut := make([]string, 0, 2) var setName string configSet := make([]string, 0, 1) @@ -1425,11 +1423,10 @@ func delInterfaceElement(element string, d *schema.ResourceData, m interface{}, } configSet = append(configSet, "delete interfaces "+setName+" "+element) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func delInterfaceOpts(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delInterfaceOpts(d *schema.ResourceData, clt *Client, junSess *junosSession) error { intCut := make([]string, 0, 2) var setName string configSet := make([]string, 0, 1) @@ -1459,24 +1456,22 @@ func delInterfaceOpts(d *schema.ResourceData, m interface{}, jnprSess *NetconfOb delPrefix+"aggregated-ether-options", ) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func delZoneInterface(zone string, d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delZoneInterface(zone string, d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete security zones security-zone "+zone+" interfaces "+d.Get("name").(string)) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func delRoutingInstanceInterface(instance string, d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func delRoutingInstanceInterface(instance string, d *schema.ResourceData, clt *Client, junSess *junosSession, ) error { - sess := m.(*Session) configSet := make([]string, 0, 1) configSet = append(configSet, delRoutingInstances+instance+" interface "+d.Get("name").(string)) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillInterfaceData(d *schema.ResourceData, interfaceOpt interfaceOptions) { @@ -1758,9 +1753,8 @@ func setFamilyAddressOld(inetAddress interface{}, intCut, configSet []string, se return configSet, nil } -func aggregatedLastChild(ae, interFace string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"interfaces"+pipeDisplaySetRelative, jnprSess) +func aggregatedLastChild(ae, interFace string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"interfaces"+pipeDisplaySetRelative, junSess) if err != nil { return false, err } @@ -1775,14 +1769,13 @@ func aggregatedLastChild(ae, interFace string, m interface{}, jnprSess *NetconfO return lastAE, nil } -func aggregatedCountSearchMax(newAE, oldAE, interFace string, m interface{}, jnprSess *NetconfObject) (string, error) { - sess := m.(*Session) +func aggregatedCountSearchMax(newAE, oldAE, interFace string, clt *Client, junSess *junosSession) (string, error) { newAENum := strings.TrimPrefix(newAE, "ae") newAENumInt, err := strconv.Atoi(newAENum) if err != nil { return "", fmt.Errorf("failed to convert internal variable newAENum to integer: %w", err) } - intShowInt, err := sess.command("show interfaces terse", jnprSess) + intShowInt, err := clt.command("show interfaces terse", junSess) if err != nil { return "", err } @@ -1801,7 +1794,7 @@ func aggregatedCountSearchMax(newAE, oldAE, interFace string, m interface{}, jnp } } } - lastOldAE, err := aggregatedLastChild(oldAE, interFace, m, jnprSess) + lastOldAE, err := aggregatedLastChild(oldAE, interFace, clt, junSess) if err != nil { return "", err } diff --git a/junos/resource_interface_logical.go b/junos/resource_interface_logical.go index 90a2da75..2ef7d698 100644 --- a/junos/resource_interface_logical.go +++ b/junos/resource_interface_logical.go @@ -15,6 +15,7 @@ import ( ) type interfaceLogicalOptions struct { + disable bool vlanID int description string routingInstance string @@ -58,6 +59,10 @@ func resourceInterfaceLogical() *schema.Resource { Type: schema.TypeString, Optional: true, }, + "disable": { + Type: schema.TypeBool, + Optional: true, + }, "family_inet": { Type: schema.TypeList, Optional: true, @@ -681,101 +686,101 @@ func resourceInterfaceLogical() *schema.Resource { } func resourceInterfaceLogicalCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := delInterfaceNC(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := delInterfaceNC(d, clt, nil); err != nil { return diag.FromErr(err) } - if err := setInterfaceLogical(d, m, nil); err != nil { + if err := setInterfaceLogical(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - ncInt, emptyInt, _, err := checkInterfaceLogicalNCEmpty(d.Get("name").(string), m, jnprSess) + ncInt, emptyInt, _, err := checkInterfaceLogicalNCEmpty(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !ncInt && !emptyInt { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("interface %s already configured", d.Get("name").(string)))...) } if ncInt { - if err := delInterfaceNC(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delInterfaceNC(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } } if d.Get("security_zone").(string) != "" { - if !checkCompatibilitySecurity(jnprSess) { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if !checkCompatibilitySecurity(junSess) { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security zone not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel))...) + junSess.SystemInformation.HardwareModel))...) } - zonesExists, err := checkSecurityZonesExists(d.Get("security_zone").(string), m, jnprSess) + zonesExists, err := checkSecurityZonesExists(d.Get("security_zone").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !zonesExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security zone %v doesn't exist", d.Get("security_zone").(string)))...) } } if d.Get("routing_instance").(string) != "" { - instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !instanceExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("routing instance %v doesn't exist", d.Get("routing_instance").(string)))...) } } - if err := setInterfaceLogical(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setInterfaceLogical(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_interface_logical", jnprSess) + warns, err := clt.commitConf("create resource junos_interface_logical", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - ncInt, emptyInt, setInt, err := checkInterfaceLogicalNCEmpty(d.Get("name").(string), m, jnprSess) + ncInt, emptyInt, setInt, err := checkInterfaceLogicalNCEmpty(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } if ncInt { - return append(diagWarns, diag.FromErr(fmt.Errorf("interface %v always disable after commit "+ + return append(diagWarns, diag.FromErr(fmt.Errorf("interface %v always disable (NC) after commit "+ "=> check your config", d.Get("name").(string)))...) } if emptyInt && !setInt { - intExists, err := checkInterfaceExists(d.Get("name").(string), m, jnprSess) + intExists, err := checkInterfaceExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -786,24 +791,24 @@ func resourceInterfaceLogicalCreate(ctx context.Context, d *schema.ResourceData, } d.SetId(d.Get("name").(string)) - return append(diagWarns, resourceInterfaceLogicalReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceInterfaceLogicalReadWJunSess(d, clt, junSess)...) } func resourceInterfaceLogicalRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceInterfaceLogicalReadWJnprSess(d, m, jnprSess) + return resourceInterfaceLogicalReadWJunSess(d, clt, junSess) } -func resourceInterfaceLogicalReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceInterfaceLogicalReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - ncInt, emptyInt, setInt, err := checkInterfaceLogicalNCEmpty(d.Get("name").(string), m, jnprSess) + ncInt, emptyInt, setInt, err := checkInterfaceLogicalNCEmpty(d.Get("name").(string), clt, junSess) if err != nil { mutex.Unlock() @@ -816,7 +821,7 @@ func resourceInterfaceLogicalReadWJnprSess(d *schema.ResourceData, m interface{} return nil } if emptyInt && !setInt { - intExists, err := checkInterfaceExists(d.Get("name").(string), m, jnprSess) + intExists, err := checkInterfaceExists(d.Get("name").(string), clt, junSess) if err != nil { mutex.Unlock() @@ -829,7 +834,7 @@ func resourceInterfaceLogicalReadWJnprSess(d *schema.ResourceData, m interface{} return nil } } - interfaceLogicalOpt, err := readInterfaceLogical(d.Get("name").(string), m, jnprSess) + interfaceLogicalOpt, err := readInterfaceLogical(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -841,82 +846,82 @@ func resourceInterfaceLogicalReadWJnprSess(d *schema.ResourceData, m interface{} func resourceInterfaceLogicalUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delInterfaceLogicalOpts(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delInterfaceLogicalOpts(d, clt, nil); err != nil { return diag.FromErr(err) } if d.HasChange("security_zone") { if oSecurityZone, _ := d.GetChange("security_zone"); oSecurityZone.(string) != "" { - if err := delZoneInterfaceLogical(oSecurityZone.(string), d, m, nil); err != nil { + if err := delZoneInterfaceLogical(oSecurityZone.(string), d, clt, nil); err != nil { return diag.FromErr(err) } } } else if v := d.Get("security_zone").(string); v != "" { - if err := delZoneInterfaceLogical(v, d, m, nil); err != nil { + if err := delZoneInterfaceLogical(v, d, clt, nil); err != nil { return diag.FromErr(err) } } if d.HasChange("routing_instance") { if oRoutingInstance, _ := d.GetChange("routing_instance"); oRoutingInstance.(string) != "" { - if err := delRoutingInstanceInterfaceLogical(oRoutingInstance.(string), d, m, nil); err != nil { + if err := delRoutingInstanceInterfaceLogical(oRoutingInstance.(string), d, clt, nil); err != nil { return diag.FromErr(err) } } } - if err := setInterfaceLogical(d, m, nil); err != nil { + if err := setInterfaceLogical(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delInterfaceLogicalOpts(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delInterfaceLogicalOpts(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if d.HasChange("security_zone") { oSecurityZone, nSecurityZone := d.GetChange("security_zone") if nSecurityZone.(string) != "" { - if !checkCompatibilitySecurity(jnprSess) { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if !checkCompatibilitySecurity(junSess) { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security zone not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel))...) + junSess.SystemInformation.HardwareModel))...) } - zonesExists, err := checkSecurityZonesExists(nSecurityZone.(string), m, jnprSess) + zonesExists, err := checkSecurityZonesExists(nSecurityZone.(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !zonesExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security zone %v doesn't exist", nSecurityZone.(string)))...) } } if oSecurityZone.(string) != "" { - err = delZoneInterfaceLogical(oSecurityZone.(string), d, m, jnprSess) + err = delZoneInterfaceLogical(oSecurityZone.(string), d, clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } } } else if v := d.Get("security_zone").(string); v != "" { - if err := delZoneInterfaceLogical(v, d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delZoneInterfaceLogical(v, d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -924,72 +929,72 @@ func resourceInterfaceLogicalUpdate(ctx context.Context, d *schema.ResourceData, if d.HasChange("routing_instance") { oRoutingInstance, nRoutingInstance := d.GetChange("routing_instance") if nRoutingInstance.(string) != "" { - instanceExists, err := checkRoutingInstanceExists(nRoutingInstance.(string), m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(nRoutingInstance.(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !instanceExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("routing instance %v doesn't exist", nRoutingInstance.(string)))...) } } if oRoutingInstance.(string) != "" { - err = delRoutingInstanceInterfaceLogical(oRoutingInstance.(string), d, m, jnprSess) + err = delRoutingInstanceInterfaceLogical(oRoutingInstance.(string), d, clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } } } - if err := setInterfaceLogical(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setInterfaceLogical(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_interface_logical", jnprSess) + warns, err := clt.commitConf("update resource junos_interface_logical", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceInterfaceLogicalReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceInterfaceLogicalReadWJunSess(d, clt, junSess)...) } func resourceInterfaceLogicalDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delInterfaceLogical(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delInterfaceLogical(d, clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delInterfaceLogical(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delInterfaceLogical(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_interface_logical", jnprSess) + warns, err := clt.commitConf("delete resource junos_interface_logical", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -1002,22 +1007,22 @@ func resourceInterfaceLogicalImport(ctx context.Context, d *schema.ResourceData, if strings.Count(d.Id(), ".") != 1 { return nil, fmt.Errorf("name of interface %s need to have 1 dot", d.Id()) } - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - ncInt, emptyInt, setInt, err := checkInterfaceLogicalNCEmpty(d.Id(), m, jnprSess) + ncInt, emptyInt, setInt, err := checkInterfaceLogicalNCEmpty(d.Id(), clt, junSess) if err != nil { return nil, err } if ncInt { - return nil, fmt.Errorf("interface '%v' is disabled, import is not possible", d.Id()) + return nil, fmt.Errorf("interface '%v' is disabled (NC), import is not possible", d.Id()) } if emptyInt && !setInt { - intExists, err := checkInterfaceExists(d.Id(), m, jnprSess) + intExists, err := checkInterfaceExists(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -1025,7 +1030,7 @@ func resourceInterfaceLogicalImport(ctx context.Context, d *schema.ResourceData, return nil, fmt.Errorf("don't find interface with id '%v' (id must be )", d.Id()) } } - interfaceLogicalOpt, err := readInterfaceLogical(d.Id(), m, jnprSess) + interfaceLogicalOpt, err := readInterfaceLogical(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -1049,10 +1054,9 @@ func resourceInterfaceLogicalImport(ctx context.Context, d *schema.ResourceData, return result, nil } -func checkInterfaceLogicalNCEmpty(interFace string, m interface{}, jnprSess *NetconfObject, +func checkInterfaceLogicalNCEmpty(interFace string, clt *Client, junSess *junosSession, ) (ncInt, emtyInt, justSet bool, _err error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"interfaces "+interFace+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"interfaces "+interFace+pipeDisplaySetRelative, junSess) if err != nil { return false, false, false, err } @@ -1078,8 +1082,8 @@ func checkInterfaceLogicalNCEmpty(interFace string, m interface{}, jnprSess *Net return false, true, true, nil } showConfig = strings.Join(showConfigLines, "\n") - if sess.junosGroupIntDel != "" { - if showConfig == "set apply-groups "+sess.junosGroupIntDel { + if clt.groupIntDel != "" { + if showConfig == "set apply-groups "+clt.groupIntDel { return true, false, false, nil } } @@ -1097,8 +1101,7 @@ func checkInterfaceLogicalNCEmpty(interFace string, m interface{}, jnprSess *Net } } -func setInterfaceLogical(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setInterfaceLogical(d *schema.ResourceData, clt *Client, junSess *junosSession) error { intCut := strings.Split(d.Get("name").(string), ".") if len(intCut) != 2 { return fmt.Errorf("the name %s doesn't contain one dot", d.Get("name").(string)) @@ -1109,6 +1112,13 @@ func setInterfaceLogical(d *schema.ResourceData, m interface{}, jnprSess *Netcon if d.Get("description").(string) != "" { configSet = append(configSet, setPrefix+"description \""+d.Get("description").(string)+"\"") } + if d.Get("disable").(bool) { + if d.Get("description").(string) == "NC" { + return fmt.Errorf("disable=true and description=NC is not allowed " + + "because the provider might consider the resource deleted") + } + configSet = append(configSet, setPrefix+"disable") + } for _, v := range d.Get("family_inet").([]interface{}) { configSet = append(configSet, setPrefix+"family inet") if v != nil { @@ -1253,14 +1263,13 @@ func setInterfaceLogical(d *schema.ResourceData, m interface{}, jnprSess *Netcon configSet = append(configSet, setPrefix+"vlan-id "+intCut[1]) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readInterfaceLogical(interFace string, m interface{}, jnprSess *NetconfObject) (interfaceLogicalOptions, error) { - sess := m.(*Session) +func readInterfaceLogical(interFace string, clt *Client, junSess *junosSession) (interfaceLogicalOptions, error) { var confRead interfaceLogicalOptions - showConfig, err := sess.command(cmdShowConfig+"interfaces "+interFace+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"interfaces "+interFace+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -1281,6 +1290,8 @@ func readInterfaceLogical(interFace string, m interface{}, jnprSess *NetconfObje switch { case strings.HasPrefix(itemTrim, "description "): confRead.description = strings.Trim(strings.TrimPrefix(itemTrim, "description "), "\"") + case itemTrim == "disable": + confRead.disable = true case strings.HasPrefix(itemTrim, "family inet6"): if len(confRead.familyInet6) == 0 { confRead.familyInet6 = append(confRead.familyInet6, map[string]interface{}{ @@ -1503,7 +1514,7 @@ func readInterfaceLogical(interFace string, m interface{}, jnprSess *NetconfObje } } } - showConfigRoutingInstances, err := sess.command(cmdShowConfig+"routing-instances"+pipeDisplaySetRelative, jnprSess) + showConfigRoutingInstances, err := clt.command(cmdShowConfig+"routing-instances"+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -1516,8 +1527,8 @@ func readInterfaceLogical(interFace string, m interface{}, jnprSess *NetconfObje break } } - if checkCompatibilitySecurity(jnprSess) { - showConfigSecurityZones, err := sess.command(cmdShowConfig+"security zones"+pipeDisplaySetRelative, jnprSess) + if checkCompatibilitySecurity(junSess) { + showConfigSecurityZones, err := clt.command(cmdShowConfig+"security zones"+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -1527,7 +1538,7 @@ func readInterfaceLogical(interFace string, m interface{}, jnprSess *NetconfObje if intMatch { itemTrimSplit := strings.Split(strings.TrimPrefix(item, "set security-zone "), " ") confRead.securityZone = itemTrimSplit[0] - if err := readInterfaceLogicalSecurityInboundTraffic(interFace, &confRead, m, jnprSess); err != nil { + if err := readInterfaceLogicalSecurityInboundTraffic(interFace, &confRead, clt, junSess); err != nil { return confRead, err } @@ -1540,12 +1551,10 @@ func readInterfaceLogical(interFace string, m interface{}, jnprSess *NetconfObje } func readInterfaceLogicalSecurityInboundTraffic( - interFace string, confRead *interfaceLogicalOptions, m interface{}, jnprSess *NetconfObject, + interFace string, confRead *interfaceLogicalOptions, clt *Client, junSess *junosSession, ) error { - sess := m.(*Session) - - showConfig, err := sess.command(cmdShowConfig+ - "security zones security-zone "+confRead.securityZone+" interfaces "+interFace+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security zones security-zone "+confRead.securityZone+" interfaces "+interFace+pipeDisplaySetRelative, junSess) if err != nil { return err } @@ -1573,28 +1582,27 @@ func readInterfaceLogicalSecurityInboundTraffic( return nil } -func delInterfaceLogical(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) - if err := sess.configSet([]string{"delete interfaces " + d.Get("name").(string)}, jnprSess); err != nil { +func delInterfaceLogical(d *schema.ResourceData, clt *Client, junSess *junosSession) error { + if err := clt.configSet([]string{"delete interfaces " + d.Get("name").(string)}, junSess); err != nil { return err } if strings.HasPrefix(d.Get("name").(string), "st0.") && !d.Get("st0_also_on_destroy").(bool) { // interface totally delete by // - junos_interface_st0_unit resource // else there is an interface st0.x empty - err := sess.configSet([]string{"set interfaces " + d.Get("name").(string)}, jnprSess) + err := clt.configSet([]string{"set interfaces " + d.Get("name").(string)}, junSess) if err != nil { return err } } if d.Get("routing_instance").(string) != "" { - if err := delRoutingInstanceInterfaceLogical(d.Get("routing_instance").(string), d, m, jnprSess); err != nil { + if err := delRoutingInstanceInterfaceLogical(d.Get("routing_instance").(string), d, clt, junSess); err != nil { return err } } if d.Get("security_zone").(string) != "" { - if jnprSess == nil || checkCompatibilitySecurity(jnprSess) { - if err := delZoneInterfaceLogical(d.Get("security_zone").(string), d, m, jnprSess); err != nil { + if junSess == nil || checkCompatibilitySecurity(junSess) { + if err := delZoneInterfaceLogical(d.Get("security_zone").(string), d, clt, junSess); err != nil { return err } } @@ -1603,41 +1611,42 @@ func delInterfaceLogical(d *schema.ResourceData, m interface{}, jnprSess *Netcon return nil } -func delInterfaceLogicalOpts(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delInterfaceLogicalOpts(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) delPrefix := "delete interfaces " + d.Get("name").(string) + " " configSet = append(configSet, delPrefix+"description", + delPrefix+"disable", delPrefix+"family inet", delPrefix+"family inet6", delPrefix+"tunnel", ) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func delZoneInterfaceLogical(zone string, d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delZoneInterfaceLogical(zone string, d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete security zones security-zone "+zone+" interfaces "+d.Get("name").(string)) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func delRoutingInstanceInterfaceLogical(instance string, d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func delRoutingInstanceInterfaceLogical(instance string, d *schema.ResourceData, clt *Client, junSess *junosSession, ) error { - sess := m.(*Session) configSet := make([]string, 0, 1) configSet = append(configSet, delRoutingInstances+instance+" interface "+d.Get("name").(string)) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillInterfaceLogicalData(d *schema.ResourceData, interfaceLogicalOpt interfaceLogicalOptions) { if tfErr := d.Set("description", interfaceLogicalOpt.description); tfErr != nil { panic(tfErr) } + if tfErr := d.Set("disable", interfaceLogicalOpt.disable); tfErr != nil { + panic(tfErr) + } if tfErr := d.Set("family_inet", interfaceLogicalOpt.familyInet); tfErr != nil { panic(tfErr) } diff --git a/junos/resource_interface_logical_test.go b/junos/resource_interface_logical_test.go index d977efc0..b475d8c7 100644 --- a/junos/resource_interface_logical_test.go +++ b/junos/resource_interface_logical_test.go @@ -229,6 +229,7 @@ resource "junos_interface_physical" "testacc_interface_logical_phy" { resource "junos_interface_logical" "testacc_interface_logical" { name = "${junos_interface_physical.testacc_interface_logical_phy.name}.100" description = "testacc_interface_${junos_interface_physical.testacc_interface_logical_phy.name}.100" + disable = true security_zone = junos_security_zone.testacc_interface_logical.name security_inbound_protocols = ["bgp"] security_inbound_services = ["ssh"] diff --git a/junos/resource_interface_physical.go b/junos/resource_interface_physical.go index f943a2cd..d8fb5362 100644 --- a/junos/resource_interface_physical.go +++ b/junos/resource_interface_physical.go @@ -14,6 +14,7 @@ import ( ) type interfacePhysicalOptions struct { + disable bool trunk bool vlanTagging bool aeMinLink int @@ -81,6 +82,10 @@ func resourceInterfacePhysical() *schema.Resource { Type: schema.TypeString, Optional: true, }, + "disable": { + Type: schema.TypeBool, + Optional: true, + }, "esi": { Type: schema.TypeList, Optional: true, @@ -488,67 +493,67 @@ func resourceInterfacePhysical() *schema.Resource { } func resourceInterfacePhysicalCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := delInterfaceNC(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := delInterfaceNC(d, clt, nil); err != nil { return diag.FromErr(err) } - if err := setInterfacePhysical(d, m, nil); err != nil { + if err := setInterfacePhysical(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - ncInt, emptyInt, err := checkInterfacePhysicalNCEmpty(d.Get("name").(string), m, jnprSess) + ncInt, emptyInt, err := checkInterfacePhysicalNCEmpty(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !ncInt && !emptyInt { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("interface %s already configured", d.Get("name").(string)))...) } if ncInt { - if err := delInterfaceNC(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delInterfaceNC(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } } - if err := setInterfacePhysical(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setInterfacePhysical(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_interface_physical", jnprSess) + warns, err := clt.commitConf("create resource junos_interface_physical", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - ncInt, emptyInt, err = checkInterfacePhysicalNCEmpty(d.Get("name").(string), m, jnprSess) + ncInt, emptyInt, err = checkInterfacePhysicalNCEmpty(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } if ncInt { - return append(diagWarns, diag.FromErr(fmt.Errorf("interface %v always disable after commit "+ + return append(diagWarns, diag.FromErr(fmt.Errorf("interface %v always disable (NC) after commit "+ "=> check your config", d.Get("name").(string)))...) } if emptyInt { - intExists, err := checkInterfaceExists(d.Get("name").(string), m, jnprSess) + intExists, err := checkInterfaceExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -559,24 +564,24 @@ func resourceInterfacePhysicalCreate(ctx context.Context, d *schema.ResourceData } d.SetId(d.Get("name").(string)) - return append(diagWarns, resourceInterfacePhysicalReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceInterfacePhysicalReadWJunSess(d, clt, junSess)...) } func resourceInterfacePhysicalRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceInterfacePhysicalReadWJnprSess(d, m, jnprSess) + return resourceInterfacePhysicalReadWJunSess(d, clt, junSess) } -func resourceInterfacePhysicalReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceInterfacePhysicalReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - ncInt, emptyInt, err := checkInterfacePhysicalNCEmpty(d.Get("name").(string), m, jnprSess) + ncInt, emptyInt, err := checkInterfacePhysicalNCEmpty(d.Get("name").(string), clt, junSess) if err != nil { mutex.Unlock() @@ -589,7 +594,7 @@ func resourceInterfacePhysicalReadWJnprSess(d *schema.ResourceData, m interface{ return nil } if emptyInt { - intExists, err := checkInterfaceExists(d.Get("name").(string), m, jnprSess) + intExists, err := checkInterfaceExists(d.Get("name").(string), clt, junSess) if err != nil { mutex.Unlock() @@ -602,7 +607,7 @@ func resourceInterfacePhysicalReadWJnprSess(d *schema.ResourceData, m interface{ return nil } } - interfaceOpt, err := readInterfacePhysical(d.Get("name").(string), m, jnprSess) + interfaceOpt, err := readInterfacePhysical(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -614,98 +619,98 @@ func resourceInterfacePhysicalReadWJnprSess(d *schema.ResourceData, m interface{ func resourceInterfacePhysicalUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delInterfacePhysicalOpts(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delInterfacePhysicalOpts(d, clt, nil); err != nil { return diag.FromErr(err) } - if err := setInterfacePhysical(d, m, nil); err != nil { + if err := setInterfacePhysical(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delInterfacePhysicalOpts(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delInterfacePhysicalOpts(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := unsetInterfacePhysicalAE(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := unsetInterfacePhysicalAE(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setInterfacePhysical(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setInterfacePhysical(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_interface_physical", jnprSess) + warns, err := clt.commitConf("update resource junos_interface_physical", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceInterfacePhysicalReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceInterfacePhysicalReadWJunSess(d, clt, junSess)...) } func resourceInterfacePhysicalDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delInterfacePhysical(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delInterfacePhysical(d, clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delInterfacePhysical(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delInterfacePhysical(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_interface_physical", jnprSess) + warns, err := clt.commitConf("delete resource junos_interface_physical", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !d.Get("no_disable_on_destroy").(bool) { - intExists, err := checkInterfaceExists(d.Get("name").(string), m, jnprSess) + intExists, err := checkInterfaceExists(d.Get("name").(string), clt, junSess) if err != nil { appendDiagWarns(&diagWarns, []error{err}) } else if intExists { - err = addInterfacePhysicalNC(d.Get("name").(string), m, jnprSess) + err = addInterfacePhysicalNC(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - _, err = sess.commitConf("disable(NC) resource junos_interface_physical", jnprSess) + _, err = clt.commitConf("disable(NC) resource junos_interface_physical", junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -720,22 +725,22 @@ func resourceInterfacePhysicalImport(ctx context.Context, d *schema.ResourceData if strings.Count(d.Id(), ".") != 0 { return nil, fmt.Errorf("name of interface %s need to doesn't have a dot", d.Id()) } - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - ncInt, emptyInt, err := checkInterfacePhysicalNCEmpty(d.Id(), m, jnprSess) + ncInt, emptyInt, err := checkInterfacePhysicalNCEmpty(d.Id(), clt, junSess) if err != nil { return nil, err } if ncInt { - return nil, fmt.Errorf("interface '%v' is disabled, import is not possible", d.Id()) + return nil, fmt.Errorf("interface '%v' is disabled (NC), import is not possible", d.Id()) } if emptyInt { - intExists, err := checkInterfaceExists(d.Id(), m, jnprSess) + intExists, err := checkInterfaceExists(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -743,7 +748,7 @@ func resourceInterfacePhysicalImport(ctx context.Context, d *schema.ResourceData return nil, fmt.Errorf("don't find interface with id '%v' (id must be )", d.Id()) } } - interfaceOpt, err := readInterfacePhysical(d.Id(), m, jnprSess) + interfaceOpt, err := readInterfacePhysical(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -757,10 +762,9 @@ func resourceInterfacePhysicalImport(ctx context.Context, d *schema.ResourceData return result, nil } -func checkInterfacePhysicalNCEmpty(interFace string, m interface{}, jnprSess *NetconfObject, +func checkInterfacePhysicalNCEmpty(interFace string, clt *Client, junSess *junosSession, ) (ncInt, emtyInt bool, errFunc error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"interfaces "+interFace+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"interfaces "+interFace+pipeDisplaySetRelative, junSess) if err != nil { return false, false, err } @@ -786,8 +790,8 @@ func checkInterfacePhysicalNCEmpty(interFace string, m interface{}, jnprSess *Ne return false, true, nil } showConfig = strings.Join(showConfigLines, "\n") - if sess.junosGroupIntDel != "" { - if showConfig == "set apply-groups "+sess.junosGroupIntDel { + if clt.groupIntDel != "" { + if showConfig == "set apply-groups "+clt.groupIntDel { return true, false, nil } } @@ -802,13 +806,13 @@ func checkInterfacePhysicalNCEmpty(interFace string, m interface{}, jnprSess *Ne return false, false, nil } -func addInterfacePhysicalNC(interFace string, m interface{}, jnprSess *NetconfObject) error { +func addInterfacePhysicalNC(interFace string, clt *Client, junSess *junosSession) error { var err error - if sess := m.(*Session); sess.junosGroupIntDel == "" { - err = sess.configSet([]string{"set interfaces " + interFace + " disable description NC"}, jnprSess) + if clt.groupIntDel == "" { + err = clt.configSet([]string{"set interfaces " + interFace + " disable description NC"}, junSess) } else { - err = sess.configSet([]string{"set interfaces " + interFace + - " apply-groups " + sess.junosGroupIntDel}, jnprSess) + err = clt.configSet([]string{"set interfaces " + interFace + + " apply-groups " + clt.groupIntDel}, junSess) } if err != nil { return err @@ -817,11 +821,10 @@ func addInterfacePhysicalNC(interFace string, m interface{}, jnprSess *NetconfOb return nil } -func checkInterfaceExists(interFace string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) +func checkInterfaceExists(interFace string, clt *Client, junSess *junosSession) (bool, error) { rpcIntName := "" + interFace + "" - reply, err := sess.commandXML(rpcIntName, jnprSess) + reply, err := clt.commandXML(rpcIntName, junSess) if err != nil { if strings.Contains(err.Error(), " not found\n") || strings.HasSuffix(err.Error(), " not found") { @@ -837,8 +840,7 @@ func checkInterfaceExists(interFace string, m interface{}, jnprSess *NetconfObje return true, nil } -func unsetInterfacePhysicalAE(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func unsetInterfacePhysicalAE(d *schema.ResourceData, clt *Client, junSess *junosSession) error { var oldAE string switch { case d.HasChange("ether802_3ad"): @@ -864,22 +866,21 @@ func unsetInterfacePhysicalAE(d *schema.ResourceData, m interface{}, jnprSess *N } } if oldAE != "" { - aggregatedCount, err := interfaceAggregatedCountSearchMax("ae-1", oldAE, d.Get("name").(string), m, jnprSess) + aggregatedCount, err := interfaceAggregatedCountSearchMax("ae-1", oldAE, d.Get("name").(string), clt, junSess) if err != nil { return err } if aggregatedCount == "0" { - return sess.configSet([]string{"delete chassis aggregated-devices ethernet device-count"}, jnprSess) + return clt.configSet([]string{"delete chassis aggregated-devices ethernet device-count"}, junSess) } - return sess.configSet([]string{"set chassis aggregated-devices ethernet device-count " + aggregatedCount}, jnprSess) + return clt.configSet([]string{"set chassis aggregated-devices ethernet device-count " + aggregatedCount}, junSess) } return nil } -func setInterfacePhysical(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setInterfacePhysical(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set interfaces " + d.Get("name").(string) + " " configSet = append(configSet, setPrefix) @@ -907,11 +908,18 @@ func setInterfacePhysical(d *schema.ResourceData, m interface{}, jnprSess *Netco if d.Get("description").(string) != "" { configSet = append(configSet, setPrefix+"description \""+d.Get("description").(string)+"\"") } - if err := setInterfacePhysicalEsi(setPrefix, d.Get("esi").([]interface{}), m, jnprSess); err != nil { + if d.Get("disable").(bool) { + if d.Get("description").(string) == "NC" { + return fmt.Errorf("disable=true and description=NC is not allowed " + + "because the provider might consider the resource deleted") + } + configSet = append(configSet, setPrefix+"disable") + } + if err := setInterfacePhysicalEsi(setPrefix, d.Get("esi").([]interface{}), clt, junSess); err != nil { return err } - if v := d.Get("name").(string); strings.HasPrefix(v, "ae") && jnprSess != nil { - aggregatedCount, err := interfaceAggregatedCountSearchMax(v, "ae-1", v, m, jnprSess) + if v := d.Get("name").(string); strings.HasPrefix(v, "ae") && junSess != nil { + aggregatedCount, err := interfaceAggregatedCountSearchMax(v, "ae-1", v, clt, junSess) if err != nil { return err } @@ -1020,9 +1028,12 @@ func setInterfacePhysical(d *schema.ResourceData, m interface{}, jnprSess *Netco } } } - if newAE != "" && jnprSess != nil { - aggregatedCount, err := interfaceAggregatedCountSearchMax(newAE, oldAE, - d.Get("name").(string), m, jnprSess) + if newAE != "" && junSess != nil { + aggregatedCount, err := interfaceAggregatedCountSearchMax( + newAE, + oldAE, + d.Get("name").(string), + clt, junSess) if err != nil { return err } @@ -1034,7 +1045,10 @@ func setInterfacePhysical(d *schema.ResourceData, m interface{}, jnprSess *Netco return fmt.Errorf("parent_ether_opts block is empty") } if err := setInterfacePhysicalParentEtherOpts( - v.(map[string]interface{}), d.Get("name").(string), m, jnprSess); err != nil { + v.(map[string]interface{}), + d.Get("name").(string), + clt, junSess, + ); err != nil { return err } } @@ -1052,12 +1066,11 @@ func setInterfacePhysical(d *schema.ResourceData, m interface{}, jnprSess *Netco configSet = append(configSet, setPrefix+"vlan-tagging") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func setInterfacePhysicalEsi(setPrefix string, esiParams []interface{}, m interface{}, jnprSess *NetconfObject, +func setInterfacePhysicalEsi(setPrefix string, esiParams []interface{}, clt *Client, junSess *junosSession, ) error { - sess := m.(*Session) configSet := make([]string, 0) for _, v := range esiParams { @@ -1079,13 +1092,12 @@ func setInterfacePhysicalEsi(setPrefix string, esiParams []interface{}, m interf } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func setInterfacePhysicalParentEtherOpts( - ethOpts map[string]interface{}, interfaceName string, m interface{}, jnprSess *NetconfObject, + ethOpts map[string]interface{}, interfaceName string, clt *Client, junSess *junosSession, ) error { - sess := m.(*Session) configSet := make([]string, 0) setPrefix := "set interfaces " + interfaceName + " " switch { @@ -1197,14 +1209,13 @@ func setInterfacePhysicalParentEtherOpts( configSet = append(configSet, setPrefix+"source-filtering") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readInterfacePhysical(interFace string, m interface{}, jnprSess *NetconfObject) (interfacePhysicalOptions, error) { - sess := m.(*Session) +func readInterfacePhysical(interFace string, clt *Client, junSess *junosSession) (interfacePhysicalOptions, error) { var confRead interfacePhysicalOptions - showConfig, err := sess.command(cmdShowConfig+"interfaces "+interFace+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"interfaces "+interFace+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -1255,6 +1266,8 @@ func readInterfacePhysical(interFace string, m interface{}, jnprSess *NetconfObj } case strings.HasPrefix(itemTrim, "description "): confRead.description = strings.Trim(strings.TrimPrefix(itemTrim, "description "), "\"") + case itemTrim == "disable": + confRead.disable = true case strings.HasPrefix(itemTrim, "esi "): if err := readInterfacePhysicalEsi(&confRead, itemTrim); err != nil { return confRead, err @@ -1566,33 +1579,32 @@ func readInterfacePhysicalParentEtherOpts(confRead *interfacePhysicalOptions, it return nil } -func delInterfacePhysical(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) - if jnprSess != nil { - if containsUnit, err := checkInterfacePhysicalContainsUnit(d.Get("name").(string), m, jnprSess); err != nil { +func delInterfacePhysical(d *schema.ResourceData, clt *Client, junSess *junosSession) error { + if junSess != nil { + if containsUnit, err := checkInterfacePhysicalContainsUnit(d.Get("name").(string), clt, junSess); err != nil { return err } else if containsUnit { return fmt.Errorf("interface %s is used for a logical unit interface", d.Get("name").(string)) } } - if err := sess.configSet([]string{"delete interfaces " + d.Get("name").(string)}, jnprSess); err != nil { + if err := clt.configSet([]string{"delete interfaces " + d.Get("name").(string)}, junSess); err != nil { return err } - if jnprSess == nil { + if junSess == nil { return nil } if v := d.Get("name").(string); strings.HasPrefix(v, "ae") { - aggregatedCount, err := interfaceAggregatedCountSearchMax("ae-1", v, v, m, jnprSess) + aggregatedCount, err := interfaceAggregatedCountSearchMax("ae-1", v, v, clt, junSess) if err != nil { return err } if aggregatedCount == "0" { - err = sess.configSet([]string{"delete chassis aggregated-devices ethernet device-count"}, jnprSess) + err = clt.configSet([]string{"delete chassis aggregated-devices ethernet device-count"}, junSess) if err != nil { return err } } else { - err = sess.configSet([]string{"set chassis aggregated-devices ethernet device-count " + aggregatedCount}, jnprSess) + err = clt.configSet([]string{"set chassis aggregated-devices ethernet device-count " + aggregatedCount}, junSess) if err != nil { return err } @@ -1612,23 +1624,26 @@ func delInterfacePhysical(d *schema.ResourceData, m interface{}, jnprSess *Netco aeDel = v["ae_8023ad"].(string) } if aeDel != "" { - lastAEchild, err := interfaceAggregatedLastChild(aeDel, d.Get("name").(string), m, jnprSess) + lastAEchild, err := interfaceAggregatedLastChild(aeDel, d.Get("name").(string), clt, junSess) if err != nil { return err } if lastAEchild { - aggregatedCount, err := interfaceAggregatedCountSearchMax("ae-1", aeDel, - d.Get("name").(string), m, jnprSess) + aggregatedCount, err := interfaceAggregatedCountSearchMax( + "ae-1", + aeDel, + d.Get("name").(string), + clt, junSess) if err != nil { return err } if aggregatedCount == "0" { - err = sess.configSet([]string{"delete chassis aggregated-devices ethernet device-count"}, jnprSess) + err = clt.configSet([]string{"delete chassis aggregated-devices ethernet device-count"}, junSess) if err != nil { return err } } else { - err = sess.configSet([]string{"set chassis aggregated-devices ethernet device-count " + aggregatedCount}, jnprSess) + err = clt.configSet([]string{"set chassis aggregated-devices ethernet device-count " + aggregatedCount}, junSess) if err != nil { return err } @@ -1640,9 +1655,8 @@ func delInterfacePhysical(d *schema.ResourceData, m interface{}, jnprSess *Netco return nil } -func checkInterfacePhysicalContainsUnit(interFace string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"interfaces "+interFace+pipeDisplaySetRelative, jnprSess) +func checkInterfacePhysicalContainsUnit(interFace string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"interfaces "+interFace+pipeDisplaySetRelative, junSess) if err != nil { return false, err } @@ -1665,26 +1679,25 @@ func checkInterfacePhysicalContainsUnit(interFace string, m interface{}, jnprSes return false, nil } -func delInterfaceNC(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delInterfaceNC(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) delPrefix := "delete interfaces " + d.Get("name").(string) + " " - if sess.junosGroupIntDel != "" { - configSet = append(configSet, delPrefix+"apply-groups "+sess.junosGroupIntDel) + if clt.groupIntDel != "" { + configSet = append(configSet, delPrefix+"apply-groups "+clt.groupIntDel) } configSet = append(configSet, delPrefix+"description") configSet = append(configSet, delPrefix+"disable") - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func delInterfacePhysicalOpts(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delInterfacePhysicalOpts(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) delPrefix := "delete interfaces " + d.Get("name").(string) + " " configSet = append(configSet, delPrefix+"aggregated-ether-options", delPrefix+"description", + delPrefix+"disable", delPrefix+"esi", delPrefix+"ether-options", delPrefix+"gigether-options", @@ -1695,7 +1708,7 @@ func delInterfacePhysicalOpts(d *schema.ResourceData, m interface{}, jnprSess *N delPrefix+"vlan-tagging", ) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillInterfacePhysicalData(d *schema.ResourceData, interfaceOpt interfacePhysicalOptions) { @@ -1723,6 +1736,9 @@ func fillInterfacePhysicalData(d *schema.ResourceData, interfaceOpt interfacePhy if tfErr := d.Set("description", interfaceOpt.description); tfErr != nil { panic(tfErr) } + if tfErr := d.Set("disable", interfaceOpt.disable); tfErr != nil { + panic(tfErr) + } if _, ok := d.GetOk("ether802_3ad"); ok { if tfErr := d.Set("ether802_3ad", interfaceOpt.v8023ad); tfErr != nil { panic(tfErr) @@ -1754,9 +1770,8 @@ func fillInterfacePhysicalData(d *schema.ResourceData, interfaceOpt interfacePhy } } -func interfaceAggregatedLastChild(ae, interFace string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"interfaces"+pipeDisplaySetRelative, jnprSess) +func interfaceAggregatedLastChild(ae, interFace string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"interfaces"+pipeDisplaySetRelative, junSess) if err != nil { return false, err } @@ -1771,15 +1786,14 @@ func interfaceAggregatedLastChild(ae, interFace string, m interface{}, jnprSess return lastAE, nil } -func interfaceAggregatedCountSearchMax(newAE, oldAE, interFace string, m interface{}, jnprSess *NetconfObject, +func interfaceAggregatedCountSearchMax(newAE, oldAE, interFace string, clt *Client, junSess *junosSession, ) (string, error) { - sess := m.(*Session) newAENum := strings.TrimPrefix(newAE, "ae") newAENumInt, err := strconv.Atoi(newAENum) if err != nil { return "", fmt.Errorf("failed to convert ae interaface '%v' to integer: %w", newAE, err) } - showConfig, err := sess.command(cmdShowConfig+"interfaces"+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"interfaces"+pipeDisplaySetRelative, junSess) if err != nil { return "", err } @@ -1808,7 +1822,7 @@ func interfaceAggregatedCountSearchMax(newAE, oldAE, interFace string, m interfa } } } - lastOldAE, err := interfaceAggregatedLastChild(oldAE, interFace, m, jnprSess) + lastOldAE, err := interfaceAggregatedLastChild(oldAE, interFace, clt, junSess) if err != nil { return "", err } diff --git a/junos/resource_interface_physical_disable.go b/junos/resource_interface_physical_disable.go index 36da11f7..7e1ccb02 100644 --- a/junos/resource_interface_physical_disable.go +++ b/junos/resource_interface_physical_disable.go @@ -35,68 +35,68 @@ func resourceInterfacePhysicalDisable() *schema.Resource { func resourceInterfacePhysicalDisableCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := addInterfacePhysicalNC(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := addInterfacePhysicalNC(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - ncInt, emptyInt, err := checkInterfacePhysicalNCEmpty(d.Get("name").(string), m, jnprSess) + ncInt, emptyInt, err := checkInterfacePhysicalNCEmpty(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !ncInt && !emptyInt { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("interface %s is configured", d.Get("name").(string)))...) } if ncInt { d.SetId(d.Get("name").(string)) - if errs := sess.configClear(jnprSess); len(errs) > 0 { + if errs := clt.configClear(junSess); len(errs) > 0 { return diagWarns } return nil } if emptyInt { - if containsUnit, err := checkInterfacePhysicalContainsUnit(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if containsUnit, err := checkInterfacePhysicalContainsUnit(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } else if containsUnit { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr( fmt.Errorf("interface %s is used for a logical unit interface", d.Get("name").(string)))...) } } - if err := addInterfacePhysicalNC(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := addInterfacePhysicalNC(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_interface_physical_disable", jnprSess) + warns, err := clt.commitConf("create resource junos_interface_physical_disable", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - ncInt, _, err = checkInterfacePhysicalNCEmpty(d.Get("name").(string), m, jnprSess) + ncInt, _, err = checkInterfacePhysicalNCEmpty(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -110,14 +110,14 @@ func resourceInterfacePhysicalDisableCreate(ctx context.Context, d *schema.Resou } func resourceInterfacePhysicalDisableRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) mutex.Lock() - ncInt, _, err := checkInterfacePhysicalNCEmpty(d.Get("name").(string), m, jnprSess) + ncInt, _, err := checkInterfacePhysicalNCEmpty(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) diff --git a/junos/resource_interface_physical_test.go b/junos/resource_interface_physical_test.go index b375e668..d0d5ac90 100644 --- a/junos/resource_interface_physical_test.go +++ b/junos/resource_interface_physical_test.go @@ -213,6 +213,7 @@ func testAccJunosInterfacePhysicalConfigCreate(interFace, interfaceAE, interFace resource "junos_interface_physical" "testacc_interface" { name = "%s" description = "testacc_interface" + disable = true gigether_opts { ae_8023ad = "%s" } diff --git a/junos/resource_interface_st0_unit.go b/junos/resource_interface_st0_unit.go index 95cdb7f3..a9dd5205 100644 --- a/junos/resource_interface_st0_unit.go +++ b/junos/resource_interface_st0_unit.go @@ -23,35 +23,35 @@ func resourceInterfaceSt0Unit() *schema.Resource { } func resourceInterfaceSt0UnitCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - newSt0, err := searchInterfaceSt0UnitToCreate(m, jnprSess) + newSt0, err := searchInterfaceSt0UnitToCreate(clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("error to find new st0 unit interface: %w", err))...) } - if err := sess.configSet([]string{"set interfaces " + newSt0}, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := clt.configSet([]string{"set interfaces " + newSt0}, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_interface_st0_unit", jnprSess) + warns, err := clt.commitConf("create resource junos_interface_st0_unit", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - ncInt, emptyInt, setInt, err := checkInterfaceLogicalNCEmpty(newSt0, m, jnprSess) + ncInt, emptyInt, setInt, err := checkInterfaceLogicalNCEmpty(newSt0, clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -69,14 +69,14 @@ func resourceInterfaceSt0UnitCreate(ctx context.Context, d *schema.ResourceData, } func resourceInterfaceSt0UnitRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) mutex.Lock() - ncInt, emptyInt, setInt, err := checkInterfaceLogicalNCEmpty(d.Id(), m, jnprSess) + ncInt, emptyInt, setInt, err := checkInterfaceLogicalNCEmpty(d.Id(), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -89,43 +89,43 @@ func resourceInterfaceSt0UnitRead(ctx context.Context, d *schema.ResourceData, m } func resourceInterfaceSt0UnitDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := sess.configSet([]string{"delete interfaces " + d.Id()}, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := clt.configSet([]string{"delete interfaces " + d.Id()}, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - ncInt, emptyInt, _, err := checkInterfaceLogicalNCEmpty(d.Id(), m, jnprSess) + ncInt, emptyInt, _, err := checkInterfaceLogicalNCEmpty(d.Id(), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !ncInt && !emptyInt { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("interface %s not empty or disable", d.Id()))...) } - if err := sess.configSet([]string{"delete interfaces " + d.Id()}, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := clt.configSet([]string{"delete interfaces " + d.Id()}, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_interface_st0_unit", jnprSess) + warns, err := clt.commitConf("delete resource junos_interface_st0_unit", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -135,17 +135,17 @@ func resourceInterfaceSt0UnitDelete(ctx context.Context, d *schema.ResourceData, func resourceInterfaceSt0UnitImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) + clt := m.(*Client) if !strings.HasPrefix(d.Id(), "st0.") { return nil, fmt.Errorf("id must be start with 'st0.'") } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - ncInt, emptyInt, setInt, err := checkInterfaceLogicalNCEmpty(d.Id(), m, jnprSess) + ncInt, emptyInt, setInt, err := checkInterfaceLogicalNCEmpty(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -161,9 +161,8 @@ func resourceInterfaceSt0UnitImport(ctx context.Context, d *schema.ResourceData, return result, nil } -func searchInterfaceSt0UnitToCreate(m interface{}, jnprSess *NetconfObject) (string, error) { - sess := m.(*Session) - st0, err := sess.command("show interfaces st0 terse", jnprSess) +func searchInterfaceSt0UnitToCreate(clt *Client, junSess *junosSession) (string, error) { + st0, err := clt.command("show interfaces st0 terse", junSess) if err != nil { return "", err } diff --git a/junos/resource_layer2_control.go b/junos/resource_layer2_control.go index 76950dd8..b89b508c 100644 --- a/junos/resource_layer2_control.go +++ b/junos/resource_layer2_control.go @@ -113,56 +113,56 @@ func resourceLayer2Control() *schema.Resource { } func resourceLayer2ControlCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setLayer2Control(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setLayer2Control(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId("layer2_control") return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := setLayer2Control(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setLayer2Control(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_layer2_control", jnprSess) + warns, err := clt.commitConf("create resource junos_layer2_control", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.SetId("layer2_control") - return append(diagWarns, resourceLayer2ControlReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceLayer2ControlReadWJunSess(d, clt, junSess)...) } func resourceLayer2ControlRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceLayer2ControlReadWJnprSess(d, m, jnprSess) + return resourceLayer2ControlReadWJunSess(d, clt, junSess) } -func resourceLayer2ControlReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceLayer2ControlReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - layer2ControlOptions, err := readLayer2Control(m, jnprSess) + layer2ControlOptions, err := readLayer2Control(clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -174,76 +174,76 @@ func resourceLayer2ControlReadWJnprSess(d *schema.ResourceData, m interface{}, j func resourceLayer2ControlUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delLayer2Control(m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delLayer2Control(clt, nil); err != nil { return diag.FromErr(err) } - if err := setLayer2Control(d, m, nil); err != nil { + if err := setLayer2Control(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delLayer2Control(m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delLayer2Control(clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setLayer2Control(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setLayer2Control(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_layer2_control", jnprSess) + warns, err := clt.commitConf("update resource junos_layer2_control", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceLayer2ControlReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceLayer2ControlReadWJunSess(d, clt, junSess)...) } func resourceLayer2ControlDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delLayer2Control(m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delLayer2Control(clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delLayer2Control(m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delLayer2Control(clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_layer2_control", jnprSess) + warns, err := clt.commitConf("delete resource junos_layer2_control", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -253,14 +253,14 @@ func resourceLayer2ControlDelete(ctx context.Context, d *schema.ResourceData, m func resourceLayer2ControlImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - layer2ControlOptions, err := readLayer2Control(m, jnprSess) + layer2ControlOptions, err := readLayer2Control(clt, junSess) if err != nil { return nil, err } @@ -271,8 +271,7 @@ func resourceLayer2ControlImport(ctx context.Context, d *schema.ResourceData, m return result, nil } -func setLayer2Control(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setLayer2Control(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set protocols layer2-control " @@ -321,15 +320,14 @@ func setLayer2Control(d *schema.ResourceData, m interface{}, jnprSess *NetconfOb configSet = append(configSet, setPrefix+"nonstop-bridging") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readLayer2Control(m interface{}, jnprSess *NetconfObject) (layer2ControlOptions, error) { - sess := m.(*Session) +func readLayer2Control(clt *Client, junSess *junosSession) (layer2ControlOptions, error) { var confRead layer2ControlOptions - showConfig, err := sess.command(cmdShowConfig+ - "protocols layer2-control"+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "protocols layer2-control"+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -407,11 +405,10 @@ func readLayer2Control(m interface{}, jnprSess *NetconfObject) (layer2ControlOpt return confRead, nil } -func delLayer2Control(m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delLayer2Control(clt *Client, junSess *junosSession) error { configSet := []string{"delete protocols layer2-control"} - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillLayer2ControlData(d *schema.ResourceData, layer2ControlOptions layer2ControlOptions) { diff --git a/junos/resource_lldp_interface.go b/junos/resource_lldp_interface.go index 51652164..682d9c05 100644 --- a/junos/resource_lldp_interface.go +++ b/junos/resource_lldp_interface.go @@ -86,50 +86,50 @@ func resourceLldpInterface() *schema.Resource { } func resourceLldpInterfaceCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setLldpInterface(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setLldpInterface(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - lldpInterfaceExists, err := checkLldpInterfaceExists(d.Get("name").(string), m, jnprSess) + lldpInterfaceExists, err := checkLldpInterfaceExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if lldpInterfaceExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf( "protocols lldp interface %v already exists", d.Get("name").(string)))...) } - if err := setLldpInterface(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setLldpInterface(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_lldp_interface", jnprSess) + warns, err := clt.commitConf("create resource junos_lldp_interface", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - lldpInterfaceExists, err = checkLldpInterfaceExists(d.Get("name").(string), m, jnprSess) + lldpInterfaceExists, err = checkLldpInterfaceExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -140,24 +140,24 @@ func resourceLldpInterfaceCreate(ctx context.Context, d *schema.ResourceData, m "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceLldpInterfaceReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceLldpInterfaceReadWJunSess(d, clt, junSess)...) } func resourceLldpInterfaceRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceLldpInterfaceReadWJnprSess(d, m, jnprSess) + return resourceLldpInterfaceReadWJunSess(d, clt, junSess) } -func resourceLldpInterfaceReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceLldpInterfaceReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - lldpInterfaceOptions, err := readLldpInterface(d.Get("name").(string), m, jnprSess) + lldpInterfaceOptions, err := readLldpInterface(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -173,76 +173,76 @@ func resourceLldpInterfaceReadWJnprSess(d *schema.ResourceData, m interface{}, j func resourceLldpInterfaceUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delLldpInterface(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delLldpInterface(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setLldpInterface(d, m, nil); err != nil { + if err := setLldpInterface(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delLldpInterface(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delLldpInterface(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setLldpInterface(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setLldpInterface(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_lldp_interface", jnprSess) + warns, err := clt.commitConf("update resource junos_lldp_interface", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceLldpInterfaceReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceLldpInterfaceReadWJunSess(d, clt, junSess)...) } func resourceLldpInterfaceDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delLldpInterface(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delLldpInterface(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delLldpInterface(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delLldpInterface(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_lldp_interface", jnprSess) + warns, err := clt.commitConf("delete resource junos_lldp_interface", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -252,22 +252,22 @@ func resourceLldpInterfaceDelete(ctx context.Context, d *schema.ResourceData, m func resourceLldpInterfaceImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - lldpInterfaceExists, err := checkLldpInterfaceExists(d.Id(), m, jnprSess) + lldpInterfaceExists, err := checkLldpInterfaceExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !lldpInterfaceExists { return nil, fmt.Errorf("don't find protocols lldp interface with id '%v' (id must be )", d.Id()) } - lldpInterfaceOptions, err := readLldpInterface(d.Id(), m, jnprSess) + lldpInterfaceOptions, err := readLldpInterface(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -278,10 +278,9 @@ func resourceLldpInterfaceImport(ctx context.Context, d *schema.ResourceData, m return result, nil } -func checkLldpInterfaceExists(name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command( - cmdShowConfig+"protocols lldp interface "+name+pipeDisplaySet, jnprSess) +func checkLldpInterfaceExists(name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command( + cmdShowConfig+"protocols lldp interface "+name+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -292,9 +291,7 @@ func checkLldpInterfaceExists(name string, m interface{}, jnprSess *NetconfObjec return true, nil } -func setLldpInterface(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) - +func setLldpInterface(d *schema.ResourceData, clt *Client, junSess *junosSession) error { setPrefix := "set protocols lldp interface " + d.Get("name").(string) + " " configSet := make([]string, 0) @@ -324,16 +321,15 @@ func setLldpInterface(d *schema.ResourceData, m interface{}, jnprSess *NetconfOb configSet = append(configSet, setPrefix+"trap-notification enable") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readLldpInterface(name string, m interface{}, jnprSess *NetconfObject, +func readLldpInterface(name string, clt *Client, junSess *junosSession, ) (lldpInterfaceOptions, error) { - sess := m.(*Session) var confRead lldpInterfaceOptions - showConfig, err := sess.command( - cmdShowConfig+"protocols lldp interface "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command( + cmdShowConfig+"protocols lldp interface "+name+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -376,12 +372,10 @@ func readLldpInterface(name string, m interface{}, jnprSess *NetconfObject, return confRead, nil } -func delLldpInterface(name string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) - +func delLldpInterface(name string, clt *Client, junSess *junosSession) error { configSet := []string{"delete protocols lldp interface " + name} - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillLldpInterfaceData(d *schema.ResourceData, lldpInterfaceOptions lldpInterfaceOptions) { diff --git a/junos/resource_lldpmed_interface.go b/junos/resource_lldpmed_interface.go index 2a240186..13042cce 100644 --- a/junos/resource_lldpmed_interface.go +++ b/junos/resource_lldpmed_interface.go @@ -131,50 +131,50 @@ func resourceLldpMedInterface() *schema.Resource { } func resourceLldpMedInterfaceCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setLldpMedInterface(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setLldpMedInterface(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - lldpMedInterfaceExists, err := checkLldpMedInterfaceExists(d.Get("name").(string), m, jnprSess) + lldpMedInterfaceExists, err := checkLldpMedInterfaceExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if lldpMedInterfaceExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf( "protocols lldp-med interface %v already exists", d.Get("name").(string)))...) } - if err := setLldpMedInterface(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setLldpMedInterface(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_lldpmed_interface", jnprSess) + warns, err := clt.commitConf("create resource junos_lldpmed_interface", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - lldpMedInterfaceExists, err = checkLldpMedInterfaceExists(d.Get("name").(string), m, jnprSess) + lldpMedInterfaceExists, err = checkLldpMedInterfaceExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -185,24 +185,24 @@ func resourceLldpMedInterfaceCreate(ctx context.Context, d *schema.ResourceData, "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceLldpMedInterfaceReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceLldpMedInterfaceReadWJunSess(d, clt, junSess)...) } func resourceLldpMedInterfaceRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceLldpMedInterfaceReadWJnprSess(d, m, jnprSess) + return resourceLldpMedInterfaceReadWJunSess(d, clt, junSess) } -func resourceLldpMedInterfaceReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceLldpMedInterfaceReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - lldpMedInterfaceOptions, err := readLldpMedInterface(d.Get("name").(string), m, jnprSess) + lldpMedInterfaceOptions, err := readLldpMedInterface(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -218,76 +218,76 @@ func resourceLldpMedInterfaceReadWJnprSess(d *schema.ResourceData, m interface{} func resourceLldpMedInterfaceUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delLldpMedInterface(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delLldpMedInterface(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setLldpMedInterface(d, m, nil); err != nil { + if err := setLldpMedInterface(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delLldpMedInterface(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delLldpMedInterface(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setLldpMedInterface(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setLldpMedInterface(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_lldpmed_interface", jnprSess) + warns, err := clt.commitConf("update resource junos_lldpmed_interface", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceLldpMedInterfaceReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceLldpMedInterfaceReadWJunSess(d, clt, junSess)...) } func resourceLldpMedInterfaceDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delLldpMedInterface(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delLldpMedInterface(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delLldpMedInterface(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delLldpMedInterface(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_lldpmed_interface", jnprSess) + warns, err := clt.commitConf("delete resource junos_lldpmed_interface", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -297,22 +297,22 @@ func resourceLldpMedInterfaceDelete(ctx context.Context, d *schema.ResourceData, func resourceLldpMedInterfaceImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - lldpMedInterfaceExists, err := checkLldpMedInterfaceExists(d.Id(), m, jnprSess) + lldpMedInterfaceExists, err := checkLldpMedInterfaceExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !lldpMedInterfaceExists { return nil, fmt.Errorf("don't find protocols lldp-med interface with id '%v' (id must be )", d.Id()) } - lldpMedInterfaceOptions, err := readLldpMedInterface(d.Id(), m, jnprSess) + lldpMedInterfaceOptions, err := readLldpMedInterface(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -323,10 +323,9 @@ func resourceLldpMedInterfaceImport(ctx context.Context, d *schema.ResourceData, return result, nil } -func checkLldpMedInterfaceExists(name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command( - cmdShowConfig+"protocols lldp-med interface "+name+pipeDisplaySet, jnprSess) +func checkLldpMedInterfaceExists(name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command( + cmdShowConfig+"protocols lldp-med interface "+name+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -337,9 +336,7 @@ func checkLldpMedInterfaceExists(name string, m interface{}, jnprSess *NetconfOb return true, nil } -func setLldpMedInterface(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) - +func setLldpMedInterface(d *schema.ResourceData, clt *Client, junSess *junosSession) error { setPrefix := "set protocols lldp-med interface " + d.Get("name").(string) + " " configSet := make([]string, 0) @@ -388,16 +385,15 @@ func setLldpMedInterface(d *schema.ResourceData, m interface{}, jnprSess *Netcon } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readLldpMedInterface(name string, m interface{}, jnprSess *NetconfObject, +func readLldpMedInterface(name string, clt *Client, junSess *junosSession, ) (lldpMedInterfaceOptions, error) { - sess := m.(*Session) var confRead lldpMedInterfaceOptions - showConfig, err := sess.command( - cmdShowConfig+"protocols lldp-med interface "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command( + cmdShowConfig+"protocols lldp-med interface "+name+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -498,12 +494,10 @@ func readLldpMedInterface(name string, m interface{}, jnprSess *NetconfObject, return confRead, nil } -func delLldpMedInterface(name string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) - +func delLldpMedInterface(name string, clt *Client, junSess *junosSession) error { configSet := []string{"delete protocols lldp-med interface " + name} - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillLldpMedInterfaceData(d *schema.ResourceData, lldpMedInterfaceOptions lldpMedInterfaceOptions) { diff --git a/junos/resource_null_commit_file.go b/junos/resource_null_commit_file.go index 4742cba3..3ec4c21d 100644 --- a/junos/resource_null_commit_file.go +++ b/junos/resource_null_commit_file.go @@ -44,41 +44,41 @@ func resourceNullCommitFile() *schema.Resource { } func resourceNullCommitFileCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics fileName := d.Get("filename").(string) configSet, err := readNullCommitFile(fileName) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } for _, v := range d.Get("append_lines").([]interface{}) { configSet = append(configSet, v.(string)) } - if err := sess.configSet(configSet, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := clt.configSet(configSet, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("commit a file with resource junos_null_commit_file", jnprSess) + warns, err := clt.commitConf("commit a file with resource junos_null_commit_file", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.SetId(fileName) if d.Get("clear_file_after_commit").(bool) { - if err := cleanNullCommitFile(fileName, sess); err != nil { + if err := cleanNullCommitFile(fileName, clt); err != nil { return append(diagWarns, diag.FromErr(err)...) } } @@ -111,11 +111,11 @@ func readNullCommitFile(filename string) ([]string, error) { return strings.Split(string(fileReadByte), "\n"), nil } -func cleanNullCommitFile(filename string, sess *Session) error { +func cleanNullCommitFile(filename string, clt *Client) error { if err := replaceTildeToHomeDir(&filename); err != nil { return err } - f, err := os.OpenFile(filename, os.O_TRUNC, os.FileMode(sess.junosFilePermission)) + f, err := os.OpenFile(filename, os.O_TRUNC, os.FileMode(clt.filePermission)) if err != nil { return fmt.Errorf("could not open file '%s' to truncate after commit: %w", filename, err) } diff --git a/junos/resource_ospf.go b/junos/resource_ospf.go index 8aaada7f..57379701 100644 --- a/junos/resource_ospf.go +++ b/junos/resource_ospf.go @@ -282,70 +282,70 @@ func resourceOspf() *schema.Resource { } func resourceOspfCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setOspf(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setOspf(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("version").(string) + idSeparator + d.Get("routing_instance").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if d.Get("routing_instance").(string) != defaultW { - instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !instanceExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("routing instance %v doesn't exist", d.Get("routing_instance").(string)))...) } } - if err := setOspf(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setOspf(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_ospf", jnprSess) + warns, err := clt.commitConf("create resource junos_ospf", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.SetId(d.Get("version").(string) + idSeparator + d.Get("routing_instance").(string)) - return append(diagWarns, resourceOspfReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceOspfReadWJunSess(d, clt, junSess)...) } func resourceOspfRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceOspfReadWJnprSess(d, m, jnprSess) + return resourceOspfReadWJunSess(d, clt, junSess) } -func resourceOspfReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) diag.Diagnostics { +func resourceOspfReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession) diag.Diagnostics { mutex.Lock() if d.Get("routing_instance").(string) != defaultW { - instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), clt, junSess) if err != nil { mutex.Unlock() @@ -358,7 +358,7 @@ func resourceOspfReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess * return nil } } - ospfOptions, err := readOspf(d.Get("version").(string), d.Get("routing_instance").(string), m, jnprSess) + ospfOptions, err := readOspf(d.Get("version").(string), d.Get("routing_instance").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -370,77 +370,77 @@ func resourceOspfReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess * func resourceOspfUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delOspf(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delOspf(d, clt, nil); err != nil { return diag.FromErr(err) } - if err := setOspf(d, m, nil); err != nil { + if err := setOspf(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delOspf(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delOspf(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setOspf(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setOspf(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_ospf", jnprSess) + warns, err := clt.commitConf("update resource junos_ospf", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceOspfReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceOspfReadWJunSess(d, clt, junSess)...) } func resourceOspfDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delOspf(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delOspf(d, clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delOspf(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delOspf(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_ospf", jnprSess) + warns, err := clt.commitConf("delete resource junos_ospf", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -450,12 +450,12 @@ func resourceOspfDelete(ctx context.Context, d *schema.ResourceData, m interface func resourceOspfImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) idSplit := strings.Split(d.Id(), idSeparator) if len(idSplit) < 2 { @@ -465,7 +465,7 @@ func resourceOspfImport(ctx context.Context, d *schema.ResourceData, m interface return nil, fmt.Errorf("%s is not a valid version", idSplit[0]) } if idSplit[1] != defaultW { - instanceExists, err := checkRoutingInstanceExists(idSplit[1], m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(idSplit[1], clt, junSess) if err != nil { return nil, err } @@ -473,7 +473,7 @@ func resourceOspfImport(ctx context.Context, d *schema.ResourceData, m interface return nil, fmt.Errorf("routing instance %v doesn't exist", idSplit[1]) } } - ospfOptions, err := readOspf(idSplit[0], idSplit[1], m, jnprSess) + ospfOptions, err := readOspf(idSplit[0], idSplit[1], clt, junSess) if err != nil { return nil, err } @@ -483,8 +483,7 @@ func resourceOspfImport(ctx context.Context, d *schema.ResourceData, m interface return result, nil } -func setOspf(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setOspf(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := setLS if d.Get("routing_instance").(string) != defaultW { @@ -631,12 +630,11 @@ func setOspf(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) err } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readOspf(version, routingInstance string, m interface{}, jnprSess *NetconfObject, +func readOspf(version, routingInstance string, clt *Client, junSess *junosSession, ) (ospfOptions, error) { - sess := m.(*Session) var confRead ospfOptions confRead.externalPreference = -1 confRead.labeledPreference = -1 @@ -650,15 +648,15 @@ func readOspf(version, routingInstance string, m interface{}, jnprSess *NetconfO } if routingInstance == defaultW { var err error - showConfig, err = sess.command(cmdShowConfig+ - "protocols "+ospfVersion+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "protocols "+ospfVersion+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } } else { var err error - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ - "protocols "+ospfVersion+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ + "protocols "+ospfVersion+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -872,8 +870,7 @@ func readOspf(version, routingInstance string, m interface{}, jnprSess *NetconfO return confRead, nil } -func delOspf(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delOspf(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) ospfVersion := ospfV2 @@ -912,7 +909,7 @@ func delOspf(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) err configSet = append(configSet, delPrefix+line) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillOspfData(d *schema.ResourceData, ospfOptions ospfOptions) { diff --git a/junos/resource_ospf_area.go b/junos/resource_ospf_area.go index f176dfa0..83d6106d 100644 --- a/junos/resource_ospf_area.go +++ b/junos/resource_ospf_area.go @@ -15,10 +15,21 @@ import ( ) type ospfAreaOptions struct { - areaID string - routingInstance string - version string - interFace []map[string]interface{} + noContextIdentifierAdvertisement bool + areaID string + routingInstance string + realm string + version string + contextIdentifier []string + interAreaPrefixExport []string + interAreaPrefixImport []string + networkSummaryExport []string + networkSummaryImport []string + areaRange []map[string]interface{} + interFace []map[string]interface{} + nssa []map[string]interface{} + stub []map[string]interface{} + virtualLink []map[string]interface{} } func resourceOspfArea() *schema.Resource { @@ -35,6 +46,8 @@ func resourceOspfArea() *schema.Resource { Type: schema.TypeString, ForceNew: true, Required: true, + ValidateFunc: validation.StringMatch( + regexp.MustCompile(`^(\d|\.)+$`), "should be usually in the IP format"), }, "routing_instance": { Type: schema.TypeString, @@ -43,6 +56,12 @@ func resourceOspfArea() *schema.Resource { Default: defaultW, ValidateDiagFunc: validateNameObjectJunos([]string{}, 64, formatDefault), }, + "realm": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{"ipv4-unicast", "ipv4-multicast", "ipv6-multicast"}, false), + }, "version": { Type: schema.TypeString, Optional: true, @@ -335,87 +354,363 @@ func resourceOspfArea() *schema.Resource { }, }, }, + "area_range": { + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "range": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.IsCIDRNetwork(0, 128), + }, + "exact": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + "override_metric": { + Type: schema.TypeInt, + Optional: true, + Default: 0, + ValidateFunc: validation.IntBetween(1, 16777215), + }, + "restrict": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + }, + }, + }, + "context_identifier": { + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateFunc: validation.StringMatch( + regexp.MustCompile(`^(\d{1,3}\.){3}\d{1,3}$`), "must be in the IP format"), + }, + ConflictsWith: []string{"no_context_identifier_advertisement"}, + }, + "inter_area_prefix_export": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateDiagFunc: validateNameObjectJunos([]string{}, 64, formatDefault), + }, + }, + "inter_area_prefix_import": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateDiagFunc: validateNameObjectJunos([]string{}, 64, formatDefault), + }, + }, + "network_summary_export": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateDiagFunc: validateNameObjectJunos([]string{}, 64, formatDefault), + }, + }, + "network_summary_import": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateDiagFunc: validateNameObjectJunos([]string{}, 64, formatDefault), + }, + }, + "no_context_identifier_advertisement": { + Type: schema.TypeBool, + Optional: true, + ConflictsWith: []string{"context_identifier"}, + }, + "nssa": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + ConflictsWith: []string{"stub"}, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "area_range": { + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "range": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.IsCIDRNetwork(0, 128), + }, + "exact": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + "override_metric": { + Type: schema.TypeInt, + Optional: true, + Default: 0, + ValidateFunc: validation.IntBetween(1, 16777215), + }, + "restrict": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + }, + }, + }, + "default_lsa": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "default_metric": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(1, 16777215), + }, + "metric_type": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(1, 2), + }, + "type_7": { + Type: schema.TypeBool, + Optional: true, + }, + }, + }, + }, + "no_summaries": { + Type: schema.TypeBool, + Optional: true, + ConflictsWith: []string{"nssa.0.summaries"}, + }, + "summaries": { + Type: schema.TypeBool, + Optional: true, + ConflictsWith: []string{"nssa.0.no_summaries"}, + }, + }, + }, + }, + "stub": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + ConflictsWith: []string{"nssa"}, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "default_metric": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(1, 16777215), + }, + "no_summaries": { + Type: schema.TypeBool, + Optional: true, + ConflictsWith: []string{"stub.0.summaries"}, + }, + "summaries": { + Type: schema.TypeBool, + Optional: true, + ConflictsWith: []string{"stub.0.no_summaries"}, + }, + }, + }, + }, + "virtual_link": { + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "neighbor_id": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.IsIPv4Address, + }, + "transit_area": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringMatch( + regexp.MustCompile(`^(\d{1,3}\.){3}\d{1,3}$`), "must be in the IP format"), + }, + "dead_interval": { + Type: schema.TypeInt, + Optional: true, + Default: 0, + ValidateFunc: validation.IntBetween(1, 65535), + }, + "demand_circuit": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + "disable": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + "flood_reduction": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + "hello_interval": { + Type: schema.TypeInt, + Optional: true, + Default: 0, + ValidateFunc: validation.IntBetween(1, 255), + }, + "ipsec_sa": { + Type: schema.TypeString, + Optional: true, + Default: "", + }, + "mtu": { + Type: schema.TypeInt, + Optional: true, + Default: 0, + ValidateFunc: validation.IntBetween(128, 65535), + }, + "retransmit_interval": { + Type: schema.TypeInt, + Optional: true, + Default: 0, + ValidateFunc: validation.IntBetween(1, 65535), + }, + "transit_delay": { + Type: schema.TypeInt, + Optional: true, + Default: 0, + ValidateFunc: validation.IntBetween(1, 65535), + }, + }, + }, + }, }, } } func resourceOspfAreaCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setOspfArea(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setOspfArea(d, clt, nil); err != nil { return diag.FromErr(err) } - d.SetId(d.Get("area_id").(string) + idSeparator + d.Get("version").(string) + - idSeparator + d.Get("routing_instance").(string)) + if realm := d.Get("realm").(string); realm != "" { + d.SetId(d.Get("area_id").(string) + idSeparator + d.Get("version").(string) + + idSeparator + realm + idSeparator + d.Get("routing_instance").(string)) + } else { + d.SetId(d.Get("area_id").(string) + idSeparator + d.Get("version").(string) + + idSeparator + d.Get("routing_instance").(string)) + } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - ospfAreaExists, err := checkOspfAreaExists(d.Get("area_id").(string), d.Get("version").(string), - d.Get("routing_instance").(string), m, jnprSess) + ospfAreaExists, err := checkOspfAreaExists( + d.Get("area_id").(string), + d.Get("version").(string), + d.Get("realm").(string), + d.Get("routing_instance").(string), + clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if ospfAreaExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) + + if realm := d.Get("realm").(string); realm != "" { + return append(diagWarns, diag.FromErr(fmt.Errorf("ospf %v realm %v area %v already exists in routing instance %v", + d.Get("version").(string), realm, d.Get("area_id").(string), d.Get("routing_instance").(string)))...) + } return append(diagWarns, diag.FromErr(fmt.Errorf("ospf %v area %v already exists in routing instance %v", d.Get("version").(string), d.Get("area_id").(string), d.Get("routing_instance").(string)))...) } - if err := setOspfArea(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setOspfArea(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_ospf_area", jnprSess) + warns, err := clt.commitConf("create resource junos_ospf_area", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - ospfAreaExists, err = checkOspfAreaExists(d.Get("area_id").(string), d.Get("version").(string), - d.Get("routing_instance").(string), m, jnprSess) + ospfAreaExists, err = checkOspfAreaExists( + d.Get("area_id").(string), + d.Get("version").(string), + d.Get("realm").(string), + d.Get("routing_instance").(string), + clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } if ospfAreaExists { - d.SetId(d.Get("area_id").(string) + idSeparator + d.Get("version").(string) + - idSeparator + d.Get("routing_instance").(string)) + if realm := d.Get("realm").(string); realm != "" { + d.SetId(d.Get("area_id").(string) + idSeparator + d.Get("version").(string) + + idSeparator + realm + idSeparator + d.Get("routing_instance").(string)) + } else { + d.SetId(d.Get("area_id").(string) + idSeparator + d.Get("version").(string) + + idSeparator + d.Get("routing_instance").(string)) + } } else { + if realm := d.Get("realm").(string); realm != "" { + return append(diagWarns, + diag.FromErr(fmt.Errorf("ospf %v realm %v area %v in routing instance %v "+ + "not exists after commit => check your config", + d.Get("version").(string), realm, d.Get("area_id").(string), d.Get("routing_instance").(string)))...) + } + return append(diagWarns, diag.FromErr(fmt.Errorf("ospf %v area %v in routing instance %v not exists after commit => check your config", d.Get("version").(string), d.Get("area_id").(string), d.Get("routing_instance").(string)))...) } - return append(diagWarns, resourceOspfAreaReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceOspfAreaReadWJunSess(d, clt, junSess)...) } func resourceOspfAreaRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceOspfAreaReadWJnprSess(d, m, jnprSess) + return resourceOspfAreaReadWJunSess(d, clt, junSess) } -func resourceOspfAreaReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) diag.Diagnostics { +func resourceOspfAreaReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession) diag.Diagnostics { mutex.Lock() - ospfAreaOptions, err := readOspfArea(d.Get("area_id").(string), d.Get("version").(string), - d.Get("routing_instance").(string), m, jnprSess) + ospfAreaOptions, err := readOspfArea( + d.Get("area_id").(string), + d.Get("version").(string), + d.Get("realm").(string), + d.Get("routing_instance").(string), + clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -431,76 +726,76 @@ func resourceOspfAreaReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSe func resourceOspfAreaUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delOspfArea(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delOspfArea(d, clt, nil); err != nil { return diag.FromErr(err) } - if err := setOspfArea(d, m, nil); err != nil { + if err := setOspfArea(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delOspfArea(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delOspfArea(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setOspfArea(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setOspfArea(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_ospf_area", jnprSess) + warns, err := clt.commitConf("update resource junos_ospf_area", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceOspfAreaReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceOspfAreaReadWJunSess(d, clt, junSess)...) } func resourceOspfAreaDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delOspfArea(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delOspfArea(d, clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delOspfArea(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delOspfArea(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_ospf_area", jnprSess) + warns, err := clt.commitConf("delete resource junos_ospf_area", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -510,53 +805,90 @@ func resourceOspfAreaDelete(ctx context.Context, d *schema.ResourceData, m inter func resourceOspfAreaImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) idSplit := strings.Split(d.Id(), idSeparator) if len(idSplit) < 3 { return nil, fmt.Errorf("missing element(s) in id with separator %v", idSeparator) } - ospfAreaExists, err := checkOspfAreaExists(idSplit[0], idSplit[1], idSplit[2], m, jnprSess) + if len(idSplit) == 3 { + ospfAreaExists, err := checkOspfAreaExists(idSplit[0], idSplit[1], "", idSplit[2], clt, junSess) + if err != nil { + return nil, err + } + if !ospfAreaExists { + return nil, fmt.Errorf("don't find ospf area with id '%v' (id must be "+ + ""+idSeparator+""+idSeparator+" or "+ + ""+idSeparator+""+idSeparator+""+idSeparator+")", d.Id()) + } + ospfAreaOptions, err := readOspfArea(idSplit[0], idSplit[1], "", idSplit[2], clt, junSess) + if err != nil { + return nil, err + } + fillOspfAreaData(d, ospfAreaOptions) + result[0] = d + + return result, nil + } + ospfAreaExists, err := checkOspfAreaExists(idSplit[0], idSplit[1], idSplit[2], idSplit[3], clt, junSess) if err != nil { return nil, err } if !ospfAreaExists { return nil, fmt.Errorf("don't find ospf area with id '%v' (id must be "+ - ""+idSeparator+""+idSeparator+")", d.Id()) + ""+idSeparator+""+idSeparator+" or "+ + ""+idSeparator+""+idSeparator+""+idSeparator+")", d.Id()) } - ospfAreaOptions, err := readOspfArea(idSplit[0], idSplit[1], idSplit[2], m, jnprSess) + ospfAreaOptions, err := readOspfArea(idSplit[0], idSplit[1], idSplit[2], idSplit[3], clt, junSess) if err != nil { return nil, err } fillOspfAreaData(d, ospfAreaOptions) + if ospfAreaOptions.realm == "" { + d.SetId(idSplit[0] + idSeparator + idSplit[1] + idSeparator + idSplit[3]) + } result[0] = d return result, nil } -func checkOspfAreaExists(idArea, version, routingInstance string, m interface{}, jnprSess *NetconfObject, +func checkOspfAreaExists(idArea, version, realm, routingInstance string, clt *Client, junSess *junosSession, ) (bool, error) { - sess := m.(*Session) var showConfig string var err error ospfVersion := ospfV2 if version == "v3" { ospfVersion = ospfV3 + } else if realm != "" { + return false, fmt.Errorf("realm can't set if version != v3") } - if routingInstance == defaultW { - showConfig, err = sess.command(cmdShowConfig+ - "protocols "+ospfVersion+" area "+idArea+pipeDisplaySet, jnprSess) + switch { + case routingInstance == defaultW && realm == "": + showConfig, err = clt.command(cmdShowConfig+ + "protocols "+ospfVersion+" area "+idArea+pipeDisplaySet, junSess) if err != nil { return false, err } - } else { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ - "protocols "+ospfVersion+" area "+idArea+pipeDisplaySet, jnprSess) + case routingInstance == defaultW && realm != "": + showConfig, err = clt.command(cmdShowConfig+ + "protocols "+ospfVersion+" realm "+realm+" area "+idArea+pipeDisplaySet, junSess) + if err != nil { + return false, err + } + case realm != "": + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ + "protocols "+ospfVersion+" realm "+realm+" area "+idArea+pipeDisplaySet, junSess) + if err != nil { + return false, err + } + default: + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ + "protocols "+ospfVersion+" area "+idArea+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -568,8 +900,7 @@ func checkOspfAreaExists(idArea, version, routingInstance string, m interface{}, return true, nil } -func setOspfArea(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setOspfArea(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := setLS if d.Get("routing_instance").(string) != defaultW { @@ -578,8 +909,14 @@ func setOspfArea(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) ospfVersion := ospfV2 if d.Get("version").(string) == "v3" { ospfVersion = ospfV3 + } else if d.Get("realm").(string) != "" { + return fmt.Errorf("realm can't set if version != v3") + } + setPrefix += "protocols " + ospfVersion + " " + if realm := d.Get("realm").(string); realm != "" { + setPrefix += "realm " + realm + " " } - setPrefix += "protocols " + ospfVersion + " area " + d.Get("area_id").(string) + " " + setPrefix += "area " + d.Get("area_id").(string) + " " interfaceNameList := make([]string, 0) for _, v := range d.Get("interface").([]interface{}) { @@ -589,233 +926,415 @@ func setOspfArea(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) } interfaceNameList = append(interfaceNameList, ospfInterface["name"].(string)) setPrefixInterface := setPrefix + "interface " + ospfInterface["name"].(string) + " " - configSet = append(configSet, setPrefixInterface) - if v := ospfInterface["authentication_simple_password"].(string); v != "" { - if len(ospfInterface["authentication_md5"].([]interface{})) != 0 { - return fmt.Errorf("conflict between 'authentication_simple_password' and 'authentication_md5'"+ - " in interface '%s'", ospfInterface["name"].(string)) - } - configSet = append(configSet, setPrefixInterface+"authentication simple-password \""+v+"\"") - } - authenticationMD5List := make([]int, 0) - for _, mAuthMD5 := range ospfInterface["authentication_md5"].([]interface{}) { - authMD5 := mAuthMD5.(map[string]interface{}) - if bchk.IntInSlice(authMD5["key_id"].(int), authenticationMD5List) { - return fmt.Errorf("multiple blocks authentication_md5 with the same key_id %d in interface with name %s", - authMD5["key_id"].(int), ospfInterface["name"].(string)) - } - authenticationMD5List = append(authenticationMD5List, authMD5["key_id"].(int)) - configSet = append(configSet, setPrefixInterface+"authentication md5 "+ - strconv.Itoa(authMD5["key_id"].(int))+" key \""+authMD5["key"].(string)+"\"") - if v := authMD5["start_time"].(string); v != "" { - configSet = append(configSet, setPrefixInterface+"authentication md5 "+ - strconv.Itoa(authMD5["key_id"].(int))+" start-time "+v) - } + configSetInterface, err := setOspfAreaInterface(setPrefixInterface, ospfInterface) + if err != nil { + return err } - bandwidthBasedMetricsList := make([]string, 0) - for _, mBandBaseMet := range ospfInterface["bandwidth_based_metrics"].(*schema.Set).List() { - bandwidthBaseMetrics := mBandBaseMet.(map[string]interface{}) - if bchk.StringInSlice(bandwidthBaseMetrics["bandwidth"].(string), bandwidthBasedMetricsList) { - return fmt.Errorf("multiple blocks bandwidth_based_metrics with the same bandwidth %s in interface with name %s", - bandwidthBaseMetrics["bandwidth"].(string), ospfInterface["name"].(string)) - } - bandwidthBasedMetricsList = append(bandwidthBasedMetricsList, bandwidthBaseMetrics["bandwidth"].(string)) - configSet = append(configSet, setPrefixInterface+"bandwidth-based-metrics bandwidth "+ - bandwidthBaseMetrics["bandwidth"].(string)+" metric "+strconv.Itoa(bandwidthBaseMetrics["metric"].(int))) + configSet = append(configSet, configSetInterface...) + } + areaRangeList := make([]string, 0) + for _, areaRangeBlock := range d.Get("area_range").(*schema.Set).List() { + areaRange := areaRangeBlock.(map[string]interface{}) + if bchk.StringInSlice(areaRange["range"].(string), areaRangeList) { + return fmt.Errorf("multiple blocks area_range with the same range %s", areaRange["range"].(string)) } - for _, mBFDLivDet := range ospfInterface["bfd_liveness_detection"].([]interface{}) { - if mBFDLivDet == nil { - return fmt.Errorf("bfd_liveness_detection block is empty in interface %s", ospfInterface["name"].(string)) - } - setPrefixBfd := setPrefixInterface + "bfd-liveness-detection " - bfdLiveDetect := mBFDLivDet.(map[string]interface{}) - if v := bfdLiveDetect["authentication_algorithm"].(string); v != "" { - configSet = append(configSet, setPrefixBfd+"authentication algorithm "+v) - } - if v := bfdLiveDetect["authentication_key_chain"].(string); v != "" { - configSet = append(configSet, setPrefixBfd+"authentication key-chain \""+v+"\"") - } - if bfdLiveDetect["authentication_loose_check"].(bool) { - configSet = append(configSet, setPrefixBfd+"authentication loose-check") - } - if v := bfdLiveDetect["detection_time_threshold"].(int); v != 0 { - configSet = append(configSet, setPrefixBfd+"detection-time threshold "+strconv.Itoa(v)) - } - if bfdLiveDetect["full_neighbors_only"].(bool) { - configSet = append(configSet, setPrefixBfd+"full-neighbors-only") - } - if v := bfdLiveDetect["holddown_interval"].(int); v != 0 { - configSet = append(configSet, setPrefixBfd+"holddown-interval "+strconv.Itoa(v)) - } - if v := bfdLiveDetect["minimum_interval"].(int); v != 0 { - configSet = append(configSet, setPrefixBfd+"minimum-interval "+strconv.Itoa(v)) - } - if v := bfdLiveDetect["minimum_receive_interval"].(int); v != 0 { - configSet = append(configSet, setPrefixBfd+"minimum-receive-interval "+strconv.Itoa(v)) + areaRangeList = append(areaRangeList, areaRange["range"].(string)) + configSet = append(configSet, setPrefix+"area-range "+areaRange["range"].(string)) + if areaRange["exact"].(bool) { + configSet = append(configSet, + setPrefix+"area-range "+areaRange["range"].(string)+" exact", + ) + } + if v := areaRange["override_metric"].(int); v != 0 { + configSet = append(configSet, + setPrefix+"area-range "+areaRange["range"].(string)+" override-metric "+strconv.Itoa(v), + ) + } + if areaRange["restrict"].(bool) { + configSet = append(configSet, + setPrefix+"area-range "+areaRange["range"].(string)+" restrict", + ) + } + } + for _, contextIdentifier := range sortSetOfString(d.Get("context_identifier").(*schema.Set).List()) { + configSet = append(configSet, setPrefix+"context-identifier "+contextIdentifier) + } + for _, interAreaPrefExport := range d.Get("inter_area_prefix_export").([]interface{}) { + configSet = append(configSet, setPrefix+"inter-area-prefix-export "+interAreaPrefExport.(string)) + } + for _, interAreaPrefImport := range d.Get("inter_area_prefix_import").([]interface{}) { + configSet = append(configSet, setPrefix+"inter-area-prefix-import "+interAreaPrefImport.(string)) + } + for _, networkSummaryExport := range d.Get("network_summary_export").([]interface{}) { + configSet = append(configSet, setPrefix+"network-summary-export "+networkSummaryExport.(string)) + } + for _, networkSummaryImport := range d.Get("network_summary_import").([]interface{}) { + configSet = append(configSet, setPrefix+"network-summary-import "+networkSummaryImport.(string)) + } + if d.Get("no_context_identifier_advertisement").(bool) { + configSet = append(configSet, setPrefix+"no-context-identifier-advertisement") + } + for _, nssaBlock := range d.Get("nssa").([]interface{}) { + setPrefixNssa := setPrefix + "nssa " + configSet = append(configSet, setPrefixNssa) + if nssaBlock != nil { + nssa := nssaBlock.(map[string]interface{}) + nssaAreaRangeList := make([]string, 0) + for _, areaRangeBlock := range nssa["area_range"].(*schema.Set).List() { + areaRange := areaRangeBlock.(map[string]interface{}) + if bchk.StringInSlice(areaRange["range"].(string), nssaAreaRangeList) { + return fmt.Errorf("multiple blocks area_range with the same range %s in nssa block", areaRange["range"].(string)) + } + nssaAreaRangeList = append(nssaAreaRangeList, areaRange["range"].(string)) + configSet = append(configSet, setPrefixNssa+"area-range "+areaRange["range"].(string)) + if areaRange["exact"].(bool) { + configSet = append(configSet, + setPrefixNssa+"area-range "+areaRange["range"].(string)+" exact", + ) + } + if v := areaRange["override_metric"].(int); v != 0 { + configSet = append(configSet, + setPrefixNssa+"area-range "+areaRange["range"].(string)+" override-metric "+strconv.Itoa(v), + ) + } + if areaRange["restrict"].(bool) { + configSet = append(configSet, + setPrefixNssa+"area-range "+areaRange["range"].(string)+" restrict", + ) + } } - if v := bfdLiveDetect["multiplier"].(int); v != 0 { - configSet = append(configSet, setPrefixBfd+"multiplier "+strconv.Itoa(v)) + for _, defaultLsaBlock := range nssa["default_lsa"].([]interface{}) { + configSet = append(configSet, setPrefixNssa+"default-lsa") + if defaultLsaBlock != nil { + defaultLsa := defaultLsaBlock.(map[string]interface{}) + if v := defaultLsa["default_metric"].(int); v != 0 { + configSet = append(configSet, setPrefixNssa+"default-lsa default-metric "+strconv.Itoa(v)) + } + if v := defaultLsa["metric_type"].(int); v != 0 { + configSet = append(configSet, setPrefixNssa+"default-lsa metric-type "+strconv.Itoa(v)) + } + if defaultLsa["type_7"].(bool) { + configSet = append(configSet, setPrefixNssa+"default-lsa type-7") + } + } } - if bfdLiveDetect["no_adaptation"].(bool) { - configSet = append(configSet, setPrefixBfd+"no-adaptation") + if nssa["no_summaries"].(bool) { + configSet = append(configSet, setPrefixNssa+"no-summaries") } - if v := bfdLiveDetect["transmit_interval_minimum_interval"].(int); v != 0 { - configSet = append(configSet, setPrefixBfd+ - "transmit-interval minimum-interval "+strconv.Itoa(v)) + if nssa["summaries"].(bool) { + configSet = append(configSet, setPrefixNssa+"summaries") } - if v := bfdLiveDetect["transmit_interval_threshold"].(int); v != 0 { - configSet = append(configSet, setPrefixBfd+ - "transmit-interval threshold "+strconv.Itoa(v)) + } + } + for _, stubBlock := range d.Get("stub").([]interface{}) { + configSet = append(configSet, setPrefix+"stub") + if stubBlock != nil { + stub := stubBlock.(map[string]interface{}) + if v := stub["default_metric"].(int); v != 0 { + configSet = append(configSet, setPrefix+"stub default-metric "+strconv.Itoa(v)) } - if v := bfdLiveDetect["version"].(string); v != "" { - configSet = append(configSet, setPrefixBfd+"version "+v) + if stub["no_summaries"].(bool) { + configSet = append(configSet, setPrefix+"stub no-summaries") } - if len(configSet) == 0 || !strings.HasPrefix(configSet[len(configSet)-1], setPrefixBfd) { - return fmt.Errorf("bfd_liveness_detection block is empty in interface %s", ospfInterface["name"].(string)) + if stub["summaries"].(bool) { + configSet = append(configSet, setPrefix+"stub summaries") } } - if v := ospfInterface["dead_interval"].(int); v != 0 { - configSet = append(configSet, setPrefixInterface+"dead-interval "+strconv.Itoa(v)) + } + virtualLinkList := make([]string, 0) + for _, virtualLinkBlock := range d.Get("virtual_link").(*schema.Set).List() { + virtualLink := virtualLinkBlock.(map[string]interface{}) + if bchk.StringInSlice( + virtualLink["neighbor_id"].(string)+idSeparator+virtualLink["transit_area"].(string), + virtualLinkList, + ) { + return fmt.Errorf( + "multiple blocks virtual_link with the same neighbor_id '%s' and transit_area '%s'", + virtualLink["neighbor_id"].(string), virtualLink["transit_area"].(string)) } - if ospfInterface["demand_circuit"].(bool) { - configSet = append(configSet, setPrefixInterface+"demand-circuit") + virtualLinkList = append( + virtualLinkList, virtualLink["neighbor_id"].(string)+idSeparator+virtualLink["transit_area"].(string), + ) + setPrefixVirtualLink := setPrefix + "virtual-link " + + "neighbor-id " + virtualLink["neighbor_id"].(string) + + " transit-area " + virtualLink["transit_area"].(string) + " " + configSet = append(configSet, setPrefixVirtualLink) + if v := virtualLink["dead_interval"].(int); v != 0 { + configSet = append(configSet, setPrefixVirtualLink+"dead-interval "+strconv.Itoa(v)) } - if ospfInterface["disable"].(bool) { - configSet = append(configSet, setPrefixInterface+"disable") + if virtualLink["demand_circuit"].(bool) { + configSet = append(configSet, setPrefixVirtualLink+"demand-circuit") } - if ospfInterface["dynamic_neighbors"].(bool) { - configSet = append(configSet, setPrefixInterface+"dynamic-neighbors") + if virtualLink["disable"].(bool) { + configSet = append(configSet, setPrefixVirtualLink+"disable") } - if ospfInterface["flood_reduction"].(bool) { - configSet = append(configSet, setPrefixInterface+"flood-reduction") + if virtualLink["flood_reduction"].(bool) { + configSet = append(configSet, setPrefixVirtualLink+"flood-reduction") } - if v := ospfInterface["hello_interval"].(int); v != 0 { - configSet = append(configSet, setPrefixInterface+"hello-interval "+strconv.Itoa(v)) + if v := virtualLink["hello_interval"].(int); v != 0 { + configSet = append(configSet, setPrefixVirtualLink+"hello-interval "+strconv.Itoa(v)) } - if v := ospfInterface["interface_type"].(string); v != "" { - configSet = append(configSet, setPrefixInterface+"interface-type "+v) + if v := virtualLink["ipsec_sa"].(string); v != "" { + configSet = append(configSet, setPrefixVirtualLink+"ipsec-sa \""+v+"\"") } - if v := ospfInterface["ipsec_sa"].(string); v != "" { - configSet = append(configSet, setPrefixInterface+"ipsec-sa \""+v+"\"") + if v := virtualLink["mtu"].(int); v != 0 { + configSet = append(configSet, setPrefixVirtualLink+"mtu "+strconv.Itoa(v)) } - if t := ospfInterface["ipv4_adjacency_segment_protected_type"].(string); t != "" { - if v := ospfInterface["ipv4_adjacency_segment_protected_value"].(string); v != "" { - configSet = append(configSet, setPrefixInterface+"ipv4-adjacency-segment protected "+t+" "+v) - } else { - configSet = append(configSet, setPrefixInterface+"ipv4-adjacency-segment protected "+t) - } - } else if ospfInterface["ipv4_adjacency_segment_protected_value"].(string) != "" { - return fmt.Errorf("ipv4_adjacency_segment_protected_type need to be set with " + - "ipv4_adjacency_segment_protected_value") - } - if t := ospfInterface["ipv4_adjacency_segment_unprotected_type"].(string); t != "" { - if v := ospfInterface["ipv4_adjacency_segment_unprotected_value"].(string); v != "" { - configSet = append(configSet, setPrefixInterface+"ipv4-adjacency-segment unprotected "+t+" "+v) - } else { - configSet = append(configSet, setPrefixInterface+"ipv4-adjacency-segment unprotected "+t) - } - } else if ospfInterface["ipv4_adjacency_segment_unprotected_value"].(string) != "" { - return fmt.Errorf("ipv4_adjacency_segment_unprotected_type need to be set with " + - "ipv4_adjacency_segment_unprotected_value") + if v := virtualLink["retransmit_interval"].(int); v != 0 { + configSet = append(configSet, setPrefixVirtualLink+"retransmit-interval "+strconv.Itoa(v)) } - if ospfInterface["link_protection"].(bool) { - configSet = append(configSet, setPrefixInterface+"link-protection") + if v := virtualLink["transit_delay"].(int); v != 0 { + configSet = append(configSet, setPrefixVirtualLink+"transit-delay "+strconv.Itoa(v)) } - if v := ospfInterface["metric"].(int); v != 0 { - configSet = append(configSet, setPrefixInterface+"metric "+strconv.Itoa(v)) + } + + return clt.configSet(configSet, junSess) +} + +func setOspfAreaInterface(setPrefix string, ospfInterface map[string]interface{}) ([]string, error) { + configSet := make([]string, 0) + + configSet = append(configSet, setPrefix) + if v := ospfInterface["authentication_simple_password"].(string); v != "" { + if len(ospfInterface["authentication_md5"].([]interface{})) != 0 { + return configSet, fmt.Errorf("conflict between 'authentication_simple_password' and 'authentication_md5'"+ + " in interface '%s'", ospfInterface["name"].(string)) } - if v := ospfInterface["mtu"].(int); v != 0 { - configSet = append(configSet, setPrefixInterface+"mtu "+strconv.Itoa(v)) + configSet = append(configSet, setPrefix+"authentication simple-password \""+v+"\"") + } + authenticationMD5List := make([]int, 0) + for _, mAuthMD5 := range ospfInterface["authentication_md5"].([]interface{}) { + authMD5 := mAuthMD5.(map[string]interface{}) + if bchk.IntInSlice(authMD5["key_id"].(int), authenticationMD5List) { + return configSet, fmt.Errorf("multiple blocks authentication_md5 with the same key_id %d in interface with name %s", + authMD5["key_id"].(int), ospfInterface["name"].(string)) } - neighborList := make([]string, 0) - for _, mNeighbor := range ospfInterface["neighbor"].(*schema.Set).List() { - neighbor := mNeighbor.(map[string]interface{}) - if bchk.StringInSlice(neighbor["address"].(string), neighborList) { - return fmt.Errorf("multiple blocks neighbor with the same address %s in interface with name %s", - neighbor["address"].(string), ospfInterface["name"].(string)) - } - neighborList = append(neighborList, neighbor["address"].(string)) - configSet = append(configSet, setPrefixInterface+"neighbor "+neighbor["address"].(string)) - if neighbor["eligible"].(bool) { - configSet = append(configSet, setPrefixInterface+"neighbor "+neighbor["address"].(string)+" eligible") - } + authenticationMD5List = append(authenticationMD5List, authMD5["key_id"].(int)) + configSet = append(configSet, setPrefix+"authentication md5 "+ + strconv.Itoa(authMD5["key_id"].(int))+" key \""+authMD5["key"].(string)+"\"") + if v := authMD5["start_time"].(string); v != "" { + configSet = append(configSet, setPrefix+"authentication md5 "+ + strconv.Itoa(authMD5["key_id"].(int))+" start-time "+v) } - if ospfInterface["no_advertise_adjacency_segment"].(bool) { - configSet = append(configSet, setPrefixInterface+"no-advertise-adjacency-segment") + } + bandwidthBasedMetricsList := make([]string, 0) + for _, mBandBaseMet := range ospfInterface["bandwidth_based_metrics"].(*schema.Set).List() { + bandwidthBaseMetrics := mBandBaseMet.(map[string]interface{}) + if bchk.StringInSlice(bandwidthBaseMetrics["bandwidth"].(string), bandwidthBasedMetricsList) { + return configSet, fmt.Errorf("multiple blocks bandwidth_based_metrics "+ + "with the same bandwidth %s in interface with name %s", + bandwidthBaseMetrics["bandwidth"].(string), ospfInterface["name"].(string)) } - if ospfInterface["no_eligible_backup"].(bool) { - configSet = append(configSet, setPrefixInterface+"no-eligible-backup") + bandwidthBasedMetricsList = append(bandwidthBasedMetricsList, bandwidthBaseMetrics["bandwidth"].(string)) + configSet = append(configSet, setPrefix+"bandwidth-based-metrics bandwidth "+ + bandwidthBaseMetrics["bandwidth"].(string)+" metric "+strconv.Itoa(bandwidthBaseMetrics["metric"].(int))) + } + for _, mBFDLivDet := range ospfInterface["bfd_liveness_detection"].([]interface{}) { + if mBFDLivDet == nil { + return configSet, fmt.Errorf("bfd_liveness_detection block is empty in interface %s", ospfInterface["name"].(string)) } - if ospfInterface["no_eligible_remote_backup"].(bool) { - configSet = append(configSet, setPrefixInterface+"no-eligible-remote-backup") + setPrefixBfd := setPrefix + "bfd-liveness-detection " + bfdLiveDetect := mBFDLivDet.(map[string]interface{}) + if v := bfdLiveDetect["authentication_algorithm"].(string); v != "" { + configSet = append(configSet, setPrefixBfd+"authentication algorithm "+v) } - if ospfInterface["no_interface_state_traps"].(bool) { - configSet = append(configSet, setPrefixInterface+"no-interface-state-traps") + if v := bfdLiveDetect["authentication_key_chain"].(string); v != "" { + configSet = append(configSet, setPrefixBfd+"authentication key-chain \""+v+"\"") } - if ospfInterface["no_neighbor_down_notification"].(bool) { - configSet = append(configSet, setPrefixInterface+"no-neighbor-down-notification") + if bfdLiveDetect["authentication_loose_check"].(bool) { + configSet = append(configSet, setPrefixBfd+"authentication loose-check") } - if ospfInterface["node_link_protection"].(bool) { - configSet = append(configSet, setPrefixInterface+"node-link-protection") + if v := bfdLiveDetect["detection_time_threshold"].(int); v != 0 { + configSet = append(configSet, setPrefixBfd+"detection-time threshold "+strconv.Itoa(v)) } - if ospfInterface["passive"].(bool) { - configSet = append(configSet, setPrefixInterface+"passive") - if v := ospfInterface["passive_traffic_engineering_remote_node_id"].(string); v != "" { - configSet = append(configSet, setPrefixInterface+"passive traffic-engineering remote-node-id "+v) - } - if v := ospfInterface["passive_traffic_engineering_remote_node_router_id"].(string); v != "" { - configSet = append(configSet, setPrefixInterface+"passive traffic-engineering remote-node-router-id "+v) - } - } else if ospfInterface["passive_traffic_engineering_remote_node_id"].(string) != "" || - ospfInterface["passive_traffic_engineering_remote_node_router_id"].(string) != "" { - return fmt.Errorf("passive need to be true with " + - "passive_traffic_engineering_remote_node_id and passive_traffic_engineering_remote_node_router_id") + if bfdLiveDetect["full_neighbors_only"].(bool) { + configSet = append(configSet, setPrefixBfd+"full-neighbors-only") + } + if v := bfdLiveDetect["holddown_interval"].(int); v != 0 { + configSet = append(configSet, setPrefixBfd+"holddown-interval "+strconv.Itoa(v)) + } + if v := bfdLiveDetect["minimum_interval"].(int); v != 0 { + configSet = append(configSet, setPrefixBfd+"minimum-interval "+strconv.Itoa(v)) } - if v := ospfInterface["poll_interval"].(int); v != 0 { - configSet = append(configSet, setPrefixInterface+"poll-interval "+strconv.Itoa(v)) + if v := bfdLiveDetect["minimum_receive_interval"].(int); v != 0 { + configSet = append(configSet, setPrefixBfd+"minimum-receive-interval "+strconv.Itoa(v)) } - if v := ospfInterface["priority"].(int); v != -1 { - configSet = append(configSet, setPrefixInterface+"priority "+strconv.Itoa(v)) + if v := bfdLiveDetect["multiplier"].(int); v != 0 { + configSet = append(configSet, setPrefixBfd+"multiplier "+strconv.Itoa(v)) } - if v := ospfInterface["retransmit_interval"].(int); v != 0 { - configSet = append(configSet, setPrefixInterface+"retransmit-interval "+strconv.Itoa(v)) + if bfdLiveDetect["no_adaptation"].(bool) { + configSet = append(configSet, setPrefixBfd+"no-adaptation") } - if ospfInterface["secondary"].(bool) { - configSet = append(configSet, setPrefixInterface+"secondary") + if v := bfdLiveDetect["transmit_interval_minimum_interval"].(int); v != 0 { + configSet = append(configSet, setPrefixBfd+ + "transmit-interval minimum-interval "+strconv.Itoa(v)) } - if ospfInterface["strict_bfd"].(bool) { - configSet = append(configSet, setPrefixInterface+"strict-bfd") + if v := bfdLiveDetect["transmit_interval_threshold"].(int); v != 0 { + configSet = append(configSet, setPrefixBfd+ + "transmit-interval threshold "+strconv.Itoa(v)) } - if v := ospfInterface["te_metric"].(int); v != 0 { - configSet = append(configSet, setPrefixInterface+"te-metric "+strconv.Itoa(v)) + if v := bfdLiveDetect["version"].(string); v != "" { + configSet = append(configSet, setPrefixBfd+"version "+v) + } + if len(configSet) == 0 || !strings.HasPrefix(configSet[len(configSet)-1], setPrefixBfd) { + return configSet, fmt.Errorf("bfd_liveness_detection block is empty in interface %s", ospfInterface["name"].(string)) + } + } + if v := ospfInterface["dead_interval"].(int); v != 0 { + configSet = append(configSet, setPrefix+"dead-interval "+strconv.Itoa(v)) + } + if ospfInterface["demand_circuit"].(bool) { + configSet = append(configSet, setPrefix+"demand-circuit") + } + if ospfInterface["disable"].(bool) { + configSet = append(configSet, setPrefix+"disable") + } + if ospfInterface["dynamic_neighbors"].(bool) { + configSet = append(configSet, setPrefix+"dynamic-neighbors") + } + if ospfInterface["flood_reduction"].(bool) { + configSet = append(configSet, setPrefix+"flood-reduction") + } + if v := ospfInterface["hello_interval"].(int); v != 0 { + configSet = append(configSet, setPrefix+"hello-interval "+strconv.Itoa(v)) + } + if v := ospfInterface["interface_type"].(string); v != "" { + configSet = append(configSet, setPrefix+"interface-type "+v) + } + if v := ospfInterface["ipsec_sa"].(string); v != "" { + configSet = append(configSet, setPrefix+"ipsec-sa \""+v+"\"") + } + if t := ospfInterface["ipv4_adjacency_segment_protected_type"].(string); t != "" { + if v := ospfInterface["ipv4_adjacency_segment_protected_value"].(string); v != "" { + configSet = append(configSet, setPrefix+"ipv4-adjacency-segment protected "+t+" "+v) + } else { + configSet = append(configSet, setPrefix+"ipv4-adjacency-segment protected "+t) + } + } else if ospfInterface["ipv4_adjacency_segment_protected_value"].(string) != "" { + return configSet, fmt.Errorf("ipv4_adjacency_segment_protected_type need to be set with " + + "ipv4_adjacency_segment_protected_value") + } + if t := ospfInterface["ipv4_adjacency_segment_unprotected_type"].(string); t != "" { + if v := ospfInterface["ipv4_adjacency_segment_unprotected_value"].(string); v != "" { + configSet = append(configSet, setPrefix+"ipv4-adjacency-segment unprotected "+t+" "+v) + } else { + configSet = append(configSet, setPrefix+"ipv4-adjacency-segment unprotected "+t) + } + } else if ospfInterface["ipv4_adjacency_segment_unprotected_value"].(string) != "" { + return configSet, fmt.Errorf("ipv4_adjacency_segment_unprotected_type need to be set with " + + "ipv4_adjacency_segment_unprotected_value") + } + if ospfInterface["link_protection"].(bool) { + configSet = append(configSet, setPrefix+"link-protection") + } + if v := ospfInterface["metric"].(int); v != 0 { + configSet = append(configSet, setPrefix+"metric "+strconv.Itoa(v)) + } + if v := ospfInterface["mtu"].(int); v != 0 { + configSet = append(configSet, setPrefix+"mtu "+strconv.Itoa(v)) + } + neighborList := make([]string, 0) + for _, mNeighbor := range ospfInterface["neighbor"].(*schema.Set).List() { + neighbor := mNeighbor.(map[string]interface{}) + if bchk.StringInSlice(neighbor["address"].(string), neighborList) { + return configSet, fmt.Errorf("multiple blocks neighbor with the same address %s in interface with name %s", + neighbor["address"].(string), ospfInterface["name"].(string)) + } + neighborList = append(neighborList, neighbor["address"].(string)) + configSet = append(configSet, setPrefix+"neighbor "+neighbor["address"].(string)) + if neighbor["eligible"].(bool) { + configSet = append(configSet, setPrefix+"neighbor "+neighbor["address"].(string)+" eligible") + } + } + if ospfInterface["no_advertise_adjacency_segment"].(bool) { + configSet = append(configSet, setPrefix+"no-advertise-adjacency-segment") + } + if ospfInterface["no_eligible_backup"].(bool) { + configSet = append(configSet, setPrefix+"no-eligible-backup") + } + if ospfInterface["no_eligible_remote_backup"].(bool) { + configSet = append(configSet, setPrefix+"no-eligible-remote-backup") + } + if ospfInterface["no_interface_state_traps"].(bool) { + configSet = append(configSet, setPrefix+"no-interface-state-traps") + } + if ospfInterface["no_neighbor_down_notification"].(bool) { + configSet = append(configSet, setPrefix+"no-neighbor-down-notification") + } + if ospfInterface["node_link_protection"].(bool) { + configSet = append(configSet, setPrefix+"node-link-protection") + } + if ospfInterface["passive"].(bool) { + configSet = append(configSet, setPrefix+"passive") + if v := ospfInterface["passive_traffic_engineering_remote_node_id"].(string); v != "" { + configSet = append(configSet, setPrefix+"passive traffic-engineering remote-node-id "+v) } - if v := ospfInterface["transit_delay"].(int); v != 0 { - configSet = append(configSet, setPrefixInterface+"transit-delay "+strconv.Itoa(v)) + if v := ospfInterface["passive_traffic_engineering_remote_node_router_id"].(string); v != "" { + configSet = append(configSet, setPrefix+"passive traffic-engineering remote-node-router-id "+v) } + } else if ospfInterface["passive_traffic_engineering_remote_node_id"].(string) != "" || + ospfInterface["passive_traffic_engineering_remote_node_router_id"].(string) != "" { + return configSet, fmt.Errorf("passive need to be true with " + + "passive_traffic_engineering_remote_node_id and passive_traffic_engineering_remote_node_router_id") + } + if v := ospfInterface["poll_interval"].(int); v != 0 { + configSet = append(configSet, setPrefix+"poll-interval "+strconv.Itoa(v)) + } + if v := ospfInterface["priority"].(int); v != -1 { + configSet = append(configSet, setPrefix+"priority "+strconv.Itoa(v)) + } + if v := ospfInterface["retransmit_interval"].(int); v != 0 { + configSet = append(configSet, setPrefix+"retransmit-interval "+strconv.Itoa(v)) + } + if ospfInterface["secondary"].(bool) { + configSet = append(configSet, setPrefix+"secondary") + } + if ospfInterface["strict_bfd"].(bool) { + configSet = append(configSet, setPrefix+"strict-bfd") + } + if v := ospfInterface["te_metric"].(int); v != 0 { + configSet = append(configSet, setPrefix+"te-metric "+strconv.Itoa(v)) + } + if v := ospfInterface["transit_delay"].(int); v != 0 { + configSet = append(configSet, setPrefix+"transit-delay "+strconv.Itoa(v)) } - return sess.configSet(configSet, jnprSess) + return configSet, nil } -func readOspfArea(idArea, version, routingInstance string, m interface{}, jnprSess *NetconfObject, +func readOspfArea(idArea, version, realm, routingInstance string, clt *Client, junSess *junosSession, ) (ospfAreaOptions, error) { - sess := m.(*Session) var confRead ospfAreaOptions var showConfig string var err error ospfVersion := ospfV2 if version == "v3" { ospfVersion = ospfV3 + } else if realm != "" { + return confRead, fmt.Errorf("realm can't set if version != v3") } - if routingInstance == defaultW { - showConfig, err = sess.command(cmdShowConfig+ - "protocols "+ospfVersion+" area "+idArea+pipeDisplaySetRelative, jnprSess) + switch { + case routingInstance == defaultW && realm == "": + showConfig, err = clt.command(cmdShowConfig+ + "protocols "+ospfVersion+" area "+idArea+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } - } else { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ - "protocols "+ospfVersion+" area "+idArea+pipeDisplaySetRelative, jnprSess) + case routingInstance == defaultW && realm != "": + showConfig, err = clt.command(cmdShowConfig+ + "protocols "+ospfVersion+" realm "+realm+" area "+idArea+pipeDisplaySetRelative, junSess) + if err != nil { + return confRead, err + } + case realm != "": + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ + "protocols "+ospfVersion+" realm "+realm+" area "+idArea+pipeDisplaySetRelative, junSess) + if err != nil { + return confRead, err + } + default: + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ + "protocols "+ospfVersion+" area "+idArea+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -823,6 +1342,7 @@ func readOspfArea(idArea, version, routingInstance string, m interface{}, jnprSe if showConfig != emptyW { confRead.areaID = idArea + confRead.realm = realm confRead.version = version confRead.routingInstance = routingInstance for _, item := range strings.Split(showConfig, "\n") { @@ -833,7 +1353,8 @@ func readOspfArea(idArea, version, routingInstance string, m interface{}, jnprSe break } itemTrim := strings.TrimPrefix(item, setLS) - if strings.HasPrefix(itemTrim, "interface ") { + switch { + case strings.HasPrefix(itemTrim, "interface "): itemInterfaceList := strings.Split(strings.TrimPrefix(itemTrim, "interface "), " ") interfaceOptions := map[string]interface{}{ "name": itemInterfaceList[0], @@ -876,206 +1397,186 @@ func readOspfArea(idArea, version, routingInstance string, m interface{}, jnprSe } itemTrimInterface := strings.TrimPrefix(itemTrim, "interface "+itemInterfaceList[0]+" ") confRead.interFace = copyAndRemoveItemMapList("name", interfaceOptions, confRead.interFace) + if err := readOspfAreaInterface(itemTrimInterface, interfaceOptions); err != nil { + return confRead, err + } + confRead.interFace = append(confRead.interFace, interfaceOptions) + case strings.HasPrefix(itemTrim, "area-range "): + itemTrimSplit := strings.Split(strings.TrimPrefix(itemTrim, "area-range "), " ") + areaRange := map[string]interface{}{ + "range": itemTrimSplit[0], + "exact": false, + "override_metric": 0, + "restrict": false, + } + confRead.areaRange = copyAndRemoveItemMapList("range", areaRange, confRead.areaRange) + itemTrimAreaRange := strings.TrimPrefix(itemTrim, "area-range "+itemTrimSplit[0]+" ") switch { - case strings.HasPrefix(itemTrimInterface, "authentication simple-password "): + case itemTrimAreaRange == "exact": + areaRange["exact"] = true + case strings.HasPrefix(itemTrimAreaRange, "override-metric "): var err error - interfaceOptions["authentication_simple_password"], err = jdecode.Decode(strings.Trim(strings.TrimPrefix( - itemTrimInterface, "authentication simple-password "), "\"")) - if err != nil { - return confRead, fmt.Errorf("failed to decode authentication simple-password: %w", err) - } - case strings.HasPrefix(itemTrimInterface, "authentication md5 "): - itemTrimInterfaceSplit := strings.Split(strings.TrimPrefix(itemTrimInterface, "authentication md5 "), " ") - keyID, err := strconv.Atoi(itemTrimInterfaceSplit[0]) + areaRange["override_metric"], err = strconv.Atoi(strings.TrimPrefix(itemTrimAreaRange, "override-metric ")) if err != nil { - return confRead, fmt.Errorf(failedConvAtoiError, itemTrimInterfaceSplit[0], err) + return confRead, fmt.Errorf(failedConvAtoiError, itemTrim, err) } - authMD5 := map[string]interface{}{ - "key_id": keyID, - "key": "", - "start_time": "", + case itemTrimAreaRange == "restrict": + areaRange["restrict"] = true + } + confRead.areaRange = append(confRead.areaRange, areaRange) + case strings.HasPrefix(itemTrim, "context-identifier "): + confRead.contextIdentifier = append(confRead.contextIdentifier, strings.TrimPrefix(itemTrim, "context-identifier ")) + case strings.HasPrefix(itemTrim, "inter-area-prefix-export "): + confRead.interAreaPrefixExport = append( + confRead.interAreaPrefixExport, strings.TrimPrefix(itemTrim, "inter-area-prefix-export ")) + case strings.HasPrefix(itemTrim, "inter-area-prefix-import "): + confRead.interAreaPrefixImport = append( + confRead.interAreaPrefixImport, strings.TrimPrefix(itemTrim, "inter-area-prefix-import ")) + case strings.HasPrefix(itemTrim, "network-summary-export "): + confRead.networkSummaryExport = append( + confRead.networkSummaryExport, strings.TrimPrefix(itemTrim, "network-summary-export ")) + case strings.HasPrefix(itemTrim, "network-summary-import "): + confRead.networkSummaryImport = append( + confRead.networkSummaryImport, strings.TrimPrefix(itemTrim, "network-summary-import ")) + case itemTrim == "no-context-identifier-advertisement": + confRead.noContextIdentifierAdvertisement = true + case strings.HasPrefix(itemTrim, "nssa"): + if len(confRead.nssa) == 0 { + confRead.nssa = append(confRead.nssa, map[string]interface{}{ + "area_range": make([]map[string]interface{}, 0), + "default_lsa": make([]map[string]interface{}, 0), + "no_summaries": false, + "summaries": false, + }) + } + switch { + case strings.HasPrefix(itemTrim, "nssa area-range "): + itemTrimSplit := strings.Split(strings.TrimPrefix(itemTrim, "nssa area-range "), " ") + areaRange := map[string]interface{}{ + "range": itemTrimSplit[0], + "exact": false, + "override_metric": 0, + "restrict": false, } - interfaceOptions["authentication_md5"] = copyAndRemoveItemMapList("key_id", authMD5, - interfaceOptions["authentication_md5"].([]map[string]interface{})) - itemTrimAuthMD5 := strings.TrimPrefix(itemTrimInterface, "authentication md5 "+itemTrimInterfaceSplit[0]+" ") + confRead.nssa[0]["area_range"] = copyAndRemoveItemMapList( + "range", + areaRange, + confRead.nssa[0]["area_range"].([]map[string]interface{}), + ) + itemTrimAreaRange := strings.TrimPrefix(itemTrim, "nssa area-range "+itemTrimSplit[0]+" ") switch { - case strings.HasPrefix(itemTrimAuthMD5, "key "): + case itemTrimAreaRange == "exact": + areaRange["exact"] = true + case strings.HasPrefix(itemTrimAreaRange, "override-metric "): var err error - authMD5["key"], err = jdecode.Decode(strings.Trim(strings.TrimPrefix( - itemTrimAuthMD5, "key "), "\"")) + areaRange["override_metric"], err = strconv.Atoi(strings.TrimPrefix(itemTrimAreaRange, "override-metric ")) if err != nil { - return confRead, fmt.Errorf("failed to decode authentication md5 key: %w", err) + return confRead, fmt.Errorf(failedConvAtoiError, itemTrim, err) } - case strings.HasPrefix(itemTrimAuthMD5, "start-time "): - authMD5["start_time"] = strings.Split(strings.Trim(strings.TrimPrefix( - itemTrimAuthMD5, "start-time "), "\""), " ")[0] - } - interfaceOptions["authentication_md5"] = append( - interfaceOptions["authentication_md5"].([]map[string]interface{}), authMD5) - case strings.HasPrefix(itemTrimInterface, "bandwidth-based-metrics bandwidth "): - itemTrimInterfaceSplit := strings.Split(strings.TrimPrefix( - itemTrimInterface, "bandwidth-based-metrics bandwidth "), " ") - if len(itemTrimInterfaceSplit) < 3 { - return confRead, fmt.Errorf("can't read values for bandwidth_based_metrics in %s", itemTrimInterface) + case itemTrimAreaRange == "restrict": + areaRange["restrict"] = true } - metric, err := strconv.Atoi(itemTrimInterfaceSplit[2]) - if err != nil { - return confRead, fmt.Errorf(failedConvAtoiError, itemTrimInterfaceSplit[2], err) - } - interfaceOptions["bandwidth_based_metrics"] = append( - interfaceOptions["bandwidth_based_metrics"].([]map[string]interface{}), map[string]interface{}{ - "bandwidth": itemTrimInterfaceSplit[0], - "metric": metric, - }) - case strings.HasPrefix(itemTrimInterface, "bfd-liveness-detection "): - if len(interfaceOptions["bfd_liveness_detection"].([]map[string]interface{})) == 0 { - interfaceOptions["bfd_liveness_detection"] = append( - interfaceOptions["bfd_liveness_detection"].([]map[string]interface{}), map[string]interface{}{ - "authentication_algorithm": "", - "authentication_key_chain": "", - "authentication_loose_check": false, - "detection_time_threshold": 0, - "full_neighbors_only": false, - "holddown_interval": 0, - "minimum_interval": 0, - "minimum_receive_interval": 0, - "multiplier": 0, - "no_adaptation": false, - "transmit_interval_minimum_interval": 0, - "transmit_interval_threshold": 0, - "version": "", - }) - } - if err := readOspfAreaInterfaceBfd(strings.TrimPrefix(itemTrimInterface, "bfd-liveness-detection "), - interfaceOptions["bfd_liveness_detection"].([]map[string]interface{})[0]); err != nil { - return confRead, err - } - case strings.HasPrefix(itemTrimInterface, "dead-interval "): - interfaceOptions["dead_interval"], err = strconv.Atoi( - strings.TrimPrefix(itemTrimInterface, "dead-interval ")) - if err != nil { - return confRead, fmt.Errorf(failedConvAtoiError, itemTrimInterface, err) - } - case itemTrimInterface == "demand-circuit": - interfaceOptions["demand_circuit"] = true - case itemTrimInterface == disableW: - interfaceOptions["disable"] = true - case itemTrimInterface == "dynamic-neighbors": - interfaceOptions["dynamic_neighbors"] = true - case itemTrimInterface == "flood-reduction": - interfaceOptions["flood_reduction"] = true - case strings.HasPrefix(itemTrimInterface, "hello-interval "): - interfaceOptions["hello_interval"], err = strconv.Atoi( - strings.TrimPrefix(itemTrimInterface, "hello-interval ")) - if err != nil { - return confRead, fmt.Errorf(failedConvAtoiError, itemTrimInterface, err) - } - case strings.HasPrefix(itemTrimInterface, "interface-type "): - interfaceOptions["interface_type"] = strings.TrimPrefix(itemTrimInterface, "interface-type ") - case strings.HasPrefix(itemTrimInterface, "ipsec-sa "): - interfaceOptions["ipsec_sa"] = strings.Trim(strings.TrimPrefix(itemTrimInterface, "ipsec-sa "), "\"") - case strings.HasPrefix(itemTrimInterface, "ipv4-adjacency-segment protected "): - itemTrimInterfaceSplit := strings.Split(strings.TrimPrefix( - itemTrimInterface, "ipv4-adjacency-segment protected "), " ") - interfaceOptions["ipv4_adjacency_segment_protected_type"] = itemTrimInterfaceSplit[0] - if len(itemTrimInterfaceSplit) > 1 { - interfaceOptions["ipv4_adjacency_segment_protected_value"] = itemTrimInterfaceSplit[1] - } - case strings.HasPrefix(itemTrimInterface, "ipv4-adjacency-segment unprotected "): - itemTrimInterfaceSplit := strings.Split(strings.TrimPrefix( - itemTrimInterface, "ipv4-adjacency-segment unprotected "), " ") - interfaceOptions["ipv4_adjacency_segment_unprotected_type"] = itemTrimInterfaceSplit[0] - if len(itemTrimInterfaceSplit) > 1 { - interfaceOptions["ipv4_adjacency_segment_unprotected_value"] = itemTrimInterfaceSplit[1] - } - case itemTrimInterface == "link-protection": - interfaceOptions["link_protection"] = true - case strings.HasPrefix(itemTrimInterface, "metric "): - interfaceOptions["metric"], err = strconv.Atoi( - strings.TrimPrefix(itemTrimInterface, "metric ")) - if err != nil { - return confRead, fmt.Errorf(failedConvAtoiError, itemTrimInterface, err) - } - case strings.HasPrefix(itemTrimInterface, "mtu "): - interfaceOptions["mtu"], err = strconv.Atoi( - strings.TrimPrefix(itemTrimInterface, "mtu ")) - if err != nil { - return confRead, fmt.Errorf(failedConvAtoiError, itemTrimInterface, err) - } - case strings.HasPrefix(itemTrimInterface, "neighbor "): - itemTrimInterfaceSplit := strings.Split(strings.TrimPrefix(itemTrimInterface, "neighbor "), " ") - address := itemTrimInterfaceSplit[0] - if len(itemTrimInterfaceSplit) > 1 && itemTrimInterfaceSplit[1] == "eligible" { - interfaceOptions["neighbor"] = append( - interfaceOptions["neighbor"].([]map[string]interface{}), map[string]interface{}{ - "address": address, - "eligible": true, - }) - } else { - interfaceOptions["neighbor"] = append( - interfaceOptions["neighbor"].([]map[string]interface{}), map[string]interface{}{ - "address": address, - "eligible": false, + confRead.nssa[0]["area_range"] = append(confRead.nssa[0]["area_range"].([]map[string]interface{}), areaRange) + case strings.HasPrefix(itemTrim, "nssa default-lsa"): + if len(confRead.nssa[0]["default_lsa"].([]map[string]interface{})) == 0 { + confRead.nssa[0]["default_lsa"] = append( + confRead.nssa[0]["default_lsa"].([]map[string]interface{}), + map[string]interface{}{ + "default_metric": 0, + "metric_type": 0, + "type_7": false, }) } - case itemTrimInterface == "no-advertise-adjacency-segment": - interfaceOptions["no_advertise_adjacency_segment"] = true - case itemTrimInterface == "no-eligible-backup": - interfaceOptions["no_eligible_backup"] = true - case itemTrimInterface == "no-eligible-remote-backup": - interfaceOptions["no_eligible_remote_backup"] = true - case itemTrimInterface == "no-interface-state-traps": - interfaceOptions["no_interface_state_traps"] = true - case itemTrimInterface == "no-neighbor-down-notification": - interfaceOptions["no_neighbor_down_notification"] = true - case itemTrimInterface == "node-link-protection": - interfaceOptions["node_link_protection"] = true - case strings.HasPrefix(itemTrimInterface, "passive"): - interfaceOptions["passive"] = true + defaultLsa := confRead.nssa[0]["default_lsa"].([]map[string]interface{})[0] switch { - case strings.HasPrefix(itemTrimInterface, "passive traffic-engineering remote-node-id "): - interfaceOptions["passive_traffic_engineering_remote_node_id"] = strings.TrimPrefix( - itemTrimInterface, "passive traffic-engineering remote-node-id ") - case strings.HasPrefix(itemTrimInterface, "passive traffic-engineering remote-node-router-id "): - interfaceOptions["passive_traffic_engineering_remote_node_router_id"] = strings.TrimPrefix( - itemTrimInterface, "passive traffic-engineering remote-node-router-id ") - } - case strings.HasPrefix(itemTrimInterface, "poll-interval "): - interfaceOptions["poll_interval"], err = strconv.Atoi( - strings.TrimPrefix(itemTrimInterface, "poll-interval ")) - if err != nil { - return confRead, fmt.Errorf(failedConvAtoiError, itemTrimInterface, err) - } - case strings.HasPrefix(itemTrimInterface, "priority "): - interfaceOptions["priority"], err = strconv.Atoi( - strings.TrimPrefix(itemTrimInterface, "priority ")) - if err != nil { - return confRead, fmt.Errorf(failedConvAtoiError, itemTrimInterface, err) - } - case strings.HasPrefix(itemTrimInterface, "retransmit-interval "): - interfaceOptions["retransmit_interval"], err = strconv.Atoi( - strings.TrimPrefix(itemTrimInterface, "retransmit-interval ")) - if err != nil { - return confRead, fmt.Errorf(failedConvAtoiError, itemTrimInterface, err) - } - case itemTrimInterface == "secondary": - interfaceOptions["secondary"] = true - case itemTrimInterface == "strict-bfd": - interfaceOptions["strict_bfd"] = true - case strings.HasPrefix(itemTrimInterface, "te-metric "): - interfaceOptions["te_metric"], err = strconv.Atoi( - strings.TrimPrefix(itemTrimInterface, "te-metric ")) - if err != nil { - return confRead, fmt.Errorf(failedConvAtoiError, itemTrimInterface, err) + case strings.HasPrefix(itemTrim, "nssa default-lsa default-metric "): + var err error + defaultLsa["default_metric"], err = strconv.Atoi(strings.TrimPrefix(itemTrim, "nssa default-lsa default-metric ")) + if err != nil { + return confRead, fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case strings.HasPrefix(itemTrim, "nssa default-lsa metric-type "): + var err error + defaultLsa["metric_type"], err = strconv.Atoi(strings.TrimPrefix(itemTrim, "nssa default-lsa metric-type ")) + if err != nil { + return confRead, fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case itemTrim == "nssa default-lsa type-7": + defaultLsa["type_7"] = true } - case strings.HasPrefix(itemTrimInterface, "transit-delay "): - interfaceOptions["transit_delay"], err = strconv.Atoi( - strings.TrimPrefix(itemTrimInterface, "transit-delay ")) + case itemTrim == "nssa no-summaries": + confRead.nssa[0]["no_summaries"] = true + case itemTrim == "nssa summaries": + confRead.nssa[0]["summaries"] = true + } + case strings.HasPrefix(itemTrim, "stub"): + if len(confRead.stub) == 0 { + confRead.stub = append(confRead.stub, map[string]interface{}{ + "default_metric": 0, + "no_summaries": false, + "summaries": false, + }) + } + switch { + case strings.HasPrefix(itemTrim, "stub default-metric "): + var err error + confRead.stub[0]["default_metric"], err = strconv.Atoi(strings.TrimPrefix(itemTrim, "stub default-metric ")) if err != nil { - return confRead, fmt.Errorf(failedConvAtoiError, itemTrimInterface, err) + return confRead, fmt.Errorf(failedConvAtoiError, itemTrim, err) } + case itemTrim == "stub no-summaries": + confRead.stub[0]["no_summaries"] = true + case itemTrim == "stub summaries": + confRead.stub[0]["summaries"] = true } - confRead.interFace = append(confRead.interFace, interfaceOptions) + case strings.HasPrefix(itemTrim, "virtual-link "): + itemTrimSplit := strings.Split(strings.TrimPrefix(itemTrim, "virtual-link "), " ") + if len(itemTrimSplit) < 4 { + return confRead, fmt.Errorf("can't find neighbor_id and transit_area in %s", itemTrim) + } + virtualLink := map[string]interface{}{ + "neighbor_id": itemTrimSplit[1], + "transit_area": itemTrimSplit[3], + "dead_interval": 0, + "demand_circuit": false, + "disable": false, + "flood_reduction": false, + "hello_interval": 0, + "ipsec_sa": "", + "mtu": 0, + "retransmit_interval": 0, + "transit_delay": 0, + } + confRead.virtualLink = copyAndRemoveItemMapList2("neighbor_id", "transit_area", virtualLink, confRead.virtualLink) + itemTrimVirtualLink := strings.TrimPrefix(itemTrim, + "virtual-link neighbor-id "+itemTrimSplit[1]+" transit-area "+itemTrimSplit[3]+" ") + var err error + switch { + case strings.HasPrefix(itemTrimVirtualLink, "dead-interval "): + virtualLink["dead_interval"], err = strconv.Atoi(strings.TrimPrefix(itemTrimVirtualLink, "dead-interval ")) + case itemTrimVirtualLink == "demand-circuit": + virtualLink["demand_circuit"] = true + case itemTrimVirtualLink == "disable": + virtualLink["disable"] = true + case itemTrimVirtualLink == "flood-reduction": + virtualLink["flood_reduction"] = true + case strings.HasPrefix(itemTrimVirtualLink, "hello-interval "): + virtualLink["hello_interval"], err = strconv.Atoi(strings.TrimPrefix(itemTrimVirtualLink, "hello-interval ")) + case strings.HasPrefix(itemTrimVirtualLink, "ipsec-sa "): + virtualLink["ipsec_sa"] = strings.Trim(strings.TrimPrefix(itemTrimVirtualLink, "ipsec-sa "), "\"") + case strings.HasPrefix(itemTrimVirtualLink, "mtu "): + virtualLink["mtu"], err = strconv.Atoi(strings.TrimPrefix(itemTrimVirtualLink, "mtu ")) + case strings.HasPrefix(itemTrimVirtualLink, "retransmit-interval "): + virtualLink["retransmit_interval"], err = strconv.Atoi(strings.TrimPrefix( + itemTrimVirtualLink, "retransmit-interval ", + )) + case strings.HasPrefix(itemTrimVirtualLink, "transit-delay "): + virtualLink["transit_delay"], err = strconv.Atoi(strings.TrimPrefix(itemTrimVirtualLink, "transit-delay ")) + } + if err != nil { + return confRead, fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + confRead.virtualLink = append(confRead.virtualLink, virtualLink) } } } @@ -1083,6 +1584,211 @@ func readOspfArea(idArea, version, routingInstance string, m interface{}, jnprSe return confRead, nil } +func readOspfAreaInterface(itemTrim string, interfaceOptions map[string]interface{}) error { + var err error + switch { + case strings.HasPrefix(itemTrim, "authentication simple-password "): + var err error + interfaceOptions["authentication_simple_password"], err = jdecode.Decode(strings.Trim(strings.TrimPrefix( + itemTrim, "authentication simple-password "), "\"")) + if err != nil { + return fmt.Errorf("failed to decode authentication simple-password: %w", err) + } + case strings.HasPrefix(itemTrim, "authentication md5 "): + itemTrimSplit := strings.Split(strings.TrimPrefix(itemTrim, "authentication md5 "), " ") + keyID, err := strconv.Atoi(itemTrimSplit[0]) + if err != nil { + return fmt.Errorf(failedConvAtoiError, itemTrimSplit[0], err) + } + authMD5 := map[string]interface{}{ + "key_id": keyID, + "key": "", + "start_time": "", + } + interfaceOptions["authentication_md5"] = copyAndRemoveItemMapList("key_id", authMD5, + interfaceOptions["authentication_md5"].([]map[string]interface{})) + itemTrimAuthMD5 := strings.TrimPrefix(itemTrim, "authentication md5 "+itemTrimSplit[0]+" ") + switch { + case strings.HasPrefix(itemTrimAuthMD5, "key "): + var err error + authMD5["key"], err = jdecode.Decode(strings.Trim(strings.TrimPrefix( + itemTrimAuthMD5, "key "), "\"")) + if err != nil { + return fmt.Errorf("failed to decode authentication md5 key: %w", err) + } + case strings.HasPrefix(itemTrimAuthMD5, "start-time "): + authMD5["start_time"] = strings.Split(strings.Trim(strings.TrimPrefix( + itemTrimAuthMD5, "start-time "), "\""), " ")[0] + } + interfaceOptions["authentication_md5"] = append( + interfaceOptions["authentication_md5"].([]map[string]interface{}), authMD5) + case strings.HasPrefix(itemTrim, "bandwidth-based-metrics bandwidth "): + itemTrimSplit := strings.Split(strings.TrimPrefix( + itemTrim, "bandwidth-based-metrics bandwidth "), " ") + if len(itemTrimSplit) < 3 { + return fmt.Errorf("can't read values for bandwidth_based_metrics in %s", itemTrim) + } + metric, err := strconv.Atoi(itemTrimSplit[2]) + if err != nil { + return fmt.Errorf(failedConvAtoiError, itemTrimSplit[2], err) + } + interfaceOptions["bandwidth_based_metrics"] = append( + interfaceOptions["bandwidth_based_metrics"].([]map[string]interface{}), map[string]interface{}{ + "bandwidth": itemTrimSplit[0], + "metric": metric, + }) + case strings.HasPrefix(itemTrim, "bfd-liveness-detection "): + if len(interfaceOptions["bfd_liveness_detection"].([]map[string]interface{})) == 0 { + interfaceOptions["bfd_liveness_detection"] = append( + interfaceOptions["bfd_liveness_detection"].([]map[string]interface{}), map[string]interface{}{ + "authentication_algorithm": "", + "authentication_key_chain": "", + "authentication_loose_check": false, + "detection_time_threshold": 0, + "full_neighbors_only": false, + "holddown_interval": 0, + "minimum_interval": 0, + "minimum_receive_interval": 0, + "multiplier": 0, + "no_adaptation": false, + "transmit_interval_minimum_interval": 0, + "transmit_interval_threshold": 0, + "version": "", + }) + } + if err := readOspfAreaInterfaceBfd(strings.TrimPrefix(itemTrim, "bfd-liveness-detection "), + interfaceOptions["bfd_liveness_detection"].([]map[string]interface{})[0]); err != nil { + return err + } + case strings.HasPrefix(itemTrim, "dead-interval "): + interfaceOptions["dead_interval"], err = strconv.Atoi( + strings.TrimPrefix(itemTrim, "dead-interval ")) + if err != nil { + return fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case itemTrim == "demand-circuit": + interfaceOptions["demand_circuit"] = true + case itemTrim == disableW: + interfaceOptions["disable"] = true + case itemTrim == "dynamic-neighbors": + interfaceOptions["dynamic_neighbors"] = true + case itemTrim == "flood-reduction": + interfaceOptions["flood_reduction"] = true + case strings.HasPrefix(itemTrim, "hello-interval "): + interfaceOptions["hello_interval"], err = strconv.Atoi( + strings.TrimPrefix(itemTrim, "hello-interval ")) + if err != nil { + return fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case strings.HasPrefix(itemTrim, "interface-type "): + interfaceOptions["interface_type"] = strings.TrimPrefix(itemTrim, "interface-type ") + case strings.HasPrefix(itemTrim, "ipsec-sa "): + interfaceOptions["ipsec_sa"] = strings.Trim(strings.TrimPrefix(itemTrim, "ipsec-sa "), "\"") + case strings.HasPrefix(itemTrim, "ipv4-adjacency-segment protected "): + itemTrimSplit := strings.Split(strings.TrimPrefix( + itemTrim, "ipv4-adjacency-segment protected "), " ") + interfaceOptions["ipv4_adjacency_segment_protected_type"] = itemTrimSplit[0] + if len(itemTrimSplit) > 1 { + interfaceOptions["ipv4_adjacency_segment_protected_value"] = itemTrimSplit[1] + } + case strings.HasPrefix(itemTrim, "ipv4-adjacency-segment unprotected "): + itemTrimSplit := strings.Split(strings.TrimPrefix( + itemTrim, "ipv4-adjacency-segment unprotected "), " ") + interfaceOptions["ipv4_adjacency_segment_unprotected_type"] = itemTrimSplit[0] + if len(itemTrimSplit) > 1 { + interfaceOptions["ipv4_adjacency_segment_unprotected_value"] = itemTrimSplit[1] + } + case itemTrim == "link-protection": + interfaceOptions["link_protection"] = true + case strings.HasPrefix(itemTrim, "metric "): + interfaceOptions["metric"], err = strconv.Atoi( + strings.TrimPrefix(itemTrim, "metric ")) + if err != nil { + return fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case strings.HasPrefix(itemTrim, "mtu "): + interfaceOptions["mtu"], err = strconv.Atoi( + strings.TrimPrefix(itemTrim, "mtu ")) + if err != nil { + return fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case strings.HasPrefix(itemTrim, "neighbor "): + itemTrimSplit := strings.Split(strings.TrimPrefix(itemTrim, "neighbor "), " ") + address := itemTrimSplit[0] + if len(itemTrimSplit) > 1 && itemTrimSplit[1] == "eligible" { + interfaceOptions["neighbor"] = append( + interfaceOptions["neighbor"].([]map[string]interface{}), map[string]interface{}{ + "address": address, + "eligible": true, + }) + } else { + interfaceOptions["neighbor"] = append( + interfaceOptions["neighbor"].([]map[string]interface{}), map[string]interface{}{ + "address": address, + "eligible": false, + }) + } + case itemTrim == "no-advertise-adjacency-segment": + interfaceOptions["no_advertise_adjacency_segment"] = true + case itemTrim == "no-eligible-backup": + interfaceOptions["no_eligible_backup"] = true + case itemTrim == "no-eligible-remote-backup": + interfaceOptions["no_eligible_remote_backup"] = true + case itemTrim == "no-interface-state-traps": + interfaceOptions["no_interface_state_traps"] = true + case itemTrim == "no-neighbor-down-notification": + interfaceOptions["no_neighbor_down_notification"] = true + case itemTrim == "node-link-protection": + interfaceOptions["node_link_protection"] = true + case strings.HasPrefix(itemTrim, "passive"): + interfaceOptions["passive"] = true + switch { + case strings.HasPrefix(itemTrim, "passive traffic-engineering remote-node-id "): + interfaceOptions["passive_traffic_engineering_remote_node_id"] = strings.TrimPrefix( + itemTrim, "passive traffic-engineering remote-node-id ") + case strings.HasPrefix(itemTrim, "passive traffic-engineering remote-node-router-id "): + interfaceOptions["passive_traffic_engineering_remote_node_router_id"] = strings.TrimPrefix( + itemTrim, "passive traffic-engineering remote-node-router-id ") + } + case strings.HasPrefix(itemTrim, "poll-interval "): + interfaceOptions["poll_interval"], err = strconv.Atoi( + strings.TrimPrefix(itemTrim, "poll-interval ")) + if err != nil { + return fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case strings.HasPrefix(itemTrim, "priority "): + interfaceOptions["priority"], err = strconv.Atoi( + strings.TrimPrefix(itemTrim, "priority ")) + if err != nil { + return fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case strings.HasPrefix(itemTrim, "retransmit-interval "): + interfaceOptions["retransmit_interval"], err = strconv.Atoi( + strings.TrimPrefix(itemTrim, "retransmit-interval ")) + if err != nil { + return fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case itemTrim == "secondary": + interfaceOptions["secondary"] = true + case itemTrim == "strict-bfd": + interfaceOptions["strict_bfd"] = true + case strings.HasPrefix(itemTrim, "te-metric "): + interfaceOptions["te_metric"], err = strconv.Atoi( + strings.TrimPrefix(itemTrim, "te-metric ")) + if err != nil { + return fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case strings.HasPrefix(itemTrim, "transit-delay "): + interfaceOptions["transit_delay"], err = strconv.Atoi( + strings.TrimPrefix(itemTrim, "transit-delay ")) + if err != nil { + return fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + } + + return nil +} + func readOspfAreaInterfaceBfd(itemTrim string, bfd map[string]interface{}) error { switch { case strings.HasPrefix(itemTrim, "authentication algorithm "): @@ -1146,34 +1852,85 @@ func readOspfAreaInterfaceBfd(itemTrim string, bfd map[string]interface{}) error return nil } -func delOspfArea(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delOspfArea(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) ospfVersion := ospfV2 if d.Get("version").(string) == "v3" { ospfVersion = ospfV3 + } else if d.Get("realm").(string) != "" { + return fmt.Errorf("realm can't set if version != v3") } - if d.Get("routing_instance").(string) == defaultW { - configSet = append(configSet, "delete protocols "+ospfVersion+" area "+d.Get("area_id").(string)) - } else { + switch { + case d.Get("routing_instance").(string) == defaultW && d.Get("realm").(string) == "": + configSet = append(configSet, deleteW+ + " protocols "+ospfVersion+ + " area "+d.Get("area_id").(string)) + case d.Get("routing_instance").(string) == defaultW && d.Get("realm").(string) != "": + configSet = append(configSet, deleteW+ + " protocols "+ospfVersion+ + " realm "+d.Get("realm").(string)+ + " area "+d.Get("area_id").(string)) + case d.Get("realm").(string) != "": configSet = append(configSet, delRoutingInstances+d.Get("routing_instance").(string)+ - " protocols "+ospfVersion+" area "+d.Get("area_id").(string)) + " protocols "+ospfVersion+ + " realm "+d.Get("realm").(string)+ + " area "+d.Get("area_id").(string)) + default: + configSet = append(configSet, delRoutingInstances+d.Get("routing_instance").(string)+ + " protocols "+ospfVersion+ + " area "+d.Get("area_id").(string)) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillOspfAreaData(d *schema.ResourceData, ospfAreaOptions ospfAreaOptions) { if tfErr := d.Set("area_id", ospfAreaOptions.areaID); tfErr != nil { panic(tfErr) } - if tfErr := d.Set("interface", ospfAreaOptions.interFace); tfErr != nil { + if tfErr := d.Set("routing_instance", ospfAreaOptions.routingInstance); tfErr != nil { panic(tfErr) } - if tfErr := d.Set("routing_instance", ospfAreaOptions.routingInstance); tfErr != nil { + if tfErr := d.Set("realm", ospfAreaOptions.realm); tfErr != nil { panic(tfErr) } if tfErr := d.Set("version", ospfAreaOptions.version); tfErr != nil { panic(tfErr) } + if tfErr := d.Set("interface", ospfAreaOptions.interFace); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("area_range", ospfAreaOptions.areaRange); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("context_identifier", ospfAreaOptions.contextIdentifier); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("inter_area_prefix_export", ospfAreaOptions.interAreaPrefixExport); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("inter_area_prefix_import", ospfAreaOptions.interAreaPrefixImport); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("network_summary_export", ospfAreaOptions.networkSummaryExport); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("network_summary_import", ospfAreaOptions.networkSummaryImport); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set( + "no_context_identifier_advertisement", + ospfAreaOptions.noContextIdentifierAdvertisement, + ); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("nssa", ospfAreaOptions.nssa); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("stub", ospfAreaOptions.stub); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("virtual_link", ospfAreaOptions.virtualLink); tfErr != nil { + panic(tfErr) + } } diff --git a/junos/resource_ospf_area_test.go b/junos/resource_ospf_area_test.go index 2e831f40..e17f0a59 100644 --- a/junos/resource_ospf_area_test.go +++ b/junos/resource_ospf_area_test.go @@ -73,6 +73,48 @@ func TestAccJunosOspfArea_basic(t *testing.T) { ImportState: true, ImportStateVerify: true, }, + { + ResourceName: "junos_ospf_area.testacc_ospfarea2", + ImportState: true, + ImportStateVerify: true, + }, + { + ResourceName: "junos_ospf_area.testacc_ospfareav3ipv4", + ImportState: true, + ImportStateVerify: true, + }, + { + ResourceName: "junos_ospf_area.testacc_ospfarea2v3realm", + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccJunosOspfAreaConfigUpdate2(), + }, + { + Config: testAccJunosOspfAreaConfigUpdate3(), + }, + { + Config: testAccJunosOspfAreaConfigUpdate4(testaccOspfArea), + }, + { + Config: testAccJunosOspfAreaConfigUpdate5(testaccOspfArea), + }, + { + ResourceName: "junos_ospf_area.testacc_ospfarea", + ImportState: true, + ImportStateVerify: true, + }, + { + ResourceName: "junos_ospf_area.testacc_ospfarea2", + ImportState: true, + ImportStateVerify: true, + }, + { + ResourceName: "junos_ospf_area.testacc_ospfarea3", + ImportState: true, + ImportStateVerify: true, + }, }, }) } @@ -96,6 +138,19 @@ resource "junos_ospf_area" "testacc_ospfarea" { secondary = true } } +resource "junos_ospf_area" "testacc_ospfareav3ipv4" { + area_id = "0.0.0.0" + version = "v3" + realm = "ipv4-unicast" + interface { + name = "all" + disable = true + } + interface { + name = junos_interface_logical.testacc_ospfarea.name + secondary = true + } +} resource "junos_interface_logical" "testacc_ospfarea" { name = "%s.0" description = "testacc_ospfarea" @@ -145,6 +200,35 @@ resource "junos_ospf_area" "testacc_ospfarea" { eligible = "true" } } + network_summary_export = [junos_policyoptions_policy_statement.testacc_ospfarea.name] + network_summary_import = [junos_policyoptions_policy_statement.testacc_ospfarea2.name] +} +resource "junos_policyoptions_policy_statement" "testacc_ospfarea" { + name = "testacc_ospfarea" + then { + action = "accept" + } +} +resource "junos_policyoptions_policy_statement" "testacc_ospfarea2" { + name = "testacc_ospfarea2" + then { + action = "reject" + } +} +resource "junos_ospf_area" "testacc_ospfareav3ipv4" { + area_id = "0.0.0.0" + version = "v3" + realm = "ipv4-unicast" + interface { + name = junos_interface_logical.testacc_ospfarea.name + priority = 0 + bfd_liveness_detection { + full_neighbors_only = true + minimum_receive_interval = 27 + transmit_interval_minimum_interval = 50 + transmit_interval_threshold = 51 + } + } } resource "junos_interface_logical" "testacc_ospfarea" { name = "%s.0" @@ -207,5 +291,206 @@ resource "junos_ospf_area" "testacc_ospfarea2" { } } } +resource "junos_ospf_area" "testacc_ospfarea2v3realm" { + area_id = "0.0.0.0" + version = "v3" + realm = "ipv4-multicast" + routing_instance = junos_routing_instance.testacc_ospfarea.name + interface { + name = "all" + passive = true + } + interface { + name = junos_interface_logical.testacc_ospfarea2.name + bfd_liveness_detection { + version = "automatic" + minimum_receive_interval = 270 + transmit_interval_minimum_interval = 500 + transmit_interval_threshold = 510 + } + } +} `, interFace, interFace2) } + +func testAccJunosOspfAreaConfigUpdate2() string { + return ` +resource "junos_ospf_area" "testacc_ospfarea" { + area_id = "0.0.0.0" + version = "v3" + interface { + name = "all" + disable = true + } + virtual_link { + neighbor_id = "192.0.2.0" + transit_area = "192.0.2.1" + } +} +` +} + +func testAccJunosOspfAreaConfigUpdate3() string { + return ` +resource "junos_ospf_area" "testacc_ospfarea" { + area_id = "0.0.0.0" + version = "v3" + interface { + name = "all" + disable = true + } + virtual_link { + neighbor_id = "192.0.2.100" + transit_area = "192.0.2.101" + dead_interval = 102 + demand_circuit = true + disable = true + flood_reduction = true + hello_interval = 103 + mtu = 1040 + retransmit_interval = 105 + transit_delay = 106 + + } + virtual_link { + neighbor_id = "192.0.2.0" + transit_area = "192.0.2.1" + } +} +` +} + +func testAccJunosOspfAreaConfigUpdate4(interFace string) string { + return fmt.Sprintf(` +resource "junos_ospf_area" "testacc_ospfarea" { + area_id = "1" + version = "v3" + interface { + name = "all" + } + area_range { + range = "fe80::/64" + } + no_context_identifier_advertisement = true + inter_area_prefix_export = [ + junos_policyoptions_policy_statement.testacc_ospfarea2.name, + ] + inter_area_prefix_import = [ + junos_policyoptions_policy_statement.testacc_ospfarea.name, + ] + nssa {} +} +resource "junos_policyoptions_policy_statement" "testacc_ospfarea" { + name = "testacc_ospfarea" + then { + action = "accept" + } +} +resource "junos_policyoptions_policy_statement" "testacc_ospfarea2" { + name = "testacc_ospfarea2" + then { + action = "reject" + } +} +resource "junos_ospf_area" "testacc_ospfarea2" { + area_id = "2" + version = "v3" + interface { + name = "%s.0" + passive = true + } + stub {} +} +`, interFace) +} + +func testAccJunosOspfAreaConfigUpdate5(interFace string) string { + return fmt.Sprintf(` +resource "junos_ospf_area" "testacc_ospfarea" { + area_id = "1" + version = "v3" + interface { + name = "all" + } + area_range { + range = "fe80:f::/64" + exact = true + } + area_range { + range = "fe80:e::/64" + exact = true + override_metric = 106 + } + area_range { + range = "fe80::/64" + restrict = true + } + context_identifier = ["127.0.0.2", "127.0.0.1"] + inter_area_prefix_export = [ + junos_policyoptions_policy_statement.testacc_ospfarea.name, + junos_policyoptions_policy_statement.testacc_ospfarea2.name, + ] + inter_area_prefix_import = [ + junos_policyoptions_policy_statement.testacc_ospfarea2.name, + junos_policyoptions_policy_statement.testacc_ospfarea.name, + ] + nssa { + area_range { + range = "fe80::/64" + exact = true + } + area_range { + range = "fe80:b::/64" + override_metric = 107 + } + area_range { + range = "fe80:a::/64" + restrict = true + } + default_lsa { + default_metric = 109 + metric_type = 2 + type_7 = true + } + summaries = true + } +} +resource "junos_policyoptions_policy_statement" "testacc_ospfarea" { + name = "testacc_ospfarea" + then { + action = "accept" + } +} +resource "junos_policyoptions_policy_statement" "testacc_ospfarea2" { + name = "testacc_ospfarea2" + then { + action = "reject" + } +} +resource "junos_ospf_area" "testacc_ospfarea2" { + area_id = "2" + version = "v3" + interface { + name = "%s.0" + passive = true + } + stub { + default_metric = 150 + no_summaries = true + } +} +resource "junos_ospf_area" "testacc_ospfarea3" { + area_id = "3" + version = "v3" + realm = "ipv4-unicast" + interface { + name = "%s.0" + passive = true + } + nssa { + no_summaries = true + default_lsa {} + } +} +`, interFace, interFace) +} diff --git a/junos/resource_policyoptions_as_path.go b/junos/resource_policyoptions_as_path.go index 969e210d..99c92bdf 100644 --- a/junos/resource_policyoptions_as_path.go +++ b/junos/resource_policyoptions_as_path.go @@ -44,50 +44,50 @@ func resourcePolicyoptionsAsPath() *schema.Resource { } func resourcePolicyoptionsAsPathCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setPolicyoptionsAsPath(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setPolicyoptionsAsPath(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - policyoptsAsPathExists, err := checkPolicyoptionsAsPathExists(d.Get("name").(string), m, jnprSess) + policyoptsAsPathExists, err := checkPolicyoptionsAsPathExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if policyoptsAsPathExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("policy-options as-path %v already exists", d.Get("name").(string)))...) } - if err := setPolicyoptionsAsPath(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setPolicyoptionsAsPath(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_policyoptions_as_path", jnprSess) + warns, err := clt.commitConf("create resource junos_policyoptions_as_path", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - policyoptsAsPathExists, err = checkPolicyoptionsAsPathExists(d.Get("name").(string), m, jnprSess) + policyoptsAsPathExists, err = checkPolicyoptionsAsPathExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -98,24 +98,24 @@ func resourcePolicyoptionsAsPathCreate(ctx context.Context, d *schema.ResourceDa "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourcePolicyoptionsAsPathReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourcePolicyoptionsAsPathReadWJunSess(d, clt, junSess)...) } func resourcePolicyoptionsAsPathRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourcePolicyoptionsAsPathReadWJnprSess(d, m, jnprSess) + return resourcePolicyoptionsAsPathReadWJunSess(d, clt, junSess) } -func resourcePolicyoptionsAsPathReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourcePolicyoptionsAsPathReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - asPathOptions, err := readPolicyoptionsAsPath(d.Get("name").(string), m, jnprSess) + asPathOptions, err := readPolicyoptionsAsPath(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -131,76 +131,76 @@ func resourcePolicyoptionsAsPathReadWJnprSess(d *schema.ResourceData, m interfac func resourcePolicyoptionsAsPathUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delPolicyoptionsAsPath(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delPolicyoptionsAsPath(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setPolicyoptionsAsPath(d, m, nil); err != nil { + if err := setPolicyoptionsAsPath(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delPolicyoptionsAsPath(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delPolicyoptionsAsPath(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setPolicyoptionsAsPath(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setPolicyoptionsAsPath(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_policyoptions_as_path", jnprSess) + warns, err := clt.commitConf("update resource junos_policyoptions_as_path", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourcePolicyoptionsAsPathReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourcePolicyoptionsAsPathReadWJunSess(d, clt, junSess)...) } func resourcePolicyoptionsAsPathDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delPolicyoptionsAsPath(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delPolicyoptionsAsPath(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delPolicyoptionsAsPath(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delPolicyoptionsAsPath(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_policyoptions_as_path", jnprSess) + warns, err := clt.commitConf("delete resource junos_policyoptions_as_path", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -210,22 +210,22 @@ func resourcePolicyoptionsAsPathDelete(ctx context.Context, d *schema.ResourceDa func resourcePolicyoptionsAsPathImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - policyoptsAsPathExists, err := checkPolicyoptionsAsPathExists(d.Id(), m, jnprSess) + policyoptsAsPathExists, err := checkPolicyoptionsAsPathExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !policyoptsAsPathExists { return nil, fmt.Errorf("don't find policy-options as-path with id '%v' (id must be )", d.Id()) } - asPathOptions, err := readPolicyoptionsAsPath(d.Id(), m, jnprSess) + asPathOptions, err := readPolicyoptionsAsPath(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -236,9 +236,8 @@ func resourcePolicyoptionsAsPathImport(ctx context.Context, d *schema.ResourceDa return result, nil } -func checkPolicyoptionsAsPathExists(name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"policy-options as-path "+name+pipeDisplaySet, jnprSess) +func checkPolicyoptionsAsPathExists(name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"policy-options as-path "+name+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -249,8 +248,7 @@ func checkPolicyoptionsAsPathExists(name string, m interface{}, jnprSess *Netcon return true, nil } -func setPolicyoptionsAsPath(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setPolicyoptionsAsPath(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) if d.Get("dynamic_db").(bool) { @@ -262,14 +260,13 @@ func setPolicyoptionsAsPath(d *schema.ResourceData, m interface{}, jnprSess *Net " \""+d.Get("path").(string)+"\"") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readPolicyoptionsAsPath(name string, m interface{}, jnprSess *NetconfObject) (asPathOptions, error) { - sess := m.(*Session) +func readPolicyoptionsAsPath(name string, clt *Client, junSess *junosSession) (asPathOptions, error) { var confRead asPathOptions - showConfig, err := sess.command(cmdShowConfig+"policy-options as-path "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"policy-options as-path "+name+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -295,12 +292,11 @@ func readPolicyoptionsAsPath(name string, m interface{}, jnprSess *NetconfObject return confRead, nil } -func delPolicyoptionsAsPath(asPath string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delPolicyoptionsAsPath(asPath string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete policy-options as-path "+asPath) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillPolicyoptionsAsPathData(d *schema.ResourceData, asPathOptions asPathOptions) { diff --git a/junos/resource_policyoptions_as_path_group.go b/junos/resource_policyoptions_as_path_group.go index 7d2744f1..233668c4 100644 --- a/junos/resource_policyoptions_as_path_group.go +++ b/junos/resource_policyoptions_as_path_group.go @@ -59,50 +59,50 @@ func resourcePolicyoptionsAsPathGroup() *schema.Resource { func resourcePolicyoptionsAsPathGroupCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setPolicyoptionsAsPathGroup(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setPolicyoptionsAsPathGroup(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - policyoptsAsPathGroupExists, err := checkPolicyoptionsAsPathGroupExists(d.Get("name").(string), m, jnprSess) + policyoptsAsPathGroupExists, err := checkPolicyoptionsAsPathGroupExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if policyoptsAsPathGroupExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("policy-options as-path-group %v already exists", d.Get("name").(string)))...) } - if err := setPolicyoptionsAsPathGroup(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setPolicyoptionsAsPathGroup(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_policyoptions_as_path_group", jnprSess) + warns, err := clt.commitConf("create resource junos_policyoptions_as_path_group", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - policyoptsAsPathGroupExists, err = checkPolicyoptionsAsPathGroupExists(d.Get("name").(string), m, jnprSess) + policyoptsAsPathGroupExists, err = checkPolicyoptionsAsPathGroupExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -113,24 +113,24 @@ func resourcePolicyoptionsAsPathGroupCreate(ctx context.Context, d *schema.Resou "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourcePolicyoptionsAsPathGroupReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourcePolicyoptionsAsPathGroupReadWJunSess(d, clt, junSess)...) } func resourcePolicyoptionsAsPathGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourcePolicyoptionsAsPathGroupReadWJnprSess(d, m, jnprSess) + return resourcePolicyoptionsAsPathGroupReadWJunSess(d, clt, junSess) } -func resourcePolicyoptionsAsPathGroupReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourcePolicyoptionsAsPathGroupReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - asPathGroupOptions, err := readPolicyoptionsAsPathGroup(d.Get("name").(string), m, jnprSess) + asPathGroupOptions, err := readPolicyoptionsAsPathGroup(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -147,77 +147,77 @@ func resourcePolicyoptionsAsPathGroupReadWJnprSess(d *schema.ResourceData, m int func resourcePolicyoptionsAsPathGroupUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delPolicyoptionsAsPathGroup(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delPolicyoptionsAsPathGroup(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setPolicyoptionsAsPathGroup(d, m, nil); err != nil { + if err := setPolicyoptionsAsPathGroup(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delPolicyoptionsAsPathGroup(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delPolicyoptionsAsPathGroup(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setPolicyoptionsAsPathGroup(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setPolicyoptionsAsPathGroup(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_policyoptions_as_path_group", jnprSess) + warns, err := clt.commitConf("update resource junos_policyoptions_as_path_group", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourcePolicyoptionsAsPathGroupReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourcePolicyoptionsAsPathGroupReadWJunSess(d, clt, junSess)...) } func resourcePolicyoptionsAsPathGroupDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delPolicyoptionsAsPathGroup(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delPolicyoptionsAsPathGroup(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delPolicyoptionsAsPathGroup(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delPolicyoptionsAsPathGroup(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_policyoptions_as_path_group", jnprSess) + warns, err := clt.commitConf("delete resource junos_policyoptions_as_path_group", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -227,22 +227,22 @@ func resourcePolicyoptionsAsPathGroupDelete(ctx context.Context, d *schema.Resou func resourcePolicyoptionsAsPathGroupImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - policyoptsAsPathGroupExists, err := checkPolicyoptionsAsPathGroupExists(d.Id(), m, jnprSess) + policyoptsAsPathGroupExists, err := checkPolicyoptionsAsPathGroupExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !policyoptsAsPathGroupExists { return nil, fmt.Errorf("don't find policy-options as-path-group with id '%v' (id must be )", d.Id()) } - asPathGroupOptions, err := readPolicyoptionsAsPathGroup(d.Id(), m, jnprSess) + asPathGroupOptions, err := readPolicyoptionsAsPathGroup(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -253,10 +253,9 @@ func resourcePolicyoptionsAsPathGroupImport(ctx context.Context, d *schema.Resou return result, nil } -func checkPolicyoptionsAsPathGroupExists(name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "policy-options as-path-group "+name+pipeDisplaySet, jnprSess) +func checkPolicyoptionsAsPathGroupExists(name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "policy-options as-path-group "+name+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -267,8 +266,7 @@ func checkPolicyoptionsAsPathGroupExists(name string, m interface{}, jnprSess *N return true, nil } -func setPolicyoptionsAsPathGroup(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setPolicyoptionsAsPathGroup(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set policy-options as-path-group " + d.Get("name").(string) @@ -287,15 +285,14 @@ func setPolicyoptionsAsPathGroup(d *schema.ResourceData, m interface{}, jnprSess configSet = append(configSet, setPrefix+" dynamic-db") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readPolicyoptionsAsPathGroup(name string, m interface{}, jnprSess *NetconfObject) (asPathGroupOptions, error) { - sess := m.(*Session) +func readPolicyoptionsAsPathGroup(name string, clt *Client, junSess *junosSession) (asPathGroupOptions, error) { var confRead asPathGroupOptions - showConfig, err := sess.command(cmdShowConfig+ - "policy-options as-path-group "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "policy-options as-path-group "+name+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -329,12 +326,11 @@ func readPolicyoptionsAsPathGroup(name string, m interface{}, jnprSess *NetconfO return confRead, nil } -func delPolicyoptionsAsPathGroup(asPathGroup string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delPolicyoptionsAsPathGroup(asPathGroup string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete policy-options as-path-group "+asPathGroup) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillPolicyoptionsAsPathGroupData(d *schema.ResourceData, asPathGroupOptions asPathGroupOptions) { diff --git a/junos/resource_policyoptions_community.go b/junos/resource_policyoptions_community.go index ac6dcb3a..9fc9eb8a 100644 --- a/junos/resource_policyoptions_community.go +++ b/junos/resource_policyoptions_community.go @@ -46,50 +46,50 @@ func resourcePolicyoptionsCommunity() *schema.Resource { } func resourcePolicyoptionsCommunityCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setPolicyoptionsCommunity(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setPolicyoptionsCommunity(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - policyoptsCommunityExists, err := checkPolicyoptionsCommunityExists(d.Get("name").(string), m, jnprSess) + policyoptsCommunityExists, err := checkPolicyoptionsCommunityExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if policyoptsCommunityExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("policy-options community %v already exists", d.Get("name").(string)))...) } - if err := setPolicyoptionsCommunity(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setPolicyoptionsCommunity(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_policyoptions_community", jnprSess) + warns, err := clt.commitConf("create resource junos_policyoptions_community", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - policyoptsCommunityExists, err = checkPolicyoptionsCommunityExists(d.Get("name").(string), m, jnprSess) + policyoptsCommunityExists, err = checkPolicyoptionsCommunityExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -100,24 +100,24 @@ func resourcePolicyoptionsCommunityCreate(ctx context.Context, d *schema.Resourc "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourcePolicyoptionsCommunityReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourcePolicyoptionsCommunityReadWJunSess(d, clt, junSess)...) } func resourcePolicyoptionsCommunityRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourcePolicyoptionsCommunityReadWJnprSess(d, m, jnprSess) + return resourcePolicyoptionsCommunityReadWJunSess(d, clt, junSess) } -func resourcePolicyoptionsCommunityReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourcePolicyoptionsCommunityReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - communityOptions, err := readPolicyoptionsCommunity(d.Get("name").(string), m, jnprSess) + communityOptions, err := readPolicyoptionsCommunity(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -133,76 +133,76 @@ func resourcePolicyoptionsCommunityReadWJnprSess(d *schema.ResourceData, m inter func resourcePolicyoptionsCommunityUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delPolicyoptionsCommunity(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delPolicyoptionsCommunity(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setPolicyoptionsCommunity(d, m, nil); err != nil { + if err := setPolicyoptionsCommunity(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delPolicyoptionsCommunity(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delPolicyoptionsCommunity(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setPolicyoptionsCommunity(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setPolicyoptionsCommunity(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_policyoptions_community", jnprSess) + warns, err := clt.commitConf("update resource junos_policyoptions_community", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourcePolicyoptionsCommunityReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourcePolicyoptionsCommunityReadWJunSess(d, clt, junSess)...) } func resourcePolicyoptionsCommunityDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delPolicyoptionsCommunity(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delPolicyoptionsCommunity(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delPolicyoptionsCommunity(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delPolicyoptionsCommunity(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_policyoptions_community", jnprSess) + warns, err := clt.commitConf("delete resource junos_policyoptions_community", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -212,22 +212,22 @@ func resourcePolicyoptionsCommunityDelete(ctx context.Context, d *schema.Resourc func resourcePolicyoptionsCommunityImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - policyoptsCommunityExists, err := checkPolicyoptionsCommunityExists(d.Id(), m, jnprSess) + policyoptsCommunityExists, err := checkPolicyoptionsCommunityExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !policyoptsCommunityExists { return nil, fmt.Errorf("don't find policy-options community with id '%v' (id must be )", d.Id()) } - communityOptions, err := readPolicyoptionsCommunity(d.Id(), m, jnprSess) + communityOptions, err := readPolicyoptionsCommunity(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -238,9 +238,8 @@ func resourcePolicyoptionsCommunityImport(ctx context.Context, d *schema.Resourc return result, nil } -func checkPolicyoptionsCommunityExists(name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"policy-options community "+name+pipeDisplaySet, jnprSess) +func checkPolicyoptionsCommunityExists(name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"policy-options community "+name+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -251,8 +250,7 @@ func checkPolicyoptionsCommunityExists(name string, m interface{}, jnprSess *Net return true, nil } -func setPolicyoptionsCommunity(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setPolicyoptionsCommunity(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set policy-options community " + d.Get("name").(string) + " " @@ -263,15 +261,14 @@ func setPolicyoptionsCommunity(d *schema.ResourceData, m interface{}, jnprSess * configSet = append(configSet, setPrefix+"invert-match") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readPolicyoptionsCommunity(name string, m interface{}, jnprSess *NetconfObject) (communityOptions, error) { - sess := m.(*Session) +func readPolicyoptionsCommunity(name string, clt *Client, junSess *junosSession) (communityOptions, error) { var confRead communityOptions - showConfig, err := sess.command(cmdShowConfig+ - "policy-options community "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "policy-options community "+name+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -297,12 +294,11 @@ func readPolicyoptionsCommunity(name string, m interface{}, jnprSess *NetconfObj return confRead, nil } -func delPolicyoptionsCommunity(community string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delPolicyoptionsCommunity(community string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete policy-options community "+community) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillPolicyoptionsCommunityData(d *schema.ResourceData, communityOptions communityOptions) { diff --git a/junos/resource_policyoptions_policy_statement.go b/junos/resource_policyoptions_policy_statement.go index 2083eeb8..53b011ab 100644 --- a/junos/resource_policyoptions_policy_statement.go +++ b/junos/resource_policyoptions_policy_statement.go @@ -437,13 +437,13 @@ func schemaPolicyoptionsPolicyStatementTo() map[string]*schema.Schema { func resourcePolicyoptionsPolicyStatementCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setPolicyStatement(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setPolicyStatement(d, clt, nil); err != nil { return diag.FromErr(err) } if d.Get("add_it_to_forwarding_table_export").(bool) { - if err := setPolicyStatementFwTableExport(d.Get("name").(string), m, nil); err != nil { + if err := setPolicyStatementFwTableExport(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } } @@ -451,48 +451,48 @@ func resourcePolicyoptionsPolicyStatementCreate(ctx context.Context, d *schema.R return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - policyStatementExists, err := checkPolicyStatementExists(d.Get("name").(string), m, jnprSess) + policyStatementExists, err := checkPolicyStatementExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if policyStatementExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("policy-options policy-statement %v already exists", d.Get("name").(string)))...) } - if err := setPolicyStatement(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setPolicyStatement(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if d.Get("add_it_to_forwarding_table_export").(bool) { - if err := setPolicyStatementFwTableExport(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setPolicyStatementFwTableExport(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } } - warns, err := sess.commitConf("create resource junos_policyoptions_policy_statement", jnprSess) + warns, err := clt.commitConf("create resource junos_policyoptions_policy_statement", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - policyStatementExists, err = checkPolicyStatementExists(d.Get("name").(string), m, jnprSess) + policyStatementExists, err = checkPolicyStatementExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -503,32 +503,32 @@ func resourcePolicyoptionsPolicyStatementCreate(ctx context.Context, d *schema.R "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourcePolicyoptionsPolicyStatementReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourcePolicyoptionsPolicyStatementReadWJunSess(d, clt, junSess)...) } func resourcePolicyoptionsPolicyStatementRead(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourcePolicyoptionsPolicyStatementReadWJnprSess(d, m, jnprSess) + return resourcePolicyoptionsPolicyStatementReadWJunSess(d, clt, junSess) } -func resourcePolicyoptionsPolicyStatementReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourcePolicyoptionsPolicyStatementReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - policyStatementOptions, err := readPolicyStatement(d.Get("name").(string), m, jnprSess) + policyStatementOptions, err := readPolicyStatement(d.Get("name").(string), clt, junSess) if err != nil { mutex.Unlock() return diag.FromErr(err) } if d.Get("add_it_to_forwarding_table_export").(bool) { - export, err := readPolicyStatementFwTableExport(d.Get("name").(string), m, jnprSess) + export, err := readPolicyStatementFwTableExport(d.Get("name").(string), clt, junSess) if err != nil { mutex.Unlock() @@ -554,23 +554,23 @@ func resourcePolicyoptionsPolicyStatementReadWJnprSess(d *schema.ResourceData, m func resourcePolicyoptionsPolicyStatementUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delPolicyStatement(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delPolicyStatement(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } if d.HasChange("add_it_to_forwarding_table_export") { if o, _ := d.GetChange("add_it_to_forwarding_table_export"); o.(bool) { - if err := delPolicyStatementFwTableExport(d.Get("name").(string), m, nil); err != nil { + if err := delPolicyStatementFwTableExport(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } } } - if err := setPolicyStatement(d, m, nil); err != nil { + if err := setPolicyStatement(d, clt, nil); err != nil { return diag.FromErr(err) } if d.Get("add_it_to_forwarding_table_export").(bool) { - if err := setPolicyStatementFwTableExport(d.Get("name").(string), m, nil); err != nil { + if err := setPolicyStatementFwTableExport(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } } @@ -578,93 +578,93 @@ func resourcePolicyoptionsPolicyStatementUpdate(ctx context.Context, d *schema.R return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delPolicyStatement(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delPolicyStatement(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if d.HasChange("add_it_to_forwarding_table_export") { if o, _ := d.GetChange("add_it_to_forwarding_table_export"); o.(bool) { - if err := delPolicyStatementFwTableExport(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delPolicyStatementFwTableExport(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } } } - if err := setPolicyStatement(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setPolicyStatement(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if d.Get("add_it_to_forwarding_table_export").(bool) { - if err := setPolicyStatementFwTableExport(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setPolicyStatementFwTableExport(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } } - warns, err := sess.commitConf("update resource junos_policyoptions_policy_statement", jnprSess) + warns, err := clt.commitConf("update resource junos_policyoptions_policy_statement", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourcePolicyoptionsPolicyStatementReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourcePolicyoptionsPolicyStatementReadWJunSess(d, clt, junSess)...) } func resourcePolicyoptionsPolicyStatementDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delPolicyStatement(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delPolicyStatement(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } if d.Get("add_it_to_forwarding_table_export").(bool) { - if err := delPolicyStatementFwTableExport(d.Get("name").(string), m, nil); err != nil { + if err := delPolicyStatementFwTableExport(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delPolicyStatement(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delPolicyStatement(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if d.Get("add_it_to_forwarding_table_export").(bool) { - if err := delPolicyStatementFwTableExport(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delPolicyStatementFwTableExport(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } } - warns, err := sess.commitConf("delete resource junos_policyoptions_policy_statement", jnprSess) + warns, err := clt.commitConf("delete resource junos_policyoptions_policy_statement", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -674,22 +674,22 @@ func resourcePolicyoptionsPolicyStatementDelete(ctx context.Context, d *schema.R func resourcePolicyoptionsPolicyStatementImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - policyStatementExists, err := checkPolicyStatementExists(d.Id(), m, jnprSess) + policyStatementExists, err := checkPolicyStatementExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !policyStatementExists { return nil, fmt.Errorf("don't find policy-options policy-statement with id '%v' (id must be )", d.Id()) } - policyStatementOptions, err := readPolicyStatement(d.Id(), m, jnprSess) + policyStatementOptions, err := readPolicyStatement(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -700,10 +700,9 @@ func resourcePolicyoptionsPolicyStatementImport(ctx context.Context, d *schema.R return result, nil } -func checkPolicyStatementExists(name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "policy-options policy-statement "+name+pipeDisplaySet, jnprSess) +func checkPolicyStatementExists(name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "policy-options policy-statement "+name+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -714,8 +713,7 @@ func checkPolicyStatementExists(name string, m interface{}, jnprSess *NetconfObj return true, nil } -func setPolicyStatement(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setPolicyStatement(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set policy-options policy-statement " + d.Get("name").(string) @@ -771,22 +769,20 @@ func setPolicyStatement(d *schema.ResourceData, m interface{}, jnprSess *Netconf } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func setPolicyStatementFwTableExport(policyName string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setPolicyStatementFwTableExport(policyName string, clt *Client, junSess *junosSession) error { configSet := []string{"set routing-options forwarding-table export " + policyName} - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readPolicyStatement(name string, m interface{}, jnprSess *NetconfObject) (policyStatementOptions, error) { - sess := m.(*Session) +func readPolicyStatement(name string, clt *Client, junSess *junosSession) (policyStatementOptions, error) { var confRead policyStatementOptions - showConfig, err := sess.command(cmdShowConfig+ - "policy-options policy-statement "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "policy-options policy-statement "+name+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -869,11 +865,10 @@ func readPolicyStatement(name string, m interface{}, jnprSess *NetconfObject) (p return confRead, nil } -func readPolicyStatementFwTableExport(policyName string, m interface{}, jnprSess *NetconfObject, +func readPolicyStatementFwTableExport(policyName string, clt *Client, junSess *junosSession, ) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "routing-options forwarding-table export"+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "routing-options forwarding-table export"+pipeDisplaySetRelative, junSess) if err != nil { return false, err } @@ -896,18 +891,16 @@ func readPolicyStatementFwTableExport(policyName string, m interface{}, jnprSess return false, nil } -func delPolicyStatement(policyName string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delPolicyStatement(policyName string, clt *Client, junSess *junosSession) error { configSet := []string{"delete policy-options policy-statement " + policyName} - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func delPolicyStatementFwTableExport(policyName string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delPolicyStatementFwTableExport(policyName string, clt *Client, junSess *junosSession) error { configSet := []string{"delete routing-options forwarding-table export " + policyName} - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillPolicyStatementData(d *schema.ResourceData, policyStatementOptions policyStatementOptions) { diff --git a/junos/resource_policyoptions_prefix_list.go b/junos/resource_policyoptions_prefix_list.go index 3eff7b5b..14a64067 100644 --- a/junos/resource_policyoptions_prefix_list.go +++ b/junos/resource_policyoptions_prefix_list.go @@ -54,50 +54,50 @@ func resourcePolicyoptionsPrefixList() *schema.Resource { func resourcePolicyoptionsPrefixListCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setPolicyoptionsPrefixList(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setPolicyoptionsPrefixList(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - policyoptsPrefixListExists, err := checkPolicyoptionsPrefixListExists(d.Get("name").(string), m, jnprSess) + policyoptsPrefixListExists, err := checkPolicyoptionsPrefixListExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if policyoptsPrefixListExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("policy-options prefix-list %v already exists", d.Get("name").(string)))...) } - if err := setPolicyoptionsPrefixList(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setPolicyoptionsPrefixList(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_policyoptions_prefix_list", jnprSess) + warns, err := clt.commitConf("create resource junos_policyoptions_prefix_list", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - policyoptsPrefixListExists, err = checkPolicyoptionsPrefixListExists(d.Get("name").(string), m, jnprSess) + policyoptsPrefixListExists, err = checkPolicyoptionsPrefixListExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -108,24 +108,24 @@ func resourcePolicyoptionsPrefixListCreate(ctx context.Context, d *schema.Resour "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourcePolicyoptionsPrefixListReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourcePolicyoptionsPrefixListReadWJunSess(d, clt, junSess)...) } func resourcePolicyoptionsPrefixListRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourcePolicyoptionsPrefixListReadWJnprSess(d, m, jnprSess) + return resourcePolicyoptionsPrefixListReadWJunSess(d, clt, junSess) } -func resourcePolicyoptionsPrefixListReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourcePolicyoptionsPrefixListReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - prefixListOptions, err := readPolicyoptionsPrefixList(d.Get("name").(string), m, jnprSess) + prefixListOptions, err := readPolicyoptionsPrefixList(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -142,77 +142,77 @@ func resourcePolicyoptionsPrefixListReadWJnprSess(d *schema.ResourceData, m inte func resourcePolicyoptionsPrefixListUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delPolicyoptionsPrefixList(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delPolicyoptionsPrefixList(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setPolicyoptionsPrefixList(d, m, nil); err != nil { + if err := setPolicyoptionsPrefixList(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delPolicyoptionsPrefixList(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delPolicyoptionsPrefixList(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setPolicyoptionsPrefixList(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setPolicyoptionsPrefixList(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_policyoptions_prefix_list", jnprSess) + warns, err := clt.commitConf("update resource junos_policyoptions_prefix_list", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourcePolicyoptionsPrefixListReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourcePolicyoptionsPrefixListReadWJunSess(d, clt, junSess)...) } func resourcePolicyoptionsPrefixListDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delPolicyoptionsPrefixList(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delPolicyoptionsPrefixList(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delPolicyoptionsPrefixList(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delPolicyoptionsPrefixList(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_policyoptions_prefix_list", jnprSess) + warns, err := clt.commitConf("delete resource junos_policyoptions_prefix_list", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -222,22 +222,22 @@ func resourcePolicyoptionsPrefixListDelete(ctx context.Context, d *schema.Resour func resourcePolicyoptionsPrefixListImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - policyoptsPrefixListExists, err := checkPolicyoptionsPrefixListExists(d.Id(), m, jnprSess) + policyoptsPrefixListExists, err := checkPolicyoptionsPrefixListExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !policyoptsPrefixListExists { return nil, fmt.Errorf("don't find policy-options prefix-list with id '%v' (id must be )", d.Id()) } - prefixListOptions, err := readPolicyoptionsPrefixList(d.Id(), m, jnprSess) + prefixListOptions, err := readPolicyoptionsPrefixList(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -248,9 +248,8 @@ func resourcePolicyoptionsPrefixListImport(ctx context.Context, d *schema.Resour return result, nil } -func checkPolicyoptionsPrefixListExists(name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"policy-options prefix-list "+name+pipeDisplaySet, jnprSess) +func checkPolicyoptionsPrefixListExists(name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"policy-options prefix-list "+name+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -261,8 +260,7 @@ func checkPolicyoptionsPrefixListExists(name string, m interface{}, jnprSess *Ne return true, nil } -func setPolicyoptionsPrefixList(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setPolicyoptionsPrefixList(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set policy-options prefix-list " + d.Get("name").(string) @@ -279,15 +277,14 @@ func setPolicyoptionsPrefixList(d *schema.ResourceData, m interface{}, jnprSess configSet = append(configSet, setPrefix+" "+v) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readPolicyoptionsPrefixList(name string, m interface{}, jnprSess *NetconfObject) (prefixListOptions, error) { - sess := m.(*Session) +func readPolicyoptionsPrefixList(name string, clt *Client, junSess *junosSession) (prefixListOptions, error) { var confRead prefixListOptions - showConfig, err := sess.command(cmdShowConfig+ - "policy-options prefix-list "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "policy-options prefix-list "+name+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -317,12 +314,11 @@ func readPolicyoptionsPrefixList(name string, m interface{}, jnprSess *NetconfOb return confRead, nil } -func delPolicyoptionsPrefixList(prefixList string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delPolicyoptionsPrefixList(prefixList string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete policy-options prefix-list "+prefixList) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillPolicyoptionsPrefixListData(d *schema.ResourceData, prefixListOptions prefixListOptions) { diff --git a/junos/resource_rib_group.go b/junos/resource_rib_group.go index f09794bf..18039c47 100644 --- a/junos/resource_rib_group.go +++ b/junos/resource_rib_group.go @@ -57,48 +57,48 @@ func resourceRibGroupCreate(ctx context.Context, d *schema.ResourceData, m inter if err := validateRibGroup(d); err != nil { return diag.FromErr(err) } - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setRibGroup(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setRibGroup(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - ribGroupExists, err := checkRibGroupExists(d.Get("name").(string), m, jnprSess) + ribGroupExists, err := checkRibGroupExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if ribGroupExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("rib-group %v already exists", d.Get("name").(string)))...) } - if err := setRibGroup(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setRibGroup(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_rib_group", jnprSess) + warns, err := clt.commitConf("create resource junos_rib_group", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - ribGroupExists, err = checkRibGroupExists(d.Get("name").(string), m, jnprSess) + ribGroupExists, err = checkRibGroupExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -109,23 +109,23 @@ func resourceRibGroupCreate(ctx context.Context, d *schema.ResourceData, m inter "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceRibGroupReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceRibGroupReadWJunSess(d, clt, junSess)...) } func resourceRibGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceRibGroupReadWJnprSess(d, m, jnprSess) + return resourceRibGroupReadWJunSess(d, clt, junSess) } -func resourceRibGroupReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) diag.Diagnostics { +func resourceRibGroupReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession) diag.Diagnostics { mutex.Lock() - ribGroupOptions, err := readRibGroup(d.Get("name").(string), m, jnprSess) + ribGroupOptions, err := readRibGroup(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -144,107 +144,104 @@ func resourceRibGroupUpdate(ctx context.Context, d *schema.ResourceData, m inter if err := validateRibGroup(d); err != nil { return diag.FromErr(err) } - sess := m.(*Session) - if sess.junosFakeUpdateAlso { + clt := m.(*Client) + if clt.fakeUpdateAlso { if d.HasChange("import_policy") { - if err := delRibGroupElement("import-policy", d.Get("name").(string), m, nil); err != nil { + if err := delRibGroupElement("import-policy", d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } } if d.HasChange("import_rib") { - if err := delRibGroupElement("import-rib", d.Get("name").(string), m, nil); err != nil { + if err := delRibGroupElement("import-rib", d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } } if d.HasChange("export_rib") { - if err := delRibGroupElement("export-rib", d.Get("name").(string), m, nil); err != nil { + if err := delRibGroupElement("export-rib", d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } } - if err := setRibGroup(d, m, nil); err != nil { + if err := setRibGroup(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if d.HasChange("import_policy") { - err = delRibGroupElement("import-policy", d.Get("name").(string), m, jnprSess) - if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delRibGroupElement("import-policy", d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } } if d.HasChange("import_rib") { - err = delRibGroupElement("import-rib", d.Get("name").(string), m, jnprSess) - if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delRibGroupElement("import-rib", d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } } if d.HasChange("export_rib") { - err = delRibGroupElement("export-rib", d.Get("name").(string), m, jnprSess) - if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delRibGroupElement("export-rib", d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } } - if err := setRibGroup(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setRibGroup(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_rib_group", jnprSess) + warns, err := clt.commitConf("update resource junos_rib_group", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceRibGroupReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceRibGroupReadWJunSess(d, clt, junSess)...) } func resourceRibGroupDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delRibGroup(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delRibGroup(d, clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delRibGroup(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delRibGroup(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_rib_group", jnprSess) + warns, err := clt.commitConf("delete resource junos_rib_group", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -254,21 +251,21 @@ func resourceRibGroupDelete(ctx context.Context, d *schema.ResourceData, m inter func resourceRibGroupImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - ribGroupExists, err := checkRibGroupExists(d.Id(), m, jnprSess) + ribGroupExists, err := checkRibGroupExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !ribGroupExists { return nil, fmt.Errorf("don't find rib group with id '%v' (id must be )", d.Id()) } - rigGroupOptions, err := readRibGroup(d.Id(), m, jnprSess) + rigGroupOptions, err := readRibGroup(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -278,9 +275,8 @@ func resourceRibGroupImport(ctx context.Context, d *schema.ResourceData, m inter return result, nil } -func checkRibGroupExists(group string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"routing-options rib-groups "+group+pipeDisplaySet, jnprSess) +func checkRibGroupExists(group string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"routing-options rib-groups "+group+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -291,8 +287,7 @@ func checkRibGroupExists(group string, m interface{}, jnprSess *NetconfObject) ( return true, nil } -func setRibGroup(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setRibGroup(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set routing-options rib-groups " + d.Get("name").(string) + " " @@ -306,15 +301,14 @@ func setRibGroup(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) configSet = append(configSet, setPrefix+"export-rib "+d.Get("export_rib").(string)) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readRibGroup(group string, m interface{}, jnprSess *NetconfObject) (ribGroupOptions, error) { - sess := m.(*Session) +func readRibGroup(group string, clt *Client, junSess *junosSession) (ribGroupOptions, error) { var confRead ribGroupOptions - showConfig, err := sess.command(cmdShowConfig+ - "routing-options rib-groups "+group+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "routing-options rib-groups "+group+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -342,20 +336,18 @@ func readRibGroup(group string, m interface{}, jnprSess *NetconfObject) (ribGrou return confRead, nil } -func delRibGroupElement(element, group string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delRibGroupElement(element, group string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete routing-options rib-groups "+group+" "+element) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func delRibGroup(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delRibGroup(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete routing-options rib-groups "+d.Get("name").(string)) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func validateRibGroup(d *schema.ResourceData) error { diff --git a/junos/resource_rip_group.go b/junos/resource_rip_group.go new file mode 100644 index 00000000..08c6da8d --- /dev/null +++ b/junos/resource_rip_group.go @@ -0,0 +1,801 @@ +package junos + +import ( + "context" + "fmt" + "strconv" + "strings" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" +) + +type ripGroupOptions struct { + demandCircuit bool + ng bool + maxRetransTime int + metricOut int + preference int + routeTimeout int + updateInterval int + name string + routingInstance string + exportPolicy []string + importPolicy []string + bfdLivenessDetection []map[string]interface{} +} + +func resourceRipGroup() *schema.Resource { + return &schema.Resource{ + CreateWithoutTimeout: resourceRipGroupCreate, + ReadWithoutTimeout: resourceRipGroupRead, + UpdateWithoutTimeout: resourceRipGroupUpdate, + DeleteWithoutTimeout: resourceRipGroupDelete, + Importer: &schema.ResourceImporter{ + StateContext: resourceRipGroupImport, + }, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringLenBetween(1, 48), + }, + "routing_instance": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + Default: defaultW, + ValidateDiagFunc: validateNameObjectJunos([]string{}, 64, formatDefault), + }, + "ng": { + Type: schema.TypeBool, + Optional: true, + ForceNew: true, + }, + "bfd_liveness_detection": { + Type: schema.TypeList, + Optional: true, + ConflictsWith: []string{"ng"}, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "authentication_algorithm": { + Type: schema.TypeString, + Optional: true, + }, + "authentication_key_chain": { + Type: schema.TypeString, + Optional: true, + }, + "authentication_loose_check": { + Type: schema.TypeBool, + Optional: true, + }, + "detection_time_threshold": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(1, 4294967295), + }, + "minimum_interval": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(1, 255000), + }, + "minimum_receive_interval": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(1, 255000), + }, + "multiplier": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(1, 255), + }, + "no_adaptation": { + Type: schema.TypeBool, + Optional: true, + }, + "transmit_interval_minimum_interval": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(1, 255000), + }, + "transmit_interval_threshold": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(1, 4294967295), + }, + "version": { + Type: schema.TypeString, + Optional: true, + }, + }, + }, + }, + "demand_circuit": { + Type: schema.TypeBool, + Optional: true, + ConflictsWith: []string{"ng"}, + }, + "export": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateDiagFunc: validateNameObjectJunos([]string{}, 64, formatDefault), + }, + }, + "import": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateDiagFunc: validateNameObjectJunos([]string{}, 64, formatDefault), + }, + }, + "max_retrans_time": { + Type: schema.TypeInt, + Optional: true, + ConflictsWith: []string{"ng"}, + ValidateFunc: validation.IntBetween(5, 180), + }, + "metric_out": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(1, 15), + }, + "preference": { + Type: schema.TypeInt, + Optional: true, + Default: -1, + ValidateFunc: validation.IntBetween(0, 4294967295), + }, + "route_timeout": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(30, 360), + }, + "update_interval": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(10, 60), + }, + }, + } +} + +func resourceRipGroupCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + clt := m.(*Client) + routingInstance := d.Get("routing_instance").(string) + name := d.Get("name").(string) + ripNg := d.Get("ng").(bool) + if clt.fakeCreateSetFile != "" { + if err := setRipGroup(d, clt, nil); err != nil { + return diag.FromErr(err) + } + if ripNg { + d.SetId(name + idSeparator + "ng" + idSeparator + routingInstance) + } else { + d.SetId(name + idSeparator + routingInstance) + } + + return nil + } + junSess, err := clt.startNewSession(ctx) + if err != nil { + return diag.FromErr(err) + } + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { + return diag.FromErr(err) + } + var diagWarns diag.Diagnostics + if routingInstance != defaultW { + instanceExists, err := checkRoutingInstanceExists(routingInstance, clt, junSess) + if err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) + + return append(diagWarns, diag.FromErr(err)...) + } + if !instanceExists { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) + + return append(diagWarns, + diag.FromErr(fmt.Errorf("routing instance %v doesn't exist", routingInstance))...) + } + } + ripGroupExists, err := checkRipGroupExists(name, ripNg, routingInstance, clt, junSess) + if err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) + + return append(diagWarns, diag.FromErr(err)...) + } + if ripGroupExists { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) + protocolsRipGroup := "rip group" + if ripNg { + protocolsRipGroup = "ripng group" + } + if routingInstance != defaultW { + return append(diagWarns, diag.FromErr(fmt.Errorf( + protocolsRipGroup+" %v already exists in routing-instance %v", name, routingInstance))...) + } + + return append(diagWarns, diag.FromErr(fmt.Errorf(protocolsRipGroup+" %v already exists", name))...) + } + if err := setRipGroup(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) + + return append(diagWarns, diag.FromErr(err)...) + } + warns, err := clt.commitConf("create resource junos_rip_group", junSess) + appendDiagWarns(&diagWarns, warns) + if err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) + + return append(diagWarns, diag.FromErr(err)...) + } + ripGroupExists, err = checkRipGroupExists(name, ripNg, routingInstance, clt, junSess) + if err != nil { + return append(diagWarns, diag.FromErr(err)...) + } + if ripGroupExists { + if ripNg { + d.SetId(name + idSeparator + "ng" + idSeparator + routingInstance) + } else { + d.SetId(name + idSeparator + routingInstance) + } + } else { + protocolsRipGroup := "rip group" + if ripNg { + protocolsRipGroup = "ripng group" + } + if routingInstance != defaultW { + return append(diagWarns, diag.FromErr(fmt.Errorf( + protocolsRipGroup+" %v not exists in routing-instance %v after commit "+ + "=> check your config", name, routingInstance))...) + } + + return append(diagWarns, diag.FromErr(fmt.Errorf(protocolsRipGroup+" %v not exists after commit "+ + "=> check your config", name))...) + } + + return append(diagWarns, resourceRipGroupReadWJunSess(d, clt, junSess)...) +} + +func resourceRipGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) + if err != nil { + return diag.FromErr(err) + } + defer clt.closeSession(junSess) + + return resourceRipGroupReadWJunSess(d, clt, junSess) +} + +func resourceRipGroupReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, +) diag.Diagnostics { + mutex.Lock() + ripGroupOptions, err := readRipGroup( + d.Get("name").(string), + d.Get("ng").(bool), + d.Get("routing_instance").(string), + clt, junSess) + mutex.Unlock() + if err != nil { + return diag.FromErr(err) + } + if ripGroupOptions.name == "" { + d.SetId("") + } else { + fillRipGroupData(d, ripGroupOptions) + } + + return nil +} + +func resourceRipGroupUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + d.Partial(true) + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delRipGroup( + d.Get("name").(string), + d.Get("ng").(bool), + d.Get("routing_instance").(string), + false, + clt, nil, + ); err != nil { + return diag.FromErr(err) + } + if err := setRipGroup(d, clt, nil); err != nil { + return diag.FromErr(err) + } + d.Partial(false) + + return nil + } + junSess, err := clt.startNewSession(ctx) + if err != nil { + return diag.FromErr(err) + } + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { + return diag.FromErr(err) + } + var diagWarns diag.Diagnostics + if err := delRipGroup( + d.Get("name").(string), + d.Get("ng").(bool), + d.Get("routing_instance").(string), + false, + clt, junSess, + ); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) + + return append(diagWarns, diag.FromErr(err)...) + } + if err := setRipGroup(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) + + return append(diagWarns, diag.FromErr(err)...) + } + warns, err := clt.commitConf("update resource junos_rip_group", junSess) + appendDiagWarns(&diagWarns, warns) + if err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) + + return append(diagWarns, diag.FromErr(err)...) + } + d.Partial(false) + + return append(diagWarns, resourceRipGroupReadWJunSess(d, clt, junSess)...) +} + +func resourceRipGroupDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delRipGroup( + d.Get("name").(string), + d.Get("ng").(bool), + d.Get("routing_instance").(string), + true, + clt, nil, + ); err != nil { + return diag.FromErr(err) + } + + return nil + } + junSess, err := clt.startNewSession(ctx) + if err != nil { + return diag.FromErr(err) + } + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { + return diag.FromErr(err) + } + var diagWarns diag.Diagnostics + if err := delRipGroup( + d.Get("name").(string), + d.Get("ng").(bool), + d.Get("routing_instance").(string), + true, + clt, junSess, + ); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) + + return append(diagWarns, diag.FromErr(err)...) + } + warns, err := clt.commitConf("delete resource junos_rip_group", junSess) + appendDiagWarns(&diagWarns, warns) + if err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) + + return append(diagWarns, diag.FromErr(err)...) + } + + return diagWarns +} + +func resourceRipGroupImport(ctx context.Context, d *schema.ResourceData, m interface{}, +) ([]*schema.ResourceData, error) { + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) + if err != nil { + return nil, err + } + defer clt.closeSession(junSess) + result := make([]*schema.ResourceData, 1) + idSplit := strings.Split(d.Id(), idSeparator) + if len(idSplit) < 2 { + return nil, fmt.Errorf("missing element(s) in id with separator %v", idSeparator) + } + if len(idSplit) == 2 { + ripGroupExists, err := checkRipGroupExists(idSplit[0], false, idSplit[1], clt, junSess) + if err != nil { + return nil, err + } + if !ripGroupExists { + return nil, fmt.Errorf("don't find rip group id '%v' "+ + "(id must be %s or %sng%s", + d.Id(), idSeparator, idSeparator, idSeparator, + ) + } + ripGroupOptions, err := readRipGroup(idSplit[0], false, idSplit[1], clt, junSess) + if err != nil { + return nil, err + } + fillRipGroupData(d, ripGroupOptions) + + result[0] = d + + return result, nil + } + if idSplit[1] != "ng" { + return nil, fmt.Errorf("id must be %s or %sng%s", + idSeparator, idSeparator, idSeparator, + ) + } + ripGroupExists, err := checkRipGroupExists(idSplit[0], true, idSplit[2], clt, junSess) + if err != nil { + return nil, err + } + if !ripGroupExists { + return nil, fmt.Errorf("don't find ripng group with id '%v' "+ + "(id must be %s or %sng%s", + d.Id(), idSeparator, idSeparator, idSeparator, + ) + } + ripGroupOptions, err := readRipGroup(idSplit[0], true, idSplit[2], clt, junSess) + if err != nil { + return nil, err + } + fillRipGroupData(d, ripGroupOptions) + result[0] = d + + return result, nil +} + +func checkRipGroupExists(name string, ripNg bool, routingInstance string, clt *Client, junSess *junosSession, +) (bool, error) { + var showConfig string + var err error + protoRipGroup := "protocols rip group \"" + name + "\"" + if ripNg { + protoRipGroup = "protocols ripng group \"" + name + "\"" + } + if routingInstance == defaultW { + showConfig, err = clt.command(cmdShowConfig+ + protoRipGroup+pipeDisplaySet, junSess) + } else { + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ + protoRipGroup+pipeDisplaySet, junSess) + } + if err != nil { + return false, err + } + if showConfig == emptyW { + return false, nil + } + + return true, nil +} + +func setRipGroup(d *schema.ResourceData, clt *Client, junSess *junosSession) error { + configSet := make([]string, 0, 1) + + setPrefix := setLS + if rI := d.Get("routing_instance").(string); rI != defaultW { + setPrefix = setRoutingInstances + rI + " " + } + if d.Get("ng").(bool) { + setPrefix += "protocols ripng group " + } else { + setPrefix += "protocols rip group " + } + setPrefix += "\"" + d.Get("name").(string) + "\" " + + configSet = append(configSet, setPrefix) + for _, mBFDLivDet := range d.Get("bfd_liveness_detection").([]interface{}) { + if mBFDLivDet == nil { + return fmt.Errorf("bfd_liveness_detection block is empty") + } + setPrefixBfd := setPrefix + "bfd-liveness-detection " + bfdLiveDetect := mBFDLivDet.(map[string]interface{}) + if v := bfdLiveDetect["authentication_algorithm"].(string); v != "" { + configSet = append(configSet, setPrefixBfd+"authentication algorithm "+v) + } + if v := bfdLiveDetect["authentication_key_chain"].(string); v != "" { + configSet = append(configSet, setPrefixBfd+"authentication key-chain \""+v+"\"") + } + if bfdLiveDetect["authentication_loose_check"].(bool) { + configSet = append(configSet, setPrefixBfd+"authentication loose-check") + } + if v := bfdLiveDetect["detection_time_threshold"].(int); v != 0 { + configSet = append(configSet, setPrefixBfd+"detection-time threshold "+strconv.Itoa(v)) + } + if v := bfdLiveDetect["minimum_interval"].(int); v != 0 { + configSet = append(configSet, setPrefixBfd+"minimum-interval "+strconv.Itoa(v)) + } + if v := bfdLiveDetect["minimum_receive_interval"].(int); v != 0 { + configSet = append(configSet, setPrefixBfd+"minimum-receive-interval "+strconv.Itoa(v)) + } + if v := bfdLiveDetect["multiplier"].(int); v != 0 { + configSet = append(configSet, setPrefixBfd+"multiplier "+strconv.Itoa(v)) + } + if bfdLiveDetect["no_adaptation"].(bool) { + configSet = append(configSet, setPrefixBfd+"no-adaptation") + } + if v := bfdLiveDetect["transmit_interval_minimum_interval"].(int); v != 0 { + configSet = append(configSet, setPrefixBfd+ + "transmit-interval minimum-interval "+strconv.Itoa(v)) + } + if v := bfdLiveDetect["transmit_interval_threshold"].(int); v != 0 { + configSet = append(configSet, setPrefixBfd+ + "transmit-interval threshold "+strconv.Itoa(v)) + } + if v := bfdLiveDetect["version"].(string); v != "" { + configSet = append(configSet, setPrefixBfd+"version "+v) + } + if len(configSet) == 0 || !strings.HasPrefix(configSet[len(configSet)-1], setPrefixBfd) { + return fmt.Errorf("bfd_liveness_detection block is empty") + } + } + if d.Get("demand_circuit").(bool) { + configSet = append(configSet, setPrefix+"demand-circuit") + } + for _, exportPolicy := range d.Get("export").([]interface{}) { + configSet = append(configSet, setPrefix+"export "+exportPolicy.(string)) + } + for _, importPolicy := range d.Get("import").([]interface{}) { + configSet = append(configSet, setPrefix+"import "+importPolicy.(string)) + } + if v := d.Get("max_retrans_time").(int); v != 0 { + configSet = append(configSet, setPrefix+"max-retrans-time "+strconv.Itoa(v)) + } + if v := d.Get("metric_out").(int); v != 0 { + configSet = append(configSet, setPrefix+"metric-out "+strconv.Itoa(v)) + } + if v := d.Get("preference").(int); v != -1 { + configSet = append(configSet, setPrefix+"preference "+strconv.Itoa(v)) + } + if v := d.Get("route_timeout").(int); v != 0 { + configSet = append(configSet, setPrefix+"route-timeout "+strconv.Itoa(v)) + } + if v := d.Get("update_interval").(int); v != 0 { + configSet = append(configSet, setPrefix+"update-interval "+strconv.Itoa(v)) + } + + return clt.configSet(configSet, junSess) +} + +func readRipGroup(name string, ripNg bool, routingInstance string, clt *Client, junSess *junosSession, +) (ripGroupOptions, error) { + var confRead ripGroupOptions + var showConfig string + var err error + confRead.preference = -1 + protoRipGroup := "protocols rip group \"" + name + "\"" + if ripNg { + protoRipGroup = "protocols ripng group \"" + name + "\"" + } + if routingInstance == defaultW { + showConfig, err = clt.command(cmdShowConfig+ + protoRipGroup+pipeDisplaySetRelative, junSess) + } else { + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ + protoRipGroup+pipeDisplaySetRelative, junSess) + } + if err != nil { + return confRead, err + } + if showConfig != emptyW { + confRead.name = name + confRead.ng = ripNg + confRead.routingInstance = routingInstance + for _, item := range strings.Split(showConfig, "\n") { + if strings.Contains(item, xmlStartTagConfigOut) { + continue + } + if strings.Contains(item, xmlEndTagConfigOut) { + break + } + itemTrim := strings.TrimPrefix(item, setLS) + switch { + case strings.HasPrefix(itemTrim, "bfd-liveness-detection "): + if len(confRead.bfdLivenessDetection) == 0 { + confRead.bfdLivenessDetection = append(confRead.bfdLivenessDetection, map[string]interface{}{ + "authentication_algorithm": "", + "authentication_key_chain": "", + "authentication_loose_check": false, + "detection_time_threshold": 0, + "minimum_interval": 0, + "minimum_receive_interval": 0, + "multiplier": 0, + "no_adaptation": false, + "transmit_interval_minimum_interval": 0, + "transmit_interval_threshold": 0, + "version": "", + }) + } + if err := readRipGroupBfd( + strings.TrimPrefix(itemTrim, "bfd-liveness-detection "), + confRead.bfdLivenessDetection[0], + ); err != nil { + return confRead, err + } + case itemTrim == "demand-circuit": + confRead.demandCircuit = true + case strings.HasPrefix(itemTrim, "export "): + confRead.exportPolicy = append(confRead.exportPolicy, strings.TrimPrefix(itemTrim, "export ")) + case strings.HasPrefix(itemTrim, "import "): + confRead.importPolicy = append(confRead.importPolicy, strings.TrimPrefix(itemTrim, "import ")) + case strings.HasPrefix(itemTrim, "max-retrans-time "): + confRead.maxRetransTime, err = strconv.Atoi(strings.TrimPrefix(itemTrim, "max-retrans-time ")) + if err != nil { + return confRead, fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case strings.HasPrefix(itemTrim, "metric-out "): + confRead.metricOut, err = strconv.Atoi(strings.TrimPrefix(itemTrim, "metric-out ")) + if err != nil { + return confRead, fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case strings.HasPrefix(itemTrim, "preference "): + confRead.preference, err = strconv.Atoi(strings.TrimPrefix(itemTrim, "preference ")) + if err != nil { + return confRead, fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case strings.HasPrefix(itemTrim, "route-timeout "): + confRead.routeTimeout, err = strconv.Atoi(strings.TrimPrefix(itemTrim, "route-timeout ")) + if err != nil { + return confRead, fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case strings.HasPrefix(itemTrim, "update-interval "): + confRead.updateInterval, err = strconv.Atoi(strings.TrimPrefix(itemTrim, "update-interval ")) + if err != nil { + return confRead, fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + } + } + } + + return confRead, nil +} + +func readRipGroupBfd(itemTrim string, bfd map[string]interface{}) error { + switch { + case strings.HasPrefix(itemTrim, "authentication algorithm "): + bfd["authentication_algorithm"] = strings.TrimPrefix(itemTrim, "authentication algorithm ") + case strings.HasPrefix(itemTrim, "authentication key-chain "): + bfd["authentication_key_chain"] = strings.Trim(strings.TrimPrefix(itemTrim, "authentication key-chain "), "\"") + case itemTrim == "authentication loose-check": + bfd["authentication_loose_check"] = true + case strings.HasPrefix(itemTrim, "detection-time threshold "): + var err error + bfd["detection_time_threshold"], err = strconv.Atoi(strings.TrimPrefix(itemTrim, "detection-time threshold ")) + if err != nil { + return fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case strings.HasPrefix(itemTrim, "minimum-interval "): + var err error + bfd["minimum_interval"], err = strconv.Atoi(strings.TrimPrefix(itemTrim, "minimum-interval ")) + if err != nil { + return fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case strings.HasPrefix(itemTrim, "minimum-receive-interval "): + var err error + bfd["minimum_receive_interval"], err = strconv.Atoi(strings.TrimPrefix(itemTrim, "minimum-receive-interval ")) + if err != nil { + return fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case strings.HasPrefix(itemTrim, "multiplier "): + var err error + bfd["multiplier"], err = strconv.Atoi(strings.TrimPrefix(itemTrim, "multiplier ")) + if err != nil { + return fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case itemTrim == "no-adaptation": + bfd["no_adaptation"] = true + case strings.HasPrefix(itemTrim, "transmit-interval minimum-interval "): + var err error + bfd["transmit_interval_minimum_interval"], err = strconv.Atoi(strings.TrimPrefix( + itemTrim, "transmit-interval minimum-interval ")) + if err != nil { + return fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case strings.HasPrefix(itemTrim, "transmit-interval threshold "): + var err error + bfd["transmit_interval_threshold"], err = strconv.Atoi(strings.TrimPrefix( + itemTrim, "transmit-interval threshold ")) + if err != nil { + return fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case strings.HasPrefix(itemTrim, "version "): + bfd["version"] = strings.TrimPrefix(itemTrim, "version ") + } + + return nil +} + +func delRipGroup( + name string, + ripNg bool, + routingInstance string, + deleteAll bool, + clt *Client, + junSess *junosSession, +) error { + delPrefix := deleteLS + if routingInstance != defaultW { + delPrefix = delRoutingInstances + routingInstance + " " + } + if ripNg { + delPrefix += "protocols ripng group " + } else { + delPrefix += "protocols rip group " + } + delPrefix += "\"" + name + "\" " + + if deleteAll { + return clt.configSet([]string{delPrefix}, junSess) + } + configSet := make([]string, 0, 10) + listLinesToDelete := []string{ + "bfd-liveness-detection", + "demand-circuit", + "export", + "import", + "max-retrans-time", + "metric-out", + "preference", + "route-timeout", + "update-interval", + } + for _, line := range listLinesToDelete { + configSet = append(configSet, delPrefix+line) + } + + return clt.configSet(configSet, junSess) +} + +func fillRipGroupData(d *schema.ResourceData, ripGroupOptions ripGroupOptions) { + if tfErr := d.Set("name", ripGroupOptions.name); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("routing_instance", ripGroupOptions.routingInstance); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("ng", ripGroupOptions.ng); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("bfd_liveness_detection", ripGroupOptions.bfdLivenessDetection); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("demand_circuit", ripGroupOptions.demandCircuit); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("export", ripGroupOptions.exportPolicy); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("import", ripGroupOptions.importPolicy); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("max_retrans_time", ripGroupOptions.maxRetransTime); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("metric_out", ripGroupOptions.metricOut); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("preference", ripGroupOptions.preference); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("route_timeout", ripGroupOptions.routeTimeout); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("update_interval", ripGroupOptions.updateInterval); tfErr != nil { + panic(tfErr) + } +} diff --git a/junos/resource_rip_group_test.go b/junos/resource_rip_group_test.go new file mode 100644 index 00000000..1bb7c054 --- /dev/null +++ b/junos/resource_rip_group_test.go @@ -0,0 +1,165 @@ +package junos_test + +import ( + "os" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +// export TESTACC_INTERFACE= for choose interface available else it's ge-0/0/3. +func TestAccJunosRipGroup_basic(t *testing.T) { + if os.Getenv("TESTACC_SWITCH") == "" { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccJunosRipGroupConfigCreate(), + }, + { + Config: testAccJunosRipGroupConfigUpdate(), + }, + { + ResourceName: "junos_rip_group.testacc_ripgroup", + ImportState: true, + ImportStateVerify: true, + }, + { + ResourceName: "junos_rip_group.testacc_ripgroup2", + ImportState: true, + ImportStateVerify: true, + }, + { + ResourceName: "junos_rip_group.testacc_ripnggroup", + ImportState: true, + ImportStateVerify: true, + }, + { + ResourceName: "junos_rip_group.testacc_ripnggroup2", + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccJunosRipGroupConfigCreate(), + }, + }, + }) + } +} + +func testAccJunosRipGroupConfigCreate() string { + return ` +resource "junos_rip_group" "testacc_ripgroup" { + name = "test_rip_group#1" +} +resource "junos_routing_instance" "testacc_ripgroup2" { + name = "testacc_ripgroup2" +} +resource "junos_rip_group" "testacc_ripgroup2" { + name = "test_rip_group#2" + routing_instance = junos_routing_instance.testacc_ripgroup2.name +} +resource "junos_rip_group" "testacc_ripnggroup" { + name = "test_ripng_group#1" + ng = true +} +resource "junos_rip_group" "testacc_ripnggroup2" { + name = "test_ripng_group#2" + ng = true + routing_instance = junos_routing_instance.testacc_ripgroup2.name +} +` +} + +func testAccJunosRipGroupConfigUpdate() string { + return ` +resource "junos_policyoptions_policy_statement" "testacc_ripgroup" { + lifecycle { + create_before_destroy = true + } + + name = "testacc_ripgroup" + then { + action = "accept" + } +} +resource "junos_policyoptions_policy_statement" "testacc_ripgroup2" { + lifecycle { + create_before_destroy = true + } + + name = "testacc_ripgroup2" + then { + action = "reject" + } +} +resource "junos_rip_group" "testacc_ripgroup" { + name = "test_rip_group#1" + demand_circuit = true + bfd_liveness_detection { + authentication_loose_check = true + detection_time_threshold = 60 + minimum_interval = 16 + minimum_receive_interval = 17 + multiplier = 2 + no_adaptation = true + transmit_interval_minimum_interval = 18 + transmit_interval_threshold = 19 + version = "automatic" + } + export = [ + junos_policyoptions_policy_statement.testacc_ripgroup.name, + ] + import = [ + junos_policyoptions_policy_statement.testacc_ripgroup2.name, + ] + max_retrans_time = 101 + metric_out = 11 + preference = 1000 + route_timeout = 90 + update_interval = 30 +} +resource "junos_routing_instance" "testacc_ripgroup2" { + name = "testacc_ripgroup2" +} +resource "junos_rip_group" "testacc_ripgroup2" { + name = "test_rip_group#2" + routing_instance = junos_routing_instance.testacc_ripgroup2.name + export = [ + junos_policyoptions_policy_statement.testacc_ripgroup2.name, + junos_policyoptions_policy_statement.testacc_ripgroup.name, + ] + import = [ + junos_policyoptions_policy_statement.testacc_ripgroup2.name, + ] +} +resource "junos_rip_group" "testacc_ripnggroup" { + name = "test_ripng_group#1" + ng = true + + export = [ + junos_policyoptions_policy_statement.testacc_ripgroup.name, + ] + import = [ + junos_policyoptions_policy_statement.testacc_ripgroup2.name, + ] + metric_out = 13 + preference = 1100 + route_timeout = 75 + update_interval = 35 +} +resource "junos_rip_group" "testacc_ripnggroup2" { + name = "test_ripng_group#2" + ng = true + routing_instance = junos_routing_instance.testacc_ripgroup2.name + export = [ + junos_policyoptions_policy_statement.testacc_ripgroup.name, + ] + import = [ + junos_policyoptions_policy_statement.testacc_ripgroup2.name, + junos_policyoptions_policy_statement.testacc_ripgroup.name, + ] +} +` +} diff --git a/junos/resource_rip_neighbor.go b/junos/resource_rip_neighbor.go new file mode 100644 index 00000000..9e574f90 --- /dev/null +++ b/junos/resource_rip_neighbor.go @@ -0,0 +1,1029 @@ +package junos + +import ( + "context" + "fmt" + "regexp" + "strconv" + "strings" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + bchk "github.com/jeremmfr/go-utils/basiccheck" + jdecode "github.com/jeremmfr/junosdecode" +) + +type ripNeighborOptions struct { + anySender bool + checkZero bool + demandCircuit bool + dynamicPeers bool + interfaceTypeP2mp bool + ng bool + noCheckZero bool + maxRetransTime int + messageSize int + metricIn int + routeTimeout int + updateInterval int + authenticationKey string + authenticationType string + name string + group string + receive string + routingInstance string + send string + importPolicy []string + peer []string + authenticationSelectiveMD5 []map[string]interface{} + bfdLivenessDetection []map[string]interface{} +} + +func resourceRipNeighbor() *schema.Resource { + return &schema.Resource{ + CreateWithoutTimeout: resourceRipNeighborCreate, + ReadWithoutTimeout: resourceRipNeighborRead, + UpdateWithoutTimeout: resourceRipNeighborUpdate, + DeleteWithoutTimeout: resourceRipNeighborDelete, + Importer: &schema.ResourceImporter{ + StateContext: resourceRipNeighborImport, + }, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { + value := v.(string) + if strings.Count(value, ".") != 1 { + errors = append(errors, fmt.Errorf( + "%q in %q need to have 1 dot", value, k)) + } + + return + }, + }, + "group": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringLenBetween(1, 48), + }, + "routing_instance": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + Default: defaultW, + ValidateDiagFunc: validateNameObjectJunos([]string{}, 64, formatDefault), + }, + "ng": { + Type: schema.TypeBool, + Optional: true, + ForceNew: true, + }, + "any_sender": { + Type: schema.TypeBool, + Optional: true, + ConflictsWith: []string{"ng"}, + }, + "authentication_key": { + Type: schema.TypeString, + Optional: true, + ConflictsWith: []string{"authentication_selective_md5", "ng"}, + Sensitive: true, + }, + "authentication_selective_md5": { + Type: schema.TypeList, + Optional: true, + ConflictsWith: []string{"authentication_key", "authentication_type", "ng"}, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "key_id": { + Type: schema.TypeInt, + Required: true, + ValidateFunc: validation.IntBetween(0, 255), + }, + "key": { + Type: schema.TypeString, + Required: true, + Sensitive: true, + }, + "start_time": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.StringMatch( + regexp.MustCompile(`^\d{4}\-\d\d?\-\d\d?\.\d{2}:\d{2}:\d{2}$`), + "must be in the format 'YYYY-MM-DD.HH:MM:SS'"), + }, + }, + }, + }, + "authentication_type": { + Type: schema.TypeString, + Optional: true, + ConflictsWith: []string{"authentication_selective_md5", "ng"}, + }, + "bfd_liveness_detection": { + Type: schema.TypeList, + Optional: true, + ConflictsWith: []string{"ng"}, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "authentication_algorithm": { + Type: schema.TypeString, + Optional: true, + }, + "authentication_key_chain": { + Type: schema.TypeString, + Optional: true, + }, + "authentication_loose_check": { + Type: schema.TypeBool, + Optional: true, + }, + "detection_time_threshold": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(1, 4294967295), + }, + "minimum_interval": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(1, 255000), + }, + "minimum_receive_interval": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(1, 255000), + }, + "multiplier": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(1, 255), + }, + "no_adaptation": { + Type: schema.TypeBool, + Optional: true, + }, + "transmit_interval_minimum_interval": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(1, 255000), + }, + "transmit_interval_threshold": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(1, 4294967295), + }, + "version": { + Type: schema.TypeString, + Optional: true, + }, + }, + }, + }, + "check_zero": { + Type: schema.TypeBool, + Optional: true, + ConflictsWith: []string{"no_check_zero", "ng"}, + }, + "demand_circuit": { + Type: schema.TypeBool, + Optional: true, + ConflictsWith: []string{"ng"}, + }, + "dynamic_peers": { + Type: schema.TypeBool, + Optional: true, + ConflictsWith: []string{"ng"}, + RequiredWith: []string{"interface_type_p2mp"}, + }, + "import": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateDiagFunc: validateNameObjectJunos([]string{}, 64, formatDefault), + }, + }, + "interface_type_p2mp": { + Type: schema.TypeBool, + Optional: true, + ConflictsWith: []string{"ng"}, + }, + "max_retrans_time": { + Type: schema.TypeInt, + Optional: true, + ConflictsWith: []string{"ng"}, + ValidateFunc: validation.IntBetween(5, 180), + }, + "message_size": { + Type: schema.TypeInt, + Optional: true, + ConflictsWith: []string{"ng"}, + ValidateFunc: validation.IntBetween(25, 255), + }, + "metric_in": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(1, 15), + }, + "no_check_zero": { + Type: schema.TypeBool, + Optional: true, + ConflictsWith: []string{"check_zero", "ng"}, + }, + "peer": { + Type: schema.TypeSet, + Optional: true, + ConflictsWith: []string{"ng"}, + RequiredWith: []string{"interface_type_p2mp"}, + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateFunc: validation.IsIPAddress, + }, + }, + "receive": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice([]string{"both", "none", "version-1", "version-2"}, false), + }, + "route_timeout": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(30, 360), + }, + "send": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice([]string{"broadcast", "multicast", "none", "version-1"}, false), + }, + "update_interval": { + Type: schema.TypeInt, + Optional: true, + ConflictsWith: []string{"ng"}, + ValidateFunc: validation.IntBetween(10, 60), + }, + }, + } +} + +func resourceRipNeighborCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + clt := m.(*Client) + name := d.Get("name").(string) + group := d.Get("group").(string) + ripNg := d.Get("ng").(bool) + routingInstance := d.Get("routing_instance").(string) + if clt.fakeCreateSetFile != "" { + if err := setRipNeighbor(d, clt, nil); err != nil { + return diag.FromErr(err) + } + if ripNg { + d.SetId(name + idSeparator + group + idSeparator + "ng" + idSeparator + routingInstance) + } else { + d.SetId(name + idSeparator + group + idSeparator + routingInstance) + } + + return nil + } + junSess, err := clt.startNewSession(ctx) + if err != nil { + return diag.FromErr(err) + } + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { + return diag.FromErr(err) + } + var diagWarns diag.Diagnostics + if routingInstance != defaultW { + instanceExists, err := checkRoutingInstanceExists(routingInstance, clt, junSess) + if err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) + + return append(diagWarns, diag.FromErr(err)...) + } + if !instanceExists { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) + + return append(diagWarns, + diag.FromErr(fmt.Errorf("routing instance %v doesn't exist", routingInstance))...) + } + } + ripGroupExists, err := checkRipGroupExists(group, ripNg, routingInstance, clt, junSess) + if err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) + + return append(diagWarns, diag.FromErr(err)...) + } + if !ripGroupExists { + if ripNg { + return append(diagWarns, diag.FromErr(fmt.Errorf("ripng group %v doesn't exist", group))...) + } + + return append(diagWarns, diag.FromErr(fmt.Errorf("rip group %v doesn't exist", group))...) + } + ripNeighborExists, err := checkRipNeighborExists(name, group, ripNg, routingInstance, clt, junSess) + if err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) + + return append(diagWarns, diag.FromErr(err)...) + } + if ripNeighborExists { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) + protocolsRipNeighbor := "rip group " + group + " neighbor" + if ripNg { + protocolsRipNeighbor = "ripng group " + group + " neighbor" + } + if routingInstance != defaultW { + return append(diagWarns, diag.FromErr(fmt.Errorf( + protocolsRipNeighbor+" %v already exists in routing-instance %v", name, routingInstance))...) + } + + return append(diagWarns, diag.FromErr(fmt.Errorf(protocolsRipNeighbor+" %v already exists", name))...) + } + if err := setRipNeighbor(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) + + return append(diagWarns, diag.FromErr(err)...) + } + warns, err := clt.commitConf("create resource junos_rip_neighbor", junSess) + appendDiagWarns(&diagWarns, warns) + if err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) + + return append(diagWarns, diag.FromErr(err)...) + } + ripNeighborExists, err = checkRipNeighborExists(name, group, ripNg, routingInstance, clt, junSess) + if err != nil { + return append(diagWarns, diag.FromErr(err)...) + } + if ripNeighborExists { + if ripNg { + d.SetId(name + idSeparator + group + idSeparator + "ng" + idSeparator + routingInstance) + } else { + d.SetId(name + idSeparator + group + idSeparator + routingInstance) + } + } else { + protocolsRipNeighbor := "rip group " + group + " neighbor" + if ripNg { + protocolsRipNeighbor = "ripng group " + group + " neighbor" + } + if routingInstance != defaultW { + return append(diagWarns, diag.FromErr(fmt.Errorf( + protocolsRipNeighbor+" %v not exists in routing-instance %v after commit "+ + "=> check your config", name, routingInstance))...) + } + + return append(diagWarns, diag.FromErr(fmt.Errorf(protocolsRipNeighbor+" %v not exists after commit "+ + "=> check your config", name))...) + } + + return append(diagWarns, resourceRipNeighborReadWJunSess(d, clt, junSess)...) +} + +func resourceRipNeighborRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) + if err != nil { + return diag.FromErr(err) + } + defer clt.closeSession(junSess) + + return resourceRipNeighborReadWJunSess(d, clt, junSess) +} + +func resourceRipNeighborReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, +) diag.Diagnostics { + mutex.Lock() + ripNeighborOptions, err := readRipNeighbor( + d.Get("name").(string), + d.Get("group").(string), + d.Get("ng").(bool), + d.Get("routing_instance").(string), + clt, junSess) + mutex.Unlock() + if err != nil { + return diag.FromErr(err) + } + if ripNeighborOptions.name == "" { + d.SetId("") + } else { + fillRipNeighborData(d, ripNeighborOptions) + } + + return nil +} + +func resourceRipNeighborUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + d.Partial(true) + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delRipNeighbor( + d.Get("name").(string), + d.Get("group").(string), + d.Get("ng").(bool), + d.Get("routing_instance").(string), + clt, nil, + ); err != nil { + return diag.FromErr(err) + } + if err := setRipNeighbor(d, clt, nil); err != nil { + return diag.FromErr(err) + } + d.Partial(false) + + return nil + } + junSess, err := clt.startNewSession(ctx) + if err != nil { + return diag.FromErr(err) + } + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { + return diag.FromErr(err) + } + var diagWarns diag.Diagnostics + if err := delRipNeighbor( + d.Get("name").(string), + d.Get("group").(string), + d.Get("ng").(bool), + d.Get("routing_instance").(string), + clt, junSess, + ); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) + + return append(diagWarns, diag.FromErr(err)...) + } + if err := setRipNeighbor(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) + + return append(diagWarns, diag.FromErr(err)...) + } + warns, err := clt.commitConf("update resource junos_rip_neighbor", junSess) + appendDiagWarns(&diagWarns, warns) + if err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) + + return append(diagWarns, diag.FromErr(err)...) + } + d.Partial(false) + + return append(diagWarns, resourceRipNeighborReadWJunSess(d, clt, junSess)...) +} + +func resourceRipNeighborDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delRipNeighbor( + d.Get("name").(string), + d.Get("group").(string), + d.Get("ng").(bool), + d.Get("routing_instance").(string), + clt, nil, + ); err != nil { + return diag.FromErr(err) + } + + return nil + } + junSess, err := clt.startNewSession(ctx) + if err != nil { + return diag.FromErr(err) + } + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { + return diag.FromErr(err) + } + var diagWarns diag.Diagnostics + if err := delRipNeighbor( + d.Get("name").(string), + d.Get("group").(string), + d.Get("ng").(bool), + d.Get("routing_instance").(string), + clt, junSess, + ); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) + + return append(diagWarns, diag.FromErr(err)...) + } + warns, err := clt.commitConf("delete resource junos_rip_neighbor", junSess) + appendDiagWarns(&diagWarns, warns) + if err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) + + return append(diagWarns, diag.FromErr(err)...) + } + + return diagWarns +} + +func resourceRipNeighborImport(ctx context.Context, d *schema.ResourceData, m interface{}, +) ([]*schema.ResourceData, error) { + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) + if err != nil { + return nil, err + } + defer clt.closeSession(junSess) + result := make([]*schema.ResourceData, 1) + idSplit := strings.Split(d.Id(), idSeparator) + if len(idSplit) < 3 { + return nil, fmt.Errorf("missing element(s) in id with separator %v", idSeparator) + } + if len(idSplit) == 3 { + ripNeighborExists, err := checkRipNeighborExists(idSplit[0], idSplit[1], false, idSplit[2], clt, junSess) + if err != nil { + return nil, err + } + if !ripNeighborExists { + return nil, fmt.Errorf("don't find rip neighbor id '%v' "+ + "(id must be %s%s or %s%sng%s", + d.Id(), idSeparator, idSeparator, idSeparator, idSeparator, idSeparator, + ) + } + ripNeighborOptions, err := readRipNeighbor(idSplit[0], idSplit[1], false, idSplit[2], clt, junSess) + if err != nil { + return nil, err + } + fillRipNeighborData(d, ripNeighborOptions) + + result[0] = d + + return result, nil + } + if idSplit[2] != "ng" { + return nil, fmt.Errorf("id must be %s%s or %s%sng%s", + idSeparator, idSeparator, idSeparator, idSeparator, idSeparator, + ) + } + ripNeighborExists, err := checkRipNeighborExists(idSplit[0], idSplit[1], true, idSplit[3], clt, junSess) + if err != nil { + return nil, err + } + if !ripNeighborExists { + return nil, fmt.Errorf("don't find ripng neighbor with id '%v' "+ + "(id must be %s%s or %s%sng%s", + d.Id(), idSeparator, idSeparator, idSeparator, idSeparator, idSeparator, + ) + } + ripNeighborOptions, err := readRipNeighbor(idSplit[0], idSplit[1], true, idSplit[3], clt, junSess) + if err != nil { + return nil, err + } + fillRipNeighborData(d, ripNeighborOptions) + result[0] = d + + return result, nil +} + +func checkRipNeighborExists( + name, group string, + ripNg bool, + routingInstance string, + clt *Client, + junSess *junosSession, +) (bool, error) { + var showConfig string + var err error + protoRipNeighbor := "protocols rip group \"" + group + "\" neighbor " + name + if ripNg { + protoRipNeighbor = "protocols ripng group \"" + group + "\" neighbor " + name + } + if routingInstance == defaultW { + showConfig, err = clt.command(cmdShowConfig+ + protoRipNeighbor+pipeDisplaySet, junSess) + } else { + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ + protoRipNeighbor+pipeDisplaySet, junSess) + } + if err != nil { + return false, err + } + if showConfig == emptyW { + return false, nil + } + + return true, nil +} + +func setRipNeighbor(d *schema.ResourceData, clt *Client, junSess *junosSession) error { + configSet := make([]string, 0, 1) + + setPrefix := setLS + if rI := d.Get("routing_instance").(string); rI != defaultW { + setPrefix = setRoutingInstances + rI + " " + } + if d.Get("ng").(bool) { + setPrefix += "protocols ripng group " + } else { + setPrefix += "protocols rip group " + } + setPrefix += "\"" + d.Get("group").(string) + "\" neighbor " + d.Get("name").(string) + " " + + configSet = append(configSet, setPrefix) + if d.Get("any_sender").(bool) { + configSet = append(configSet, setPrefix+"any-sender") + } + if v := d.Get("authentication_key").(string); v != "" { + configSet = append(configSet, setPrefix+"authentication-key \""+v+"\"") + } + authSimpleMD5List := make([]int, 0) + for _, authSimpMd5Block := range d.Get("authentication_selective_md5").([]interface{}) { + authSimpMd5 := authSimpMd5Block.(map[string]interface{}) + if bchk.IntInSlice(authSimpMd5["key_id"].(int), authSimpleMD5List) { + return fmt.Errorf("multiple blocks authentication_selective_md5 "+ + "with the same key_id %d", authSimpMd5["key_id"].(int)) + } + authSimpleMD5List = append(authSimpleMD5List, authSimpMd5["key_id"].(int)) + configSet = append(configSet, setPrefix+"authentication-selective-md5 "+ + strconv.Itoa(authSimpMd5["key_id"].(int))+" key \""+authSimpMd5["key"].(string)+"\"") + if v := authSimpMd5["start_time"].(string); v != "" { + configSet = append(configSet, setPrefix+"authentication-selective-md5 "+ + strconv.Itoa(authSimpMd5["key_id"].(int))+" start-time "+v) + } + } + if v := d.Get("authentication_type").(string); v != "" { + configSet = append(configSet, setPrefix+"authentication-type "+v) + } + for _, mBFDLivDet := range d.Get("bfd_liveness_detection").([]interface{}) { + if mBFDLivDet == nil { + return fmt.Errorf("bfd_liveness_detection block is empty") + } + setPrefixBfd := setPrefix + "bfd-liveness-detection " + bfdLiveDetect := mBFDLivDet.(map[string]interface{}) + if v := bfdLiveDetect["authentication_algorithm"].(string); v != "" { + configSet = append(configSet, setPrefixBfd+"authentication algorithm "+v) + } + if v := bfdLiveDetect["authentication_key_chain"].(string); v != "" { + configSet = append(configSet, setPrefixBfd+"authentication key-chain \""+v+"\"") + } + if bfdLiveDetect["authentication_loose_check"].(bool) { + configSet = append(configSet, setPrefixBfd+"authentication loose-check") + } + if v := bfdLiveDetect["detection_time_threshold"].(int); v != 0 { + configSet = append(configSet, setPrefixBfd+"detection-time threshold "+strconv.Itoa(v)) + } + if v := bfdLiveDetect["minimum_interval"].(int); v != 0 { + configSet = append(configSet, setPrefixBfd+"minimum-interval "+strconv.Itoa(v)) + } + if v := bfdLiveDetect["minimum_receive_interval"].(int); v != 0 { + configSet = append(configSet, setPrefixBfd+"minimum-receive-interval "+strconv.Itoa(v)) + } + if v := bfdLiveDetect["multiplier"].(int); v != 0 { + configSet = append(configSet, setPrefixBfd+"multiplier "+strconv.Itoa(v)) + } + if bfdLiveDetect["no_adaptation"].(bool) { + configSet = append(configSet, setPrefixBfd+"no-adaptation") + } + if v := bfdLiveDetect["transmit_interval_minimum_interval"].(int); v != 0 { + configSet = append(configSet, setPrefixBfd+ + "transmit-interval minimum-interval "+strconv.Itoa(v)) + } + if v := bfdLiveDetect["transmit_interval_threshold"].(int); v != 0 { + configSet = append(configSet, setPrefixBfd+ + "transmit-interval threshold "+strconv.Itoa(v)) + } + if v := bfdLiveDetect["version"].(string); v != "" { + configSet = append(configSet, setPrefixBfd+"version "+v) + } + if len(configSet) == 0 || !strings.HasPrefix(configSet[len(configSet)-1], setPrefixBfd) { + return fmt.Errorf("bfd_liveness_detection block is empty") + } + } + if d.Get("check_zero").(bool) { + configSet = append(configSet, setPrefix+"check-zero") + } + if d.Get("demand_circuit").(bool) { + configSet = append(configSet, setPrefix+"demand-circuit") + } + if d.Get("dynamic_peers").(bool) { + configSet = append(configSet, setPrefix+"dynamic-peers") + } + for _, importPolicy := range d.Get("import").([]interface{}) { + configSet = append(configSet, setPrefix+"import "+importPolicy.(string)) + } + if d.Get("interface_type_p2mp").(bool) { + configSet = append(configSet, setPrefix+"interface-type p2mp") + } + if v := d.Get("max_retrans_time").(int); v != 0 { + configSet = append(configSet, setPrefix+"max-retrans-time "+strconv.Itoa(v)) + } + if v := d.Get("message_size").(int); v != 0 { + configSet = append(configSet, setPrefix+"message-size "+strconv.Itoa(v)) + } + if v := d.Get("metric_in").(int); v != 0 { + configSet = append(configSet, setPrefix+"metric-in "+strconv.Itoa(v)) + } + if d.Get("no_check_zero").(bool) { + configSet = append(configSet, setPrefix+"no-check-zero") + } + for _, peer := range sortSetOfString(d.Get("peer").(*schema.Set).List()) { + configSet = append(configSet, setPrefix+"peer "+peer) + } + if v := d.Get("receive").(string); v != "" { + configSet = append(configSet, setPrefix+"receive "+v) + } + if v := d.Get("route_timeout").(int); v != 0 { + configSet = append(configSet, setPrefix+"route-timeout "+strconv.Itoa(v)) + } + if v := d.Get("send").(string); v != "" { + configSet = append(configSet, setPrefix+"send "+v) + } + if v := d.Get("update_interval").(int); v != 0 { + configSet = append(configSet, setPrefix+"update-interval "+strconv.Itoa(v)) + } + + return clt.configSet(configSet, junSess) +} + +func readRipNeighbor(name, group string, ripNg bool, routingInstance string, clt *Client, junSess *junosSession, +) (ripNeighborOptions, error) { + var confRead ripNeighborOptions + var showConfig string + var err error + protoRipNeighbor := "protocols rip group \"" + group + "\" neighbor " + name + if ripNg { + protoRipNeighbor = "protocols ripng group \"" + group + "\" neighbor " + name + } + if routingInstance == defaultW { + showConfig, err = clt.command(cmdShowConfig+ + protoRipNeighbor+pipeDisplaySetRelative, junSess) + } else { + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ + protoRipNeighbor+pipeDisplaySetRelative, junSess) + } + if err != nil { + return confRead, err + } + if showConfig != emptyW { + confRead.name = name + confRead.group = group + confRead.ng = ripNg + confRead.routingInstance = routingInstance + for _, item := range strings.Split(showConfig, "\n") { + if strings.Contains(item, xmlStartTagConfigOut) { + continue + } + if strings.Contains(item, xmlEndTagConfigOut) { + break + } + itemTrim := strings.TrimPrefix(item, setLS) + switch { + case itemTrim == "any-sender": + confRead.anySender = true + case strings.HasPrefix(itemTrim, "authentication-key "): + confRead.authenticationKey, err = jdecode.Decode( + strings.Trim(strings.TrimPrefix(itemTrim, "authentication-key "), "\"")) + if err != nil { + return confRead, fmt.Errorf("failed to decode authentication-key: %w", err) + } + case strings.HasPrefix(itemTrim, "authentication-selective-md5 "): + itemTrimplit := strings.Split(strings.TrimPrefix(itemTrim, "authentication-selective-md5 "), " ") + keyID, err := strconv.Atoi(itemTrimplit[0]) + if err != nil { + return confRead, fmt.Errorf(failedConvAtoiError, itemTrimplit[0], err) + } + authSelectMD5 := map[string]interface{}{ + "key_id": keyID, + "key": "", + "start_time": "", + } + confRead.authenticationSelectiveMD5 = copyAndRemoveItemMapList( + "key_id", authSelectMD5, confRead.authenticationSelectiveMD5) + itemTrimAuthSelectMD5 := strings.TrimPrefix(itemTrim, "authentication-selective-md5 "+itemTrimplit[0]+" ") + switch { + case strings.HasPrefix(itemTrimAuthSelectMD5, "key "): + var err error + authSelectMD5["key"], err = jdecode.Decode(strings.Trim(strings.TrimPrefix( + itemTrimAuthSelectMD5, "key "), "\"")) + if err != nil { + return confRead, fmt.Errorf("failed to decode authentication-selective-md5 key: %w", err) + } + case strings.HasPrefix(itemTrimAuthSelectMD5, "start-time "): + authSelectMD5["start_time"] = strings.Split(strings.Trim(strings.TrimPrefix( + itemTrimAuthSelectMD5, "start-time "), "\""), " ")[0] + } + confRead.authenticationSelectiveMD5 = append(confRead.authenticationSelectiveMD5, authSelectMD5) + case strings.HasPrefix(itemTrim, "authentication-type "): + confRead.authenticationType = strings.TrimPrefix(itemTrim, "authentication-type ") + case strings.HasPrefix(itemTrim, "bfd-liveness-detection "): + if len(confRead.bfdLivenessDetection) == 0 { + confRead.bfdLivenessDetection = append(confRead.bfdLivenessDetection, map[string]interface{}{ + "authentication_algorithm": "", + "authentication_key_chain": "", + "authentication_loose_check": false, + "detection_time_threshold": 0, + "minimum_interval": 0, + "minimum_receive_interval": 0, + "multiplier": 0, + "no_adaptation": false, + "transmit_interval_minimum_interval": 0, + "transmit_interval_threshold": 0, + "version": "", + }) + } + if err := readRipNeighborBfd( + strings.TrimPrefix(itemTrim, "bfd-liveness-detection "), + confRead.bfdLivenessDetection[0], + ); err != nil { + return confRead, err + } + case itemTrim == "check-zero": + confRead.checkZero = true + case itemTrim == "demand-circuit": + confRead.demandCircuit = true + case itemTrim == "dynamic-peers": + confRead.dynamicPeers = true + case strings.HasPrefix(itemTrim, "import "): + confRead.importPolicy = append(confRead.importPolicy, strings.TrimPrefix(itemTrim, "import ")) + case itemTrim == "interface-type p2mp": + confRead.interfaceTypeP2mp = true + case strings.HasPrefix(itemTrim, "max-retrans-time "): + confRead.maxRetransTime, err = strconv.Atoi(strings.TrimPrefix(itemTrim, "max-retrans-time ")) + if err != nil { + return confRead, fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case strings.HasPrefix(itemTrim, "message-size "): + confRead.messageSize, err = strconv.Atoi(strings.TrimPrefix(itemTrim, "message-size ")) + if err != nil { + return confRead, fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case strings.HasPrefix(itemTrim, "metric-in "): + confRead.metricIn, err = strconv.Atoi(strings.TrimPrefix(itemTrim, "metric-in ")) + if err != nil { + return confRead, fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case itemTrim == "no-check-zero": + confRead.noCheckZero = true + case strings.HasPrefix(itemTrim, "peer "): + confRead.peer = append(confRead.peer, strings.TrimPrefix(itemTrim, "peer ")) + case strings.HasPrefix(itemTrim, "receive "): + confRead.receive = strings.TrimPrefix(itemTrim, "receive ") + case strings.HasPrefix(itemTrim, "route-timeout "): + confRead.routeTimeout, err = strconv.Atoi(strings.TrimPrefix(itemTrim, "route-timeout ")) + if err != nil { + return confRead, fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case strings.HasPrefix(itemTrim, "send "): + confRead.send = strings.TrimPrefix(itemTrim, "send ") + case strings.HasPrefix(itemTrim, "update-interval "): + confRead.updateInterval, err = strconv.Atoi(strings.TrimPrefix(itemTrim, "update-interval ")) + if err != nil { + return confRead, fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + } + } + } + + return confRead, nil +} + +func readRipNeighborBfd(itemTrim string, bfd map[string]interface{}) error { + switch { + case strings.HasPrefix(itemTrim, "authentication algorithm "): + bfd["authentication_algorithm"] = strings.TrimPrefix(itemTrim, "authentication algorithm ") + case strings.HasPrefix(itemTrim, "authentication key-chain "): + bfd["authentication_key_chain"] = strings.Trim(strings.TrimPrefix(itemTrim, "authentication key-chain "), "\"") + case itemTrim == "authentication loose-check": + bfd["authentication_loose_check"] = true + case strings.HasPrefix(itemTrim, "detection-time threshold "): + var err error + bfd["detection_time_threshold"], err = strconv.Atoi(strings.TrimPrefix(itemTrim, "detection-time threshold ")) + if err != nil { + return fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case strings.HasPrefix(itemTrim, "minimum-interval "): + var err error + bfd["minimum_interval"], err = strconv.Atoi(strings.TrimPrefix(itemTrim, "minimum-interval ")) + if err != nil { + return fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case strings.HasPrefix(itemTrim, "minimum-receive-interval "): + var err error + bfd["minimum_receive_interval"], err = strconv.Atoi(strings.TrimPrefix(itemTrim, "minimum-receive-interval ")) + if err != nil { + return fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case strings.HasPrefix(itemTrim, "multiplier "): + var err error + bfd["multiplier"], err = strconv.Atoi(strings.TrimPrefix(itemTrim, "multiplier ")) + if err != nil { + return fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case itemTrim == "no-adaptation": + bfd["no_adaptation"] = true + case strings.HasPrefix(itemTrim, "transmit-interval minimum-interval "): + var err error + bfd["transmit_interval_minimum_interval"], err = strconv.Atoi(strings.TrimPrefix( + itemTrim, "transmit-interval minimum-interval ")) + if err != nil { + return fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case strings.HasPrefix(itemTrim, "transmit-interval threshold "): + var err error + bfd["transmit_interval_threshold"], err = strconv.Atoi(strings.TrimPrefix( + itemTrim, "transmit-interval threshold ")) + if err != nil { + return fmt.Errorf(failedConvAtoiError, itemTrim, err) + } + case strings.HasPrefix(itemTrim, "version "): + bfd["version"] = strings.TrimPrefix(itemTrim, "version ") + } + + return nil +} + +func delRipNeighbor( + name, group string, + ripNg bool, + routingInstance string, + clt *Client, + junSess *junosSession, +) error { + delPrefix := deleteLS + if routingInstance != defaultW { + delPrefix = delRoutingInstances + routingInstance + " " + } + if ripNg { + delPrefix += "protocols ripng group " + } else { + delPrefix += "protocols rip group " + } + delPrefix += "\"" + group + "\" neighbor " + name + " " + + return clt.configSet([]string{delPrefix}, junSess) +} + +func fillRipNeighborData(d *schema.ResourceData, ripNeighborOptions ripNeighborOptions) { + if tfErr := d.Set("name", ripNeighborOptions.name); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("group", ripNeighborOptions.group); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("routing_instance", ripNeighborOptions.routingInstance); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("ng", ripNeighborOptions.ng); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("any_sender", ripNeighborOptions.anySender); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("authentication_key", ripNeighborOptions.authenticationKey); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("authentication_selective_md5", ripNeighborOptions.authenticationSelectiveMD5); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("authentication_type", ripNeighborOptions.authenticationType); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("bfd_liveness_detection", ripNeighborOptions.bfdLivenessDetection); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("check_zero", ripNeighborOptions.checkZero); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("demand_circuit", ripNeighborOptions.demandCircuit); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("dynamic_peers", ripNeighborOptions.dynamicPeers); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("import", ripNeighborOptions.importPolicy); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("interface_type_p2mp", ripNeighborOptions.interfaceTypeP2mp); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("max_retrans_time", ripNeighborOptions.maxRetransTime); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("message_size", ripNeighborOptions.messageSize); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("metric_in", ripNeighborOptions.metricIn); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("no_check_zero", ripNeighborOptions.noCheckZero); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("peer", ripNeighborOptions.peer); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("receive", ripNeighborOptions.receive); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("route_timeout", ripNeighborOptions.routeTimeout); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("send", ripNeighborOptions.send); tfErr != nil { + panic(tfErr) + } + if tfErr := d.Set("update_interval", ripNeighborOptions.updateInterval); tfErr != nil { + panic(tfErr) + } +} diff --git a/junos/resource_rip_neighbor_test.go b/junos/resource_rip_neighbor_test.go new file mode 100644 index 00000000..65deda27 --- /dev/null +++ b/junos/resource_rip_neighbor_test.go @@ -0,0 +1,226 @@ +package junos_test + +import ( + "fmt" + "os" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +// export TESTACC_INTERFACE= for choose interface available else it's ge-0/0/3. +func TestAccJunosRipNeighbor_basic(t *testing.T) { + testaccRipNeigh := defaultInterfaceTestAcc + if iface := os.Getenv("TESTACC_INTERFACE"); iface != "" { + testaccRipNeigh = iface + } + if os.Getenv("TESTACC_SWITCH") == "" { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccJunosRipNeighborConfigCreate(testaccRipNeigh), + }, + { + Config: testAccJunosRipNeighborConfigUpdate(testaccRipNeigh), + }, + { + ResourceName: "junos_rip_neighbor.testacc_ripneigh", + ImportState: true, + ImportStateVerify: true, + }, + { + ResourceName: "junos_rip_neighbor.testacc_ripneigh2", + ImportState: true, + ImportStateVerify: true, + }, + { + ResourceName: "junos_rip_neighbor.testacc_ripngneigh", + ImportState: true, + ImportStateVerify: true, + }, + { + ResourceName: "junos_rip_neighbor.testacc_ripngneigh2", + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccJunosRipNeighborConfigCreate(testaccRipNeigh), + }, + }, + }) + } +} + +func testAccJunosRipNeighborConfigCreate(interFace string) string { + return fmt.Sprintf(` +resource "junos_rip_group" "testacc_ripneigh" { + name = "test_rip_group#1" +} +resource "junos_routing_instance" "testacc_ripneigh2" { + name = "testacc_ripneigh2" +} +resource "junos_rip_group" "testacc_ripneigh2" { + name = "test_rip_group#2" + routing_instance = junos_routing_instance.testacc_ripneigh2.name +} +resource "junos_rip_group" "testacc_ripngneigh" { + name = "test_ripng_group#1" + ng = true +} +resource "junos_rip_group" "testacc_ripngneigh2" { + name = "test_ripng_group#2" + ng = true + routing_instance = junos_routing_instance.testacc_ripneigh2.name +} +resource "junos_rip_neighbor" "testacc_ripneigh" { + name = "ae0.0" + group = junos_rip_group.testacc_ripneigh.name + authentication_type = "none" +} +resource "junos_rip_neighbor" "testacc_ripngneigh" { + name = "ae0.0" + ng = true + group = junos_rip_group.testacc_ripngneigh.name +} +resource "junos_interface_physical" "testacc_ripneigh2" { + name = "%s" +} +resource "junos_interface_logical" "testacc_ripneigh2" { + name = "${junos_interface_physical.testacc_ripneigh2.name}.0" + routing_instance = junos_routing_instance.testacc_ripneigh2.name +} +resource "junos_rip_neighbor" "testacc_ripneigh2" { + name = junos_interface_logical.testacc_ripneigh2.name + group = junos_rip_group.testacc_ripneigh2.name + routing_instance = junos_routing_instance.testacc_ripneigh2.name +} +resource "junos_rip_neighbor" "testacc_ripngneigh2" { + name = junos_interface_logical.testacc_ripneigh2.name + ng = true + group = junos_rip_group.testacc_ripngneigh2.name + routing_instance = junos_routing_instance.testacc_ripneigh2.name +} +`, interFace) +} + +func testAccJunosRipNeighborConfigUpdate(interFace string) string { + return fmt.Sprintf(` +resource "junos_policyoptions_policy_statement" "testacc_ripneigh" { + lifecycle { + create_before_destroy = true + } + + name = "testacc_ripneigh" + then { + action = "accept" + } +} +resource "junos_policyoptions_policy_statement" "testacc_ripneigh2" { + lifecycle { + create_before_destroy = true + } + + name = "testacc_ripneigh2" + then { + action = "reject" + } +} +resource "junos_rip_group" "testacc_ripneigh" { + name = "test_rip_group#1" +} +resource "junos_routing_instance" "testacc_ripneigh2" { + name = "testacc_ripneigh2" +} +resource "junos_rip_group" "testacc_ripneigh2" { + name = "test_rip_group#2" + routing_instance = junos_routing_instance.testacc_ripneigh2.name +} +resource "junos_rip_group" "testacc_ripngneigh" { + name = "test_ripng_group#1" + ng = true +} +resource "junos_rip_group" "testacc_ripngneigh2" { + name = "test_ripng_group#2" + ng = true + routing_instance = junos_routing_instance.testacc_ripneigh2.name +} +resource "junos_rip_neighbor" "testacc_ripneigh" { + name = "ae0.0" + group = junos_rip_group.testacc_ripneigh.name + any_sender = true + authentication_key = "testKey#1" + authentication_type = "md5" + check_zero = true + dynamic_peers = true + import = [ + junos_policyoptions_policy_statement.testacc_ripneigh.name, + ] + interface_type_p2mp = true + max_retrans_time = 111 + demand_circuit = true + peer = [ + "192.0.2.3", + "192.0.2.1", + ] + +} +resource "junos_rip_neighbor" "testacc_ripngneigh" { + name = "ae0.0" + ng = true + group = junos_rip_group.testacc_ripngneigh.name + import = [ + junos_policyoptions_policy_statement.testacc_ripneigh2.name, + junos_policyoptions_policy_statement.testacc_ripneigh.name, + ] + metric_in = 3 + receive = "none" +} +resource "junos_interface_physical" "testacc_ripneigh2" { + name = "%s" +} +resource "junos_interface_logical" "testacc_ripneigh2" { + name = "${junos_interface_physical.testacc_ripneigh2.name}.0" + routing_instance = junos_routing_instance.testacc_ripneigh2.name +} +resource "junos_rip_neighbor" "testacc_ripneigh2" { + name = junos_interface_logical.testacc_ripneigh2.name + group = junos_rip_group.testacc_ripneigh2.name + routing_instance = junos_routing_instance.testacc_ripneigh2.name + authentication_selective_md5 { + key_id = 4 + key = "testKey#4" + } + authentication_selective_md5 { + key_id = 3 + key = "testKey#3" + start_time = "2016-1-1.02:00:00" + } + bfd_liveness_detection { + authentication_loose_check = true + detection_time_threshold = 60 + minimum_interval = 16 + minimum_receive_interval = 17 + multiplier = 2 + no_adaptation = true + transmit_interval_minimum_interval = 18 + transmit_interval_threshold = 19 + version = "automatic" + } + no_check_zero = true + message_size = 200 + receive = "both" + update_interval = 30 + send = "multicast" +} +resource "junos_rip_neighbor" "testacc_ripngneigh2" { + name = junos_interface_logical.testacc_ripneigh2.name + ng = true + group = junos_rip_group.testacc_ripngneigh2.name + routing_instance = junos_routing_instance.testacc_ripneigh2.name + route_timeout = 300 + send = "none" +} +`, interFace) +} diff --git a/junos/resource_routing_instance.go b/junos/resource_routing_instance.go index 897dde68..8620cd11 100644 --- a/junos/resource_routing_instance.go +++ b/junos/resource_routing_instance.go @@ -155,48 +155,48 @@ func resourceRoutingInstance() *schema.Resource { } func resourceRoutingInstanceCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setRoutingInstance(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setRoutingInstance(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - routingInstanceExists, err := checkRoutingInstanceExists(d.Get("name").(string), m, jnprSess) + routingInstanceExists, err := checkRoutingInstanceExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if routingInstanceExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("routing-instance %v already exists", d.Get("name").(string)))...) } - if err := setRoutingInstance(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setRoutingInstance(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_routing_instance", jnprSess) + warns, err := clt.commitConf("create resource junos_routing_instance", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - routingInstanceExists, err = checkRoutingInstanceExists(d.Get("name").(string), m, jnprSess) + routingInstanceExists, err = checkRoutingInstanceExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -207,24 +207,24 @@ func resourceRoutingInstanceCreate(ctx context.Context, d *schema.ResourceData, "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceRoutingInstanceReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceRoutingInstanceReadWJunSess(d, clt, junSess)...) } func resourceRoutingInstanceRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceRoutingInstanceReadWJnprSess(d, m, jnprSess) + return resourceRoutingInstanceReadWJunSess(d, clt, junSess) } -func resourceRoutingInstanceReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceRoutingInstanceReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - instanceOptions, err := readRoutingInstance(d.Get("name").(string), m, jnprSess) + instanceOptions, err := readRoutingInstance(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -240,76 +240,76 @@ func resourceRoutingInstanceReadWJnprSess(d *schema.ResourceData, m interface{}, func resourceRoutingInstanceUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delRoutingInstanceOpts(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delRoutingInstanceOpts(d, clt, nil); err != nil { return diag.FromErr(err) } - if err := setRoutingInstance(d, m, nil); err != nil { + if err := setRoutingInstance(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delRoutingInstanceOpts(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delRoutingInstanceOpts(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setRoutingInstance(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setRoutingInstance(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_routing_instance", jnprSess) + warns, err := clt.commitConf("update resource junos_routing_instance", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceRoutingInstanceReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceRoutingInstanceReadWJunSess(d, clt, junSess)...) } func resourceRoutingInstanceDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delRoutingInstance(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delRoutingInstance(d, clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delRoutingInstance(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delRoutingInstance(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_routing_instance", jnprSess) + warns, err := clt.commitConf("delete resource junos_routing_instance", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -319,21 +319,21 @@ func resourceRoutingInstanceDelete(ctx context.Context, d *schema.ResourceData, func resourceRoutingInstanceImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - routingInstanceExists, err := checkRoutingInstanceExists(d.Id(), m, jnprSess) + routingInstanceExists, err := checkRoutingInstanceExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !routingInstanceExists { return nil, fmt.Errorf("don't find routing instance with id '%v' (id must be )", d.Id()) } - instanceOptions, err := readRoutingInstance(d.Id(), m, jnprSess) + instanceOptions, err := readRoutingInstance(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -343,9 +343,8 @@ func resourceRoutingInstanceImport(ctx context.Context, d *schema.ResourceData, return result, nil } -func checkRoutingInstanceExists(instance string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+routingInstancesWS+instance+pipeDisplaySet, jnprSess) +func checkRoutingInstanceExists(instance string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+routingInstancesWS+instance+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -356,8 +355,7 @@ func checkRoutingInstanceExists(instance string, m interface{}, jnprSess *Netcon return true, nil } -func setRoutingInstance(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setRoutingInstance(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := setRoutingInstances + d.Get("name").(string) + " " @@ -409,14 +407,13 @@ func setRoutingInstance(d *schema.ResourceData, m interface{}, jnprSess *Netconf configSet = append(configSet, setPrefix+"vtep-source-interface "+v) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readRoutingInstance(instance string, m interface{}, jnprSess *NetconfObject) (instanceOptions, error) { - sess := m.(*Session) +func readRoutingInstance(instance string, clt *Client, junSess *junosSession) (instanceOptions, error) { var confRead instanceOptions - showConfig, err := sess.command(cmdShowConfig+routingInstancesWS+instance+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+routingInstancesWS+instance+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -469,8 +466,7 @@ func readRoutingInstance(instance string, m interface{}, jnprSess *NetconfObject return confRead, nil } -func delRoutingInstanceOpts(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delRoutingInstanceOpts(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := delRoutingInstances + d.Get("name").(string) + " " configSet = append(configSet, @@ -492,15 +488,14 @@ func delRoutingInstanceOpts(d *schema.ResourceData, m interface{}, jnprSess *Net ) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func delRoutingInstance(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delRoutingInstance(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, delRoutingInstances+d.Get("name").(string)) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillRoutingInstanceData(d *schema.ResourceData, instanceOptions instanceOptions) { diff --git a/junos/resource_routing_options.go b/junos/resource_routing_options.go index 66a7e071..5b95d135 100644 --- a/junos/resource_routing_options.go +++ b/junos/resource_routing_options.go @@ -188,56 +188,56 @@ func resourceRoutingOptions() *schema.Resource { } func resourceRoutingOptionsCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setRoutingOptions(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setRoutingOptions(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId("routing_options") return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := setRoutingOptions(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setRoutingOptions(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_routing_options", jnprSess) + warns, err := clt.commitConf("create resource junos_routing_options", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.SetId("routing_options") - return append(diagWarns, resourceRoutingOptionsReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceRoutingOptionsReadWJunSess(d, clt, junSess)...) } func resourceRoutingOptionsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceRoutingOptionsReadWJnprSess(d, m, jnprSess) + return resourceRoutingOptionsReadWJunSess(d, clt, junSess) } -func resourceRoutingOptionsReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceRoutingOptionsReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - routingOptionsOptions, err := readRoutingOptions(m, jnprSess) + routingOptionsOptions, err := readRoutingOptions(clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -272,76 +272,76 @@ func resourceRoutingOptionsUpdate(ctx context.Context, d *schema.ResourceData, m }) } } - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delRoutingOptions(fwTableExportConfigSingly, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delRoutingOptions(fwTableExportConfigSingly, clt, nil); err != nil { return append(diagWarns, diag.FromErr(err)...) } - if err := setRoutingOptions(d, m, nil); err != nil { + if err := setRoutingOptions(d, clt, nil); err != nil { return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) return diagWarns } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } - if err := delRoutingOptions(fwTableExportConfigSingly, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delRoutingOptions(fwTableExportConfigSingly, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setRoutingOptions(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setRoutingOptions(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_routing_options", jnprSess) + warns, err := clt.commitConf("update resource junos_routing_options", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceRoutingOptionsReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceRoutingOptionsReadWJunSess(d, clt, junSess)...) } func resourceRoutingOptionsDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { if d.Get("clean_on_destroy").(bool) { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delRoutingOptions(d.Get("forwarding_table_export_configure_singly").(bool), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delRoutingOptions(d.Get("forwarding_table_export_configure_singly").(bool), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delRoutingOptions(d.Get("forwarding_table_export_configure_singly").(bool), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delRoutingOptions(d.Get("forwarding_table_export_configure_singly").(bool), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_routing_options", jnprSess) + warns, err := clt.commitConf("delete resource junos_routing_options", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -352,14 +352,14 @@ func resourceRoutingOptionsDelete(ctx context.Context, d *schema.ResourceData, m func resourceRoutingOptionsImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - routingOptionsOptions, err := readRoutingOptions(m, jnprSess) + routingOptionsOptions, err := readRoutingOptions(clt, junSess) if err != nil { return nil, err } @@ -370,9 +370,7 @@ func resourceRoutingOptionsImport(ctx context.Context, d *schema.ResourceData, m return result, nil } -func setRoutingOptions(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) - +func setRoutingOptions(d *schema.ResourceData, clt *Client, junSess *junosSession) error { setPrefix := "set routing-options " configSet := make([]string, 0) @@ -458,10 +456,10 @@ func setRoutingOptions(d *schema.ResourceData, m interface{}, jnprSess *NetconfO configSet = append(configSet, setPrefix+"router-id "+v) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func delRoutingOptions(fwTableExportConfigSingly bool, m interface{}, jnprSess *NetconfObject) error { +func delRoutingOptions(fwTableExportConfigSingly bool, clt *Client, junSess *junosSession) error { listLinesToDelete := []string{ "autonomous-system", "graceful-restart", @@ -488,7 +486,6 @@ func delRoutingOptions(fwTableExportConfigSingly bool, m interface{}, jnprSess * } else { listLinesToDelete = append(listLinesToDelete, "forwarding-table") } - sess := m.(*Session) configSet := make([]string, 0) delPrefix := "delete routing-options " for _, line := range listLinesToDelete { @@ -496,15 +493,14 @@ func delRoutingOptions(fwTableExportConfigSingly bool, m interface{}, jnprSess * delPrefix+line) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readRoutingOptions(m interface{}, jnprSess *NetconfObject) (routingOptionsOptions, error) { - sess := m.(*Session) +func readRoutingOptions(clt *Client, junSess *junosSession) (routingOptionsOptions, error) { var confRead routingOptionsOptions - showConfig, err := sess.command(cmdShowConfig+ - "routing-options"+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "routing-options"+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } diff --git a/junos/resource_rstp.go b/junos/resource_rstp.go index c9db07e3..ea1fe297 100644 --- a/junos/resource_rstp.go +++ b/junos/resource_rstp.go @@ -135,70 +135,70 @@ func resourceRstp() *schema.Resource { } func resourceRstpCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setRstp(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setRstp(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("routing_instance").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if d.Get("routing_instance").(string) != defaultW { - instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !instanceExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("routing instance %v doesn't exist", d.Get("routing_instance").(string)))...) } } - if err := setRstp(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setRstp(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_rstp", jnprSess) + warns, err := clt.commitConf("create resource junos_rstp", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.SetId(d.Get("routing_instance").(string)) - return append(diagWarns, resourceRstpReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceRstpReadWJunSess(d, clt, junSess)...) } func resourceRstpRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceRstpReadWJnprSess(d, m, jnprSess) + return resourceRstpReadWJunSess(d, clt, junSess) } -func resourceRstpReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) diag.Diagnostics { +func resourceRstpReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession) diag.Diagnostics { mutex.Lock() if d.Get("routing_instance").(string) != defaultW { - instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), clt, junSess) if err != nil { mutex.Unlock() @@ -211,7 +211,7 @@ func resourceRstpReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess * return nil } } - rstpOptions, err := readRstp(d.Get("routing_instance").(string), m, jnprSess) + rstpOptions, err := readRstp(d.Get("routing_instance").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -223,76 +223,76 @@ func resourceRstpReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess * func resourceRstpUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delRstp(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delRstp(d, clt, nil); err != nil { return diag.FromErr(err) } - if err := setRstp(d, m, nil); err != nil { + if err := setRstp(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delRstp(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delRstp(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setRstp(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setRstp(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_rstp", jnprSess) + warns, err := clt.commitConf("update resource junos_rstp", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceRstpReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceRstpReadWJunSess(d, clt, junSess)...) } func resourceRstpDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delRstp(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delRstp(d, clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delRstp(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delRstp(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_rstp", jnprSess) + warns, err := clt.commitConf("delete resource junos_rstp", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -302,14 +302,14 @@ func resourceRstpDelete(ctx context.Context, d *schema.ResourceData, m interface func resourceRstpImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) if d.Id() != defaultW { - instanceExists, err := checkRoutingInstanceExists(d.Id(), m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -318,7 +318,7 @@ func resourceRstpImport(ctx context.Context, d *schema.ResourceData, m interface } } result := make([]*schema.ResourceData, 1) - rstpOptions, err := readRstp(d.Id(), m, jnprSess) + rstpOptions, err := readRstp(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -328,8 +328,7 @@ func resourceRstpImport(ctx context.Context, d *schema.ResourceData, m interface return result, nil } -func setRstp(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setRstp(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := setLS if d.Get("routing_instance").(string) != defaultW { @@ -389,26 +388,25 @@ func setRstp(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) err configSet = append(configSet, setPrefix+"vpls-flush-on-topology-change") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readRstp(routingInstance string, m interface{}, jnprSess *NetconfObject) (rstpOptions, error) { - sess := m.(*Session) +func readRstp(routingInstance string, clt *Client, junSess *junosSession) (rstpOptions, error) { var confRead rstpOptions confRead.extendedSystemID = -1 var showConfig string if routingInstance == defaultW { var err error - showConfig, err = sess.command(cmdShowConfig+ - "protocols rstp"+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "protocols rstp"+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } } else { var err error - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ - "protocols rstp"+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ + "protocols rstp"+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -494,8 +492,7 @@ func readRstp(routingInstance string, m interface{}, jnprSess *NetconfObject) (r return confRead, nil } -func delRstp(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delRstp(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) delPrefix := deleteLS if d.Get("routing_instance").(string) != defaultW { @@ -535,7 +532,7 @@ func delRstp(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) err } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillRstpData(d *schema.ResourceData, rstpOptions rstpOptions) { diff --git a/junos/resource_rstp_interface.go b/junos/resource_rstp_interface.go index b47bff50..9328cdbb 100644 --- a/junos/resource_rstp_interface.go +++ b/junos/resource_rstp_interface.go @@ -96,47 +96,49 @@ func resourceRstpInterface() *schema.Resource { } func resourceRstpInterfaceCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setRstpInterface(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setRstpInterface(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string) + idSeparator + d.Get("routing_instance").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if d.Get("routing_instance").(string) != defaultW { - instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !instanceExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("routing instance %v doesn't exist", d.Get("routing_instance").(string)))...) } } rstpInterfaceExists, err := checkRstpInterfaceExists( - d.Get("name").(string), d.Get("routing_instance").(string), m, jnprSess) + d.Get("name").(string), + d.Get("routing_instance").(string), + clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if rstpInterfaceExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) if d.Get("routing_instance").(string) == defaultW { return append(diagWarns, diag.FromErr(fmt.Errorf("protocols rstp interface %v already exists", d.Get("name").(string)))...) @@ -147,20 +149,22 @@ func resourceRstpInterfaceCreate(ctx context.Context, d *schema.ResourceData, m d.Get("name").(string), d.Get("routing_instance").(string)))...) } - if err := setRstpInterface(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setRstpInterface(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_rstp_interface", jnprSess) + warns, err := clt.commitConf("create resource junos_rstp_interface", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } rstpInterfaceExists, err = checkRstpInterfaceExists( - d.Get("name").(string), d.Get("routing_instance").(string), m, jnprSess) + d.Get("name").(string), + d.Get("routing_instance").(string), + clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -177,25 +181,27 @@ func resourceRstpInterfaceCreate(ctx context.Context, d *schema.ResourceData, m "=> check your config", d.Get("name").(string), d.Get("routing_instance").(string)))...) } - return append(diagWarns, resourceRstpInterfaceReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceRstpInterfaceReadWJunSess(d, clt, junSess)...) } func resourceRstpInterfaceRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceRstpInterfaceReadWJnprSess(d, m, jnprSess) + return resourceRstpInterfaceReadWJunSess(d, clt, junSess) } -func resourceRstpInterfaceReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceRstpInterfaceReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() rstpInterfaceOptions, err := readRstpInterface( - d.Get("name").(string), d.Get("routing_instance").(string), m, jnprSess) + d.Get("name").(string), + d.Get("routing_instance").(string), + clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -211,76 +217,76 @@ func resourceRstpInterfaceReadWJnprSess(d *schema.ResourceData, m interface{}, j func resourceRstpInterfaceUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delRstpInterface(d.Get("name").(string), d.Get("routing_instance").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delRstpInterface(d.Get("name").(string), d.Get("routing_instance").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setRstpInterface(d, m, nil); err != nil { + if err := setRstpInterface(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delRstpInterface(d.Get("name").(string), d.Get("routing_instance").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delRstpInterface(d.Get("name").(string), d.Get("routing_instance").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setRstpInterface(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setRstpInterface(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_rstp_interface", jnprSess) + warns, err := clt.commitConf("update resource junos_rstp_interface", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceRstpInterfaceReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceRstpInterfaceReadWJunSess(d, clt, junSess)...) } func resourceRstpInterfaceDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delRstpInterface(d.Get("name").(string), d.Get("routing_instance").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delRstpInterface(d.Get("name").(string), d.Get("routing_instance").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delRstpInterface(d.Get("name").(string), d.Get("routing_instance").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delRstpInterface(d.Get("name").(string), d.Get("routing_instance").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_rstp_interface", jnprSess) + warns, err := clt.commitConf("delete resource junos_rstp_interface", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -290,18 +296,18 @@ func resourceRstpInterfaceDelete(ctx context.Context, d *schema.ResourceData, m func resourceRstpInterfaceImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) idSplit := strings.Split(d.Id(), idSeparator) if len(idSplit) < 2 { return nil, fmt.Errorf("missing element(s) in id with separator %v", idSeparator) } - rstpInterfaceExists, err := checkRstpInterfaceExists(idSplit[0], idSplit[1], m, jnprSess) + rstpInterfaceExists, err := checkRstpInterfaceExists(idSplit[0], idSplit[1], clt, junSess) if err != nil { return nil, err } @@ -309,7 +315,7 @@ func resourceRstpInterfaceImport(ctx context.Context, d *schema.ResourceData, m return nil, fmt.Errorf("don't find protocols rstp interface with id '%v' "+ "(id must be %s)", d.Id(), idSeparator) } - rstpInterfaceOptions, err := readRstpInterface(idSplit[0], idSplit[1], m, jnprSess) + rstpInterfaceOptions, err := readRstpInterface(idSplit[0], idSplit[1], clt, junSess) if err != nil { return nil, err } @@ -320,16 +326,15 @@ func resourceRstpInterfaceImport(ctx context.Context, d *schema.ResourceData, m return result, nil } -func checkRstpInterfaceExists(name, routingInstance string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) +func checkRstpInterfaceExists(name, routingInstance string, clt *Client, junSess *junosSession) (bool, error) { var showConfig string var err error if routingInstance == defaultW { - showConfig, err = sess.command(cmdShowConfig+ - "protocols rstp interface "+name+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "protocols rstp interface "+name+pipeDisplaySet, junSess) } else { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ - "protocols rstp interface "+name+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ + "protocols rstp interface "+name+pipeDisplaySet, junSess) } if err != nil { return false, err @@ -341,8 +346,7 @@ func checkRstpInterfaceExists(name, routingInstance string, m interface{}, jnprS return true, nil } -func setRstpInterface(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setRstpInterface(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := setLS @@ -377,22 +381,21 @@ func setRstpInterface(d *schema.ResourceData, m interface{}, jnprSess *NetconfOb configSet = append(configSet, setPrefix+"priority "+strconv.Itoa(v)) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readRstpInterface(name, routingInstance string, m interface{}, jnprSess *NetconfObject, +func readRstpInterface(name, routingInstance string, clt *Client, junSess *junosSession, ) (rstpInterfaceOptions, error) { - sess := m.(*Session) var confRead rstpInterfaceOptions confRead.priority = -1 // default -1 var showConfig string var err error if routingInstance == defaultW { - showConfig, err = sess.command(cmdShowConfig+ - "protocols rstp interface "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "protocols rstp interface "+name+pipeDisplaySetRelative, junSess) } else { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ - "protocols rstp interface "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ + "protocols rstp interface "+name+pipeDisplaySetRelative, junSess) } if err != nil { return confRead, err @@ -440,8 +443,7 @@ func readRstpInterface(name, routingInstance string, m interface{}, jnprSess *Ne return confRead, nil } -func delRstpInterface(name, routingInstance string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delRstpInterface(name, routingInstance string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) if routingInstance == defaultW { @@ -450,7 +452,7 @@ func delRstpInterface(name, routingInstance string, m interface{}, jnprSess *Net configSet = append(configSet, delRoutingInstances+routingInstance+" protocols rstp interface "+name) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillRstpInterfaceData(d *schema.ResourceData, rstpInterfaceOptions rstpInterfaceOptions) { diff --git a/junos/resource_security.go b/junos/resource_security.go index d3da1e79..237fa82b 100644 --- a/junos/resource_security.go +++ b/junos/resource_security.go @@ -837,59 +837,59 @@ func resourceSecurity() *schema.Resource { } func resourceSecurityCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSecurity(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSecurity(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId("security") return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel)) + junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := setSecurity(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurity(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security", jnprSess) + warns, err := clt.commitConf("create resource junos_security", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.SetId("security") - return append(diagWarns, resourceSecurityReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityReadWJunSess(d, clt, junSess)...) } func resourceSecurityRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSecurityReadWJnprSess(d, m, jnprSess) + return resourceSecurityReadWJunSess(d, clt, junSess) } -func resourceSecurityReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) diag.Diagnostics { +func resourceSecurityReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession) diag.Diagnostics { mutex.Lock() - securityOptions, err := readSecurity(m, jnprSess) + securityOptions, err := readSecurity(clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -901,77 +901,77 @@ func resourceSecurityReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSe func resourceSecurityUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSecurity(m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSecurity(clt, nil); err != nil { return diag.FromErr(err) } - if err := setSecurity(d, m, nil); err != nil { + if err := setSecurity(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurity(m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurity(clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSecurity(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurity(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security", jnprSess) + warns, err := clt.commitConf("update resource junos_security", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSecurityReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityReadWJunSess(d, clt, junSess)...) } func resourceSecurityDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { if d.Get("clean_on_destroy").(bool) { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSecurity(m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSecurity(clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurity(m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurity(clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security", jnprSess) + warns, err := clt.commitConf("delete resource junos_security", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -982,14 +982,14 @@ func resourceSecurityDelete(ctx context.Context, d *schema.ResourceData, m inter func resourceSecurityImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - securityOptions, err := readSecurity(m, jnprSess) + securityOptions, err := readSecurity(clt, junSess) if err != nil { return nil, err } @@ -1000,9 +1000,7 @@ func resourceSecurityImport(ctx context.Context, d *schema.ResourceData, m inter return result, nil } -func setSecurity(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) - +func setSecurity(d *schema.ResourceData, clt *Client, junSess *junosSession) error { setPrefix := "set security " configSet := make([]string, 0) @@ -1138,7 +1136,7 @@ func setSecurity(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func setSecurityAlg(alg interface{}) ([]string, error) { @@ -1758,7 +1756,7 @@ func listLinesSecurityUtm() []string { } } -func delSecurity(m interface{}, jnprSess *NetconfObject) error { +func delSecurity(clt *Client, junSess *junosSession) error { listLinesToDelete := []string{ "ike traceoptions", } @@ -1772,7 +1770,7 @@ func delSecurity(m interface{}, jnprSess *NetconfObject) error { listLinesToDelete = append(listLinesToDelete, listLinesSecurityPolicies()...) listLinesToDelete = append(listLinesToDelete, listLinesSecurityUserIdentificationAuthSource()...) listLinesToDelete = append(listLinesToDelete, listLinesSecurityUtm()...) - sess := m.(*Session) + configSet := make([]string, 0) delPrefix := "delete security " for _, line := range listLinesToDelete { @@ -1780,14 +1778,13 @@ func delSecurity(m interface{}, jnprSess *NetconfObject) error { delPrefix+line) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSecurity(m interface{}, jnprSess *NetconfObject) (securityOptions, error) { - sess := m.(*Session) +func readSecurity(clt *Client, junSess *junosSession) (securityOptions, error) { var confRead securityOptions - showConfig, err := sess.command(cmdShowConfig+"security"+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"security"+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } diff --git a/junos/resource_security_address_book.go b/junos/resource_security_address_book.go index c2c29647..55cbb728 100644 --- a/junos/resource_security_address_book.go +++ b/junos/resource_security_address_book.go @@ -185,53 +185,53 @@ func resourceSecurityAddressBook() *schema.Resource { } func resourceSecurityAddressBookCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSecurityAddressBook(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSecurityAddressBook(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security policy not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel)) + junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - addressBookExists, err := checkSecurityAddressBookExists(d.Get("name").(string), m, jnprSess) + addressBookExists, err := checkSecurityAddressBookExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if addressBookExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security address book %v already exists", d.Get("name").(string)))...) } - if err := setSecurityAddressBook(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityAddressBook(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_address_book", jnprSess) + warns, err := clt.commitConf("create resource junos_security_address_book", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - addressBookExists, err = checkSecurityAddressBookExists(d.Get("name").(string), m, jnprSess) + addressBookExists, err = checkSecurityAddressBookExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -242,24 +242,24 @@ func resourceSecurityAddressBookCreate(ctx context.Context, d *schema.ResourceDa "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceSecurityAddressBookReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityAddressBookReadWJunSess(d, clt, junSess)...) } func resourceSecurityAddressBookRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSecurityAddressBookReadWJnprSess(d, m, jnprSess) + return resourceSecurityAddressBookReadWJunSess(d, clt, junSess) } -func resourceSecurityAddressBookReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSecurityAddressBookReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - addressOptions, err := readSecurityAddressBook(d.Get("name").(string), m, jnprSess) + addressOptions, err := readSecurityAddressBook(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -275,76 +275,76 @@ func resourceSecurityAddressBookReadWJnprSess(d *schema.ResourceData, m interfac func resourceSecurityAddressBookUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSecurityAddressBook(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSecurityAddressBook(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSecurityAddressBook(d, m, nil); err != nil { + if err := setSecurityAddressBook(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityAddressBook(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityAddressBook(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSecurityAddressBook(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityAddressBook(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_address_book", jnprSess) + warns, err := clt.commitConf("update resource junos_security_address_book", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSecurityAddressBookReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityAddressBookReadWJunSess(d, clt, junSess)...) } func resourceSecurityAddressBookDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSecurityAddressBook(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSecurityAddressBook(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityAddressBook(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityAddressBook(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_address_book", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_address_book", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -354,21 +354,21 @@ func resourceSecurityAddressBookDelete(ctx context.Context, d *schema.ResourceDa func resourceSecurityAddressBookImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - securityAddressBookExists, err := checkSecurityAddressBookExists(d.Id(), m, jnprSess) + securityAddressBookExists, err := checkSecurityAddressBookExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !securityAddressBookExists { return nil, fmt.Errorf("don't find address book with id '%v' (id must be )", d.Id()) } - addressOptions, err := readSecurityAddressBook(d.Id(), m, jnprSess) + addressOptions, err := readSecurityAddressBook(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -379,11 +379,9 @@ func resourceSecurityAddressBookImport(ctx context.Context, d *schema.ResourceDa return result, nil } -func checkSecurityAddressBookExists(addrBook string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - - showConfig, err := sess.command(cmdShowConfig+ - "security address-book "+addrBook+pipeDisplaySet, jnprSess) +func checkSecurityAddressBookExists(addrBook string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "security address-book "+addrBook+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -394,8 +392,7 @@ func checkSecurityAddressBookExists(addrBook string, m interface{}, jnprSess *Ne return true, nil } -func setSecurityAddressBook(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSecurityAddressBook(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set security address-book " + d.Get("name").(string) @@ -481,15 +478,14 @@ func setSecurityAddressBook(d *schema.ResourceData, m interface{}, jnprSess *Net } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSecurityAddressBook(addrBook string, m interface{}, jnprSess *NetconfObject) (addressBookOptions, error) { - sess := m.(*Session) +func readSecurityAddressBook(addrBook string, clt *Client, junSess *junosSession) (addressBookOptions, error) { var confRead addressBookOptions - showConfig, err := sess.command(cmdShowConfig+ - "security address-book "+addrBook+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security address-book "+addrBook+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -575,12 +571,11 @@ func readSecurityAddressBook(addrBook string, m interface{}, jnprSess *NetconfOb return confRead, nil } -func delSecurityAddressBook(addrBook string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSecurityAddressBook(addrBook string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete security address-book "+addrBook) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSecurityAddressBookData(d *schema.ResourceData, addressOptions addressBookOptions) { diff --git a/junos/resource_security_dynamic_address_feed_server.go b/junos/resource_security_dynamic_address_feed_server.go index 3d52fe39..01a73639 100644 --- a/junos/resource_security_dynamic_address_feed_server.go +++ b/junos/resource_security_dynamic_address_feed_server.go @@ -114,56 +114,58 @@ func resourceSecurityDynamicAddressFeedServer() *schema.Resource { func resourceSecurityDynamicAddressFeedServerCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSecurityDynamicAddressFeedServer(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSecurityDynamicAddressFeedServer(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security dynamic-address feed-server "+ - "not compatible with Junos device %s", jnprSess.SystemInformation.HardwareModel)) + "not compatible with Junos device %s", junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics securityDynamicAddressFeedServerExists, err := checkSecurityDynamicAddressFeedServersExists( - d.Get("name").(string), m, jnprSess) + d.Get("name").(string), + clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if securityDynamicAddressFeedServerExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf( "security dynamic-address feed-server %v already exists", d.Get("name").(string)))...) } - if err := setSecurityDynamicAddressFeedServer(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityDynamicAddressFeedServer(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_dynamic_address_feed_server", jnprSess) + warns, err := clt.commitConf("create resource junos_security_dynamic_address_feed_server", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } securityDynamicAddressFeedServerExists, err = checkSecurityDynamicAddressFeedServersExists( - d.Get("name").(string), m, jnprSess) + d.Get("name").(string), + clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -174,26 +176,26 @@ func resourceSecurityDynamicAddressFeedServerCreate(ctx context.Context, d *sche "not exists after commit => check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceSecurityDynamicAddressFeedServerReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityDynamicAddressFeedServerReadWJunSess(d, clt, junSess)...) } func resourceSecurityDynamicAddressFeedServerRead(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSecurityDynamicAddressFeedServerReadWJnprSess(d, m, jnprSess) + return resourceSecurityDynamicAddressFeedServerReadWJunSess(d, clt, junSess) } -func resourceSecurityDynamicAddressFeedServerReadWJnprSess( - d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSecurityDynamicAddressFeedServerReadWJunSess( + d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - dynamicAddressFeedServerOptions, err := readSecurityDynamicAddressFeedServer(d.Get("name").(string), m, jnprSess) + dynamicAddressFeedServerOptions, err := readSecurityDynamicAddressFeedServer(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -210,77 +212,77 @@ func resourceSecurityDynamicAddressFeedServerReadWJnprSess( func resourceSecurityDynamicAddressFeedServerUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSecurityDynamicAddressFeedServer(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSecurityDynamicAddressFeedServer(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSecurityDynamicAddressFeedServer(d, m, nil); err != nil { + if err := setSecurityDynamicAddressFeedServer(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityDynamicAddressFeedServer(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityDynamicAddressFeedServer(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSecurityDynamicAddressFeedServer(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityDynamicAddressFeedServer(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_dynamic_address_feed_server", jnprSess) + warns, err := clt.commitConf("update resource junos_security_dynamic_address_feed_server", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSecurityDynamicAddressFeedServerReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityDynamicAddressFeedServerReadWJunSess(d, clt, junSess)...) } func resourceSecurityDynamicAddressFeedServerDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSecurityDynamicAddressFeedServer(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSecurityDynamicAddressFeedServer(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityDynamicAddressFeedServer(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityDynamicAddressFeedServer(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_dynamic_address_feed_server", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_dynamic_address_feed_server", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -290,21 +292,21 @@ func resourceSecurityDynamicAddressFeedServerDelete(ctx context.Context, d *sche func resourceSecurityDynamicAddressFeedServerImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - securityDynamicAddressFeedServerExists, err := checkSecurityDynamicAddressFeedServersExists(d.Id(), m, jnprSess) + securityDynamicAddressFeedServerExists, err := checkSecurityDynamicAddressFeedServersExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !securityDynamicAddressFeedServerExists { return nil, fmt.Errorf("security dynamic-address feed-server with id '%v' (id must be )", d.Id()) } - dynamicAddressFeedServerOptions, err := readSecurityDynamicAddressFeedServer(d.Id(), m, jnprSess) + dynamicAddressFeedServerOptions, err := readSecurityDynamicAddressFeedServer(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -315,10 +317,9 @@ func resourceSecurityDynamicAddressFeedServerImport(ctx context.Context, d *sche return result, nil } -func checkSecurityDynamicAddressFeedServersExists(name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "security dynamic-address feed-server "+name+pipeDisplaySet, jnprSess) +func checkSecurityDynamicAddressFeedServersExists(name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "security dynamic-address feed-server "+name+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -329,8 +330,7 @@ func checkSecurityDynamicAddressFeedServersExists(name string, m interface{}, jn return true, nil } -func setSecurityDynamicAddressFeedServer(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSecurityDynamicAddressFeedServer(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set security dynamic-address feed-server " + d.Get("name").(string) + " " @@ -377,18 +377,17 @@ func setSecurityDynamicAddressFeedServer(d *schema.ResourceData, m interface{}, configSet = append(configSet, setPrefix+"validate-certificate-attributes subject-or-subject-alternative-names") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSecurityDynamicAddressFeedServer(name string, m interface{}, jnprSess *NetconfObject, +func readSecurityDynamicAddressFeedServer(name string, clt *Client, junSess *junosSession, ) (dynamicAddressFeedServerOptions, error) { - sess := m.(*Session) var confRead dynamicAddressFeedServerOptions // default -1 confRead.holdInterval = -1 - showConfig, err := sess.command(cmdShowConfig+ - "security dynamic-address feed-server "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security dynamic-address feed-server "+name+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -462,11 +461,10 @@ func readSecurityDynamicAddressFeedServer(name string, m interface{}, jnprSess * return confRead, nil } -func delSecurityDynamicAddressFeedServer(name string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSecurityDynamicAddressFeedServer(name string, clt *Client, junSess *junosSession) error { configSet := []string{"delete security dynamic-address feed-server " + name} - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSecurityDynamicAddressFeedServerData( diff --git a/junos/resource_security_dynamic_address_name.go b/junos/resource_security_dynamic_address_name.go index 17446318..a519ad8d 100644 --- a/junos/resource_security_dynamic_address_name.go +++ b/junos/resource_security_dynamic_address_name.go @@ -90,54 +90,54 @@ func resourceSecurityDynamicAddressName() *schema.Resource { func resourceSecurityDynamicAddressNameCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSecurityDynamicAddressName(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSecurityDynamicAddressName(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security dynamic-address address-name "+ - "not compatible with Junos device %s", jnprSess.SystemInformation.HardwareModel)) + "not compatible with Junos device %s", junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - securityDynamicAddressNameExists, err := checkSecurityDynamicAddressNamesExists(d.Get("name").(string), m, jnprSess) + securityDynamicAddressNameExists, err := checkSecurityDynamicAddressNamesExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if securityDynamicAddressNameExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf( "security dynamic-address address-name %v already exists", d.Get("name").(string)))...) } - if err := setSecurityDynamicAddressName(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityDynamicAddressName(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_dynamic_address_name", jnprSess) + warns, err := clt.commitConf("create resource junos_security_dynamic_address_name", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - securityDynamicAddressNameExists, err = checkSecurityDynamicAddressNamesExists(d.Get("name").(string), m, jnprSess) + securityDynamicAddressNameExists, err = checkSecurityDynamicAddressNamesExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -148,25 +148,25 @@ func resourceSecurityDynamicAddressNameCreate(ctx context.Context, d *schema.Res "not exists after commit => check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceSecurityDynamicAddressNameReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityDynamicAddressNameReadWJunSess(d, clt, junSess)...) } func resourceSecurityDynamicAddressNameRead(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSecurityDynamicAddressNameReadWJnprSess(d, m, jnprSess) + return resourceSecurityDynamicAddressNameReadWJunSess(d, clt, junSess) } -func resourceSecurityDynamicAddressNameReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSecurityDynamicAddressNameReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - dynamicAddressNameOptions, err := readSecurityDynamicAddressName(d.Get("name").(string), m, jnprSess) + dynamicAddressNameOptions, err := readSecurityDynamicAddressName(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -183,77 +183,77 @@ func resourceSecurityDynamicAddressNameReadWJnprSess(d *schema.ResourceData, m i func resourceSecurityDynamicAddressNameUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSecurityDynamicAddressName(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSecurityDynamicAddressName(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSecurityDynamicAddressName(d, m, nil); err != nil { + if err := setSecurityDynamicAddressName(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityDynamicAddressName(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityDynamicAddressName(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSecurityDynamicAddressName(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityDynamicAddressName(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_dynamic_address_name", jnprSess) + warns, err := clt.commitConf("update resource junos_security_dynamic_address_name", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSecurityDynamicAddressNameReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityDynamicAddressNameReadWJunSess(d, clt, junSess)...) } func resourceSecurityDynamicAddressNameDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSecurityDynamicAddressName(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSecurityDynamicAddressName(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityDynamicAddressName(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityDynamicAddressName(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_dynamic_address_name", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_dynamic_address_name", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -263,21 +263,21 @@ func resourceSecurityDynamicAddressNameDelete(ctx context.Context, d *schema.Res func resourceSecurityDynamicAddressNameImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - securityDynamicAddressNameExists, err := checkSecurityDynamicAddressNamesExists(d.Id(), m, jnprSess) + securityDynamicAddressNameExists, err := checkSecurityDynamicAddressNamesExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !securityDynamicAddressNameExists { return nil, fmt.Errorf("security dynamic-address address-name with id '%v' (id must be )", d.Id()) } - dynamicAddressNameOptions, err := readSecurityDynamicAddressName(d.Id(), m, jnprSess) + dynamicAddressNameOptions, err := readSecurityDynamicAddressName(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -288,10 +288,9 @@ func resourceSecurityDynamicAddressNameImport(ctx context.Context, d *schema.Res return result, nil } -func checkSecurityDynamicAddressNamesExists(name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "security dynamic-address address-name "+name+pipeDisplaySet, jnprSess) +func checkSecurityDynamicAddressNamesExists(name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "security dynamic-address address-name "+name+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -302,8 +301,7 @@ func checkSecurityDynamicAddressNamesExists(name string, m interface{}, jnprSess return true, nil } -func setSecurityDynamicAddressName(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSecurityDynamicAddressName(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set security dynamic-address address-name " + d.Get("name").(string) + " " @@ -335,16 +333,15 @@ func setSecurityDynamicAddressName(d *schema.ResourceData, m interface{}, jnprSe } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSecurityDynamicAddressName(name string, m interface{}, jnprSess *NetconfObject, +func readSecurityDynamicAddressName(name string, clt *Client, junSess *junosSession, ) (dynamicAddressNameOptions, error) { - sess := m.(*Session) var confRead dynamicAddressNameOptions - showConfig, err := sess.command(cmdShowConfig+ - "security dynamic-address address-name "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security dynamic-address address-name "+name+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -403,11 +400,10 @@ func readSecurityDynamicAddressName(name string, m interface{}, jnprSess *Netcon return confRead, nil } -func delSecurityDynamicAddressName(name string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSecurityDynamicAddressName(name string, clt *Client, junSess *junosSession) error { configSet := []string{"delete security dynamic-address address-name " + name} - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSecurityDynamicAddressNameData(d *schema.ResourceData, dynamicAddressNameOptions dynamicAddressNameOptions, diff --git a/junos/resource_security_global_policy.go b/junos/resource_security_global_policy.go index 56c51c50..b9de0f15 100644 --- a/junos/resource_security_global_policy.go +++ b/junos/resource_security_global_policy.go @@ -187,72 +187,72 @@ func resourceSecurityGlobalPolicy() *schema.Resource { } func resourceSecurityGlobalPolicyCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSecurityGlobalPolicy(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSecurityGlobalPolicy(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId("security_global_policy") return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security policies global not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel)) + junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - glbPolicy, err := readSecurityGlobalPolicy(m, jnprSess) + glbPolicy, err := readSecurityGlobalPolicy(clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if len(glbPolicy.policy) != 0 { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security policies global already set"))...) } - if err := setSecurityGlobalPolicy(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityGlobalPolicy(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_global_policy", jnprSess) + warns, err := clt.commitConf("create resource junos_security_global_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.SetId("security_global_policy") - return append(diagWarns, resourceSecurityGlobalPolicyReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityGlobalPolicyReadWJunSess(d, clt, junSess)...) } func resourceSecurityGlobalPolicyRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSecurityGlobalPolicyReadWJnprSess(d, m, jnprSess) + return resourceSecurityGlobalPolicyReadWJunSess(d, clt, junSess) } -func resourceSecurityGlobalPolicyReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSecurityGlobalPolicyReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - globalPolicyOptions, err := readSecurityGlobalPolicy(m, jnprSess) + globalPolicyOptions, err := readSecurityGlobalPolicy(clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -264,77 +264,77 @@ func resourceSecurityGlobalPolicyReadWJnprSess(d *schema.ResourceData, m interfa func resourceSecurityGlobalPolicyUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSecurityGlobalPolicy(m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSecurityGlobalPolicy(clt, nil); err != nil { return diag.FromErr(err) } - if err := setSecurityGlobalPolicy(d, m, nil); err != nil { + if err := setSecurityGlobalPolicy(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityGlobalPolicy(m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityGlobalPolicy(clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSecurityGlobalPolicy(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityGlobalPolicy(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_global_policy", jnprSess) + warns, err := clt.commitConf("update resource junos_security_global_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSecurityGlobalPolicyReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityGlobalPolicyReadWJunSess(d, clt, junSess)...) } func resourceSecurityGlobalPolicyDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSecurityGlobalPolicy(m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSecurityGlobalPolicy(clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityGlobalPolicy(m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityGlobalPolicy(clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_global_policy", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_global_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -344,14 +344,14 @@ func resourceSecurityGlobalPolicyDelete(ctx context.Context, d *schema.ResourceD func resourceSecurityGlobalPolicyImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - globalPolicyOptions, err := readSecurityGlobalPolicy(m, jnprSess) + globalPolicyOptions, err := readSecurityGlobalPolicy(clt, junSess) if err != nil { return nil, err } @@ -361,8 +361,7 @@ func resourceSecurityGlobalPolicyImport(ctx context.Context, d *schema.ResourceD return result, nil } -func setSecurityGlobalPolicy(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSecurityGlobalPolicy(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set security policies global policy " @@ -433,14 +432,13 @@ func setSecurityGlobalPolicy(d *schema.ResourceData, m interface{}, jnprSess *Ne } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSecurityGlobalPolicy(m interface{}, jnprSess *NetconfObject) (globalPolicyOptions, error) { - sess := m.(*Session) +func readSecurityGlobalPolicy(clt *Client, junSess *junosSession) (globalPolicyOptions, error) { var confRead globalPolicyOptions - showConfig, err := sess.command(cmdShowConfig+"security policies global"+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"security policies global"+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -531,12 +529,11 @@ func readSecurityGlobalPolicy(m interface{}, jnprSess *NetconfObject) (globalPol return confRead, nil } -func delSecurityGlobalPolicy(m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSecurityGlobalPolicy(clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete security policies global") - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSecurityGlobalPolicyData(d *schema.ResourceData, globalPolicyOptions globalPolicyOptions) { diff --git a/junos/resource_security_idp_custom_attack.go b/junos/resource_security_idp_custom_attack.go index d4bf43d6..5f55b555 100644 --- a/junos/resource_security_idp_custom_attack.go +++ b/junos/resource_security_idp_custom_attack.go @@ -817,53 +817,53 @@ func schemaSecurityIdpCustomAttackTypeSignature(chain bool) map[string]*schema.S func resourceSecurityIdpCustomAttackCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSecurityIdpCustomAttack(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSecurityIdpCustomAttack(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security idp custom-attack not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel)) + junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - idpCustomAttackExists, err := checkSecurityIdpCustomAttackExists(d.Get("name").(string), m, jnprSess) + idpCustomAttackExists, err := checkSecurityIdpCustomAttackExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if idpCustomAttackExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security idp custom-attack %v already exists", d.Get("name").(string)))...) } - if err := setSecurityIdpCustomAttack(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityIdpCustomAttack(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_idp_custom_attack", jnprSess) + warns, err := clt.commitConf("create resource junos_security_idp_custom_attack", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - idpCustomAttackExists, err = checkSecurityIdpCustomAttackExists(d.Get("name").(string), m, jnprSess) + idpCustomAttackExists, err = checkSecurityIdpCustomAttackExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -874,24 +874,24 @@ func resourceSecurityIdpCustomAttackCreate(ctx context.Context, d *schema.Resour "not exists after commit => check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceSecurityIdpCustomAttackReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityIdpCustomAttackReadWJunSess(d, clt, junSess)...) } func resourceSecurityIdpCustomAttackRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSecurityIdpCustomAttackReadWJnprSess(d, m, jnprSess) + return resourceSecurityIdpCustomAttackReadWJunSess(d, clt, junSess) } -func resourceSecurityIdpCustomAttackReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSecurityIdpCustomAttackReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - idpCustomAttackOptions, err := readSecurityIdpCustomAttack(d.Get("name").(string), m, jnprSess) + idpCustomAttackOptions, err := readSecurityIdpCustomAttack(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -908,77 +908,77 @@ func resourceSecurityIdpCustomAttackReadWJnprSess(d *schema.ResourceData, m inte func resourceSecurityIdpCustomAttackUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSecurityIdpCustomAttack(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSecurityIdpCustomAttack(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSecurityIdpCustomAttack(d, m, nil); err != nil { + if err := setSecurityIdpCustomAttack(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityIdpCustomAttack(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityIdpCustomAttack(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSecurityIdpCustomAttack(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityIdpCustomAttack(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_idp_custom_attack", jnprSess) + warns, err := clt.commitConf("update resource junos_security_idp_custom_attack", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSecurityIdpCustomAttackReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityIdpCustomAttackReadWJunSess(d, clt, junSess)...) } func resourceSecurityIdpCustomAttackDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSecurityIdpCustomAttack(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSecurityIdpCustomAttack(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityIdpCustomAttack(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityIdpCustomAttack(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_idp_custom_attack", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_idp_custom_attack", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -988,21 +988,21 @@ func resourceSecurityIdpCustomAttackDelete(ctx context.Context, d *schema.Resour func resourceSecurityIdpCustomAttackImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - idpCustomAttackExists, err := checkSecurityIdpCustomAttackExists(d.Id(), m, jnprSess) + idpCustomAttackExists, err := checkSecurityIdpCustomAttackExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !idpCustomAttackExists { return nil, fmt.Errorf("don't find security idp custom-attack with id '%v' (id must be )", d.Id()) } - idpCustomAttackOptions, err := readSecurityIdpCustomAttack(d.Id(), m, jnprSess) + idpCustomAttackOptions, err := readSecurityIdpCustomAttack(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -1013,10 +1013,9 @@ func resourceSecurityIdpCustomAttackImport(ctx context.Context, d *schema.Resour return result, nil } -func checkSecurityIdpCustomAttackExists(customAttack string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "security idp custom-attack \""+customAttack+"\""+pipeDisplaySet, jnprSess) +func checkSecurityIdpCustomAttackExists(customAttack string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "security idp custom-attack \""+customAttack+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -1027,8 +1026,7 @@ func checkSecurityIdpCustomAttackExists(customAttack string, m interface{}, jnpr return true, nil } -func setSecurityIdpCustomAttack(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSecurityIdpCustomAttack(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set security idp custom-attack \"" + d.Get("name").(string) + "\" " @@ -1101,7 +1099,7 @@ func setSecurityIdpCustomAttack(d *schema.ResourceData, m interface{}, jnprSess configSet = append(configSet, setPrefix+"time-binding scope "+v) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func setSecurityIdpCustomAttackTypeAnomaly(setPrefixOrigin string, attackAnomaly map[string]interface{}, @@ -1526,14 +1524,13 @@ func setSecurityIdpCustomAttackTypeSignatureProtoUDP(setPrefixOrigin string, pro return configSet } -func readSecurityIdpCustomAttack(customAttack string, m interface{}, jnprSess *NetconfObject, +func readSecurityIdpCustomAttack(customAttack string, clt *Client, junSess *junosSession, ) (idpCustomAttackOptions, error) { - sess := m.(*Session) var confRead idpCustomAttackOptions confRead.timeBindingCount = -1 // default to -1 - showConfig, err := sess.command(cmdShowConfig+ - "security idp custom-attack \""+customAttack+"\""+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security idp custom-attack \""+customAttack+"\""+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -2254,11 +2251,10 @@ func readSecurityIdpCustomAttackTypeSignatureProtoUDP(itemTrim string, protoUDP return nil } -func delSecurityIdpCustomAttack(customAttack string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSecurityIdpCustomAttack(customAttack string, clt *Client, junSess *junosSession) error { configSet := []string{"delete security idp custom-attack \"" + customAttack + "\""} - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSecurityIdpCustomAttackData(d *schema.ResourceData, idpCustomAttackOptions idpCustomAttackOptions) { diff --git a/junos/resource_security_idp_custom_attack_group.go b/junos/resource_security_idp_custom_attack_group.go index c3af51c6..6c8f57e0 100644 --- a/junos/resource_security_idp_custom_attack_group.go +++ b/junos/resource_security_idp_custom_attack_group.go @@ -41,53 +41,53 @@ func resourceSecurityIdpCustomAttackGroup() *schema.Resource { func resourceSecurityIdpCustomAttackGroupCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSecurityIdpCustomAttackGroup(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSecurityIdpCustomAttackGroup(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security idp custom-attack-group not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel)) + junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - idpCustomAttackGroupExists, err := checkSecurityIdpCustomAttackGroupExists(d.Get("name").(string), m, jnprSess) + idpCustomAttackGroupExists, err := checkSecurityIdpCustomAttackGroupExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if idpCustomAttackGroupExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security idp custom-attack-group %v already exists", d.Get("name").(string)))...) } - if err := setSecurityIdpCustomAttackGroup(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityIdpCustomAttackGroup(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_idp_custom_attack_group", jnprSess) + warns, err := clt.commitConf("create resource junos_security_idp_custom_attack_group", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - idpCustomAttackGroupExists, err = checkSecurityIdpCustomAttackGroupExists(d.Get("name").(string), m, jnprSess) + idpCustomAttackGroupExists, err = checkSecurityIdpCustomAttackGroupExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -98,25 +98,25 @@ func resourceSecurityIdpCustomAttackGroupCreate(ctx context.Context, d *schema.R "not exists after commit => check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceSecurityIdpCustomAttackGroupReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityIdpCustomAttackGroupReadWJunSess(d, clt, junSess)...) } func resourceSecurityIdpCustomAttackGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSecurityIdpCustomAttackGroupReadWJnprSess(d, m, jnprSess) + return resourceSecurityIdpCustomAttackGroupReadWJunSess(d, clt, junSess) } -func resourceSecurityIdpCustomAttackGroupReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSecurityIdpCustomAttackGroupReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - idpCustomAttackGroupOptions, err := readSecurityIdpCustomAttackGroup(d.Get("name").(string), m, jnprSess) + idpCustomAttackGroupOptions, err := readSecurityIdpCustomAttackGroup(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -133,12 +133,12 @@ func resourceSecurityIdpCustomAttackGroupReadWJnprSess(d *schema.ResourceData, m func resourceSecurityIdpCustomAttackGroupUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSecurityIdpCustomAttackGroup(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSecurityIdpCustomAttackGroup(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSecurityIdpCustomAttackGroup(d, m, nil); err != nil { + if err := setSecurityIdpCustomAttackGroup(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) @@ -146,65 +146,65 @@ func resourceSecurityIdpCustomAttackGroupUpdate(ctx context.Context, d *schema.R return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityIdpCustomAttackGroup(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityIdpCustomAttackGroup(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSecurityIdpCustomAttackGroup(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityIdpCustomAttackGroup(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_idp_custom_attack_group", jnprSess) + warns, err := clt.commitConf("update resource junos_security_idp_custom_attack_group", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSecurityIdpCustomAttackGroupReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityIdpCustomAttackGroupReadWJunSess(d, clt, junSess)...) } func resourceSecurityIdpCustomAttackGroupDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSecurityIdpCustomAttackGroup(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSecurityIdpCustomAttackGroup(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityIdpCustomAttackGroup(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityIdpCustomAttackGroup(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_idp_custom_attack_group", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_idp_custom_attack_group", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -214,21 +214,21 @@ func resourceSecurityIdpCustomAttackGroupDelete(ctx context.Context, d *schema.R func resourceSecurityIdpCustomAttackGroupImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - idpCustomAttackGroupExists, err := checkSecurityIdpCustomAttackGroupExists(d.Id(), m, jnprSess) + idpCustomAttackGroupExists, err := checkSecurityIdpCustomAttackGroupExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !idpCustomAttackGroupExists { return nil, fmt.Errorf("don't find security idp custom-attack-group with id '%v' (id must be )", d.Id()) } - idpCustomAttackGroupOptions, err := readSecurityIdpCustomAttackGroup(d.Id(), m, jnprSess) + idpCustomAttackGroupOptions, err := readSecurityIdpCustomAttackGroup(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -239,11 +239,10 @@ func resourceSecurityIdpCustomAttackGroupImport(ctx context.Context, d *schema.R return result, nil } -func checkSecurityIdpCustomAttackGroupExists(customAttackGroup string, m interface{}, jnprSess *NetconfObject, +func checkSecurityIdpCustomAttackGroupExists(customAttackGroup string, clt *Client, junSess *junosSession, ) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "security idp custom-attack-group \""+customAttackGroup+"\""+pipeDisplaySet, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security idp custom-attack-group \""+customAttackGroup+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -254,8 +253,7 @@ func checkSecurityIdpCustomAttackGroupExists(customAttackGroup string, m interfa return true, nil } -func setSecurityIdpCustomAttackGroup(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSecurityIdpCustomAttackGroup(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set security idp custom-attack-group \"" + d.Get("name").(string) + "\" " @@ -264,16 +262,15 @@ func setSecurityIdpCustomAttackGroup(d *schema.ResourceData, m interface{}, jnpr configSet = append(configSet, setPrefix+"group-members \""+v+"\"") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSecurityIdpCustomAttackGroup(customAttackGroup string, m interface{}, jnprSess *NetconfObject, +func readSecurityIdpCustomAttackGroup(customAttackGroup string, clt *Client, junSess *junosSession, ) (idpCustomAttackGroupOptions, error) { - sess := m.(*Session) var confRead idpCustomAttackGroupOptions - showConfig, err := sess.command(cmdShowConfig+ - "security idp custom-attack-group \""+customAttackGroup+"\""+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security idp custom-attack-group \""+customAttackGroup+"\""+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -296,11 +293,10 @@ func readSecurityIdpCustomAttackGroup(customAttackGroup string, m interface{}, j return confRead, nil } -func delSecurityIdpCustomAttackGroup(customAttack string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSecurityIdpCustomAttackGroup(customAttack string, clt *Client, junSess *junosSession) error { configSet := []string{"delete security idp custom-attack-group \"" + customAttack + "\""} - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSecurityIdpCustomAttackGroupData( diff --git a/junos/resource_security_idp_policy.go b/junos/resource_security_idp_policy.go index 712a3f52..c20ea3e6 100644 --- a/junos/resource_security_idp_policy.go +++ b/junos/resource_security_idp_policy.go @@ -278,54 +278,54 @@ func schemaSecurityIdpPolicyRuleMatch(exempt bool) map[string]*schema.Schema { } func resourceSecurityIdpPolicyCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSecurityIdpPolicy(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSecurityIdpPolicy(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security idp policy not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel)) + junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - idpPolicyExists, err := checkSecurityIdpPolicyExists(d.Get("name").(string), m, jnprSess) + idpPolicyExists, err := checkSecurityIdpPolicyExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if idpPolicyExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security idp idp-policy %v already exists", d.Get("name").(string)))...) } - if err := setSecurityIdpPolicy(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityIdpPolicy(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_idp_policy", jnprSess) + warns, err := clt.commitConf("create resource junos_security_idp_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - idpPolicyExists, err = checkSecurityIdpPolicyExists(d.Get("name").(string), m, jnprSess) + idpPolicyExists, err = checkSecurityIdpPolicyExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -336,24 +336,24 @@ func resourceSecurityIdpPolicyCreate(ctx context.Context, d *schema.ResourceData "not exists after commit => check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceSecurityIdpPolicyReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityIdpPolicyReadWJunSess(d, clt, junSess)...) } func resourceSecurityIdpPolicyRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSecurityIdpPolicyReadWJnprSess(d, m, jnprSess) + return resourceSecurityIdpPolicyReadWJunSess(d, clt, junSess) } -func resourceSecurityIdpPolicyReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSecurityIdpPolicyReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - idpPolicyOptions, err := readSecurityIdpPolicy(d.Get("name").(string), m, jnprSess) + idpPolicyOptions, err := readSecurityIdpPolicy(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -369,76 +369,76 @@ func resourceSecurityIdpPolicyReadWJnprSess(d *schema.ResourceData, m interface{ func resourceSecurityIdpPolicyUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSecurityIdpPolicy(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSecurityIdpPolicy(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSecurityIdpPolicy(d, m, nil); err != nil { + if err := setSecurityIdpPolicy(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityIdpPolicy(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityIdpPolicy(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSecurityIdpPolicy(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityIdpPolicy(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_idp_policy", jnprSess) + warns, err := clt.commitConf("update resource junos_security_idp_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSecurityIdpPolicyReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityIdpPolicyReadWJunSess(d, clt, junSess)...) } func resourceSecurityIdpPolicyDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSecurityIdpPolicy(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSecurityIdpPolicy(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityIdpPolicy(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityIdpPolicy(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_idp_policy", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_idp_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -448,21 +448,21 @@ func resourceSecurityIdpPolicyDelete(ctx context.Context, d *schema.ResourceData func resourceSecurityIdpPolicyImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - idpPolicyExists, err := checkSecurityIdpPolicyExists(d.Id(), m, jnprSess) + idpPolicyExists, err := checkSecurityIdpPolicyExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !idpPolicyExists { return nil, fmt.Errorf("don't find security idp idp-policy with id '%v' (id must be )", d.Id()) } - idpPolicyOptions, err := readSecurityIdpPolicy(d.Id(), m, jnprSess) + idpPolicyOptions, err := readSecurityIdpPolicy(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -473,10 +473,9 @@ func resourceSecurityIdpPolicyImport(ctx context.Context, d *schema.ResourceData return result, nil } -func checkSecurityIdpPolicyExists(policy string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "security idp idp-policy \""+policy+"\""+pipeDisplaySet, jnprSess) +func checkSecurityIdpPolicyExists(policy string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "security idp idp-policy \""+policy+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -487,8 +486,7 @@ func checkSecurityIdpPolicyExists(policy string, m interface{}, jnprSess *Netcon return true, nil } -func setSecurityIdpPolicy(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSecurityIdpPolicy(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set security idp idp-policy \"" + d.Get("name").(string) + "\" " @@ -520,7 +518,7 @@ func setSecurityIdpPolicy(d *schema.ResourceData, m interface{}, jnprSess *Netco configSet = append(configSet, sets...) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func setSecurityIdpPolicyExemptRule(setPrefix string, rule map[string]interface{}) ([]string, error) { @@ -739,13 +737,12 @@ func setSecurityIdpPolicyIpsRule(setPrefix string, rule map[string]interface{}) return configSet, nil } -func readSecurityIdpPolicy(policy string, m interface{}, jnprSess *NetconfObject, +func readSecurityIdpPolicy(policy string, clt *Client, junSess *junosSession, ) (idpPolicyOptions, error) { - sess := m.(*Session) var confRead idpPolicyOptions - showConfig, err := sess.command(cmdShowConfig+ - "security idp idp-policy \""+policy+"\""+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security idp idp-policy \""+policy+"\""+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -1002,11 +999,10 @@ func readSecurityIdpPolicy(policy string, m interface{}, jnprSess *NetconfObject return confRead, nil } -func delSecurityIdpPolicy(policy string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSecurityIdpPolicy(policy string, clt *Client, junSess *junosSession) error { configSet := []string{"delete security idp idp-policy \"" + policy + "\""} - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSecurityIdpPolicyData(d *schema.ResourceData, idpPolicyOptions idpPolicyOptions) { diff --git a/junos/resource_security_ike_gateway.go b/junos/resource_security_ike_gateway.go index 5da346cd..bd1fda9e 100644 --- a/junos/resource_security_ike_gateway.go +++ b/junos/resource_security_ike_gateway.go @@ -277,53 +277,53 @@ func resourceIkeGateway() *schema.Resource { } func resourceIkeGatewayCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setIkeGateway(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setIkeGateway(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security ike gateway not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel)) + junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - ikeGatewayExists, err := checkIkeGatewayExists(d.Get("name").(string), m, jnprSess) + ikeGatewayExists, err := checkIkeGatewayExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if ikeGatewayExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security ike gateway %v already exists", d.Get("name").(string)))...) } - if err := setIkeGateway(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setIkeGateway(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_ike_gateway", jnprSess) + warns, err := clt.commitConf("create resource junos_security_ike_gateway", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - ikeGatewayExists, err = checkIkeGatewayExists(d.Get("name").(string), m, jnprSess) + ikeGatewayExists, err = checkIkeGatewayExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -334,23 +334,23 @@ func resourceIkeGatewayCreate(ctx context.Context, d *schema.ResourceData, m int "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceIkeGatewayReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceIkeGatewayReadWJunSess(d, clt, junSess)...) } func resourceIkeGatewayRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceIkeGatewayReadWJnprSess(d, m, jnprSess) + return resourceIkeGatewayReadWJunSess(d, clt, junSess) } -func resourceIkeGatewayReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) diag.Diagnostics { +func resourceIkeGatewayReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession) diag.Diagnostics { mutex.Lock() - ikeGatewayOptions, err := readIkeGateway(d.Get("name").(string), m, jnprSess) + ikeGatewayOptions, err := readIkeGateway(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -366,76 +366,76 @@ func resourceIkeGatewayReadWJnprSess(d *schema.ResourceData, m interface{}, jnpr func resourceIkeGatewayUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delIkeGateway(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delIkeGateway(d, clt, nil); err != nil { return diag.FromErr(err) } - if err := setIkeGateway(d, m, nil); err != nil { + if err := setIkeGateway(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delIkeGateway(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delIkeGateway(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setIkeGateway(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setIkeGateway(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_ike_gateway", jnprSess) + warns, err := clt.commitConf("update resource junos_security_ike_gateway", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceIkeGatewayReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceIkeGatewayReadWJunSess(d, clt, junSess)...) } func resourceIkeGatewayDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delIkeGateway(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delIkeGateway(d, clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delIkeGateway(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delIkeGateway(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_ike_gateway", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_ike_gateway", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -445,21 +445,21 @@ func resourceIkeGatewayDelete(ctx context.Context, d *schema.ResourceData, m int func resourceIkeGatewayImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - ikeGatewayExists, err := checkIkeGatewayExists(d.Id(), m, jnprSess) + ikeGatewayExists, err := checkIkeGatewayExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !ikeGatewayExists { return nil, fmt.Errorf("don't find security ike gateway with id '%v' (id must be )", d.Id()) } - ikeGatewayOptions, err := readIkeGateway(d.Id(), m, jnprSess) + ikeGatewayOptions, err := readIkeGateway(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -469,9 +469,8 @@ func resourceIkeGatewayImport(ctx context.Context, d *schema.ResourceData, m int return result, nil } -func checkIkeGatewayExists(ikeGateway string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"security ike gateway "+ikeGateway+pipeDisplaySet, jnprSess) +func checkIkeGatewayExists(ikeGateway string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"security ike gateway "+ikeGateway+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -482,8 +481,7 @@ func checkIkeGatewayExists(ikeGateway string, m interface{}, jnprSess *NetconfOb return true, nil } -func setIkeGateway(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setIkeGateway(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set security ike gateway " + d.Get("name").(string) @@ -603,15 +601,14 @@ func setIkeGateway(d *schema.ResourceData, m interface{}, jnprSess *NetconfObjec configSet = append(configSet, setPrefix+" version "+d.Get("version").(string)) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readIkeGateway(ikeGateway string, m interface{}, jnprSess *NetconfObject) (ikeGatewayOptions, error) { - sess := m.(*Session) +func readIkeGateway(ikeGateway string, clt *Client, junSess *junosSession) (ikeGatewayOptions, error) { var confRead ikeGatewayOptions - showConfig, err := sess.command(cmdShowConfig+ - "security ike gateway "+ikeGateway+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security ike gateway "+ikeGateway+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -758,12 +755,11 @@ func readIkeGateway(ikeGateway string, m interface{}, jnprSess *NetconfObject) ( return confRead, nil } -func delIkeGateway(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delIkeGateway(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete security ike gateway "+d.Get("name").(string)) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillIkeGatewayData(d *schema.ResourceData, ikeGatewayOptions ikeGatewayOptions) { diff --git a/junos/resource_security_ike_policy.go b/junos/resource_security_ike_policy.go index 3f3e1a0f..77070e44 100644 --- a/junos/resource_security_ike_policy.go +++ b/junos/resource_security_ike_policy.go @@ -84,52 +84,52 @@ func resourceIkePolicy() *schema.Resource { } func resourceIkePolicyCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setIkePolicy(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setIkePolicy(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security ike policy not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel)) + junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - ikePolicyExists, err := checkIkePolicyExists(d.Get("name").(string), m, jnprSess) + ikePolicyExists, err := checkIkePolicyExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if ikePolicyExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security ike policy %v already exists", d.Get("name").(string)))...) } - if err := setIkePolicy(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setIkePolicy(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_ike_policy", jnprSess) + warns, err := clt.commitConf("create resource junos_security_ike_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - ikePolicyExists, err = checkIkePolicyExists(d.Get("name").(string), m, jnprSess) + ikePolicyExists, err = checkIkePolicyExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -140,23 +140,23 @@ func resourceIkePolicyCreate(ctx context.Context, d *schema.ResourceData, m inte "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceIkePolicyReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceIkePolicyReadWJunSess(d, clt, junSess)...) } func resourceIkePolicyRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceIkePolicyReadWJnprSess(d, m, jnprSess) + return resourceIkePolicyReadWJunSess(d, clt, junSess) } -func resourceIkePolicyReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) diag.Diagnostics { +func resourceIkePolicyReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession) diag.Diagnostics { mutex.Lock() - ikePolicyOptions, err := readIkePolicy(d.Get("name").(string), m, jnprSess) + ikePolicyOptions, err := readIkePolicy(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -172,77 +172,77 @@ func resourceIkePolicyReadWJnprSess(d *schema.ResourceData, m interface{}, jnprS func resourceIkePolicyUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delIkePolicy(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delIkePolicy(d, clt, nil); err != nil { return diag.FromErr(err) } - if err := setIkePolicy(d, m, nil); err != nil { + if err := setIkePolicy(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delIkePolicy(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delIkePolicy(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setIkePolicy(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setIkePolicy(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_ike_policy", jnprSess) + warns, err := clt.commitConf("update resource junos_security_ike_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceIkePolicyReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceIkePolicyReadWJunSess(d, clt, junSess)...) } func resourceIkePolicyDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delIkePolicy(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delIkePolicy(d, clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delIkePolicy(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delIkePolicy(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_ike_policy", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_ike_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -252,21 +252,21 @@ func resourceIkePolicyDelete(ctx context.Context, d *schema.ResourceData, m inte func resourceIkePolicyImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - ikePolicyExists, err := checkIkePolicyExists(d.Id(), m, jnprSess) + ikePolicyExists, err := checkIkePolicyExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !ikePolicyExists { return nil, fmt.Errorf("don't find security ike policy with id '%v' (id must be )", d.Id()) } - ikePolicyOptions, err := readIkePolicy(d.Id(), m, jnprSess) + ikePolicyOptions, err := readIkePolicy(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -276,9 +276,8 @@ func resourceIkePolicyImport(ctx context.Context, d *schema.ResourceData, m inte return result, nil } -func checkIkePolicyExists(ikePolicy string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"security ike policy "+ikePolicy+pipeDisplaySet, jnprSess) +func checkIkePolicyExists(ikePolicy string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"security ike policy "+ikePolicy+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -289,8 +288,7 @@ func checkIkePolicyExists(ikePolicy string, m interface{}, jnprSess *NetconfObje return true, nil } -func setIkePolicy(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setIkePolicy(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set security ike policy " + d.Get("name").(string) @@ -313,15 +311,14 @@ func setIkePolicy(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject configSet = append(configSet, setPrefix+" pre-shared-key hexadecimal "+d.Get("pre_shared_key_hexa").(string)) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readIkePolicy(ikePolicy string, m interface{}, jnprSess *NetconfObject) (ikePolicyOptions, error) { - sess := m.(*Session) +func readIkePolicy(ikePolicy string, clt *Client, junSess *junosSession) (ikePolicyOptions, error) { var confRead ikePolicyOptions - showConfig, err := sess.command(cmdShowConfig+ - "security ike policy "+ikePolicy+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security ike policy "+ikePolicy+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -361,12 +358,11 @@ func readIkePolicy(ikePolicy string, m interface{}, jnprSess *NetconfObject) (ik return confRead, nil } -func delIkePolicy(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delIkePolicy(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete security ike policy "+d.Get("name").(string)) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillIkePolicyData(d *schema.ResourceData, ikePolicyOptions ikePolicyOptions) { diff --git a/junos/resource_security_ike_proposal.go b/junos/resource_security_ike_proposal.go index a8e765c0..b9c09b07 100644 --- a/junos/resource_security_ike_proposal.go +++ b/junos/resource_security_ike_proposal.go @@ -63,53 +63,53 @@ func resourceIkeProposal() *schema.Resource { } func resourceIkeProposalCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setIkeProposal(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setIkeProposal(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security ike proposal not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel)) + junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - ikeProposalExists, err := checkIkeProposalExists(d.Get("name").(string), m, jnprSess) + ikeProposalExists, err := checkIkeProposalExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if ikeProposalExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security ike proposal %v already exists", d.Get("name").(string)))...) } - if err := setIkeProposal(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setIkeProposal(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_ike_proposal", jnprSess) + warns, err := clt.commitConf("create resource junos_security_ike_proposal", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - ikeProposalExists, err = checkIkeProposalExists(d.Get("name").(string), m, jnprSess) + ikeProposalExists, err = checkIkeProposalExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -120,23 +120,23 @@ func resourceIkeProposalCreate(ctx context.Context, d *schema.ResourceData, m in "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceIkeProposalReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceIkeProposalReadWJunSess(d, clt, junSess)...) } func resourceIkeProposalRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceIkeProposalReadWJnprSess(d, m, jnprSess) + return resourceIkeProposalReadWJunSess(d, clt, junSess) } -func resourceIkeProposalReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) diag.Diagnostics { +func resourceIkeProposalReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession) diag.Diagnostics { mutex.Lock() - ikeProposalOptions, err := readIkeProposal(d.Get("name").(string), m, jnprSess) + ikeProposalOptions, err := readIkeProposal(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -152,76 +152,76 @@ func resourceIkeProposalReadWJnprSess(d *schema.ResourceData, m interface{}, jnp func resourceIkeProposalUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delIkeProposal(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delIkeProposal(d, clt, nil); err != nil { return diag.FromErr(err) } - if err := setIkeProposal(d, m, nil); err != nil { + if err := setIkeProposal(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delIkeProposal(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delIkeProposal(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setIkeProposal(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setIkeProposal(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_ike_proposal", jnprSess) + warns, err := clt.commitConf("update resource junos_security_ike_proposal", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceIkeProposalReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceIkeProposalReadWJunSess(d, clt, junSess)...) } func resourceIkeProposalDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delIkeProposal(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delIkeProposal(d, clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delIkeProposal(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delIkeProposal(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_ike_proposal", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_ike_proposal", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -231,21 +231,21 @@ func resourceIkeProposalDelete(ctx context.Context, d *schema.ResourceData, m in func resourceIkeProposalImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - ikeProposalExists, err := checkIkeProposalExists(d.Id(), m, jnprSess) + ikeProposalExists, err := checkIkeProposalExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !ikeProposalExists { return nil, fmt.Errorf("don't find security ike proposal with id '%v' (id must be )", d.Id()) } - ikeProposalOptions, err := readIkeProposal(d.Id(), m, jnprSess) + ikeProposalOptions, err := readIkeProposal(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -255,10 +255,9 @@ func resourceIkeProposalImport(ctx context.Context, d *schema.ResourceData, m in return result, nil } -func checkIkeProposalExists(ikeProposal string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "security ike proposal "+ikeProposal+pipeDisplaySet, jnprSess) +func checkIkeProposalExists(ikeProposal string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "security ike proposal "+ikeProposal+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -269,8 +268,7 @@ func checkIkeProposalExists(ikeProposal string, m interface{}, jnprSess *Netconf return true, nil } -func setIkeProposal(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setIkeProposal(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set security ike proposal " + d.Get("name").(string) @@ -290,15 +288,14 @@ func setIkeProposal(d *schema.ResourceData, m interface{}, jnprSess *NetconfObje configSet = append(configSet, setPrefix+" lifetime-seconds "+strconv.Itoa(d.Get("lifetime_seconds").(int))) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readIkeProposal(ikeProposal string, m interface{}, jnprSess *NetconfObject) (ikeProposalOptions, error) { - sess := m.(*Session) +func readIkeProposal(ikeProposal string, clt *Client, junSess *junosSession) (ikeProposalOptions, error) { var confRead ikeProposalOptions - showConfig, err := sess.command(cmdShowConfig+ - "security ike proposal "+ikeProposal+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security ike proposal "+ikeProposal+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -333,12 +330,11 @@ func readIkeProposal(ikeProposal string, m interface{}, jnprSess *NetconfObject) return confRead, nil } -func delIkeProposal(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delIkeProposal(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete security ike proposal "+d.Get("name").(string)) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillIkeProposalData(d *schema.ResourceData, ikeProposalOptions ikeProposalOptions) { diff --git a/junos/resource_security_ipsec_policy.go b/junos/resource_security_ipsec_policy.go index 6960a891..ebe8e163 100644 --- a/junos/resource_security_ipsec_policy.go +++ b/junos/resource_security_ipsec_policy.go @@ -55,53 +55,53 @@ func resourceIpsecPolicy() *schema.Resource { } func resourceIpsecPolicyCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setIpsecPolicy(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setIpsecPolicy(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security ipsec policy not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel)) + junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - ipsecPolicyExists, err := checkIpsecPolicyExists(d.Get("name").(string), m, jnprSess) + ipsecPolicyExists, err := checkIpsecPolicyExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if ipsecPolicyExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security ipsec policy %v already exists", d.Get("name").(string)))...) } - if err := setIpsecPolicy(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setIpsecPolicy(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_ipsec_policy", jnprSess) + warns, err := clt.commitConf("create resource junos_security_ipsec_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - ipsecPolicyExists, err = checkIpsecPolicyExists(d.Get("name").(string), m, jnprSess) + ipsecPolicyExists, err = checkIpsecPolicyExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -112,23 +112,23 @@ func resourceIpsecPolicyCreate(ctx context.Context, d *schema.ResourceData, m in "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceIpsecPolicyReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceIpsecPolicyReadWJunSess(d, clt, junSess)...) } func resourceIpsecPolicyRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceIpsecPolicyReadWJnprSess(d, m, jnprSess) + return resourceIpsecPolicyReadWJunSess(d, clt, junSess) } -func resourceIpsecPolicyReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) diag.Diagnostics { +func resourceIpsecPolicyReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession) diag.Diagnostics { mutex.Lock() - ipsecPolicyOptions, err := readIpsecPolicy(d.Get("name").(string), m, jnprSess) + ipsecPolicyOptions, err := readIpsecPolicy(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -144,76 +144,76 @@ func resourceIpsecPolicyReadWJnprSess(d *schema.ResourceData, m interface{}, jnp func resourceIpsecPolicyUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delIpsecPolicy(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delIpsecPolicy(d, clt, nil); err != nil { return diag.FromErr(err) } - if err := setIpsecPolicy(d, m, nil); err != nil { + if err := setIpsecPolicy(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delIpsecPolicy(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delIpsecPolicy(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setIpsecPolicy(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setIpsecPolicy(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_ipsec_policy", jnprSess) + warns, err := clt.commitConf("update resource junos_security_ipsec_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceIpsecPolicyReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceIpsecPolicyReadWJunSess(d, clt, junSess)...) } func resourceIpsecPolicyDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delIpsecPolicy(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delIpsecPolicy(d, clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delIpsecPolicy(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delIpsecPolicy(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_ipsec_policy", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_ipsec_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -223,21 +223,21 @@ func resourceIpsecPolicyDelete(ctx context.Context, d *schema.ResourceData, m in func resourceIpsecPolicyImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - ipsecPolicyExists, err := checkIpsecPolicyExists(d.Id(), m, jnprSess) + ipsecPolicyExists, err := checkIpsecPolicyExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !ipsecPolicyExists { return nil, fmt.Errorf("don't find security ipsec policy with id '%v' (id must be )", d.Id()) } - ipsecPolicyOptions, err := readIpsecPolicy(d.Id(), m, jnprSess) + ipsecPolicyOptions, err := readIpsecPolicy(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -247,10 +247,9 @@ func resourceIpsecPolicyImport(ctx context.Context, d *schema.ResourceData, m in return result, nil } -func checkIpsecPolicyExists(ipsecPolicy string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "security ipsec policy "+ipsecPolicy+pipeDisplaySet, jnprSess) +func checkIpsecPolicyExists(ipsecPolicy string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "security ipsec policy "+ipsecPolicy+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -261,8 +260,7 @@ func checkIpsecPolicyExists(ipsecPolicy string, m interface{}, jnprSess *Netconf return true, nil } -func setIpsecPolicy(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setIpsecPolicy(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set security ipsec policy " + d.Get("name").(string) @@ -276,15 +274,14 @@ func setIpsecPolicy(d *schema.ResourceData, m interface{}, jnprSess *NetconfObje configSet = append(configSet, setPrefix+" proposal-set "+d.Get("proposal_set").(string)) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readIpsecPolicy(ipsecPolicy string, m interface{}, jnprSess *NetconfObject) (ipsecPolicyOptions, error) { - sess := m.(*Session) +func readIpsecPolicy(ipsecPolicy string, clt *Client, junSess *junosSession) (ipsecPolicyOptions, error) { var confRead ipsecPolicyOptions - showConfig, err := sess.command(cmdShowConfig+ - "security ipsec policy "+ipsecPolicy+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security ipsec policy "+ipsecPolicy+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -312,12 +309,11 @@ func readIpsecPolicy(ipsecPolicy string, m interface{}, jnprSess *NetconfObject) return confRead, nil } -func delIpsecPolicy(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delIpsecPolicy(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete security ipsec policy "+d.Get("name").(string)) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillIpsecPolicyData(d *schema.ResourceData, ipsecPolicyOptions ipsecPolicyOptions) { diff --git a/junos/resource_security_ipsec_proposal.go b/junos/resource_security_ipsec_proposal.go index 5712e5d0..6c4132d8 100644 --- a/junos/resource_security_ipsec_proposal.go +++ b/junos/resource_security_ipsec_proposal.go @@ -64,53 +64,53 @@ func resourceIpsecProposal() *schema.Resource { } func resourceIpsecProposalCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setIpsecProposal(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setIpsecProposal(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security ipsec proposal not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel)) + junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - ipsecProposalExists, err := checkIpsecProposalExists(d.Get("name").(string), m, jnprSess) + ipsecProposalExists, err := checkIpsecProposalExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if ipsecProposalExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security ipsec proposal %v already exists", d.Get("name").(string)))...) } - if err := setIpsecProposal(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setIpsecProposal(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_ipsec_proposal", jnprSess) + warns, err := clt.commitConf("create resource junos_security_ipsec_proposal", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - ipsecProposalExists, err = checkIpsecProposalExists(d.Get("name").(string), m, jnprSess) + ipsecProposalExists, err = checkIpsecProposalExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -121,24 +121,24 @@ func resourceIpsecProposalCreate(ctx context.Context, d *schema.ResourceData, m "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceIpsecProposalReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceIpsecProposalReadWJunSess(d, clt, junSess)...) } func resourceIpsecProposalRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceIpsecProposalReadWJnprSess(d, m, jnprSess) + return resourceIpsecProposalReadWJunSess(d, clt, junSess) } -func resourceIpsecProposalReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceIpsecProposalReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - ipsecProposalOptions, err := readIpsecProposal(d.Get("name").(string), m, jnprSess) + ipsecProposalOptions, err := readIpsecProposal(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -154,76 +154,76 @@ func resourceIpsecProposalReadWJnprSess(d *schema.ResourceData, m interface{}, j func resourceIpsecProposalUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delIpsecProposal(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delIpsecProposal(d, clt, nil); err != nil { return diag.FromErr(err) } - if err := setIpsecProposal(d, m, nil); err != nil { + if err := setIpsecProposal(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delIpsecProposal(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delIpsecProposal(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setIpsecProposal(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setIpsecProposal(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_ipsec_proposal", jnprSess) + warns, err := clt.commitConf("update resource junos_security_ipsec_proposal", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceIpsecProposalReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceIpsecProposalReadWJunSess(d, clt, junSess)...) } func resourceIpsecProposalDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delIpsecProposal(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delIpsecProposal(d, clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delIpsecProposal(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delIpsecProposal(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_ipsec_proposal", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_ipsec_proposal", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -233,21 +233,21 @@ func resourceIpsecProposalDelete(ctx context.Context, d *schema.ResourceData, m func resourceIpsecProposalImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - ipsecProposalExists, err := checkIpsecProposalExists(d.Id(), m, jnprSess) + ipsecProposalExists, err := checkIpsecProposalExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !ipsecProposalExists { return nil, fmt.Errorf("don't find security ipsec proposal with id '%v' (id must be )", d.Id()) } - ipsecProposalOptions, err := readIpsecProposal(d.Id(), m, jnprSess) + ipsecProposalOptions, err := readIpsecProposal(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -257,10 +257,9 @@ func resourceIpsecProposalImport(ctx context.Context, d *schema.ResourceData, m return result, nil } -func checkIpsecProposalExists(ipsecProposal string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "security ipsec proposal "+ipsecProposal+pipeDisplaySet, jnprSess) +func checkIpsecProposalExists(ipsecProposal string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "security ipsec proposal "+ipsecProposal+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -271,8 +270,7 @@ func checkIpsecProposalExists(ipsecProposal string, m interface{}, jnprSess *Net return true, nil } -func setIpsecProposal(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setIpsecProposal(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set security ipsec proposal " + d.Get("name").(string) @@ -292,15 +290,14 @@ func setIpsecProposal(d *schema.ResourceData, m interface{}, jnprSess *NetconfOb configSet = append(configSet, setPrefix+" protocol "+d.Get("protocol").(string)) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readIpsecProposal(ipsecProposal string, m interface{}, jnprSess *NetconfObject) (ipsecProposalOptions, error) { - sess := m.(*Session) +func readIpsecProposal(ipsecProposal string, clt *Client, junSess *junosSession) (ipsecProposalOptions, error) { var confRead ipsecProposalOptions - showConfig, err := sess.command(cmdShowConfig+ - "security ipsec proposal "+ipsecProposal+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security ipsec proposal "+ipsecProposal+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -338,12 +335,11 @@ func readIpsecProposal(ipsecProposal string, m interface{}, jnprSess *NetconfObj return confRead, nil } -func delIpsecProposal(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delIpsecProposal(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete security ipsec proposal "+d.Get("name").(string)) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillIpsecProposalData(d *schema.ResourceData, ipsecProposalOptions ipsecProposalOptions) { diff --git a/junos/resource_security_ipsec_vpn.go b/junos/resource_security_ipsec_vpn.go index c808ff03..fbc81c15 100644 --- a/junos/resource_security_ipsec_vpn.go +++ b/junos/resource_security_ipsec_vpn.go @@ -148,43 +148,43 @@ func resourceIpsecVpn() *schema.Resource { } func resourceIpsecVpnCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" && !d.Get("bind_interface_auto").(bool) { - if err := setIpsecVpn(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" && !d.Get("bind_interface_auto").(bool) { + if err := setIpsecVpn(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security ipsec vpn not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel)) + junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - ipsecVpnExists, err := checkIpsecVpnExists(d.Get("name").(string), m, jnprSess) + ipsecVpnExists, err := checkIpsecVpnExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if ipsecVpnExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security ipsec vpn %v already exists", d.Get("name").(string)))...) } if d.Get("bind_interface_auto").(bool) { - newSt0, err := searchInterfaceSt0UnitToCreate(m, jnprSess) + newSt0, err := searchInterfaceSt0UnitToCreate(clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("error to find new bind interface: %w", err))...) } @@ -193,19 +193,19 @@ func resourceIpsecVpnCreate(ctx context.Context, d *schema.ResourceData, m inter panic(tfErr) } } - if err := setIpsecVpn(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setIpsecVpn(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_ipsec_vpn", jnprSess) + warns, err := clt.commitConf("create resource junos_security_ipsec_vpn", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - ipsecVpnExists, err = checkIpsecVpnExists(d.Get("name").(string), m, jnprSess) + ipsecVpnExists, err = checkIpsecVpnExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -216,23 +216,23 @@ func resourceIpsecVpnCreate(ctx context.Context, d *schema.ResourceData, m inter "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceIpsecVpnReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceIpsecVpnReadWJunSess(d, clt, junSess)...) } func resourceIpsecVpnRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceIpsecVpnReadWJnprSess(d, m, jnprSess) + return resourceIpsecVpnReadWJunSess(d, clt, junSess) } -func resourceIpsecVpnReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) diag.Diagnostics { +func resourceIpsecVpnReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession) diag.Diagnostics { mutex.Lock() - ipsecVpnOptions, err := readIpsecVpn(d.Get("name").(string), m, jnprSess) + ipsecVpnOptions, err := readIpsecVpn(d.Get("name").(string), clt, junSess) mutex.Unlock() // copy state vpn_monitor.0.source_interface_auto to struct if len(ipsecVpnOptions.vpnMonitor) > 0 { @@ -265,97 +265,97 @@ func resourceIpsecVpnUpdate(ctx context.Context, d *schema.ResourceData, m inter AttributePath: cty.Path{cty.GetAttrStep{Name: "bind_interface_auto"}}, }) } - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delIpsecVpnConf(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delIpsecVpnConf(d, clt, nil); err != nil { return append(diagWarns, diag.FromErr(err)...) } if d.HasChanges("bind_interface") && d.Get("bind_interface_auto").(bool) { oldInt, _ := d.GetChange("bind_interface") - if err := sess.configSet([]string{"delete interfaces " + oldInt.(string)}, nil); err != nil { + if err := clt.configSet([]string{"delete interfaces " + oldInt.(string)}, nil); err != nil { return append(diagWarns, diag.FromErr(err)...) } } - if err := setIpsecVpn(d, m, nil); err != nil { + if err := setIpsecVpn(d, clt, nil); err != nil { return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) return diagWarns } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } - if err := delIpsecVpnConf(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delIpsecVpnConf(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if d.HasChanges("bind_interface") && d.Get("bind_interface_auto").(bool) { oldInt, _ := d.GetChange("bind_interface") - st0NC, st0Emtpy, _, err := checkInterfaceLogicalNCEmpty(oldInt.(string), m, jnprSess) + st0NC, st0Emtpy, _, err := checkInterfaceLogicalNCEmpty(oldInt.(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if st0NC || st0Emtpy { - if err := sess.configSet([]string{"delete interfaces " + oldInt.(string)}, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := clt.configSet([]string{"delete interfaces " + oldInt.(string)}, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } } } - if err := setIpsecVpn(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setIpsecVpn(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_ipsec_vpn", jnprSess) + warns, err := clt.commitConf("update resource junos_security_ipsec_vpn", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceIpsecVpnReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceIpsecVpnReadWJunSess(d, clt, junSess)...) } func resourceIpsecVpnDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delIpsecVpn(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delIpsecVpn(d, clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delIpsecVpn(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delIpsecVpn(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_ipsec_vpn", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_ipsec_vpn", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -365,21 +365,21 @@ func resourceIpsecVpnDelete(ctx context.Context, d *schema.ResourceData, m inter func resourceIpsecVpnImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - ipsecVpnExists, err := checkIpsecVpnExists(d.Id(), m, jnprSess) + ipsecVpnExists, err := checkIpsecVpnExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !ipsecVpnExists { return nil, fmt.Errorf("don't find security ipsec vpn with id '%v' (id must be )", d.Id()) } - ipsecVpnOptions, err := readIpsecVpn(d.Id(), m, jnprSess) + ipsecVpnOptions, err := readIpsecVpn(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -389,9 +389,8 @@ func resourceIpsecVpnImport(ctx context.Context, d *schema.ResourceData, m inter return result, nil } -func checkIpsecVpnExists(ipsecVpn string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"security ipsec vpn "+ipsecVpn+pipeDisplaySet, jnprSess) +func checkIpsecVpnExists(ipsecVpn string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"security ipsec vpn "+ipsecVpn+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -402,8 +401,7 @@ func checkIpsecVpnExists(ipsecVpn string, m interface{}, jnprSess *NetconfObject return true, nil } -func setIpsecVpn(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setIpsecVpn(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) if d.Get("bind_interface").(string) != "" { @@ -465,15 +463,14 @@ func setIpsecVpn(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readIpsecVpn(ipsecVpn string, m interface{}, jnprSess *NetconfObject) (ipsecVpnOptions, error) { - sess := m.(*Session) +func readIpsecVpn(ipsecVpn string, clt *Client, junSess *junosSession) (ipsecVpnOptions, error) { var confRead ipsecVpnOptions - showConfig, err := sess.command(cmdShowConfig+ - "security ipsec vpn "+ipsecVpn+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security ipsec vpn "+ipsecVpn+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -557,23 +554,21 @@ func readIpsecVpn(ipsecVpn string, m interface{}, jnprSess *NetconfObject) (ipse return confRead, nil } -func delIpsecVpnConf(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delIpsecVpnConf(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete security ipsec vpn "+d.Get("name").(string)) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func delIpsecVpn(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delIpsecVpn(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete security ipsec vpn "+d.Get("name").(string)) if d.Get("bind_interface_auto").(bool) { - if jnprSess == nil { + if junSess == nil { configSet = append(configSet, "delete interfaces "+d.Get("bind_interface").(string)) } else { - st0NC, st0Emtpy, _, err := checkInterfaceLogicalNCEmpty(d.Get("bind_interface").(string), m, jnprSess) + st0NC, st0Emtpy, _, err := checkInterfaceLogicalNCEmpty(d.Get("bind_interface").(string), clt, junSess) if err != nil { return err } @@ -583,7 +578,7 @@ func delIpsecVpn(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillIpsecVpnData(d *schema.ResourceData, ipsecVpnOptions ipsecVpnOptions) { diff --git a/junos/resource_security_log_stream.go b/junos/resource_security_log_stream.go index 2b634194..a5886fb3 100644 --- a/junos/resource_security_log_stream.go +++ b/junos/resource_security_log_stream.go @@ -123,53 +123,53 @@ func resourceSecurityLogStream() *schema.Resource { } func resourceSecurityLogStreamCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSecurityLogStream(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSecurityLogStream(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security log stream "+ - "not compatible with Junos device %s", jnprSess.SystemInformation.HardwareModel)) + "not compatible with Junos device %s", junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - securityLogStreamExists, err := checkSecurityLogStreamExists(d.Get("name").(string), m, jnprSess) + securityLogStreamExists, err := checkSecurityLogStreamExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if securityLogStreamExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security log stream %v already exists", d.Get("name").(string)))...) } - if err := setSecurityLogStream(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityLogStream(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_log_stream", jnprSess) + warns, err := clt.commitConf("create resource junos_security_log_stream", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - securityLogStreamExists, err = checkSecurityLogStreamExists(d.Get("name").(string), m, jnprSess) + securityLogStreamExists, err = checkSecurityLogStreamExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -180,24 +180,24 @@ func resourceSecurityLogStreamCreate(ctx context.Context, d *schema.ResourceData "not exists after commit => check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceSecurityLogStreamReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityLogStreamReadWJunSess(d, clt, junSess)...) } func resourceSecurityLogStreamRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSecurityLogStreamReadWJnprSess(d, m, jnprSess) + return resourceSecurityLogStreamReadWJunSess(d, clt, junSess) } -func resourceSecurityLogStreamReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSecurityLogStreamReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - securityLogStreamOptions, err := readSecurityLogStream(d.Get("name").(string), m, jnprSess) + securityLogStreamOptions, err := readSecurityLogStream(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -213,76 +213,76 @@ func resourceSecurityLogStreamReadWJnprSess(d *schema.ResourceData, m interface{ func resourceSecurityLogStreamUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delLogStream(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delLogStream(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSecurityLogStream(d, m, nil); err != nil { + if err := setSecurityLogStream(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delLogStream(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delLogStream(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSecurityLogStream(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityLogStream(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_log_stream", jnprSess) + warns, err := clt.commitConf("update resource junos_security_log_stream", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSecurityLogStreamReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityLogStreamReadWJunSess(d, clt, junSess)...) } func resourceSecurityLogStreamDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delLogStream(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delLogStream(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delLogStream(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delLogStream(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_log_stream", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_log_stream", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -292,21 +292,21 @@ func resourceSecurityLogStreamDelete(ctx context.Context, d *schema.ResourceData func resourceSecurityLogStreamImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - securityLogStreamExists, err := checkSecurityLogStreamExists(d.Id(), m, jnprSess) + securityLogStreamExists, err := checkSecurityLogStreamExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !securityLogStreamExists { return nil, fmt.Errorf("don't find security log stream with id '%v' (id must be )", d.Id()) } - securityLogStreamOptions, err := readSecurityLogStream(d.Id(), m, jnprSess) + securityLogStreamOptions, err := readSecurityLogStream(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -317,10 +317,9 @@ func resourceSecurityLogStreamImport(ctx context.Context, d *schema.ResourceData return result, nil } -func checkSecurityLogStreamExists(securityLogStream string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "security log stream \""+securityLogStream+"\""+pipeDisplaySet, jnprSess) +func checkSecurityLogStreamExists(securityLogStream string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "security log stream \""+securityLogStream+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -331,8 +330,7 @@ func checkSecurityLogStreamExists(securityLogStream string, m interface{}, jnprS return true, nil } -func setSecurityLogStream(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSecurityLogStream(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set security log stream \"" + d.Get("name").(string) + "\" " @@ -379,16 +377,15 @@ func setSecurityLogStream(d *schema.ResourceData, m interface{}, jnprSess *Netco d.Get("severity").(string)) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSecurityLogStream(securityLogStream string, m interface{}, jnprSess *NetconfObject, +func readSecurityLogStream(securityLogStream string, clt *Client, junSess *junosSession, ) (securityLogStreamOptions, error) { - sess := m.(*Session) var confRead securityLogStreamOptions - showConfig, err := sess.command(cmdShowConfig+ - "security log stream \""+securityLogStream+"\""+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security log stream \""+securityLogStream+"\""+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -471,12 +468,11 @@ func readSecurityLogStream(securityLogStream string, m interface{}, jnprSess *Ne return confRead, nil } -func delLogStream(securityLogStream string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delLogStream(securityLogStream string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete security log stream \""+securityLogStream+"\"") - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSecurityLogStreamData(d *schema.ResourceData, securityLogStreamOptions securityLogStreamOptions) { diff --git a/junos/resource_security_nat_destination.go b/junos/resource_security_nat_destination.go index bb82f7ad..f06bce4e 100644 --- a/junos/resource_security_nat_destination.go +++ b/junos/resource_security_nat_destination.go @@ -133,54 +133,54 @@ func resourceSecurityNatDestination() *schema.Resource { } func resourceSecurityNatDestinationCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSecurityNatDestination(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSecurityNatDestination(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security nat destination not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel)) + junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - securityNatDestinationExists, err := checkSecurityNatDestinationExists(d.Get("name").(string), m, jnprSess) + securityNatDestinationExists, err := checkSecurityNatDestinationExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if securityNatDestinationExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security nat destination %v already exists", d.Get("name").(string)))...) } - if err := setSecurityNatDestination(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityNatDestination(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_nat_destination", jnprSess) + warns, err := clt.commitConf("create resource junos_security_nat_destination", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - securityNatDestinationExists, err = checkSecurityNatDestinationExists(d.Get("name").(string), m, jnprSess) + securityNatDestinationExists, err = checkSecurityNatDestinationExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -191,24 +191,24 @@ func resourceSecurityNatDestinationCreate(ctx context.Context, d *schema.Resourc "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceSecurityNatDestinationReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityNatDestinationReadWJunSess(d, clt, junSess)...) } func resourceSecurityNatDestinationRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSecurityNatDestinationReadWJnprSess(d, m, jnprSess) + return resourceSecurityNatDestinationReadWJunSess(d, clt, junSess) } -func resourceSecurityNatDestinationReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSecurityNatDestinationReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - natDestinationOptions, err := readSecurityNatDestination(d.Get("name").(string), m, jnprSess) + natDestinationOptions, err := readSecurityNatDestination(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -224,76 +224,76 @@ func resourceSecurityNatDestinationReadWJnprSess(d *schema.ResourceData, m inter func resourceSecurityNatDestinationUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSecurityNatDestination(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSecurityNatDestination(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSecurityNatDestination(d, m, nil); err != nil { + if err := setSecurityNatDestination(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityNatDestination(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityNatDestination(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSecurityNatDestination(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityNatDestination(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_nat_destination", jnprSess) + warns, err := clt.commitConf("update resource junos_security_nat_destination", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSecurityNatDestinationReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityNatDestinationReadWJunSess(d, clt, junSess)...) } func resourceSecurityNatDestinationDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSecurityNatDestination(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSecurityNatDestination(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityNatDestination(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityNatDestination(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_nat_destination", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_nat_destination", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -303,22 +303,22 @@ func resourceSecurityNatDestinationDelete(ctx context.Context, d *schema.Resourc func resourceSecurityNatDestinationImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - securityNatDestinationExists, err := checkSecurityNatDestinationExists(d.Id(), m, jnprSess) + securityNatDestinationExists, err := checkSecurityNatDestinationExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !securityNatDestinationExists { return nil, fmt.Errorf("don't find nat destination with id '%v' (id must be )", d.Id()) } - natDestinationOptions, err := readSecurityNatDestination(d.Id(), m, jnprSess) + natDestinationOptions, err := readSecurityNatDestination(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -329,10 +329,9 @@ func resourceSecurityNatDestinationImport(ctx context.Context, d *schema.Resourc return result, nil } -func checkSecurityNatDestinationExists(name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "security nat destination rule-set "+name+pipeDisplaySet, jnprSess) +func checkSecurityNatDestinationExists(name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "security nat destination rule-set "+name+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -343,8 +342,7 @@ func checkSecurityNatDestinationExists(name string, m interface{}, jnprSess *Net return true, nil } -func setSecurityNatDestination(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSecurityNatDestination(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) regexpDestPort := regexp.MustCompile(`^\d+( to \d+)?$`) @@ -412,15 +410,14 @@ func setSecurityNatDestination(d *schema.ResourceData, m interface{}, jnprSess * configSet = append(configSet, setPrefix+" description \""+v+"\"") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSecurityNatDestination(name string, m interface{}, jnprSess *NetconfObject) (natDestinationOptions, error) { - sess := m.(*Session) +func readSecurityNatDestination(name string, clt *Client, junSess *junosSession) (natDestinationOptions, error) { var confRead natDestinationOptions - showConfig, err := sess.command(cmdShowConfig+ - "security nat destination rule-set "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security nat destination rule-set "+name+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -507,12 +504,11 @@ func readSecurityNatDestination(name string, m interface{}, jnprSess *NetconfObj return confRead, nil } -func delSecurityNatDestination(natDestination string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSecurityNatDestination(natDestination string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete security nat destination rule-set "+natDestination) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSecurityNatDestinationData(d *schema.ResourceData, natDestinationOptions natDestinationOptions) { diff --git a/junos/resource_security_nat_destination_pool.go b/junos/resource_security_nat_destination_pool.go index f583d2ec..88a79360 100644 --- a/junos/resource_security_nat_destination_pool.go +++ b/junos/resource_security_nat_destination_pool.go @@ -68,54 +68,54 @@ func resourceSecurityNatDestinationPool() *schema.Resource { func resourceSecurityNatDestinationPoolCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSecurityNatDestinationPool(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSecurityNatDestinationPool(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security nat destination pool not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel)) + junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - securityNatDestinationPoolExists, err := checkSecurityNatDestinationPoolExists(d.Get("name").(string), m, jnprSess) + securityNatDestinationPoolExists, err := checkSecurityNatDestinationPoolExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if securityNatDestinationPoolExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security nat destination pool %v already exists", d.Get("name").(string)))...) } - if err := setSecurityNatDestinationPool(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityNatDestinationPool(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_nat_destination_pool", jnprSess) + warns, err := clt.commitConf("create resource junos_security_nat_destination_pool", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - securityNatDestinationPoolExists, err = checkSecurityNatDestinationPoolExists(d.Get("name").(string), m, jnprSess) + securityNatDestinationPoolExists, err = checkSecurityNatDestinationPoolExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -126,25 +126,25 @@ func resourceSecurityNatDestinationPoolCreate(ctx context.Context, d *schema.Res "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceSecurityNatDestinationPoolReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityNatDestinationPoolReadWJunSess(d, clt, junSess)...) } func resourceSecurityNatDestinationPoolRead(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSecurityNatDestinationPoolReadWJnprSess(d, m, jnprSess) + return resourceSecurityNatDestinationPoolReadWJunSess(d, clt, junSess) } -func resourceSecurityNatDestinationPoolReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSecurityNatDestinationPoolReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - natDestinationPoolOptions, err := readSecurityNatDestinationPool(d.Get("name").(string), m, jnprSess) + natDestinationPoolOptions, err := readSecurityNatDestinationPool(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -161,77 +161,77 @@ func resourceSecurityNatDestinationPoolReadWJnprSess(d *schema.ResourceData, m i func resourceSecurityNatDestinationPoolUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSecurityNatDestinationPool(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSecurityNatDestinationPool(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSecurityNatDestinationPool(d, m, nil); err != nil { + if err := setSecurityNatDestinationPool(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityNatDestinationPool(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityNatDestinationPool(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSecurityNatDestinationPool(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityNatDestinationPool(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_nat_destination_pool", jnprSess) + warns, err := clt.commitConf("update resource junos_security_nat_destination_pool", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSecurityNatDestinationPoolReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityNatDestinationPoolReadWJunSess(d, clt, junSess)...) } func resourceSecurityNatDestinationPoolDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSecurityNatDestinationPool(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSecurityNatDestinationPool(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityNatDestinationPool(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityNatDestinationPool(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_nat_destination_pool", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_nat_destination_pool", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -241,22 +241,22 @@ func resourceSecurityNatDestinationPoolDelete(ctx context.Context, d *schema.Res func resourceSecurityNatDestinationPoolImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - securityNatDestinationPoolExists, err := checkSecurityNatDestinationPoolExists(d.Id(), m, jnprSess) + securityNatDestinationPoolExists, err := checkSecurityNatDestinationPoolExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !securityNatDestinationPoolExists { return nil, fmt.Errorf("don't find nat destination pool with id '%v' (id must be )", d.Id()) } - natDestinationPoolOptions, err := readSecurityNatDestinationPool(d.Id(), m, jnprSess) + natDestinationPoolOptions, err := readSecurityNatDestinationPool(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -267,10 +267,9 @@ func resourceSecurityNatDestinationPoolImport(ctx context.Context, d *schema.Res return result, nil } -func checkSecurityNatDestinationPoolExists(name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "security nat destination pool "+name+pipeDisplaySet, jnprSess) +func checkSecurityNatDestinationPoolExists(name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "security nat destination pool "+name+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -281,8 +280,7 @@ func checkSecurityNatDestinationPoolExists(name string, m interface{}, jnprSess return true, nil } -func setSecurityNatDestinationPool(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSecurityNatDestinationPool(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set security nat destination pool " + d.Get("name").(string) @@ -300,16 +298,15 @@ func setSecurityNatDestinationPool(d *schema.ResourceData, m interface{}, jnprSe configSet = append(configSet, setPrefix+" routing-instance "+d.Get("routing_instance").(string)) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSecurityNatDestinationPool(name string, m interface{}, jnprSess *NetconfObject, +func readSecurityNatDestinationPool(name string, clt *Client, junSess *junosSession, ) (natDestinationPoolOptions, error) { - sess := m.(*Session) var confRead natDestinationPoolOptions - showConfig, err := sess.command(cmdShowConfig+ - "security nat destination pool "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security nat destination pool "+name+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -344,12 +341,11 @@ func readSecurityNatDestinationPool(name string, m interface{}, jnprSess *Netcon return confRead, nil } -func delSecurityNatDestinationPool(natDestinationPool string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSecurityNatDestinationPool(natDestinationPool string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete security nat destination pool "+natDestinationPool) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSecurityNatDestinationPoolData(d *schema.ResourceData, natDestinationPoolOptions natDestinationPoolOptions) { diff --git a/junos/resource_security_nat_source.go b/junos/resource_security_nat_source.go index 41ae3729..3dd684b4 100644 --- a/junos/resource_security_nat_source.go +++ b/junos/resource_security_nat_source.go @@ -172,53 +172,53 @@ func resourceSecurityNatSource() *schema.Resource { } func resourceSecurityNatSourceCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSecurityNatSource(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSecurityNatSource(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security nat source not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel)) + junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - securityNatSourceExists, err := checkSecurityNatSourceExists(d.Get("name").(string), m, jnprSess) + securityNatSourceExists, err := checkSecurityNatSourceExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if securityNatSourceExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security nat source %v already exists", d.Get("name").(string)))...) } - if err := setSecurityNatSource(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityNatSource(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_nat_source", jnprSess) + warns, err := clt.commitConf("create resource junos_security_nat_source", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - securityNatSourceExists, err = checkSecurityNatSourceExists(d.Get("name").(string), m, jnprSess) + securityNatSourceExists, err = checkSecurityNatSourceExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -229,24 +229,24 @@ func resourceSecurityNatSourceCreate(ctx context.Context, d *schema.ResourceData "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceSecurityNatSourceReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityNatSourceReadWJunSess(d, clt, junSess)...) } func resourceSecurityNatSourceRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSecurityNatSourceReadWJnprSess(d, m, jnprSess) + return resourceSecurityNatSourceReadWJunSess(d, clt, junSess) } -func resourceSecurityNatSourceReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSecurityNatSourceReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - natSourceOptions, err := readSecurityNatSource(d.Get("name").(string), m, jnprSess) + natSourceOptions, err := readSecurityNatSource(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -262,76 +262,76 @@ func resourceSecurityNatSourceReadWJnprSess(d *schema.ResourceData, m interface{ func resourceSecurityNatSourceUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSecurityNatSource(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSecurityNatSource(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSecurityNatSource(d, m, nil); err != nil { + if err := setSecurityNatSource(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityNatSource(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityNatSource(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSecurityNatSource(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityNatSource(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_nat_source", jnprSess) + warns, err := clt.commitConf("update resource junos_security_nat_source", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSecurityNatSourceReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityNatSourceReadWJunSess(d, clt, junSess)...) } func resourceSecurityNatSourceDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSecurityNatSource(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSecurityNatSource(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityNatSource(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityNatSource(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_nat_source", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_nat_source", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -341,22 +341,22 @@ func resourceSecurityNatSourceDelete(ctx context.Context, d *schema.ResourceData func resourceSecurityNatSourceImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - securityNatSourceExists, err := checkSecurityNatSourceExists(d.Id(), m, jnprSess) + securityNatSourceExists, err := checkSecurityNatSourceExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !securityNatSourceExists { return nil, fmt.Errorf("don't find nat source with id '%v' (id must be )", d.Id()) } - natSourceOptions, err := readSecurityNatSource(d.Id(), m, jnprSess) + natSourceOptions, err := readSecurityNatSource(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -367,10 +367,9 @@ func resourceSecurityNatSourceImport(ctx context.Context, d *schema.ResourceData return result, nil } -func checkSecurityNatSourceExists(name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "security nat source rule-set "+name+pipeDisplaySet, jnprSess) +func checkSecurityNatSourceExists(name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "security nat source rule-set "+name+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -381,8 +380,7 @@ func checkSecurityNatSourceExists(name string, m interface{}, jnprSess *NetconfO return true, nil } -func setSecurityNatSource(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSecurityNatSource(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) regexpPort := regexp.MustCompile(`^\d+( to \d+)?$`) @@ -471,15 +469,14 @@ func setSecurityNatSource(d *schema.ResourceData, m interface{}, jnprSess *Netco configSet = append(configSet, setPrefix+" description \""+v+"\"") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSecurityNatSource(name string, m interface{}, jnprSess *NetconfObject) (natSourceOptions, error) { - sess := m.(*Session) +func readSecurityNatSource(name string, clt *Client, junSess *junosSession) (natSourceOptions, error) { var confRead natSourceOptions - showConfig, err := sess.command(cmdShowConfig+ - "security nat source rule-set "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security nat source rule-set "+name+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -591,12 +588,11 @@ func readSecurityNatSource(name string, m interface{}, jnprSess *NetconfObject) return confRead, nil } -func delSecurityNatSource(natSource string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSecurityNatSource(natSource string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete security nat source rule-set "+natSource) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSecurityNatSourceData(d *schema.ResourceData, natSourceOptions natSourceOptions) { diff --git a/junos/resource_security_nat_source_pool.go b/junos/resource_security_nat_source_pool.go index b20eb375..92a26320 100644 --- a/junos/resource_security_nat_source_pool.go +++ b/junos/resource_security_nat_source_pool.go @@ -98,54 +98,54 @@ func resourceSecurityNatSourcePool() *schema.Resource { } func resourceSecurityNatSourcePoolCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSecurityNatSourcePool(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSecurityNatSourcePool(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security nat source pool not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel)) + junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - securityNatSourcePoolExists, err := checkSecurityNatSourcePoolExists(d.Get("name").(string), m, jnprSess) + securityNatSourcePoolExists, err := checkSecurityNatSourcePoolExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if securityNatSourcePoolExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security nat source pool %v already exists", d.Get("name").(string)))...) } - if err := setSecurityNatSourcePool(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityNatSourcePool(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_nat_source_pool", jnprSess) + warns, err := clt.commitConf("create resource junos_security_nat_source_pool", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - securityNatSourcePoolExists, err = checkSecurityNatSourcePoolExists(d.Get("name").(string), m, jnprSess) + securityNatSourcePoolExists, err = checkSecurityNatSourcePoolExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -156,24 +156,24 @@ func resourceSecurityNatSourcePoolCreate(ctx context.Context, d *schema.Resource "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceSecurityNatSourcePoolReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityNatSourcePoolReadWJunSess(d, clt, junSess)...) } func resourceSecurityNatSourcePoolRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSecurityNatSourcePoolReadWJnprSess(d, m, jnprSess) + return resourceSecurityNatSourcePoolReadWJunSess(d, clt, junSess) } -func resourceSecurityNatSourcePoolReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSecurityNatSourcePoolReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - natSourcePoolOptions, err := readSecurityNatSourcePool(d.Get("name").(string), m, jnprSess) + natSourcePoolOptions, err := readSecurityNatSourcePool(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -189,76 +189,76 @@ func resourceSecurityNatSourcePoolReadWJnprSess(d *schema.ResourceData, m interf func resourceSecurityNatSourcePoolUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSecurityNatSourcePool(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSecurityNatSourcePool(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSecurityNatSourcePool(d, m, nil); err != nil { + if err := setSecurityNatSourcePool(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityNatSourcePool(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityNatSourcePool(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSecurityNatSourcePool(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityNatSourcePool(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_nat_source_pool", jnprSess) + warns, err := clt.commitConf("update resource junos_security_nat_source_pool", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSecurityNatSourcePoolReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityNatSourcePoolReadWJunSess(d, clt, junSess)...) } func resourceSecurityNatSourcePoolDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSecurityNatSourcePool(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSecurityNatSourcePool(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityNatSourcePool(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityNatSourcePool(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_nat_source_pool", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_nat_source_pool", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -268,22 +268,22 @@ func resourceSecurityNatSourcePoolDelete(ctx context.Context, d *schema.Resource func resourceSecurityNatSourcePoolImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - securityNatSourcePoolExists, err := checkSecurityNatSourcePoolExists(d.Id(), m, jnprSess) + securityNatSourcePoolExists, err := checkSecurityNatSourcePoolExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !securityNatSourcePoolExists { return nil, fmt.Errorf("don't find nat source pool with id '%v' (id must be )", d.Id()) } - natSourcePoolOptions, err := readSecurityNatSourcePool(d.Id(), m, jnprSess) + natSourcePoolOptions, err := readSecurityNatSourcePool(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -294,10 +294,9 @@ func resourceSecurityNatSourcePoolImport(ctx context.Context, d *schema.Resource return result, nil } -func checkSecurityNatSourcePoolExists(name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "security nat source pool "+name+pipeDisplaySet, jnprSess) +func checkSecurityNatSourcePoolExists(name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "security nat source pool "+name+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -308,8 +307,7 @@ func checkSecurityNatSourcePoolExists(name string, m interface{}, jnprSess *Netc return true, nil } -func setSecurityNatSourcePool(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSecurityNatSourcePool(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set security nat source pool " + d.Get("name").(string) @@ -349,15 +347,14 @@ func setSecurityNatSourcePool(d *schema.ResourceData, m interface{}, jnprSess *N configSet = append(configSet, setPrefix+" routing-instance "+d.Get("routing_instance").(string)) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSecurityNatSourcePool(name string, m interface{}, jnprSess *NetconfObject) (natSourcePoolOptions, error) { - sess := m.(*Session) +func readSecurityNatSourcePool(name string, clt *Client, junSess *junosSession) (natSourcePoolOptions, error) { var confRead natSourcePoolOptions - showConfig, err := sess.command(cmdShowConfig+ - "security nat source pool "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security nat source pool "+name+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -413,12 +410,11 @@ func readSecurityNatSourcePool(name string, m interface{}, jnprSess *NetconfObje return confRead, nil } -func delSecurityNatSourcePool(natSourcePool string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSecurityNatSourcePool(natSourcePool string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete security nat source pool "+natSourcePool) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSecurityNatSourcePoolData(d *schema.ResourceData, natSourcePoolOptions natSourcePoolOptions) { diff --git a/junos/resource_security_nat_static.go b/junos/resource_security_nat_static.go index f67b27fc..b27e86ac 100644 --- a/junos/resource_security_nat_static.go +++ b/junos/resource_security_nat_static.go @@ -155,53 +155,53 @@ func resourceSecurityNatStatic() *schema.Resource { } func resourceSecurityNatStaticCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSecurityNatStatic(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSecurityNatStatic(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security nat static not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel)) + junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - securityNatStaticExists, err := checkSecurityNatStaticExists(d.Get("name").(string), m, jnprSess) + securityNatStaticExists, err := checkSecurityNatStaticExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if securityNatStaticExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security nat static %v already exists", d.Get("name").(string)))...) } - if err := setSecurityNatStatic(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityNatStatic(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_nat_static", jnprSess) + warns, err := clt.commitConf("create resource junos_security_nat_static", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - securityNatStaticExists, err = checkSecurityNatStaticExists(d.Get("name").(string), m, jnprSess) + securityNatStaticExists, err = checkSecurityNatStaticExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -212,24 +212,24 @@ func resourceSecurityNatStaticCreate(ctx context.Context, d *schema.ResourceData "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceSecurityNatStaticReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityNatStaticReadWJunSess(d, clt, junSess)...) } func resourceSecurityNatStaticRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSecurityNatStaticReadWJnprSess(d, m, jnprSess) + return resourceSecurityNatStaticReadWJunSess(d, clt, junSess) } -func resourceSecurityNatStaticReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSecurityNatStaticReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - natStaticOptions, err := readSecurityNatStatic(d.Get("name").(string), m, jnprSess) + natStaticOptions, err := readSecurityNatStatic(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -267,89 +267,89 @@ func resourceSecurityNatStaticUpdate(ctx context.Context, d *schema.ResourceData }) } } - sess := m.(*Session) - if sess.junosFakeUpdateAlso { + clt := m.(*Client) + if clt.fakeUpdateAlso { if configureRulesSingly { - if err := delSecurityNatStaticWithoutRules(d.Get("name").(string), m, nil); err != nil { + if err := delSecurityNatStaticWithoutRules(d.Get("name").(string), clt, nil); err != nil { return append(diagWarns, diag.FromErr(err)...) } } else { - if err := delSecurityNatStatic(d.Get("name").(string), m, nil); err != nil { + if err := delSecurityNatStatic(d.Get("name").(string), clt, nil); err != nil { return append(diagWarns, diag.FromErr(err)...) } } - if err := setSecurityNatStatic(d, m, nil); err != nil { + if err := setSecurityNatStatic(d, clt, nil); err != nil { return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) return diagWarns } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } if configureRulesSingly { - if err := delSecurityNatStaticWithoutRules(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityNatStaticWithoutRules(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } } else { - if err := delSecurityNatStatic(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityNatStatic(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } } - if err := setSecurityNatStatic(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityNatStatic(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_nat_static", jnprSess) + warns, err := clt.commitConf("update resource junos_security_nat_static", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSecurityNatStaticReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityNatStaticReadWJunSess(d, clt, junSess)...) } func resourceSecurityNatStaticDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSecurityNatStatic(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSecurityNatStatic(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityNatStatic(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityNatStatic(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_nat_static", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_nat_static", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -359,15 +359,15 @@ func resourceSecurityNatStaticDelete(ctx context.Context, d *schema.ResourceData func resourceSecurityNatStaticImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) idList := strings.Split(d.Id(), idSeparator) - securityNatStaticExists, err := checkSecurityNatStaticExists(idList[0], m, jnprSess) + securityNatStaticExists, err := checkSecurityNatStaticExists(idList[0], clt, junSess) if err != nil { return nil, err } @@ -375,7 +375,7 @@ func resourceSecurityNatStaticImport(ctx context.Context, d *schema.ResourceData return nil, fmt.Errorf("don't find nat static with id '%v' "+ "(id must be or %sno_rules)", idList[0], idSeparator) } - natStaticOptions, err := readSecurityNatStatic(idList[0], m, jnprSess) + natStaticOptions, err := readSecurityNatStatic(idList[0], clt, junSess) if err != nil { return nil, err } @@ -391,10 +391,9 @@ func resourceSecurityNatStaticImport(ctx context.Context, d *schema.ResourceData return result, nil } -func checkSecurityNatStaticExists(name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "security nat static rule-set "+name+pipeDisplaySet, jnprSess) +func checkSecurityNatStaticExists(name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "security nat static rule-set "+name+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -405,8 +404,7 @@ func checkSecurityNatStaticExists(name string, m interface{}, jnprSess *NetconfO return true, nil } -func setSecurityNatStatic(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSecurityNatStatic(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) regexpSourcePort := regexp.MustCompile(`^\d+( to \d+)?$`) @@ -510,15 +508,14 @@ func setSecurityNatStatic(d *schema.ResourceData, m interface{}, jnprSess *Netco configSet = append(configSet, setPrefix+" description \""+v+"\"") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSecurityNatStatic(name string, m interface{}, jnprSess *NetconfObject) (natStaticOptions, error) { - sess := m.(*Session) +func readSecurityNatStatic(name string, clt *Client, junSess *junosSession) (natStaticOptions, error) { var confRead natStaticOptions - showConfig, err := sess.command(cmdShowConfig+ - "security nat static rule-set "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security nat static rule-set "+name+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -642,21 +639,19 @@ func readSecurityNatStatic(name string, m interface{}, jnprSess *NetconfObject) return confRead, nil } -func delSecurityNatStatic(natStatic string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSecurityNatStatic(natStatic string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete security nat static rule-set "+natStatic) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func delSecurityNatStaticWithoutRules(natStatic string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSecurityNatStaticWithoutRules(natStatic string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete security nat static rule-set "+natStatic+" description") configSet = append(configSet, "delete security nat static rule-set "+natStatic+" from") - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSecurityNatStaticData(d *schema.ResourceData, natStaticOptions natStaticOptions) { diff --git a/junos/resource_security_nat_static_rule.go b/junos/resource_security_nat_static_rule.go index be02971c..a0d99af7 100644 --- a/junos/resource_security_nat_static_rule.go +++ b/junos/resource_security_nat_static_rule.go @@ -127,69 +127,73 @@ func resourceSecurityNatStaticRule() *schema.Resource { func resourceSecurityNatStaticRuleCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSecurityNatStaticRule(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSecurityNatStaticRule(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("rule_set").(string) + idSeparator + d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security nat static rule in rule-set not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel)) + junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - natStaticExists, err := checkSecurityNatStaticExists(d.Get("rule_set").(string), m, jnprSess) + natStaticExists, err := checkSecurityNatStaticExists(d.Get("rule_set").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !natStaticExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security nat static rule-set %v doesn't exist", d.Get("rule_set").(string)))...) } natStaticRuleExists, err := checkSecurityNatStaticRuleExists( - d.Get("rule_set").(string), d.Get("name").(string), m, jnprSess) + d.Get("rule_set").(string), + d.Get("name").(string), + clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if natStaticRuleExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf( "security nat static rule %v already exists in rule-set %s", d.Get("name").(string), d.Get("rule_set").(string)))...) } - if err := setSecurityNatStaticRule(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityNatStaticRule(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_nat_static_rule", jnprSess) + warns, err := clt.commitConf("create resource junos_security_nat_static_rule", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } natStaticRuleExists, err = checkSecurityNatStaticRuleExists( - d.Get("rule_set").(string), d.Get("name").(string), m, jnprSess) + d.Get("rule_set").(string), + d.Get("name").(string), + clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -201,24 +205,27 @@ func resourceSecurityNatStaticRuleCreate(ctx context.Context, d *schema.Resource "=> check your config", d.Get("name").(string), d.Get("rule_set").(string)))...) } - return append(diagWarns, resourceSecurityNatStaticRuleReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityNatStaticRuleReadWJunSess(d, clt, junSess)...) } func resourceSecurityNatStaticRuleRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSecurityNatStaticRuleReadWJnprSess(d, m, jnprSess) + return resourceSecurityNatStaticRuleReadWJunSess(d, clt, junSess) } -func resourceSecurityNatStaticRuleReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSecurityNatStaticRuleReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - natStaticRuleOptions, err := readSecurityNatStaticRule(d.Get("rule_set").(string), d.Get("name").(string), m, jnprSess) + natStaticRuleOptions, err := readSecurityNatStaticRule( + d.Get("rule_set").(string), + d.Get("name").(string), + clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -235,77 +242,77 @@ func resourceSecurityNatStaticRuleReadWJnprSess(d *schema.ResourceData, m interf func resourceSecurityNatStaticRuleUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSecurityNatStaticRule(d.Get("rule_set").(string), d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSecurityNatStaticRule(d.Get("rule_set").(string), d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSecurityNatStaticRule(d, m, nil); err != nil { + if err := setSecurityNatStaticRule(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityNatStaticRule(d.Get("rule_set").(string), d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityNatStaticRule(d.Get("rule_set").(string), d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSecurityNatStaticRule(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityNatStaticRule(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_nat_static_rule", jnprSess) + warns, err := clt.commitConf("update resource junos_security_nat_static_rule", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSecurityNatStaticRuleReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityNatStaticRuleReadWJunSess(d, clt, junSess)...) } func resourceSecurityNatStaticRuleDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSecurityNatStaticRule(d.Get("rule_set").(string), d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSecurityNatStaticRule(d.Get("rule_set").(string), d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityNatStaticRule(d.Get("rule_set").(string), d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityNatStaticRule(d.Get("rule_set").(string), d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_nat_static_rule", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_nat_static_rule", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -315,18 +322,18 @@ func resourceSecurityNatStaticRuleDelete(ctx context.Context, d *schema.Resource func resourceSecurityNatStaticRuleImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) idList := strings.Split(d.Id(), idSeparator) if len(idList) < 2 { return nil, fmt.Errorf("missing element(s) in id with separator %v", idSeparator) } - natStaticRuleExists, err := checkSecurityNatStaticRuleExists(idList[0], idList[1], m, jnprSess) + natStaticRuleExists, err := checkSecurityNatStaticRuleExists(idList[0], idList[1], clt, junSess) if err != nil { return nil, err } @@ -334,7 +341,7 @@ func resourceSecurityNatStaticRuleImport(ctx context.Context, d *schema.Resource return nil, fmt.Errorf( "don't find static nat rule with id '%v' (id must be "+idSeparator+")", d.Id()) } - natStaticRuleOptions, err := readSecurityNatStaticRule(idList[0], idList[1], m, jnprSess) + natStaticRuleOptions, err := readSecurityNatStaticRule(idList[0], idList[1], clt, junSess) if err != nil { return nil, err } @@ -345,10 +352,9 @@ func resourceSecurityNatStaticRuleImport(ctx context.Context, d *schema.Resource return result, nil } -func checkSecurityNatStaticRuleExists(ruleSet, name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "security nat static rule-set "+ruleSet+" rule "+name+pipeDisplaySet, jnprSess) +func checkSecurityNatStaticRuleExists(ruleSet, name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "security nat static rule-set "+ruleSet+" rule "+name+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -359,8 +365,7 @@ func checkSecurityNatStaticRuleExists(ruleSet, name string, m interface{}, jnprS return true, nil } -func setSecurityNatStaticRule(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSecurityNatStaticRule(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) regexpSourcePort := regexp.MustCompile(`^\d+( to \d+)?$`) @@ -439,16 +444,15 @@ func setSecurityNatStaticRule(d *schema.ResourceData, m interface{}, jnprSess *N } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSecurityNatStaticRule(ruleSet, name string, m interface{}, jnprSess *NetconfObject, +func readSecurityNatStaticRule(ruleSet, name string, clt *Client, junSess *junosSession, ) (natStaticRuleOptions, error) { - sess := m.(*Session) var confRead natStaticRuleOptions - showConfig, err := sess.command(cmdShowConfig+ - "security nat static rule-set "+ruleSet+" rule "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security nat static rule-set "+ruleSet+" rule "+name+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -540,11 +544,10 @@ func readSecurityNatStaticRule(ruleSet, name string, m interface{}, jnprSess *Ne return confRead, nil } -func delSecurityNatStaticRule(ruleSet, name string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSecurityNatStaticRule(ruleSet, name string, clt *Client, junSess *junosSession) error { configSet := []string{"delete security nat static rule-set " + ruleSet + " rule " + name} - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSecurityNatStaticRuleData(d *schema.ResourceData, natStaticRuleOptions natStaticRuleOptions) { diff --git a/junos/resource_security_policy.go b/junos/resource_security_policy.go index 2fca2922..6f66b256 100644 --- a/junos/resource_security_policy.go +++ b/junos/resource_security_policy.go @@ -193,56 +193,60 @@ func resourceSecurityPolicy() *schema.Resource { } func resourceSecurityPolicyCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSecurityPolicy(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSecurityPolicy(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("from_zone").(string) + idSeparator + d.Get("to_zone").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security policy not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel)) + junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - securityPolicyExists, err := checkSecurityPolicyExists(d.Get("from_zone").(string), d.Get("to_zone").(string), - m, jnprSess) + securityPolicyExists, err := checkSecurityPolicyExists( + d.Get("from_zone").(string), + d.Get("to_zone").(string), + clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if securityPolicyExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security policy from %v to %v already exists", d.Get("from_zone").(string), d.Get("to_zone").(string)))...) } - if err := setSecurityPolicy(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityPolicy(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_policy", jnprSess) + warns, err := clt.commitConf("create resource junos_security_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - securityPolicyExists, err = checkSecurityPolicyExists(d.Get("from_zone").(string), d.Get("to_zone").(string), - m, jnprSess) + securityPolicyExists, err = checkSecurityPolicyExists( + d.Get("from_zone").(string), + d.Get("to_zone").(string), + clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -253,25 +257,27 @@ func resourceSecurityPolicyCreate(ctx context.Context, d *schema.ResourceData, m "=> check your config", d.Get("from_zone").(string), d.Get("to_zone").(string)))...) } - return append(diagWarns, resourceSecurityPolicyReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityPolicyReadWJunSess(d, clt, junSess)...) } func resourceSecurityPolicyRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSecurityPolicyReadWJnprSess(d, m, jnprSess) + return resourceSecurityPolicyReadWJunSess(d, clt, junSess) } -func resourceSecurityPolicyReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSecurityPolicyReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - policyOptions, err := readSecurityPolicy(d.Get("from_zone").(string)+idSeparator+d.Get("to_zone").(string), - m, jnprSess) + policyOptions, err := readSecurityPolicy( + d.Get("from_zone").(string), + d.Get("to_zone").(string), + clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -287,88 +293,92 @@ func resourceSecurityPolicyReadWJnprSess(d *schema.ResourceData, m interface{}, func resourceSecurityPolicyUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSecurityPolicy(d.Get("from_zone").(string), d.Get("to_zone").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSecurityPolicy(d.Get("from_zone").(string), d.Get("to_zone").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSecurityPolicy(d, m, nil); err != nil { + if err := setSecurityPolicy(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics listLinesToPairPolicy := make([]string, 0) - if err := readSecurityPolicyTunnelPairPolicyLines(&listLinesToPairPolicy, - d.Get("from_zone").(string), d.Get("to_zone").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := readSecurityPolicyTunnelPairPolicyLines( + &listLinesToPairPolicy, + d.Get("from_zone").(string), + d.Get("to_zone").(string), + clt, junSess, + ); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := delSecurityPolicy(d.Get("from_zone").(string), d.Get("to_zone").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityPolicy(d.Get("from_zone").(string), d.Get("to_zone").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSecurityPolicy(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityPolicy(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := sess.configSet(listLinesToPairPolicy, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := clt.configSet(listLinesToPairPolicy, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_policy", jnprSess) + warns, err := clt.commitConf("update resource junos_security_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSecurityPolicyReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityPolicyReadWJunSess(d, clt, junSess)...) } func resourceSecurityPolicyDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSecurityPolicy(d.Get("from_zone").(string), d.Get("to_zone").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSecurityPolicy(d.Get("from_zone").(string), d.Get("to_zone").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityPolicy(d.Get("from_zone").(string), d.Get("to_zone").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityPolicy(d.Get("from_zone").(string), d.Get("to_zone").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_policy", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -378,25 +388,25 @@ func resourceSecurityPolicyDelete(ctx context.Context, d *schema.ResourceData, m func resourceSecurityPolicyImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) idList := strings.Split(d.Id(), idSeparator) if len(idList) < 2 { return nil, fmt.Errorf("missing element(s) in id with separator %v", idSeparator) } - securityPolicyExists, err := checkSecurityPolicyExists(idList[0], idList[1], m, jnprSess) + securityPolicyExists, err := checkSecurityPolicyExists(idList[0], idList[1], clt, junSess) if err != nil { return nil, err } if !securityPolicyExists { return nil, fmt.Errorf("don't find policy with id '%v' (id must be "+idSeparator+")", d.Id()) } - policyOptions, err := readSecurityPolicy(d.Id(), m, jnprSess) + policyOptions, err := readSecurityPolicy(idList[0], idList[1], clt, junSess) if err != nil { return nil, err } @@ -407,10 +417,9 @@ func resourceSecurityPolicyImport(ctx context.Context, d *schema.ResourceData, m return result, nil } -func checkSecurityPolicyExists(fromZone, toZone string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "security policies from-zone "+fromZone+" to-zone "+toZone+pipeDisplaySet, jnprSess) +func checkSecurityPolicyExists(fromZone, toZone string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "security policies from-zone "+fromZone+" to-zone "+toZone+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -421,8 +430,7 @@ func checkSecurityPolicyExists(fromZone, toZone string, m interface{}, jnprSess return true, nil } -func setSecurityPolicy(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSecurityPolicy(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set security policies" + @@ -498,19 +506,14 @@ func setSecurityPolicy(d *schema.ResourceData, m interface{}, jnprSess *NetconfO } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSecurityPolicy(idPolicy string, m interface{}, jnprSess *NetconfObject) (policyOptions, error) { - zone := strings.Split(idPolicy, idSeparator) - fromZone := zone[0] - toZone := zone[1] - - sess := m.(*Session) +func readSecurityPolicy(fromZone, toZone string, clt *Client, junSess *junosSession) (policyOptions, error) { var confRead policyOptions - showConfig, err := sess.command(cmdShowConfig+ - "security policies from-zone "+fromZone+" to-zone "+toZone+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security policies from-zone "+fromZone+" to-zone "+toZone+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -602,11 +605,10 @@ func readSecurityPolicy(idPolicy string, m interface{}, jnprSess *NetconfObject) } func readSecurityPolicyTunnelPairPolicyLines( - listLines *[]string, fromZone, toZone string, m interface{}, jnprSess *NetconfObject, + listLines *[]string, fromZone, toZone string, clt *Client, junSess *junosSession, ) error { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "security policies from-zone "+fromZone+" to-zone "+toZone+pipeDisplaySet, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security policies from-zone "+fromZone+" to-zone "+toZone+pipeDisplaySet, junSess) if err != nil { return err } @@ -627,12 +629,11 @@ func readSecurityPolicyTunnelPairPolicyLines( return nil } -func delSecurityPolicy(fromZone, toZone string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSecurityPolicy(fromZone, toZone string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete security policies from-zone "+fromZone+" to-zone "+toZone) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSecurityPolicyData(d *schema.ResourceData, policyOptions policyOptions) { diff --git a/junos/resource_security_policy_tunnel_pair_policy.go b/junos/resource_security_policy_tunnel_pair_policy.go index 9baca140..88f1a1a4 100644 --- a/junos/resource_security_policy_tunnel_pair_policy.go +++ b/junos/resource_security_policy_tunnel_pair_policy.go @@ -55,9 +55,9 @@ func resourceSecurityPolicyTunnelPairPolicy() *schema.Resource { func resourceSecurityPolicyTunnelPairPolicyCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSecurityPolicyTunnelPairPolicy(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSecurityPolicyTunnelPairPolicy(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("zone_a").(string) + idSeparator + d.Get("policy_a_to_b").(string) + @@ -65,71 +65,85 @@ func resourceSecurityPolicyTunnelPairPolicyCreate(ctx context.Context, d *schema return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security policy tunnel pair policy not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel)) + junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - securityPolicyExists, err := checkSecurityPolicyExists(d.Get("zone_a").(string), d.Get("zone_b").(string), m, jnprSess) + securityPolicyExists, err := checkSecurityPolicyExists( + d.Get("zone_a").(string), + d.Get("zone_b").(string), + clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !securityPolicyExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security policy from %v to %v not exists", d.Get("zone_a").(string), d.Get("zone_b").(string)))...) } - securityPolicyExists, err = checkSecurityPolicyExists(d.Get("zone_b").(string), d.Get("zone_a").(string), m, jnprSess) + securityPolicyExists, err = checkSecurityPolicyExists( + d.Get("zone_b").(string), + d.Get("zone_a").(string), + clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !securityPolicyExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security policy from %v to %v not exists", d.Get("zone_b").(string), d.Get("zone_a").(string)))...) } - pairPolicyExists, err := checkSecurityPolicyPairExists(d.Get("zone_a").(string), d.Get("policy_a_to_b").(string), - d.Get("zone_b").(string), d.Get("policy_b_to_a").(string), m, jnprSess) + pairPolicyExists, err := checkSecurityPolicyPairExists( + d.Get("zone_a").(string), + d.Get("policy_a_to_b").(string), + d.Get("zone_b").(string), + d.Get("policy_b_to_a").(string), + clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if pairPolicyExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security policy pair policy %v(%v) / %v(%v) already exists", d.Get("zone_a").(string), d.Get("policy_a_to_b").(string), d.Get("zone_b").(string), d.Get("policy_b_to_a").(string)))...) } - if err := setSecurityPolicyTunnelPairPolicy(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityPolicyTunnelPairPolicy(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_policy_tunnel_pair_policy", jnprSess) + warns, err := clt.commitConf("create resource junos_security_policy_tunnel_pair_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - pairPolicyExists, err = checkSecurityPolicyPairExists(d.Get("zone_a").(string), d.Get("policy_a_to_b").(string), - d.Get("zone_b").(string), d.Get("policy_b_to_a").(string), m, jnprSess) + pairPolicyExists, err = checkSecurityPolicyPairExists( + d.Get("zone_a").(string), + d.Get("policy_a_to_b").(string), + d.Get("zone_b").(string), + d.Get("policy_b_to_a").(string), + clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -141,28 +155,30 @@ func resourceSecurityPolicyTunnelPairPolicyCreate(ctx context.Context, d *schema "=> check your config"))...) } - return append(diagWarns, resourceSecurityPolicyTunnelPairPolicyReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityPolicyTunnelPairPolicyReadWJunSess(d, clt, junSess)...) } func resourceSecurityPolicyTunnelPairPolicyRead(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSecurityPolicyTunnelPairPolicyReadWJnprSess(d, m, jnprSess) + return resourceSecurityPolicyTunnelPairPolicyReadWJunSess(d, clt, junSess) } -func resourceSecurityPolicyTunnelPairPolicyReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSecurityPolicyTunnelPairPolicyReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - policyPairPolicyOptions, err := readSecurityPolicyTunnelPairPolicy(d.Get("zone_a").(string)+idSeparator+ - d.Get("policy_a_to_b").(string)+idSeparator+ - d.Get("zone_b").(string)+idSeparator+ - d.Get("policy_b_to_a").(string), m, jnprSess) + policyPairPolicyOptions, err := readSecurityPolicyTunnelPairPolicy( + d.Get("zone_a").(string), + d.Get("policy_a_to_b").(string), + d.Get("zone_b").(string), + d.Get("policy_b_to_a").(string), + clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -178,32 +194,32 @@ func resourceSecurityPolicyTunnelPairPolicyReadWJnprSess(d *schema.ResourceData, func resourceSecurityPolicyTunnelPairPolicyDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSecurityPolicyTunnelPairPolicy(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSecurityPolicyTunnelPairPolicy(d, clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityPolicyTunnelPairPolicy(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityPolicyTunnelPairPolicy(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_policy_tunnel_pair_policy", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_policy_tunnel_pair_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -213,18 +229,23 @@ func resourceSecurityPolicyTunnelPairPolicyDelete(ctx context.Context, d *schema func resourceSecurityPolicyTunnelPairPolicyImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) idList := strings.Split(d.Id(), idSeparator) if len(idList) < 4 { return nil, fmt.Errorf("missing element(s) in id with separator %v", idSeparator) } - poliyPairPolicyExists, err := checkSecurityPolicyPairExists(idList[0], idList[1], idList[2], idList[3], m, jnprSess) + poliyPairPolicyExists, err := checkSecurityPolicyPairExists( + idList[0], + idList[1], + idList[2], + idList[3], + clt, junSess) if err != nil { return nil, err } @@ -234,7 +255,12 @@ func resourceSecurityPolicyTunnelPairPolicyImport(ctx context.Context, d *schema ""+idSeparator+")", d.Id()) } - policyPairPolicyOptions, err := readSecurityPolicyTunnelPairPolicy(d.Id(), m, jnprSess) + policyPairPolicyOptions, err := readSecurityPolicyTunnelPairPolicy( + idList[0], + idList[1], + idList[2], + idList[3], + clt, junSess) if err != nil { return nil, err } @@ -245,19 +271,17 @@ func resourceSecurityPolicyTunnelPairPolicyImport(ctx context.Context, d *schema return result, nil } -func checkSecurityPolicyPairExists(zoneA, policyAtoB, zoneB, policyBtoA string, m interface{}, jnprSess *NetconfObject, +func checkSecurityPolicyPairExists(zoneA, policyAtoB, zoneB, policyBtoA string, clt *Client, junSess *junosSession, ) (bool, error) { - sess := m.(*Session) - - showConfigPairAtoB, err := sess.command(cmdShowConfig+ + showConfigPairAtoB, err := clt.command(cmdShowConfig+ "security policies from-zone "+zoneA+" to-zone "+zoneB+" policy "+policyAtoB+ - " then permit tunnel pair-policy"+pipeDisplaySet, jnprSess) + " then permit tunnel pair-policy"+pipeDisplaySet, junSess) if err != nil { return false, err } - showConfigPairBtoA, err := sess.command(cmdShowConfig+ + showConfigPairBtoA, err := clt.command(cmdShowConfig+ "security policies from-zone "+zoneB+" to-zone "+zoneA+" policy "+policyBtoA+ - " then permit tunnel pair-policy"+pipeDisplaySet, jnprSess) + " then permit tunnel pair-policy"+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -268,8 +292,7 @@ func checkSecurityPolicyPairExists(zoneA, policyAtoB, zoneB, policyBtoA string, return true, nil } -func setSecurityPolicyTunnelPairPolicy(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSecurityPolicyTunnelPairPolicy(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 2) configSet = append(configSet, "set security policies from-zone "+ @@ -281,23 +304,16 @@ func setSecurityPolicyTunnelPairPolicy(d *schema.ResourceData, m interface{}, jn " policy "+d.Get("policy_b_to_a").(string)+ " then permit tunnel pair-policy "+d.Get("policy_a_to_b").(string)) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSecurityPolicyTunnelPairPolicy(idRessource string, m interface{}, jnprSess *NetconfObject, +func readSecurityPolicyTunnelPairPolicy(zoneA, policyAtoB, zoneB, policyBtoA string, clt *Client, junSess *junosSession, ) (policyPairPolicyOptions, error) { - zone := strings.Split(idRessource, idSeparator) - zoneA := zone[0] - policyAtoB := zone[1] - zoneB := zone[2] - policyBtoA := zone[3] - - sess := m.(*Session) var confRead policyPairPolicyOptions - showConfig, err := sess.command(cmdShowConfig+ + showConfig, err := clt.command(cmdShowConfig+ "security policies from-zone "+zoneA+" to-zone "+zoneB+" policy "+policyAtoB+ - " then permit tunnel pair-policy"+pipeDisplaySet, jnprSess) + " then permit tunnel pair-policy"+pipeDisplaySet, junSess) if err != nil { return confRead, err } @@ -318,9 +334,9 @@ func readSecurityPolicyTunnelPairPolicy(idRessource string, m interface{}, jnprS } } } - showConfig, err = sess.command(cmdShowConfig+ + showConfig, err = clt.command(cmdShowConfig+ "security policies from-zone "+zoneB+" to-zone "+zoneA+" policy "+policyBtoA+ - " then permit tunnel pair-policy"+pipeDisplaySet, jnprSess) + " then permit tunnel pair-policy"+pipeDisplaySet, junSess) if err != nil { return confRead, err } @@ -345,8 +361,7 @@ func readSecurityPolicyTunnelPairPolicy(idRessource string, m interface{}, jnprS return confRead, nil } -func delSecurityPolicyTunnelPairPolicy(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSecurityPolicyTunnelPairPolicy(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 2) configSet = append(configSet, "delete security policies"+ " from-zone "+d.Get("zone_a").(string)+" to-zone "+d.Get("zone_b").(string)+ @@ -357,7 +372,7 @@ func delSecurityPolicyTunnelPairPolicy(d *schema.ResourceData, m interface{}, jn " policy "+d.Get("policy_b_to_a").(string)+ " then permit tunnel pair-policy "+d.Get("policy_a_to_b").(string)) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSecurityPolicyPairData(d *schema.ResourceData, policyPairPolicyOptions policyPairPolicyOptions) { diff --git a/junos/resource_security_screen.go b/junos/resource_security_screen.go index f83e2d6b..00461e4d 100644 --- a/junos/resource_security_screen.go +++ b/junos/resource_security_screen.go @@ -599,53 +599,53 @@ func resourceSecurityScreen() *schema.Resource { } func resourceSecurityScreenCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSecurityScreen(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSecurityScreen(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security screen not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel)) + junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - securityScreenExists, err := checkSecurityScreenExists(d.Get("name").(string), m, jnprSess) + securityScreenExists, err := checkSecurityScreenExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if securityScreenExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security screen %v already exists", d.Get("name").(string)))...) } - if err := setSecurityScreen(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityScreen(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_screen", jnprSess) + warns, err := clt.commitConf("create resource junos_security_screen", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - securityScreenExists, err = checkSecurityScreenExists(d.Get("name").(string), m, jnprSess) + securityScreenExists, err = checkSecurityScreenExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -656,24 +656,24 @@ func resourceSecurityScreenCreate(ctx context.Context, d *schema.ResourceData, m "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceSecurityScreenReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityScreenReadWJunSess(d, clt, junSess)...) } func resourceSecurityScreenRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSecurityScreenReadWJnprSess(d, m, jnprSess) + return resourceSecurityScreenReadWJunSess(d, clt, junSess) } -func resourceSecurityScreenReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSecurityScreenReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - screenOptions, err := readSecurityScreen(d.Get("name").(string), m, jnprSess) + screenOptions, err := readSecurityScreen(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -689,76 +689,76 @@ func resourceSecurityScreenReadWJnprSess(d *schema.ResourceData, m interface{}, func resourceSecurityScreenUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSecurityScreen(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSecurityScreen(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSecurityScreen(d, m, nil); err != nil { + if err := setSecurityScreen(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityScreen(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityScreen(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSecurityScreen(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityScreen(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_screen", jnprSess) + warns, err := clt.commitConf("update resource junos_security_screen", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSecurityScreenReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityScreenReadWJunSess(d, clt, junSess)...) } func resourceSecurityScreenDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSecurityScreen(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSecurityScreen(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityScreen(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityScreen(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_screen", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_screen", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -768,21 +768,21 @@ func resourceSecurityScreenDelete(ctx context.Context, d *schema.ResourceData, m func resourceSecurityScreenImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - securityScreenExists, err := checkSecurityScreenExists(d.Id(), m, jnprSess) + securityScreenExists, err := checkSecurityScreenExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !securityScreenExists { return nil, fmt.Errorf("don't find screen with id '%v' (id must be )", d.Id()) } - screenOptions, err := readSecurityScreen(d.Id(), m, jnprSess) + screenOptions, err := readSecurityScreen(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -793,9 +793,8 @@ func resourceSecurityScreenImport(ctx context.Context, d *schema.ResourceData, m return result, nil } -func checkSecurityScreenExists(name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"security screen ids-option \""+name+"\""+pipeDisplaySet, jnprSess) +func checkSecurityScreenExists(name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"security screen ids-option \""+name+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -806,8 +805,7 @@ func checkSecurityScreenExists(name string, m interface{}, jnprSess *NetconfObje return true, nil } -func setSecurityScreen(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSecurityScreen(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set security screen ids-option \"" + d.Get("name").(string) + "\" " @@ -865,7 +863,7 @@ func setSecurityScreen(d *schema.ResourceData, m interface{}, jnprSess *NetconfO configSet = append(configSet, setSecurityScreenUDP(udp, setPrefix)...) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func setSecurityScreenIcmp(icmp map[string]interface{}, setPrefix string) []string { @@ -1246,12 +1244,11 @@ func setSecurityScreenUDP(udp map[string]interface{}, setPrefix string) []string return configSet } -func readSecurityScreen(name string, m interface{}, jnprSess *NetconfObject) (screenOptions, error) { - sess := m.(*Session) +func readSecurityScreen(name string, clt *Client, junSess *junosSession) (screenOptions, error) { var confRead screenOptions - showConfig, err := sess.command(cmdShowConfig+ - "security screen ids-option \""+name+"\""+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security screen ids-option \""+name+"\""+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -1806,12 +1803,11 @@ func readSecurityScreenUDP(confRead *screenOptions, itemTrim string) error { return nil } -func delSecurityScreen(name string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSecurityScreen(name string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete security screen ids-option \""+name+"\"") - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSecurityScreenData(d *schema.ResourceData, screenOptions screenOptions) { diff --git a/junos/resource_security_screen_whitelist.go b/junos/resource_security_screen_whitelist.go index 36a1d8b5..be44ac96 100644 --- a/junos/resource_security_screen_whitelist.go +++ b/junos/resource_security_screen_whitelist.go @@ -45,54 +45,54 @@ func resourceSecurityScreenWhiteList() *schema.Resource { func resourceSecurityScreenWhiteListCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSecurityScreenWhiteList(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSecurityScreenWhiteList(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security screen white-list not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel)) + junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - securityScreenWhiteListExists, err := checkSecurityScreenWhiteListExists(d.Get("name").(string), m, jnprSess) + securityScreenWhiteListExists, err := checkSecurityScreenWhiteListExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if securityScreenWhiteListExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security screen white-list %v already exists", d.Get("name").(string)))...) } - if err := setSecurityScreenWhiteList(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityScreenWhiteList(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_screen_whitelist", jnprSess) + warns, err := clt.commitConf("create resource junos_security_screen_whitelist", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - securityScreenWhiteListExists, err = checkSecurityScreenWhiteListExists(d.Get("name").(string), m, jnprSess) + securityScreenWhiteListExists, err = checkSecurityScreenWhiteListExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -103,25 +103,25 @@ func resourceSecurityScreenWhiteListCreate(ctx context.Context, d *schema.Resour "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceSecurityScreenWhiteListReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityScreenWhiteListReadWJunSess(d, clt, junSess)...) } func resourceSecurityScreenWhiteListRead(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSecurityScreenWhiteListReadWJnprSess(d, m, jnprSess) + return resourceSecurityScreenWhiteListReadWJunSess(d, clt, junSess) } -func resourceSecurityScreenWhiteListReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSecurityScreenWhiteListReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - whiteListOptions, err := readSecurityScreenWhiteList(d.Get("name").(string), m, jnprSess) + whiteListOptions, err := readSecurityScreenWhiteList(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -138,77 +138,77 @@ func resourceSecurityScreenWhiteListReadWJnprSess(d *schema.ResourceData, m inte func resourceSecurityScreenWhiteListUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSecurityScreenWhiteList(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSecurityScreenWhiteList(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSecurityScreenWhiteList(d, m, nil); err != nil { + if err := setSecurityScreenWhiteList(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityScreenWhiteList(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityScreenWhiteList(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSecurityScreenWhiteList(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityScreenWhiteList(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_screen_whitelist", jnprSess) + warns, err := clt.commitConf("update resource junos_security_screen_whitelist", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSecurityScreenWhiteListReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityScreenWhiteListReadWJunSess(d, clt, junSess)...) } func resourceSecurityScreenWhiteListDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSecurityScreenWhiteList(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSecurityScreenWhiteList(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityScreenWhiteList(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityScreenWhiteList(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_screen_whitelist", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_screen_whitelist", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -218,21 +218,21 @@ func resourceSecurityScreenWhiteListDelete(ctx context.Context, d *schema.Resour func resourceSecurityScreenWhiteListImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - securityScreenWhiteListExists, err := checkSecurityScreenWhiteListExists(d.Id(), m, jnprSess) + securityScreenWhiteListExists, err := checkSecurityScreenWhiteListExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !securityScreenWhiteListExists { return nil, fmt.Errorf("don't find screen white-list with id '%v' (id must be )", d.Id()) } - whiteListOptions, err := readSecurityScreenWhiteList(d.Id(), m, jnprSess) + whiteListOptions, err := readSecurityScreenWhiteList(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -243,9 +243,8 @@ func resourceSecurityScreenWhiteListImport(ctx context.Context, d *schema.Resour return result, nil } -func checkSecurityScreenWhiteListExists(name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"security screen white-list "+name+pipeDisplaySet, jnprSess) +func checkSecurityScreenWhiteListExists(name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"security screen white-list "+name+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -256,8 +255,7 @@ func checkSecurityScreenWhiteListExists(name string, m interface{}, jnprSess *Ne return true, nil } -func setSecurityScreenWhiteList(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSecurityScreenWhiteList(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set security screen white-list " + d.Get("name").(string) + " " @@ -266,15 +264,14 @@ func setSecurityScreenWhiteList(d *schema.ResourceData, m interface{}, jnprSess configSet = append(configSet, setPrefix+"address "+v) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSecurityScreenWhiteList(name string, m interface{}, jnprSess *NetconfObject) (screenWhiteListOptions, error) { - sess := m.(*Session) +func readSecurityScreenWhiteList(name string, clt *Client, junSess *junosSession) (screenWhiteListOptions, error) { var confRead screenWhiteListOptions - showConfig, err := sess.command(cmdShowConfig+ - "security screen white-list "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security screen white-list "+name+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -297,12 +294,11 @@ func readSecurityScreenWhiteList(name string, m interface{}, jnprSess *NetconfOb return confRead, nil } -func delSecurityScreenWhiteList(name string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSecurityScreenWhiteList(name string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete security screen white-list "+name) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSecurityScreenWhiteListData(d *schema.ResourceData, whiteListOptions screenWhiteListOptions) { diff --git a/junos/resource_security_utm_custom_url_category.go b/junos/resource_security_utm_custom_url_category.go index 5bf526c9..591542d9 100644 --- a/junos/resource_security_utm_custom_url_category.go +++ b/junos/resource_security_utm_custom_url_category.go @@ -42,54 +42,54 @@ func resourceSecurityUtmCustomURLCategory() *schema.Resource { func resourceSecurityUtmCustomURLCategoryCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setUtmCustomURLCategory(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setUtmCustomURLCategory(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security utm custom-objects custom-url-category "+ - "not compatible with Junos device %s", jnprSess.SystemInformation.HardwareModel)) + "not compatible with Junos device %s", junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - utmCustomURLCategoryExists, err := checkUtmCustomURLCategorysExists(d.Get("name").(string), m, jnprSess) + utmCustomURLCategoryExists, err := checkUtmCustomURLCategorysExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if utmCustomURLCategoryExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf( "security utm custom-objects custom-url-category %v already exists", d.Get("name").(string)))...) } - if err := setUtmCustomURLCategory(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setUtmCustomURLCategory(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_utm_custom_url_category", jnprSess) + warns, err := clt.commitConf("create resource junos_security_utm_custom_url_category", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - utmCustomURLCategoryExists, err = checkUtmCustomURLCategorysExists(d.Get("name").(string), m, jnprSess) + utmCustomURLCategoryExists, err = checkUtmCustomURLCategorysExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -100,25 +100,25 @@ func resourceSecurityUtmCustomURLCategoryCreate(ctx context.Context, d *schema.R "not exists after commit => check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceSecurityUtmCustomURLCategoryReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityUtmCustomURLCategoryReadWJunSess(d, clt, junSess)...) } func resourceSecurityUtmCustomURLCategoryRead(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSecurityUtmCustomURLCategoryReadWJnprSess(d, m, jnprSess) + return resourceSecurityUtmCustomURLCategoryReadWJunSess(d, clt, junSess) } -func resourceSecurityUtmCustomURLCategoryReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSecurityUtmCustomURLCategoryReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - utmCustomURLCategoryOptions, err := readUtmCustomURLCategory(d.Get("name").(string), m, jnprSess) + utmCustomURLCategoryOptions, err := readUtmCustomURLCategory(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -135,77 +135,77 @@ func resourceSecurityUtmCustomURLCategoryReadWJnprSess(d *schema.ResourceData, m func resourceSecurityUtmCustomURLCategoryUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delUtmCustomURLCategory(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delUtmCustomURLCategory(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setUtmCustomURLCategory(d, m, nil); err != nil { + if err := setUtmCustomURLCategory(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delUtmCustomURLCategory(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delUtmCustomURLCategory(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setUtmCustomURLCategory(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setUtmCustomURLCategory(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_utm_custom_url_category", jnprSess) + warns, err := clt.commitConf("update resource junos_security_utm_custom_url_category", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSecurityUtmCustomURLCategoryReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityUtmCustomURLCategoryReadWJunSess(d, clt, junSess)...) } func resourceSecurityUtmCustomURLCategoryDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delUtmCustomURLCategory(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delUtmCustomURLCategory(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delUtmCustomURLCategory(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delUtmCustomURLCategory(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_utm_custom_url_category", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_utm_custom_url_category", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -215,14 +215,14 @@ func resourceSecurityUtmCustomURLCategoryDelete(ctx context.Context, d *schema.R func resourceSecurityUtmCustomURLCategoryImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - utmCustomURLCategoryExists, err := checkUtmCustomURLCategorysExists(d.Id(), m, jnprSess) + utmCustomURLCategoryExists, err := checkUtmCustomURLCategorysExists(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -230,7 +230,7 @@ func resourceSecurityUtmCustomURLCategoryImport(ctx context.Context, d *schema.R return nil, fmt.Errorf( "missing security utm custom-objects custom-url-category with id '%v' (id must be )", d.Id()) } - utmCustomURLCategoryOptions, err := readUtmCustomURLCategory(d.Id(), m, jnprSess) + utmCustomURLCategoryOptions, err := readUtmCustomURLCategory(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -241,10 +241,9 @@ func resourceSecurityUtmCustomURLCategoryImport(ctx context.Context, d *schema.R return result, nil } -func checkUtmCustomURLCategorysExists(urlCategory string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "security utm custom-objects custom-url-category "+urlCategory+pipeDisplaySet, jnprSess) +func checkUtmCustomURLCategorysExists(urlCategory string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "security utm custom-objects custom-url-category "+urlCategory+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -255,8 +254,7 @@ func checkUtmCustomURLCategorysExists(urlCategory string, m interface{}, jnprSes return true, nil } -func setUtmCustomURLCategory(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setUtmCustomURLCategory(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set security utm custom-objects custom-url-category " + d.Get("name").(string) + " " @@ -264,16 +262,15 @@ func setUtmCustomURLCategory(d *schema.ResourceData, m interface{}, jnprSess *Ne configSet = append(configSet, setPrefix+"value "+v.(string)) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readUtmCustomURLCategory(urlCategory string, m interface{}, jnprSess *NetconfObject, +func readUtmCustomURLCategory(urlCategory string, clt *Client, junSess *junosSession, ) (utmCustomURLCategoryOptions, error) { - sess := m.(*Session) var confRead utmCustomURLCategoryOptions - showConfig, err := sess.command(cmdShowConfig+ - "security utm custom-objects custom-url-category "+urlCategory+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security utm custom-objects custom-url-category "+urlCategory+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -296,12 +293,11 @@ func readUtmCustomURLCategory(urlCategory string, m interface{}, jnprSess *Netco return confRead, nil } -func delUtmCustomURLCategory(urlCategory string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delUtmCustomURLCategory(urlCategory string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete security utm custom-objects custom-url-category "+urlCategory) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillUtmCustomURLCategoryData(d *schema.ResourceData, utmCustomURLCategoryOptions utmCustomURLCategoryOptions) { diff --git a/junos/resource_security_utm_custom_url_pattern.go b/junos/resource_security_utm_custom_url_pattern.go index 8ad63b0d..5de1572f 100644 --- a/junos/resource_security_utm_custom_url_pattern.go +++ b/junos/resource_security_utm_custom_url_pattern.go @@ -42,53 +42,53 @@ func resourceSecurityUtmCustomURLPattern() *schema.Resource { func resourceSecurityUtmCustomURLPatternCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setUtmCustomURLPattern(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setUtmCustomURLPattern(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security utm custom-objects url-pattern "+ - "not compatible with Junos device %s", jnprSess.SystemInformation.HardwareModel)) + "not compatible with Junos device %s", junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - utmCustomURLPatternExists, err := checkUtmCustomURLPatternsExists(d.Get("name").(string), m, jnprSess) + utmCustomURLPatternExists, err := checkUtmCustomURLPatternsExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if utmCustomURLPatternExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security utm custom-objects url-pattern %v already exists", d.Get("name").(string)))...) } - if err := setUtmCustomURLPattern(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setUtmCustomURLPattern(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_utm_custom_url_pattern", jnprSess) + warns, err := clt.commitConf("create resource junos_security_utm_custom_url_pattern", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - utmCustomURLPatternExists, err = checkUtmCustomURLPatternsExists(d.Get("name").(string), m, jnprSess) + utmCustomURLPatternExists, err = checkUtmCustomURLPatternsExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -99,25 +99,25 @@ func resourceSecurityUtmCustomURLPatternCreate(ctx context.Context, d *schema.Re "not exists after commit => check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceSecurityUtmCustomURLPatternReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityUtmCustomURLPatternReadWJunSess(d, clt, junSess)...) } func resourceSecurityUtmCustomURLPatternRead(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSecurityUtmCustomURLPatternReadWJnprSess(d, m, jnprSess) + return resourceSecurityUtmCustomURLPatternReadWJunSess(d, clt, junSess) } -func resourceSecurityUtmCustomURLPatternReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSecurityUtmCustomURLPatternReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - utmCustomURLPatternOptions, err := readUtmCustomURLPattern(d.Get("name").(string), m, jnprSess) + utmCustomURLPatternOptions, err := readUtmCustomURLPattern(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -134,77 +134,77 @@ func resourceSecurityUtmCustomURLPatternReadWJnprSess(d *schema.ResourceData, m func resourceSecurityUtmCustomURLPatternUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delUtmCustomURLPattern(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delUtmCustomURLPattern(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setUtmCustomURLPattern(d, m, nil); err != nil { + if err := setUtmCustomURLPattern(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delUtmCustomURLPattern(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delUtmCustomURLPattern(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setUtmCustomURLPattern(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setUtmCustomURLPattern(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_utm_custom_url_pattern", jnprSess) + warns, err := clt.commitConf("update resource junos_security_utm_custom_url_pattern", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSecurityUtmCustomURLPatternReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityUtmCustomURLPatternReadWJunSess(d, clt, junSess)...) } func resourceSecurityUtmCustomURLPatternDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delUtmCustomURLPattern(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delUtmCustomURLPattern(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delUtmCustomURLPattern(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delUtmCustomURLPattern(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_utm_custom_url_pattern", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_utm_custom_url_pattern", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -214,21 +214,21 @@ func resourceSecurityUtmCustomURLPatternDelete(ctx context.Context, d *schema.Re func resourceSecurityUtmCustomURLPatternImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - utmCustomURLPatternExists, err := checkUtmCustomURLPatternsExists(d.Id(), m, jnprSess) + utmCustomURLPatternExists, err := checkUtmCustomURLPatternsExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !utmCustomURLPatternExists { return nil, fmt.Errorf("don't find security utm custom-objects url-pattern with id '%v' (id must be )", d.Id()) } - utmCustomURLPatternOptions, err := readUtmCustomURLPattern(d.Id(), m, jnprSess) + utmCustomURLPatternOptions, err := readUtmCustomURLPattern(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -239,10 +239,9 @@ func resourceSecurityUtmCustomURLPatternImport(ctx context.Context, d *schema.Re return result, nil } -func checkUtmCustomURLPatternsExists(urlPattern string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "security utm custom-objects url-pattern "+urlPattern+pipeDisplaySet, jnprSess) +func checkUtmCustomURLPatternsExists(urlPattern string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "security utm custom-objects url-pattern "+urlPattern+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -253,8 +252,7 @@ func checkUtmCustomURLPatternsExists(urlPattern string, m interface{}, jnprSess return true, nil } -func setUtmCustomURLPattern(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setUtmCustomURLPattern(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set security utm custom-objects url-pattern " + d.Get("name").(string) + " " @@ -262,16 +260,15 @@ func setUtmCustomURLPattern(d *schema.ResourceData, m interface{}, jnprSess *Net configSet = append(configSet, setPrefix+"value "+v.(string)) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readUtmCustomURLPattern(urlPattern string, m interface{}, jnprSess *NetconfObject, +func readUtmCustomURLPattern(urlPattern string, clt *Client, junSess *junosSession, ) (utmCustomURLPatternOptions, error) { - sess := m.(*Session) var confRead utmCustomURLPatternOptions - showConfig, err := sess.command(cmdShowConfig+ - "security utm custom-objects url-pattern "+urlPattern+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security utm custom-objects url-pattern "+urlPattern+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -294,12 +291,11 @@ func readUtmCustomURLPattern(urlPattern string, m interface{}, jnprSess *Netconf return confRead, nil } -func delUtmCustomURLPattern(urlPattern string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delUtmCustomURLPattern(urlPattern string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete security utm custom-objects url-pattern "+urlPattern) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillUtmCustomURLPatternData(d *schema.ResourceData, utmCustomURLPatternOptions utmCustomURLPatternOptions) { diff --git a/junos/resource_security_utm_policy.go b/junos/resource_security_utm_policy.go index 8b2c2adb..b2480cc5 100644 --- a/junos/resource_security_utm_policy.go +++ b/junos/resource_security_utm_policy.go @@ -134,54 +134,54 @@ func resourceSecurityUtmPolicy() *schema.Resource { } func resourceSecurityUtmPolicyCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setUtmPolicy(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setUtmPolicy(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security utm utm-policy "+ - "not compatible with Junos device %s", jnprSess.SystemInformation.HardwareModel)) + "not compatible with Junos device %s", junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - utmPolicyExists, err := checkUtmPolicysExists(d.Get("name").(string), m, jnprSess) + utmPolicyExists, err := checkUtmPolicysExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if utmPolicyExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security utm utm-policy %v already exists", d.Get("name").(string)))...) } - if err := setUtmPolicy(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setUtmPolicy(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_utm_policy", jnprSess) + warns, err := clt.commitConf("create resource junos_security_utm_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - utmPolicyExists, err = checkUtmPolicysExists(d.Get("name").(string), m, jnprSess) + utmPolicyExists, err = checkUtmPolicysExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -192,24 +192,24 @@ func resourceSecurityUtmPolicyCreate(ctx context.Context, d *schema.ResourceData "not exists after commit => check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceSecurityUtmPolicyReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityUtmPolicyReadWJunSess(d, clt, junSess)...) } func resourceSecurityUtmPolicyRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSecurityUtmPolicyReadWJnprSess(d, m, jnprSess) + return resourceSecurityUtmPolicyReadWJunSess(d, clt, junSess) } -func resourceSecurityUtmPolicyReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSecurityUtmPolicyReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - utmPolicyOptions, err := readUtmPolicy(d.Get("name").(string), m, jnprSess) + utmPolicyOptions, err := readUtmPolicy(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -225,76 +225,76 @@ func resourceSecurityUtmPolicyReadWJnprSess(d *schema.ResourceData, m interface{ func resourceSecurityUtmPolicyUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delUtmPolicy(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delUtmPolicy(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setUtmPolicy(d, m, nil); err != nil { + if err := setUtmPolicy(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delUtmPolicy(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delUtmPolicy(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setUtmPolicy(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setUtmPolicy(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_utm_policy", jnprSess) + warns, err := clt.commitConf("update resource junos_security_utm_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSecurityUtmPolicyReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityUtmPolicyReadWJunSess(d, clt, junSess)...) } func resourceSecurityUtmPolicyDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delUtmPolicy(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delUtmPolicy(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delUtmPolicy(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delUtmPolicy(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_utm_policy", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_utm_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -304,21 +304,21 @@ func resourceSecurityUtmPolicyDelete(ctx context.Context, d *schema.ResourceData func resourceSecurityUtmPolicyImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - utmPolicyExists, err := checkUtmPolicysExists(d.Id(), m, jnprSess) + utmPolicyExists, err := checkUtmPolicysExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !utmPolicyExists { return nil, fmt.Errorf("don't find security utm utm-policy with id '%v' (id must be )", d.Id()) } - utmPolicyOptions, err := readUtmPolicy(d.Id(), m, jnprSess) + utmPolicyOptions, err := readUtmPolicy(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -329,9 +329,8 @@ func resourceSecurityUtmPolicyImport(ctx context.Context, d *schema.ResourceData return result, nil } -func checkUtmPolicysExists(policy string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"security utm utm-policy \""+policy+"\""+pipeDisplaySet, jnprSess) +func checkUtmPolicysExists(policy string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"security utm utm-policy \""+policy+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -342,8 +341,7 @@ func checkUtmPolicysExists(policy string, m interface{}, jnprSess *NetconfObject return true, nil } -func setUtmPolicy(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setUtmPolicy(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set security utm utm-policy \"" + d.Get("name").(string) + "\" " @@ -434,16 +432,15 @@ func setUtmPolicy(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject d.Get("web_filtering_profile").(string)+"\"") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readUtmPolicy(policy string, m interface{}, jnprSess *NetconfObject, +func readUtmPolicy(policy string, clt *Client, junSess *junosSession, ) (utmPolicyOptions, error) { - sess := m.(*Session) var confRead utmPolicyOptions - showConfig, err := sess.command(cmdShowConfig+ - "security utm utm-policy \""+policy+"\""+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security utm utm-policy \""+policy+"\""+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -532,12 +529,11 @@ func readUtmPolicyProfile(itemTrimPolicyProfile string, profileMap map[string]in } } -func delUtmPolicy(policy string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delUtmPolicy(policy string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete security utm utm-policy \""+policy+"\"") - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillUtmPolicyData(d *schema.ResourceData, utmPolicyOptions utmPolicyOptions) { diff --git a/junos/resource_security_utm_profile_web_filtering_juniper_enhanced.go b/junos/resource_security_utm_profile_web_filtering_juniper_enhanced.go index 103d57d1..5ee149c8 100644 --- a/junos/resource_security_utm_profile_web_filtering_juniper_enhanced.go +++ b/junos/resource_security_utm_profile_web_filtering_juniper_enhanced.go @@ -190,54 +190,54 @@ func resourceSecurityUtmProfileWebFilteringEnhanced() *schema.Resource { func resourceSecurityUtmProfileWebFilteringEnhancedCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setUtmProfileWebFEnhanced(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setUtmProfileWebFEnhanced(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security utm feature-profile web-filtering juniper-enhanced "+ - "not compatible with Junos device %s", jnprSess.SystemInformation.HardwareModel)) + "not compatible with Junos device %s", junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - utmProfileWebFEnhancedExists, err := checkUtmProfileWebFEnhancedExists(d.Get("name").(string), m, jnprSess) + utmProfileWebFEnhancedExists, err := checkUtmProfileWebFEnhancedExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if utmProfileWebFEnhancedExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security utm feature-profile web-filtering juniper-enhanced "+ "%v already exists", d.Get("name").(string)))...) } - if err := setUtmProfileWebFEnhanced(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setUtmProfileWebFEnhanced(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_utm_profile_web_filtering_juniper_enhanced", jnprSess) + warns, err := clt.commitConf("create resource junos_security_utm_profile_web_filtering_juniper_enhanced", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - utmProfileWebFEnhancedExists, err = checkUtmProfileWebFEnhancedExists(d.Get("name").(string), m, jnprSess) + utmProfileWebFEnhancedExists, err = checkUtmProfileWebFEnhancedExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -248,26 +248,26 @@ func resourceSecurityUtmProfileWebFilteringEnhancedCreate(ctx context.Context, d "not exists after commit => check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceSecurityUtmProfileWebFilteringEnhancedReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityUtmProfileWebFilteringEnhancedReadWJunSess(d, clt, junSess)...) } func resourceSecurityUtmProfileWebFilteringEnhancedRead(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSecurityUtmProfileWebFilteringEnhancedReadWJnprSess(d, m, jnprSess) + return resourceSecurityUtmProfileWebFilteringEnhancedReadWJunSess(d, clt, junSess) } -func resourceSecurityUtmProfileWebFilteringEnhancedReadWJnprSess( - d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSecurityUtmProfileWebFilteringEnhancedReadWJunSess( + d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - utmProfileWebFEnhancedOptions, err := readUtmProfileWebFEnhanced(d.Get("name").(string), m, jnprSess) + utmProfileWebFEnhancedOptions, err := readUtmProfileWebFEnhanced(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -284,77 +284,77 @@ func resourceSecurityUtmProfileWebFilteringEnhancedReadWJnprSess( func resourceSecurityUtmProfileWebFilteringEnhancedUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delUtmProfileWebFEnhanced(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delUtmProfileWebFEnhanced(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setUtmProfileWebFEnhanced(d, m, nil); err != nil { + if err := setUtmProfileWebFEnhanced(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delUtmProfileWebFEnhanced(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delUtmProfileWebFEnhanced(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setUtmProfileWebFEnhanced(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setUtmProfileWebFEnhanced(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_utm_profile_web_filtering_juniper_enhanced", jnprSess) + warns, err := clt.commitConf("update resource junos_security_utm_profile_web_filtering_juniper_enhanced", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSecurityUtmProfileWebFilteringEnhancedReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityUtmProfileWebFilteringEnhancedReadWJunSess(d, clt, junSess)...) } func resourceSecurityUtmProfileWebFilteringEnhancedDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delUtmProfileWebFEnhanced(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delUtmProfileWebFEnhanced(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delUtmProfileWebFEnhanced(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delUtmProfileWebFEnhanced(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_utm_profile_web_filtering_juniper_enhanced", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_utm_profile_web_filtering_juniper_enhanced", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -364,14 +364,14 @@ func resourceSecurityUtmProfileWebFilteringEnhancedDelete(ctx context.Context, d func resourceSecurityUtmProfileWebFilteringEnhancedImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - utmProfileWebFEnhancedExists, err := checkUtmProfileWebFEnhancedExists(d.Id(), m, jnprSess) + utmProfileWebFEnhancedExists, err := checkUtmProfileWebFEnhancedExists(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -379,7 +379,7 @@ func resourceSecurityUtmProfileWebFilteringEnhancedImport(ctx context.Context, d return nil, fmt.Errorf("don't find security utm feature-profile web-filtering juniper-enhanced with id "+ "'%v' (id must be )", d.Id()) } - utmProfileWebFEnhancedOptions, err := readUtmProfileWebFEnhanced(d.Id(), m, jnprSess) + utmProfileWebFEnhancedOptions, err := readUtmProfileWebFEnhanced(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -390,10 +390,9 @@ func resourceSecurityUtmProfileWebFilteringEnhancedImport(ctx context.Context, d return result, nil } -func checkUtmProfileWebFEnhancedExists(profile string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "security utm feature-profile web-filtering juniper-enhanced profile \""+profile+"\""+pipeDisplaySet, jnprSess) +func checkUtmProfileWebFEnhancedExists(profile string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "security utm feature-profile web-filtering juniper-enhanced profile \""+profile+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -404,8 +403,7 @@ func checkUtmProfileWebFEnhancedExists(profile string, m interface{}, jnprSess * return true, nil } -func setUtmProfileWebFEnhanced(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setUtmProfileWebFEnhanced(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set security utm feature-profile web-filtering juniper-enhanced " + @@ -508,17 +506,16 @@ func setUtmProfileWebFEnhanced(d *schema.ResourceData, m interface{}, jnprSess * configSet = append(configSet, setPrefix+"timeout "+strconv.Itoa(d.Get("timeout").(int))) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readUtmProfileWebFEnhanced(profile string, m interface{}, jnprSess *NetconfObject, +func readUtmProfileWebFEnhanced(profile string, clt *Client, junSess *junosSession, ) (utmProfileWebFilteringEnhancedOptions, error) { - sess := m.(*Session) var confRead utmProfileWebFilteringEnhancedOptions - showConfig, err := sess.command(cmdShowConfig+ + showConfig, err := clt.command(cmdShowConfig+ "security utm feature-profile web-filtering juniper-enhanced"+ - " profile \""+profile+"\""+pipeDisplaySetRelative, jnprSess) + " profile \""+profile+"\""+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -628,13 +625,12 @@ func readUtmProfileWebFEnhanced(profile string, m interface{}, jnprSess *Netconf return confRead, nil } -func delUtmProfileWebFEnhanced(profile string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delUtmProfileWebFEnhanced(profile string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete security utm feature-profile web-filtering juniper-enhanced "+ "profile \""+profile+"\"") - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillUtmProfileWebFEnhancedData( diff --git a/junos/resource_security_utm_profile_web_filtering_juniper_local.go b/junos/resource_security_utm_profile_web_filtering_juniper_local.go index 105edf5a..364a4c5f 100644 --- a/junos/resource_security_utm_profile_web_filtering_juniper_local.go +++ b/junos/resource_security_utm_profile_web_filtering_juniper_local.go @@ -83,54 +83,54 @@ func resourceSecurityUtmProfileWebFilteringLocal() *schema.Resource { func resourceSecurityUtmProfileWebFilteringLocalCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setUtmProfileWebFLocal(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setUtmProfileWebFLocal(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security utm feature-profile web-filtering juniper-local "+ - "not compatible with Junos device %s", jnprSess.SystemInformation.HardwareModel)) + "not compatible with Junos device %s", junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - utmProfileWebFLocalExists, err := checkUtmProfileWebFLocalExists(d.Get("name").(string), m, jnprSess) + utmProfileWebFLocalExists, err := checkUtmProfileWebFLocalExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if utmProfileWebFLocalExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security utm feature-profile web-filtering juniper-local "+ "%v already exists", d.Get("name").(string)))...) } - if err := setUtmProfileWebFLocal(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setUtmProfileWebFLocal(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_utm_profile_web_filtering_juniper_local", jnprSess) + warns, err := clt.commitConf("create resource junos_security_utm_profile_web_filtering_juniper_local", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - utmProfileWebFLocalExists, err = checkUtmProfileWebFLocalExists(d.Get("name").(string), m, jnprSess) + utmProfileWebFLocalExists, err = checkUtmProfileWebFLocalExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -141,26 +141,26 @@ func resourceSecurityUtmProfileWebFilteringLocalCreate(ctx context.Context, d *s "not exists after commit => check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceSecurityUtmProfileWebFilteringLocalReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityUtmProfileWebFilteringLocalReadWJunSess(d, clt, junSess)...) } func resourceSecurityUtmProfileWebFilteringLocalRead(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSecurityUtmProfileWebFilteringLocalReadWJnprSess(d, m, jnprSess) + return resourceSecurityUtmProfileWebFilteringLocalReadWJunSess(d, clt, junSess) } -func resourceSecurityUtmProfileWebFilteringLocalReadWJnprSess( - d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSecurityUtmProfileWebFilteringLocalReadWJunSess( + d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - utmProfileWebFLocalOptions, err := readUtmProfileWebFLocal(d.Get("name").(string), m, jnprSess) + utmProfileWebFLocalOptions, err := readUtmProfileWebFLocal(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -177,77 +177,77 @@ func resourceSecurityUtmProfileWebFilteringLocalReadWJnprSess( func resourceSecurityUtmProfileWebFilteringLocalUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delUtmProfileWebFLocal(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delUtmProfileWebFLocal(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setUtmProfileWebFLocal(d, m, nil); err != nil { + if err := setUtmProfileWebFLocal(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delUtmProfileWebFLocal(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delUtmProfileWebFLocal(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setUtmProfileWebFLocal(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setUtmProfileWebFLocal(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_utm_profile_web_filtering_juniper_local", jnprSess) + warns, err := clt.commitConf("update resource junos_security_utm_profile_web_filtering_juniper_local", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSecurityUtmProfileWebFilteringLocalReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityUtmProfileWebFilteringLocalReadWJunSess(d, clt, junSess)...) } func resourceSecurityUtmProfileWebFilteringLocalDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delUtmProfileWebFLocal(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delUtmProfileWebFLocal(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delUtmProfileWebFLocal(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delUtmProfileWebFLocal(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_utm_profile_web_filtering_juniper_local", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_utm_profile_web_filtering_juniper_local", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -257,14 +257,14 @@ func resourceSecurityUtmProfileWebFilteringLocalDelete(ctx context.Context, d *s func resourceSecurityUtmProfileWebFilteringLocalImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - utmProfileWebFLocalExists, err := checkUtmProfileWebFLocalExists(d.Id(), m, jnprSess) + utmProfileWebFLocalExists, err := checkUtmProfileWebFLocalExists(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -272,7 +272,7 @@ func resourceSecurityUtmProfileWebFilteringLocalImport(ctx context.Context, d *s return nil, fmt.Errorf("don't find security utm feature-profile web-filtering juniper-local with id "+ "'%v' (id must be )", d.Id()) } - utmProfileWebFLocalOptions, err := readUtmProfileWebFLocal(d.Id(), m, jnprSess) + utmProfileWebFLocalOptions, err := readUtmProfileWebFLocal(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -283,10 +283,9 @@ func resourceSecurityUtmProfileWebFilteringLocalImport(ctx context.Context, d *s return result, nil } -func checkUtmProfileWebFLocalExists(profile string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "security utm feature-profile web-filtering juniper-local profile \""+profile+"\""+pipeDisplaySet, jnprSess) +func checkUtmProfileWebFLocalExists(profile string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "security utm feature-profile web-filtering juniper-local profile \""+profile+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -297,8 +296,7 @@ func checkUtmProfileWebFLocalExists(profile string, m interface{}, jnprSess *Net return true, nil } -func setUtmProfileWebFLocal(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setUtmProfileWebFLocal(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set security utm feature-profile web-filtering juniper-local " + @@ -336,16 +334,15 @@ func setUtmProfileWebFLocal(d *schema.ResourceData, m interface{}, jnprSess *Net configSet = append(configSet, setPrefix+"timeout "+strconv.Itoa(d.Get("timeout").(int))) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readUtmProfileWebFLocal(profile string, m interface{}, jnprSess *NetconfObject, +func readUtmProfileWebFLocal(profile string, clt *Client, junSess *junosSession, ) (utmProfileWebFilteringLocalOptions, error) { - sess := m.(*Session) var confRead utmProfileWebFilteringLocalOptions - showConfig, err := sess.command(cmdShowConfig+ - "security utm feature-profile web-filtering juniper-local profile \""+profile+"\""+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security utm feature-profile web-filtering juniper-local profile \""+profile+"\""+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -397,13 +394,12 @@ func readUtmProfileWebFLocal(profile string, m interface{}, jnprSess *NetconfObj return confRead, nil } -func delUtmProfileWebFLocal(profile string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delUtmProfileWebFLocal(profile string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete security utm feature-profile web-filtering juniper-local "+ "profile \""+profile+"\"") - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillUtmProfileWebFLocalData( diff --git a/junos/resource_security_utm_profile_web_filtering_websense_redirect.go b/junos/resource_security_utm_profile_web_filtering_websense_redirect.go index 165ef884..22384d04 100644 --- a/junos/resource_security_utm_profile_web_filtering_websense_redirect.go +++ b/junos/resource_security_utm_profile_web_filtering_websense_redirect.go @@ -107,54 +107,54 @@ func resourceSecurityUtmProfileWebFilteringWebsense() *schema.Resource { func resourceSecurityUtmProfileWebFilteringWebsenseCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setUtmProfileWebFWebsense(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setUtmProfileWebFWebsense(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security utm feature-profile web-filtering websense-redirect "+ - "not compatible with Junos device %s", jnprSess.SystemInformation.HardwareModel)) + "not compatible with Junos device %s", junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - utmProfileWebFWebsenseExists, err := checkUtmProfileWebFWebsenseExists(d.Get("name").(string), m, jnprSess) + utmProfileWebFWebsenseExists, err := checkUtmProfileWebFWebsenseExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if utmProfileWebFWebsenseExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security utm feature-profile web-filtering websense-redirect "+ "%v already exists", d.Get("name").(string)))...) } - if err := setUtmProfileWebFWebsense(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setUtmProfileWebFWebsense(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_utm_profile_web_filtering_websense_redirect", jnprSess) + warns, err := clt.commitConf("create resource junos_security_utm_profile_web_filtering_websense_redirect", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - utmProfileWebFWebsenseExists, err = checkUtmProfileWebFWebsenseExists(d.Get("name").(string), m, jnprSess) + utmProfileWebFWebsenseExists, err = checkUtmProfileWebFWebsenseExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -165,26 +165,26 @@ func resourceSecurityUtmProfileWebFilteringWebsenseCreate(ctx context.Context, d "not exists after commit => check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceSecurityUtmProfileWebFilteringWebsenseReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityUtmProfileWebFilteringWebsenseReadWJunSess(d, clt, junSess)...) } func resourceSecurityUtmProfileWebFilteringWebsenseRead(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSecurityUtmProfileWebFilteringWebsenseReadWJnprSess(d, m, jnprSess) + return resourceSecurityUtmProfileWebFilteringWebsenseReadWJunSess(d, clt, junSess) } -func resourceSecurityUtmProfileWebFilteringWebsenseReadWJnprSess( - d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSecurityUtmProfileWebFilteringWebsenseReadWJunSess( + d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - utmProfileWebFWebsenseOptions, err := readUtmProfileWebFWebsense(d.Get("name").(string), m, jnprSess) + utmProfileWebFWebsenseOptions, err := readUtmProfileWebFWebsense(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -201,77 +201,77 @@ func resourceSecurityUtmProfileWebFilteringWebsenseReadWJnprSess( func resourceSecurityUtmProfileWebFilteringWebsenseUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delUtmProfileWebFWebsense(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delUtmProfileWebFWebsense(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setUtmProfileWebFWebsense(d, m, nil); err != nil { + if err := setUtmProfileWebFWebsense(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delUtmProfileWebFWebsense(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delUtmProfileWebFWebsense(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setUtmProfileWebFWebsense(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setUtmProfileWebFWebsense(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_utm_profile_web_filtering_websense_redirect", jnprSess) + warns, err := clt.commitConf("update resource junos_security_utm_profile_web_filtering_websense_redirect", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSecurityUtmProfileWebFilteringWebsenseReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityUtmProfileWebFilteringWebsenseReadWJunSess(d, clt, junSess)...) } func resourceSecurityUtmProfileWebFilteringWebsenseDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delUtmProfileWebFWebsense(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delUtmProfileWebFWebsense(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delUtmProfileWebFWebsense(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delUtmProfileWebFWebsense(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_utm_profile_web_filtering_websense_redirect", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_utm_profile_web_filtering_websense_redirect", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -281,14 +281,14 @@ func resourceSecurityUtmProfileWebFilteringWebsenseDelete(ctx context.Context, d func resourceSecurityUtmProfileWebFilteringWebsenseImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - utmProfileWebFWebsenseExists, err := checkUtmProfileWebFWebsenseExists(d.Id(), m, jnprSess) + utmProfileWebFWebsenseExists, err := checkUtmProfileWebFWebsenseExists(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -296,7 +296,7 @@ func resourceSecurityUtmProfileWebFilteringWebsenseImport(ctx context.Context, d return nil, fmt.Errorf("don't find security utm feature-profile web-filtering websense-redirect with id "+ "'%v' (id must be )", d.Id()) } - utmProfileWebFWebsenseOptions, err := readUtmProfileWebFWebsense(d.Id(), m, jnprSess) + utmProfileWebFWebsenseOptions, err := readUtmProfileWebFWebsense(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -307,10 +307,9 @@ func resourceSecurityUtmProfileWebFilteringWebsenseImport(ctx context.Context, d return result, nil } -func checkUtmProfileWebFWebsenseExists(profile string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "security utm feature-profile web-filtering websense-redirect profile \""+profile+"\""+pipeDisplaySet, jnprSess) +func checkUtmProfileWebFWebsenseExists(profile string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "security utm feature-profile web-filtering websense-redirect profile \""+profile+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -321,8 +320,7 @@ func checkUtmProfileWebFWebsenseExists(profile string, m interface{}, jnprSess * return true, nil } -func setUtmProfileWebFWebsense(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setUtmProfileWebFWebsense(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set security utm feature-profile web-filtering websense-redirect " + @@ -377,17 +375,16 @@ func setUtmProfileWebFWebsense(d *schema.ResourceData, m interface{}, jnprSess * configSet = append(configSet, setPrefix+"timeout "+strconv.Itoa(d.Get("timeout").(int))) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readUtmProfileWebFWebsense(profile string, m interface{}, jnprSess *NetconfObject, +func readUtmProfileWebFWebsense(profile string, clt *Client, junSess *junosSession, ) (utmProfileWebFilteringWebsenseOptions, error) { - sess := m.(*Session) var confRead utmProfileWebFilteringWebsenseOptions - showConfig, err := sess.command(cmdShowConfig+ + showConfig, err := clt.command(cmdShowConfig+ "security utm feature-profile web-filtering websense-redirect"+ - " profile \""+profile+"\""+pipeDisplaySetRelative, jnprSess) + " profile \""+profile+"\""+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -462,13 +459,12 @@ func readUtmProfileWebFWebsense(profile string, m interface{}, jnprSess *Netconf return confRead, nil } -func delUtmProfileWebFWebsense(profile string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delUtmProfileWebFWebsense(profile string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete security utm feature-profile web-filtering websense-redirect "+ "profile \""+profile+"\"") - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillUtmProfileWebFWebsenseData( diff --git a/junos/resource_security_zone.go b/junos/resource_security_zone.go index d39bbec6..51a346cf 100644 --- a/junos/resource_security_zone.go +++ b/junos/resource_security_zone.go @@ -241,53 +241,53 @@ func resourceSecurityZone() *schema.Resource { } func resourceSecurityZoneCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSecurityZone(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSecurityZone(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security zone not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel)) + junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - securityZoneExists, err := checkSecurityZonesExists(d.Get("name").(string), m, jnprSess) + securityZoneExists, err := checkSecurityZonesExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if securityZoneExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security zone %v already exists", d.Get("name").(string)))...) } - if err := setSecurityZone(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityZone(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_zone", jnprSess) + warns, err := clt.commitConf("create resource junos_security_zone", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - securityZoneExists, err = checkSecurityZonesExists(d.Get("name").(string), m, jnprSess) + securityZoneExists, err = checkSecurityZonesExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -298,24 +298,24 @@ func resourceSecurityZoneCreate(ctx context.Context, d *schema.ResourceData, m i "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceSecurityZoneReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityZoneReadWJunSess(d, clt, junSess)...) } func resourceSecurityZoneRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSecurityZoneReadWJnprSess(d, m, jnprSess) + return resourceSecurityZoneReadWJunSess(d, clt, junSess) } -func resourceSecurityZoneReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSecurityZoneReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - zoneOptions, err := readSecurityZone(d.Get("name").(string), m, jnprSess) + zoneOptions, err := readSecurityZone(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -353,77 +353,83 @@ func resourceSecurityZoneUpdate(ctx context.Context, d *schema.ResourceData, m i }) } } - sess := m.(*Session) - if sess.junosFakeUpdateAlso { + clt := m.(*Client) + if clt.fakeUpdateAlso { if err := delSecurityZoneOpts( - d.Get("name").(string), addressBookConfiguredSingly, m, nil); err != nil { + d.Get("name").(string), + addressBookConfiguredSingly, + clt, nil, + ); err != nil { return append(diagWarns, diag.FromErr(err)...) } - if err := setSecurityZone(d, m, nil); err != nil { + if err := setSecurityZone(d, clt, nil); err != nil { return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) return diagWarns } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } if err := delSecurityZoneOpts( - d.Get("name").(string), addressBookConfiguredSingly, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + d.Get("name").(string), + addressBookConfiguredSingly, + clt, junSess, + ); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSecurityZone(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityZone(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_zone", jnprSess) + warns, err := clt.commitConf("update resource junos_security_zone", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSecurityZoneReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityZoneReadWJunSess(d, clt, junSess)...) } func resourceSecurityZoneDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSecurityZone(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSecurityZone(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityZone(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityZone(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_zone", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_zone", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -433,21 +439,21 @@ func resourceSecurityZoneDelete(ctx context.Context, d *schema.ResourceData, m i func resourceSecurityZoneImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - securityZoneExists, err := checkSecurityZonesExists(d.Id(), m, jnprSess) + securityZoneExists, err := checkSecurityZonesExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !securityZoneExists { return nil, fmt.Errorf("don't find zone with id '%v' (id must be )", d.Id()) } - zoneOptions, err := readSecurityZone(d.Id(), m, jnprSess) + zoneOptions, err := readSecurityZone(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -458,9 +464,8 @@ func resourceSecurityZoneImport(ctx context.Context, d *schema.ResourceData, m i return result, nil } -func checkSecurityZonesExists(zone string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"security zones security-zone "+zone+pipeDisplaySet, jnprSess) +func checkSecurityZonesExists(zone string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"security zones security-zone "+zone+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -471,8 +476,7 @@ func checkSecurityZonesExists(zone string, m interface{}, jnprSess *NetconfObjec return true, nil } -func setSecurityZone(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSecurityZone(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set security zones security-zone " + d.Get("name").(string) @@ -593,15 +597,14 @@ func setSecurityZone(d *schema.ResourceData, m interface{}, jnprSess *NetconfObj configSet = append(configSet, setPrefix+" tcp-rst") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSecurityZone(zone string, m interface{}, jnprSess *NetconfObject) (zoneOptions, error) { - sess := m.(*Session) +func readSecurityZone(zone string, clt *Client, junSess *junosSession) (zoneOptions, error) { var confRead zoneOptions - showConfig, err := sess.command(cmdShowConfig+ - "security zones security-zone "+zone+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security zones security-zone "+zone+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -730,8 +733,7 @@ func readSecurityZone(zone string, m interface{}, jnprSess *NetconfObject) (zone return confRead, nil } -func delSecurityZoneOpts(zone string, addressBookSingly bool, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSecurityZoneOpts(zone string, addressBookSingly bool, clt *Client, junSess *junosSession) error { listLinesToDelete := []string{ "advance-policy-based-routing-profile", "description", @@ -751,15 +753,14 @@ func delSecurityZoneOpts(zone string, addressBookSingly bool, m interface{}, jnp configSet = append(configSet, delPrefix+line) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func delSecurityZone(zone string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSecurityZone(zone string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete security zones security-zone "+zone) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSecurityZoneData(d *schema.ResourceData, zoneOptions zoneOptions) { diff --git a/junos/resource_security_zone_book_address.go b/junos/resource_security_zone_book_address.go index be4b0da0..ebc3302e 100644 --- a/junos/resource_security_zone_book_address.go +++ b/junos/resource_security_zone_book_address.go @@ -97,69 +97,73 @@ func resourceSecurityZoneBookAddress() *schema.Resource { func resourceSecurityZoneBookAddressCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSecurityZoneBookAddress(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSecurityZoneBookAddress(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("zone").(string) + idSeparator + d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security zone address-book address not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel)) + junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - zonesExists, err := checkSecurityZonesExists(d.Get("zone").(string), m, jnprSess) + zonesExists, err := checkSecurityZonesExists(d.Get("zone").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !zonesExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security zone %v doesn't exist", d.Get("zone").(string)))...) } securityZoneBookAddressExists, err := checkSecurityZoneBookAddresssExists( - d.Get("zone").(string), d.Get("name").(string), m, jnprSess) + d.Get("zone").(string), + d.Get("name").(string), + clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if securityZoneBookAddressExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf( "security zone address-book address %v already exists in zone %s", d.Get("name").(string), d.Get("zone").(string)))...) } - if err := setSecurityZoneBookAddress(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityZoneBookAddress(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_zone_book_address", jnprSess) + warns, err := clt.commitConf("create resource junos_security_zone_book_address", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } securityZoneBookAddressExists, err = checkSecurityZoneBookAddresssExists( - d.Get("zone").(string), d.Get("name").(string), m, jnprSess) + d.Get("zone").(string), + d.Get("name").(string), + clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -171,24 +175,27 @@ func resourceSecurityZoneBookAddressCreate(ctx context.Context, d *schema.Resour "=> check your config", d.Get("name").(string), d.Get("zone").(string)))...) } - return append(diagWarns, resourceSecurityZoneBookAddressReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityZoneBookAddressReadWJunSess(d, clt, junSess)...) } func resourceSecurityZoneBookAddressRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSecurityZoneBookAddressReadWJnprSess(d, m, jnprSess) + return resourceSecurityZoneBookAddressReadWJunSess(d, clt, junSess) } -func resourceSecurityZoneBookAddressReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSecurityZoneBookAddressReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - zoneBookAddressOptions, err := readSecurityZoneBookAddress(d.Get("zone").(string), d.Get("name").(string), m, jnprSess) + zoneBookAddressOptions, err := readSecurityZoneBookAddress( + d.Get("zone").(string), + d.Get("name").(string), + clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -205,77 +212,77 @@ func resourceSecurityZoneBookAddressReadWJnprSess(d *schema.ResourceData, m inte func resourceSecurityZoneBookAddressUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSecurityZoneBookAddress(d.Get("zone").(string), d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSecurityZoneBookAddress(d.Get("zone").(string), d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSecurityZoneBookAddress(d, m, nil); err != nil { + if err := setSecurityZoneBookAddress(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityZoneBookAddress(d.Get("zone").(string), d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityZoneBookAddress(d.Get("zone").(string), d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSecurityZoneBookAddress(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityZoneBookAddress(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_zone_book_address", jnprSess) + warns, err := clt.commitConf("update resource junos_security_zone_book_address", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSecurityZoneBookAddressReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityZoneBookAddressReadWJunSess(d, clt, junSess)...) } func resourceSecurityZoneBookAddressDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSecurityZoneBookAddress(d.Get("zone").(string), d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSecurityZoneBookAddress(d.Get("zone").(string), d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityZoneBookAddress(d.Get("zone").(string), d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityZoneBookAddress(d.Get("zone").(string), d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_zone_book_address", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_zone_book_address", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -285,18 +292,18 @@ func resourceSecurityZoneBookAddressDelete(ctx context.Context, d *schema.Resour func resourceSecurityZoneBookAddressImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) idList := strings.Split(d.Id(), idSeparator) if len(idList) < 2 { return nil, fmt.Errorf("missing element(s) in id with separator %v", idSeparator) } - securityZoneBookAddressExists, err := checkSecurityZoneBookAddresssExists(idList[0], idList[1], m, jnprSess) + securityZoneBookAddressExists, err := checkSecurityZoneBookAddresssExists(idList[0], idList[1], clt, junSess) if err != nil { return nil, err } @@ -304,7 +311,7 @@ func resourceSecurityZoneBookAddressImport(ctx context.Context, d *schema.Resour return nil, fmt.Errorf( "don't find zone address-book address with id '%v' (id must be "+idSeparator+")", d.Id()) } - zoneBookAddressOptions, err := readSecurityZoneBookAddress(idList[0], idList[1], m, jnprSess) + zoneBookAddressOptions, err := readSecurityZoneBookAddress(idList[0], idList[1], clt, junSess) if err != nil { return nil, err } @@ -315,10 +322,9 @@ func resourceSecurityZoneBookAddressImport(ctx context.Context, d *schema.Resour return result, nil } -func checkSecurityZoneBookAddresssExists(zone, address string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "security zones security-zone "+zone+" address-book address "+address+pipeDisplaySet, jnprSess) +func checkSecurityZoneBookAddresssExists(zone, address string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "security zones security-zone "+zone+" address-book address "+address+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -329,8 +335,7 @@ func checkSecurityZoneBookAddresssExists(zone, address string, m interface{}, jn return true, nil } -func setSecurityZoneBookAddress(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSecurityZoneBookAddress(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set security zones security-zone " + @@ -358,16 +363,15 @@ func setSecurityZoneBookAddress(d *schema.ResourceData, m interface{}, jnprSess configSet = append(configSet, setPrefix+"wildcard-address "+v) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSecurityZoneBookAddress(zone, address string, m interface{}, jnprSess *NetconfObject, +func readSecurityZoneBookAddress(zone, address string, clt *Client, junSess *junosSession, ) (zoneBookAddressOptions, error) { - sess := m.(*Session) var confRead zoneBookAddressOptions - showConfig, err := sess.command(cmdShowConfig+ - "security zones security-zone "+zone+" address-book address "+address+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security zones security-zone "+zone+" address-book address "+address+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -411,11 +415,10 @@ func readSecurityZoneBookAddress(zone, address string, m interface{}, jnprSess * return confRead, nil } -func delSecurityZoneBookAddress(zone, address string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSecurityZoneBookAddress(zone, address string, clt *Client, junSess *junosSession) error { configSet := []string{"delete security zones security-zone " + zone + " address-book address " + address} - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSecurityZoneBookAddressData(d *schema.ResourceData, zoneBookAddressOptions zoneBookAddressOptions) { diff --git a/junos/resource_security_zone_book_address_set.go b/junos/resource_security_zone_book_address_set.go index 031a78a9..59a0dbd8 100644 --- a/junos/resource_security_zone_book_address_set.go +++ b/junos/resource_security_zone_book_address_set.go @@ -67,69 +67,73 @@ func resourceSecurityZoneBookAddressSet() *schema.Resource { func resourceSecurityZoneBookAddressSetCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSecurityZoneBookAddressSet(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSecurityZoneBookAddressSet(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("zone").(string) + idSeparator + d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if !checkCompatibilitySecurity(jnprSess) { + defer clt.closeSession(junSess) + if !checkCompatibilitySecurity(junSess) { return diag.FromErr(fmt.Errorf("security zone address-book address-set not compatible with Junos device %s", - jnprSess.SystemInformation.HardwareModel)) + junSess.SystemInformation.HardwareModel)) } - if err := sess.configLock(ctx, jnprSess); err != nil { + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - zonesExists, err := checkSecurityZonesExists(d.Get("zone").(string), m, jnprSess) + zonesExists, err := checkSecurityZonesExists(d.Get("zone").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !zonesExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("security zone %v doesn't exist", d.Get("zone").(string)))...) } securityZoneBookAddressSetExists, err := checkSecurityZoneBookAddressSetsExists( - d.Get("zone").(string), d.Get("name").(string), m, jnprSess) + d.Get("zone").(string), + d.Get("name").(string), + clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if securityZoneBookAddressSetExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf( "security zone address-book address-set %v already exists in zone %s", d.Get("name").(string), d.Get("zone").(string)))...) } - if err := setSecurityZoneBookAddressSet(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityZoneBookAddressSet(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_security_zone_book_address_set", jnprSess) + warns, err := clt.commitConf("create resource junos_security_zone_book_address_set", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } securityZoneBookAddressSetExists, err = checkSecurityZoneBookAddressSetsExists( - d.Get("zone").(string), d.Get("name").(string), m, jnprSess) + d.Get("zone").(string), + d.Get("name").(string), + clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -141,26 +145,28 @@ func resourceSecurityZoneBookAddressSetCreate(ctx context.Context, d *schema.Res "=> check your config", d.Get("name").(string), d.Get("zone").(string)))...) } - return append(diagWarns, resourceSecurityZoneBookAddressSetReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityZoneBookAddressSetReadWJunSess(d, clt, junSess)...) } func resourceSecurityZoneBookAddressSetRead(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSecurityZoneBookAddressSetReadWJnprSess(d, m, jnprSess) + return resourceSecurityZoneBookAddressSetReadWJunSess(d, clt, junSess) } -func resourceSecurityZoneBookAddressSetReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSecurityZoneBookAddressSetReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() zoneBookAddressSetOptions, err := readSecurityZoneBookAddressSet( - d.Get("zone").(string), d.Get("name").(string), m, jnprSess) + d.Get("zone").(string), + d.Get("name").(string), + clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -177,77 +183,77 @@ func resourceSecurityZoneBookAddressSetReadWJnprSess(d *schema.ResourceData, m i func resourceSecurityZoneBookAddressSetUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSecurityZoneBookAddressSet(d.Get("zone").(string), d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSecurityZoneBookAddressSet(d.Get("zone").(string), d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSecurityZoneBookAddressSet(d, m, nil); err != nil { + if err := setSecurityZoneBookAddressSet(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityZoneBookAddressSet(d.Get("zone").(string), d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityZoneBookAddressSet(d.Get("zone").(string), d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSecurityZoneBookAddressSet(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSecurityZoneBookAddressSet(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_security_zone_book_address_set", jnprSess) + warns, err := clt.commitConf("update resource junos_security_zone_book_address_set", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSecurityZoneBookAddressSetReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSecurityZoneBookAddressSetReadWJunSess(d, clt, junSess)...) } func resourceSecurityZoneBookAddressSetDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSecurityZoneBookAddressSet(d.Get("zone").(string), d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSecurityZoneBookAddressSet(d.Get("zone").(string), d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSecurityZoneBookAddressSet(d.Get("zone").(string), d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSecurityZoneBookAddressSet(d.Get("zone").(string), d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_security_zone_book_address_set", jnprSess) + warns, err := clt.commitConf("delete resource junos_security_zone_book_address_set", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -257,18 +263,18 @@ func resourceSecurityZoneBookAddressSetDelete(ctx context.Context, d *schema.Res func resourceSecurityZoneBookAddressSetImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) idList := strings.Split(d.Id(), idSeparator) if len(idList) < 2 { return nil, fmt.Errorf("missing element(s) in id with separator %v", idSeparator) } - securityZoneBookAddressSetExists, err := checkSecurityZoneBookAddressSetsExists(idList[0], idList[1], m, jnprSess) + securityZoneBookAddressSetExists, err := checkSecurityZoneBookAddressSetsExists(idList[0], idList[1], clt, junSess) if err != nil { return nil, err } @@ -276,7 +282,7 @@ func resourceSecurityZoneBookAddressSetImport(ctx context.Context, d *schema.Res return nil, fmt.Errorf( "don't find zone address-book address-set with id '%v' (id must be "+idSeparator+")", d.Id()) } - zoneBookAddressSetOptions, err := readSecurityZoneBookAddressSet(idList[0], idList[1], m, jnprSess) + zoneBookAddressSetOptions, err := readSecurityZoneBookAddressSet(idList[0], idList[1], clt, junSess) if err != nil { return nil, err } @@ -287,11 +293,10 @@ func resourceSecurityZoneBookAddressSetImport(ctx context.Context, d *schema.Res return result, nil } -func checkSecurityZoneBookAddressSetsExists(zone, addressSet string, m interface{}, jnprSess *NetconfObject, +func checkSecurityZoneBookAddressSetsExists(zone, addressSet string, clt *Client, junSess *junosSession, ) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "security zones security-zone "+zone+" address-book address-set "+addressSet+pipeDisplaySet, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security zones security-zone "+zone+" address-book address-set "+addressSet+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -302,8 +307,7 @@ func checkSecurityZoneBookAddressSetsExists(zone, addressSet string, m interface return true, nil } -func setSecurityZoneBookAddressSet(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSecurityZoneBookAddressSet(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set security zones security-zone " + @@ -322,16 +326,15 @@ func setSecurityZoneBookAddressSet(d *schema.ResourceData, m interface{}, jnprSe configSet = append(configSet, setPrefix+"description \""+v+"\"") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSecurityZoneBookAddressSet(zone, addressSet string, m interface{}, jnprSess *NetconfObject, +func readSecurityZoneBookAddressSet(zone, addressSet string, clt *Client, junSess *junosSession, ) (zoneBookAddressSetOptions, error) { - sess := m.(*Session) var confRead zoneBookAddressSetOptions - showConfig, err := sess.command(cmdShowConfig+ - "security zones security-zone "+zone+" address-book address-set "+addressSet+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "security zones security-zone "+zone+" address-book address-set "+addressSet+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -360,11 +363,10 @@ func readSecurityZoneBookAddressSet(zone, addressSet string, m interface{}, jnpr return confRead, nil } -func delSecurityZoneBookAddressSet(zone, addressSet string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSecurityZoneBookAddressSet(zone, addressSet string, clt *Client, junSess *junosSession) error { configSet := []string{"delete security zones security-zone " + zone + " address-book address-set " + addressSet} - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSecurityZoneBookAddressSetData(d *schema.ResourceData, zoneBookAddressSetOptions zoneBookAddressSetOptions) { diff --git a/junos/resource_services.go b/junos/resource_services.go index ccf58a6d..d7e2d3c2 100644 --- a/junos/resource_services.go +++ b/junos/resource_services.go @@ -615,55 +615,55 @@ func resourceServices() *schema.Resource { } func resourceServicesCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setServices(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setServices(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId("services") return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := setServices(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setServices(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_services", jnprSess) + warns, err := clt.commitConf("create resource junos_services", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.SetId("services") - return append(diagWarns, resourceServicesReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceServicesReadWJunSess(d, clt, junSess)...) } func resourceServicesRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceServicesReadWJnprSess(d, m, jnprSess) + return resourceServicesReadWJunSess(d, clt, junSess) } -func resourceServicesReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) diag.Diagnostics { +func resourceServicesReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession) diag.Diagnostics { mutex.Lock() - servicesOptions, err := readServices(m, jnprSess) + servicesOptions, err := readServices(clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -675,77 +675,77 @@ func resourceServicesReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSe func resourceServicesUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delServices(d, m, nil, false); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delServices(d, false, clt, nil); err != nil { return diag.FromErr(err) } - if err := setServices(d, m, nil); err != nil { + if err := setServices(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delServices(d, m, jnprSess, false); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delServices(d, false, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setServices(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setServices(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_services", jnprSess) + warns, err := clt.commitConf("update resource junos_services", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceServicesReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceServicesReadWJunSess(d, clt, junSess)...) } func resourceServicesDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { if d.Get("clean_on_destroy").(bool) { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delServices(d, m, nil, true); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delServices(d, true, clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delServices(d, m, jnprSess, true); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delServices(d, true, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_services", jnprSess) + warns, err := clt.commitConf("delete resource junos_services", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -756,14 +756,14 @@ func resourceServicesDelete(ctx context.Context, d *schema.ResourceData, m inter func resourceServicesImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - servicesOptions, err := readServices(m, jnprSess) + servicesOptions, err := readServices(clt, junSess) if err != nil { return nil, err } @@ -774,9 +774,7 @@ func resourceServicesImport(ctx context.Context, d *schema.ResourceData, m inter return result, nil } -func setServices(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) - +func setServices(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) for _, v := range d.Get("advanced_anti_malware").([]interface{}) { configSetAdvAntiMalware, err := setServicesAdvancedAntiMalware(d, v) @@ -807,7 +805,7 @@ func setServices(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) configSet = append(configSet, configSetUserIdent...) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func setServicesAdvancedAntiMalware(d *schema.ResourceData, advAntiMalware interface{}) ([]string, error) { @@ -1236,13 +1234,13 @@ func listLinesServicesUserIdentificationAdAccess() []string { } } -func delServices(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, cleanAll bool) error { +func delServices(d *schema.ResourceData, cleanAll bool, clt *Client, junSess *junosSession) error { listLinesToDelete := make([]string, 0) listLinesToDelete = append(listLinesToDelete, listLinesServicesAdvancedAntiMalware()...) listLinesToDelete = append(listLinesToDelete, listLinesServicesApplicationIdentification()...) listLinesToDelete = append(listLinesToDelete, listLinesServicesSecurityIntel()...) listLinesToDelete = append(listLinesToDelete, listLinesServicesUserIdentification()...) - sess := m.(*Session) + configSet := make([]string, 0) delPrefix := "delete services " for _, line := range listLinesToDelete { @@ -1268,14 +1266,13 @@ func delServices(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, configSet = append(configSet, delPrefix+"user-identification active-directory-access") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readServices(m interface{}, jnprSess *NetconfObject) (servicesOptions, error) { - sess := m.(*Session) +func readServices(clt *Client, junSess *junosSession) (servicesOptions, error) { var confRead servicesOptions - showConfig, err := sess.command(cmdShowConfig+"services"+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"services"+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } diff --git a/junos/resource_services_advanced_anti_malware_policy.go b/junos/resource_services_advanced_anti_malware_policy.go index 72218185..a550eb95 100644 --- a/junos/resource_services_advanced_anti_malware_policy.go +++ b/junos/resource_services_advanced_anti_malware_policy.go @@ -138,53 +138,55 @@ func resourceServicesAdvancedAntiMalwarePolicy() *schema.Resource { func resourceServicesAdvancedAntiMalwarePolicyCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setServicesAdvancedAntiMalwarePolicy(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setServicesAdvancedAntiMalwarePolicy(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics svcAdvancedAntiMalwarePolicyExists, err := checkServicesAdvancedAntiMalwarePolicyExists( - d.Get("name").(string), m, jnprSess) + d.Get("name").(string), + clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if svcAdvancedAntiMalwarePolicyExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf( "services advanced-anti-malware policy %v already exists", d.Get("name").(string)))...) } - if err := setServicesAdvancedAntiMalwarePolicy(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setServicesAdvancedAntiMalwarePolicy(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_services_advanced_anti_malware_policy", jnprSess) + warns, err := clt.commitConf("create resource junos_services_advanced_anti_malware_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } svcAdvancedAntiMalwarePolicyExists, err = checkServicesAdvancedAntiMalwarePolicyExists( - d.Get("name").(string), m, jnprSess) + d.Get("name").(string), + clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -196,26 +198,28 @@ func resourceServicesAdvancedAntiMalwarePolicyCreate(ctx context.Context, d *sch "not exists after commit => check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceServicesAdvancedAntiMalwarePolicyReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceServicesAdvancedAntiMalwarePolicyReadWJunSess(d, clt, junSess)...) } func resourceServicesAdvancedAntiMalwarePolicyRead(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceServicesAdvancedAntiMalwarePolicyReadWJnprSess(d, m, jnprSess) + return resourceServicesAdvancedAntiMalwarePolicyReadWJunSess(d, clt, junSess) } -func resourceServicesAdvancedAntiMalwarePolicyReadWJnprSess( - d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceServicesAdvancedAntiMalwarePolicyReadWJunSess( + d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - svcAdvancedAntiMalwarePolicyOptions, err := readServicesAdvancedAntiMalwarePolicy(d.Get("name").(string), m, jnprSess) + svcAdvancedAntiMalwarePolicyOptions, err := readServicesAdvancedAntiMalwarePolicy( + d.Get("name").(string), + clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -232,77 +236,77 @@ func resourceServicesAdvancedAntiMalwarePolicyReadWJnprSess( func resourceServicesAdvancedAntiMalwarePolicyUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delServicesAdvancedAntiMalwarePolicy(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delServicesAdvancedAntiMalwarePolicy(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setServicesAdvancedAntiMalwarePolicy(d, m, nil); err != nil { + if err := setServicesAdvancedAntiMalwarePolicy(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delServicesAdvancedAntiMalwarePolicy(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delServicesAdvancedAntiMalwarePolicy(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setServicesAdvancedAntiMalwarePolicy(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setServicesAdvancedAntiMalwarePolicy(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_services_advanced_anti_malware_policy", jnprSess) + warns, err := clt.commitConf("update resource junos_services_advanced_anti_malware_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceServicesAdvancedAntiMalwarePolicyReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceServicesAdvancedAntiMalwarePolicyReadWJunSess(d, clt, junSess)...) } func resourceServicesAdvancedAntiMalwarePolicyDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delServicesAdvancedAntiMalwarePolicy(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delServicesAdvancedAntiMalwarePolicy(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delServicesAdvancedAntiMalwarePolicy(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delServicesAdvancedAntiMalwarePolicy(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_services_advanced_anti_malware_policy", jnprSess) + warns, err := clt.commitConf("delete resource junos_services_advanced_anti_malware_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -312,21 +316,21 @@ func resourceServicesAdvancedAntiMalwarePolicyDelete(ctx context.Context, d *sch func resourceServicesAdvancedAntiMalwarePolicyImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - svcAdvancedAntiMalwarePolicyExists, err := checkServicesAdvancedAntiMalwarePolicyExists(d.Id(), m, jnprSess) + svcAdvancedAntiMalwarePolicyExists, err := checkServicesAdvancedAntiMalwarePolicyExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !svcAdvancedAntiMalwarePolicyExists { return nil, fmt.Errorf("don't find services advanced-anti-malware policy with id '%v' (id must be )", d.Id()) } - svcAdvancedAntiMalwarePolicyOptions, err := readServicesAdvancedAntiMalwarePolicy(d.Id(), m, jnprSess) + svcAdvancedAntiMalwarePolicyOptions, err := readServicesAdvancedAntiMalwarePolicy(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -337,11 +341,10 @@ func resourceServicesAdvancedAntiMalwarePolicyImport(ctx context.Context, d *sch return result, nil } -func checkServicesAdvancedAntiMalwarePolicyExists(policy string, m interface{}, jnprSess *NetconfObject, +func checkServicesAdvancedAntiMalwarePolicyExists(policy string, clt *Client, junSess *junosSession, ) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "services advanced-anti-malware policy \""+policy+"\""+pipeDisplaySet, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "services advanced-anti-malware policy \""+policy+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -352,8 +355,7 @@ func checkServicesAdvancedAntiMalwarePolicyExists(policy string, m interface{}, return true, nil } -func setServicesAdvancedAntiMalwarePolicy(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setServicesAdvancedAntiMalwarePolicy(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set services advanced-anti-malware policy \"" + d.Get("name").(string) + "\" " @@ -409,16 +411,15 @@ func setServicesAdvancedAntiMalwarePolicy(d *schema.ResourceData, m interface{}, configSet = append(configSet, setPrefix+"whitelist-notification log") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readServicesAdvancedAntiMalwarePolicy(policy string, m interface{}, jnprSess *NetconfObject, +func readServicesAdvancedAntiMalwarePolicy(policy string, clt *Client, junSess *junosSession, ) (svcAdvancedAntiMalwarePolicyOptions, error) { - sess := m.(*Session) var confRead svcAdvancedAntiMalwarePolicyOptions - showConfig, err := sess.command(cmdShowConfig+ - "services advanced-anti-malware policy \""+policy+"\""+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "services advanced-anti-malware policy \""+policy+"\""+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -475,13 +476,12 @@ func readServicesAdvancedAntiMalwarePolicy(policy string, m interface{}, jnprSes return confRead, nil } -func delServicesAdvancedAntiMalwarePolicy(policy string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delServicesAdvancedAntiMalwarePolicy(policy string, clt *Client, junSess *junosSession) error { configSet := []string{ "delete services advanced-anti-malware policy \"" + policy + "\"", } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillServicesAdvancedAntiMalwarePolicyData( diff --git a/junos/resource_services_flowmonitoring_vipfix_template.go b/junos/resource_services_flowmonitoring_vipfix_template.go index 192d81c9..fd36e76f 100644 --- a/junos/resource_services_flowmonitoring_vipfix_template.go +++ b/junos/resource_services_flowmonitoring_vipfix_template.go @@ -132,53 +132,55 @@ func resourceServicesFlowMonitoringVIPFixTemplate() *schema.Resource { func resourceServicesFlowMonitoringVIPFixTemplateCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setServicesFlowMonitoringVIPFixTemplate(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setServicesFlowMonitoringVIPFixTemplate(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics flowMonitoringVIPFixTemplateExists, err := checkServicesFlowMonitoringVIPFixTemplateExists( - d.Get("name").(string), m, jnprSess) + d.Get("name").(string), + clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if flowMonitoringVIPFixTemplateExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("services flow-monitoring version-ipfix template "+ " %v already exists", d.Get("name").(string)))...) } - if err := setServicesFlowMonitoringVIPFixTemplate(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setServicesFlowMonitoringVIPFixTemplate(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_services_flowmonitoring_vipfix_template", jnprSess) + warns, err := clt.commitConf("create resource junos_services_flowmonitoring_vipfix_template", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } flowMonitoringVIPFixTemplateExists, err = checkServicesFlowMonitoringVIPFixTemplateExists( - d.Get("name").(string), m, jnprSess) + d.Get("name").(string), + clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -189,27 +191,28 @@ func resourceServicesFlowMonitoringVIPFixTemplateCreate(ctx context.Context, d * "not exists after commit => check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceServicesFlowMonitoringVIPFixTemplateReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceServicesFlowMonitoringVIPFixTemplateReadWJunSess(d, clt, junSess)...) } func resourceServicesFlowMonitoringVIPFixTemplateRead(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceServicesFlowMonitoringVIPFixTemplateReadWJnprSess(d, m, jnprSess) + return resourceServicesFlowMonitoringVIPFixTemplateReadWJunSess(d, clt, junSess) } -func resourceServicesFlowMonitoringVIPFixTemplateReadWJnprSess( - d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceServicesFlowMonitoringVIPFixTemplateReadWJunSess( + d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() flowMonitoringVIPFixTemplateOptions, err := readServicesFlowMonitoringVIPFixTemplate( - d.Get("name").(string), m, jnprSess) + d.Get("name").(string), + clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -226,77 +229,77 @@ func resourceServicesFlowMonitoringVIPFixTemplateReadWJnprSess( func resourceServicesFlowMonitoringVIPFixTemplateUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delServicesFlowMonitoringVIPFixTemplate(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delServicesFlowMonitoringVIPFixTemplate(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setServicesFlowMonitoringVIPFixTemplate(d, m, nil); err != nil { + if err := setServicesFlowMonitoringVIPFixTemplate(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delServicesFlowMonitoringVIPFixTemplate(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delServicesFlowMonitoringVIPFixTemplate(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setServicesFlowMonitoringVIPFixTemplate(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setServicesFlowMonitoringVIPFixTemplate(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_services_flowmonitoring_vipfix_template", jnprSess) + warns, err := clt.commitConf("update resource junos_services_flowmonitoring_vipfix_template", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceServicesFlowMonitoringVIPFixTemplateReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceServicesFlowMonitoringVIPFixTemplateReadWJunSess(d, clt, junSess)...) } func resourceServicesFlowMonitoringVIPFixTemplateDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delServicesFlowMonitoringVIPFixTemplate(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delServicesFlowMonitoringVIPFixTemplate(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delServicesFlowMonitoringVIPFixTemplate(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delServicesFlowMonitoringVIPFixTemplate(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_services_flowmonitoring_vipfix_template", jnprSess) + warns, err := clt.commitConf("delete resource junos_services_flowmonitoring_vipfix_template", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -306,14 +309,14 @@ func resourceServicesFlowMonitoringVIPFixTemplateDelete(ctx context.Context, d * func resourceServicesFlowMonitoringVIPFixTemplateImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - flowMonitoringVIPFixTemplateExists, err := checkServicesFlowMonitoringVIPFixTemplateExists(d.Id(), m, jnprSess) + flowMonitoringVIPFixTemplateExists, err := checkServicesFlowMonitoringVIPFixTemplateExists(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -321,7 +324,7 @@ func resourceServicesFlowMonitoringVIPFixTemplateImport(ctx context.Context, d * return nil, fmt.Errorf("don't find services flow-monitoring version-ipfix template with "+ "id '%v' (id must be )", d.Id()) } - flowMonitoringVIPFixTemplateOptions, err := readServicesFlowMonitoringVIPFixTemplate(d.Id(), m, jnprSess) + flowMonitoringVIPFixTemplateOptions, err := readServicesFlowMonitoringVIPFixTemplate(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -332,11 +335,10 @@ func resourceServicesFlowMonitoringVIPFixTemplateImport(ctx context.Context, d * return result, nil } -func checkServicesFlowMonitoringVIPFixTemplateExists(template string, m interface{}, jnprSess *NetconfObject, +func checkServicesFlowMonitoringVIPFixTemplateExists(template string, clt *Client, junSess *junosSession, ) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "services flow-monitoring version-ipfix template \""+template+"\""+pipeDisplaySet, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "services flow-monitoring version-ipfix template \""+template+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -347,8 +349,7 @@ func checkServicesFlowMonitoringVIPFixTemplateExists(template string, m interfac return true, nil } -func setServicesFlowMonitoringVIPFixTemplate(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setServicesFlowMonitoringVIPFixTemplate(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set services flow-monitoring version-ipfix template \"" + d.Get("name").(string) + "\" " @@ -405,18 +406,17 @@ func setServicesFlowMonitoringVIPFixTemplate(d *schema.ResourceData, m interface configSet = append(configSet, setPrefix+"tunnel-observation ipv6") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readServicesFlowMonitoringVIPFixTemplate(template string, m interface{}, jnprSess *NetconfObject, +func readServicesFlowMonitoringVIPFixTemplate(template string, clt *Client, junSess *junosSession, ) (flowMonitoringVIPFixTemplateOptions, error) { - sess := m.(*Session) var confRead flowMonitoringVIPFixTemplateOptions // setup default value confRead.observationDomainID = -1 - showConfig, err := sess.command(cmdShowConfig+ - "services flow-monitoring version-ipfix template \""+template+"\""+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "services flow-monitoring version-ipfix template \""+template+"\""+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -511,11 +511,10 @@ func readServicesFlowMonitoringVIPFixTemplate(template string, m interface{}, jn return confRead, nil } -func delServicesFlowMonitoringVIPFixTemplate(template string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delServicesFlowMonitoringVIPFixTemplate(template string, clt *Client, junSess *junosSession) error { configSet := []string{"delete services flow-monitoring version-ipfix template \"" + template + "\""} - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillServicesFlowMonitoringVIPFixTemplateData( diff --git a/junos/resource_services_proxy_profile.go b/junos/resource_services_proxy_profile.go index bc9f616f..70516ce7 100644 --- a/junos/resource_services_proxy_profile.go +++ b/junos/resource_services_proxy_profile.go @@ -47,50 +47,50 @@ func resourceServicesProxyProfile() *schema.Resource { func resourceServicesProxyProfileCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setServicesProxyProfile(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setServicesProxyProfile(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - proxyProfileExists, err := checkServicesProxyProfileExists(d.Get("name").(string), m, jnprSess) + proxyProfileExists, err := checkServicesProxyProfileExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if proxyProfileExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("services proxy profile %v already exists", d.Get("name").(string)))...) } - if err := setServicesProxyProfile(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setServicesProxyProfile(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_services_proxy_profile", jnprSess) + warns, err := clt.commitConf("create resource junos_services_proxy_profile", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - proxyProfileExists, err = checkServicesProxyProfileExists(d.Get("name").(string), m, jnprSess) + proxyProfileExists, err = checkServicesProxyProfileExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -101,25 +101,25 @@ func resourceServicesProxyProfileCreate(ctx context.Context, d *schema.ResourceD "not exists after commit => check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceServicesProxyProfileReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceServicesProxyProfileReadWJunSess(d, clt, junSess)...) } func resourceServicesProxyProfileRead(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceServicesProxyProfileReadWJnprSess(d, m, jnprSess) + return resourceServicesProxyProfileReadWJunSess(d, clt, junSess) } -func resourceServicesProxyProfileReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceServicesProxyProfileReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - proxyProfileOptions, err := readServicesProxyProfile(d.Get("name").(string), m, jnprSess) + proxyProfileOptions, err := readServicesProxyProfile(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -136,77 +136,77 @@ func resourceServicesProxyProfileReadWJnprSess(d *schema.ResourceData, m interfa func resourceServicesProxyProfileUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delServicesProxyProfile(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delServicesProxyProfile(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setServicesProxyProfile(d, m, nil); err != nil { + if err := setServicesProxyProfile(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delServicesProxyProfile(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delServicesProxyProfile(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setServicesProxyProfile(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setServicesProxyProfile(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_services_proxy_profile", jnprSess) + warns, err := clt.commitConf("update resource junos_services_proxy_profile", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceServicesProxyProfileReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceServicesProxyProfileReadWJunSess(d, clt, junSess)...) } func resourceServicesProxyProfileDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delServicesProxyProfile(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delServicesProxyProfile(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delServicesProxyProfile(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delServicesProxyProfile(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_services_proxy_profile", jnprSess) + warns, err := clt.commitConf("delete resource junos_services_proxy_profile", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -216,21 +216,21 @@ func resourceServicesProxyProfileDelete(ctx context.Context, d *schema.ResourceD func resourceServicesProxyProfileImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - proxyProfileExists, err := checkServicesProxyProfileExists(d.Id(), m, jnprSess) + proxyProfileExists, err := checkServicesProxyProfileExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !proxyProfileExists { return nil, fmt.Errorf("don't find services proxy profile with id '%v' (id must be )", d.Id()) } - proxyProfileOptions, err := readServicesProxyProfile(d.Id(), m, jnprSess) + proxyProfileOptions, err := readServicesProxyProfile(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -241,10 +241,9 @@ func resourceServicesProxyProfileImport(ctx context.Context, d *schema.ResourceD return result, nil } -func checkServicesProxyProfileExists(profile string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "services proxy profile \""+profile+"\""+pipeDisplaySet, jnprSess) +func checkServicesProxyProfileExists(profile string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "services proxy profile \""+profile+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -255,8 +254,7 @@ func checkServicesProxyProfileExists(profile string, m interface{}, jnprSess *Ne return true, nil } -func setServicesProxyProfile(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setServicesProxyProfile(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set services proxy profile \"" + d.Get("name").(string) + "\" " @@ -265,15 +263,14 @@ func setServicesProxyProfile(d *schema.ResourceData, m interface{}, jnprSess *Ne configSet = append(configSet, setPrefix+"protocol http port "+strconv.Itoa(d.Get("protocol_http_port").(int))) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readServicesProxyProfile(profile string, m interface{}, jnprSess *NetconfObject) (proxyProfileOptions, error) { - sess := m.(*Session) +func readServicesProxyProfile(profile string, clt *Client, junSess *junosSession) (proxyProfileOptions, error) { var confRead proxyProfileOptions - showConfig, err := sess.command(cmdShowConfig+ - "services proxy profile \""+profile+"\""+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "services proxy profile \""+profile+"\""+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -303,11 +300,10 @@ func readServicesProxyProfile(profile string, m interface{}, jnprSess *NetconfOb return confRead, nil } -func delServicesProxyProfile(profile string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delServicesProxyProfile(profile string, clt *Client, junSess *junosSession) error { configSet := []string{"delete services proxy profile \"" + profile + "\""} - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillServicesProxyProfileData(d *schema.ResourceData, proxyProfileOptions proxyProfileOptions, diff --git a/junos/resource_services_rpm_probe.go b/junos/resource_services_rpm_probe.go index 76c80350..9e34a960 100644 --- a/junos/resource_services_rpm_probe.go +++ b/junos/resource_services_rpm_probe.go @@ -320,50 +320,50 @@ func resourceServicesRpmProbe() *schema.Resource { func resourceServicesRpmProbeCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setServicesRpmProbe(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setServicesRpmProbe(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - rpmProbeExists, err := checkServicesRpmProbeExists(d.Get("name").(string), m, jnprSess) + rpmProbeExists, err := checkServicesRpmProbeExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if rpmProbeExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("services rpm probe %v already exists", d.Get("name").(string)))...) } - if err := setServicesRpmProbe(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setServicesRpmProbe(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_services_rpm_probe", jnprSess) + warns, err := clt.commitConf("create resource junos_services_rpm_probe", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - rpmProbeExists, err = checkServicesRpmProbeExists(d.Get("name").(string), m, jnprSess) + rpmProbeExists, err = checkServicesRpmProbeExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -374,25 +374,25 @@ func resourceServicesRpmProbeCreate(ctx context.Context, d *schema.ResourceData, "not exists after commit => check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceServicesRpmProbeReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceServicesRpmProbeReadWJunSess(d, clt, junSess)...) } func resourceServicesRpmProbeRead(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceServicesRpmProbeReadWJnprSess(d, m, jnprSess) + return resourceServicesRpmProbeReadWJunSess(d, clt, junSess) } -func resourceServicesRpmProbeReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceServicesRpmProbeReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - rpmProbeOptions, err := readServicesRpmProbe(d.Get("name").(string), m, jnprSess) + rpmProbeOptions, err := readServicesRpmProbe(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -409,77 +409,77 @@ func resourceServicesRpmProbeReadWJnprSess(d *schema.ResourceData, m interface{} func resourceServicesRpmProbeUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delServicesRpmProbe(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delServicesRpmProbe(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setServicesRpmProbe(d, m, nil); err != nil { + if err := setServicesRpmProbe(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delServicesRpmProbe(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delServicesRpmProbe(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setServicesRpmProbe(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setServicesRpmProbe(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_services_rpm_probe", jnprSess) + warns, err := clt.commitConf("update resource junos_services_rpm_probe", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceServicesRpmProbeReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceServicesRpmProbeReadWJunSess(d, clt, junSess)...) } func resourceServicesRpmProbeDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delServicesRpmProbe(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delServicesRpmProbe(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delServicesRpmProbe(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delServicesRpmProbe(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_services_rpm_probe", jnprSess) + warns, err := clt.commitConf("delete resource junos_services_rpm_probe", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -489,21 +489,21 @@ func resourceServicesRpmProbeDelete(ctx context.Context, d *schema.ResourceData, func resourceServicesRpmProbeImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - rpmProbeExists, err := checkServicesRpmProbeExists(d.Id(), m, jnprSess) + rpmProbeExists, err := checkServicesRpmProbeExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !rpmProbeExists { return nil, fmt.Errorf("don't find services rpm probe with id '%v' (id must be )", d.Id()) } - rpmProbeOptions, err := readServicesRpmProbe(d.Id(), m, jnprSess) + rpmProbeOptions, err := readServicesRpmProbe(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -514,9 +514,8 @@ func resourceServicesRpmProbeImport(ctx context.Context, d *schema.ResourceData, return result, nil } -func checkServicesRpmProbeExists(probe string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"services rpm probe \""+probe+"\""+pipeDisplaySet, jnprSess) +func checkServicesRpmProbeExists(probe string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"services rpm probe \""+probe+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -527,8 +526,7 @@ func checkServicesRpmProbeExists(probe string, m interface{}, jnprSess *NetconfO return true, nil } -func setServicesRpmProbe(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setServicesRpmProbe(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set services rpm probe \"" + d.Get("name").(string) + "\" " @@ -693,15 +691,14 @@ func setServicesRpmProbe(d *schema.ResourceData, m interface{}, jnprSess *Netcon } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readServicesRpmProbe(probe string, m interface{}, jnprSess *NetconfObject, +func readServicesRpmProbe(probe string, clt *Client, junSess *junosSession, ) (rpmProbeOptions, error) { - sess := m.(*Session) var confRead rpmProbeOptions - showConfig, err := sess.command(cmdShowConfig+"services rpm probe \""+probe+"\""+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"services rpm probe \""+probe+"\""+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -980,11 +977,10 @@ func readServicesRpmProbeTest(itemTrim string, test map[string]interface{}) erro return nil } -func delServicesRpmProbe(probe string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delServicesRpmProbe(probe string, clt *Client, junSess *junosSession) error { configSet := []string{"delete services rpm probe \"" + probe + "\""} - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillServicesRpmProbeData(d *schema.ResourceData, rpmProbeOptions rpmProbeOptions, diff --git a/junos/resource_services_security_intelligence_policy.go b/junos/resource_services_security_intelligence_policy.go index 9859fb1f..1aa4b83a 100644 --- a/junos/resource_services_security_intelligence_policy.go +++ b/junos/resource_services_security_intelligence_policy.go @@ -59,50 +59,50 @@ func resourceServicesSecurityIntellPolicy() *schema.Resource { func resourceServicesSecurityIntellPolicyCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setServicesSecurityIntellPolicy(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setServicesSecurityIntellPolicy(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - securityIntellPolicyExists, err := checkServicesSecurityIntellPolicyExists(d.Get("name").(string), m, jnprSess) + securityIntellPolicyExists, err := checkServicesSecurityIntellPolicyExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if securityIntellPolicyExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("services security-intelligence policy %v already exists", d.Get("name").(string)))...) } - if err := setServicesSecurityIntellPolicy(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setServicesSecurityIntellPolicy(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_services_security_intelligence_policy", jnprSess) + warns, err := clt.commitConf("create resource junos_services_security_intelligence_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - securityIntellPolicyExists, err = checkServicesSecurityIntellPolicyExists(d.Get("name").(string), m, jnprSess) + securityIntellPolicyExists, err = checkServicesSecurityIntellPolicyExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -113,25 +113,25 @@ func resourceServicesSecurityIntellPolicyCreate(ctx context.Context, d *schema.R "not exists after commit => check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceServicesSecurityIntellPolicyReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceServicesSecurityIntellPolicyReadWJunSess(d, clt, junSess)...) } func resourceServicesSecurityIntellPolicyRead(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceServicesSecurityIntellPolicyReadWJnprSess(d, m, jnprSess) + return resourceServicesSecurityIntellPolicyReadWJunSess(d, clt, junSess) } -func resourceServicesSecurityIntellPolicyReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceServicesSecurityIntellPolicyReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - securityIntellPolicyOptions, err := readServicesSecurityIntellPolicy(d.Get("name").(string), m, jnprSess) + securityIntellPolicyOptions, err := readServicesSecurityIntellPolicy(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -148,77 +148,77 @@ func resourceServicesSecurityIntellPolicyReadWJnprSess(d *schema.ResourceData, m func resourceServicesSecurityIntellPolicyUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delServicesSecurityIntellPolicy(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delServicesSecurityIntellPolicy(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setServicesSecurityIntellPolicy(d, m, nil); err != nil { + if err := setServicesSecurityIntellPolicy(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delServicesSecurityIntellPolicy(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delServicesSecurityIntellPolicy(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setServicesSecurityIntellPolicy(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setServicesSecurityIntellPolicy(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_services_security_intelligence_policy", jnprSess) + warns, err := clt.commitConf("update resource junos_services_security_intelligence_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceServicesSecurityIntellPolicyReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceServicesSecurityIntellPolicyReadWJunSess(d, clt, junSess)...) } func resourceServicesSecurityIntellPolicyDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delServicesSecurityIntellPolicy(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delServicesSecurityIntellPolicy(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delServicesSecurityIntellPolicy(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delServicesSecurityIntellPolicy(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_services_security_intelligence_policy", jnprSess) + warns, err := clt.commitConf("delete resource junos_services_security_intelligence_policy", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -228,21 +228,21 @@ func resourceServicesSecurityIntellPolicyDelete(ctx context.Context, d *schema.R func resourceServicesSecurityIntellPolicyImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - securityIntellPolicyExists, err := checkServicesSecurityIntellPolicyExists(d.Id(), m, jnprSess) + securityIntellPolicyExists, err := checkServicesSecurityIntellPolicyExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !securityIntellPolicyExists { return nil, fmt.Errorf("don't find services security-intelligence policy with id '%v' (id must be )", d.Id()) } - securityIntellPolicyOptions, err := readServicesSecurityIntellPolicy(d.Id(), m, jnprSess) + securityIntellPolicyOptions, err := readServicesSecurityIntellPolicy(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -253,10 +253,9 @@ func resourceServicesSecurityIntellPolicyImport(ctx context.Context, d *schema.R return result, nil } -func checkServicesSecurityIntellPolicyExists(policy string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "services security-intelligence policy \""+policy+"\""+pipeDisplaySet, jnprSess) +func checkServicesSecurityIntellPolicyExists(policy string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "services security-intelligence policy \""+policy+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -267,8 +266,7 @@ func checkServicesSecurityIntellPolicyExists(policy string, m interface{}, jnprS return true, nil } -func setServicesSecurityIntellPolicy(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setServicesSecurityIntellPolicy(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set services security-intelligence policy \"" + d.Get("name").(string) + "\" " @@ -286,16 +284,15 @@ func setServicesSecurityIntellPolicy(d *schema.ResourceData, m interface{}, jnpr configSet = append(configSet, setPrefix+"description \""+v+"\"") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readServicesSecurityIntellPolicy(policy string, m interface{}, jnprSess *NetconfObject, +func readServicesSecurityIntellPolicy(policy string, clt *Client, junSess *junosSession, ) (securityIntellPolicyOptions, error) { - sess := m.(*Session) var confRead securityIntellPolicyOptions - showConfig, err := sess.command(cmdShowConfig+ - "services security-intelligence policy \""+policy+"\""+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "services security-intelligence policy \""+policy+"\""+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -325,11 +322,10 @@ func readServicesSecurityIntellPolicy(policy string, m interface{}, jnprSess *Ne return confRead, nil } -func delServicesSecurityIntellPolicy(policy string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delServicesSecurityIntellPolicy(policy string, clt *Client, junSess *junosSession) error { configSet := []string{"delete services security-intelligence policy \"" + policy + "\""} - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillServicesSecurityIntellPolicyData( diff --git a/junos/resource_services_security_intelligence_profile.go b/junos/resource_services_security_intelligence_profile.go index a9a911c4..b7822c23 100644 --- a/junos/resource_services_security_intelligence_profile.go +++ b/junos/resource_services_security_intelligence_profile.go @@ -120,50 +120,50 @@ func resourceServicesSecurityIntellProfile() *schema.Resource { func resourceServicesSecurityIntellProfileCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setServicesSecurityIntellProfile(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setServicesSecurityIntellProfile(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - securityIntellProfileExists, err := checkServicesSecurityIntellProfileExists(d.Get("name").(string), m, jnprSess) + securityIntellProfileExists, err := checkServicesSecurityIntellProfileExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if securityIntellProfileExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("services security-intelligence profile %v already exists", d.Get("name").(string)))...) } - if err := setServicesSecurityIntellProfile(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setServicesSecurityIntellProfile(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_services_security_intelligence_profile", jnprSess) + warns, err := clt.commitConf("create resource junos_services_security_intelligence_profile", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - securityIntellProfileExists, err = checkServicesSecurityIntellProfileExists(d.Get("name").(string), m, jnprSess) + securityIntellProfileExists, err = checkServicesSecurityIntellProfileExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -174,25 +174,25 @@ func resourceServicesSecurityIntellProfileCreate(ctx context.Context, d *schema. "not exists after commit => check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceServicesSecurityIntellProfileReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceServicesSecurityIntellProfileReadWJunSess(d, clt, junSess)...) } func resourceServicesSecurityIntellProfileRead(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceServicesSecurityIntellProfileReadWJnprSess(d, m, jnprSess) + return resourceServicesSecurityIntellProfileReadWJunSess(d, clt, junSess) } -func resourceServicesSecurityIntellProfileReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceServicesSecurityIntellProfileReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - securityIntellProfileOptions, err := readServicesSecurityIntellProfile(d.Get("name").(string), m, jnprSess) + securityIntellProfileOptions, err := readServicesSecurityIntellProfile(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -209,77 +209,77 @@ func resourceServicesSecurityIntellProfileReadWJnprSess(d *schema.ResourceData, func resourceServicesSecurityIntellProfileUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delServicesSecurityIntellProfile(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delServicesSecurityIntellProfile(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setServicesSecurityIntellProfile(d, m, nil); err != nil { + if err := setServicesSecurityIntellProfile(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delServicesSecurityIntellProfile(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delServicesSecurityIntellProfile(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setServicesSecurityIntellProfile(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setServicesSecurityIntellProfile(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_services_security_intelligence_profile", jnprSess) + warns, err := clt.commitConf("update resource junos_services_security_intelligence_profile", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceServicesSecurityIntellProfileReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceServicesSecurityIntellProfileReadWJunSess(d, clt, junSess)...) } func resourceServicesSecurityIntellProfileDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delServicesSecurityIntellProfile(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delServicesSecurityIntellProfile(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delServicesSecurityIntellProfile(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delServicesSecurityIntellProfile(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_services_security_intelligence_profile", jnprSess) + warns, err := clt.commitConf("delete resource junos_services_security_intelligence_profile", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -289,21 +289,21 @@ func resourceServicesSecurityIntellProfileDelete(ctx context.Context, d *schema. func resourceServicesSecurityIntellProfileImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - securityIntellProfileExists, err := checkServicesSecurityIntellProfileExists(d.Id(), m, jnprSess) + securityIntellProfileExists, err := checkServicesSecurityIntellProfileExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !securityIntellProfileExists { return nil, fmt.Errorf("don't find services security-intelligence profile with id '%v' (id must be )", d.Id()) } - securityIntellProfileOptions, err := readServicesSecurityIntellProfile(d.Id(), m, jnprSess) + securityIntellProfileOptions, err := readServicesSecurityIntellProfile(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -314,10 +314,9 @@ func resourceServicesSecurityIntellProfileImport(ctx context.Context, d *schema. return result, nil } -func checkServicesSecurityIntellProfileExists(profile string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "services security-intelligence profile \""+profile+"\""+pipeDisplaySet, jnprSess) +func checkServicesSecurityIntellProfileExists(profile string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "services security-intelligence profile \""+profile+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -328,8 +327,7 @@ func checkServicesSecurityIntellProfileExists(profile string, m interface{}, jnp return true, nil } -func setServicesSecurityIntellProfile(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setServicesSecurityIntellProfile(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set services security-intelligence profile \"" + d.Get("name").(string) + "\" " @@ -370,16 +368,15 @@ func setServicesSecurityIntellProfile(d *schema.ResourceData, m interface{}, jnp configSet = append(configSet, setPrefix+"description \""+v+"\"") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readServicesSecurityIntellProfile(profile string, m interface{}, jnprSess *NetconfObject, +func readServicesSecurityIntellProfile(profile string, clt *Client, junSess *junosSession, ) (securityIntellProfileOptions, error) { - sess := m.(*Session) var confRead securityIntellProfileOptions - showConfig, err := sess.command(cmdShowConfig+ - "services security-intelligence profile \""+profile+"\""+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "services security-intelligence profile \""+profile+"\""+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -466,11 +463,10 @@ func readServicesSecurityIntellProfileRule(itemTrimPolicyRule string, ruleMap ma return nil } -func delServicesSecurityIntellProfile(profile string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delServicesSecurityIntellProfile(profile string, clt *Client, junSess *junosSession) error { configSet := []string{"delete services security-intelligence profile \"" + profile + "\""} - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillServicesSecurityIntellProfileData( diff --git a/junos/resource_services_ssl_initiation_profile.go b/junos/resource_services_ssl_initiation_profile.go index f0bf52f9..50d9e26f 100644 --- a/junos/resource_services_ssl_initiation_profile.go +++ b/junos/resource_services_ssl_initiation_profile.go @@ -102,51 +102,51 @@ func resourceServicesSSLInitiationProfile() *schema.Resource { func resourceServicesSSLInitiationProfileCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setServicesSSLInitiationProfile(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setServicesSSLInitiationProfile(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - svcSSLInitiationProfileExists, err := checkServicesSSLInitiationProfileExists(d.Get("name").(string), m, jnprSess) + svcSSLInitiationProfileExists, err := checkServicesSSLInitiationProfileExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if svcSSLInitiationProfileExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf( "services ssl initiation profile %v already exists", d.Get("name").(string)))...) } - if err := setServicesSSLInitiationProfile(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setServicesSSLInitiationProfile(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_services_ssl_initiation_profile", jnprSess) + warns, err := clt.commitConf("create resource junos_services_ssl_initiation_profile", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - svcSSLInitiationProfileExists, err = checkServicesSSLInitiationProfileExists(d.Get("name").(string), m, jnprSess) + svcSSLInitiationProfileExists, err = checkServicesSSLInitiationProfileExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -158,25 +158,25 @@ func resourceServicesSSLInitiationProfileCreate(ctx context.Context, d *schema.R "not exists after commit => check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceServicesSSLInitiationProfileReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceServicesSSLInitiationProfileReadWJunSess(d, clt, junSess)...) } func resourceServicesSSLInitiationProfileRead(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceServicesSSLInitiationProfileReadWJnprSess(d, m, jnprSess) + return resourceServicesSSLInitiationProfileReadWJunSess(d, clt, junSess) } -func resourceServicesSSLInitiationProfileReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceServicesSSLInitiationProfileReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - svcSSLInitiationProfileOptions, err := readServicesSSLInitiationProfile(d.Get("name").(string), m, jnprSess) + svcSSLInitiationProfileOptions, err := readServicesSSLInitiationProfile(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -193,77 +193,77 @@ func resourceServicesSSLInitiationProfileReadWJnprSess(d *schema.ResourceData, m func resourceServicesSSLInitiationProfileUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delServicesSSLInitiationProfile(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delServicesSSLInitiationProfile(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setServicesSSLInitiationProfile(d, m, nil); err != nil { + if err := setServicesSSLInitiationProfile(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delServicesSSLInitiationProfile(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delServicesSSLInitiationProfile(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setServicesSSLInitiationProfile(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setServicesSSLInitiationProfile(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_services_ssl_initiation_profile", jnprSess) + warns, err := clt.commitConf("update resource junos_services_ssl_initiation_profile", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceServicesSSLInitiationProfileReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceServicesSSLInitiationProfileReadWJunSess(d, clt, junSess)...) } func resourceServicesSSLInitiationProfileDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delServicesSSLInitiationProfile(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delServicesSSLInitiationProfile(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delServicesSSLInitiationProfile(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delServicesSSLInitiationProfile(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_services_ssl_initiation_profile", jnprSess) + warns, err := clt.commitConf("delete resource junos_services_ssl_initiation_profile", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -273,21 +273,21 @@ func resourceServicesSSLInitiationProfileDelete(ctx context.Context, d *schema.R func resourceServicesSSLInitiationProfileImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - svcSSLInitiationProfileExists, err := checkServicesSSLInitiationProfileExists(d.Id(), m, jnprSess) + svcSSLInitiationProfileExists, err := checkServicesSSLInitiationProfileExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !svcSSLInitiationProfileExists { return nil, fmt.Errorf("don't find services ssl initiation profile with id '%v' (id must be )", d.Id()) } - svcSSLInitiationProfileOptions, err := readServicesSSLInitiationProfile(d.Id(), m, jnprSess) + svcSSLInitiationProfileOptions, err := readServicesSSLInitiationProfile(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -298,11 +298,10 @@ func resourceServicesSSLInitiationProfileImport(ctx context.Context, d *schema.R return result, nil } -func checkServicesSSLInitiationProfileExists(profile string, m interface{}, jnprSess *NetconfObject, +func checkServicesSSLInitiationProfileExists(profile string, clt *Client, junSess *junosSession, ) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "services ssl initiation profile \""+profile+"\""+pipeDisplaySet, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "services ssl initiation profile \""+profile+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -313,8 +312,7 @@ func checkServicesSSLInitiationProfileExists(profile string, m interface{}, jnpr return true, nil } -func setServicesSSLInitiationProfile(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setServicesSSLInitiationProfile(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set services ssl initiation profile \"" + d.Get("name").(string) + "\" " @@ -359,16 +357,15 @@ func setServicesSSLInitiationProfile(d *schema.ResourceData, m interface{}, jnpr configSet = append(configSet, setPrefix+"trusted-ca \""+v+"\"") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readServicesSSLInitiationProfile(profile string, m interface{}, jnprSess *NetconfObject, +func readServicesSSLInitiationProfile(profile string, clt *Client, junSess *junosSession, ) (svcSSLInitiationProfileOptions, error) { - sess := m.(*Session) var confRead svcSSLInitiationProfileOptions - showConfig, err := sess.command(cmdShowConfig+ - "services ssl initiation profile \""+profile+"\""+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "services ssl initiation profile \""+profile+"\""+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -424,13 +421,12 @@ func readServicesSSLInitiationProfile(profile string, m interface{}, jnprSess *N return confRead, nil } -func delServicesSSLInitiationProfile(profile string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delServicesSSLInitiationProfile(profile string, clt *Client, junSess *junosSession) error { configSet := []string{ "delete services ssl initiation profile \"" + profile + "\"", } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillServicesSSLInitiationProfileData( diff --git a/junos/resource_services_user_identification_ad_access_domain.go b/junos/resource_services_user_identification_ad_access_domain.go index ab5caa17..cda1fa45 100644 --- a/junos/resource_services_user_identification_ad_access_domain.go +++ b/junos/resource_services_user_identification_ad_access_domain.go @@ -127,52 +127,55 @@ func resourceServicesUserIdentAdAccessDomain() *schema.Resource { func resourceServicesUserIdentAdAccessDomainCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setServicesUserIdentAdAccessDomain(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setServicesUserIdentAdAccessDomain(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics svcUserIdentAdAccessDomainExists, err := checkServicesUserIdentAdAccessDomainExists( - d.Get("name").(string), m, jnprSess) + d.Get("name").(string), + clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if svcUserIdentAdAccessDomainExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf( "services user-identification active-directory-access domain %v already exists", d.Get("name").(string)))...) } - if err := setServicesUserIdentAdAccessDomain(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setServicesUserIdentAdAccessDomain(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_services_user_identification_ad_access_domain", jnprSess) + warns, err := clt.commitConf("create resource junos_services_user_identification_ad_access_domain", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - svcUserIdentAdAccessDomainExists, err = checkServicesUserIdentAdAccessDomainExists(d.Get("name").(string), m, jnprSess) + svcUserIdentAdAccessDomainExists, err = checkServicesUserIdentAdAccessDomainExists( + d.Get("name").(string), + clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -184,26 +187,26 @@ func resourceServicesUserIdentAdAccessDomainCreate(ctx context.Context, d *schem "not exists after commit => check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceServicesUserIdentAdAccessDomainReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceServicesUserIdentAdAccessDomainReadWJunSess(d, clt, junSess)...) } func resourceServicesUserIdentAdAccessDomainRead(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceServicesUserIdentAdAccessDomainReadWJnprSess(d, m, jnprSess) + return resourceServicesUserIdentAdAccessDomainReadWJunSess(d, clt, junSess) } -func resourceServicesUserIdentAdAccessDomainReadWJnprSess( - d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceServicesUserIdentAdAccessDomainReadWJunSess( + d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - svcUserIdentAdAccessDomainOptions, err := readServicesUserIdentAdAccessDomain(d.Get("name").(string), m, jnprSess) + svcUserIdentAdAccessDomainOptions, err := readServicesUserIdentAdAccessDomain(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -220,77 +223,77 @@ func resourceServicesUserIdentAdAccessDomainReadWJnprSess( func resourceServicesUserIdentAdAccessDomainUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delServicesUserIdentAdAccessDomain(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delServicesUserIdentAdAccessDomain(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setServicesUserIdentAdAccessDomain(d, m, nil); err != nil { + if err := setServicesUserIdentAdAccessDomain(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delServicesUserIdentAdAccessDomain(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delServicesUserIdentAdAccessDomain(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setServicesUserIdentAdAccessDomain(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setServicesUserIdentAdAccessDomain(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_services_user_identification_ad_access_domain", jnprSess) + warns, err := clt.commitConf("update resource junos_services_user_identification_ad_access_domain", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceServicesUserIdentAdAccessDomainReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceServicesUserIdentAdAccessDomainReadWJunSess(d, clt, junSess)...) } func resourceServicesUserIdentAdAccessDomainDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delServicesUserIdentAdAccessDomain(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delServicesUserIdentAdAccessDomain(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delServicesUserIdentAdAccessDomain(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delServicesUserIdentAdAccessDomain(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_services_user_identification_ad_access_domain", jnprSess) + warns, err := clt.commitConf("delete resource junos_services_user_identification_ad_access_domain", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -300,14 +303,14 @@ func resourceServicesUserIdentAdAccessDomainDelete(ctx context.Context, d *schem func resourceServicesUserIdentAdAccessDomainImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - svcUserIdentAdAccessDomainExists, err := checkServicesUserIdentAdAccessDomainExists(d.Id(), m, jnprSess) + svcUserIdentAdAccessDomainExists, err := checkServicesUserIdentAdAccessDomainExists(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -315,7 +318,7 @@ func resourceServicesUserIdentAdAccessDomainImport(ctx context.Context, d *schem return nil, fmt.Errorf("don't find services user-identification "+ "active-directory-access domain with id '%v' (id must be )", d.Id()) } - svcUserIdentAdAccessDomainOptions, err := readServicesUserIdentAdAccessDomain(d.Id(), m, jnprSess) + svcUserIdentAdAccessDomainOptions, err := readServicesUserIdentAdAccessDomain(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -326,11 +329,10 @@ func resourceServicesUserIdentAdAccessDomainImport(ctx context.Context, d *schem return result, nil } -func checkServicesUserIdentAdAccessDomainExists(domain string, m interface{}, jnprSess *NetconfObject, +func checkServicesUserIdentAdAccessDomainExists(domain string, clt *Client, junSess *junosSession, ) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "services user-identification active-directory-access domain "+domain+pipeDisplaySet, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "services user-identification active-directory-access domain "+domain+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -341,8 +343,7 @@ func checkServicesUserIdentAdAccessDomainExists(domain string, m interface{}, jn return true, nil } -func setServicesUserIdentAdAccessDomain(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setServicesUserIdentAdAccessDomain(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set services user-identification active-directory-access domain " + d.Get("name").(string) + " " @@ -392,16 +393,15 @@ func setServicesUserIdentAdAccessDomain(d *schema.ResourceData, m interface{}, j } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readServicesUserIdentAdAccessDomain(domain string, m interface{}, jnprSess *NetconfObject, +func readServicesUserIdentAdAccessDomain(domain string, clt *Client, junSess *junosSession, ) (svcUserIdentAdAccessDomainOptions, error) { - sess := m.(*Session) var confRead svcUserIdentAdAccessDomainOptions - showConfig, err := sess.command(cmdShowConfig+ - "services user-identification active-directory-access domain "+domain+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "services user-identification active-directory-access domain "+domain+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -493,13 +493,12 @@ func readServicesUserIdentAdAccessDomain(domain string, m interface{}, jnprSess return confRead, nil } -func delServicesUserIdentAdAccessDomain(domain string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delServicesUserIdentAdAccessDomain(domain string, clt *Client, junSess *junosSession) error { configSet := []string{ "delete services user-identification active-directory-access domain " + domain, } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillServicesUserIdentAdAccessDomainData( diff --git a/junos/resource_services_user_identification_device_identity_profile.go b/junos/resource_services_user_identification_device_identity_profile.go index ba52a998..42867959 100644 --- a/junos/resource_services_user_identification_device_identity_profile.go +++ b/junos/resource_services_user_identification_device_identity_profile.go @@ -63,53 +63,55 @@ func resourceServicesUserIdentDeviceIdentityProfile() *schema.Resource { func resourceServicesUserIdentDeviceIdentityProfileCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setServicesUserIdentDeviceIdentityProfile(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setServicesUserIdentDeviceIdentityProfile(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics svcUserIdentDevIdentProfileExists, err := checkServicesUserIdentDeviceIdentityProfileExists( - d.Get("name").(string), m, jnprSess) + d.Get("name").(string), + clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if svcUserIdentDevIdentProfileExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf( "services user-identification device-information end-user-profile %v already exists", d.Get("name").(string)))...) } - if err := setServicesUserIdentDeviceIdentityProfile(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setServicesUserIdentDeviceIdentityProfile(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_services_user_identification_device_identity_profile", jnprSess) + warns, err := clt.commitConf("create resource junos_services_user_identification_device_identity_profile", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } svcUserIdentDevIdentProfileExists, err = checkServicesUserIdentDeviceIdentityProfileExists( - d.Get("name").(string), m, jnprSess) + d.Get("name").(string), + clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -121,27 +123,28 @@ func resourceServicesUserIdentDeviceIdentityProfileCreate(ctx context.Context, d "not exists after commit => check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceServicesUserIdentDeviceIdentityProfileReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceServicesUserIdentDeviceIdentityProfileReadWJunSess(d, clt, junSess)...) } func resourceServicesUserIdentDeviceIdentityProfileRead(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceServicesUserIdentDeviceIdentityProfileReadWJnprSess(d, m, jnprSess) + return resourceServicesUserIdentDeviceIdentityProfileReadWJunSess(d, clt, junSess) } -func resourceServicesUserIdentDeviceIdentityProfileReadWJnprSess( - d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceServicesUserIdentDeviceIdentityProfileReadWJunSess( + d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() svcUserIdentDevIdentProfileOptions, err := readServicesUserIdentDeviceIdentityProfile( - d.Get("name").(string), m, jnprSess) + d.Get("name").(string), + clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -158,78 +161,78 @@ func resourceServicesUserIdentDeviceIdentityProfileReadWJnprSess( func resourceServicesUserIdentDeviceIdentityProfileUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delServicesUserIdentDeviceIdentityProfile(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delServicesUserIdentDeviceIdentityProfile(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setServicesUserIdentDeviceIdentityProfile(d, m, nil); err != nil { + if err := setServicesUserIdentDeviceIdentityProfile(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delServicesUserIdentDeviceIdentityProfile(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delServicesUserIdentDeviceIdentityProfile(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setServicesUserIdentDeviceIdentityProfile(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setServicesUserIdentDeviceIdentityProfile(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_services_user_identification_device_identity_profile", jnprSess) + warns, err := clt.commitConf("update resource junos_services_user_identification_device_identity_profile", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceServicesUserIdentDeviceIdentityProfileReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceServicesUserIdentDeviceIdentityProfileReadWJunSess(d, clt, junSess)...) } func resourceServicesUserIdentDeviceIdentityProfileDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delServicesUserIdentDeviceIdentityProfile(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delServicesUserIdentDeviceIdentityProfile(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delServicesUserIdentDeviceIdentityProfile(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delServicesUserIdentDeviceIdentityProfile(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_services_user_identification_device_identity_profile", jnprSess) + warns, err := clt.commitConf("delete resource junos_services_user_identification_device_identity_profile", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -239,14 +242,14 @@ func resourceServicesUserIdentDeviceIdentityProfileDelete(ctx context.Context, d func resourceServicesUserIdentDeviceIdentityProfileImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - svcUserIdentDevIdentProfileExists, err := checkServicesUserIdentDeviceIdentityProfileExists(d.Id(), m, jnprSess) + svcUserIdentDevIdentProfileExists, err := checkServicesUserIdentDeviceIdentityProfileExists(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -254,7 +257,7 @@ func resourceServicesUserIdentDeviceIdentityProfileImport(ctx context.Context, d return nil, fmt.Errorf("don't find services user-identification "+ "device-information end-user-profile with id '%v' (id must be )", d.Id()) } - svcUserIdentDevIdentProfileOptions, err := readServicesUserIdentDeviceIdentityProfile(d.Id(), m, jnprSess) + svcUserIdentDevIdentProfileOptions, err := readServicesUserIdentDeviceIdentityProfile(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -265,11 +268,10 @@ func resourceServicesUserIdentDeviceIdentityProfileImport(ctx context.Context, d return result, nil } -func checkServicesUserIdentDeviceIdentityProfileExists(profile string, m interface{}, jnprSess *NetconfObject, +func checkServicesUserIdentDeviceIdentityProfileExists(profile string, clt *Client, junSess *junosSession, ) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "services user-identification device-information end-user-profile profile-name "+profile+pipeDisplaySet, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "services user-identification device-information end-user-profile profile-name "+profile+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -280,8 +282,7 @@ func checkServicesUserIdentDeviceIdentityProfileExists(profile string, m interfa return true, nil } -func setServicesUserIdentDeviceIdentityProfile(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setServicesUserIdentDeviceIdentityProfile(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set services user-identification device-information end-user-profile profile-name " + @@ -300,17 +301,16 @@ func setServicesUserIdentDeviceIdentityProfile(d *schema.ResourceData, m interfa } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readServicesUserIdentDeviceIdentityProfile(profile string, m interface{}, jnprSess *NetconfObject, +func readServicesUserIdentDeviceIdentityProfile(profile string, clt *Client, junSess *junosSession, ) (svcUserIdentDevIdentProfileOptions, error) { - sess := m.(*Session) var confRead svcUserIdentDevIdentProfileOptions - showConfig, err := sess.command(cmdShowConfig+ + showConfig, err := clt.command(cmdShowConfig+ "services user-identification device-information end-user-profile"+ - " profile-name "+profile+pipeDisplaySetRelative, jnprSess) + " profile-name "+profile+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -344,13 +344,12 @@ func readServicesUserIdentDeviceIdentityProfile(profile string, m interface{}, j return confRead, nil } -func delServicesUserIdentDeviceIdentityProfile(profile string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delServicesUserIdentDeviceIdentityProfile(profile string, clt *Client, junSess *junosSession) error { configSet := []string{ "delete services user-identification device-information end-user-profile profile-name " + profile, } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillServicesUserIdentDeviceIdentityProfileData( diff --git a/junos/resource_snmp.go b/junos/resource_snmp.go index 94363fd0..6fea361a 100644 --- a/junos/resource_snmp.go +++ b/junos/resource_snmp.go @@ -157,55 +157,55 @@ func resourceSnmp() *schema.Resource { } func resourceSnmpCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSnmp(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSnmp(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId("snmp") return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := setSnmp(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSnmp(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_snmp", jnprSess) + warns, err := clt.commitConf("create resource junos_snmp", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.SetId("snmp") - return append(diagWarns, resourceSnmpReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSnmpReadWJunSess(d, clt, junSess)...) } func resourceSnmpRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSnmpReadWJnprSess(d, m, jnprSess) + return resourceSnmpReadWJunSess(d, clt, junSess) } -func resourceSnmpReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) diag.Diagnostics { +func resourceSnmpReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession) diag.Diagnostics { mutex.Lock() - snmpOptions, err := readSnmp(m, jnprSess) + snmpOptions, err := readSnmp(clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -217,77 +217,77 @@ func resourceSnmpReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess * func resourceSnmpUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSnmp(m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSnmp(clt, nil); err != nil { return diag.FromErr(err) } - if err := setSnmp(d, m, nil); err != nil { + if err := setSnmp(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSnmp(m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSnmp(clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSnmp(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSnmp(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_snmp", jnprSess) + warns, err := clt.commitConf("update resource junos_snmp", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSnmpReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSnmpReadWJunSess(d, clt, junSess)...) } func resourceSnmpDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { if d.Get("clean_on_destroy").(bool) { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSnmp(m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSnmp(clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSnmp(m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSnmp(clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_snmp", jnprSess) + warns, err := clt.commitConf("delete resource junos_snmp", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -298,14 +298,14 @@ func resourceSnmpDelete(ctx context.Context, d *schema.ResourceData, m interface func resourceSnmpImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - snmpOptions, err := readSnmp(m, jnprSess) + snmpOptions, err := readSnmp(clt, junSess) if err != nil { return nil, err } @@ -316,9 +316,7 @@ func resourceSnmpImport(ctx context.Context, d *schema.ResourceData, m interface return result, nil } -func setSnmp(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) - +func setSnmp(d *schema.ResourceData, clt *Client, junSess *junosSession) error { setPrefix := "set snmp " configSet := make([]string, 0) @@ -389,10 +387,10 @@ func setSnmp(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) err configSet = append(configSet, setPrefix+"routing-instance-access access-list \""+v+"\"") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func delSnmp(m interface{}, jnprSess *NetconfObject) error { +func delSnmp(clt *Client, junSess *junosSession) error { listLinesToDelete := []string{ "arp", "contact", @@ -406,7 +404,7 @@ func delSnmp(m interface{}, jnprSess *NetconfObject) error { "location", "routing-instance-access", } - sess := m.(*Session) + configSet := make([]string, 0) delPrefix := "delete snmp " for _, line := range listLinesToDelete { @@ -414,14 +412,13 @@ func delSnmp(m interface{}, jnprSess *NetconfObject) error { delPrefix+line) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSnmp(m interface{}, jnprSess *NetconfObject) (snmpOptions, error) { - sess := m.(*Session) +func readSnmp(clt *Client, junSess *junosSession) (snmpOptions, error) { var confRead snmpOptions - showConfig, err := sess.command(cmdShowConfig+"snmp"+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"snmp"+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } diff --git a/junos/resource_snmp_clientlist.go b/junos/resource_snmp_clientlist.go index 7689061f..922eec1a 100644 --- a/junos/resource_snmp_clientlist.go +++ b/junos/resource_snmp_clientlist.go @@ -39,49 +39,49 @@ func resourceSnmpClientlist() *schema.Resource { } func resourceSnmpClientlistCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSnmpClientlist(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSnmpClientlist(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - snmpClientlistExists, err := checkSnmpClientlistExists(d.Get("name").(string), m, jnprSess) + snmpClientlistExists, err := checkSnmpClientlistExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if snmpClientlistExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("snmp client-list %v already exists", d.Get("name").(string)))...) } - if err := setSnmpClientlist(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSnmpClientlist(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_snmp_clientlist", jnprSess) + warns, err := clt.commitConf("create resource junos_snmp_clientlist", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - snmpClientlistExists, err = checkSnmpClientlistExists(d.Get("name").(string), m, jnprSess) + snmpClientlistExists, err = checkSnmpClientlistExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -92,24 +92,24 @@ func resourceSnmpClientlistCreate(ctx context.Context, d *schema.ResourceData, m "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceSnmpClientlistReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSnmpClientlistReadWJunSess(d, clt, junSess)...) } func resourceSnmpClientlistRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSnmpClientlistReadWJnprSess(d, m, jnprSess) + return resourceSnmpClientlistReadWJunSess(d, clt, junSess) } -func resourceSnmpClientlistReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSnmpClientlistReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - snmpClientlistOptions, err := readSnmpClientlist(d.Get("name").(string), m, jnprSess) + snmpClientlistOptions, err := readSnmpClientlist(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -125,76 +125,76 @@ func resourceSnmpClientlistReadWJnprSess(d *schema.ResourceData, m interface{}, func resourceSnmpClientlistUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSnmpClientlist(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSnmpClientlist(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSnmpClientlist(d, m, nil); err != nil { + if err := setSnmpClientlist(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSnmpClientlist(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSnmpClientlist(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSnmpClientlist(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSnmpClientlist(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_snmp_clientlist", jnprSess) + warns, err := clt.commitConf("update resource junos_snmp_clientlist", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSnmpClientlistReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSnmpClientlistReadWJunSess(d, clt, junSess)...) } func resourceSnmpClientlistDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSnmpClientlist(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSnmpClientlist(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSnmpClientlist(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSnmpClientlist(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_snmp_clientlist", jnprSess) + warns, err := clt.commitConf("delete resource junos_snmp_clientlist", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -204,22 +204,22 @@ func resourceSnmpClientlistDelete(ctx context.Context, d *schema.ResourceData, m func resourceSnmpClientlistImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - snmpClientlistExists, err := checkSnmpClientlistExists(d.Id(), m, jnprSess) + snmpClientlistExists, err := checkSnmpClientlistExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !snmpClientlistExists { return nil, fmt.Errorf("don't find snmp client-list with id '%v' (id must be )", d.Id()) } - snmpClientlistOptions, err := readSnmpClientlist(d.Id(), m, jnprSess) + snmpClientlistOptions, err := readSnmpClientlist(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -230,9 +230,8 @@ func resourceSnmpClientlistImport(ctx context.Context, d *schema.ResourceData, m return result, nil } -func checkSnmpClientlistExists(name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"snmp client-list \""+name+"\""+pipeDisplaySet, jnprSess) +func checkSnmpClientlistExists(name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"snmp client-list \""+name+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -243,9 +242,7 @@ func checkSnmpClientlistExists(name string, m interface{}, jnprSess *NetconfObje return true, nil } -func setSnmpClientlist(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) - +func setSnmpClientlist(d *schema.ResourceData, clt *Client, junSess *junosSession) error { setPrefix := "set snmp client-list \"" + d.Get("name").(string) + "\" " configSet := make([]string, 0) @@ -254,14 +251,13 @@ func setSnmpClientlist(d *schema.ResourceData, m interface{}, jnprSess *NetconfO configSet = append(configSet, setPrefix+v) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSnmpClientlist(name string, m interface{}, jnprSess *NetconfObject) (snmpClientlistOptions, error) { - sess := m.(*Session) +func readSnmpClientlist(name string, clt *Client, junSess *junosSession) (snmpClientlistOptions, error) { var confRead snmpClientlistOptions - showConfig, err := sess.command(cmdShowConfig+"snmp client-list \""+name+"\""+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"snmp client-list \""+name+"\""+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -284,11 +280,10 @@ func readSnmpClientlist(name string, m interface{}, jnprSess *NetconfObject) (sn return confRead, nil } -func delSnmpClientlist(name string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSnmpClientlist(name string, clt *Client, junSess *junosSession) error { configSet := []string{"delete snmp client-list \"" + name + "\""} - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSnmpClientlistData(d *schema.ResourceData, snmpClientlistOptions snmpClientlistOptions) { diff --git a/junos/resource_snmp_community.go b/junos/resource_snmp_community.go index 4c549c64..362ab606 100644 --- a/junos/resource_snmp_community.go +++ b/junos/resource_snmp_community.go @@ -87,49 +87,49 @@ func resourceSnmpCommunity() *schema.Resource { } func resourceSnmpCommunityCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSnmpCommunity(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSnmpCommunity(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - snmpCommunityExists, err := checkSnmpCommunityExists(d.Get("name").(string), m, jnprSess) + snmpCommunityExists, err := checkSnmpCommunityExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if snmpCommunityExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("snmp community %v already exists", d.Get("name").(string)))...) } - if err := setSnmpCommunity(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSnmpCommunity(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_snmp_community", jnprSess) + warns, err := clt.commitConf("create resource junos_snmp_community", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - snmpCommunityExists, err = checkSnmpCommunityExists(d.Get("name").(string), m, jnprSess) + snmpCommunityExists, err = checkSnmpCommunityExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -140,24 +140,24 @@ func resourceSnmpCommunityCreate(ctx context.Context, d *schema.ResourceData, m "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceSnmpCommunityReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSnmpCommunityReadWJunSess(d, clt, junSess)...) } func resourceSnmpCommunityRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSnmpCommunityReadWJnprSess(d, m, jnprSess) + return resourceSnmpCommunityReadWJunSess(d, clt, junSess) } -func resourceSnmpCommunityReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSnmpCommunityReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - snmpCommunityOptions, err := readSnmpCommunity(d.Get("name").(string), m, jnprSess) + snmpCommunityOptions, err := readSnmpCommunity(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -173,76 +173,76 @@ func resourceSnmpCommunityReadWJnprSess(d *schema.ResourceData, m interface{}, j func resourceSnmpCommunityUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSnmpCommunity(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSnmpCommunity(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSnmpCommunity(d, m, nil); err != nil { + if err := setSnmpCommunity(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSnmpCommunity(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSnmpCommunity(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSnmpCommunity(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSnmpCommunity(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_snmp_community", jnprSess) + warns, err := clt.commitConf("update resource junos_snmp_community", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSnmpCommunityReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSnmpCommunityReadWJunSess(d, clt, junSess)...) } func resourceSnmpCommunityDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSnmpCommunity(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSnmpCommunity(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSnmpCommunity(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSnmpCommunity(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_snmp_community", jnprSess) + warns, err := clt.commitConf("delete resource junos_snmp_community", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -252,22 +252,22 @@ func resourceSnmpCommunityDelete(ctx context.Context, d *schema.ResourceData, m func resourceSnmpCommunityImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - snmpCommunityExists, err := checkSnmpCommunityExists(d.Id(), m, jnprSess) + snmpCommunityExists, err := checkSnmpCommunityExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !snmpCommunityExists { return nil, fmt.Errorf("don't find snmp community with id '%v' (id must be )", d.Id()) } - snmpCommunityOptions, err := readSnmpCommunity(d.Id(), m, jnprSess) + snmpCommunityOptions, err := readSnmpCommunity(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -278,9 +278,8 @@ func resourceSnmpCommunityImport(ctx context.Context, d *schema.ResourceData, m return result, nil } -func checkSnmpCommunityExists(name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"snmp community \""+name+"\""+pipeDisplaySet, jnprSess) +func checkSnmpCommunityExists(name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"snmp community \""+name+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -291,9 +290,7 @@ func checkSnmpCommunityExists(name string, m interface{}, jnprSess *NetconfObjec return true, nil } -func setSnmpCommunity(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) - +func setSnmpCommunity(d *schema.ResourceData, clt *Client, junSess *junosSession) error { setPrefix := "set snmp community \"" + d.Get("name").(string) + "\" " configSet := make([]string, 0) @@ -334,14 +331,13 @@ func setSnmpCommunity(d *schema.ResourceData, m interface{}, jnprSess *NetconfOb configSet = append(configSet, setPrefix+"view \""+v+"\"") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSnmpCommunity(name string, m interface{}, jnprSess *NetconfObject) (snmpCommunityOptions, error) { - sess := m.(*Session) +func readSnmpCommunity(name string, clt *Client, junSess *junosSession) (snmpCommunityOptions, error) { var confRead snmpCommunityOptions - showConfig, err := sess.command(cmdShowConfig+"snmp community \""+name+"\""+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"snmp community \""+name+"\""+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -391,11 +387,10 @@ func readSnmpCommunity(name string, m interface{}, jnprSess *NetconfObject) (snm return confRead, nil } -func delSnmpCommunity(name string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSnmpCommunity(name string, clt *Client, junSess *junosSession) error { configSet := []string{"delete snmp community \"" + name + "\""} - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSnmpCommunityData(d *schema.ResourceData, snmpCommunityOptions snmpCommunityOptions) { diff --git a/junos/resource_snmp_v3_community.go b/junos/resource_snmp_v3_community.go index c3b9d242..a69126a0 100644 --- a/junos/resource_snmp_v3_community.go +++ b/junos/resource_snmp_v3_community.go @@ -54,50 +54,50 @@ func resourceSnmpV3Community() *schema.Resource { } func resourceSnmpV3CommunityCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSnmpV3Community(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSnmpV3Community(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("community_index").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - snmpV3CommunityExists, err := checkSnmpV3CommunityExists(d.Get("community_index").(string), m, jnprSess) + snmpV3CommunityExists, err := checkSnmpV3CommunityExists(d.Get("community_index").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if snmpV3CommunityExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf( "snmp v3 snmp-community %v already exists", d.Get("community_index").(string)))...) } - if err := setSnmpV3Community(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSnmpV3Community(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_snmp_v3_community", jnprSess) + warns, err := clt.commitConf("create resource junos_snmp_v3_community", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - snmpV3CommunityExists, err = checkSnmpV3CommunityExists(d.Get("community_index").(string), m, jnprSess) + snmpV3CommunityExists, err = checkSnmpV3CommunityExists(d.Get("community_index").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -108,24 +108,24 @@ func resourceSnmpV3CommunityCreate(ctx context.Context, d *schema.ResourceData, "=> check your config", d.Get("community_index").(string)))...) } - return append(diagWarns, resourceSnmpV3CommunityReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSnmpV3CommunityReadWJunSess(d, clt, junSess)...) } func resourceSnmpV3CommunityRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSnmpV3CommunityReadWJnprSess(d, m, jnprSess) + return resourceSnmpV3CommunityReadWJunSess(d, clt, junSess) } -func resourceSnmpV3CommunityReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSnmpV3CommunityReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - snmpV3CommunityOptions, err := readSnmpV3Community(d.Get("community_index").(string), m, jnprSess) + snmpV3CommunityOptions, err := readSnmpV3Community(d.Get("community_index").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -141,76 +141,76 @@ func resourceSnmpV3CommunityReadWJnprSess(d *schema.ResourceData, m interface{}, func resourceSnmpV3CommunityUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSnmpV3Community(d.Get("community_index").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSnmpV3Community(d.Get("community_index").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSnmpV3Community(d, m, nil); err != nil { + if err := setSnmpV3Community(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSnmpV3Community(d.Get("community_index").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSnmpV3Community(d.Get("community_index").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSnmpV3Community(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSnmpV3Community(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_snmp_v3_community", jnprSess) + warns, err := clt.commitConf("update resource junos_snmp_v3_community", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSnmpV3CommunityReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSnmpV3CommunityReadWJunSess(d, clt, junSess)...) } func resourceSnmpV3CommunityDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSnmpV3Community(d.Get("community_index").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSnmpV3Community(d.Get("community_index").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSnmpV3Community(d.Get("community_index").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSnmpV3Community(d.Get("community_index").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_snmp_v3_community", jnprSess) + warns, err := clt.commitConf("delete resource junos_snmp_v3_community", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -220,22 +220,22 @@ func resourceSnmpV3CommunityDelete(ctx context.Context, d *schema.ResourceData, func resourceSnmpV3CommunityImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - snmpV3CommunityExists, err := checkSnmpV3CommunityExists(d.Id(), m, jnprSess) + snmpV3CommunityExists, err := checkSnmpV3CommunityExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !snmpV3CommunityExists { return nil, fmt.Errorf("don't find snmp v3 snmp-community with id '%v' (id must be )", d.Id()) } - snmpV3CommunityOptions, err := readSnmpV3Community(d.Id(), m, jnprSess) + snmpV3CommunityOptions, err := readSnmpV3Community(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -246,10 +246,9 @@ func resourceSnmpV3CommunityImport(ctx context.Context, d *schema.ResourceData, return result, nil } -func checkSnmpV3CommunityExists(communityIndex string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+ - "snmp v3 snmp-community \""+communityIndex+"\""+pipeDisplaySet, jnprSess) +func checkSnmpV3CommunityExists(communityIndex string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+ + "snmp v3 snmp-community \""+communityIndex+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -260,9 +259,7 @@ func checkSnmpV3CommunityExists(communityIndex string, m interface{}, jnprSess * return true, nil } -func setSnmpV3Community(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) - +func setSnmpV3Community(d *schema.ResourceData, clt *Client, junSess *junosSession) error { setPrefix := "set snmp v3 snmp-community \"" + d.Get("community_index").(string) + "\" " configSet := make([]string, 0) @@ -277,16 +274,15 @@ func setSnmpV3Community(d *schema.ResourceData, m interface{}, jnprSess *Netconf configSet = append(configSet, setPrefix+"tag \""+v+"\"") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSnmpV3Community(communityIndex string, m interface{}, jnprSess *NetconfObject, +func readSnmpV3Community(communityIndex string, clt *Client, junSess *junosSession, ) (snmpV3CommunityOptions, error) { - sess := m.(*Session) var confRead snmpV3CommunityOptions - showConfig, err := sess.command(cmdShowConfig+ - "snmp v3 snmp-community \""+communityIndex+"\""+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "snmp v3 snmp-community \""+communityIndex+"\""+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -320,12 +316,10 @@ func readSnmpV3Community(communityIndex string, m interface{}, jnprSess *Netconf return confRead, nil } -func delSnmpV3Community(communityIndex string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) - +func delSnmpV3Community(communityIndex string, clt *Client, junSess *junosSession) error { configSet := []string{"delete snmp v3 snmp-community \"" + communityIndex + "\""} - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSnmpV3CommunityData(d *schema.ResourceData, snmpV3CommunityOptions snmpV3CommunityOptions) { diff --git a/junos/resource_snmp_v3_usm_user.go b/junos/resource_snmp_v3_usm_user.go index 6daf9555..413bb753 100644 --- a/junos/resource_snmp_v3_usm_user.go +++ b/junos/resource_snmp_v3_usm_user.go @@ -102,9 +102,9 @@ func resourceSnmpV3UsmUser() *schema.Resource { } func resourceSnmpV3UsmUserCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSnmpV3UsmUser(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSnmpV3UsmUser(d, clt, nil); err != nil { return diag.FromErr(err) } if d.Get("engine_type").(string) == "local" { @@ -115,24 +115,27 @@ func resourceSnmpV3UsmUserCreate(ctx context.Context, d *schema.ResourceData, m return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics snmpV3UsmUserExists, err := checkSnmpV3UsmUserExists( - d.Get("name").(string), d.Get("engine_type").(string), d.Get("engine_id").(string), m, jnprSess) + d.Get("name").(string), + d.Get("engine_type").(string), + d.Get("engine_id").(string), + clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if snmpV3UsmUserExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) if d.Get("engine_type").(string) != "local" { return append(diagWarns, diag.FromErr(fmt.Errorf("snmp v3 usm user %v in remote-engine %s already exists", @@ -143,20 +146,23 @@ func resourceSnmpV3UsmUserCreate(ctx context.Context, d *schema.ResourceData, m d.Get("name").(string)))...) } - if err := setSnmpV3UsmUser(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSnmpV3UsmUser(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_snmp_v3_usm_user", jnprSess) + warns, err := clt.commitConf("create resource junos_snmp_v3_usm_user", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } snmpV3UsmUserExists, err = checkSnmpV3UsmUserExists( - d.Get("name").(string), d.Get("engine_type").(string), d.Get("engine_id").(string), m, jnprSess) + d.Get("name").(string), + d.Get("engine_type").(string), + d.Get("engine_id").(string), + clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -176,21 +182,21 @@ func resourceSnmpV3UsmUserCreate(ctx context.Context, d *schema.ResourceData, m "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceSnmpV3UsmUserReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSnmpV3UsmUserReadWJunSess(d, clt, junSess)...) } func resourceSnmpV3UsmUserRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSnmpV3UsmUserReadWJnprSess(d, m, jnprSess) + return resourceSnmpV3UsmUserReadWJunSess(d, clt, junSess) } -func resourceSnmpV3UsmUserReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSnmpV3UsmUserReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { configSrc := snmpV3UsmUserOptions{ name: d.Get("name").(string), @@ -202,7 +208,7 @@ func resourceSnmpV3UsmUserReadWJnprSess(d *schema.ResourceData, m interface{}, j privacyType: d.Get("privacy_type").(string), } mutex.Lock() - snmpV3UsmUserOptions, err := readSnmpV3UsmUser(configSrc, m, jnprSess) + snmpV3UsmUserOptions, err := readSnmpV3UsmUser(configSrc, clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -218,80 +224,96 @@ func resourceSnmpV3UsmUserReadWJnprSess(d *schema.ResourceData, m interface{}, j func resourceSnmpV3UsmUserUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { + clt := m.(*Client) + if clt.fakeUpdateAlso { if err := delSnmpV3UsmUser( - d.Get("name").(string), d.Get("engine_type").(string), d.Get("engine_id").(string), m, nil); err != nil { + d.Get("name").(string), + d.Get("engine_type").(string), + d.Get("engine_id").(string), + clt, nil, + ); err != nil { return diag.FromErr(err) } - if err := setSnmpV3UsmUser(d, m, nil); err != nil { + if err := setSnmpV3UsmUser(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if err := delSnmpV3UsmUser( - d.Get("name").(string), d.Get("engine_type").(string), d.Get("engine_id").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + d.Get("name").(string), + d.Get("engine_type").(string), + d.Get("engine_id").(string), + clt, junSess, + ); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSnmpV3UsmUser(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSnmpV3UsmUser(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_snmp_v3_usm_user", jnprSess) + warns, err := clt.commitConf("update resource junos_snmp_v3_usm_user", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSnmpV3UsmUserReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSnmpV3UsmUserReadWJunSess(d, clt, junSess)...) } func resourceSnmpV3UsmUserDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { + clt := m.(*Client) + if clt.fakeDeleteAlso { if err := delSnmpV3UsmUser( - d.Get("name").(string), d.Get("engine_type").(string), d.Get("engine_id").(string), m, nil); err != nil { + d.Get("name").(string), + d.Get("engine_type").(string), + d.Get("engine_id").(string), + clt, nil, + ); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if err := delSnmpV3UsmUser( - d.Get("name").(string), d.Get("engine_type").(string), d.Get("engine_id").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + d.Get("name").(string), + d.Get("engine_type").(string), + d.Get("engine_id").(string), + clt, junSess, + ); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_snmp_v3_usm_user", jnprSess) + warns, err := clt.commitConf("delete resource junos_snmp_v3_usm_user", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -301,18 +323,18 @@ func resourceSnmpV3UsmUserDelete(ctx context.Context, d *schema.ResourceData, m func resourceSnmpV3UsmUserImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) idSplit := strings.Split(d.Id(), idSeparator) var configImport snmpV3UsmUserOptions switch { case len(idSplit) == 2 && idSplit[0] == "local": - snmpV3UsmUserExists, err := checkSnmpV3UsmUserExists(idSplit[1], idSplit[0], "", m, jnprSess) + snmpV3UsmUserExists, err := checkSnmpV3UsmUserExists(idSplit[1], idSplit[0], "", clt, junSess) if err != nil { return nil, err } @@ -322,7 +344,7 @@ func resourceSnmpV3UsmUserImport(ctx context.Context, d *schema.ResourceData, m configImport.name = idSplit[1] configImport.engineType = idSplit[0] case len(idSplit) == 3 && idSplit[0] == "remote": - snmpV3UsmUserExists, err := checkSnmpV3UsmUserExists(idSplit[2], idSplit[0], idSplit[1], m, jnprSess) + snmpV3UsmUserExists, err := checkSnmpV3UsmUserExists(idSplit[2], idSplit[0], idSplit[1], clt, junSess) if err != nil { return nil, err } @@ -337,7 +359,7 @@ func resourceSnmpV3UsmUserImport(ctx context.Context, d *schema.ResourceData, m return nil, fmt.Errorf("can't find snmp v3 usm user with id "+ "'%v' (id must be local%s or remote%s%s)", d.Id(), idSeparator, idSeparator, idSeparator) } - snmpV3UsmUserOptions, err := readSnmpV3UsmUser(configImport, m, jnprSess) + snmpV3UsmUserOptions, err := readSnmpV3UsmUser(configImport, clt, junSess) if err != nil { return nil, err } @@ -348,11 +370,10 @@ func resourceSnmpV3UsmUserImport(ctx context.Context, d *schema.ResourceData, m return result, nil } -func checkSnmpV3UsmUserExists(name, engineType, engineID string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) +func checkSnmpV3UsmUserExists(name, engineType, engineID string, clt *Client, junSess *junosSession) (bool, error) { if engineType == "local" { - showConfig, err := sess.command(cmdShowConfig+ - "snmp v3 usm local-engine user \""+name+"\""+pipeDisplaySet, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "snmp v3 usm local-engine user \""+name+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -360,8 +381,8 @@ func checkSnmpV3UsmUserExists(name, engineType, engineID string, m interface{}, return false, nil } } else { - showConfig, err := sess.command(cmdShowConfig+ - "snmp v3 usm remote-engine \""+engineID+"\" user \""+name+"\""+pipeDisplaySet, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "snmp v3 usm remote-engine \""+engineID+"\" user \""+name+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -373,9 +394,7 @@ func checkSnmpV3UsmUserExists(name, engineType, engineID string, m interface{}, return true, nil } -func setSnmpV3UsmUser(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) - +func setSnmpV3UsmUser(d *schema.ResourceData, clt *Client, junSess *junosSession) error { setPrefix := "set snmp v3 usm local-engine user \"" + d.Get("name").(string) + "\" " if d.Get("engine_type").(string) != "local" { engineID := d.Get("engine_id").(string) @@ -433,12 +452,11 @@ func setSnmpV3UsmUser(d *schema.ResourceData, m interface{}, jnprSess *NetconfOb configSet = append(configSet, setPrefix+"privacy-none") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSnmpV3UsmUser(confSrc snmpV3UsmUserOptions, m interface{}, jnprSess *NetconfObject, +func readSnmpV3UsmUser(confSrc snmpV3UsmUserOptions, clt *Client, junSess *junosSession, ) (snmpV3UsmUserOptions, error) { - sess := m.(*Session) var confRead snmpV3UsmUserOptions showCommand := cmdShowConfig + "snmp v3 usm local-engine user \"" + confSrc.name + "\"" + pipeDisplaySetRelative @@ -446,7 +464,7 @@ func readSnmpV3UsmUser(confSrc snmpV3UsmUserOptions, m interface{}, jnprSess *Ne showCommand = cmdShowConfig + "snmp v3 usm remote-engine \"" + confSrc.engineID + "\" user \"" + confSrc.name + "\"" + pipeDisplaySetRelative } - showConfig, err := sess.command(showCommand, jnprSess) + showConfig, err := clt.command(showCommand, junSess) if err != nil { return confRead, err } @@ -534,8 +552,7 @@ func readSnmpV3UsmUser(confSrc snmpV3UsmUserOptions, m interface{}, jnprSess *Ne return confRead, nil } -func delSnmpV3UsmUser(name, engineType, engineID string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSnmpV3UsmUser(name, engineType, engineID string, clt *Client, junSess *junosSession) error { var configSet []string if engineType == "local" { configSet = append(configSet, "delete snmp v3 usm local-engine user \""+name+"\"") @@ -543,7 +560,7 @@ func delSnmpV3UsmUser(name, engineType, engineID string, m interface{}, jnprSess configSet = append(configSet, "delete snmp v3 usm remote-engine \""+engineID+"\" user \""+name+"\"") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSnmpV3UsmUserData(d *schema.ResourceData, snmpV3UsmUserOptions snmpV3UsmUserOptions) { diff --git a/junos/resource_snmp_v3_vacm_accessgroup.go b/junos/resource_snmp_v3_vacm_accessgroup.go index 4d4b75ff..8ea8c731 100644 --- a/junos/resource_snmp_v3_vacm_accessgroup.go +++ b/junos/resource_snmp_v3_vacm_accessgroup.go @@ -129,50 +129,50 @@ func resourceSnmpV3VacmAccessGroup() *schema.Resource { } func resourceSnmpV3VacmAccessGroupCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSnmpV3VacmAccessGroup(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSnmpV3VacmAccessGroup(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - snmpV3VacmAccessGroupExists, err := checkSnmpV3VacmAccessGroupExists(d.Get("name").(string), m, jnprSess) + snmpV3VacmAccessGroupExists, err := checkSnmpV3VacmAccessGroupExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if snmpV3VacmAccessGroupExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf( "snmp v3 vacm access group %v already exists", d.Get("name").(string)))...) } - if err := setSnmpV3VacmAccessGroup(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSnmpV3VacmAccessGroup(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_snmp_v3_vacm_accessgroup", jnprSess) + warns, err := clt.commitConf("create resource junos_snmp_v3_vacm_accessgroup", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - snmpV3VacmAccessGroupExists, err = checkSnmpV3VacmAccessGroupExists(d.Get("name").(string), m, jnprSess) + snmpV3VacmAccessGroupExists, err = checkSnmpV3VacmAccessGroupExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -183,24 +183,24 @@ func resourceSnmpV3VacmAccessGroupCreate(ctx context.Context, d *schema.Resource "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceSnmpV3VacmAccessGroupReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSnmpV3VacmAccessGroupReadWJunSess(d, clt, junSess)...) } func resourceSnmpV3VacmAccessGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSnmpV3VacmAccessGroupReadWJnprSess(d, m, jnprSess) + return resourceSnmpV3VacmAccessGroupReadWJunSess(d, clt, junSess) } -func resourceSnmpV3VacmAccessGroupReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSnmpV3VacmAccessGroupReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - snmpV3VacmAccessGroupOptions, err := readSnmpV3VacmAccessGroup(d.Get("name").(string), m, jnprSess) + snmpV3VacmAccessGroupOptions, err := readSnmpV3VacmAccessGroup(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -216,76 +216,76 @@ func resourceSnmpV3VacmAccessGroupReadWJnprSess(d *schema.ResourceData, m interf func resourceSnmpV3VacmAccessGroupUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSnmpV3VacmAccessGroup(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSnmpV3VacmAccessGroup(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSnmpV3VacmAccessGroup(d, m, nil); err != nil { + if err := setSnmpV3VacmAccessGroup(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSnmpV3VacmAccessGroup(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSnmpV3VacmAccessGroup(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSnmpV3VacmAccessGroup(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSnmpV3VacmAccessGroup(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_snmp_v3_vacm_accessgroup", jnprSess) + warns, err := clt.commitConf("update resource junos_snmp_v3_vacm_accessgroup", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSnmpV3VacmAccessGroupReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSnmpV3VacmAccessGroupReadWJunSess(d, clt, junSess)...) } func resourceSnmpV3VacmAccessGroupDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSnmpV3VacmAccessGroup(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSnmpV3VacmAccessGroup(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSnmpV3VacmAccessGroup(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSnmpV3VacmAccessGroup(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_snmp_v3_vacm_accessgroup", jnprSess) + warns, err := clt.commitConf("delete resource junos_snmp_v3_vacm_accessgroup", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -295,22 +295,22 @@ func resourceSnmpV3VacmAccessGroupDelete(ctx context.Context, d *schema.Resource func resourceSnmpV3VacmAccessGroupImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - snmpV3VacmAccessGroupExists, err := checkSnmpV3VacmAccessGroupExists(d.Id(), m, jnprSess) + snmpV3VacmAccessGroupExists, err := checkSnmpV3VacmAccessGroupExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !snmpV3VacmAccessGroupExists { return nil, fmt.Errorf("don't find snmp v3 vacm access group with id '%v' (id must be )", d.Id()) } - snmpV3VacmAccessGroupOptions, err := readSnmpV3VacmAccessGroup(d.Id(), m, jnprSess) + snmpV3VacmAccessGroupOptions, err := readSnmpV3VacmAccessGroup(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -321,9 +321,8 @@ func resourceSnmpV3VacmAccessGroupImport(ctx context.Context, d *schema.Resource return result, nil } -func checkSnmpV3VacmAccessGroupExists(name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"snmp v3 vacm access group \""+name+"\""+pipeDisplaySet, jnprSess) +func checkSnmpV3VacmAccessGroupExists(name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"snmp v3 vacm access group \""+name+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -334,9 +333,7 @@ func checkSnmpV3VacmAccessGroupExists(name string, m interface{}, jnprSess *Netc return true, nil } -func setSnmpV3VacmAccessGroup(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) - +func setSnmpV3VacmAccessGroup(d *schema.ResourceData, clt *Client, junSess *junosSession) error { setPrefix := "set snmp v3 vacm access group \"" + d.Get("name").(string) + "\" " configSet := make([]string, 0) @@ -407,16 +404,15 @@ func setSnmpV3VacmAccessGroup(d *schema.ResourceData, m interface{}, jnprSess *N } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSnmpV3VacmAccessGroup(name string, m interface{}, jnprSess *NetconfObject, +func readSnmpV3VacmAccessGroup(name string, clt *Client, junSess *junosSession, ) (snmpV3VacmAccessGroupOptions, error) { - sess := m.(*Session) var confRead snmpV3VacmAccessGroupOptions - showConfig, err := sess.command(cmdShowConfig+ - "snmp v3 vacm access group \""+name+"\""+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+ + "snmp v3 vacm access group \""+name+"\""+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -496,12 +492,10 @@ func readSnmpV3VacmAccessGroupContextPrefixConfig(itemTrim string, config map[st } } -func delSnmpV3VacmAccessGroup(name string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) - +func delSnmpV3VacmAccessGroup(name string, clt *Client, junSess *junosSession) error { configSet := []string{"delete snmp v3 vacm access group \"" + name + "\""} - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSnmpV3VacmAccessGroupData(d *schema.ResourceData, snmpV3VacmAccessGroupOptions snmpV3VacmAccessGroupOptions) { diff --git a/junos/resource_snmp_v3_vacm_securitytogroup.go b/junos/resource_snmp_v3_vacm_securitytogroup.go index 0418737e..cacda969 100644 --- a/junos/resource_snmp_v3_vacm_securitytogroup.go +++ b/junos/resource_snmp_v3_vacm_securitytogroup.go @@ -48,53 +48,57 @@ func resourceSnmpV3VacmSecurityToGroup() *schema.Resource { func resourceSnmpV3VacmSecurityToGroupCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSnmpV3VacmSecurityToGroup(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSnmpV3VacmSecurityToGroup(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("model").(string) + idSeparator + d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics snmpV3VacmSecurityToGroupExists, err := checkSnmpV3VacmSecurityToGroupExists( - d.Get("model").(string), d.Get("name").(string), m, jnprSess) + d.Get("model").(string), + d.Get("name").(string), + clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if snmpV3VacmSecurityToGroupExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf( "snmp v3 vacm security-to-group security-model %v security-name %v already exists", d.Get("model").(string), d.Get("name").(string)))...) } - if err := setSnmpV3VacmSecurityToGroup(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSnmpV3VacmSecurityToGroup(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_snmp_v3_vacm_securitytogroup", jnprSess) + warns, err := clt.commitConf("create resource junos_snmp_v3_vacm_securitytogroup", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } snmpV3VacmSecurityToGroupExists, err = checkSnmpV3VacmSecurityToGroupExists( - d.Get("model").(string), d.Get("name").(string), m, jnprSess) + d.Get("model").(string), + d.Get("name").(string), + clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -106,26 +110,28 @@ func resourceSnmpV3VacmSecurityToGroupCreate(ctx context.Context, d *schema.Reso "=> check your config", d.Get("model").(string), d.Get("name").(string)))...) } - return append(diagWarns, resourceSnmpV3VacmSecurityToGroupReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSnmpV3VacmSecurityToGroupReadWJunSess(d, clt, junSess)...) } func resourceSnmpV3VacmSecurityToGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSnmpV3VacmSecurityToGroupReadWJnprSess(d, m, jnprSess) + return resourceSnmpV3VacmSecurityToGroupReadWJunSess(d, clt, junSess) } -func resourceSnmpV3VacmSecurityToGroupReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSnmpV3VacmSecurityToGroupReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() snmpV3VacmSecurityToGroupOptions, err := readSnmpV3VacmSecurityToGroup( - d.Get("model").(string), d.Get("name").(string), m, jnprSess) + d.Get("model").(string), + d.Get("name").(string), + clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -142,77 +148,77 @@ func resourceSnmpV3VacmSecurityToGroupReadWJnprSess(d *schema.ResourceData, m in func resourceSnmpV3VacmSecurityToGroupUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSnmpV3VacmSecurityToGroup(d.Get("model").(string), d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSnmpV3VacmSecurityToGroup(d.Get("model").(string), d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSnmpV3VacmSecurityToGroup(d, m, nil); err != nil { + if err := setSnmpV3VacmSecurityToGroup(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSnmpV3VacmSecurityToGroup(d.Get("model").(string), d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSnmpV3VacmSecurityToGroup(d.Get("model").(string), d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSnmpV3VacmSecurityToGroup(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSnmpV3VacmSecurityToGroup(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_snmp_v3_vacm_securitytogroup", jnprSess) + warns, err := clt.commitConf("update resource junos_snmp_v3_vacm_securitytogroup", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSnmpV3VacmSecurityToGroupReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSnmpV3VacmSecurityToGroupReadWJunSess(d, clt, junSess)...) } func resourceSnmpV3VacmSecurityToGroupDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSnmpV3VacmSecurityToGroup(d.Get("model").(string), d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSnmpV3VacmSecurityToGroup(d.Get("model").(string), d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSnmpV3VacmSecurityToGroup(d.Get("model").(string), d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSnmpV3VacmSecurityToGroup(d.Get("model").(string), d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_snmp_v3_vacm_securitytogroup", jnprSess) + warns, err := clt.commitConf("delete resource junos_snmp_v3_vacm_securitytogroup", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -222,12 +228,12 @@ func resourceSnmpV3VacmSecurityToGroupDelete(ctx context.Context, d *schema.Reso func resourceSnmpV3VacmSecurityToGroupImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) idSplit := strings.Split(d.Id(), idSeparator) if len(idSplit) != 2 { @@ -238,7 +244,7 @@ func resourceSnmpV3VacmSecurityToGroupImport(ctx context.Context, d *schema.Reso return nil, fmt.Errorf("can't find snmp v3 vacm security-to-group "+ "with id '%v' (id must be %s)", d.Id(), idSeparator) } - snmpV3VacmSecurityToGroupExists, err := checkSnmpV3VacmSecurityToGroupExists(idSplit[0], idSplit[1], m, jnprSess) + snmpV3VacmSecurityToGroupExists, err := checkSnmpV3VacmSecurityToGroupExists(idSplit[0], idSplit[1], clt, junSess) if err != nil { return nil, err } @@ -246,7 +252,7 @@ func resourceSnmpV3VacmSecurityToGroupImport(ctx context.Context, d *schema.Reso return nil, fmt.Errorf("don't find snmp v3 vacm security-to-group "+ "with id '%v' (id must be %s)", d.Id(), idSeparator) } - snmpV3VacmSecurityToGroupOptions, err := readSnmpV3VacmSecurityToGroup(idSplit[0], idSplit[1], m, jnprSess) + snmpV3VacmSecurityToGroupOptions, err := readSnmpV3VacmSecurityToGroup(idSplit[0], idSplit[1], clt, junSess) if err != nil { return nil, err } @@ -257,10 +263,9 @@ func resourceSnmpV3VacmSecurityToGroupImport(ctx context.Context, d *schema.Reso return result, nil } -func checkSnmpV3VacmSecurityToGroupExists(model, name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"snmp v3 vacm security-to-group "+ - "security-model "+model+" security-name \""+name+"\""+pipeDisplaySet, jnprSess) +func checkSnmpV3VacmSecurityToGroupExists(model, name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"snmp v3 vacm security-to-group "+ + "security-model "+model+" security-name \""+name+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -271,9 +276,7 @@ func checkSnmpV3VacmSecurityToGroupExists(model, name string, m interface{}, jnp return true, nil } -func setSnmpV3VacmSecurityToGroup(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) - +func setSnmpV3VacmSecurityToGroup(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 1) if group := d.Get("group").(string); group != "" { configSet[0] = "set snmp v3 vacm security-to-group" + @@ -284,16 +287,15 @@ func setSnmpV3VacmSecurityToGroup(d *schema.ResourceData, m interface{}, jnprSes return fmt.Errorf("group need to be set") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSnmpV3VacmSecurityToGroup(model, name string, m interface{}, jnprSess *NetconfObject, +func readSnmpV3VacmSecurityToGroup(model, name string, clt *Client, junSess *junosSession, ) (snmpV3VacmSecurityToGroupOptions, error) { - sess := m.(*Session) var confRead snmpV3VacmSecurityToGroupOptions - showConfig, err := sess.command(cmdShowConfig+"snmp v3 vacm security-to-group "+ - "security-model "+model+" security-name \""+name+"\""+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"snmp v3 vacm security-to-group "+ + "security-model "+model+" security-name \""+name+"\""+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -316,12 +318,11 @@ func readSnmpV3VacmSecurityToGroup(model, name string, m interface{}, jnprSess * return confRead, nil } -func delSnmpV3VacmSecurityToGroup(model, name string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSnmpV3VacmSecurityToGroup(model, name string, clt *Client, junSess *junosSession) error { configSet := []string{"delete snmp v3 vacm security-to-group " + "security-model " + model + " security-name \"" + name + "\""} - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSnmpV3VacmSecurityToGroupData( diff --git a/junos/resource_snmp_view.go b/junos/resource_snmp_view.go index 513fe7a9..839d79bd 100644 --- a/junos/resource_snmp_view.go +++ b/junos/resource_snmp_view.go @@ -47,49 +47,49 @@ func resourceSnmpView() *schema.Resource { } func resourceSnmpViewCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSnmpView(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSnmpView(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - snmpViewExists, err := checkSnmpViewExists(d.Get("name").(string), m, jnprSess) + snmpViewExists, err := checkSnmpViewExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if snmpViewExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("snmp view %v already exists", d.Get("name").(string)))...) } - if err := setSnmpView(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSnmpView(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_snmp_view", jnprSess) + warns, err := clt.commitConf("create resource junos_snmp_view", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - snmpViewExists, err = checkSnmpViewExists(d.Get("name").(string), m, jnprSess) + snmpViewExists, err = checkSnmpViewExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -100,24 +100,24 @@ func resourceSnmpViewCreate(ctx context.Context, d *schema.ResourceData, m inter "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceSnmpViewReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSnmpViewReadWJunSess(d, clt, junSess)...) } func resourceSnmpViewRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSnmpViewReadWJnprSess(d, m, jnprSess) + return resourceSnmpViewReadWJunSess(d, clt, junSess) } -func resourceSnmpViewReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSnmpViewReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - snmpViewOptions, err := readSnmpView(d.Get("name").(string), m, jnprSess) + snmpViewOptions, err := readSnmpView(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -133,76 +133,76 @@ func resourceSnmpViewReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSe func resourceSnmpViewUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSnmpView(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSnmpView(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSnmpView(d, m, nil); err != nil { + if err := setSnmpView(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSnmpView(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSnmpView(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSnmpView(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSnmpView(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_snmp_view", jnprSess) + warns, err := clt.commitConf("update resource junos_snmp_view", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSnmpViewReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSnmpViewReadWJunSess(d, clt, junSess)...) } func resourceSnmpViewDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSnmpView(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSnmpView(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSnmpView(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSnmpView(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_snmp_view", jnprSess) + warns, err := clt.commitConf("delete resource junos_snmp_view", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -212,22 +212,22 @@ func resourceSnmpViewDelete(ctx context.Context, d *schema.ResourceData, m inter func resourceSnmpViewImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - snmpViewExists, err := checkSnmpViewExists(d.Id(), m, jnprSess) + snmpViewExists, err := checkSnmpViewExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !snmpViewExists { return nil, fmt.Errorf("don't find snmp view with id '%v' (id must be )", d.Id()) } - snmpViewOptions, err := readSnmpView(d.Id(), m, jnprSess) + snmpViewOptions, err := readSnmpView(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -238,9 +238,8 @@ func resourceSnmpViewImport(ctx context.Context, d *schema.ResourceData, m inter return result, nil } -func checkSnmpViewExists(name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"snmp view \""+name+"\""+pipeDisplaySet, jnprSess) +func checkSnmpViewExists(name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"snmp view \""+name+"\""+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -251,9 +250,7 @@ func checkSnmpViewExists(name string, m interface{}, jnprSess *NetconfObject) (b return true, nil } -func setSnmpView(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) - +func setSnmpView(d *schema.ResourceData, clt *Client, junSess *junosSession) error { setPrefix := "set snmp view \"" + d.Get("name").(string) + "\" " configSet := make([]string, 0) @@ -264,14 +261,13 @@ func setSnmpView(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) configSet = append(configSet, setPrefix+"oid "+v+" exclude") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSnmpView(name string, m interface{}, jnprSess *NetconfObject) (snmpViewOptions, error) { - sess := m.(*Session) +func readSnmpView(name string, clt *Client, junSess *junosSession) (snmpViewOptions, error) { var confRead snmpViewOptions - showConfig, err := sess.command(cmdShowConfig+"snmp view \""+name+"\""+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"snmp view \""+name+"\""+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -300,11 +296,10 @@ func readSnmpView(name string, m interface{}, jnprSess *NetconfObject) (snmpView return confRead, nil } -func delSnmpView(name string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSnmpView(name string, clt *Client, junSess *junosSession) error { configSet := []string{"delete snmp view \"" + name + "\""} - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSnmpViewData(d *schema.ResourceData, snmpViewOptions snmpViewOptions) { diff --git a/junos/resource_static_route.go b/junos/resource_static_route.go index 83233d77..adc4e9f4 100644 --- a/junos/resource_static_route.go +++ b/junos/resource_static_route.go @@ -207,65 +207,69 @@ func resourceStaticRoute() *schema.Resource { } func resourceStaticRouteCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setStaticRoute(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setStaticRoute(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("destination").(string) + idSeparator + d.Get("routing_instance").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if d.Get("routing_instance").(string) != defaultW { - instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !instanceExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("routing instance %v doesn't exist", d.Get("routing_instance").(string)))...) } } - staticRouteExists, err := checkStaticRouteExists(d.Get("destination").(string), d.Get("routing_instance").(string), - m, jnprSess) + staticRouteExists, err := checkStaticRouteExists( + d.Get("destination").(string), + d.Get("routing_instance").(string), + clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if staticRouteExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("static route %v already exists on table %s", d.Get("destination").(string), d.Get("routing_instance").(string)))...) } - if err := setStaticRoute(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setStaticRoute(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_static_route", jnprSess) + warns, err := clt.commitConf("create resource junos_static_route", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - staticRouteExists, err = checkStaticRouteExists(d.Get("destination").(string), d.Get("routing_instance").(string), - m, jnprSess) + staticRouteExists, err = checkStaticRouteExists( + d.Get("destination").(string), + d.Get("routing_instance").(string), + clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -276,24 +280,26 @@ func resourceStaticRouteCreate(ctx context.Context, d *schema.ResourceData, m in "=> check your config", d.Get("destination").(string), d.Get("routing_instance").(string)))...) } - return append(diagWarns, resourceStaticRouteReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceStaticRouteReadWJunSess(d, clt, junSess)...) } func resourceStaticRouteRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceStaticRouteReadWJnprSess(d, m, jnprSess) + return resourceStaticRouteReadWJunSess(d, clt, junSess) } -func resourceStaticRouteReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) diag.Diagnostics { +func resourceStaticRouteReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession) diag.Diagnostics { mutex.Lock() - staticRouteOptions, err := readStaticRoute(d.Get("destination").(string), d.Get("routing_instance").(string), - m, jnprSess) + staticRouteOptions, err := readStaticRoute( + d.Get("destination").(string), + d.Get("routing_instance").(string), + clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -309,77 +315,85 @@ func resourceStaticRouteReadWJnprSess(d *schema.ResourceData, m interface{}, jnp func resourceStaticRouteUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delStaticRoute(d.Get("destination").(string), d.Get("routing_instance").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delStaticRoute(d.Get("destination").(string), d.Get("routing_instance").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setStaticRoute(d, m, nil); err != nil { + if err := setStaticRoute(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delStaticRoute(d.Get("destination").(string), d.Get("routing_instance").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delStaticRoute( + d.Get("destination").(string), + d.Get("routing_instance").(string), + clt, junSess, + ); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setStaticRoute(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setStaticRoute(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_static_route", jnprSess) + warns, err := clt.commitConf("update resource junos_static_route", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceStaticRouteReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceStaticRouteReadWJunSess(d, clt, junSess)...) } func resourceStaticRouteDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delStaticRoute(d.Get("destination").(string), d.Get("routing_instance").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delStaticRoute(d.Get("destination").(string), d.Get("routing_instance").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delStaticRoute(d.Get("destination").(string), d.Get("routing_instance").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delStaticRoute( + d.Get("destination").(string), + d.Get("routing_instance").(string), + clt, junSess, + ); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_static_route", jnprSess) + warns, err := clt.commitConf("delete resource junos_static_route", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -389,18 +403,18 @@ func resourceStaticRouteDelete(ctx context.Context, d *schema.ResourceData, m in func resourceStaticRouteImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) idSplit := strings.Split(d.Id(), idSeparator) if len(idSplit) < 2 { return nil, fmt.Errorf("missing element(s) in id with separator %v", idSeparator) } - staticRouteExists, err := checkStaticRouteExists(idSplit[0], idSplit[1], m, jnprSess) + staticRouteExists, err := checkStaticRouteExists(idSplit[0], idSplit[1], clt, junSess) if err != nil { return nil, err } @@ -408,7 +422,7 @@ func resourceStaticRouteImport(ctx context.Context, d *schema.ResourceData, m in return nil, fmt.Errorf("don't find static route with id '%v' (id must be "+ ""+idSeparator+")", d.Id()) } - staticRouteOptions, err := readStaticRoute(idSplit[0], idSplit[1], m, jnprSess) + staticRouteOptions, err := readStaticRoute(idSplit[0], idSplit[1], clt, junSess) if err != nil { return nil, err } @@ -419,34 +433,33 @@ func resourceStaticRouteImport(ctx context.Context, d *schema.ResourceData, m in return result, nil } -func checkStaticRouteExists(destination, instance string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) +func checkStaticRouteExists(destination, instance string, clt *Client, junSess *junosSession) (bool, error) { var showConfig string var err error if instance == defaultW { if !strings.Contains(destination, ":") { - showConfig, err = sess.command(cmdShowConfig+ - "routing-options static route "+destination+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "routing-options static route "+destination+pipeDisplaySet, junSess) if err != nil { return false, err } } else { - showConfig, err = sess.command(cmdShowConfig+ - "routing-options rib inet6.0 static route "+destination+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "routing-options rib inet6.0 static route "+destination+pipeDisplaySet, junSess) if err != nil { return false, err } } } else { if !strings.Contains(destination, ":") { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+instance+" "+ - "routing-options static route "+destination+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+instance+" "+ + "routing-options static route "+destination+pipeDisplaySet, junSess) if err != nil { return false, err } } else { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+instance+" "+ - "routing-options rib "+instance+".inet6.0 static route "+destination+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+instance+" "+ + "routing-options rib "+instance+".inet6.0 static route "+destination+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -460,8 +473,7 @@ func checkStaticRouteExists(destination, instance string, m interface{}, jnprSes return true, nil } -func setStaticRoute(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setStaticRoute(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) var setPrefix string @@ -576,30 +588,29 @@ func setStaticRoute(d *schema.ResourceData, m interface{}, jnprSess *NetconfObje configSet = append(configSet, setPrefix+" no-retain") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readStaticRoute(destination, instance string, m interface{}, jnprSess *NetconfObject) (staticRouteOptions, error) { - sess := m.(*Session) +func readStaticRoute(destination, instance string, clt *Client, junSess *junosSession) (staticRouteOptions, error) { var confRead staticRouteOptions var showConfig string var err error if instance == defaultW { if !strings.Contains(destination, ":") { - showConfig, err = sess.command(cmdShowConfig+ - "routing-options static route "+destination+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "routing-options static route "+destination+pipeDisplaySetRelative, junSess) } else { - showConfig, err = sess.command(cmdShowConfig+ - "routing-options rib inet6.0 static route "+destination+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "routing-options rib inet6.0 static route "+destination+pipeDisplaySetRelative, junSess) } } else { if !strings.Contains(destination, ":") { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+instance+" "+ - "routing-options static route "+destination+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+instance+" "+ + "routing-options static route "+destination+pipeDisplaySetRelative, junSess) } else { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+instance+" "+ - "routing-options rib "+instance+".inet6.0 static route "+destination+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+instance+" "+ + "routing-options rib "+instance+".inet6.0 static route "+destination+pipeDisplaySetRelative, junSess) } } if err != nil { @@ -705,8 +716,7 @@ func readStaticRoute(destination, instance string, m interface{}, jnprSess *Netc return confRead, nil } -func delStaticRoute(destination, instance string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delStaticRoute(destination, instance string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) switch { case instance == defaultW && !strings.Contains(destination, ":"): @@ -721,7 +731,7 @@ func delStaticRoute(destination, instance string, m interface{}, jnprSess *Netco "routing-options rib "+instance+".inet6.0 static route "+destination) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillStaticRouteData(d *schema.ResourceData, staticRouteOptions staticRouteOptions) { diff --git a/junos/resource_switch_options.go b/junos/resource_switch_options.go index f0962e17..4722f8f2 100644 --- a/junos/resource_switch_options.go +++ b/junos/resource_switch_options.go @@ -45,56 +45,56 @@ func resourceSwitchOptions() *schema.Resource { } func resourceSwitchOptionsCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSwitchOptions(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSwitchOptions(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId("switch_options") return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := setSwitchOptions(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSwitchOptions(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_switch_options", jnprSess) + warns, err := clt.commitConf("create resource junos_switch_options", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.SetId("switch_options") - return append(diagWarns, resourceSwitchOptionsReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSwitchOptionsReadWJunSess(d, clt, junSess)...) } func resourceSwitchOptionsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSwitchOptionsReadWJnprSess(d, m, jnprSess) + return resourceSwitchOptionsReadWJunSess(d, clt, junSess) } -func resourceSwitchOptionsReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSwitchOptionsReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - switchOptionsOptions, err := readSwitchOptions(m, jnprSess) + switchOptionsOptions, err := readSwitchOptions(clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -106,77 +106,77 @@ func resourceSwitchOptionsReadWJnprSess(d *schema.ResourceData, m interface{}, j func resourceSwitchOptionsUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSwitchOptions(m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSwitchOptions(clt, nil); err != nil { return diag.FromErr(err) } - if err := setSwitchOptions(d, m, nil); err != nil { + if err := setSwitchOptions(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSwitchOptions(m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSwitchOptions(clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSwitchOptions(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSwitchOptions(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_switch_options", jnprSess) + warns, err := clt.commitConf("update resource junos_switch_options", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSwitchOptionsReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSwitchOptionsReadWJunSess(d, clt, junSess)...) } func resourceSwitchOptionsDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { if d.Get("clean_on_destroy").(bool) { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSwitchOptions(m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSwitchOptions(clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSwitchOptions(m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSwitchOptions(clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_switch_options", jnprSess) + warns, err := clt.commitConf("delete resource junos_switch_options", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -187,14 +187,14 @@ func resourceSwitchOptionsDelete(ctx context.Context, d *schema.ResourceData, m func resourceSwitchOptionsImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - switchOptionsOptions, err := readSwitchOptions(m, jnprSess) + switchOptionsOptions, err := readSwitchOptions(clt, junSess) if err != nil { return nil, err } @@ -205,9 +205,7 @@ func resourceSwitchOptionsImport(ctx context.Context, d *schema.ResourceData, m return result, nil } -func setSwitchOptions(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) - +func setSwitchOptions(d *schema.ResourceData, clt *Client, junSess *junosSession) error { setPrefix := "set switch-options " configSet := make([]string, 0) @@ -215,13 +213,12 @@ func setSwitchOptions(d *schema.ResourceData, m interface{}, jnprSess *NetconfOb configSet = append(configSet, setPrefix+"vtep-source-interface "+v) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func delSwitchOptions(m interface{}, jnprSess *NetconfObject) error { +func delSwitchOptions(clt *Client, junSess *junosSession) error { listLinesToDelete := []string{"vtep-source-interface"} - sess := m.(*Session) configSet := make([]string, 0) delPrefix := "delete switch-options " for _, line := range listLinesToDelete { @@ -229,14 +226,13 @@ func delSwitchOptions(m interface{}, jnprSess *NetconfObject) error { delPrefix+line) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSwitchOptions(m interface{}, jnprSess *NetconfObject) (switchOptionsOptions, error) { - sess := m.(*Session) +func readSwitchOptions(clt *Client, junSess *junosSession) (switchOptionsOptions, error) { var confRead switchOptionsOptions - showConfig, err := sess.command(cmdShowConfig+"switch-options"+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"switch-options"+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } diff --git a/junos/resource_system.go b/junos/resource_system.go index 2f72194c..4da6e60a 100644 --- a/junos/resource_system.go +++ b/junos/resource_system.go @@ -1020,55 +1020,55 @@ func resourceSystem() *schema.Resource { } func resourceSystemCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSystem(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSystem(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId("system") return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := setSystem(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSystem(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_system", jnprSess) + warns, err := clt.commitConf("create resource junos_system", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.SetId("system") - return append(diagWarns, resourceSystemReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSystemReadWJunSess(d, clt, junSess)...) } func resourceSystemRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSystemReadWJnprSess(d, m, jnprSess) + return resourceSystemReadWJunSess(d, clt, junSess) } -func resourceSystemReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) diag.Diagnostics { +func resourceSystemReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession) diag.Diagnostics { mutex.Lock() - systemOptions, err := readSystem(m, jnprSess) + systemOptions, err := readSystem(clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -1080,47 +1080,47 @@ func resourceSystemReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess func resourceSystemUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSystem(m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSystem(clt, nil); err != nil { return diag.FromErr(err) } - if err := setSystem(d, m, nil); err != nil { + if err := setSystem(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSystem(m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSystem(clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSystem(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSystem(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_system", jnprSess) + warns, err := clt.commitConf("update resource junos_system", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSystemReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSystemReadWJunSess(d, clt, junSess)...) } func resourceSystemDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { @@ -1129,14 +1129,14 @@ func resourceSystemDelete(ctx context.Context, d *schema.ResourceData, m interfa func resourceSystemImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - systemOptions, err := readSystem(m, jnprSess) + systemOptions, err := readSystem(clt, junSess) if err != nil { return nil, err } @@ -1147,9 +1147,7 @@ func resourceSystemImport(ctx context.Context, d *schema.ResourceData, m interfa return result, nil } -func setSystem(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) - +func setSystem(d *schema.ResourceData, clt *Client, junSess *junosSession) error { setPrefix := "set system " configSet := make([]string, 0) @@ -1200,7 +1198,7 @@ func setSystem(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) e configSet = append(configSet, setPrefix+"inet6-backup-router destination "+dest) } } - if err := setSystemInternetOptions(d, m, jnprSess); err != nil { + if err := setSystemInternetOptions(d, clt, junSess); err != nil { return err } for _, v := range d.Get("license").([]interface{}) { @@ -1232,7 +1230,7 @@ func setSystem(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) e return fmt.Errorf("license block is empty") } } - if err := setSystemLogin(d, m, jnprSess); err != nil { + if err := setSystemLogin(d, clt, junSess); err != nil { return err } if d.Get("max_configuration_rollbacks").(int) != -1 { @@ -1336,10 +1334,10 @@ func setSystem(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) e if d.Get("radius_options_password_protocol_mschapv2").(bool) { configSet = append(configSet, setPrefix+"radius-options password-protocol mschap-v2") } - if err := setSystemServices(d, m, jnprSess); err != nil { + if err := setSystemServices(d, clt, junSess); err != nil { return err } - if err := setSystemSyslog(d, m, jnprSess); err != nil { + if err := setSystemSyslog(d, clt, junSess); err != nil { return err } if d.Get("time_zone").(string) != "" { @@ -1350,11 +1348,10 @@ func setSystem(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) e d.Get("tracing_dest_override_syslog_host").(string)) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func setSystemServices(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSystemServices(d *schema.ResourceData, clt *Client, junSess *junosSession) error { setPrefix := "set system services " configSet := make([]string, 0) @@ -1527,11 +1524,10 @@ func setSystemServices(d *schema.ResourceData, m interface{}, jnprSess *NetconfO } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func setSystemInternetOptions(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSystemInternetOptions(d *schema.ResourceData, clt *Client, junSess *junosSession) error { setPrefix := "set system internet-options " configSet := make([]string, 0) for _, v := range d.Get("internet_options").([]interface{}) { @@ -1633,11 +1629,10 @@ func setSystemInternetOptions(d *schema.ResourceData, m interface{}, jnprSess *N return fmt.Errorf("internet_options block is empty") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func setSystemLogin(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSystemLogin(d *schema.ResourceData, clt *Client, junSess *junosSession) error { setPrefix := "set system login " configSet := make([]string, 0) for _, v := range d.Get("login").([]interface{}) { @@ -1739,11 +1734,10 @@ func setSystemLogin(d *schema.ResourceData, m interface{}, jnprSess *NetconfObje } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func setSystemSyslog(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSystemSyslog(d *schema.ResourceData, clt *Client, junSess *junosSession) error { setPrefix := "set system syslog " configSet := make([]string, 0) for _, syslog := range d.Get("syslog").([]interface{}) { @@ -1850,7 +1844,7 @@ func setSystemSyslog(d *schema.ResourceData, m interface{}, jnprSess *NetconfObj } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func listLinesLogin() []string { @@ -1943,7 +1937,7 @@ func listLinesSyslog() []string { } } -func delSystem(m interface{}, jnprSess *NetconfObject) error { +func delSystem(clt *Client, junSess *junosSession) error { listLinesToDelete := make([]string, 0) listLinesToDelete = append(listLinesToDelete, "archival configuration") listLinesToDelete = append(listLinesToDelete, "authentication-order") @@ -1972,7 +1966,7 @@ func delSystem(m interface{}, jnprSess *NetconfObject) error { listLinesToDelete = append(listLinesToDelete, "tracing destination-override syslog host", ) - sess := m.(*Session) + configSet := make([]string, 0) delPrefix := "delete system " for _, line := range listLinesToDelete { @@ -1980,17 +1974,16 @@ func delSystem(m interface{}, jnprSess *NetconfObject) error { delPrefix+line) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSystem(m interface{}, jnprSess *NetconfObject) (systemOptions, error) { - sess := m.(*Session) +func readSystem(clt *Client, junSess *junosSession) (systemOptions, error) { var confRead systemOptions // default -1 confRead.maxConfigurationRollbacks = -1 confRead.maxConfigurationsOnFlash = -1 - showConfig, err := sess.command(cmdShowConfig+"system"+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"system"+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } diff --git a/junos/resource_system_login_class.go b/junos/resource_system_login_class.go index 3fe0dc25..3d05c361 100644 --- a/junos/resource_system_login_class.go +++ b/junos/resource_system_login_class.go @@ -185,49 +185,49 @@ func resourceSystemLoginClass() *schema.Resource { } func resourceSystemLoginClassCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSystemLoginClass(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSystemLoginClass(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - systemLoginClassExists, err := checkSystemLoginClassExists(d.Get("name").(string), m, jnprSess) + systemLoginClassExists, err := checkSystemLoginClassExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if systemLoginClassExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("system login class %v already exists", d.Get("name").(string)))...) } - if err := setSystemLoginClass(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSystemLoginClass(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_system_login_class", jnprSess) + warns, err := clt.commitConf("create resource junos_system_login_class", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - systemLoginClassExists, err = checkSystemLoginClassExists(d.Get("name").(string), m, jnprSess) + systemLoginClassExists, err = checkSystemLoginClassExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -238,24 +238,24 @@ func resourceSystemLoginClassCreate(ctx context.Context, d *schema.ResourceData, "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceSystemLoginClassReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSystemLoginClassReadWJunSess(d, clt, junSess)...) } func resourceSystemLoginClassRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSystemLoginClassReadWJnprSess(d, m, jnprSess) + return resourceSystemLoginClassReadWJunSess(d, clt, junSess) } -func resourceSystemLoginClassReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSystemLoginClassReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - systemLoginClassOptions, err := readSystemLoginClass(d.Get("name").(string), m, jnprSess) + systemLoginClassOptions, err := readSystemLoginClass(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -271,76 +271,76 @@ func resourceSystemLoginClassReadWJnprSess(d *schema.ResourceData, m interface{} func resourceSystemLoginClassUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSystemLoginClass(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSystemLoginClass(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSystemLoginClass(d, m, nil); err != nil { + if err := setSystemLoginClass(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSystemLoginClass(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSystemLoginClass(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSystemLoginClass(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSystemLoginClass(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_system_login_class", jnprSess) + warns, err := clt.commitConf("update resource junos_system_login_class", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSystemLoginClassReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSystemLoginClassReadWJunSess(d, clt, junSess)...) } func resourceSystemLoginClassDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSystemLoginClass(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSystemLoginClass(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSystemLoginClass(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSystemLoginClass(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_system_login_class", jnprSess) + warns, err := clt.commitConf("delete resource junos_system_login_class", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -350,22 +350,22 @@ func resourceSystemLoginClassDelete(ctx context.Context, d *schema.ResourceData, func resourceSystemLoginClassImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - systemLoginClassExists, err := checkSystemLoginClassExists(d.Id(), m, jnprSess) + systemLoginClassExists, err := checkSystemLoginClassExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !systemLoginClassExists { return nil, fmt.Errorf("don't find system login class with id '%v' (id must be )", d.Id()) } - systemLoginClassOptions, err := readSystemLoginClass(d.Id(), m, jnprSess) + systemLoginClassOptions, err := readSystemLoginClass(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -376,9 +376,8 @@ func resourceSystemLoginClassImport(ctx context.Context, d *schema.ResourceData, return result, nil } -func checkSystemLoginClassExists(name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"system login class "+name+pipeDisplaySet, jnprSess) +func checkSystemLoginClassExists(name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"system login class "+name+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -389,8 +388,7 @@ func checkSystemLoginClassExists(name string, m interface{}, jnprSess *NetconfOb return true, nil } -func setSystemLoginClass(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSystemLoginClass(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set system login class " + d.Get("name").(string) + " " @@ -467,14 +465,13 @@ func setSystemLoginClass(d *schema.ResourceData, m interface{}, jnprSess *Netcon configSet = append(configSet, setPrefix+"tenant \""+d.Get("tenant").(string)+"\"") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSystemLoginClass(name string, m interface{}, jnprSess *NetconfObject) (systemLoginClassOptions, error) { - sess := m.(*Session) +func readSystemLoginClass(name string, clt *Client, junSess *junosSession) (systemLoginClassOptions, error) { var confRead systemLoginClassOptions - showConfig, err := sess.command(cmdShowConfig+"system login class "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"system login class "+name+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -557,12 +554,11 @@ func readSystemLoginClass(name string, m interface{}, jnprSess *NetconfObject) ( return confRead, nil } -func delSystemLoginClass(systemLoginClass string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSystemLoginClass(systemLoginClass string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete system login class "+systemLoginClass) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSystemLoginClassData(d *schema.ResourceData, systemLoginClassOptions systemLoginClassOptions) { diff --git a/junos/resource_system_login_user.go b/junos/resource_system_login_user.go index 293f5c1d..38a93578 100644 --- a/junos/resource_system_login_user.go +++ b/junos/resource_system_login_user.go @@ -94,49 +94,49 @@ func resourceSystemLoginUser() *schema.Resource { } func resourceSystemLoginUserCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSystemLoginUser(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSystemLoginUser(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - systemLoginUserExists, err := checkSystemLoginUserExists(d.Get("name").(string), m, jnprSess) + systemLoginUserExists, err := checkSystemLoginUserExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if systemLoginUserExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("system login user %v already exists", d.Get("name").(string)))...) } - if err := setSystemLoginUser(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSystemLoginUser(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_system_login_user", jnprSess) + warns, err := clt.commitConf("create resource junos_system_login_user", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - systemLoginUserExists, err = checkSystemLoginUserExists(d.Get("name").(string), m, jnprSess) + systemLoginUserExists, err = checkSystemLoginUserExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -147,25 +147,25 @@ func resourceSystemLoginUserCreate(ctx context.Context, d *schema.ResourceData, "=> check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceSystemLoginUserReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSystemLoginUserReadWJunSess(d, clt, junSess)...) } func resourceSystemLoginUserRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSystemLoginUserReadWJnprSess(d, m, jnprSess) + return resourceSystemLoginUserReadWJunSess(d, clt, junSess) } -func resourceSystemLoginUserReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSystemLoginUserReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { plainTextPassword := readSystemLoginUserReadDataPlainTextPassword(d) mutex.Lock() - systemLoginUserOptions, err := readSystemLoginUser(d.Get("name").(string), plainTextPassword, m, jnprSess) + systemLoginUserOptions, err := readSystemLoginUser(d.Get("name").(string), plainTextPassword, clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -181,77 +181,77 @@ func resourceSystemLoginUserReadWJnprSess(d *schema.ResourceData, m interface{}, func resourceSystemLoginUserUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSystemLoginUser(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSystemLoginUser(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSystemLoginUser(d, m, nil); err != nil { + if err := setSystemLoginUser(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSystemLoginUser(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSystemLoginUser(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSystemLoginUser(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSystemLoginUser(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_system_login_user", jnprSess) + warns, err := clt.commitConf("update resource junos_system_login_user", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSystemLoginUserReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSystemLoginUserReadWJunSess(d, clt, junSess)...) } func resourceSystemLoginUserDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSystemLoginUser(d.Get("name").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSystemLoginUser(d.Get("name").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSystemLoginUser(d.Get("name").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSystemLoginUser(d.Get("name").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_system_login_user", jnprSess) + warns, err := clt.commitConf("delete resource junos_system_login_user", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -261,22 +261,22 @@ func resourceSystemLoginUserDelete(ctx context.Context, d *schema.ResourceData, func resourceSystemLoginUserImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - systemLoginUserExists, err := checkSystemLoginUserExists(d.Id(), m, jnprSess) + systemLoginUserExists, err := checkSystemLoginUserExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !systemLoginUserExists { return nil, fmt.Errorf("don't find system login user with id '%v' (id must be )", d.Id()) } - systemLoginUserOptions, err := readSystemLoginUser(d.Id(), "", m, jnprSess) + systemLoginUserOptions, err := readSystemLoginUser(d.Id(), "", clt, junSess) if err != nil { return nil, err } @@ -287,9 +287,8 @@ func resourceSystemLoginUserImport(ctx context.Context, d *schema.ResourceData, return result, nil } -func checkSystemLoginUserExists(name string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"system login user "+name+pipeDisplaySet, jnprSess) +func checkSystemLoginUserExists(name string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"system login user "+name+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -300,8 +299,7 @@ func checkSystemLoginUserExists(name string, m interface{}, jnprSess *NetconfObj return true, nil } -func setSystemLoginUser(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSystemLoginUser(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set system login user " + d.Get("name").(string) + " " @@ -348,7 +346,7 @@ func setSystemLoginUser(d *schema.ResourceData, m interface{}, jnprSess *Netconf configSet = append(configSet, setPrefix+"full-name \""+v+"\"") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func readSystemLoginUserReadDataPlainTextPassword(d *schema.ResourceData) string { @@ -364,12 +362,11 @@ func readSystemLoginUserReadDataPlainTextPassword(d *schema.ResourceData) string return "" } -func readSystemLoginUser(name, plainTextPassword string, m interface{}, jnprSess *NetconfObject, +func readSystemLoginUser(name, plainTextPassword string, clt *Client, junSess *junosSession, ) (systemLoginUserOptions, error) { - sess := m.(*Session) var confRead systemLoginUserOptions - showConfig, err := sess.command(cmdShowConfig+"system login user "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"system login user "+name+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -435,12 +432,11 @@ func readSystemLoginUser(name, plainTextPassword string, m interface{}, jnprSess return confRead, nil } -func delSystemLoginUser(systemLoginUser string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSystemLoginUser(systemLoginUser string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete system login user "+systemLoginUser) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSystemLoginUserData(d *schema.ResourceData, systemLoginUserOptions systemLoginUserOptions) { diff --git a/junos/resource_system_ntp_server.go b/junos/resource_system_ntp_server.go index 0dbf98e8..a9ec9d1e 100644 --- a/junos/resource_system_ntp_server.go +++ b/junos/resource_system_ntp_server.go @@ -59,50 +59,50 @@ func resourceSystemNtpServer() *schema.Resource { } func resourceSystemNtpServerCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSystemNtpServer(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSystemNtpServer(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("address").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - ntpServerExists, err := checkSystemNtpServerExists(d.Get("address").(string), m, jnprSess) + ntpServerExists, err := checkSystemNtpServerExists(d.Get("address").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if ntpServerExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("system ntp server %v already exists", d.Get("address").(string)))...) } - if err := setSystemNtpServer(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSystemNtpServer(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_system_ntp_server", jnprSess) + warns, err := clt.commitConf("create resource junos_system_ntp_server", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - ntpServerExists, err = checkSystemNtpServerExists(d.Get("address").(string), m, jnprSess) + ntpServerExists, err = checkSystemNtpServerExists(d.Get("address").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -113,24 +113,24 @@ func resourceSystemNtpServerCreate(ctx context.Context, d *schema.ResourceData, "=> check your config", d.Get("address").(string)))...) } - return append(diagWarns, resourceSystemNtpServerReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSystemNtpServerReadWJunSess(d, clt, junSess)...) } func resourceSystemNtpServerRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSystemNtpServerReadWJnprSess(d, m, jnprSess) + return resourceSystemNtpServerReadWJunSess(d, clt, junSess) } -func resourceSystemNtpServerReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSystemNtpServerReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - ntpServerOptions, err := readSystemNtpServer(d.Get("address").(string), m, jnprSess) + ntpServerOptions, err := readSystemNtpServer(d.Get("address").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -146,77 +146,77 @@ func resourceSystemNtpServerReadWJnprSess(d *schema.ResourceData, m interface{}, func resourceSystemNtpServerUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSystemNtpServer(d.Get("address").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSystemNtpServer(d.Get("address").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSystemNtpServer(d, m, nil); err != nil { + if err := setSystemNtpServer(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSystemNtpServer(d.Get("address").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSystemNtpServer(d.Get("address").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSystemNtpServer(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSystemNtpServer(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_system_ntp_server", jnprSess) + warns, err := clt.commitConf("update resource junos_system_ntp_server", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSystemNtpServerReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSystemNtpServerReadWJunSess(d, clt, junSess)...) } func resourceSystemNtpServerDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSystemNtpServer(d.Get("address").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSystemNtpServer(d.Get("address").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSystemNtpServer(d.Get("address").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSystemNtpServer(d.Get("address").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_system_ntp_server", jnprSess) + warns, err := clt.commitConf("delete resource junos_system_ntp_server", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -226,22 +226,22 @@ func resourceSystemNtpServerDelete(ctx context.Context, d *schema.ResourceData, func resourceSystemNtpServerImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - ntpServerExists, err := checkSystemNtpServerExists(d.Id(), m, jnprSess) + ntpServerExists, err := checkSystemNtpServerExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !ntpServerExists { return nil, fmt.Errorf("don't find system ntp server with id '%v' (id must be
)", d.Id()) } - ntpServerOptions, err := readSystemNtpServer(d.Id(), m, jnprSess) + ntpServerOptions, err := readSystemNtpServer(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -252,9 +252,8 @@ func resourceSystemNtpServerImport(ctx context.Context, d *schema.ResourceData, return result, nil } -func checkSystemNtpServerExists(address string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"system ntp server "+address+pipeDisplaySet, jnprSess) +func checkSystemNtpServerExists(address string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"system ntp server "+address+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -265,9 +264,7 @@ func checkSystemNtpServerExists(address string, m interface{}, jnprSess *Netconf return true, nil } -func setSystemNtpServer(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) - +func setSystemNtpServer(d *schema.ResourceData, clt *Client, junSess *junosSession) error { setPrefix := "set system ntp server " + d.Get("address").(string) configSet := []string{setPrefix} @@ -284,14 +281,13 @@ func setSystemNtpServer(d *schema.ResourceData, m interface{}, jnprSess *Netconf configSet = append(configSet, setPrefix+" version "+strconv.Itoa(d.Get("version").(int))) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSystemNtpServer(address string, m interface{}, jnprSess *NetconfObject) (ntpServerOptions, error) { - sess := m.(*Session) +func readSystemNtpServer(address string, clt *Client, junSess *junosSession) (ntpServerOptions, error) { var confRead ntpServerOptions - showConfig, err := sess.command(cmdShowConfig+"system ntp server "+address+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"system ntp server "+address+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -329,12 +325,11 @@ func readSystemNtpServer(address string, m interface{}, jnprSess *NetconfObject) return confRead, nil } -func delSystemNtpServer(address string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSystemNtpServer(address string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete system ntp server "+address) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSystemNtpServerData(d *schema.ResourceData, ntpServerOptions ntpServerOptions) { diff --git a/junos/resource_system_radius_server.go b/junos/resource_system_radius_server.go index ea9f5fad..34b86a4c 100644 --- a/junos/resource_system_radius_server.go +++ b/junos/resource_system_radius_server.go @@ -123,50 +123,50 @@ func resourceSystemRadiusServer() *schema.Resource { } func resourceSystemRadiusServerCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSystemRadiusServer(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSystemRadiusServer(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("address").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - radiusServerExists, err := checkSystemRadiusServerExists(d.Get("address").(string), m, jnprSess) + radiusServerExists, err := checkSystemRadiusServerExists(d.Get("address").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if radiusServerExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("system radius-server %v already exists", d.Get("address").(string)))...) } - if err := setSystemRadiusServer(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSystemRadiusServer(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_system_radius_server", jnprSess) + warns, err := clt.commitConf("create resource junos_system_radius_server", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - radiusServerExists, err = checkSystemRadiusServerExists(d.Get("address").(string), m, jnprSess) + radiusServerExists, err = checkSystemRadiusServerExists(d.Get("address").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -177,24 +177,24 @@ func resourceSystemRadiusServerCreate(ctx context.Context, d *schema.ResourceDat "=> check your config", d.Get("address").(string)))...) } - return append(diagWarns, resourceSystemRadiusServerReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSystemRadiusServerReadWJunSess(d, clt, junSess)...) } func resourceSystemRadiusServerRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSystemRadiusServerReadWJnprSess(d, m, jnprSess) + return resourceSystemRadiusServerReadWJunSess(d, clt, junSess) } -func resourceSystemRadiusServerReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSystemRadiusServerReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - radiusServerOptions, err := readSystemRadiusServer(d.Get("address").(string), m, jnprSess) + radiusServerOptions, err := readSystemRadiusServer(d.Get("address").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -210,76 +210,76 @@ func resourceSystemRadiusServerReadWJnprSess(d *schema.ResourceData, m interface func resourceSystemRadiusServerUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSystemRadiusServer(d.Get("address").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSystemRadiusServer(d.Get("address").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSystemRadiusServer(d, m, nil); err != nil { + if err := setSystemRadiusServer(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSystemRadiusServer(d.Get("address").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSystemRadiusServer(d.Get("address").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSystemRadiusServer(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSystemRadiusServer(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_system_radius_server", jnprSess) + warns, err := clt.commitConf("update resource junos_system_radius_server", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSystemRadiusServerReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSystemRadiusServerReadWJunSess(d, clt, junSess)...) } func resourceSystemRadiusServerDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSystemRadiusServer(d.Get("address").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSystemRadiusServer(d.Get("address").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSystemRadiusServer(d.Get("address").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSystemRadiusServer(d.Get("address").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_system_radius_server", jnprSess) + warns, err := clt.commitConf("delete resource junos_system_radius_server", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -289,22 +289,22 @@ func resourceSystemRadiusServerDelete(ctx context.Context, d *schema.ResourceDat func resourceSystemRadiusServerImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - radiusServerExists, err := checkSystemRadiusServerExists(d.Id(), m, jnprSess) + radiusServerExists, err := checkSystemRadiusServerExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !radiusServerExists { return nil, fmt.Errorf("don't find system radius-server with id '%v' (id must be
)", d.Id()) } - radiusServerOptions, err := readSystemRadiusServer(d.Id(), m, jnprSess) + radiusServerOptions, err := readSystemRadiusServer(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -315,9 +315,8 @@ func resourceSystemRadiusServerImport(ctx context.Context, d *schema.ResourceDat return result, nil } -func checkSystemRadiusServerExists(address string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"system radius-server "+address+pipeDisplaySet, jnprSess) +func checkSystemRadiusServerExists(address string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"system radius-server "+address+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -328,9 +327,7 @@ func checkSystemRadiusServerExists(address string, m interface{}, jnprSess *Netc return true, nil } -func setSystemRadiusServer(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) - +func setSystemRadiusServer(d *schema.ResourceData, clt *Client, junSess *junosSession) error { setPrefix := "set system radius-server " + d.Get("address").(string) configSet := []string{setPrefix + " secret \"" + d.Get("secret").(string) + "\""} @@ -383,17 +380,16 @@ func setSystemRadiusServer(d *schema.ResourceData, m interface{}, jnprSess *Netc strconv.Itoa(d.Get("timeout").(int))) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSystemRadiusServer(address string, m interface{}, jnprSess *NetconfObject) (radiusServerOptions, error) { - sess := m.(*Session) +func readSystemRadiusServer(address string, clt *Client, junSess *junosSession) (radiusServerOptions, error) { var confRead radiusServerOptions confRead.accountingRetry = -1 confRead.accountingTimeout = -1 confRead.maxOutstandingRequests = -1 - showConfig, err := sess.command(cmdShowConfig+"system radius-server "+address+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"system radius-server "+address+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -487,12 +483,11 @@ func readSystemRadiusServer(address string, m interface{}, jnprSess *NetconfObje return confRead, nil } -func delSystemRadiusServer(address string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSystemRadiusServer(address string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete system radius-server "+address) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSystemRadiusServerData(d *schema.ResourceData, radiusServerOptions radiusServerOptions) { diff --git a/junos/resource_system_root_authentication.go b/junos/resource_system_root_authentication.go index 56f39048..f939d317 100644 --- a/junos/resource_system_root_authentication.go +++ b/junos/resource_system_root_authentication.go @@ -59,70 +59,70 @@ func resourceSystemRootAuthentication() *schema.Resource { func resourceSystemRootAuthenticationCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { // To be able detect a plain text password not accepted by system if d.Get("plain_text_password").(string) != "" { - if err := delSystemRootAuthenticationPassword(m, nil); err != nil { + if err := delSystemRootAuthenticationPassword(clt, nil); err != nil { return diag.FromErr(err) } } - if err := setSystemRootAuthentication(d, m, nil); err != nil { + if err := setSystemRootAuthentication(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId("system_root_authentication") return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics // To be able detect a plain text password not accepted by system if d.Get("plain_text_password").(string) != "" { - if err := delSystemRootAuthenticationPassword(m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSystemRootAuthenticationPassword(clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } } - if err := setSystemRootAuthentication(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSystemRootAuthentication(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_system_root_authentication", jnprSess) + warns, err := clt.commitConf("create resource junos_system_root_authentication", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.SetId("system_root_authentication") - return append(diagWarns, resourceSystemRootAuthenticationReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSystemRootAuthenticationReadWJunSess(d, clt, junSess)...) } func resourceSystemRootAuthenticationRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSystemRootAuthenticationReadWJnprSess(d, m, jnprSess) + return resourceSystemRootAuthenticationReadWJunSess(d, clt, junSess) } -func resourceSystemRootAuthenticationReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSystemRootAuthenticationReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - systemRootAuthOptions, err := readSystemRootAuthentication(m, jnprSess) + systemRootAuthOptions, err := readSystemRootAuthentication(clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -135,47 +135,47 @@ func resourceSystemRootAuthenticationReadWJnprSess(d *schema.ResourceData, m int func resourceSystemRootAuthenticationUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSystemRootAuthentication(m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSystemRootAuthentication(clt, nil); err != nil { return diag.FromErr(err) } - if err := setSystemRootAuthentication(d, m, nil); err != nil { + if err := setSystemRootAuthentication(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSystemRootAuthentication(m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSystemRootAuthentication(clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSystemRootAuthentication(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSystemRootAuthentication(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_system_root_authentication", jnprSess) + warns, err := clt.commitConf("update resource junos_system_root_authentication", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSystemRootAuthenticationReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSystemRootAuthenticationReadWJunSess(d, clt, junSess)...) } func resourceSystemRootAuthenticationDelete(ctx context.Context, d *schema.ResourceData, m interface{}, @@ -185,15 +185,15 @@ func resourceSystemRootAuthenticationDelete(ctx context.Context, d *schema.Resou func resourceSystemRootAuthenticationImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - systemRootAuthOptions, err := readSystemRootAuthentication(m, jnprSess) + systemRootAuthOptions, err := readSystemRootAuthentication(clt, junSess) if err != nil { return nil, err } @@ -204,8 +204,7 @@ func resourceSystemRootAuthenticationImport(ctx context.Context, d *schema.Resou return result, nil } -func setSystemRootAuthentication(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSystemRootAuthentication(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set system root-authentication " @@ -234,14 +233,13 @@ func setSystemRootAuthentication(d *schema.ResourceData, m interface{}, jnprSess } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSystemRootAuthentication(m interface{}, jnprSess *NetconfObject) (systemRootAuthOptions, error) { - sess := m.(*Session) +func readSystemRootAuthentication(clt *Client, junSess *junosSession) (systemRootAuthOptions, error) { var confRead systemRootAuthOptions - showConfig, err := sess.command(cmdShowConfig+"system root-authentication"+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"system root-authentication"+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -278,20 +276,18 @@ func readSystemRootAuthentication(m interface{}, jnprSess *NetconfObject) (syste return confRead, nil } -func delSystemRootAuthentication(m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSystemRootAuthentication(clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete system root-authentication") - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func delSystemRootAuthenticationPassword(m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSystemRootAuthenticationPassword(clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete system root-authentication encrypted-password") - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSystemRootAuthenticationData(d *schema.ResourceData, systemRootAuthOptions systemRootAuthOptions) { diff --git a/junos/resource_system_services_dhcp_localserver_group.go b/junos/resource_system_services_dhcp_localserver_group.go index 38543d16..2788534c 100644 --- a/junos/resource_system_services_dhcp_localserver_group.go +++ b/junos/resource_system_services_dhcp_localserver_group.go @@ -732,9 +732,9 @@ func schemaSystemServicesDhcpLocalServerGroupOverridesV6() map[string]*schema.Sc func resourceSystemServicesDhcpLocalServerGroupCreate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSystemServicesDhcpLocalServerGroup(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSystemServicesDhcpLocalServerGroup(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string) + @@ -743,38 +743,41 @@ func resourceSystemServicesDhcpLocalServerGroupCreate(ctx context.Context, d *sc return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if d.Get("routing_instance").(string) != defaultW { - instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !instanceExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("routing instance %v doesn't exist", d.Get("routing_instance").(string)))...) } } systemServicesDhcpLocalServerGroupExists, err := checkSystemServicesDhcpLocalServerGroupExists( - d.Get("name").(string), d.Get("routing_instance").(string), d.Get("version").(string), m, jnprSess) + d.Get("name").(string), + d.Get("routing_instance").(string), + d.Get("version").(string), + clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if systemServicesDhcpLocalServerGroupExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) if d.Get("version").(string) == "v6" { return append(diagWarns, diag.FromErr( fmt.Errorf("system services dhcp-local-server dhcpv6 group %v already exists in routing-instance %s", @@ -785,20 +788,23 @@ func resourceSystemServicesDhcpLocalServerGroupCreate(ctx context.Context, d *sc fmt.Errorf("system services dhcp-local-server group %v already exists in routing-instance %s", d.Get("name").(string), d.Get("routing_instance").(string)))...) } - if err := setSystemServicesDhcpLocalServerGroup(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSystemServicesDhcpLocalServerGroup(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_system_services_dhcp_localserver_group", jnprSess) + warns, err := clt.commitConf("create resource junos_system_services_dhcp_localserver_group", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } systemServicesDhcpLocalServerGroupExists, err = checkSystemServicesDhcpLocalServerGroupExists( - d.Get("name").(string), d.Get("routing_instance").(string), d.Get("version").(string), m, jnprSess) + d.Get("name").(string), + d.Get("routing_instance").(string), + d.Get("version").(string), + clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -820,27 +826,30 @@ func resourceSystemServicesDhcpLocalServerGroupCreate(ctx context.Context, d *sc d.Get("name").(string), d.Get("routing_instance").(string)))...) } - return append(diagWarns, resourceSystemServicesDhcpLocalServerGroupReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSystemServicesDhcpLocalServerGroupReadWJunSess(d, clt, junSess)...) } func resourceSystemServicesDhcpLocalServerGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSystemServicesDhcpLocalServerGroupReadWJnprSess(d, m, jnprSess) + return resourceSystemServicesDhcpLocalServerGroupReadWJunSess(d, clt, junSess) } -func resourceSystemServicesDhcpLocalServerGroupReadWJnprSess( - d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSystemServicesDhcpLocalServerGroupReadWJunSess( + d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() systemServicesDhcpLocalServerGroupOptions, err := readSystemServicesDhcpLocalServerGroup( - d.Get("name").(string), d.Get("routing_instance").(string), d.Get("version").(string), m, jnprSess) + d.Get("name").(string), + d.Get("routing_instance").(string), + d.Get("version").(string), + clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -857,81 +866,97 @@ func resourceSystemServicesDhcpLocalServerGroupReadWJnprSess( func resourceSystemServicesDhcpLocalServerGroupUpdate(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { + clt := m.(*Client) + if clt.fakeUpdateAlso { if err := delSystemServicesDhcpLocalServerGroup( - d.Get("name").(string), d.Get("routing_instance").(string), d.Get("version").(string), m, nil); err != nil { + d.Get("name").(string), + d.Get("routing_instance").(string), + d.Get("version").(string), + clt, nil, + ); err != nil { return diag.FromErr(err) } - if err := setSystemServicesDhcpLocalServerGroup(d, m, nil); err != nil { + if err := setSystemServicesDhcpLocalServerGroup(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if err := delSystemServicesDhcpLocalServerGroup( - d.Get("name").(string), d.Get("routing_instance").(string), d.Get("version").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + d.Get("name").(string), + d.Get("routing_instance").(string), + d.Get("version").(string), + clt, junSess, + ); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSystemServicesDhcpLocalServerGroup(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSystemServicesDhcpLocalServerGroup(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_system_services_dhcp_localserver_group", jnprSess) + warns, err := clt.commitConf("update resource junos_system_services_dhcp_localserver_group", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSystemServicesDhcpLocalServerGroupReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSystemServicesDhcpLocalServerGroupReadWJunSess(d, clt, junSess)...) } func resourceSystemServicesDhcpLocalServerGroupDelete(ctx context.Context, d *schema.ResourceData, m interface{}, ) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { + clt := m.(*Client) + if clt.fakeDeleteAlso { if err := delSystemServicesDhcpLocalServerGroup( - d.Get("name").(string), d.Get("routing_instance").(string), d.Get("version").(string), m, nil); err != nil { + d.Get("name").(string), + d.Get("routing_instance").(string), + d.Get("version").(string), + clt, nil, + ); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if err := delSystemServicesDhcpLocalServerGroup( - d.Get("name").(string), d.Get("routing_instance").(string), d.Get("version").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + d.Get("name").(string), + d.Get("routing_instance").(string), + d.Get("version").(string), + clt, junSess, + ); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_system_services_dhcp_localserver_group", jnprSess) + warns, err := clt.commitConf("delete resource junos_system_services_dhcp_localserver_group", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -941,12 +966,12 @@ func resourceSystemServicesDhcpLocalServerGroupDelete(ctx context.Context, d *sc func resourceSystemServicesDhcpLocalServerGroupImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) idSplit := strings.Split(d.Id(), idSeparator) if len(idSplit) < 3 { @@ -957,7 +982,10 @@ func resourceSystemServicesDhcpLocalServerGroupImport(ctx context.Context, d *sc ""+idSeparator+""+idSeparator+")", idSplit[2]) } systemServicesDhcpLocalServerGroupExists, err := checkSystemServicesDhcpLocalServerGroupExists( - idSplit[0], idSplit[1], idSplit[2], m, jnprSess) + idSplit[0], + idSplit[1], + idSplit[2], + clt, junSess) if err != nil { return nil, err } @@ -971,7 +999,10 @@ func resourceSystemServicesDhcpLocalServerGroupImport(ctx context.Context, d *sc ""+idSeparator+""+idSeparator+")", d.Id()) } systemServicesDhcpLocalServerGroupOptions, err := readSystemServicesDhcpLocalServerGroup( - idSplit[0], idSplit[1], idSplit[2], m, jnprSess) + idSplit[0], + idSplit[1], + idSplit[2], + clt, junSess) if err != nil { return nil, err } @@ -983,9 +1014,8 @@ func resourceSystemServicesDhcpLocalServerGroupImport(ctx context.Context, d *sc } func checkSystemServicesDhcpLocalServerGroupExists( - name, instance, version string, m interface{}, jnprSess *NetconfObject, + name, instance, version string, clt *Client, junSess *junosSession, ) (bool, error) { - sess := m.(*Session) var showConfig string var err error showCmd := cmdShowConfig @@ -999,7 +1029,7 @@ func checkSystemServicesDhcpLocalServerGroupExists( showCmd += "group " + name } - showConfig, err = sess.command(showCmd+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(showCmd+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -1011,8 +1041,7 @@ func checkSystemServicesDhcpLocalServerGroupExists( return true, nil } -func setSystemServicesDhcpLocalServerGroup(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setSystemServicesDhcpLocalServerGroup(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := setLS @@ -1283,7 +1312,7 @@ func setSystemServicesDhcpLocalServerGroup(d *schema.ResourceData, m interface{} configSet = append(configSet, setPrefix+"short-cycle-protection lockout-min-time "+strconv.Itoa(v)) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func setSystemServicesDhcpLocalServerGroupInterface( @@ -1504,9 +1533,8 @@ func setSystemServicesDhcpLocalServerGroupFamilyDhcpOverridesV6(overrides map[st return configSet, nil } -func readSystemServicesDhcpLocalServerGroup(name, instance, version string, m interface{}, jnprSess *NetconfObject, +func readSystemServicesDhcpLocalServerGroup(name, instance, version string, clt *Client, junSess *junosSession, ) (systemServicesDhcpLocalServerGroupOptions, error) { - sess := m.(*Session) var confRead systemServicesDhcpLocalServerGroupOptions var showConfig string var err error @@ -1521,7 +1549,7 @@ func readSystemServicesDhcpLocalServerGroup(name, instance, version string, m in showCmd += "group " + name } - showConfig, err = sess.command(showCmd+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(showCmd+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -2049,9 +2077,8 @@ func readSystemServicesDhcpLocalServerGroupOverridesV6(itemTrim string, override return nil } -func delSystemServicesDhcpLocalServerGroup(name, instance, version string, m interface{}, jnprSess *NetconfObject, +func delSystemServicesDhcpLocalServerGroup(name, instance, version string, clt *Client, junSess *junosSession, ) error { - sess := m.(*Session) configSet := make([]string, 0, 1) switch { case instance == defaultW && version == "v6": @@ -2066,7 +2093,7 @@ func delSystemServicesDhcpLocalServerGroup(name, instance, version string, m int "system services dhcp-local-server group "+name) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSystemServicesDhcpLocalServerGroupData( diff --git a/junos/resource_system_syslog_file.go b/junos/resource_system_syslog_file.go index 7680e546..785565d3 100644 --- a/junos/resource_system_syslog_file.go +++ b/junos/resource_system_syslog_file.go @@ -233,50 +233,50 @@ func resourceSystemSyslogFile() *schema.Resource { } func resourceSystemSyslogFileCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSystemSyslogFile(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSystemSyslogFile(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("filename").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - syslogFileExists, err := checkSystemSyslogFileExists(d.Get("filename").(string), m, jnprSess) + syslogFileExists, err := checkSystemSyslogFileExists(d.Get("filename").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if syslogFileExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("system syslog file %v already exists", d.Get("filename").(string)))...) } - if err := setSystemSyslogFile(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSystemSyslogFile(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_system_syslog_file", jnprSess) + warns, err := clt.commitConf("create resource junos_system_syslog_file", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - syslogFileExists, err = checkSystemSyslogFileExists(d.Get("filename").(string), m, jnprSess) + syslogFileExists, err = checkSystemSyslogFileExists(d.Get("filename").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -287,24 +287,24 @@ func resourceSystemSyslogFileCreate(ctx context.Context, d *schema.ResourceData, "=> check your config", d.Get("filename").(string)))...) } - return append(diagWarns, resourceSystemSyslogFileReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSystemSyslogFileReadWJunSess(d, clt, junSess)...) } func resourceSystemSyslogFileRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSystemSyslogFileReadWJnprSess(d, m, jnprSess) + return resourceSystemSyslogFileReadWJunSess(d, clt, junSess) } -func resourceSystemSyslogFileReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSystemSyslogFileReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - syslogFileOptions, err := readSystemSyslogFile(d.Get("filename").(string), m, jnprSess) + syslogFileOptions, err := readSystemSyslogFile(d.Get("filename").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -320,76 +320,76 @@ func resourceSystemSyslogFileReadWJnprSess(d *schema.ResourceData, m interface{} func resourceSystemSyslogFileUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSystemSyslogFile(d.Get("filename").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSystemSyslogFile(d.Get("filename").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSystemSyslogFile(d, m, nil); err != nil { + if err := setSystemSyslogFile(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSystemSyslogFile(d.Get("filename").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSystemSyslogFile(d.Get("filename").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSystemSyslogFile(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSystemSyslogFile(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_system_syslog_file", jnprSess) + warns, err := clt.commitConf("update resource junos_system_syslog_file", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSystemSyslogFileReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSystemSyslogFileReadWJunSess(d, clt, junSess)...) } func resourceSystemSyslogFileDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSystemSyslogFile(d.Get("filename").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSystemSyslogFile(d.Get("filename").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSystemSyslogFile(d.Get("filename").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSystemSyslogFile(d.Get("filename").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_system_syslog_file", jnprSess) + warns, err := clt.commitConf("delete resource junos_system_syslog_file", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -399,22 +399,22 @@ func resourceSystemSyslogFileDelete(ctx context.Context, d *schema.ResourceData, func resourceSystemSyslogFileImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - syslogFileExists, err := checkSystemSyslogFileExists(d.Id(), m, jnprSess) + syslogFileExists, err := checkSystemSyslogFileExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !syslogFileExists { return nil, fmt.Errorf("don't find system syslog file with id '%v' (id must be )", d.Id()) } - syslogFileOptions, err := readSystemSyslogFile(d.Id(), m, jnprSess) + syslogFileOptions, err := readSystemSyslogFile(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -425,9 +425,8 @@ func resourceSystemSyslogFileImport(ctx context.Context, d *schema.ResourceData, return result, nil } -func checkSystemSyslogFileExists(filename string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"system syslog file "+filename+pipeDisplaySet, jnprSess) +func checkSystemSyslogFileExists(filename string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"system syslog file "+filename+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -438,9 +437,7 @@ func checkSystemSyslogFileExists(filename string, m interface{}, jnprSess *Netco return true, nil } -func setSystemSyslogFile(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) - +func setSystemSyslogFile(d *schema.ResourceData, clt *Client, junSess *junosSession) error { setPrefix := "set system syslog file " + d.Get("filename").(string) configSet := make([]string, 0) @@ -564,14 +561,13 @@ func setSystemSyslogFile(d *schema.ResourceData, m interface{}, jnprSess *Netcon } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSystemSyslogFile(filename string, m interface{}, jnprSess *NetconfObject) (syslogFileOptions, error) { - sess := m.(*Session) +func readSystemSyslogFile(filename string, clt *Client, junSess *junosSession) (syslogFileOptions, error) { var confRead syslogFileOptions - showConfig, err := sess.command(cmdShowConfig+"system syslog file "+filename+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"system syslog file "+filename+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -710,12 +706,11 @@ func readSystemSyslogFile(filename string, m interface{}, jnprSess *NetconfObjec return confRead, nil } -func delSystemSyslogFile(filename string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSystemSyslogFile(filename string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete system syslog file "+filename) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSystemSyslogFileData(d *schema.ResourceData, syslogFileOptions syslogFileOptions) { diff --git a/junos/resource_system_syslog_host.go b/junos/resource_system_syslog_host.go index f7a7d88e..30f141c5 100644 --- a/junos/resource_system_syslog_host.go +++ b/junos/resource_system_syslog_host.go @@ -190,49 +190,49 @@ func resourceSystemSyslogHost() *schema.Resource { } func resourceSystemSyslogHostCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setSystemSyslogHost(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setSystemSyslogHost(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("host").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - syslogHostExists, err := checkSystemSyslogHostExists(d.Get("host").(string), m, jnprSess) + syslogHostExists, err := checkSystemSyslogHostExists(d.Get("host").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if syslogHostExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("system syslog host %v already exists", d.Get("host").(string)))...) } - if err := setSystemSyslogHost(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSystemSyslogHost(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_system_syslog_host", jnprSess) + warns, err := clt.commitConf("create resource junos_system_syslog_host", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - syslogHostExists, err = checkSystemSyslogHostExists(d.Get("host").(string), m, jnprSess) + syslogHostExists, err = checkSystemSyslogHostExists(d.Get("host").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -243,24 +243,24 @@ func resourceSystemSyslogHostCreate(ctx context.Context, d *schema.ResourceData, "=> check your config", d.Get("host").(string)))...) } - return append(diagWarns, resourceSystemSyslogHostReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSystemSyslogHostReadWJunSess(d, clt, junSess)...) } func resourceSystemSyslogHostRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceSystemSyslogHostReadWJnprSess(d, m, jnprSess) + return resourceSystemSyslogHostReadWJunSess(d, clt, junSess) } -func resourceSystemSyslogHostReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceSystemSyslogHostReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - syslogHostOptions, err := readSystemSyslogHost(d.Get("host").(string), m, jnprSess) + syslogHostOptions, err := readSystemSyslogHost(d.Get("host").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -276,76 +276,76 @@ func resourceSystemSyslogHostReadWJnprSess(d *schema.ResourceData, m interface{} func resourceSystemSyslogHostUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delSystemSyslogHost(d.Get("host").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delSystemSyslogHost(d.Get("host").(string), clt, nil); err != nil { return diag.FromErr(err) } - if err := setSystemSyslogHost(d, m, nil); err != nil { + if err := setSystemSyslogHost(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSystemSyslogHost(d.Get("host").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSystemSyslogHost(d.Get("host").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setSystemSyslogHost(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setSystemSyslogHost(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_system_syslog_host", jnprSess) + warns, err := clt.commitConf("update resource junos_system_syslog_host", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceSystemSyslogHostReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceSystemSyslogHostReadWJunSess(d, clt, junSess)...) } func resourceSystemSyslogHostDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delSystemSyslogHost(d.Get("host").(string), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delSystemSyslogHost(d.Get("host").(string), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delSystemSyslogHost(d.Get("host").(string), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delSystemSyslogHost(d.Get("host").(string), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_system_syslog_host", jnprSess) + warns, err := clt.commitConf("delete resource junos_system_syslog_host", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -355,22 +355,22 @@ func resourceSystemSyslogHostDelete(ctx context.Context, d *schema.ResourceData, func resourceSystemSyslogHostImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - syslogHostExists, err := checkSystemSyslogHostExists(d.Id(), m, jnprSess) + syslogHostExists, err := checkSystemSyslogHostExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !syslogHostExists { return nil, fmt.Errorf("don't find system syslog host with id '%v' (id must be )", d.Id()) } - syslogHostOptions, err := readSystemSyslogHost(d.Id(), m, jnprSess) + syslogHostOptions, err := readSystemSyslogHost(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -381,9 +381,8 @@ func resourceSystemSyslogHostImport(ctx context.Context, d *schema.ResourceData, return result, nil } -func checkSystemSyslogHostExists(host string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"system syslog host "+host+pipeDisplaySet, jnprSess) +func checkSystemSyslogHostExists(host string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"system syslog host "+host+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -394,9 +393,7 @@ func checkSystemSyslogHostExists(host string, m interface{}, jnprSess *NetconfOb return true, nil } -func setSystemSyslogHost(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) - +func setSystemSyslogHost(d *schema.ResourceData, clt *Client, junSess *junosSession) error { setPrefix := "set system syslog host " + d.Get("host").(string) configSet := make([]string, 0) @@ -482,14 +479,13 @@ func setSystemSyslogHost(d *schema.ResourceData, m interface{}, jnprSess *Netcon configSet = append(configSet, setPrefix+" user "+d.Get("user_severity").(string)) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readSystemSyslogHost(host string, m interface{}, jnprSess *NetconfObject) (syslogHostOptions, error) { - sess := m.(*Session) +func readSystemSyslogHost(host string, clt *Client, junSess *junosSession) (syslogHostOptions, error) { var confRead syslogHostOptions - showConfig, err := sess.command(cmdShowConfig+"system syslog host "+host+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"system syslog host "+host+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -573,12 +569,11 @@ func readSystemSyslogHost(host string, m interface{}, jnprSess *NetconfObject) ( return confRead, nil } -func delSystemSyslogHost(host string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delSystemSyslogHost(host string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete system syslog host "+host) - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillSystemSyslogHostData(d *schema.ResourceData, syslogHostOptions syslogHostOptions) { diff --git a/junos/resource_vlan.go b/junos/resource_vlan.go index 2eef5939..26b8eb6c 100644 --- a/junos/resource_vlan.go +++ b/junos/resource_vlan.go @@ -153,49 +153,49 @@ func resourceVlan() *schema.Resource { } func resourceVlanCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setVlan(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setVlan(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("name").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - vlanExists, err := checkVlansExists(d.Get("name").(string), m, jnprSess) + vlanExists, err := checkVlansExists(d.Get("name").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if vlanExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("vlan %v already exists", d.Get("name").(string)))...) } - if err := setVlan(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setVlan(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_vlan", jnprSess) + warns, err := clt.commitConf("create resource junos_vlan", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - vlanExists, err = checkVlansExists(d.Get("name").(string), m, jnprSess) + vlanExists, err = checkVlansExists(d.Get("name").(string), clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -206,23 +206,23 @@ func resourceVlanCreate(ctx context.Context, d *schema.ResourceData, m interface diag.FromErr(fmt.Errorf("vlan %v not exists after commit => check your config", d.Get("name").(string)))...) } - return append(diagWarns, resourceVlanReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceVlanReadWJunSess(d, clt, junSess)...) } func resourceVlanRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceVlanReadWJnprSess(d, m, jnprSess) + return resourceVlanReadWJunSess(d, clt, junSess) } -func resourceVlanReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) diag.Diagnostics { +func resourceVlanReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession) diag.Diagnostics { mutex.Lock() - vlanOptions, err := readVlan(d.Get("name").(string), m, jnprSess) + vlanOptions, err := readVlan(d.Get("name").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -238,88 +238,88 @@ func resourceVlanReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess * func resourceVlanUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { + clt := m.(*Client) + if clt.fakeUpdateAlso { if d.HasChange("vxlan") { oldVxlan, _ := d.GetChange("vxlan") - if err := delVlan(d.Get("name").(string), oldVxlan.([]interface{}), m, nil); err != nil { + if err := delVlan(d.Get("name").(string), oldVxlan.([]interface{}), clt, nil); err != nil { return diag.FromErr(err) } - } else if err := delVlan(d.Get("name").(string), d.Get("vxlan").([]interface{}), m, nil); err != nil { + } else if err := delVlan(d.Get("name").(string), d.Get("vxlan").([]interface{}), clt, nil); err != nil { return diag.FromErr(err) } - if err := setVlan(d, m, nil); err != nil { + if err := setVlan(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if d.HasChange("vxlan") { oldVxlan, _ := d.GetChange("vxlan") - if err := delVlan(d.Get("name").(string), oldVxlan.([]interface{}), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delVlan(d.Get("name").(string), oldVxlan.([]interface{}), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - } else if err := delVlan(d.Get("name").(string), d.Get("vxlan").([]interface{}), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + } else if err := delVlan(d.Get("name").(string), d.Get("vxlan").([]interface{}), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setVlan(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setVlan(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_vlan", jnprSess) + warns, err := clt.commitConf("update resource junos_vlan", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceVlanReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceVlanReadWJunSess(d, clt, junSess)...) } func resourceVlanDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delVlan(d.Get("name").(string), d.Get("vxlan").([]interface{}), m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delVlan(d.Get("name").(string), d.Get("vxlan").([]interface{}), clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delVlan(d.Get("name").(string), d.Get("vxlan").([]interface{}), m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delVlan(d.Get("name").(string), d.Get("vxlan").([]interface{}), clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_vlan", jnprSess) + warns, err := clt.commitConf("delete resource junos_vlan", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -329,21 +329,21 @@ func resourceVlanDelete(ctx context.Context, d *schema.ResourceData, m interface func resourceVlanImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) - vlanExists, err := checkVlansExists(d.Id(), m, jnprSess) + vlanExists, err := checkVlansExists(d.Id(), clt, junSess) if err != nil { return nil, err } if !vlanExists { return nil, fmt.Errorf("don't find vlan with id '%v' (id must be )", d.Id()) } - vlanOptions, err := readVlan(d.Id(), m, jnprSess) + vlanOptions, err := readVlan(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -354,9 +354,8 @@ func resourceVlanImport(ctx context.Context, d *schema.ResourceData, m interface return result, nil } -func checkVlansExists(vlan string, m interface{}, jnprSess *NetconfObject) (bool, error) { - sess := m.(*Session) - showConfig, err := sess.command(cmdShowConfig+"vlans "+vlan+pipeDisplaySet, jnprSess) +func checkVlansExists(vlan string, clt *Client, junSess *junosSession) (bool, error) { + showConfig, err := clt.command(cmdShowConfig+"vlans "+vlan+pipeDisplaySet, junSess) if err != nil { return false, err } @@ -367,8 +366,7 @@ func checkVlansExists(vlan string, m interface{}, jnprSess *NetconfObject) (bool return true, nil } -func setVlan(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setVlan(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := "set vlans " + d.Get("name").(string) + " " @@ -433,14 +431,13 @@ func setVlan(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) err } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readVlan(vlan string, m interface{}, jnprSess *NetconfObject) (vlanOptions, error) { - sess := m.(*Session) +func readVlan(vlan string, clt *Client, junSess *junosSession) (vlanOptions, error) { var confRead vlanOptions - showConfig, err := sess.command(cmdShowConfig+"vlans "+vlan+pipeDisplaySetRelative, jnprSess) + showConfig, err := clt.command(cmdShowConfig+"vlans "+vlan+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -510,7 +507,7 @@ func readVlan(vlan string, m interface{}, jnprSess *NetconfObject) (vlanOptions, return confRead, fmt.Errorf(failedConvAtoiError, itemTrim, err) } if vxlan["vni"] != -1 { - showConfigEvpn, err := sess.command(cmdShowConfig+"protocols evpn"+pipeDisplaySetRelative, jnprSess) + showConfigEvpn, err := clt.command(cmdShowConfig+"protocols evpn"+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -551,8 +548,7 @@ func readVlan(vlan string, m interface{}, jnprSess *NetconfObject) (vlanOptions, return confRead, nil } -func delVlan(vlan string, vxlan []interface{}, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delVlan(vlan string, vxlan []interface{}, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) configSet = append(configSet, "delete vlans "+vlan) for _, v := range vxlan { @@ -562,7 +558,7 @@ func delVlan(vlan string, vxlan []interface{}, m interface{}, jnprSess *NetconfO } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillVlanData(d *schema.ResourceData, vlanOptions vlanOptions) { diff --git a/junos/resource_vstp.go b/junos/resource_vstp.go index 48efafa5..1bce1b39 100644 --- a/junos/resource_vstp.go +++ b/junos/resource_vstp.go @@ -84,70 +84,70 @@ func resourceVstp() *schema.Resource { } func resourceVstpCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setVstp(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setVstp(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(d.Get("routing_instance").(string)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if d.Get("routing_instance").(string) != defaultW { - instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !instanceExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("routing instance %v doesn't exist", d.Get("routing_instance").(string)))...) } } - if err := setVstp(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setVstp(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_vstp", jnprSess) + warns, err := clt.commitConf("create resource junos_vstp", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.SetId(d.Get("routing_instance").(string)) - return append(diagWarns, resourceVstpReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceVstpReadWJunSess(d, clt, junSess)...) } func resourceVstpRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceVstpReadWJnprSess(d, m, jnprSess) + return resourceVstpReadWJunSess(d, clt, junSess) } -func resourceVstpReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) diag.Diagnostics { +func resourceVstpReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession) diag.Diagnostics { mutex.Lock() if d.Get("routing_instance").(string) != defaultW { - instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(d.Get("routing_instance").(string), clt, junSess) if err != nil { mutex.Unlock() @@ -160,7 +160,7 @@ func resourceVstpReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess * return nil } } - vstpOptions, err := readVstp(d.Get("routing_instance").(string), m, jnprSess) + vstpOptions, err := readVstp(d.Get("routing_instance").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -172,76 +172,76 @@ func resourceVstpReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess * func resourceVstpUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delVstp(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delVstp(d, clt, nil); err != nil { return diag.FromErr(err) } - if err := setVstp(d, m, nil); err != nil { + if err := setVstp(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delVstp(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delVstp(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setVstp(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setVstp(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_vstp", jnprSess) + warns, err := clt.commitConf("update resource junos_vstp", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceVstpReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceVstpReadWJunSess(d, clt, junSess)...) } func resourceVstpDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delVstp(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delVstp(d, clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delVstp(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delVstp(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_vstp", jnprSess) + warns, err := clt.commitConf("delete resource junos_vstp", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -251,14 +251,14 @@ func resourceVstpDelete(ctx context.Context, d *schema.ResourceData, m interface func resourceVstpImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) if d.Id() != defaultW { - instanceExists, err := checkRoutingInstanceExists(d.Id(), m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -267,7 +267,7 @@ func resourceVstpImport(ctx context.Context, d *schema.ResourceData, m interface } } result := make([]*schema.ResourceData, 1) - vstpOptions, err := readVstp(d.Id(), m, jnprSess) + vstpOptions, err := readVstp(d.Id(), clt, junSess) if err != nil { return nil, err } @@ -277,8 +277,7 @@ func resourceVstpImport(ctx context.Context, d *schema.ResourceData, m interface return result, nil } -func setVstp(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setVstp(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := setLS if d.Get("routing_instance").(string) != defaultW { @@ -314,25 +313,24 @@ func setVstp(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) err configSet = append(configSet, setPrefix+"vpls-flush-on-topology-change") } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readVstp(routingInstance string, m interface{}, jnprSess *NetconfObject) (vstpOptions, error) { - sess := m.(*Session) +func readVstp(routingInstance string, clt *Client, junSess *junosSession) (vstpOptions, error) { var confRead vstpOptions var showConfig string if routingInstance == defaultW { var err error - showConfig, err = sess.command(cmdShowConfig+ - "protocols vstp"+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "protocols vstp"+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } } else { var err error - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ - "protocols vstp"+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ + "protocols vstp"+pipeDisplaySetRelative, junSess) if err != nil { return confRead, err } @@ -385,8 +383,7 @@ func readVstp(routingInstance string, m interface{}, jnprSess *NetconfObject) (v return confRead, nil } -func delVstp(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delVstp(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) delPrefix := deleteLS if d.Get("routing_instance").(string) != defaultW { @@ -418,7 +415,7 @@ func delVstp(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) err } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillVstpData(d *schema.ResourceData, vstpOptions vstpOptions) { diff --git a/junos/resource_vstp_interface.go b/junos/resource_vstp_interface.go index 96721088..1273bbc8 100644 --- a/junos/resource_vstp_interface.go +++ b/junos/resource_vstp_interface.go @@ -184,21 +184,21 @@ func resourceVstpInterfaceReadID(resourceID string) (vType vstpInterfaceVIdType, } func resourceVstpInterfaceCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeCreateSetFile != "" { - if err := setVstpInterface(d, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeCreateSetFile != "" { + if err := setVstpInterface(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(resourceVstpInterfaceNewID(d)) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics @@ -206,28 +206,28 @@ func resourceVstpInterfaceCreate(ctx context.Context, d *schema.ResourceData, m vlan := d.Get("vlan").(string) vlanGroup := d.Get("vlan_group").(string) if routingInstance != defaultW { - instanceExists, err := checkRoutingInstanceExists(routingInstance, m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(routingInstance, clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !instanceExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("routing instance %v doesn't exist", routingInstance))...) } } if vlan != "" { - vstpVlanExists, err := checkVstpVlanExists(vlan, routingInstance, m, jnprSess) + vstpVlanExists, err := checkVstpVlanExists(vlan, routingInstance, clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !vstpVlanExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) if routingInstance == defaultW { return append(diagWarns, diag.FromErr(fmt.Errorf("protocol vstp vlan %v doesn't exist", vlan))...) @@ -238,14 +238,14 @@ func resourceVstpInterfaceCreate(ctx context.Context, d *schema.ResourceData, m } } if vlanGroup != "" { - vstpVlanGroupExists, err := checkVstpVlanGroupExists(vlanGroup, routingInstance, m, jnprSess) + vstpVlanGroupExists, err := checkVstpVlanGroupExists(vlanGroup, routingInstance, clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !vstpVlanGroupExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) if routingInstance == defaultW { return append(diagWarns, diag.FromErr(fmt.Errorf("protocol vstp vlan-group group %v doesn't exist", vlanGroup))...) @@ -256,14 +256,14 @@ func resourceVstpInterfaceCreate(ctx context.Context, d *schema.ResourceData, m "protocol vstp vlan-group group %v in routing-instance %v doesn't exist", vlanGroup, routingInstance))...) } } - vstpInterfaceExists, err := checkVstpInterfaceExists(name, routingInstance, vlan, vlanGroup, m, jnprSess) + vstpInterfaceExists, err := checkVstpInterfaceExists(name, routingInstance, vlan, vlanGroup, clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if vstpInterfaceExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) switch vstpInterfaceVIdType { case vstpInterfaceVIdTypeNone: if routingInstance == defaultW { @@ -293,19 +293,19 @@ func resourceVstpInterfaceCreate(ctx context.Context, d *schema.ResourceData, m name, vlanGroup, routingInstance))...) } } - if err := setVstpInterface(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setVstpInterface(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_vstp_interface", jnprSess) + warns, err := clt.commitConf("create resource junos_vstp_interface", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - vstpInterfaceExists, err = checkVstpInterfaceExists(name, routingInstance, vlan, vlanGroup, m, jnprSess) + vstpInterfaceExists, err = checkVstpInterfaceExists(name, routingInstance, vlan, vlanGroup, clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -346,27 +346,29 @@ func resourceVstpInterfaceCreate(ctx context.Context, d *schema.ResourceData, m } } - return append(diagWarns, resourceVstpInterfaceReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceVstpInterfaceReadWJunSess(d, clt, junSess)...) } func resourceVstpInterfaceRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceVstpInterfaceReadWJnprSess(d, m, jnprSess) + return resourceVstpInterfaceReadWJunSess(d, clt, junSess) } -func resourceVstpInterfaceReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceVstpInterfaceReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() vstpInterfaceOptions, err := readVstpInterface( - d.Get("name").(string), d.Get("routing_instance").(string), - d.Get("vlan").(string), d.Get("vlan_group").(string), - m, jnprSess) + d.Get("name").(string), + d.Get("routing_instance").(string), + d.Get("vlan").(string), + d.Get("vlan_group").(string), + clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -382,88 +384,100 @@ func resourceVstpInterfaceReadWJnprSess(d *schema.ResourceData, m interface{}, j func resourceVstpInterfaceUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { + clt := m.(*Client) + if clt.fakeUpdateAlso { if err := delVstpInterface( - d.Get("name").(string), d.Get("routing_instance").(string), - d.Get("vlan").(string), d.Get("vlan_group").(string), - m, nil); err != nil { + d.Get("name").(string), + d.Get("routing_instance").(string), + d.Get("vlan").(string), + d.Get("vlan_group").(string), + clt, nil, + ); err != nil { return diag.FromErr(err) } - if err := setVstpInterface(d, m, nil); err != nil { + if err := setVstpInterface(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if err := delVstpInterface( - d.Get("name").(string), d.Get("routing_instance").(string), - d.Get("vlan").(string), d.Get("vlan_group").(string), - m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + d.Get("name").(string), + d.Get("routing_instance").(string), + d.Get("vlan").(string), + d.Get("vlan_group").(string), + clt, junSess, + ); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setVstpInterface(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setVstpInterface(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_vstp_interface", jnprSess) + warns, err := clt.commitConf("update resource junos_vstp_interface", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceVstpInterfaceReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceVstpInterfaceReadWJunSess(d, clt, junSess)...) } func resourceVstpInterfaceDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { + clt := m.(*Client) + if clt.fakeDeleteAlso { if err := delVstpInterface( - d.Get("name").(string), d.Get("routing_instance").(string), - d.Get("vlan").(string), d.Get("vlan_group").(string), - m, nil); err != nil { + d.Get("name").(string), + d.Get("routing_instance").(string), + d.Get("vlan").(string), + d.Get("vlan_group").(string), + clt, nil, + ); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if err := delVstpInterface( - d.Get("name").(string), d.Get("routing_instance").(string), - d.Get("vlan").(string), d.Get("vlan_group").(string), - m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + d.Get("name").(string), + d.Get("routing_instance").(string), + d.Get("vlan").(string), + d.Get("vlan_group").(string), + clt, junSess, + ); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_vstp_interface", jnprSess) + warns, err := clt.commitConf("delete resource junos_vstp_interface", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -473,12 +487,12 @@ func resourceVstpInterfaceDelete(ctx context.Context, d *schema.ResourceData, m func resourceVstpInterfaceImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) if len(strings.Split(d.Id(), idSeparator)) < 3 { return nil, fmt.Errorf("missing element(s) in id with separator %v", idSeparator) @@ -487,11 +501,11 @@ func resourceVstpInterfaceImport(ctx context.Context, d *schema.ResourceData, m var vstpInterfaceExists bool switch vType { case vstpInterfaceVIdTypeNone: - vstpInterfaceExists, err = checkVstpInterfaceExists(name, routingInstance, "", "", m, jnprSess) + vstpInterfaceExists, err = checkVstpInterfaceExists(name, routingInstance, "", "", clt, junSess) case vstpInterfaceVIdTypeVlan: - vstpInterfaceExists, err = checkVstpInterfaceExists(name, routingInstance, vName, "", m, jnprSess) + vstpInterfaceExists, err = checkVstpInterfaceExists(name, routingInstance, vName, "", clt, junSess) case vstpInterfaceVIdTypeVlanGroup: - vstpInterfaceExists, err = checkVstpInterfaceExists(name, routingInstance, "", vName, m, jnprSess) + vstpInterfaceExists, err = checkVstpInterfaceExists(name, routingInstance, "", vName, clt, junSess) } if err != nil { return nil, err @@ -507,11 +521,11 @@ func resourceVstpInterfaceImport(ctx context.Context, d *schema.ResourceData, m var vstpInterfaceOptions vstpInterfaceOptions switch vType { case vstpInterfaceVIdTypeNone: - vstpInterfaceOptions, err = readVstpInterface(name, routingInstance, "", "", m, jnprSess) + vstpInterfaceOptions, err = readVstpInterface(name, routingInstance, "", "", clt, junSess) case vstpInterfaceVIdTypeVlan: - vstpInterfaceOptions, err = readVstpInterface(name, routingInstance, vName, "", m, jnprSess) + vstpInterfaceOptions, err = readVstpInterface(name, routingInstance, vName, "", clt, junSess) case vstpInterfaceVIdTypeVlanGroup: - vstpInterfaceOptions, err = readVstpInterface(name, routingInstance, "", vName, m, jnprSess) + vstpInterfaceOptions, err = readVstpInterface(name, routingInstance, "", vName, clt, junSess) } if err != nil { return nil, err @@ -523,9 +537,8 @@ func resourceVstpInterfaceImport(ctx context.Context, d *schema.ResourceData, m return result, nil } -func checkVstpInterfaceExists(name, routingInstance, vlan, vlanGroup string, m interface{}, jnprSess *NetconfObject, +func checkVstpInterfaceExists(name, routingInstance, vlan, vlanGroup string, clt *Client, junSess *junosSession, ) (bool, error) { - sess := m.(*Session) var showConfig string var err error if vlan != "" && vlanGroup != "" { @@ -534,26 +547,26 @@ func checkVstpInterfaceExists(name, routingInstance, vlan, vlanGroup string, m i if routingInstance == defaultW { switch { case vlan != "": - showConfig, err = sess.command(cmdShowConfig+ - "protocols vstp vlan "+vlan+" interface "+name+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "protocols vstp vlan "+vlan+" interface "+name+pipeDisplaySet, junSess) case vlanGroup != "": - showConfig, err = sess.command(cmdShowConfig+ - "protocols vstp vlan-group group "+vlanGroup+" interface "+name+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "protocols vstp vlan-group group "+vlanGroup+" interface "+name+pipeDisplaySet, junSess) default: - showConfig, err = sess.command(cmdShowConfig+ - "protocols vstp interface "+name+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "protocols vstp interface "+name+pipeDisplaySet, junSess) } } else { switch { case vlan != "": - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ - "protocols vstp vlan "+vlan+" interface "+name+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ + "protocols vstp vlan "+vlan+" interface "+name+pipeDisplaySet, junSess) case vlanGroup != "": - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ - "protocols vstp vlan-group group "+vlanGroup+" interface "+name+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ + "protocols vstp vlan-group group "+vlanGroup+" interface "+name+pipeDisplaySet, junSess) default: - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ - "protocols vstp interface "+name+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ + "protocols vstp interface "+name+pipeDisplaySet, junSess) } } if err != nil { @@ -566,8 +579,7 @@ func checkVstpInterfaceExists(name, routingInstance, vlan, vlanGroup string, m i return true, nil } -func setVstpInterface(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setVstpInterface(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) name := d.Get("name").(string) @@ -612,12 +624,11 @@ func setVstpInterface(d *schema.ResourceData, m interface{}, jnprSess *NetconfOb configSet = append(configSet, setPrefix+"priority "+strconv.Itoa(v)) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readVstpInterface(name, routingInstance, vlan, vlanGroup string, m interface{}, jnprSess *NetconfObject, +func readVstpInterface(name, routingInstance, vlan, vlanGroup string, clt *Client, junSess *junosSession, ) (vstpInterfaceOptions, error) { - sess := m.(*Session) var confRead vstpInterfaceOptions if vlan != "" && vlanGroup != "" { return confRead, fmt.Errorf("internal error: readVstpInterface called with vlan and vlanGroup") @@ -628,26 +639,26 @@ func readVstpInterface(name, routingInstance, vlan, vlanGroup string, m interfac if routingInstance == defaultW { switch { case vlan != "": - showConfig, err = sess.command(cmdShowConfig+ - "protocols vstp vlan "+vlan+" interface "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "protocols vstp vlan "+vlan+" interface "+name+pipeDisplaySetRelative, junSess) case vlanGroup != "": - showConfig, err = sess.command(cmdShowConfig+ - "protocols vstp vlan-group group "+vlanGroup+" interface "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "protocols vstp vlan-group group "+vlanGroup+" interface "+name+pipeDisplaySetRelative, junSess) default: - showConfig, err = sess.command(cmdShowConfig+ - "protocols vstp interface "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "protocols vstp interface "+name+pipeDisplaySetRelative, junSess) } } else { switch { case vlan != "": - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ - "protocols vstp vlan "+vlan+" interface "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ + "protocols vstp vlan "+vlan+" interface "+name+pipeDisplaySetRelative, junSess) case vlanGroup != "": - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ - "protocols vstp vlan-group group "+vlanGroup+" interface "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ + "protocols vstp vlan-group group "+vlanGroup+" interface "+name+pipeDisplaySetRelative, junSess) default: - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ - "protocols vstp interface "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ + "protocols vstp interface "+name+pipeDisplaySetRelative, junSess) } } if err != nil { @@ -698,8 +709,7 @@ func readVstpInterface(name, routingInstance, vlan, vlanGroup string, m interfac return confRead, nil } -func delVstpInterface(name, routingInstance, vlan, vlanGroup string, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delVstpInterface(name, routingInstance, vlan, vlanGroup string, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) if vlan != "" && vlanGroup != "" { return fmt.Errorf("internal error: delVstpInterface called with vlan and vlanGroup") @@ -727,7 +737,7 @@ func delVstpInterface(name, routingInstance, vlan, vlanGroup string, m interface } } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillVstpInterfaceData(d *schema.ResourceData, vstpInterfaceOptions vstpInterfaceOptions) { diff --git a/junos/resource_vstp_vlan.go b/junos/resource_vstp_vlan.go index 214d8075..1fc26dcb 100644 --- a/junos/resource_vstp_vlan.go +++ b/junos/resource_vstp_vlan.go @@ -84,48 +84,48 @@ func resourceVstpVlan() *schema.Resource { } func resourceVstpVlanCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) + clt := m.(*Client) routingInstance := d.Get("routing_instance").(string) vlanID := d.Get("vlan_id").(string) - if sess.junosFakeCreateSetFile != "" { - if err := setVstpVlan(d, m, nil); err != nil { + if clt.fakeCreateSetFile != "" { + if err := setVstpVlan(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(vlanID + idSeparator + routingInstance) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if routingInstance != defaultW { - instanceExists, err := checkRoutingInstanceExists(routingInstance, m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(routingInstance, clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !instanceExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("routing instance %v doesn't exist", d.Get("routing_instance").(string)))...) } } - vstpVlanExists, err := checkVstpVlanExists(vlanID, routingInstance, m, jnprSess) + vstpVlanExists, err := checkVstpVlanExists(vlanID, routingInstance, clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if vstpVlanExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) if routingInstance != defaultW { return append(diagWarns, diag.FromErr(fmt.Errorf( "protocols vstp vlan %v already exists in routing-instance %v", vlanID, routingInstance))...) @@ -133,19 +133,19 @@ func resourceVstpVlanCreate(ctx context.Context, d *schema.ResourceData, m inter return append(diagWarns, diag.FromErr(fmt.Errorf("protocols vstp vlan %v already exists", vlanID))...) } - if err := setVstpVlan(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setVstpVlan(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_vstp_vlan", jnprSess) + warns, err := clt.commitConf("create resource junos_vstp_vlan", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - vstpVlanExists, err = checkVstpVlanExists(vlanID, routingInstance, m, jnprSess) + vstpVlanExists, err = checkVstpVlanExists(vlanID, routingInstance, clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -162,24 +162,24 @@ func resourceVstpVlanCreate(ctx context.Context, d *schema.ResourceData, m inter "=> check your config", vlanID))...) } - return append(diagWarns, resourceVstpVlanReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceVstpVlanReadWJunSess(d, clt, junSess)...) } func resourceVstpVlanRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceVstpVlanReadWJnprSess(d, m, jnprSess) + return resourceVstpVlanReadWJunSess(d, clt, junSess) } -func resourceVstpVlanReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceVstpVlanReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - vstpVlanOptions, err := readVstpVlan(d.Get("vlan_id").(string), d.Get("routing_instance").(string), m, jnprSess) + vstpVlanOptions, err := readVstpVlan(d.Get("vlan_id").(string), d.Get("routing_instance").(string), clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -195,76 +195,86 @@ func resourceVstpVlanReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSe func resourceVstpVlanUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { - if err := delVstpVlan(d.Get("vlan_id").(string), d.Get("routing_instance").(string), false, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeUpdateAlso { + if err := delVstpVlan(d.Get("vlan_id").(string), d.Get("routing_instance").(string), false, clt, nil); err != nil { return diag.FromErr(err) } - if err := setVstpVlan(d, m, nil); err != nil { + if err := setVstpVlan(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delVstpVlan(d.Get("vlan_id").(string), d.Get("routing_instance").(string), false, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delVstpVlan( + d.Get("vlan_id").(string), + d.Get("routing_instance").(string), + false, + clt, junSess, + ); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setVstpVlan(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setVstpVlan(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_vstp_vlan", jnprSess) + warns, err := clt.commitConf("update resource junos_vstp_vlan", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceVstpVlanReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceVstpVlanReadWJunSess(d, clt, junSess)...) } func resourceVstpVlanDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delVstpVlan(d.Get("vlan_id").(string), d.Get("routing_instance").(string), true, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delVstpVlan(d.Get("vlan_id").(string), d.Get("routing_instance").(string), true, clt, nil); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delVstpVlan(d.Get("vlan_id").(string), d.Get("routing_instance").(string), true, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delVstpVlan( + d.Get("vlan_id").(string), + d.Get("routing_instance").(string), + true, + clt, junSess, + ); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_vstp_vlan", jnprSess) + warns, err := clt.commitConf("delete resource junos_vstp_vlan", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -274,18 +284,18 @@ func resourceVstpVlanDelete(ctx context.Context, d *schema.ResourceData, m inter func resourceVstpVlanImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) idSplit := strings.Split(d.Id(), idSeparator) if len(idSplit) < 2 { return nil, fmt.Errorf("missing element(s) in id with separator %v", idSeparator) } - vstpVlanExists, err := checkVstpVlanExists(idSplit[0], idSplit[1], m, jnprSess) + vstpVlanExists, err := checkVstpVlanExists(idSplit[0], idSplit[1], clt, junSess) if err != nil { return nil, err } @@ -293,7 +303,7 @@ func resourceVstpVlanImport(ctx context.Context, d *schema.ResourceData, m inter return nil, fmt.Errorf("don't find protocols vstp vlan with id '%v' "+ "(id must be %s", d.Id(), idSeparator) } - vstpVlanOptions, err := readVstpVlan(idSplit[0], idSplit[1], m, jnprSess) + vstpVlanOptions, err := readVstpVlan(idSplit[0], idSplit[1], clt, junSess) if err != nil { return nil, err } @@ -304,17 +314,16 @@ func resourceVstpVlanImport(ctx context.Context, d *schema.ResourceData, m inter return result, nil } -func checkVstpVlanExists(vlanID, routingInstance string, m interface{}, jnprSess *NetconfObject, +func checkVstpVlanExists(vlanID, routingInstance string, clt *Client, junSess *junosSession, ) (bool, error) { - sess := m.(*Session) var showConfig string var err error if routingInstance == defaultW { - showConfig, err = sess.command(cmdShowConfig+ - "protocols vstp vlan "+vlanID+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "protocols vstp vlan "+vlanID+pipeDisplaySet, junSess) } else { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ - "protocols vstp vlan "+vlanID+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ + "protocols vstp vlan "+vlanID+pipeDisplaySet, junSess) } if err != nil { return false, err @@ -326,8 +335,7 @@ func checkVstpVlanExists(vlanID, routingInstance string, m interface{}, jnprSess return true, nil } -func setVstpVlan(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setVstpVlan(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) setPrefix := setLS @@ -356,21 +364,20 @@ func setVstpVlan(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) configSet = append(configSet, setPrefix+"system-identifier "+v) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readVstpVlan(vlanID, routingInstance string, m interface{}, jnprSess *NetconfObject, +func readVstpVlan(vlanID, routingInstance string, clt *Client, junSess *junosSession, ) (vstpVlanOptions, error) { - sess := m.(*Session) var confRead vstpVlanOptions var showConfig string var err error if routingInstance == defaultW { - showConfig, err = sess.command(cmdShowConfig+ - "protocols vstp vlan "+vlanID+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "protocols vstp vlan "+vlanID+pipeDisplaySetRelative, junSess) } else { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ - "protocols vstp vlan "+vlanID+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ + "protocols vstp vlan "+vlanID+pipeDisplaySetRelative, junSess) } if err != nil { return confRead, err @@ -418,8 +425,7 @@ func readVstpVlan(vlanID, routingInstance string, m interface{}, jnprSess *Netco return confRead, nil } -func delVstpVlan(vlanID, routingInstance string, deleteAll bool, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delVstpVlan(vlanID, routingInstance string, deleteAll bool, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) delPrefix := deleteLS if routingInstance != defaultW { @@ -428,7 +434,7 @@ func delVstpVlan(vlanID, routingInstance string, deleteAll bool, m interface{}, delPrefix += "protocols vstp vlan " + vlanID + " " if deleteAll { - return sess.configSet([]string{delPrefix}, jnprSess) + return clt.configSet([]string{delPrefix}, junSess) } listLinesToDelete := []string{ "backup-bridge-priority", @@ -442,7 +448,7 @@ func delVstpVlan(vlanID, routingInstance string, deleteAll bool, m interface{}, configSet = append(configSet, delPrefix+line) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillVstpVlanData(d *schema.ResourceData, vstpVlanOptions vstpVlanOptions) { diff --git a/junos/resource_vstp_vlan_group.go b/junos/resource_vstp_vlan_group.go index b6c687ee..93b173bf 100644 --- a/junos/resource_vstp_vlan_group.go +++ b/junos/resource_vstp_vlan_group.go @@ -89,48 +89,48 @@ func resourceVstpVlanGroup() *schema.Resource { } func resourceVstpVlanGroupCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) + clt := m.(*Client) routingInstance := d.Get("routing_instance").(string) name := d.Get("name").(string) - if sess.junosFakeCreateSetFile != "" { - if err := setVstpVlanGroup(d, m, nil); err != nil { + if clt.fakeCreateSetFile != "" { + if err := setVstpVlanGroup(d, clt, nil); err != nil { return diag.FromErr(err) } d.SetId(name + idSeparator + routingInstance) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if routingInstance != defaultW { - instanceExists, err := checkRoutingInstanceExists(routingInstance, m, jnprSess) + instanceExists, err := checkRoutingInstanceExists(routingInstance, clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if !instanceExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(fmt.Errorf("routing instance %v doesn't exist", d.Get("routing_instance").(string)))...) } } - vstpVlanGroupExists, err := checkVstpVlanGroupExists(name, routingInstance, m, jnprSess) + vstpVlanGroupExists, err := checkVstpVlanGroupExists(name, routingInstance, clt, junSess) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } if vstpVlanGroupExists { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) if routingInstance != defaultW { return append(diagWarns, diag.FromErr(fmt.Errorf( "protocols vstp vlan-group group %v already exists in routing-instance %v", @@ -140,19 +140,19 @@ func resourceVstpVlanGroupCreate(ctx context.Context, d *schema.ResourceData, m return append(diagWarns, diag.FromErr(fmt.Errorf("protocols vstp vlan-group group %v already exists", name))...) } - if err := setVstpVlanGroup(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setVstpVlanGroup(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("create resource junos_vstp_vlan_group", jnprSess) + warns, err := clt.commitConf("create resource junos_vstp_vlan_group", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - vstpVlanGroupExists, err = checkVstpVlanGroupExists(name, routingInstance, m, jnprSess) + vstpVlanGroupExists, err = checkVstpVlanGroupExists(name, routingInstance, clt, junSess) if err != nil { return append(diagWarns, diag.FromErr(err)...) } @@ -169,24 +169,27 @@ func resourceVstpVlanGroupCreate(ctx context.Context, d *schema.ResourceData, m "=> check your config", name))...) } - return append(diagWarns, resourceVstpVlanGroupReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceVstpVlanGroupReadWJunSess(d, clt, junSess)...) } func resourceVstpVlanGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) - return resourceVstpVlanGroupReadWJnprSess(d, m, jnprSess) + return resourceVstpVlanGroupReadWJunSess(d, clt, junSess) } -func resourceVstpVlanGroupReadWJnprSess(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject, +func resourceVstpVlanGroupReadWJunSess(d *schema.ResourceData, clt *Client, junSess *junosSession, ) diag.Diagnostics { mutex.Lock() - vstpVlanGroupOptions, err := readVstpVlanGroup(d.Get("name").(string), d.Get("routing_instance").(string), m, jnprSess) + vstpVlanGroupOptions, err := readVstpVlanGroup( + d.Get("name").(string), + d.Get("routing_instance").(string), + clt, junSess) mutex.Unlock() if err != nil { return diag.FromErr(err) @@ -202,78 +205,96 @@ func resourceVstpVlanGroupReadWJnprSess(d *schema.ResourceData, m interface{}, j func resourceVstpVlanGroupUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { d.Partial(true) - sess := m.(*Session) - if sess.junosFakeUpdateAlso { + clt := m.(*Client) + if clt.fakeUpdateAlso { if err := delVstpVlanGroup( - d.Get("name").(string), d.Get("routing_instance").(string), false, m, nil); err != nil { + d.Get("name").(string), + d.Get("routing_instance").(string), + false, + clt, nil, + ); err != nil { return diag.FromErr(err) } - if err := setVstpVlanGroup(d, m, nil); err != nil { + if err := setVstpVlanGroup(d, clt, nil); err != nil { return diag.FromErr(err) } d.Partial(false) return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics if err := delVstpVlanGroup( - d.Get("name").(string), d.Get("routing_instance").(string), false, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + d.Get("name").(string), + d.Get("routing_instance").(string), + false, + clt, junSess, + ); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - if err := setVstpVlanGroup(d, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := setVstpVlanGroup(d, clt, junSess); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("update resource junos_vstp_vlan_group", jnprSess) + warns, err := clt.commitConf("update resource junos_vstp_vlan_group", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } d.Partial(false) - return append(diagWarns, resourceVstpVlanGroupReadWJnprSess(d, m, jnprSess)...) + return append(diagWarns, resourceVstpVlanGroupReadWJunSess(d, clt, junSess)...) } func resourceVstpVlanGroupDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - sess := m.(*Session) - if sess.junosFakeDeleteAlso { - if err := delVstpVlanGroup(d.Get("name").(string), d.Get("routing_instance").(string), true, m, nil); err != nil { + clt := m.(*Client) + if clt.fakeDeleteAlso { + if err := delVstpVlanGroup( + d.Get("name").(string), + d.Get("routing_instance").(string), + true, + clt, nil, + ); err != nil { return diag.FromErr(err) } return nil } - jnprSess, err := sess.startNewSession(ctx) + junSess, err := clt.startNewSession(ctx) if err != nil { return diag.FromErr(err) } - defer sess.closeSession(jnprSess) - if err := sess.configLock(ctx, jnprSess); err != nil { + defer clt.closeSession(junSess) + if err := clt.configLock(ctx, junSess); err != nil { return diag.FromErr(err) } var diagWarns diag.Diagnostics - if err := delVstpVlanGroup(d.Get("name").(string), d.Get("routing_instance").(string), true, m, jnprSess); err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + if err := delVstpVlanGroup( + d.Get("name").(string), + d.Get("routing_instance").(string), + true, + clt, junSess, + ); err != nil { + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } - warns, err := sess.commitConf("delete resource junos_vstp_vlan_group", jnprSess) + warns, err := clt.commitConf("delete resource junos_vstp_vlan_group", junSess) appendDiagWarns(&diagWarns, warns) if err != nil { - appendDiagWarns(&diagWarns, sess.configClear(jnprSess)) + appendDiagWarns(&diagWarns, clt.configClear(junSess)) return append(diagWarns, diag.FromErr(err)...) } @@ -283,18 +304,18 @@ func resourceVstpVlanGroupDelete(ctx context.Context, d *schema.ResourceData, m func resourceVstpVlanGroupImport(ctx context.Context, d *schema.ResourceData, m interface{}, ) ([]*schema.ResourceData, error) { - sess := m.(*Session) - jnprSess, err := sess.startNewSession(ctx) + clt := m.(*Client) + junSess, err := clt.startNewSession(ctx) if err != nil { return nil, err } - defer sess.closeSession(jnprSess) + defer clt.closeSession(junSess) result := make([]*schema.ResourceData, 1) idSplit := strings.Split(d.Id(), idSeparator) if len(idSplit) < 2 { return nil, fmt.Errorf("missing element(s) in id with separator %v", idSeparator) } - vstpVlanGroupExists, err := checkVstpVlanGroupExists(idSplit[0], idSplit[1], m, jnprSess) + vstpVlanGroupExists, err := checkVstpVlanGroupExists(idSplit[0], idSplit[1], clt, junSess) if err != nil { return nil, err } @@ -302,7 +323,7 @@ func resourceVstpVlanGroupImport(ctx context.Context, d *schema.ResourceData, m return nil, fmt.Errorf("don't find protocols vstp vlan-group group with id '%v' "+ "(id must be %s", d.Id(), idSeparator) } - vstpVlanGroupOptions, err := readVstpVlanGroup(idSplit[0], idSplit[1], m, jnprSess) + vstpVlanGroupOptions, err := readVstpVlanGroup(idSplit[0], idSplit[1], clt, junSess) if err != nil { return nil, err } @@ -313,17 +334,16 @@ func resourceVstpVlanGroupImport(ctx context.Context, d *schema.ResourceData, m return result, nil } -func checkVstpVlanGroupExists(name, routingInstance string, m interface{}, jnprSess *NetconfObject, +func checkVstpVlanGroupExists(name, routingInstance string, clt *Client, junSess *junosSession, ) (bool, error) { - sess := m.(*Session) var showConfig string var err error if routingInstance == defaultW { - showConfig, err = sess.command(cmdShowConfig+ - "protocols vstp vlan-group group "+name+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "protocols vstp vlan-group group "+name+pipeDisplaySet, junSess) } else { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ - "protocols vstp vlan-group group "+name+pipeDisplaySet, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ + "protocols vstp vlan-group group "+name+pipeDisplaySet, junSess) } if err != nil { return false, err @@ -335,8 +355,7 @@ func checkVstpVlanGroupExists(name, routingInstance string, m interface{}, jnprS return true, nil } -func setVstpVlanGroup(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func setVstpVlanGroup(d *schema.ResourceData, clt *Client, junSess *junosSession) error { configSet := make([]string, 0) setPrefix := setLS @@ -367,21 +386,20 @@ func setVstpVlanGroup(d *schema.ResourceData, m interface{}, jnprSess *NetconfOb configSet = append(configSet, setPrefix+"system-identifier "+v) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } -func readVstpVlanGroup(name, routingInstance string, m interface{}, jnprSess *NetconfObject, +func readVstpVlanGroup(name, routingInstance string, clt *Client, junSess *junosSession, ) (vstpVlanGroupOptions, error) { - sess := m.(*Session) var confRead vstpVlanGroupOptions var showConfig string var err error if routingInstance == defaultW { - showConfig, err = sess.command(cmdShowConfig+ - "protocols vstp vlan-group group "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+ + "protocols vstp vlan-group group "+name+pipeDisplaySetRelative, junSess) } else { - showConfig, err = sess.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ - "protocols vstp vlan-group group "+name+pipeDisplaySetRelative, jnprSess) + showConfig, err = clt.command(cmdShowConfig+routingInstancesWS+routingInstance+" "+ + "protocols vstp vlan-group group "+name+pipeDisplaySetRelative, junSess) } if err != nil { return confRead, err @@ -431,8 +449,7 @@ func readVstpVlanGroup(name, routingInstance string, m interface{}, jnprSess *Ne return confRead, nil } -func delVstpVlanGroup(name, routingInstance string, deleteAll bool, m interface{}, jnprSess *NetconfObject) error { - sess := m.(*Session) +func delVstpVlanGroup(name, routingInstance string, deleteAll bool, clt *Client, junSess *junosSession) error { configSet := make([]string, 0, 1) delPrefix := deleteLS if routingInstance != defaultW { @@ -441,7 +458,7 @@ func delVstpVlanGroup(name, routingInstance string, deleteAll bool, m interface{ delPrefix += "protocols vstp vlan-group group " + name + " " if deleteAll { - return sess.configSet([]string{delPrefix}, jnprSess) + return clt.configSet([]string{delPrefix}, junSess) } listLinesToDelete := []string{ "backup-bridge-priority", @@ -456,7 +473,7 @@ func delVstpVlanGroup(name, routingInstance string, deleteAll bool, m interface{ configSet = append(configSet, delPrefix+line) } - return sess.configSet(configSet, jnprSess) + return clt.configSet(configSet, junSess) } func fillVstpVlanGroupData(d *schema.ResourceData, vstpVlanGroupOptions vstpVlanGroupOptions) { diff --git a/junos/session.go b/junos/session.go deleted file mode 100644 index 801fe2fa..00000000 --- a/junos/session.go +++ /dev/null @@ -1,221 +0,0 @@ -package junos - -import ( - "context" - "fmt" - "log" - "os" - "path" - "strconv" - "time" -) - -const directoryPermission = 0o755 - -// Session information to connect on Junos Device and more. -type Session struct { - junosFakeUpdateAlso bool - junosFakeDeleteAlso bool - junosPort int - junosSleepLock int - junosSleepShort int - junosSleepSSHClosed int - junosSSHTimeoutToEstab int - junosFilePermission int64 - junosIP string - junosUserName string - junosPassword string - junosSSHKeyPEM string - junosSSHKeyFile string - junosKeyPass string - junosGroupIntDel string - junosLogFile string - junosFakeCreateSetFile string - junosSSHCiphers []string -} - -func (sess *Session) startNewSession(ctx context.Context) (*NetconfObject, error) { - var auth netconfAuthMethod - auth.Username = sess.junosUserName - auth.Ciphers = sess.junosSSHCiphers - if sess.junosSSHKeyPEM != "" { - auth.PrivateKeyPEM = sess.junosSSHKeyPEM - if sess.junosKeyPass != "" { - auth.Passphrase = sess.junosKeyPass - } - } - if sess.junosSSHKeyFile != "" { - auth.PrivateKeyFile = sess.junosSSHKeyFile - if sess.junosKeyPass != "" { - auth.Passphrase = sess.junosKeyPass - } - } - if sess.junosPassword != "" { - auth.Password = sess.junosPassword - } - auth.Timeout = sess.junosSSHTimeoutToEstab - jnpr, err := netconfNewSession(ctx, sess.junosIP+":"+strconv.Itoa(sess.junosPort), &auth) - if err != nil { - return nil, err - } - if jnpr.SystemInformation.HardwareModel == "" { - return jnpr, fmt.Errorf("can't read model of device with netconf command") - } - sess.logFile("[startNewSession] started") - - return jnpr, nil -} - -func (sess *Session) closeSession(jnpr *NetconfObject) { - err := jnpr.close(sess.junosSleepSSHClosed) - if err != nil { - sess.logFile(fmt.Sprintf("[closeSession] err: %q", err)) - } else { - sess.logFile("[closeSession] closed") - } -} - -func (sess *Session) command(cmd string, jnpr *NetconfObject) (string, error) { - read, err := jnpr.netconfCommand(cmd) - sess.logFile(fmt.Sprintf("[command] cmd: %q", cmd)) - sess.logFile(fmt.Sprintf("[command] read: %q", read)) - sleepShort(sess.junosSleepShort) - if err != nil && read != emptyW { - sess.logFile(fmt.Sprintf("[command] err: %q", err)) - - return "", err - } - - return read, nil -} - -func (sess *Session) commandXML(cmd string, jnpr *NetconfObject) (string, error) { - read, err := jnpr.netconfCommandXML(cmd) - sess.logFile(fmt.Sprintf("[commandXML] cmd: %q", cmd)) - sess.logFile(fmt.Sprintf("[commandXML] read: %q", read)) - sleepShort(sess.junosSleepShort) - if err != nil { - sess.logFile(fmt.Sprintf("[commandXML] err: %q", err)) - - return "", err - } - - return read, nil -} - -func (sess *Session) configSet(cmd []string, jnpr *NetconfObject) error { - if jnpr != nil { - message, err := jnpr.netconfConfigSet(cmd) - sleepShort(sess.junosSleepShort) - sess.logFile(fmt.Sprintf("[configSet] cmd: %q", cmd)) - sess.logFile(fmt.Sprintf("[configSet] message: %q", message)) - if err != nil { - sess.logFile(fmt.Sprintf("[configSet] err: %q", err)) - - return err - } - - return nil - } else if sess.junosFakeCreateSetFile != "" { - return sess.appendFakeCreateSetFile(cmd) - } - - return fmt.Errorf("internal error: sess.configSet call without connection on device") -} - -func (sess *Session) appendFakeCreateSetFile(lines []string) error { - dirSetFile := path.Dir(sess.junosFakeCreateSetFile) - if _, err := os.Stat(dirSetFile); err != nil { - if err := os.MkdirAll(dirSetFile, os.FileMode(directoryPermission)); err != nil { - return fmt.Errorf("failed to create parent directory of '%s': %w", sess.junosFakeCreateSetFile, err) - } - } - f, err := os.OpenFile(sess.junosFakeCreateSetFile, - os.O_APPEND|os.O_CREATE|os.O_WRONLY, os.FileMode(sess.junosFilePermission)) - if err != nil { - return fmt.Errorf("failed to open file '%s': %w", sess.junosFakeCreateSetFile, err) - } - defer f.Close() - for _, v := range lines { - if _, err := f.WriteString(v + "\n"); err != nil { - return fmt.Errorf("failed to write in file '%s': %w", sess.junosFakeCreateSetFile, err) - } - } - - return nil -} - -func (sess *Session) commitConf(logMessage string, jnpr *NetconfObject) (_warnings []error, _err error) { - sess.logFile(fmt.Sprintf("[commitConf] commit %q", logMessage)) - warns, err := jnpr.netconfCommit(logMessage) - sleepShort(sess.junosSleepShort) - if len(warns) > 0 { - for _, w := range warns { - sess.logFile(fmt.Sprintf("[commitConf] commit warning: %q", w)) - } - } - if err != nil { - sess.logFile(fmt.Sprintf("[commitConf] commit error: %q", err)) - - return warns, err - } - - return warns, nil -} - -func (sess *Session) configLock(ctx context.Context, jnpr *NetconfObject) error { - for { - select { - case <-ctx.Done(): - sess.logFile("[configLock] aborted") - - return fmt.Errorf("candidate configuration lock attempt aborted") - default: - if jnpr.netconfConfigLock() { - sess.logFile("[configLock] locked") - sleepShort(sess.junosSleepShort) - - return nil - } - sess.logFile("[configLock] sleep to wait the lock") - sleep(sess.junosSleepLock) - } - } -} - -func (sess *Session) configClear(jnpr *NetconfObject) (errs []error) { - errs = append(errs, jnpr.netconfConfigClear()...) - sleepShort(sess.junosSleepShort) - sess.logFile("[configClear] config clear") - - errs = append(errs, jnpr.netconfConfigUnlock()...) - sleepShort(sess.junosSleepShort) - sess.logFile("[configClear] config unlock") - - return -} - -// log message in junosLogFile. -func (sess *Session) logFile(message string) { - if sess.junosLogFile != "" { - f, err := os.OpenFile(sess.junosLogFile, - os.O_WRONLY|os.O_CREATE|os.O_APPEND, os.FileMode(sess.junosFilePermission)) - if err != nil { - log.Fatal(err) - } - defer f.Close() - - log.SetOutput(f) - log.SetPrefix(time.Now().Format("2006-01-02 15:04:05")) - - log.Printf("%s", message) - } -} - -func sleep(timeSleep int) { - time.Sleep(time.Duration(timeSleep) * time.Second) -} - -func sleepShort(timeSleep int) { - time.Sleep(time.Duration(timeSleep) * time.Millisecond) -}