cisco_node_utils 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +4 -0
  3. data/.rubocop.yml +81 -1
  4. data/.travis.yml +9 -0
  5. data/CHANGELOG.md +72 -6
  6. data/CONTRIBUTING.md +32 -7
  7. data/README.md +70 -7
  8. data/Rakefile +17 -0
  9. data/bin/check_metric_limits.rb +109 -0
  10. data/bin/git/hooks/commit-msg/enforce_style +81 -0
  11. data/bin/git/hooks/hook_lib +108 -0
  12. data/bin/git/hooks/hooks-wrapper +38 -0
  13. data/bin/git/hooks/post-flow-hotfix-start/update-version +24 -0
  14. data/bin/git/hooks/post-flow-release-finish/update-version +29 -0
  15. data/bin/git/hooks/post-flow-release-start/update-version +19 -0
  16. data/bin/git/hooks/post-merge/update-hooks +6 -0
  17. data/bin/git/hooks/post-rewrite/update-hooks +6 -0
  18. data/bin/git/hooks/pre-commit/rubocop +20 -0
  19. data/bin/git/hooks/pre-commit/validate-diffs +31 -0
  20. data/bin/git/hooks/pre-push/check-changelog +24 -0
  21. data/bin/git/hooks/pre-push/rubocop +7 -0
  22. data/bin/git/update-hooks +65 -0
  23. data/cisco_node_utils.gemspec +9 -3
  24. data/docs/README-develop-best-practices.md +404 -0
  25. data/docs/README-develop-node-utils-APIs.md +215 -365
  26. data/docs/README-maintainers.md +33 -3
  27. data/docs/template-router.rb +89 -91
  28. data/docs/template-test_router.rb +52 -55
  29. data/lib/.rubocop.yml +18 -0
  30. data/lib/cisco_node_utils.rb +2 -19
  31. data/lib/cisco_node_utils/README_YAML.md +1 -9
  32. data/lib/cisco_node_utils/bgp.rb +664 -0
  33. data/lib/cisco_node_utils/bgp_af.rb +530 -0
  34. data/lib/cisco_node_utils/bgp_neighbor.rb +425 -0
  35. data/lib/cisco_node_utils/bgp_neighbor_af.rb +709 -0
  36. data/lib/cisco_node_utils/cisco_cmn_utils.rb +59 -25
  37. data/lib/cisco_node_utils/command_reference.rb +72 -74
  38. data/lib/cisco_node_utils/command_reference_common.yaml +174 -9
  39. data/lib/cisco_node_utils/command_reference_common_bgp.yaml +535 -0
  40. data/lib/cisco_node_utils/command_reference_n7k.yaml +4 -0
  41. data/lib/cisco_node_utils/command_reference_n9k.yaml +0 -9
  42. data/lib/cisco_node_utils/configparser_lib.rb +152 -147
  43. data/lib/cisco_node_utils/dns_domain.rb +79 -0
  44. data/lib/cisco_node_utils/domain_name.rb +71 -0
  45. data/lib/cisco_node_utils/interface.rb +167 -161
  46. data/lib/cisco_node_utils/interface_ospf.rb +78 -81
  47. data/lib/cisco_node_utils/name_server.rb +64 -0
  48. data/lib/cisco_node_utils/node.rb +154 -198
  49. data/lib/cisco_node_utils/node_util.rb +61 -0
  50. data/lib/cisco_node_utils/ntp_config.rb +65 -0
  51. data/lib/cisco_node_utils/ntp_server.rb +76 -0
  52. data/lib/cisco_node_utils/platform.rb +174 -165
  53. data/lib/cisco_node_utils/radius_global.rb +146 -0
  54. data/lib/cisco_node_utils/radius_server.rb +295 -0
  55. data/lib/cisco_node_utils/router_ospf.rb +59 -63
  56. data/lib/cisco_node_utils/router_ospf_vrf.rb +226 -210
  57. data/lib/cisco_node_utils/snmpcommunity.rb +52 -58
  58. data/lib/cisco_node_utils/snmpgroup.rb +22 -23
  59. data/lib/cisco_node_utils/snmpserver.rb +99 -103
  60. data/lib/cisco_node_utils/snmpuser.rb +294 -274
  61. data/lib/cisco_node_utils/syslog_server.rb +92 -0
  62. data/lib/cisco_node_utils/syslog_settings.rb +69 -0
  63. data/lib/cisco_node_utils/tacacs_server.rb +137 -133
  64. data/lib/cisco_node_utils/tacacs_server_host.rb +84 -87
  65. data/lib/cisco_node_utils/version.rb +2 -1
  66. data/lib/cisco_node_utils/vlan.rb +28 -31
  67. data/lib/cisco_node_utils/vrf.rb +80 -0
  68. data/lib/cisco_node_utils/vtp.rb +100 -97
  69. data/lib/cisco_node_utils/yum.rb +15 -17
  70. data/tests/.rubocop.yml +15 -0
  71. data/tests/basetest.rb +81 -36
  72. data/tests/ciscotest.rb +38 -78
  73. data/{lib/cisco_node_utils → tests}/platform_info.rb +12 -8
  74. data/{lib/cisco_node_utils → tests}/platform_info.yaml +1 -1
  75. data/tests/test_bgp_af.rb +920 -0
  76. data/tests/test_bgp_neighbor.rb +403 -0
  77. data/tests/test_bgp_neighbor_af.rb +589 -0
  78. data/tests/test_command_config.rb +65 -62
  79. data/tests/test_command_reference.rb +31 -45
  80. data/tests/test_dns_domain.rb +113 -0
  81. data/tests/test_domain_name.rb +86 -0
  82. data/tests/test_interface.rb +424 -548
  83. data/tests/test_interface_ospf.rb +248 -432
  84. data/tests/test_interface_svi.rb +56 -79
  85. data/tests/test_interface_switchport.rb +196 -272
  86. data/tests/test_name_server.rb +85 -0
  87. data/tests/test_node.rb +7 -6
  88. data/tests/test_node_ext.rb +133 -186
  89. data/tests/test_ntp_config.rb +49 -0
  90. data/tests/test_ntp_server.rb +74 -0
  91. data/tests/test_platform.rb +58 -37
  92. data/tests/test_radius_global.rb +78 -0
  93. data/tests/test_radius_server.rb +185 -0
  94. data/tests/test_router_bgp.rb +838 -0
  95. data/tests/test_router_ospf.rb +49 -80
  96. data/tests/test_router_ospf_vrf.rb +274 -392
  97. data/tests/test_snmpcommunity.rb +128 -172
  98. data/tests/test_snmpgroup.rb +12 -14
  99. data/tests/test_snmpserver.rb +160 -189
  100. data/tests/test_snmpuser.rb +568 -717
  101. data/tests/test_syslog_server.rb +88 -0
  102. data/tests/test_syslog_settings.rb +54 -0
  103. data/tests/test_tacacs_server.rb +113 -148
  104. data/tests/test_tacacs_server_host.rb +108 -161
  105. data/tests/test_vlan.rb +63 -79
  106. data/tests/test_vrf.rb +92 -0
  107. data/tests/test_vtp.rb +108 -126
  108. data/tests/test_yum.rb +47 -41
  109. metadata +92 -56
  110. data/.rubocop_todo.yml +0 -293
  111. data/docs/.rubocop.yml +0 -13
  112. data/docs/template-feature.rb +0 -45
  113. data/docs/template-test_feature.rb +0 -51
  114. data/tests/test_all_cisco.rb +0 -46
