puppet_x_eos_eapi 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (123) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/Gemfile +24 -0
  4. data/LICENSE.txt +202 -0
  5. data/README.md +87 -0
  6. data/Rakefile +1 -0
  7. data/lib/puppet_x/eos/autoload.rb +57 -0
  8. data/lib/puppet_x/eos/eapi.rb +259 -0
  9. data/lib/puppet_x/eos/module_base.rb +37 -0
  10. data/lib/puppet_x/eos/modules/daemon.rb +109 -0
  11. data/lib/puppet_x/eos/modules/extension.rb +167 -0
  12. data/lib/puppet_x/eos/modules/interface.rb +180 -0
  13. data/lib/puppet_x/eos/modules/ipinterface.rb +133 -0
  14. data/lib/puppet_x/eos/modules/mlag.rb +268 -0
  15. data/lib/puppet_x/eos/modules/ntp.rb +129 -0
  16. data/lib/puppet_x/eos/modules/ospf.rb +129 -0
  17. data/lib/puppet_x/eos/modules/portchannel.rb +277 -0
  18. data/lib/puppet_x/eos/modules/radius.rb +367 -0
  19. data/lib/puppet_x/eos/modules/snmp.rb +177 -0
  20. data/lib/puppet_x/eos/modules/switchport.rb +255 -0
  21. data/lib/puppet_x/eos/modules/system.rb +138 -0
  22. data/lib/puppet_x/eos/modules/tacacs.rb +302 -0
  23. data/lib/puppet_x/eos/modules/vlan.rb +179 -0
  24. data/lib/puppet_x/eos/modules/vxlan.rb +132 -0
  25. data/lib/puppet_x/eos/provider.rb +71 -0
  26. data/lib/puppet_x/eos/version.rb +41 -0
  27. data/lib/puppet_x/net_dev/eos_api.rb +1011 -0
  28. data/lib/puppet_x/net_dev/eos_api/common_methods.rb +27 -0
  29. data/lib/puppet_x/net_dev/eos_api/snmp_methods.rb +647 -0
  30. data/lib/puppet_x/net_dev/eos_api/version.rb +8 -0
  31. data/lib/puppet_x_eos_eapi.rb +4 -0
  32. data/puppet_x_eos_eapi.gemspec +31 -0
  33. data/spec/fixtures/fixture_all_portchannel_modes.json +8 -0
  34. data/spec/fixtures/fixture_all_portchannels_detailed.json +15 -0
  35. data/spec/fixtures/fixture_create_vlan_error.json +17 -0
  36. data/spec/fixtures/fixture_create_vlan_success.json +12 -0
  37. data/spec/fixtures/fixture_eapi_conf.yaml +4 -0
  38. data/spec/fixtures/fixture_enable_configure_vlan_3111_name_foo.json +14 -0
  39. data/spec/fixtures/fixture_enable_configure_vlan_foo_name_bar.json +19 -0
  40. data/spec/fixtures/fixture_get_snmp_communities_non_existent_acl.yaml +2 -0
  41. data/spec/fixtures/fixture_get_snmp_location_westeros.json +5 -0
  42. data/spec/fixtures/fixture_portchannel_min_links_1.json +8 -0
  43. data/spec/fixtures/fixture_portchannel_min_links_2.json +8 -0
  44. data/spec/fixtures/fixture_running_config.yaml +1 -0
  45. data/spec/fixtures/fixture_running_configuration_radius_configured.yaml +30 -0
  46. data/spec/fixtures/fixture_running_configuration_radius_default.yaml +29 -0
  47. data/spec/fixtures/fixture_running_configuration_radius_server_groups.yaml +38 -0
  48. data/spec/fixtures/fixture_running_configuration_radius_servers.yaml +34 -0
  49. data/spec/fixtures/fixture_running_configuration_tacacs_configured.yaml +38 -0
  50. data/spec/fixtures/fixture_running_configuration_tacacs_default.yaml +38 -0
  51. data/spec/fixtures/fixture_running_configuration_tacacs_groups.yaml +1 -0
  52. data/spec/fixtures/fixture_running_configuration_tacacs_groups_3.yaml +43 -0
  53. data/spec/fixtures/fixture_running_configuration_tacacs_servers.yaml +41 -0
  54. data/spec/fixtures/fixture_s4_show_etherchannel_detailed.json +9 -0
  55. data/spec/fixtures/fixture_show_flowcontrol_et1.json +5 -0
  56. data/spec/fixtures/fixture_show_interfaces.json +297 -0
  57. data/spec/fixtures/fixture_show_interfaces_switchport_format_text.json +9 -0
  58. data/spec/fixtures/fixture_show_port_channel_summary_2_lags.json +9 -0
  59. data/spec/fixtures/fixture_show_port_channel_summary_static.json +9 -0
  60. data/spec/fixtures/fixture_show_snmp_community.yaml +2 -0
  61. data/spec/fixtures/fixture_show_snmp_contact_empty.json +5 -0
  62. data/spec/fixtures/fixture_show_snmp_contact_name.json +5 -0
  63. data/spec/fixtures/fixture_show_snmp_disabled.json +5 -0
  64. data/spec/fixtures/fixture_show_snmp_enabled.json +5 -0
  65. data/spec/fixtures/fixture_show_snmp_host.yaml +2 -0
  66. data/spec/fixtures/fixture_show_snmp_host_duplicates.yaml +2 -0
  67. data/spec/fixtures/fixture_show_snmp_host_more_duplicates.yaml +2 -0
  68. data/spec/fixtures/fixture_show_snmp_location_empty.json +5 -0
  69. data/spec/fixtures/fixture_show_snmp_trap.yaml +2 -0
  70. data/spec/fixtures/fixture_show_snmp_user.yaml +2 -0
  71. data/spec/fixtures/fixture_show_snmp_user_raw_text.yaml +1 -0
  72. data/spec/fixtures/fixture_show_vlan.json +37 -0
  73. data/spec/fixtures/fixture_show_vlan_3110.json +18 -0
  74. data/spec/fixtures/fixture_show_vlan_4000.json +18 -0
  75. data/spec/fixtures/fixture_snmp_host_opts.yaml +11 -0
  76. data/spec/spec_helper.rb +21 -0
  77. data/spec/support/fixtures.rb +104 -0
  78. data/spec/unit/puppet_x/eos/eapi_spec.rb +182 -0
  79. data/spec/unit/puppet_x/eos/module_base_spec.rb +26 -0
  80. data/spec/unit/puppet_x/eos/modules/daemon_spec.rb +110 -0
  81. data/spec/unit/puppet_x/eos/modules/extension_spec.rb +197 -0
  82. data/spec/unit/puppet_x/eos/modules/fixtures/daemon_getall.json +3 -0
  83. data/spec/unit/puppet_x/eos/modules/fixtures/extension_getall.json +28 -0
  84. data/spec/unit/puppet_x/eos/modules/fixtures/hostname.json +6 -0
  85. data/spec/unit/puppet_x/eos/modules/fixtures/interface_getall.json +509 -0
  86. data/spec/unit/puppet_x/eos/modules/fixtures/ipinterface_getall.json +56 -0
  87. data/spec/unit/puppet_x/eos/modules/fixtures/mlag_get.json +21 -0
  88. data/spec/unit/puppet_x/eos/modules/fixtures/mlag_get_interfaces.json +18 -0
  89. data/spec/unit/puppet_x/eos/modules/fixtures/ntp_get.json +5 -0
  90. data/spec/unit/puppet_x/eos/modules/fixtures/ospf_instance_getall.json +58 -0
  91. data/spec/unit/puppet_x/eos/modules/fixtures/portchannel_get.json +54 -0
  92. data/spec/unit/puppet_x/eos/modules/fixtures/portchannel_getlacpmode.json +5 -0
  93. data/spec/unit/puppet_x/eos/modules/fixtures/portchannel_getmembers.json +5 -0
  94. data/spec/unit/puppet_x/eos/modules/fixtures/portchannel_po1.json +7 -0
  95. data/spec/unit/puppet_x/eos/modules/fixtures/snmp_get.json +14 -0
  96. data/spec/unit/puppet_x/eos/modules/fixtures/switchport_get.json +5 -0
  97. data/spec/unit/puppet_x/eos/modules/fixtures/switchport_get_et1.json +7 -0
  98. data/spec/unit/puppet_x/eos/modules/fixtures/switchport_getall_interfaces.json +230 -0
  99. data/spec/unit/puppet_x/eos/modules/fixtures/system_domain_list.json +5 -0
  100. data/spec/unit/puppet_x/eos/modules/fixtures/system_domain_name.json +5 -0
  101. data/spec/unit/puppet_x/eos/modules/fixtures/system_hostname.json +6 -0
  102. data/spec/unit/puppet_x/eos/modules/fixtures/system_name_servers.json +5 -0
  103. data/spec/unit/puppet_x/eos/modules/fixtures/vlan_getall.json +123 -0
  104. data/spec/unit/puppet_x/eos/modules/fixtures/vxlan_get.json +24 -0
  105. data/spec/unit/puppet_x/eos/modules/interface_spec.rb +281 -0
  106. data/spec/unit/puppet_x/eos/modules/ipinterface_spec.rb +143 -0
  107. data/spec/unit/puppet_x/eos/modules/mlag_spec.rb +349 -0
  108. data/spec/unit/puppet_x/eos/modules/ntp_spec.rb +136 -0
  109. data/spec/unit/puppet_x/eos/modules/ospf_spec.rb +143 -0
  110. data/spec/unit/puppet_x/eos/modules/portchannel_spec.rb +357 -0
  111. data/spec/unit/puppet_x/eos/modules/radius_spec.rb +509 -0
  112. data/spec/unit/puppet_x/eos/modules/snmp_spec.rb +202 -0
  113. data/spec/unit/puppet_x/eos/modules/switchport_get_et1.json +7 -0
  114. data/spec/unit/puppet_x/eos/modules/switchport_spec.rb +307 -0
  115. data/spec/unit/puppet_x/eos/modules/system_spec.rb +170 -0
  116. data/spec/unit/puppet_x/eos/modules/tacacs_spec.rb +448 -0
  117. data/spec/unit/puppet_x/eos/modules/vlan_spec.rb +244 -0
  118. data/spec/unit/puppet_x/eos/modules/vxlan_spec.rb +189 -0
  119. data/spec/unit/puppet_x/eos/provider_spec.rb +35 -0
  120. data/spec/unit/puppet_x/net_dev/eos_api/common_methods_spec.rb +34 -0
  121. data/spec/unit/puppet_x/net_dev/eos_api/snmp_methods_spec.rb +842 -0
  122. data/spec/unit/puppet_x/net_dev/eos_api_spec.rb +1000 -0
  123. metadata +369 -0
