From 7de6a04ba0f01cff44b4fb193445ec6faa1e4246 Mon Sep 17 00:00:00 2001 From: Will Meek Date: Thu, 6 Sep 2018 16:37:15 +0100 Subject: [PATCH] (NETDEV-37) Add support for hostname This commit adds support for hostname, as used by network_dns type. --- CHANGELOG.md | 2 + lib/cisco_node_utils/cmd_ref/hostname.yaml | 8 +++ lib/cisco_node_utils/hostname.rb | 62 +++++++++++++++++++++ tests/test_hostname.rb | 64 ++++++++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 lib/cisco_node_utils/cmd_ref/hostname.yaml create mode 100644 lib/cisco_node_utils/hostname.rb create mode 100644 tests/test_hostname.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index e3b3cb60..8c98b562 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ Changelog * `vlan` * `set_erspan_dscp` * `set_erspan_gre_proto` +* Extend network_dns with attributes: + * `hostname` * Added ability to specify environment at run time Example: diff --git a/lib/cisco_node_utils/cmd_ref/hostname.yaml b/lib/cisco_node_utils/cmd_ref/hostname.yaml new file mode 100644 index 00000000..c3e60cc0 --- /dev/null +++ b/lib/cisco_node_utils/cmd_ref/hostname.yaml @@ -0,0 +1,8 @@ +# hostname +--- + +name: + get_command: "show running-config all | include 'hostname '" + get_value: '/^hostname (\S+)$/' + set_value: " hostname " + default_value: "" diff --git a/lib/cisco_node_utils/hostname.rb b/lib/cisco_node_utils/hostname.rb new file mode 100644 index 00000000..a4242d43 --- /dev/null +++ b/lib/cisco_node_utils/hostname.rb @@ -0,0 +1,62 @@ +# Hostname provider class +# +# September 2018 +# +# Copyright (c) 2014-2018 Cisco and/or its affiliates. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require_relative 'node_util' + +module Cisco + # Hostname- node utility class for hostname configuration + class HostName < NodeUtil + attr_reader :name + + def initialize(name, instantiate=true) + @name = name + create if instantiate + end + + def self.hostname + hash = {} + hostname = config_get('hostname', 'name') + hash[hostname] = HostName.new(hostname, false) + hash + end + + def hostname=(host) + if host + config_set( + 'hostname', 'name', + state: '', name: host) + else + config_set( + 'hostname', 'name', + state: 'no', name: '') + end + end + + def ==(other) + name == other.name + end + + def create + config_set('hostname', 'name', state: '', name: @name) + end + + def destroy + config_set('hostname', 'name', state: 'no', name: @name) + end + end # class +end # module diff --git a/tests/test_hostname.rb b/tests/test_hostname.rb new file mode 100644 index 00000000..097451f9 --- /dev/null +++ b/tests/test_hostname.rb @@ -0,0 +1,64 @@ +# +# Minitest for HostName class +# +# Copyright (c) 2014-2018 Cisco and/or its affiliates. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require_relative 'ciscotest' +require_relative '../lib/cisco_node_utils/hostname' + +# TestHostname - Minitest for SyslogSetting node utility. +class TestHostName < CiscoTestCase + def setup + # setup runs at the beginning of each test + super + hostname_setup + end + + def teardown + # teardown runs at the end of each test + hostname_teardown + super + end + + @current_hostname = '' + + def hostname_setup + hostname_output = Cisco::Client.filter_cli( + cli_output: config('show running-config | include ^hostname'), + value: /hostname (.*)/) + @current_hostname = hostname_output.first unless hostname_output.nil? + # Turn the feature off for a clean test. + config('no hostname') + end + + def hostname_teardown + if @current_hostname != '' + config("hostname #{@current_hostname}") + else + config('no hostname') + end + end + + # TESTS + + def test_hostname_name + hostname_setting = Cisco::HostName.new('testhost') + assert_equal(Cisco::HostName.hostname['testhost'], hostname_setting) + hostname_setting = Cisco::HostName.new('testhost2') + assert_equal(Cisco::HostName.hostname['testhost2'], hostname_setting) + hostname_setting.send('hostname=', nil) + assert_nil(Cisco::HostName.hostname['testhost2']) + end +end