@@ -1,5 +1,3 @@
1
- # TacacsServerHost class
2
- #
3
1
  # Alex Hunsberger, March 2015
4
2
  #
5
3
  # Copyright (c) 2015 Cisco and/or its affiliates.
@@ -16,113 +14,112 @@
16
14
  # See the License for the specific language governing permissions and
17
15
  # limitations under the License.
18
16
 
19
- require File.join(File.dirname(__FILE__), 'node')
20
- require File.join(File.dirname(__FILE__), 'tacacs_server')
17
+ require_relative 'node_util'
18
+ require_relative 'tacacs_server'
21
19
 
22
20
  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
21
+ # TacacsServerHost - node utility class for TACACS+ server host config
22
+ class TacacsServerHost < NodeUtil
23
+ attr_reader :name
24
+ @hosts = {}
25
+
26
+ def initialize(name, create=true)
27
+ fail TypeError unless name.is_a? String
28
+ fail ArgumentError if name.empty?
29
+ @name = name
30
+ return unless create
31
+ # 'feature tacacs+' must be enabled to create a host
35
32
  TacacsServer.new.enable unless TacacsServer.enabled
36
- @@node.config_set("tacacs_server_host", "host", "", name)
33
+ config_set('tacacs_server_host', 'host', '', name)
37
34
  end
