Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions lib/cisco_node_utils/cmd_ref/hostname.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# hostname
---

name:
get_command: "show running-config all | include 'hostname '"
get_value: '/^hostname (\S+)$/'
set_value: "<state> hostname <name>"
default_value: ""
62 changes: 62 additions & 0 deletions lib/cisco_node_utils/hostname.rb
Original file line number Diff line number Diff line change
@@ -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
64 changes: 64 additions & 0 deletions tests/test_hostname.rb
Original file line number Diff line number Diff line change
@@ -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