cisco_node_utils 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.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +2 -0
  3. data/.rubocop.yml +3 -0
  4. data/.rubocop_todo.yml +293 -0
  5. data/CHANGELOG.md +5 -0
  6. data/CONTRIBUTING.md +31 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE +201 -0
  9. data/README.md +113 -0
  10. data/Rakefile +4 -0
  11. data/cisco_node_utils.gemspec +30 -0
  12. data/lib/cisco_node_utils.rb +33 -0
  13. data/lib/cisco_node_utils/README_YAML.md +333 -0
  14. data/lib/cisco_node_utils/cisco_cmn_utils.rb +92 -0
  15. data/lib/cisco_node_utils/command_reference.rb +415 -0
  16. data/lib/cisco_node_utils/command_reference_common.yaml +845 -0
  17. data/lib/cisco_node_utils/command_reference_n3064.yaml +13 -0
  18. data/lib/cisco_node_utils/command_reference_n7k.yaml +48 -0
  19. data/lib/cisco_node_utils/command_reference_n9k.yaml +35 -0
  20. data/lib/cisco_node_utils/configparser_lib.rb +196 -0
  21. data/lib/cisco_node_utils/interface.rb +501 -0
  22. data/lib/cisco_node_utils/interface_ospf.rb +241 -0
  23. data/lib/cisco_node_utils/node.rb +673 -0
  24. data/lib/cisco_node_utils/platform.rb +184 -0
  25. data/lib/cisco_node_utils/platform_info.rb +58 -0
  26. data/lib/cisco_node_utils/platform_info.yaml +10 -0
  27. data/lib/cisco_node_utils/router_ospf.rb +96 -0
  28. data/lib/cisco_node_utils/router_ospf_vrf.rb +258 -0
  29. data/lib/cisco_node_utils/snmpcommunity.rb +91 -0
  30. data/lib/cisco_node_utils/snmpgroup.rb +55 -0
  31. data/lib/cisco_node_utils/snmpserver.rb +150 -0
  32. data/lib/cisco_node_utils/snmpuser.rb +342 -0
  33. data/lib/cisco_node_utils/tacacs_server.rb +175 -0
  34. data/lib/cisco_node_utils/tacacs_server_host.rb +128 -0
  35. data/lib/cisco_node_utils/version.rb +17 -0
  36. data/lib/cisco_node_utils/vlan.rb +153 -0
  37. data/lib/cisco_node_utils/vtp.rb +127 -0
  38. data/lib/cisco_node_utils/yum.rb +84 -0
  39. data/tests/basetest.rb +93 -0
  40. data/tests/ciscotest.rb +136 -0
  41. data/tests/cmd_config.yaml +51 -0
  42. data/tests/cmd_config_invalid.yaml +16 -0
  43. data/tests/test_all_cisco.rb +46 -0
  44. data/tests/test_command_config.rb +192 -0
  45. data/tests/test_command_reference.rb +222 -0
  46. data/tests/test_interface.rb +1017 -0
  47. data/tests/test_interface_ospf.rb +763 -0
  48. data/tests/test_interface_svi.rb +267 -0
  49. data/tests/test_interface_switchport.rb +722 -0
  50. data/tests/test_node.rb +108 -0
  51. data/tests/test_node_ext.rb +450 -0
  52. data/tests/test_platform.rb +188 -0
  53. data/tests/test_router_ospf.rb +164 -0
  54. data/tests/test_router_ospf_vrf.rb +753 -0
  55. data/tests/test_snmpcommunity.rb +344 -0
  56. data/tests/test_snmpgroup.rb +71 -0
  57. data/tests/test_snmpserver.rb +443 -0
  58. data/tests/test_snmpuser.rb +803 -0
  59. data/tests/test_tacacs_server.rb +388 -0
  60. data/tests/test_tacacs_server_host.rb +391 -0
  61. data/tests/test_vlan.rb +264 -0
  62. data/tests/test_vtp.rb +319 -0
  63. data/tests/test_yum.rb +106 -0
  64. metadata +188 -0