38
- end
39
35
 
40
- def TacacsServerHost.hosts
41
- @@hosts = {}
36
+ def self.hosts
37
+ @hosts = {}
42
38
 
43
- return @@hosts unless TacacsServer.enabled
39
+ return @hosts unless TacacsServer.enabled
44
40
 
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
- }
41
+ hosts = config_get('tacacs_server_host', 'hosts')
42
+ unless hosts.nil?
43
+ hosts = [hosts] if hosts.is_a?(Hash)
44
+ hosts.each do |name|
45
+ @hosts[name] = TacacsServerHost.new(name, false) if @hosts[name].nil?
46
+ end
47
+ end
48
+ @hosts
51
49
  end
52
- @@hosts
53
- end
54
50
 
55
- def destroy
56
- @@node.config_set("tacacs_server_host", "host", "no", @name)
57
- @@hosts.delete(@name) unless @@hosts.nil?
58
- end
51
+ def destroy
52
+ config_set('tacacs_server_host', 'host', 'no', @name)
53
+ end
59
54
 
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
55
+ def port
56
+ p = config_get('tacacs_server_host', 'port', @name)
57
+ fail "unable to retrieve port information for #{@name}" if p.nil?
58
+ p.first.to_i
59
+ end
65
60
 
66
- def port=(n)
67
- @@node.config_set("tacacs_server_host", "port", @name, n.to_i)
68
- end
61
+ def port=(n)
62
+ config_set('tacacs_server_host', 'port', @name, n.to_i)
63
+ end
69
64
 
70
- def TacacsServerHost.default_port
71
- @@node.config_get_default("tacacs_server_host", "port")
72
- end
65
+ def self.default_port
66
+ config_get_default('tacacs_server_host', 'port')
67
+ end
73
68
 
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
69
+ def encryption_type
70
+ type = config_get('tacacs_server_host', 'encryption_type', @name)
71
+ type.nil? ? TACACS_SERVER_ENC_UNKNOWN : type.first.to_i
72
+ end
78
73
 
79
- def TacacsServerHost.default_encryption_type
80
- TacacsServer.default_encryption_type
81
- end
74
+ def self.default_encryption_type
75
+ TacacsServer.default_encryption_type
76
+ end
82
77
 
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
78
+ def encryption_password
79
+ pass = config_get('tacacs_server_host', 'encryption_password', @name)
80
+ pass.nil? ? TacacsServerHost.default_encryption_password : pass.first
81
+ end
87
82
 
88
- def TacacsServerHost.default_encryption_password
89
- @@node.config_get_default("tacacs_server_host", "encryption_password")
90
- end
83
+ def self.default_encryption_password
84
+ config_get_default('tacacs_server_host', 'encryption_password')
85
+ end
91
86
 
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)
87
+ def encryption_key_set(enctype, password)
88
+ fail TypeError unless enctype.is_a? Fixnum
89
+ fail ArgumentError if password && ![TACACS_SERVER_ENC_NONE,
90
+ TACACS_SERVER_ENC_CISCO_TYPE_7,
91
+ TACACS_SERVER_ENC_UNKNOWN,
92
+ ].include?(enctype)
93
+ # if enctype is TACACS_SERVER_ENC_UNKNOWN, we'll unset the key
94
+ if enctype == TACACS_SERVER_ENC_UNKNOWN
95
+ # if current encryption type is not TACACS_SERVER_ENC_UNKNOWN, we need
96
+ # to unset the key value. Otherwise, the box is not configured with key,
97
+ # thus we don't need to do anything
98
+ if encryption_type != TACACS_SERVER_ENC_UNKNOWN
99
+ config_set('tacacs_server_host', 'encryption', 'no', @name,
100
+ encryption_type,
101
+ encryption_password)
102
+ end
103
+ else
104
+ config_set('tacacs_server_host', 'encryption',
105
+ '', @name, enctype, password)
106
106
  end
107
- else
108
- @@node.config_set("tacacs_server_host", "encryption", "", @name, enctype, password)
109
107
  end
110
- end
111
108
 
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
109
+ def timeout
110
+ t = config_get('tacacs_server_host', 'timeout', @name)
111
+ t.nil? ? TacacsServerHost.default_timeout : t.first.to_i
112
+ end
116
113
 