@@ -0,0 +1,302 @@
1
+ # encoding: utf-8
2
+
3
+ module PuppetX
4
+ ##
5
+ # Eos is module namesapce for working with the EOS command API
6
+ module Eos
7
+ ##
8
+ # Tacacs provides instance methods to retrieve and set tacacs configuration
9
+ # values.
10
+ class Tacacs < ModuleBase
11
+ # Regular expression to extract a tacacs server's attributes from the
12
+ # running-configuration text. The explicit [ ] spaces enable line
13
+ # wrappping and indentation with the /x flag.
14
+ SERVER_REGEXP = /tacacs-server[ ]host[ ](.*?)
15
+ (?:[ ](single-connection))?
16
+ (?:[ ]port[ ](\d+))?
17
+ (?:[ ]timeout[ ](\d+))?
18
+ (?:[ ]key[ ](\d+)[ ](\w+))?\s/x
19
+
20
+ # Default Tacacs TCP port
21
+ DEFAULT_PORT = 49
22
+
23
+ # Regular expression to extract a tacacs server's attributes from the
24
+ # running-configuration text. The explicit [ ] spaces enable line
25
+ # wrappping and indentation with the /x flag.
26
+ SERVER_GROUP_REGEXP = /aaa group server tacacs[+]? (.*)/
27
+
28
+ # FIXME Needs to be updated
29
+ GROUP_MEMBER_REGEXP = /server[ ](.*?)
30
+ (?:[ ]vrf[ ]([^ ]+))?
31
+ (?:[ ]port[ ](\d+))?\s/x
32
+
33
+ ##
34
+ # getall Returns an Array with a single resource Hash describing the
35
+ # current state of the global tacacs configuration on the target device.
36
+ # This method is intended to be used by a provider's instances class
37
+ # method.
38
+ #
39
+ # The resource hash returned contains the following information:
40
+ # * name: ('settings')
41
+ # * enable: (true | false) if tacacs functionality is enabled. This is
42
+ # always true for EOS.
43
+ # * key: (String) the key either in plaintext or hashed format
44
+ # * key_format: (Integer) e.g. 0 or 7
45
+ # * timeout: (Integer) seconds before the timeout period ends
46
+ #
47
+ # @api public
48
+ #
49
+ # @return [Array<Hash>] Single element Array of resource hashes
50
+ def getall
51
+ config = running_configuration
52
+ rsrc_hsh = tacacs_global_defaults
53
+ rsrc_hsh.merge!(parse_global_key(config))
54
+ rsrc_hsh.merge!(parse_global_timeout(config))
55
+ [rsrc_hsh]
56
+ end
57
+
58
+ ##
59
+ # tacacs_global_defaults returns the default values for the tacacs_global
60
+ # resource. This is in a single method to keep the information in one
61
+ # place. If a value is explicitly configured to be the same as a default
62
+ # value it will not show up in the running configuration and as a result
63
+ # will not be parsed out by the parse instance methods. This method
64
+ # exposes the default values.
65
+ #
66
+ # @return [Array<Hash>] Single element Array of resource hashes
67
+ def tacacs_global_defaults
68
+ {
69
+ name: 'settings',
70
+ enable: true,
71
+ timeout: 5,
72
+ }
73
+ end
74
+ private :tacacs_global_defaults
75
+
76
+ ##
77
+ # parse_global_key takes a running configuration as a string and
78
+ # parses out the radius global key and global key format if it exists in
79
+ # the configuration. An empty Hash is returned if there is no global key
80
+ # configured. The intent of the Hash is to be merged into a property
81
+ # hash.
82
+ #
83
+ # @param [String] config The running configuration as a single string.
84
+ #
85
+ # @api private
86
+ #
87
+ # @return [Hash<Symbol,Object>] resource hash attributes
88
+ def parse_global_key(config)
89
+ rsrc_hsh = {}
90
+ (key_format, key) = config.scan(/tacacs-server key (\d+) (\w+)/).first
91
+ rsrc_hsh[:key_format] = key_format.to_i if key_format
92
+ rsrc_hsh[:key] = key if key
93
+ rsrc_hsh
94
+ end
95
+ private :parse_global_key
96
+
97
+ ##
98
+ # parse_global_timeout takes a running configuration as a string
99
+ # and parses out the tacacs global timeout if it exists in the
100
+ # configuration. An empty Hash is returned if there is no global timeout
101
+ # value configured. The intent of the Hash is to be merged into a
102
+ # property hash.
103
+ #
104
+ # @param [String] config The running configuration as a single string.
105
+ #
106
+ # @api private
107
+ #
108
+ # @return [Hash<Symbol,Object>] resource hash attributes
109
+ def parse_global_timeout(config)
110
+ rsrc_hsh = {}
111
+ timeout = config.scan(/tacacs-server timeout (\d+)/).first
112
+ # EOS default is 5 (does not show up in the running config)
113
+ rsrc_hsh[:timeout] = timeout.first.to_i if timeout
114
+ rsrc_hsh
115
+ end
116
+ private :parse_global_timeout
117
+
118
+ ##
119
+ # set_global_key configures the tacacs default key. This method maps to
120
+ # the `tacacs-server key` EOS configuration command, e.g. `tacacs-server
121
+ # key 7 070E234F1F5B4A`.
122
+ #
123
+ # @option opts [String] :key ('070E234F1F5B4A') The key value
124
+ #
125
+ # @option opts [Fixnum] :key_format (7) The key format, 0 for plaintext
126
+ # and 7 for a hashed value. 7 will be assumed if this option is not
127
+ # provided.
128
+ #
129
+ # @api public
130
+ #
131
+ # @return [Boolean] true if no errors
132
+ def set_global_key(opts = {})
133
+ format = opts[:key_format] || 7
134
+ key = opts[:key]
135
+ fail ArgumentError, 'key option is required' unless key
136
+ result = api.config("tacacs-server key #{format} #{key}")
137
+ result == [{}]
138
+ end
139
+
140
+ ##
141
+ # set_timeout configures the tacacs default timeout. This method maps to
142
+ # the `tacacs-server timeout` setting.
143
+ #
144
+ # @option opts [Fixnum] :timeout (50) The timeout in seconds to
145
+ # configure.
146
+ #
147
+ # @api public
148
+ #
149
+ # @return [Boolean] true if no errors
150
+ def set_timeout(opts = {})
151
+ timeout = opts[:timeout]
152
+ fail ArgumentError, 'timeout option is required' unless timeout
153
+ result = api.config("tacacs-server timeout #{timeout}")
154
+ result == [{}]
155
+ end
156
+
157
+ ##
158
+ # servers returns an Array of tacacs server resource hashes. Each hash
159
+ # describes the current state of the tacacs server and is suitable for
160
+ # use in initializing a tacacs_server provider.
161
+ #
162
+ # The resource hash returned contains the following information:
163
+ #
164
+ # * hostname: hostname or ip address, part of the identifier
165
+ # * port: (Fixnum) TCP port of the server, part of the identifier
166
+ # * key: (String) the key either in plaintext or hashed format
167
+ # * key_format: (Fixnum) e.g. 0 or 7
168
+ # * timeout: (Fixnum) seconds before the timeout period ends
169
+ # * multiplex: (Boolean) true when configured to make requests through a
170
+ # single connection
171
+ #
172
+ # @api public
173
+ #
174
+ # @return [Array<Hash<Symbol,Object>>] Array of resource hashes
175
+ def servers
176
+ config = running_configuration
177
+ tuples = config.scan(SERVER_REGEXP)
178
+ tuples.map do |(host, mplex, port, tout, keyfm, key)|
179
+ hsh = { port: DEFAULT_PORT }
180
+ hsh[:hostname] = host if host
181
+ hsh[:port] = port.to_i if port
182
+ hsh[:timeout] = tout.to_i if tout
183
+ hsh[:key_format] = keyfm.to_i if keyfm
184
+ hsh[:key] = key if key
185
+ hsh[:multiplex] = mplex ? true : false
186
+ hsh
187
+ end
188
+ end
189
+
190
+ ##
191
+ # server_groups retrieves a list of tacacs server groups from the target
192
+ # device.
193
+ #
194
+ # @api public
195
+ #
196
+ # @return [Array<Hash<Symbol,Object>>] Array of resource hashes
197
+ def server_groups
198
+ config = running_configuration
199
+ regexp = SERVER_GROUP_REGEXP
200
+ tuples = config.scan(regexp)
201
+ tuples.map do |(name)|
202
+ { name: name, servers: parse_group_servers(config, name) }
203
+ end
204
+ end
205
+
206
+ ##
207
+ # parse_group_servers parses the list of servers associated with a tacacs
208
+ # server group given a group name and a running configuration text.
209
+ #
210
+ # @param [String] config The running configuration text.
211
+ #
212
+ # @param [String] name The name of the server group to parse.
213
+ #
214
+ # @api private
215
+ #
216
+ # @return [Array<Hash<Symbol,Object>] Array of server attributes
217
+ def parse_group_servers(config, name)
218
+ regexp = /aaa group server tacacs[+] #{name}(.*?)!/m
219
+ mdata = regexp.match(config)
220
+ tuples = mdata[1].scan(GROUP_MEMBER_REGEXP)
221
+ tuples.collect do |(hostname, vrf, port)|
222
+ { hostname: hostname, port: port ? port.to_i : DEFAULT_PORT }
223
+ end
224
+ end
225
+
226
+ ##
227
+ # update_server_group updates a tacacs server group given an Array of
228
+ # server attributes and the name of the server group. The update happens
229
+ # by first deleting the existing group if it exists then creating it
230
+ # again with all of the specified servers.
231
+ #
232
+ # @param [String] name The name of the server group to update
233
+ #
234
+ # @param [Array<Hash<Symbol,Object>>] servers The array of servers to
235
+ # associate with the server group. This hash should have at least the
236
+ # :hostname key.
237
+ #
238
+ # @api public
239
+ #
240
+ # @return [Boolean] true if no errors
241
+ def update_server_group(opts = {})
242
+ cmd = "aaa group server tacacs+ #{opts[:name]}"
243
+ api.config("no #{cmd}")
244
+ cmds = [cmd]
245
+ opts[:servers].each do |hsh|
246
+ cmds << "server #{hsh[:hostname]} port #{hsh[:port] || DEFAULT_PORT}"
247
+ end
248
+ result = api.config(cmds)
249
+ !result.find { |r| r != {} }
250
+ end
251
+
252
+ ##
253
+ # remove_server_group removes a tacacs server group by name. This API
254
+ # call maps to the `no aaa group server tacacs <name>` command.
255
+ #
256
+ # @option opts [String] :name ('TAC-GR') The name of the tacacs server
257
+ # group to remove.
258
+ #
259
+ # @api public
260
+ #
261
+ # @return [Boolean] true if no errors
262
+ def remove_server_group(opts = {})
263
+ result = api.config("no aaa group server tacacs+ #{opts[:name]}")
264
+ result == [{}]
265
+ end
266
+
267
+ ##
268
+ # update_server configures a tacacs server resource on the target device.
269
+ # This API method maps to the `tacacs server host` command, e.g.
270
+ # `tacacs-server host 1.2.3.4 single-connection port 4949 timeout 6 key 7
271
+ # 06070D221D1C5A`
272
+ #
273
+ # @api public
274
+ #
275
+ # @return [Boolean] true if there are no errors
276
+ def update_server(opts = {})
277
+ key_format = opts[:key_format] || 7
278
+ cmd = "tacacs-server host #{opts[:hostname]}"
279
+ cmd << ' single-connection' if opts[:multiplex]
280
+ cmd << " port #{opts[:port]}" if opts[:port]
281
+ cmd << " timeout #{opts[:timeout]}" if opts[:timeout]
282
+ cmd << " key #{key_format} #{opts[:key]}" if opts[:key]
283
+ result = api.config(cmd)
284
+ result == [{}]
285
+ end
286
+
287
+ ##
288
+ # remove_server removes the tacacs server identified by the hostname,
289
+ # and port attributes.
290
+ #
291
+ # @api public
292
+ #
293
+ # @return [Boolean] true if no errors
294
+ def remove_server(opts = {})
295
+ cmd = "no tacacs-server host #{opts[:hostname]}"
296
+ cmd << " port #{opts[:port]}" if opts[:port]
297
+ result = api.config(cmd)
298
+ result == [{}]
299
+ end
300
+ end
301
+ end
302
+ end
@@ -0,0 +1,179 @@
1
+ #
2
+ # Copyright (c) 2014, Arista Networks, Inc.
3
+ # All rights reserved.
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without
6
+ # modification, are permitted provided that the following conditions are
7
+ # met:
8
+ #
9
+ # Redistributions of source code must retain the above copyright notice,
10
+ # this list of conditions and the following disclaimer.
11
+ #
12
+ # Redistributions in binary form must reproduce the above copyright
13
+ # notice, this list of conditions and the following disclaimer in the
14
+ # documentation and/or other materials provided with the distribution.
15
+ #
16
+ # Neither the name of Arista Networks nor the names of its
17
+ # contributors may be used to endorse or promote products derived from
18
+ # this software without specific prior written permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
+ # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARISTA NETWORKS
24
+ # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27
+ # BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28
+ # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29
+ # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30
+ # IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ #
32
+
33
+ ##
34
+ # Eos is the toplevel namespace for working with Arista EOS nodes
35
+ module PuppetX
36
+ ##
37
+ # Eapi is module namesapce for working with the EOS command API
38
+ module Eos
39
+ ##
40
+ # The Vlan class provides an interface for working wit VLAN resources
41
+ # in EOS. All configuration is sent and received using eAPI. In order
42
+ # to use this class, eAPI must be enablined in EOS. This class
43
+ # can be instatiated either using the Eos::Eapi::Switch.load_class
44
+ # method or used directly.
45
+ #
46
+ class Vlan
47
+ def initialize(api)
48
+ @api = api
49
+ end
50
+
51
+ ##
52
+ # Returns the vlan data for the provided id with the
53
+ # show vlan <id> command. If the id doesn't exist then
54
+ # nil is returned
55
+ #
56
+ # Example:
57
+ # [
58
+ # { "sourceDetail": <string>, "vlans": {...} },
59
+ # { "trunkGroups": {...} }
60
+ # ]
61
+ #
62
+ # @return [nil, Hash<String, String|Hash|Array>] Hash describing the
63
+ # vlan configuration specified by id. If the id is not
64
+ # found then nil is returned
65
+ def getall
66
+ @api.enable(['show vlan', 'show vlan trunk group'])
67
+ end
68
+
69
+ ##
70
+ # Adds a new VLAN resource in EOS setting the VLAN ID to id. The
71
+ # VLAN ID must be in the valid range of 1 through 4094
72
+ #
73
+ # @param [String] id The VLAN identifier (e.g. 1)
74
+ #
75
+ # @return [Boolean] returns true if the command completed successfully
76
+ def create(id)
77
+ @api.config("vlan #{id}") == [{}]
78
+ end
79
+
80
+ ##
81
+ # Deletes an existing VLAN resource in EOS as specified by ID. If
82
+ # the supplied VLAN ID does not exist no error is raised
83
+ #
84
+ # @param [String] id The VLAN identifier (e.g. 1)
85
+ #
86
+ # @return [Boolean] always returns true
87
+ def delete(id)
88
+ @api.config("no vlan #{id}") == [{}]
89
+ end
90
+
91
+ ##
92
+ # Defaults an existing VLAN resource in EOS as specified by ID. If
93
+ # the supplied VLAN ID does not exist no error is raised. Note: setting
94
+ # a vlan to default is equivalent to negating it
95
+ #
96
+ # @param [String] id The VLAN identifier (e.g. 1)
97
+ #
98
+ # @return [Boolean] returns true if the command completed successfully
99
+ def default(id)
100
+ @api.config("default vlan #{id}") == [{}]
101
+ end
102
+
103
+ ##
104
+ # Configures the VLAN name of the VLAN specified by ID. set_name maps
105
+ # to the EOS name WORD command. Spaces in the name will be converted
106
+ # to _
107
+ #
108
+ # @param [Hash] opts The configuration parameters for the VLAN
109
+ # @option opts [String] :id The VLAN ID to change
110
+ # @option opts [string] :value The value to set the name to
111
+ # @option opts [Boolean] :default The value should be set to default
112
+ #
113
+ # @return [Boolean] returns true if the command completed successfully
114
+ def set_name(id, opts = {})
115
+ value = opts[:value]
116
+ default = opts[:default] || false
117
+
118
+ cmds = ["vlan #{id}"]
119
+ case default
120
+ when true
121
+ cmds << 'default name'
122
+ when false
123
+ cmds << (value.nil? ? 'no name' : "name #{value}")
124
+ end
125
+ @api.config(cmds) == [{}, {}]
126
+ end
127
+
128
+ ##
129
+ # Configures the administrative state of the VLAN specified by ID. The
130
+ # set_state function accepts 'active' or 'suspend' to configure the
131
+ # VLAN state.
132
+ #
133
+ # @param [Hash] opts The configuration parameters for the VLAN
134
+ # @option opts [String] :id The VLAN ID to change
135
+ # @option opts [string] :value The value to set the state to
136
+ # @option opts [Boolean] :default The value should be set to default
137
+ #
138
+ # @return [Boolean] returns true if the command completed successfully
139
+ def set_state(id, opts = {})
140
+ value = opts[:value]
141
+ default = opts[:default] || false
142
+
143
+ cmds = ["vlan #{id}"]
144
+ case default
145
+ when true
146
+ cmds << 'default state'
147
+ when false
148
+ cmds << (value.nil? ? 'no state' : "state #{value}")
149
+ end
150
+ @api.config(cmds) == [{}, {}]
151
+ end
152
+
153
+ ##
154
+ # Configures the trunk group value for the VLAN specified by ID. The
155
+ # trunk group setting is typically used to associate VLANs with MLAG
156
+ # configurations
157
+ #
158
+ # @param [Hash] opts The configuration parameters for the VLAN
159
+ # @option opts [String] :id The VLAN ID to change
160
+ # @option opts [string] :value The value to set the trunk group to
161
+ # @option opts [Boolean] :default The value should be set to default
162
+ #
163
+ # @return [Boolean] returns true if the command completed successfully
164
+ def set_trunk_group(id, opts = {})
165
+ value = opts[:value]
166
+ default = opts[:default] || false
167
+
168
+ cmds = ["vlan #{id}"]
169
+ case default
170
+ when true
171
+ cmds << 'default trunk group'
172
+ when false
173
+ cmds << (value.nil? ? 'no trunk group' : "trunk group #{value}")
174
+ end
175
+ @api.config(cmds) == [{}, {}]
176
+ end
177
+ end
178
+ end
179
+ end