@@ -0,0 +1,175 @@
1
+ # TacacsServer provider class
2
+ #
3
+ # Mike Wiebe, January 2015
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.join(File.dirname(__FILE__), 'node')
20
+
21
+ module Cisco
22
+ TACACS_SERVER_ENC_NONE = 0
23
+ TACACS_SERVER_ENC_CISCO_TYPE_7 = 7
24
+ TACACS_SERVER_ENC_UNKNOWN = 8
25
+
26
+ class TacacsServer
27
+ @@node = Cisco::Node.instance
28
+
29
+ def initialize(instantiate=true)
30
+ enable if instantiate and not TacacsServer.enabled
31
+ end
32
+
33
+ # Check feature enablement
34
+ def TacacsServer.enabled
35
+ feat = @@node.config_get("tacacs_server", "feature")
36
+ return (!feat.nil? and !feat.empty?)
37
+ rescue Cisco::CliError => e
38
+ # cmd will syntax reject when feature is not enabled
39
+ raise unless e.clierror =~ /Syntax error/
40
+ return false
41
+ end
42
+
43
+ # Enable tacacs_server feature
44
+ def enable
45
+ @@node.config_set("tacacs_server", "feature", "")
46
+ end
47
+
48
+ # Disable tacacs_server feature
49
+ def destroy
50
+ @@node.config_set("tacacs_server", "feature", "no")
51
+ end
52
+
53
+ # --------------------
54
+ # Getters and Setters
55
+ # --------------------
56
+
57
+ # Set timeout
58
+ def timeout=(timeout)
59
+ # 'no tacacs timeout' will fail, just set it to the requested timeout value.
60
+ @@node.config_set("tacacs_server", "timeout", "", timeout)
61
+ end
62
+
63
+ # Get timeout
64
+ def timeout
65
+ match = @@node.config_get("tacacs_server", "timeout")
66
+ match.nil? ? TacacsServer.default_timeout : match.first.to_i
67
+ end
68
+
69
+ # Get default timeout
70
+ def TacacsServer.default_timeout
71
+ @@node.config_get_default("tacacs_server", "timeout")
72
+ end
73
+
74
+ # Set deadtime
75
+ def deadtime=(deadtime)
76
+ # 'no tacacs deadtime' will fail, just set it to the requested timeout value.
77
+ @@node.config_set("tacacs_server", "deadtime", "", deadtime)
78
+ end
79
+
80
+ # Get deadtime
81
+ def deadtime
82
+ match = @@node.config_get("tacacs_server", "deadtime")
83
+ match.nil? ? TacacsServer.default_deadtime : match.first.to_i
84
+ end
85
+
86
+ # Get default deadtime
87
+ def TacacsServer.default_deadtime
88
+ @@node.config_get_default("tacacs_server", "deadtime")
89
+ end
90
+
91
+ # Set directed_request
92
+ def directed_request=(state)
93
+ raise TypeError unless state == true || state == false
94
+ state == TacacsServer.default_directed_request ?
95
+ @@node.config_set("tacacs_server", "directed_request", "no") :
96
+ @@node.config_set("tacacs_server", "directed_request", "")
97
+ end
98
+
99
+ # Check if directed request is enabled
100
+ def directed_request?
101
+ match = @@node.config_get("tacacs_server", "directed_request")
102
+ return TacacsServer.default_directed_request if match.nil?
103
+ match.first[/^no/] ? false : true
104
+ end
105
+
106
+ # Get default directed_request
107
+ def TacacsServer.default_directed_request
108
+ @@node.config_get_default("tacacs_server", "directed_request")
109
+ end
110
+
111
+ # Set source interface
112
+ def source_interface=(name)
113
+ raise TypeError unless name.is_a? String
114
+ name.empty? ?
115
+ @@node.config_set("tacacs_server", "source_interface", "no", "") :
116
+ @@node.config_set("tacacs_server", "source_interface", "", name)
117
+ end
118
+
119
+ # Get source interface
120
+ def source_interface
121
+ # Sample output
122
+ # ip tacacs source-interface Ethernet1/1
123
+ # no tacacs source-interface
124
+ match = @@node.config_get("tacacs_server", "source_interface")
125
+ return TacacsServer.default_source_interface if match.nil?
126
+ # match_data will contain one of the following
127
+ # [nil, " Ethernet1/1"] or ["no", nil]
128
+ match[0][0] == "no" ? TacacsServer.default_source_interface : match[0][1]
129
+ end
130
+
131
+ # Get default source interface
132
+ def TacacsServer.default_source_interface
133
+ @@node.config_get_default("tacacs_server", "source_interface")
134
+ end
135
+
136
+ # Get encryption type used for the key
137
+ def encryption_type
138
+ match = @@node.config_get("tacacs_server", "encryption_type")
139
+ match.nil? ? TACACS_SERVER_ENC_UNKNOWN : match[0][0].to_i
140
+ end
141
+
142
+ # Get default encryption type
143
+ def TacacsServer.default_encryption_type
144
+ @@node.config_get_default("tacacs_server", "encryption_type")
145
+ end
146
+
147
+ # Get encryption password
148
+ def encryption_password
149
+ match = @@node.config_get("tacacs_server", "encryption_password")
150
+ match.nil? ? TacacsServer.default_encryption_password : match[0][1]
151
+ end
152
+
153
+ # Get default encryption password
154
+ def TacacsServer.default_encryption_password
155
+ @@node.config_get_default("tacacs_server", "encryption_password")
156
+ end
157
+
158
+ # Set encryption type and password
159
+ def encryption_key_set(enctype, password)
160
+ # if enctype is TACACS_SERVER_ENC_UNKNOWN, we will unset the key
161
+ if enctype == TACACS_SERVER_ENC_UNKNOWN
162
+ # if current encryption type is not TACACS_SERVER_ENC_UNKNOWN, we
163
+ # need to unset it. Otherwise the box is not configured with key, we
164
+ # don't need to do anything
165
+ if encryption_type != TACACS_SERVER_ENC_UNKNOWN
166
+ @@node.config_set("tacacs_server", "encryption", "no",
167
+ encryption_type,
168
+ encryption_password)
169
+ end
170
+ else
171
+ @@node.config_set("tacacs_server", "encryption", "", enctype, password)
172
+ end
173
+ end
174
+ end
175
+ end
@@ -0,0 +1,128 @@
1
+ # TacacsServerHost class
2
+ #
3
+ # Alex Hunsberger, March 2015
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.join(File.dirname(__FILE__), 'node')
20
+ require File.join(File.dirname(__FILE__), 'tacacs_server')
21
+
22
+ module Cisco
23
+ class TacacsServerHost
24
+ attr_reader :name
25
+ @@node = Cisco::Node.instance
26
+ @@hosts = {}
27
+
28
+ def initialize(name, create=true)
29
+ raise TypeError unless name.is_a? String
30
+ raise ArgumentError if name.empty?
31
+ @name = name
32
+
33
+ if create
34
+ # feature Tacacs+ must be enabled to create a host
35
+ TacacsServer.new.enable unless TacacsServer.enabled
36
+ @@node.config_set("tacacs_server_host", "host", "", name)
37
+ end
38
+ end
39
+
40
+ def TacacsServerHost.hosts
41
+ @@hosts = {}
42
+
43
+ return @@hosts unless TacacsServer.enabled
44
+
45
+ hosts = @@node.config_get("tacacs_server_host", "hosts")
46
+ unless hosts.nil?
47
+ hosts = [hosts] if hosts.is_a?(Hash)
48
+ hosts.each { |name|
49
+ @@hosts[name] = TacacsServerHost.new(name, false) if @@hosts[name].nil?
50
+ }
51
+ end
52
+ @@hosts
53
+ end
54
+
55
+ def destroy
56
+ @@node.config_set("tacacs_server_host", "host", "no", @name)
57
+ @@hosts.delete(@name) unless @@hosts.nil?
58
+ end
59
+
60
+ def port
61
+ p = @@node.config_get("tacacs_server_host", "port", @name)
62
+ raise "unable to retrieve port information for #{@name}" if p.nil?
63
+ p.first.to_i
64
+ end
65
+
66
+ def port=(n)
67
+ @@node.config_set("tacacs_server_host", "port", @name, n.to_i)
68
+ end
69
+
70
+ def TacacsServerHost.default_port
71
+ @@node.config_get_default("tacacs_server_host", "port")
72
+ end
73
+
74
+ def encryption_type
75
+ type = @@node.config_get("tacacs_server_host", "encryption_type", @name)
76
+ type.nil? ? TACACS_SERVER_ENC_UNKNOWN : type.first.to_i
77
+ end
78
+
79
+ def TacacsServerHost.default_encryption_type
80
+ TacacsServer.default_encryption_type
81
+ end
82
+
83
+ def encryption_password
84
+ pass = @@node.config_get("tacacs_server_host", "encryption_password", @name)
85
+ pass.nil? ? TacacsServerHost.default_encryption_password : pass.first
86
+ end
87
+
88
+ def TacacsServerHost.default_encryption_password
89
+ @@node.config_get_default("tacacs_server_host", "encryption_password")
90
+ end
91
+
92
+ def encryption_key_set(enctype, password)
93
+ raise TypeError unless enctype.is_a? Fixnum
94
+ raise ArgumentError if password and not [TACACS_SERVER_ENC_NONE,
95
+ TACACS_SERVER_ENC_CISCO_TYPE_7,
96
+ TACACS_SERVER_ENC_UNKNOWN].include? enctype
97
+ # if enctype is TACACS_SERVER_ENC_UNKNOWN, we'll unset the key
98
+ if enctype == TACACS_SERVER_ENC_UNKNOWN
99
+ # if current encryption type is not TACACS_SERVER_ENC_UNKNOWN, we need
100
+ # to unset the key value. Otherwise, the box is not configured with key,
101
+ # thus we don't need to do anything
102
+ if encryption_type != TACACS_SERVER_ENC_UNKNOWN
103
+ @@node.config_set("tacacs_server_host", "encryption", "no", @name,
104
+ encryption_type,
105
+ encryption_password)
106
+ end
107
+ else
108
+ @@node.config_set("tacacs_server_host", "encryption", "", @name, enctype, password)
109
+ end
110
+ end
111
+
112
+ def timeout
113
+ t = @@node.config_get("tacacs_server_host", "timeout", @name)
114
+ t.nil? ? TacacsServerHost.default_timeout : t.first.to_i
115
+ end
116
+
117
+ def timeout=(t)
118
+ raise TypeError unless t.is_a? Fixnum
119
+ return if t == timeout
120
+
121
+ @@node.config_set("tacacs_server_host", "timeout", "", @name, t)
122
+ end
123
+
124
+ def TacacsServerHost.default_timeout
125
+ @@node.config_get_default("tacacs_server_host", "timeout")
126
+ end
127
+ end
128
+ 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 CiscoNodeUtils
16
+ VERSION = "0.9.0"
17
+ end
@@ -0,0 +1,153 @@
1
+ # VLAN provider class
2
+ #
3
+ # Jie Yang, November 2014
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.join(File.dirname(__FILE__), 'node')
20
+ require File.join(File.dirname(__FILE__), 'interface')
21
+
22
+ module Cisco
23
+ VLAN_NAME_SIZE = 33
24
+
25
+ class Vlan
26
+ attr_reader :name, :vlan_id
27
+
28
+ @@node = Node.instance
29
+ raise TypeError if @@node.nil?
30
+
31
+ def initialize(vlan_id, instantiate=true)
32
+ @vlan_id = vlan_id.to_s
33
+ raise ArgumentError,
34
+ "Invalid value(non-numeric Vlan id)" unless @vlan_id[/^\d+$/]
35
+
36
+ create if instantiate
37
+ end
38
+
39
+ def Vlan.vlans
40
+ hash = {}
41
+ vlan_list = @@node.config_get("vlan", "all_vlans")
42
+ return hash if vlan_list.nil?
43
+
44
+ vlan_list.each do |id|
45
+ hash[id] = Vlan.new(id, false)
46
+ end
47
+ hash
48
+ end
49
+
50
+ def create
51
+ @@node.config_set("vlan", "create", @vlan_id)
52
+ end
53
+
54
+ def destroy
55
+ @@node.config_set("vlan", "destroy", @vlan_id)
56
+ end
57
+
58
+ def cli_error_check(result)
59
+ # The NXOS vlan cli does not raise an exception in some conditions and
60
+ # instead just displays a STDOUT error message; thus NXAPI does not detect
61
+ # the failure and we must catch it by inspecting the "body" hash entry
62
+ # returned by NXAPI. This vlan cli behavior is unlikely to change.
63
+ raise result[2]["body"] unless result[2]["body"].empty?
64
+ end
65
+
66
+ def vlan_name
67
+ result = @@node.config_get("vlan", "name", @vlan_id)
68
+ return default_vlan_name if result.nil?
69
+ result.shift
70
+ end
71
+
72
+ def vlan_name=(str)
73
+ raise TypeError unless str.is_a?(String)
74
+ if str.empty?
75
+ result = @@node.config_set("vlan", "name", @vlan_id, "no", "")
76
+ else
77
+ result = @@node.config_set("vlan", "name", @vlan_id, "", str)
78
+ end
79
+ cli_error_check(result)
80
+ rescue CliError => e
81
+ raise "[vlan #{@vlan_id}] '#{e.command}' : #{e.clierror}"
82
+ end
83
+
84
+ def default_vlan_name
85
+ "VLAN%04d" % @vlan_id
86
+ end
87
+
88
+ def state
89
+ result = @@node.config_get("vlan", "state", @vlan_id)
90
+ return default_state if result.nil?
91
+ case result.first
92
+ when /act/
93
+ return "active"
94
+ when /sus/
95
+ return "suspend"
96
+ end
97
+ end
98
+
99
+ def state=(str)
100
+ str = str.to_s
101
+ if str.empty?
102
+ result = @@node.config_set("vlan", "state", @vlan_id, "no", "")
103
+ else
104
+ result = @@node.config_set("vlan", "state", @vlan_id, "", str)
105
+ end
106
+ cli_error_check(result)
107
+ rescue CliError => e
108
+ raise "[vlan #{@vlan_id}] '#{e.command}' : #{e.clierror}"
109
+ end
110
+
111
+ def default_state
112
+ @@node.config_get_default("vlan", "state")
113
+ end
114
+
115
+ def shutdown
116
+ result = @@node.config_get("vlan", "shutdown", @vlan_id)
117
+ return default_shutdown if result.nil?
118
+ # valid result is either: "active"(aka no shutdown) or "shutdown"
119
+ result.first[/shut/] ? true : false
120
+ end
121
+
122
+ def shutdown=(val)
123
+ no_cmd = (val) ? "" : "no"
124
+ result = @@node.config_set("vlan", "shutdown", @vlan_id, no_cmd)
125
+ cli_error_check(result)
126
+ rescue CliError => e
127
+ raise "[vlan #{@vlan_id}] '#{e.command}' : #{e.clierror}"
128
+ end
129
+
130
+ def default_shutdown
131
+ @@node.config_get_default("vlan", "shutdown")
132
+ end
133
+
134
+ def add_interface(interface)
135
+ interface.access_vlan = @vlan_id
136
+ end
137
+
138
+ def remove_interface(interface)
139
+ interface.access_vlan = interface.default_access_vlan
140
+ end
141
+
142
+ def interfaces
143
+ all_interfaces = Interface.interfaces
144
+ interfaces = {}
145
+ all_interfaces.each do |name, i|
146
+ next unless i.switchport_mode == :access
147
+ next unless i.access_vlan == @vlan_id
148
+ interfaces[name] = i
149
+ end
150
+ interfaces
151
+ end
152
+ end # class
153
+ end # module