117
- def timeout=(t)
118
- raise TypeError unless t.is_a? Fixnum
119
- return if t == timeout
114
+ def timeout=(t)
115
+ fail TypeError unless t.is_a? Fixnum
116
+ return if t == timeout
120
117
 
121
- @@node.config_set("tacacs_server_host", "timeout", "", @name, t)
122
- end
118
+ config_set('tacacs_server_host', 'timeout', '', @name, t)
119
+ end
123
120
 
124
- def TacacsServerHost.default_timeout
125
- @@node.config_get_default("tacacs_server_host", "timeout")
121
+ def self.default_timeout
122
+ config_get_default('tacacs_server_host', 'timeout')
123
+ end
126
124
  end
127
125
  end
128
- end
@@ -12,6 +12,7 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+ # Container module for version number only.
15
16
  module CiscoNodeUtils
16
- VERSION = "1.0.1"
17
+ VERSION = '1.1.0'
17
18
  end
@@ -1,5 +1,3 @@
1
- # VLAN provider class
2
- #
3
1
  # Jie Yang, November 2014
4
2
  #
5
3
  # Copyright (c) 2014-2015 Cisco and/or its affiliates.
@@ -16,29 +14,28 @@
16
14
  # See the License for the specific language governing permissions and
17
15
  # limitations under the License.
18
16
 
19
- require File.join(File.dirname(__FILE__), 'node')
20
- require File.join(File.dirname(__FILE__), 'interface')
17
+ require_relative 'node_util'
18
+ require_relative 'interface'
21
19
 
20
+ # Add some Vlan-specific constants to the Cisco namespace
22
21
  module Cisco
23
22
  VLAN_NAME_SIZE = 33
24
23
 
25
- class Vlan
24
+ # Vlan - node utility class for VLAN configuration management
25
+ class Vlan < NodeUtil
26
26
  attr_reader :name, :vlan_id
27
27
 
28
- @@node = Node.instance
29
- raise TypeError if @@node.nil?
30
-
31
28
  def initialize(vlan_id, instantiate=true)
32
29
  @vlan_id = vlan_id.to_s
33
- raise ArgumentError,
34
- "Invalid value(non-numeric Vlan id)" unless @vlan_id[/^\d+$/]
30
+ fail ArgumentError,
31
+ 'Invalid value(non-numeric Vlan id)' unless @vlan_id[/^\d+$/]
35
32
 
36
33
  create if instantiate
37
34
  end
38
35
 
39
- def Vlan.vlans
36
+ def self.vlans
40
37
  hash = {}
41
- vlan_list = @@node.config_get("vlan", "all_vlans")
38
+ vlan_list = config_get('vlan', 'all_vlans')
42
39
  return hash if vlan_list.nil?
43
40
 
44
41
  vlan_list.each do |id|
@@ -48,11 +45,11 @@ module Cisco
48
45
  end
49
46
 
50
47
  def create
51
- @@node.config_set("vlan", "create", @vlan_id)
48
+ config_set('vlan', 'create', @vlan_id)
52
49
  end
53
50
 
54
51
  def destroy
55
- @@node.config_set("vlan", "destroy", @vlan_id)
52
+ config_set('vlan', 'destroy', @vlan_id)
56
53
  end
57
54
 
58
55
  def cli_error_check(result)
@@ -60,21 +57,21 @@ module Cisco
60
57
  # instead just displays a STDOUT error message; thus NXAPI does not detect
61
58
  # the failure and we must catch it by inspecting the "body" hash entry
62
59
  # returned by NXAPI. This vlan cli behavior is unlikely to change.
63
- raise result[2]["body"] unless result[2]["body"].empty?
60
+ fail result[2]['body'] unless result[2]['body'].empty?
64
61
  end
65
62
 
66
63
  def vlan_name
67
- result = @@node.config_get("vlan", "name", @vlan_id)
64
+ result = config_get('vlan', 'name', @vlan_id)
68
65
  return default_vlan_name if result.nil?
69
66
  result.shift
70
67
  end
71
68
 
72
69
  def vlan_name=(str)
73
- raise TypeError unless str.is_a?(String)
70
+ fail TypeError unless str.is_a?(String)
74
71
  if str.empty?
75
- result = @@node.config_set("vlan", "name", @vlan_id, "no", "")
72
+ result = config_set('vlan', 'name', @vlan_id, 'no', '')
76
73
  else
