Class: Cisco::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/cisco_node_utils/environment.rb

Overview

Class representing the configuration environment

Constant Summary collapse

DEFAULT_ENVIRONMENT =

We have three tiers of configuration: 1) default (defined in this file) 2) System-wide gem configuration in /etc/cisco_node_utils.yaml 3) User configuration in ~/cisco_node_utils.yaml

{
  host:     nil, # localhost by default
  port:     nil, # only applicable to gRPC
  username: nil,
  password: nil,
  cookie:   nil, # only applicable to nxapi
}

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.default_environment_nameObject

Returns the value of attribute default_environment_name


30
31
32
# File 'lib/cisco_node_utils/environment.rb', line 30

def default_environment_name
  @default_environment_name
end

Class Method Details

.add_env(env_name, env_hash) ⇒ Object


94
95
96
97
98
# File 'lib/cisco_node_utils/environment.rb', line 94

def self.add_env(env_name, env_hash)
  fail ArgumentError, 'empty environment name' if env_name.empty?
  fail TypeError, 'invalid environment hash' unless env_hash.is_a?(Hash)
  @environments[env_name] = env_hash
end

.data_from_file(path) ⇒ Object


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/cisco_node_utils/environment.rb', line 72

def self.data_from_file(path)
  begin
    path = File.expand_path(path)
  rescue ArgumentError => e
    # Can happen if path includes '~' but $HOME is not defined
    Cisco::Logger.debug "Failed to load #{path}: #{e}"
    return {}
  end
  unless File.file?(path)
    Cisco::Logger.debug "No file found at #{path}"
    return {}
  end
  unless File.readable?(path)
    Cisco::Logger.debug "No permissions to read #{path}"
    return {}
  end
  YAML.load_file(path)
rescue Psych::SyntaxError => e
  Cisco::Logger.error("Error loading #{path}: #{e}")
  {}
end

.environment(name = nil) ⇒ Object


104
105
106
107
108
# File 'lib/cisco_node_utils/environment.rb', line 104

def self.environment(name=nil)
  name ||= @default_environment_name
  Cisco::Logger.debug("Getting environment '#{name}'")
  environments.fetch(name, DEFAULT_ENVIRONMENT)
end

.environmentsObject


46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cisco_node_utils/environment.rb', line 46

def self.environments
  if @environments.empty?
    @environments = merge_config('/etc/cisco_node_utils.yaml',
                                 @environments)
    @environments = merge_config('~/cisco_node_utils.yaml',
                                 @environments)
    @environments.each do |name, config|
      Cisco::Logger.debug("Environment '#{name}': #{config}")
    end
  end
  @environments
end

.merge_config(path, current_config) ⇒ Object


59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cisco_node_utils/environment.rb', line 59

def self.merge_config(path, current_config)
  data = data_from_file(path)
  data.each do |name, config|
    # in case config is nil:
    config ||= {}
    # in case current_config has no entry for this name:
    current_config[name] ||= DEFAULT_ENVIRONMENT.clone
    # merge it on in!
    current_config[name].merge!(strings_to_symbols(config))
  end
  current_config
end

.strings_to_symbols(hash) ⇒ Object


100
101
102
# File 'lib/cisco_node_utils/environment.rb', line 100

def self.strings_to_symbols(hash)
  Hash[hash.map { |k, v| [k.to_sym, v] }]
end