Class: Cisco::Util::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/util/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,
}

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.default_environment_nameObject

Returns the value of attribute default_environment_name.



28
29
30
# File 'lib/util/environment.rb', line 28

def default_environment_name
  @default_environment_name
end

Class Method Details

.data_from_file(path) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/util/environment.rb', line 77

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
  Cisco::Logger.debug "File found at #{path}"
  YAML.load_file(path)
rescue Psych::SyntaxError => e
  Cisco::Logger.error("Error loading #{path}: #{e}")
  {}
end

.environment(name) ⇒ Object



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

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

.environment_namesObject



56
57
58
59
60
61
62
# File 'lib/util/environment.rb', line 56

def self.environment_names
  names = []
  environments.each do |name, _config|
    names << name
  end
  names
end

.environmentsObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/util/environment.rb', line 43

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

.merge_config(path, current_config) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/util/environment.rb', line 64

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/util/environment.rb', line 100

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