77
- result = @@node.config_set("vlan", "name", @vlan_id, "", str)
74
+ result = config_set('vlan', 'name', @vlan_id, '', str)
78
75
  end
79
76
  cli_error_check(result)
80
77
  rescue CliError => e
@@ -82,26 +79,26 @@ module Cisco
82
79
  end
83
80
 
84
81
  def default_vlan_name
85
- "VLAN%04d" % @vlan_id
82
+ sprintf('VLAN%04d', @vlan_id)
86
83
  end
87
84
 
88
85
  def state
89
- result = @@node.config_get("vlan", "state", @vlan_id)
86
+ result = config_get('vlan', 'state', @vlan_id)
90
87
  return default_state if result.nil?
91
88
  case result.first
92
89
  when /act/
93
- return "active"
90
+ return 'active'
94
91
  when /sus/
95
- return "suspend"
92
+ return 'suspend'
96
93
  end
97
94
  end
98
95
 
99
96
  def state=(str)
100
97
  str = str.to_s
101
98
  if str.empty?
102
- result = @@node.config_set("vlan", "state", @vlan_id, "no", "")
99
+ result = config_set('vlan', 'state', @vlan_id, 'no', '')
103
100
  else
104
- result = @@node.config_set("vlan", "state", @vlan_id, "", str)
101
+ result = config_set('vlan', 'state', @vlan_id, '', str)
105
102
  end
106
103
  cli_error_check(result)
107
104
  rescue CliError => e
@@ -109,26 +106,26 @@ module Cisco
109
106
  end
110
107
 
111
108
  def default_state
112
- @@node.config_get_default("vlan", "state")
109
+ config_get_default('vlan', 'state')
113
110
  end
114
111
 
115
112
  def shutdown
116
- result = @@node.config_get("vlan", "shutdown", @vlan_id)
113
+ result = config_get('vlan', 'shutdown', @vlan_id)
117
114
  return default_shutdown if result.nil?
118
- # valid result is either: "active"(aka no shutdown) or "shutdown"
115
+ # Valid result is either: "active"(aka no shutdown) or "shutdown"
119
116
  result.first[/shut/] ? true : false
120
117
  end
121
118
 
122
119
  def shutdown=(val)
123
- no_cmd = (val) ? "" : "no"
124
- result = @@node.config_set("vlan", "shutdown", @vlan_id, no_cmd)
120
+ no_cmd = (val) ? '' : 'no'
121
+ result = config_set('vlan', 'shutdown', @vlan_id, no_cmd)
125
122
  cli_error_check(result)
126
123
  rescue CliError => e
127
124
  raise "[vlan #{@vlan_id}] '#{e.command}' : #{e.clierror}"
128
125
  end
129
126
 
130
127
  def default_shutdown
131
- @@node.config_get_default("vlan", "shutdown")
128
+ config_get_default('vlan', 'shutdown')
132
129
  end
133
130
 
134
131
  def add_interface(interface)
@@ -0,0 +1,80 @@
1
+ # VRF provider class
2
+ #
3
+ # Jie Yang, July 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_relative 'node_util'
20
+
21
+ module Cisco
22
+ # Vrf - node utility class for VRF configuration management
23
+ class Vrf < NodeUtil
24
+ attr_reader :name
25
+
26
+ def initialize(name, instantiate=true)
27
+ fail TypeError unless name.is_a?(String)
28
+ @name = name.downcase.strip
29
+ @args = { vrf: @name }
30
+ create if instantiate
31
+ end
32
+
33
+ def self.vrfs
34
+ hash = {}
35
+ vrf_list = config_get('vrf', 'all_vrfs')
36
+ return hash if vrf_list.nil?
37
+
38
+ vrf_list.each do |id|
39
+ id = id.downcase.strip
40
+ hash[id] = Vrf.new(id, false)
41
+ end
42
+ hash
43
+ end
44
+
45
+ def create
46
+ config_set('vrf', 'create', @args)
47
+ end
48
+
49
+ def destroy
50
+ config_set('vrf', 'destroy', @args)
51
+ end
52
+
53
+ def description
54
+ desc = config_get('vrf', 'description', @args)
55
+ return '' if desc.nil?
56
+ desc.shift.strip
57
+ end
58
+
59
+ def description=(desc)
60
+ fail TypeError unless desc.is_a?(String)
61
+ desc.strip!
62
+ no_cmd = desc.empty? ? 'no' : ''
63
+ config_set('vrf', 'description', vrf: @name, state: no_cmd, desc: desc)
64
+ rescue Cisco::CliError => e
65
+ raise "[#{@name}] '#{e.command}' : #{e.clierror}"
66
+ end
67
+
68
+ def shutdown
69
+ result = config_get('vrf', 'shutdown', @args)
70
+ result ? true : false
71
+ end
72
+
73
+ def shutdown=(val)
74
+ no_cmd = (val) ? '' : 'no'
75
+ config_set('vrf', 'shutdown', vrf: @name, state: no_cmd)
76
+ rescue Cisco::CliError => e
77
+ raise "[vrf #{@name}] '#{e.command}' : #{e.clierror}"
78
+ end
79
+ end # class
80
+ end # module
@@ -1,5 +1,3 @@
1
- # VTP provider class
2
- #
3
1
  # Mike Wiebe, November 2014
