cisco_nxapi 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.rubocop.yml +3 -0
- data/.rubocop_todo.yml +55 -0
- data/CHANGELOG.md +5 -0
- data/CONTRIBUTING.md +19 -0
- data/Gemfile +4 -0
- data/LICENSE +201 -0
- data/README.md +77 -0
- data/Rakefile +4 -0
- data/cisco_nxapi.gemspec +29 -0
- data/lib/cisco_nxapi.rb +17 -0
- data/lib/cisco_nxapi/cisco_logger.rb +69 -0
- data/lib/cisco_nxapi/cisco_nxapi.rb +317 -0
- data/lib/cisco_nxapi/version.rb +17 -0
- data/tests/basetest.rb +88 -0
- data/tests/test_all_cisco.rb +25 -0
- data/tests/test_logger.rb +41 -0
- data/tests/test_nxapi.rb +188 -0
- metadata +149 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 02b798d719eedf373814ac7666a93169f6d3bade
|
|
4
|
+
data.tar.gz: 8796e7a3e3a94e8e6ea421f3c037091469f0d574
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 81cda2b93af091507ae8a528e62ef20319b49d1e255e762079164fa2a303f916266ce4f043bf40529a7556ba1ba491e1e794e57e27a268228eef70b42496cce7
|
|
7
|
+
data.tar.gz: 05c7cc8332991d7bcdbfc634349d8b15b23d9bd0ff21014b24cb5af98c69b1299780bd4337965c3b092b406e909324e598643d23aed63abf4390039070108245
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
data/.rubocop_todo.yml
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# This configuration was generated by `rubocop --auto-gen-config`
|
|
2
|
+
# on 2015-06-22 10:12:11 -0400 using RuboCop version 0.32.0.
|
|
3
|
+
# The point is for the user to remove these configuration records
|
|
4
|
+
# one by one as the offenses are removed from the code base.
|
|
5
|
+
# Note that changes in the inspected code, or installation of new
|
|
6
|
+
# versions of RuboCop, may require this file to be generated again.
|
|
7
|
+
|
|
8
|
+
# Offense count: 6
|
|
9
|
+
Metrics/AbcSize:
|
|
10
|
+
Max: 48
|
|
11
|
+
|
|
12
|
+
# Offense count: 2
|
|
13
|
+
# Configuration parameters: CountComments.
|
|
14
|
+
Metrics/ClassLength:
|
|
15
|
+
Max: 164
|
|
16
|
+
|
|
17
|
+
# Offense count: 2
|
|
18
|
+
Metrics/CyclomaticComplexity:
|
|
19
|
+
Max: 13
|
|
20
|
+
|
|
21
|
+
# Offense count: 1
|
|
22
|
+
# Configuration parameters: AllowURI, URISchemes.
|
|
23
|
+
Metrics/LineLength:
|
|
24
|
+
Max: 86
|
|
25
|
+
|
|
26
|
+
# Offense count: 6
|
|
27
|
+
# Configuration parameters: CountComments.
|
|
28
|
+
Metrics/MethodLength:
|
|
29
|
+
Max: 55
|
|
30
|
+
|
|
31
|
+
# Offense count: 2
|
|
32
|
+
Metrics/PerceivedComplexity:
|
|
33
|
+
Max: 15
|
|
34
|
+
|
|
35
|
+
# Offense count: 12
|
|
36
|
+
Style/ClassVars:
|
|
37
|
+
Enabled: false
|
|
38
|
+
|
|
39
|
+
# Offense count: 8
|
|
40
|
+
Style/Documentation:
|
|
41
|
+
Enabled: false
|
|
42
|
+
|
|
43
|
+
# Offense count: 1
|
|
44
|
+
# Configuration parameters: MinBodyLength.
|
|
45
|
+
Style/GuardClause:
|
|
46
|
+
Enabled: false
|
|
47
|
+
|
|
48
|
+
# Offense count: 1
|
|
49
|
+
Style/ModuleFunction:
|
|
50
|
+
Enabled: false
|
|
51
|
+
|
|
52
|
+
# Offense count: 1
|
|
53
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
|
54
|
+
Style/RaiseArgs:
|
|
55
|
+
Enabled: false
|
data/CHANGELOG.md
ADDED
data/CONTRIBUTING.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Contributing
|
|
2
|
+
|
|
3
|
+
## Submitting an Issue
|
|
4
|
+
|
|
5
|
+
Issues are tracked on GitHub (TODO).
|
|
6
|
+
|
|
7
|
+
## Submitting a Pull Request
|
|
8
|
+
|
|
9
|
+
1. Fork the code (https://github.com/cisco/cisco_nxapi/fork) (TODO)
|
|
10
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
11
|
+
3. Write your code
|
|
12
|
+
4. Write minitest cases to cover your new code
|
|
13
|
+
5. Verify that minitest passes in full (`ruby tests/test_all_cisco.rb --
|
|
14
|
+
n3k_test.mycompany.com username password`)
|
|
15
|
+
6. Run rubocop (`rake rubocop`) and fix any failures.
|
|
16
|
+
7. Commit your changes (`git commit -am 'Add some feature'`)
|
|
17
|
+
8. Push to the branch (`git push origin my-new-feature`)
|
|
18
|
+
9. Create a new Pull Request
|
|
19
|
+
|
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright (c) 2014-2015 Cisco and/or its affiliates.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
data/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# CiscoNxapi - Cisco NX-API Utilities
|
|
2
|
+
|
|
3
|
+
The CiscoNxapi gem provides utilities for communicating with Cisco network
|
|
4
|
+
nodes running NX-OS 7.0(3)I2(1) and later via the NX-API management API.
|
|
5
|
+
|
|
6
|
+
This is a low-level library for direct communication with NX-API.
|
|
7
|
+
For a greater level of abstraction, use the [CiscoNodeUtils gem](https://rubygems.org/gems/cisco_node_utils).
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
To install the CiscoNxapi gem, use the following command:
|
|
12
|
+
|
|
13
|
+
$ gem install cisco_nxapi
|
|
14
|
+
|
|
15
|
+
(Add `sudo` if you're installing under a POSIX system as root)
|
|
16
|
+
|
|
17
|
+
Alternatively, if you've checked the source out directly, you can call
|
|
18
|
+
`rake install` from the root project directory.
|
|
19
|
+
|
|
20
|
+
## Examples
|
|
21
|
+
|
|
22
|
+
This gem can be used directly on a Cisco device (as used by Puppet and Chef)
|
|
23
|
+
or can run on a workstation and point to a Cisco device (as used by the
|
|
24
|
+
included minitest suite).
|
|
25
|
+
|
|
26
|
+
### Usage on a Cisco device
|
|
27
|
+
|
|
28
|
+
```ruby
|
|
29
|
+
require 'cisco_nxapi'
|
|
30
|
+
|
|
31
|
+
client = CiscoNxapi::NxapiClient.new()
|
|
32
|
+
|
|
33
|
+
client.show("show version")
|
|
34
|
+
|
|
35
|
+
client.config(["interface ethernet1/1",
|
|
36
|
+
"description Managed by NX-API",
|
|
37
|
+
"no shutdown"])
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Remote usage
|
|
41
|
+
|
|
42
|
+
```ruby
|
|
43
|
+
require 'cisco_nxapi'
|
|
44
|
+
|
|
45
|
+
client = CiscoNxapi::NxapiClient.new("n3k.mycompany.com",
|
|
46
|
+
"username", "password")
|
|
47
|
+
|
|
48
|
+
client.show("show version")
|
|
49
|
+
|
|
50
|
+
client.config(["interface ethernet1/1",
|
|
51
|
+
"description Managed by NX-API",
|
|
52
|
+
"no shutdown"])
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Changelog
|
|
56
|
+
|
|
57
|
+
See [CHANGELOG](CHANGELOG.md) for a list of changes.
|
|
58
|
+
|
|
59
|
+
## Contributing
|
|
60
|
+
|
|
61
|
+
See [CONTRIBUTING](CONTRIBUTING.md).
|
|
62
|
+
|
|
63
|
+
## License
|
|
64
|
+
|
|
65
|
+
Copyright (c) 2013-2015 Cisco and/or its affiliates.
|
|
66
|
+
|
|
67
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
68
|
+
you may not use this file except in compliance with the License.
|
|
69
|
+
You may obtain a copy of the License at
|
|
70
|
+
|
|
71
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
72
|
+
|
|
73
|
+
Unless required by applicable law or agreed to in writing, software
|
|
74
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
75
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
76
|
+
See the License for the specific language governing permissions and
|
|
77
|
+
limitations under the License.
|
data/Rakefile
ADDED
data/cisco_nxapi.gemspec
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'cisco_nxapi/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = 'cisco_nxapi'
|
|
8
|
+
spec.version = CiscoNxapi::VERSION
|
|
9
|
+
spec.authors = ['Alex Hunsberger', 'Glenn Matthews',
|
|
10
|
+
'Chris Van Heuveln', 'Mike Wiebe', 'Jie Yang']
|
|
11
|
+
spec.email = 'cisco_agent_gem@cisco.com'
|
|
12
|
+
spec.summary = 'Utilities for working with Cisco NX-OS NX-API'
|
|
13
|
+
spec.description = <<-EOF
|
|
14
|
+
Utilities for working with the Cisco NX-OS NX-API.
|
|
15
|
+
Designed to be used with Puppet and Chef and the cisco_node_utils gem.
|
|
16
|
+
EOF
|
|
17
|
+
spec.license = 'Apache-2.0'
|
|
18
|
+
|
|
19
|
+
spec.files = `git ls-files -z`.split("\x0")
|
|
20
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
21
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
22
|
+
spec.require_paths = ['lib']
|
|
23
|
+
|
|
24
|
+
spec.add_development_dependency 'minitest', '>= 2.5.1', '< 5.0.0'
|
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
|
26
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
|
27
|
+
spec.add_development_dependency 'rubocop', '>= 0.32'
|
|
28
|
+
spec.add_runtime_dependency 'net_http_unix', '~> 0.2', '>= 0.2.1'
|
|
29
|
+
end
|
data/lib/cisco_nxapi.rb
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Copyright (c) 2015 Cisco and/or its affiliates.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
require 'cisco_nxapi/version'
|
|
16
|
+
require 'cisco_nxapi/cisco_nxapi'
|
|
17
|
+
require 'cisco_nxapi/cisco_logger'
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
#
|
|
2
|
+
# Cisco Logger Library.
|
|
3
|
+
#
|
|
4
|
+
# January 2015, Jie Yang
|
|
5
|
+
#
|
|
6
|
+
# Copyright (c) 2015 Cisco and/or its affiliates.
|
|
7
|
+
#
|
|
8
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
9
|
+
# you may not use this file except in compliance with the License.
|
|
10
|
+
# You may obtain a copy of the License at
|
|
11
|
+
#
|
|
12
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
13
|
+
#
|
|
14
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
15
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
16
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17
|
+
# See the License for the specific language governing permissions and
|
|
18
|
+
# limitations under the License.
|
|
19
|
+
|
|
20
|
+
require 'logger'
|
|
21
|
+
|
|
22
|
+
module CiscoLogger
|
|
23
|
+
extend self
|
|
24
|
+
|
|
25
|
+
# Figure out what provider logging utility we
|
|
26
|
+
# should use: Puppet or Chef.
|
|
27
|
+
# If not found use the Ruby Logger/STDOUT/INFO.
|
|
28
|
+
if defined? (Puppet)
|
|
29
|
+
@@logger = Puppet
|
|
30
|
+
def error(string)
|
|
31
|
+
@@logger.err(string)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def warn(string)
|
|
35
|
+
@@logger.warning(string)
|
|
36
|
+
end
|
|
37
|
+
else
|
|
38
|
+
if defined? (Chef::Log)
|
|
39
|
+
@@logger = Chef::Log
|
|
40
|
+
else
|
|
41
|
+
@@logger = Logger.new(STDOUT)
|
|
42
|
+
@@logger.level = Logger::INFO
|
|
43
|
+
|
|
44
|
+
def debug_enable
|
|
45
|
+
@@logger.level = Logger::DEBUG
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def debug_disable
|
|
49
|
+
@@logger.level = Logger::INFO
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def error(string)
|
|
54
|
+
@@logger.error(string)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def warn(string)
|
|
58
|
+
@@logger.warn(string)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def debug(string)
|
|
63
|
+
@@logger.debug(string)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def info(string)
|
|
67
|
+
@@logger.info(string)
|
|
68
|
+
end
|
|
69
|
+
end # module
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
# NXAPI client library.
|
|
2
|
+
#
|
|
3
|
+
# November 2014, Glenn F. Matthews
|
|
4
|
+
#
|
|
5
|
+
# Copyright (c) 2014-2015 Cisco and/or its affiliates.
|
|
6
|
+
#
|
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
# you may not use this file except in compliance with the License.
|
|
9
|
+
# You may obtain a copy of the License at
|
|
10
|
+
#
|
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
#
|
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
# See the License for the specific language governing permissions and
|
|
17
|
+
# limitations under the License.
|
|
18
|
+
|
|
19
|
+
require 'json'
|
|
20
|
+
require File.join(File.dirname(__FILE__), 'cisco_logger')
|
|
21
|
+
|
|
22
|
+
include CiscoLogger
|
|
23
|
+
require 'net/http'
|
|
24
|
+
|
|
25
|
+
module CiscoNxapi
|
|
26
|
+
class NxapiError < RuntimeError
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
class CliError < NxapiError
|
|
30
|
+
attr_reader :input, :msg, :code, :clierror, :previous
|
|
31
|
+
def initialize(input, msg, code, clierror, previous)
|
|
32
|
+
@input = input
|
|
33
|
+
@msg = msg
|
|
34
|
+
@code = code
|
|
35
|
+
@clierror = clierror
|
|
36
|
+
@previous = previous
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def to_s
|
|
40
|
+
"CliError: '#{@input}' rejected with message: '#{clierror}'"
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def message
|
|
44
|
+
to_s
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
class RequestNotSupported < NxapiError
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
class ConnectionRefused < NxapiError
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
class HTTPBadRequest < NxapiError
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
class HTTPUnauthorized < NxapiError
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Location of unix domain socket for NXAPI localhost
|
|
61
|
+
NXAPI_UDS = '/tmp/nginx_local/nginx_1_be_nxapi.sock'
|
|
62
|
+
# NXAPI listens for remote connections to "http://<switch IP>/ins"
|
|
63
|
+
# NXAPI listens for local connections to "http://<UDS>/ins_local"
|
|
64
|
+
NXAPI_REMOTE_URI_PATH = '/ins'
|
|
65
|
+
NXAPI_UDS_URI_PATH = '/ins_local'
|
|
66
|
+
# Latest supported version is 1.0
|
|
67
|
+
NXAPI_VERSION = '1.0'
|
|
68
|
+
|
|
69
|
+
class NxapiClient
|
|
70
|
+
# Constructor for NxapiClient. By default this connects to the local
|
|
71
|
+
# unix domain socket. If you need to connect to a remote device,
|
|
72
|
+
# you must provide the address/username/password parameters.
|
|
73
|
+
def initialize(address = nil, username = nil, password = nil)
|
|
74
|
+
# Default: connect to unix domain socket on localhost, if available
|
|
75
|
+
if address.nil?
|
|
76
|
+
if File.socket?(NXAPI_UDS)
|
|
77
|
+
# net_http_unix provides NetX::HTTPUnix, a small subclass of Net::HTTP
|
|
78
|
+
# which supports connection to local unix domain sockets. We need this
|
|
79
|
+
# in order to run natively under NX-OS but it's not needed for off-box
|
|
80
|
+
# unit testing where the base Net::HTTP will meet our needs.
|
|
81
|
+
require 'net_http_unix'
|
|
82
|
+
@http = NetX::HTTPUnix.new('unix://' + NXAPI_UDS)
|
|
83
|
+
else
|
|
84
|
+
fail "No address specified but no UDS found at #{NXAPI_UDS} either"
|
|
85
|
+
end
|
|
86
|
+
else
|
|
87
|
+
fail TypeError, 'invalid address' unless address.is_a?(String)
|
|
88
|
+
fail ArgumentError, 'empty address' if address.empty?
|
|
89
|
+
# Remote connection. This is primarily expected
|
|
90
|
+
# when running e.g. from a Unix server as part of Minitest.
|
|
91
|
+
@http = Net::HTTP.new(address)
|
|
92
|
+
# In this case, a username and password are mandatory
|
|
93
|
+
fail TypeError if username.nil? || password.nil?
|
|
94
|
+
end
|
|
95
|
+
unless username.nil?
|
|
96
|
+
fail TypeError, 'invalid username' unless username.is_a?(String)
|
|
97
|
+
fail ArgumentError, 'empty username' unless username.length > 0
|
|
98
|
+
end
|
|
99
|
+
unless password.nil?
|
|
100
|
+
fail TypeError, 'invalid password' unless password.is_a?(String)
|
|
101
|
+
fail ArgumentError, 'empty password' unless password.length > 0
|
|
102
|
+
end
|
|
103
|
+
@username = username
|
|
104
|
+
@password = password
|
|
105
|
+
@cache_enable = true
|
|
106
|
+
@cache_auto = true
|
|
107
|
+
cache_flush
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def to_s
|
|
111
|
+
@http.address
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def inspect
|
|
115
|
+
"<NxapiClient of #{@http.address}>"
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def reload
|
|
119
|
+
# no-op for now
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def cache_enable?
|
|
123
|
+
@cache_enable
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def cache_enable=(enable)
|
|
127
|
+
@cache_enable = enable
|
|
128
|
+
cache_flush unless enable
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def cache_auto?
|
|
132
|
+
@cache_auto
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
attr_writer :cache_auto
|
|
136
|
+
|
|
137
|
+
# Clear the cache of CLI output results.
|
|
138
|
+
#
|
|
139
|
+
# If cache_auto is true (default) then this will be performed automatically
|
|
140
|
+
# whenever a config() or exec() is called, but providers may also call this
|
|
141
|
+
# to explicitly force the cache to be cleared.
|
|
142
|
+
def cache_flush
|
|
143
|
+
@cache_hash = {
|
|
144
|
+
'cli_conf' => {},
|
|
145
|
+
'cli_show' => {},
|
|
146
|
+
'cli_show_ascii' => {}
|
|
147
|
+
}
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# Configure the given command(s) on the device.
|
|
151
|
+
#
|
|
152
|
+
# @raise [CiscoNxapi::CliError] if any command is rejected by the device
|
|
153
|
+
#
|
|
154
|
+
# @param commands [String, Array<String>] either of:
|
|
155
|
+
# 1) The configuration sequence, as a newline-separated string
|
|
156
|
+
# 2) An array of command strings (one command per string, no newlines)
|
|
157
|
+
def config(commands)
|
|
158
|
+
cache_flush if cache_auto?
|
|
159
|
+
|
|
160
|
+
if commands.is_a?(String)
|
|
161
|
+
commands = commands.split(/\n/)
|
|
162
|
+
elsif !commands.is_a?(Array)
|
|
163
|
+
fail TypeError
|
|
164
|
+
end
|
|
165
|
+
req('cli_conf', commands)
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
# Executes a command in exec mode on the device.
|
|
169
|
+
#
|
|
170
|
+
# If cache_auto? (on by default) is set then the CLI cache will be flushed.
|
|
171
|
+
#
|
|
172
|
+
# For "show" commands please use show() instead of exec().
|
|
173
|
+
#
|
|
174
|
+
# @param command [String] the exec command to execute
|
|
175
|
+
# @return [String, nil] the body of the output of the exec command
|
|
176
|
+
# (if any)
|
|
177
|
+
def exec(command)
|
|
178
|
+
cache_flush if cache_auto?
|
|
179
|
+
req('cli_show_ascii', command)
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
# Executes a "show" command on the device, returning either ASCII or
|
|
183
|
+
# structured output.
|
|
184
|
+
#
|
|
185
|
+
# Unlike config() and exec() this will not clear the CLI cache;
|
|
186
|
+
# multiple calls to the same "show" command may return cached data
|
|
187
|
+
# rather than querying the device repeatedly.
|
|
188
|
+
#
|
|
189
|
+
# @raise [CiscoNxapi::RequestNotSupported] if
|
|
190
|
+
# structured output is requested but the given command can't provide it.
|
|
191
|
+
# @raise [CiscoNxapi::CliError] if the command is rejected by the device
|
|
192
|
+
#
|
|
193
|
+
# @param command [String] the show command to execute
|
|
194
|
+
# @param type [:ascii, :structured] ASCII or structured output.
|
|
195
|
+
# Default is :ascii
|
|
196
|
+
# @return [String] the output of the show command, if type == :ascii
|
|
197
|
+
# @return [Hash{String=>String}] key-value pairs, if type == :structured
|
|
198
|
+
def show(command, type = :ascii)
|
|
199
|
+
if type == :ascii
|
|
200
|
+
return req('cli_show_ascii', command)
|
|
201
|
+
elsif type == :structured
|
|
202
|
+
return req('cli_show', command)
|
|
203
|
+
else
|
|
204
|
+
fail TypeError
|
|
205
|
+
end
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
# @raise CiscoNxapi::ConnectionRefused if NXAPI is disabled
|
|
209
|
+
# @raise CiscoNxapi::HTTPUnauthorized if username/password are invalid
|
|
210
|
+
# @raise CiscoNxapi::HTTPBadRequest (should never occur)
|
|
211
|
+
# @raise CiscoNxapi::RequestNotSupported
|
|
212
|
+
# @raise CiscoNxapi::CliError if any command is rejected as invalid
|
|
213
|
+
def req(type, command_or_list)
|
|
214
|
+
if command_or_list.is_a?(Array)
|
|
215
|
+
# NXAPI wants config lines to be separated by ' ; '
|
|
216
|
+
command = command_or_list.join(' ; ')
|
|
217
|
+
else
|
|
218
|
+
command = command_or_list
|
|
219
|
+
command_or_list = [command]
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
debug("Input (#{type}): \'#{command}\'")
|
|
223
|
+
if cache_enable? && @cache_hash[type] && @cache_hash[type][command]
|
|
224
|
+
return @cache_hash[type][command]
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
# form the request
|
|
228
|
+
if !(@username.nil? || @password.nil?)
|
|
229
|
+
request = Net::HTTP::Post.new(NXAPI_REMOTE_URI_PATH)
|
|
230
|
+
request.basic_auth("#{@username}", "#{@password}")
|
|
231
|
+
else
|
|
232
|
+
request = Net::HTTP::Post.new(NXAPI_UDS_URI_PATH)
|
|
233
|
+
request['Cookie'] = 'nxapi_auth=admin:local'
|
|
234
|
+
end
|
|
235
|
+
request.content_type = 'application/json'
|
|
236
|
+
request.body = {
|
|
237
|
+
'ins_api' => {
|
|
238
|
+
'version' => NXAPI_VERSION,
|
|
239
|
+
'type' => "#{type}",
|
|
240
|
+
'chunk' => '0',
|
|
241
|
+
'sid' => '1',
|
|
242
|
+
'input' => "#{command}",
|
|
243
|
+
'output_format' => 'json'
|
|
244
|
+
}
|
|
245
|
+
}.to_json
|
|
246
|
+
|
|
247
|
+
# send the request and get the response
|
|
248
|
+
debug("Sending HTTP request to NX-API at #{@http.address}:\n" +
|
|
249
|
+
"#{request.to_hash}\n#{request.body}")
|
|
250
|
+
begin
|
|
251
|
+
response = @http.request(request)
|
|
252
|
+
rescue Errno::ECONNREFUSED, Errno::ECONNRESET
|
|
253
|
+
emsg = 'Connection refused or reset. Is the NX-API feature enabled?'
|
|
254
|
+
raise ConnectionRefused, emsg
|
|
255
|
+
end
|
|
256
|
+
handle_http_response(response)
|
|
257
|
+
body = JSON.parse(response.body)
|
|
258
|
+
# In case of an error the JSON may not be complete, so we need to
|
|
259
|
+
# proceed carefully, as blindly doing body["ins_api"]["outputs"]["output"]
|
|
260
|
+
# could throw an error otherwise.
|
|
261
|
+
output = body['ins_api']
|
|
262
|
+
fail NxapiError, "unexpected JSON output:\n#{body}" unless output
|
|
263
|
+
output = output['outputs'] if output['outputs']
|
|
264
|
+
output = output['output'] if output['output']
|
|
265
|
+
|
|
266
|
+
prev_cmds = []
|
|
267
|
+
if output.is_a?(Array)
|
|
268
|
+
output.zip(command_or_list) do |o, cmd|
|
|
269
|
+
handle_output(prev_cmds, cmd, o)
|
|
270
|
+
prev_cmds << cmd
|
|
271
|
+
end
|
|
272
|
+
output = output.each { |o| o['body'] }
|
|
273
|
+
else
|
|
274
|
+
handle_output(prev_cmds, command, output)
|
|
275
|
+
output = output['body']
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
@cache_hash[type][command] = output if cache_enable?
|
|
279
|
+
output
|
|
280
|
+
end
|
|
281
|
+
private :req
|
|
282
|
+
|
|
283
|
+
def handle_http_response(response)
|
|
284
|
+
debug("HTTP Response: #{response.message}\n#{response.body}")
|
|
285
|
+
case response
|
|
286
|
+
when Net::HTTPUnauthorized
|
|
287
|
+
emsg = 'HTTP 401 Unauthorized. Are your NX-API credentials correct?'
|
|
288
|
+
fail CiscoNxapi::HTTPUnauthorized, emsg
|
|
289
|
+
when Net::HTTPBadRequest
|
|
290
|
+
emsg = "HTTP 400 Bad Request\n#{response.body}"
|
|
291
|
+
fail CiscoNxapi::HTTPBadRequest, emsg
|
|
292
|
+
end
|
|
293
|
+
end
|
|
294
|
+
private :handle_http_response
|
|
295
|
+
|
|
296
|
+
def handle_output(prev_cmds, command, output)
|
|
297
|
+
if output['code'] == '400'
|
|
298
|
+
# CLI error.
|
|
299
|
+
# Examples: "Invalid input", "Incomplete command", etc.
|
|
300
|
+
fail CliError.new(command, output['msg'], output['code'],
|
|
301
|
+
output['clierror'], prev_cmds)
|
|
302
|
+
elsif output['code'] == '501'
|
|
303
|
+
# if structured output is not supported for this command,
|
|
304
|
+
# raise an exception so that the calling function can
|
|
305
|
+
# handle accordingly
|
|
306
|
+
fail RequestNotSupported.new(
|
|
307
|
+
"Structured output not supported for #{command}")
|
|
308
|
+
else
|
|
309
|
+
debug("Result for '#{command}': #{output['msg']}")
|
|
310
|
+
if output['body'] && !output['body'].empty?
|
|
311
|
+
debug("Output: #{output['body']}")
|
|
312
|
+
end
|
|
313
|
+
end
|
|
314
|
+
end
|
|
315
|
+
private :handle_output
|
|
316
|
+
end
|
|
317
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Copyright (c) 2015 Cisco and/or its affiliates.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
module CiscoNxapi
|
|
16
|
+
VERSION = '0.9.0'
|
|
17
|
+
end
|
data/tests/basetest.rb
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
#
|
|
3
|
+
# Basic unit test case class.
|
|
4
|
+
# December 2014, Glenn F. Matthews
|
|
5
|
+
#
|
|
6
|
+
# Copyright (c) 2014-2015 Cisco and/or its affiliates.
|
|
7
|
+
#
|
|
8
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
9
|
+
# you may not use this file except in compliance with the License.
|
|
10
|
+
# You may obtain a copy of the License at
|
|
11
|
+
#
|
|
12
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
13
|
+
#
|
|
14
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
15
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
16
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17
|
+
# See the License for the specific language governing permissions and
|
|
18
|
+
# limitations under the License.
|
|
19
|
+
|
|
20
|
+
require 'rubygems'
|
|
21
|
+
gem 'minitest', '>= 2.5.1', '< 5.0.0'
|
|
22
|
+
require 'minitest/autorun'
|
|
23
|
+
require 'net/telnet'
|
|
24
|
+
require 'test/unit'
|
|
25
|
+
require File.expand_path('../../lib/cisco_nxapi/cisco_logger', __FILE__)
|
|
26
|
+
|
|
27
|
+
class TestCase < Test::Unit::TestCase
|
|
28
|
+
@@address = nil
|
|
29
|
+
@@username = nil
|
|
30
|
+
@@password = nil
|
|
31
|
+
|
|
32
|
+
def process_arguments
|
|
33
|
+
if ARGV.length != 3 && ARGV.length != 4
|
|
34
|
+
puts 'Usage:'
|
|
35
|
+
puts ' ruby test_nxapi.rb [options] -- <address> <username> <password> [debug]'
|
|
36
|
+
exit
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Record the version of Ruby we got invoked with.
|
|
40
|
+
puts "\nRuby Version - #{RUBY_VERSION}"
|
|
41
|
+
|
|
42
|
+
@@address = ARGV[0]
|
|
43
|
+
@@username = ARGV[1]
|
|
44
|
+
@@password = ARGV[2]
|
|
45
|
+
if ARGV.length == 4
|
|
46
|
+
if ARGV[3] == 'debug'
|
|
47
|
+
CiscoLogger.debug_enable
|
|
48
|
+
else
|
|
49
|
+
puts "Only 'debug' is allowed"
|
|
50
|
+
exit
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# setup-once params
|
|
56
|
+
def address
|
|
57
|
+
process_arguments unless @@address
|
|
58
|
+
@@address
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def username
|
|
62
|
+
process_arguments unless @@username
|
|
63
|
+
@@username
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def password
|
|
67
|
+
process_arguments unless @@password
|
|
68
|
+
@@password
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def setup
|
|
72
|
+
@device = Net::Telnet.new('Host' => address, 'Timeout' => 240)
|
|
73
|
+
@device.login(username, password)
|
|
74
|
+
rescue Errno::ECONNREFUSED
|
|
75
|
+
puts 'Connection refused - please check that the IP address is correct'
|
|
76
|
+
puts " and that you have enabled 'feature telnet' on the UUT"
|
|
77
|
+
exit
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def teardown
|
|
81
|
+
@device.close unless @device.nil?
|
|
82
|
+
GC.start
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def test_placeholder
|
|
86
|
+
# needed so that we don't get a "no tests were specified" error
|
|
87
|
+
end
|
|
88
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
#
|
|
3
|
+
# One-stop shop for running all of our current test cases.
|
|
4
|
+
# January 2015, Jie Yang
|
|
5
|
+
#
|
|
6
|
+
# Copyright (c) 2015 Cisco and/or its affiliates.
|
|
7
|
+
#
|
|
8
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
9
|
+
# you may not use this file except in compliance with the License.
|
|
10
|
+
# You may obtain a copy of the License at
|
|
11
|
+
#
|
|
12
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
13
|
+
#
|
|
14
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
15
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
16
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17
|
+
# See the License for the specific language governing permissions and
|
|
18
|
+
# limitations under the License.
|
|
19
|
+
|
|
20
|
+
require 'rubygems'
|
|
21
|
+
gem 'minitest', '>= 2.5.1', '< 5.0.0'
|
|
22
|
+
require 'minitest/autorun'
|
|
23
|
+
|
|
24
|
+
require File.expand_path('../test_nxapi', __FILE__)
|
|
25
|
+
require File.expand_path('../test_logger', __FILE__)
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# Cisco Logger Unit Tests
|
|
2
|
+
#
|
|
3
|
+
# January 2015, Jie Yang
|
|
4
|
+
#
|
|
5
|
+
# Copyright (c) 2015 Cisco and/or its affiliates.
|
|
6
|
+
#
|
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
# you may not use this file except in compliance with the License.
|
|
9
|
+
# You may obtain a copy of the License at
|
|
10
|
+
#
|
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
#
|
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
# See the License for the specific language governing permissions and
|
|
17
|
+
# limitations under the License.
|
|
18
|
+
|
|
19
|
+
require File.expand_path('../basetest', __FILE__)
|
|
20
|
+
require File.expand_path('../../lib/cisco_nxapi/cisco_logger', __FILE__)
|
|
21
|
+
|
|
22
|
+
class TestLogger < TestCase
|
|
23
|
+
def test_logger_methods
|
|
24
|
+
assert(defined?(CiscoLogger.debug), 'debug method not defined')
|
|
25
|
+
assert(defined?(CiscoLogger.info), 'info method not defined')
|
|
26
|
+
assert(defined?(CiscoLogger.warn), 'warning method not defined')
|
|
27
|
+
assert(defined?(CiscoLogger.error), 'error method not defined')
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Due to current limitation of minitest, the following test case actually
|
|
31
|
+
# will never fail. But it will print out all the messages that the tested
|
|
32
|
+
# functions are supposed to print. So I still keep this test here
|
|
33
|
+
def test_default_logger_output
|
|
34
|
+
CiscoLogger.debug_enable
|
|
35
|
+
assert_output { CiscoLogger.debug('Test default debug output') }
|
|
36
|
+
assert_output { CiscoLogger.info('Test default info output') }
|
|
37
|
+
assert_output { CiscoLogger.warn('Test default warn output') }
|
|
38
|
+
assert_output { CiscoLogger.error('Test default error output') }
|
|
39
|
+
CiscoLogger.debug_disable
|
|
40
|
+
end
|
|
41
|
+
end
|
data/tests/test_nxapi.rb
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
# NXAPI Client Unit Tests
|
|
2
|
+
#
|
|
3
|
+
# November 2014, Glenn F. Matthews
|
|
4
|
+
#
|
|
5
|
+
# Copyright (c) 2014-2015 Cisco and/or its affiliates.
|
|
6
|
+
#
|
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
# you may not use this file except in compliance with the License.
|
|
9
|
+
# You may obtain a copy of the License at
|
|
10
|
+
#
|
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
#
|
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
# See the License for the specific language governing permissions and
|
|
17
|
+
# limitations under the License.
|
|
18
|
+
|
|
19
|
+
require File.expand_path('../basetest', __FILE__)
|
|
20
|
+
require File.expand_path('../../lib/cisco_nxapi/cisco_nxapi', __FILE__)
|
|
21
|
+
|
|
22
|
+
class TestNxapi < TestCase
|
|
23
|
+
@@client = nil
|
|
24
|
+
|
|
25
|
+
def client
|
|
26
|
+
unless @@client
|
|
27
|
+
client = CiscoNxapi::NxapiClient.new(address, username, password)
|
|
28
|
+
client.cache_enable = true
|
|
29
|
+
client.cache_auto = true
|
|
30
|
+
@@client = client
|
|
31
|
+
end
|
|
32
|
+
@@client
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Test cases for new NXAPI client APIs
|
|
36
|
+
|
|
37
|
+
def test_config_string
|
|
38
|
+
client.config("int et1/1\ndescr panda\n")
|
|
39
|
+
run = client.show('show run int et1/1')
|
|
40
|
+
desc = run.match(/description (.*)/)[1]
|
|
41
|
+
assert_equal(desc, 'panda')
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def test_config_array
|
|
45
|
+
client.config(['int et1/1', 'descr elephant'])
|
|
46
|
+
run = client.show('show run int et1/1')
|
|
47
|
+
desc = run.match(/description (.*)/)[1]
|
|
48
|
+
assert_equal(desc, 'elephant')
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def test_config_invalid
|
|
52
|
+
e = assert_raises CiscoNxapi::CliError do
|
|
53
|
+
client.config(['int et1/1', 'exit', 'int et1/2', 'plover'])
|
|
54
|
+
end
|
|
55
|
+
assert_equal('plover', e.input)
|
|
56
|
+
assert_equal('CLI execution error', e.msg)
|
|
57
|
+
assert_equal('400', e.code)
|
|
58
|
+
assert_equal("% Invalid command\n", e.clierror)
|
|
59
|
+
assert_equal(['int et1/1', 'exit', 'int et1/2'], e.previous)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def test_exec
|
|
63
|
+
result = client.exec('echo hello')
|
|
64
|
+
assert_equal(result.strip, 'hello')
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def test_exec_invalid
|
|
68
|
+
e = assert_raises CiscoNxapi::CliError do
|
|
69
|
+
client.exec('xyzzy')
|
|
70
|
+
end
|
|
71
|
+
assert_equal('xyzzy', e.input)
|
|
72
|
+
assert_equal('Input CLI command error', e.msg)
|
|
73
|
+
assert_equal('400', e.code)
|
|
74
|
+
assert_match(/Syntax error/, e.clierror)
|
|
75
|
+
assert_equal([], e.previous)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def test_exec_too_long
|
|
79
|
+
assert_raises CiscoNxapi::NxapiError do
|
|
80
|
+
client.exec('0' * 500_000)
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def test_show_ascii_default
|
|
85
|
+
result = client.show('show hostname')
|
|
86
|
+
s = @device.cmd('show hostname')
|
|
87
|
+
assert_equal(result.strip, s.split("\n")[1].strip)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def test_show_ascii_invalid
|
|
91
|
+
assert_raises CiscoNxapi::CliError do
|
|
92
|
+
client.show('show plugh')
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def test_element_show_ascii_incomplete
|
|
97
|
+
assert_raises CiscoNxapi::CliError do
|
|
98
|
+
client.show('show ')
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def test_show_ascii_explicit
|
|
103
|
+
result = client.show('show hostname', :ascii)
|
|
104
|
+
s = @device.cmd('show hostname')
|
|
105
|
+
assert_equal(result.strip, s.split("\n")[1].strip)
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def test_show_structured
|
|
109
|
+
result = client.show('show hostname', :structured)
|
|
110
|
+
s = @device.cmd('show hostname')
|
|
111
|
+
assert_equal(result['hostname'], s.split("\n")[1].strip)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def test_show_structured_invalid
|
|
115
|
+
assert_raises CiscoNxapi::CliError do
|
|
116
|
+
client.show('show frobozz', :structured)
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def test_show_structured_unsupported
|
|
121
|
+
# TBD: n3k DOES support structured for this command,
|
|
122
|
+
# n9k DOES NOT support structured for this command
|
|
123
|
+
assert_raises CiscoNxapi::RequestNotSupported do
|
|
124
|
+
client.show('show snmp internal globals', :structured)
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def test_connection_refused
|
|
129
|
+
@device.cmd('configure terminal')
|
|
130
|
+
@device.cmd('no feature nxapi')
|
|
131
|
+
@device.cmd('end')
|
|
132
|
+
client.cache_flush
|
|
133
|
+
assert_raises CiscoNxapi::ConnectionRefused do
|
|
134
|
+
client.show('show version')
|
|
135
|
+
end
|
|
136
|
+
assert_raises CiscoNxapi::ConnectionRefused do
|
|
137
|
+
client.exec('show version')
|
|
138
|
+
end
|
|
139
|
+
assert_raises CiscoNxapi::ConnectionRefused do
|
|
140
|
+
client.config('interface Et1/1')
|
|
141
|
+
end
|
|
142
|
+
# On the off chance that things behave differently when NXAPI is
|
|
143
|
+
# disabled while we're connected, versus trying to connect afresh...
|
|
144
|
+
@@client = nil
|
|
145
|
+
assert_raises CiscoNxapi::ConnectionRefused do
|
|
146
|
+
client.show('show version')
|
|
147
|
+
end
|
|
148
|
+
assert_raises CiscoNxapi::ConnectionRefused do
|
|
149
|
+
client.exec('show version')
|
|
150
|
+
end
|
|
151
|
+
assert_raises CiscoNxapi::ConnectionRefused do
|
|
152
|
+
client.config('interface Et1/1')
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
@device.cmd('configure terminal')
|
|
156
|
+
@device.cmd('feature nxapi')
|
|
157
|
+
@device.cmd('end')
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def test_unauthorized
|
|
161
|
+
def client.password=(new) # rubocop:disable Style/TrivialAccessors
|
|
162
|
+
@password = new
|
|
163
|
+
end
|
|
164
|
+
client.password = 'wrong_password'
|
|
165
|
+
client.cache_flush
|
|
166
|
+
assert_raises CiscoNxapi::HTTPUnauthorized do
|
|
167
|
+
client.show('show version')
|
|
168
|
+
end
|
|
169
|
+
assert_raises CiscoNxapi::HTTPUnauthorized do
|
|
170
|
+
client.exec('show version')
|
|
171
|
+
end
|
|
172
|
+
assert_raises CiscoNxapi::HTTPUnauthorized do
|
|
173
|
+
client.config('interface Et1/1')
|
|
174
|
+
end
|
|
175
|
+
client.password = password
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def test_unsupported
|
|
179
|
+
# Add a method to the NXAPI that sends a request of invalid type
|
|
180
|
+
def client.hello
|
|
181
|
+
req('hello', 'world')
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
assert_raises CiscoNxapi::RequestNotSupported do
|
|
185
|
+
client.hello
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: cisco_nxapi
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.9.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Alex Hunsberger
|
|
8
|
+
- Glenn Matthews
|
|
9
|
+
- Chris Van Heuveln
|
|
10
|
+
- Mike Wiebe
|
|
11
|
+
- Jie Yang
|
|
12
|
+
autorequire:
|
|
13
|
+
bindir: bin
|
|
14
|
+
cert_chain: []
|
|
15
|
+
date: 2015-07-16 00:00:00.000000000 Z
|
|
16
|
+
dependencies:
|
|
17
|
+
- !ruby/object:Gem::Dependency
|
|
18
|
+
name: minitest
|
|
19
|
+
requirement: !ruby/object:Gem::Requirement
|
|
20
|
+
requirements:
|
|
21
|
+
- - ">="
|
|
22
|
+
- !ruby/object:Gem::Version
|
|
23
|
+
version: 2.5.1
|
|
24
|
+
- - "<"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 5.0.0
|
|
27
|
+
type: :development
|
|
28
|
+
prerelease: false
|
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: 2.5.1
|
|
34
|
+
- - "<"
|
|
35
|
+
- !ruby/object:Gem::Version
|
|
36
|
+
version: 5.0.0
|
|
37
|
+
- !ruby/object:Gem::Dependency
|
|
38
|
+
name: bundler
|
|
39
|
+
requirement: !ruby/object:Gem::Requirement
|
|
40
|
+
requirements:
|
|
41
|
+
- - "~>"
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: '1.7'
|
|
44
|
+
type: :development
|
|
45
|
+
prerelease: false
|
|
46
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
47
|
+
requirements:
|
|
48
|
+
- - "~>"
|
|
49
|
+
- !ruby/object:Gem::Version
|
|
50
|
+
version: '1.7'
|
|
51
|
+
- !ruby/object:Gem::Dependency
|
|
52
|
+
name: rake
|
|
53
|
+
requirement: !ruby/object:Gem::Requirement
|
|
54
|
+
requirements:
|
|
55
|
+
- - "~>"
|
|
56
|
+
- !ruby/object:Gem::Version
|
|
57
|
+
version: '10.0'
|
|
58
|
+
type: :development
|
|
59
|
+
prerelease: false
|
|
60
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
61
|
+
requirements:
|
|
62
|
+
- - "~>"
|
|
63
|
+
- !ruby/object:Gem::Version
|
|
64
|
+
version: '10.0'
|
|
65
|
+
- !ruby/object:Gem::Dependency
|
|
66
|
+
name: rubocop
|
|
67
|
+
requirement: !ruby/object:Gem::Requirement
|
|
68
|
+
requirements:
|
|
69
|
+
- - ">="
|
|
70
|
+
- !ruby/object:Gem::Version
|
|
71
|
+
version: '0.32'
|
|
72
|
+
type: :development
|
|
73
|
+
prerelease: false
|
|
74
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
75
|
+
requirements:
|
|
76
|
+
- - ">="
|
|
77
|
+
- !ruby/object:Gem::Version
|
|
78
|
+
version: '0.32'
|
|
79
|
+
- !ruby/object:Gem::Dependency
|
|
80
|
+
name: net_http_unix
|
|
81
|
+
requirement: !ruby/object:Gem::Requirement
|
|
82
|
+
requirements:
|
|
83
|
+
- - "~>"
|
|
84
|
+
- !ruby/object:Gem::Version
|
|
85
|
+
version: '0.2'
|
|
86
|
+
- - ">="
|
|
87
|
+
- !ruby/object:Gem::Version
|
|
88
|
+
version: 0.2.1
|
|
89
|
+
type: :runtime
|
|
90
|
+
prerelease: false
|
|
91
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
92
|
+
requirements:
|
|
93
|
+
- - "~>"
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
version: '0.2'
|
|
96
|
+
- - ">="
|
|
97
|
+
- !ruby/object:Gem::Version
|
|
98
|
+
version: 0.2.1
|
|
99
|
+
description: |
|
|
100
|
+
Utilities for working with the Cisco NX-OS NX-API.
|
|
101
|
+
Designed to be used with Puppet and Chef and the cisco_node_utils gem.
|
|
102
|
+
email: cisco_agent_gem@cisco.com
|
|
103
|
+
executables: []
|
|
104
|
+
extensions: []
|
|
105
|
+
extra_rdoc_files: []
|
|
106
|
+
files:
|
|
107
|
+
- ".gitignore"
|
|
108
|
+
- ".rubocop.yml"
|
|
109
|
+
- ".rubocop_todo.yml"
|
|
110
|
+
- CHANGELOG.md
|
|
111
|
+
- CONTRIBUTING.md
|
|
112
|
+
- Gemfile
|
|
113
|
+
- LICENSE
|
|
114
|
+
- README.md
|
|
115
|
+
- Rakefile
|
|
116
|
+
- cisco_nxapi.gemspec
|
|
117
|
+
- lib/cisco_nxapi.rb
|
|
118
|
+
- lib/cisco_nxapi/cisco_logger.rb
|
|
119
|
+
- lib/cisco_nxapi/cisco_nxapi.rb
|
|
120
|
+
- lib/cisco_nxapi/version.rb
|
|
121
|
+
- tests/basetest.rb
|
|
122
|
+
- tests/test_all_cisco.rb
|
|
123
|
+
- tests/test_logger.rb
|
|
124
|
+
- tests/test_nxapi.rb
|
|
125
|
+
homepage:
|
|
126
|
+
licenses:
|
|
127
|
+
- Apache-2.0
|
|
128
|
+
metadata: {}
|
|
129
|
+
post_install_message:
|
|
130
|
+
rdoc_options: []
|
|
131
|
+
require_paths:
|
|
132
|
+
- lib
|
|
133
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
134
|
+
requirements:
|
|
135
|
+
- - ">="
|
|
136
|
+
- !ruby/object:Gem::Version
|
|
137
|
+
version: '0'
|
|
138
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
139
|
+
requirements:
|
|
140
|
+
- - ">="
|
|
141
|
+
- !ruby/object:Gem::Version
|
|
142
|
+
version: '0'
|
|
143
|
+
requirements: []
|
|
144
|
+
rubyforge_project:
|
|
145
|
+
rubygems_version: 2.2.2
|
|
146
|
+
signing_key:
|
|
147
|
+
specification_version: 4
|
|
148
|
+
summary: Utilities for working with Cisco NX-OS NX-API
|
|
149
|
+
test_files: []
|