4
2
  #
5
3
  # Copyright (c) 2014-2015 Cisco and/or its affiliates.
@@ -16,121 +14,126 @@
16
14
  # See the License for the specific language governing permissions and
17
15
  # limitations under the License.
18
16
 
19
- require File.join(File.dirname(__FILE__), 'node')
17
+ require_relative 'node_util'
20
18
 
21
19
  module Cisco
22
- class Vtp
23
- attr_reader :name
24
-
25
- MAX_VTP_DOMAIN_NAME_SIZE = 32
26
- MAX_VTP_PASSWORD_SIZE = 64
20
+ # Vtp - node utility class for VTP configuration management
21
+ class Vtp < NodeUtil
22
+ attr_reader :name
27
23
 
28
- @@node = Node.instance
24
+ MAX_VTP_DOMAIN_NAME_SIZE = 32
25
+ MAX_VTP_PASSWORD_SIZE = 64
29
26
 
30
- # Constructor for Vtp
31
- def initialize(instantiate=true)
32
- enable if instantiate and not Vtp.enabled
33
- end
27
+ # Constructor for Vtp
28
+ def initialize(instantiate=true)
29
+ enable if instantiate && !Vtp.enabled
30
+ end
34
31
 
35
- def Vtp.enabled
36
- not @@node.config_get("vtp", "feature").nil?
37
- end
32
+ def self.enabled
33
+ !config_get('vtp', 'feature').nil?
34
+ end
38
35
 
39
- def enable
40
- @@node.config_set("vtp", "feature", "")
41
- end
36
+ def enable
37
+ config_set('vtp', 'feature', '')
38
+ end
42
39
 
43
- # Disable vtp feature
44
- def destroy
45
- @@node.config_set("vtp", "feature", "no")
46
- end
40
+ # Disable vtp feature
41
+ def destroy
42
+ config_set('vtp', 'feature', 'no')
43
+ end
47
44
 
48
- # Get vtp domain name
49
- def Vtp.domain
50
- enabled ? @@node.config_get("vtp", "domain") : ""
51
- end
45
+ # Get vtp domain name
46
+ def self.domain
47
+ enabled ? config_get('vtp', 'domain') : ''
48
+ end
52
49
 
53
- def domain
54
- Vtp.domain
55
- end
50
+ def domain
51
+ Vtp.domain
52
+ end
56
53
 
57
- # Set vtp domain name
58
- def domain=(d)
59
- raise ArgumentError unless d and d.is_a? String and
60
- d.length.between?(1, MAX_VTP_DOMAIN_NAME_SIZE)
61
- enable unless Vtp.enabled
62
- begin
63
- @@node.config_set("vtp", "domain", d)
64
- rescue Cisco::CliError => e
65
- # cmd will syntax reject when setting name to same name
66
- raise unless e.clierror =~ /ERROR: Domain name already set to /
54
+ # Set vtp domain name
55
+ def domain=(d)
56
+ fail ArgumentError unless d && d.is_a?(String) &&
57
+ d.length.between?(1, MAX_VTP_DOMAIN_NAME_SIZE)
58
+ enable unless Vtp.enabled
59
+ begin
60
+ config_set('vtp', 'domain', d)
61
+ rescue Cisco::CliError => e
62
+ # cmd will syntax reject when setting name to same name
63
+ raise unless e.clierror =~ /ERROR: Domain name already set to /
64
+ end
67
65
  end
68
- end
69
66
 
70
- # Get vtp password
71
- def password
72
- # Unfortunately nxapi returns "\\" when the password is not set
73
- password = @@node.config_get("vtp", "password") if Vtp.enabled
74
- return '' if password.nil? or password == "\\"
75
- password
76
- end
67
+ # Get vtp password
68
+ def password
69
+ # Unfortunately nxapi returns "\\" when the password is not set
70
+ password = config_get('vtp', 'password') if Vtp.enabled
71
+ return '' if password.nil? || password == '\\'
72
+ password
73
+ end
77
74
 
78
- # Set vtp password
79
- def password=(password)
80
- raise TypeError if password.nil?
81
- raise TypeError unless password.is_a? String
82
- raise ArgumentError if password.length > MAX_VTP_PASSWORD_SIZE
83
- enable unless Vtp.enabled
84
- begin
85
- password == default_password ?
86
- @@node.config_set("vtp", "password", "no", "") :
87
- @@node.config_set("vtp", "password", "", password)
88
- rescue Cisco::CliError => e
89
- raise unless e.clierror =~ /password cannot be set for NULL domain/
90
- raise "Setting VTP password requires first setting VTP domain" unless password == default_password
75
+ # Set vtp password
76
+ def password=(password)
77
+ fail TypeError if password.nil?
78
+ fail TypeError unless password.is_a? String
79
+ fail ArgumentError if password.length > MAX_VTP_PASSWORD_SIZE
80
+ enable unless Vtp.enabled
81
+ begin
82
+ if password == default_password
83
+ config_set('vtp', 'password', 'no', '')
84
+ else
85
+ config_set('vtp', 'password', '', password)
86
+ end
87
+ rescue Cisco::CliError => e
88
+ raise unless e.clierror =~ /password cannot be set for NULL domain/
89
+ unless password == default_password
90
+ raise 'Setting VTP password requires first setting VTP domain'
91
+ end
92
+ end
91
93
  end
92
- end
93
94
 
94
- # Get default vtp password
95
- def default_password
96
- @@node.config_get_default("vtp", "password")
97
- end
95
+ # Get default vtp password
96
+ def default_password
97
+ config_get_default('vtp', 'password')
98
+ end
98
99
 
99
- # Get vtp filename
100
- def filename
101
- match = @@node.config_get("vtp", "filename")
102
- match.nil? ? default_filename : match.first
103
- end
100
+ # Get vtp filename
101
+ def filename
102
+ match = config_get('vtp', 'filename')
103
+ match.nil? ? default_filename : match.first
104
+ end
104
105
 
105
- # Set vtp filename
106
- def filename=(uri)
107
- raise TypeError if uri.nil?
108
- enable unless Vtp.enabled
109
- uri.empty? ?
110
- @@node.config_set("vtp", "filename", "no", "") :
111
- @@node.config_set("vtp", "filename", "", uri)
112
- end
106
+ # Set vtp filename
107
+ def filename=(uri)
108
+ fail TypeError if uri.nil?
109
+ enable unless Vtp.enabled
110
+ if uri.empty?
111
+ config_set('vtp', 'filename', 'no', '')
112
+ else
113
+ config_set('vtp', 'filename', '', uri)
114
+ end
115
+ end
113
116
 
114
- # Get default vtp filename
115
- def default_filename
116
- @@node.config_get_default("vtp", "filename")
117
- end
117
+ # Get default vtp filename
118
+ def default_filename
119
+ config_get_default('vtp', 'filename')
120
+ end
118
121
 
119
- # Get vtp version
120
- def version
121
- match = @@node.config_get("vtp", "version") if Vtp.enabled
122
- match.nil? ? default_version : match.first.to_i
123
- end
122
+ # Get vtp version
123
+ def version
124
+ match = config_get('vtp', 'version') if Vtp.enabled
125
+ match.nil? ? default_version : match.first.to_i
126
+ end
124
127
 
125
- # Set vtp version
126
- def version=(version)
127
- enable unless Vtp.enabled
128
- @@node.config_set("vtp", "version", "#{version}")
129
- end
128
+ # Set vtp version
129
+ def version=(version)
130
+ enable unless Vtp.enabled
131
+ config_set('vtp', 'version', "#{version}")
132
+ end
130
133
 
131
- # Get default vtp version
132
- def default_version
133
- @@node.config_get_default("vtp", "version")
134
+ # Get default vtp version
135
+ def default_version
136
+ config_get_default('vtp', 'version')
137
+ end
134
138
  end
135
139
  end
136
- end