class Enc::CollinsHelper::Node::NodeAsset
Constants
- VALID_ENC_FLAGS
Note: Do not include the logging module here. If the logging module loads a file handle, the Marshal.dump will fail horribly. This class is meant to be used as a resource.
Public Class Methods
from_json(json_hash, bare_asset=false)
click to toggle source
# File lib/enc/collins_helper/node/node_asset.rb, line 16 def from_json(json_hash, bare_asset=false) raise Collins::CollinsError, 'Invalid JSON specified for Asset.from_json' if (json_hash.nil? || !json_hash.is_a?(Hash)) json = deep_copy_hash(json_hash) json = json['data'] && json['data'] || json if bare_asset or !json.include?('ASSET') asset = NodeAsset.new json else asset = NodeAsset.new json.delete('ASSET') end asset.send('ipmi='.to_sym, Collins::Ipmi.from_json(json.delete('IPMI'))) asset.send('addresses='.to_sym, Collins::Address.from_json(json.delete('ADDRESSES'))) asset.send('power='.to_sym, Collins::Power.from_json(json.delete('POWER'))) asset.send('location=', json.delete('LOCATION')) asset.send('extras=', json) asset end
Public Instance Methods
get_datacenter()
click to toggle source
# File lib/enc/collins_helper/node/node_asset.rb, line 77 def get_datacenter begin value = get_safe_attribute('PUPPET_DATACENTER') rescue KeyError raise NoDatacenter, 'The node does not have a puppet_datacenter tag set' end value end
get_deployment_environment()
click to toggle source
# File lib/enc/collins_helper/node/node_asset.rb, line 68 def get_deployment_environment begin value = get_safe_attribute('ENVIRONMENT') rescue KeyError raise NoDeploymentEnvironment, 'The node does not have an environment tag set' end value end
get_flattened_attributes()
click to toggle source
We need to take all the attributes and make them more human-readable. TODO: Make it not so ugly.
# File lib/enc/collins_helper/node/node_asset.rb, line 109 def get_flattened_attributes flattened_attributes = Hash.new Hash[extract(extras, 'ATTRIBS').inject({}){ |hash, (k, v)| hash.merge(k.to_i => v) }. sort_by {|k,_| k} ].each { |_,hash| hash.each { |k,v| flattened_attributes.has_key?(k) && (flattened_attributes[k].is_a?(Array) && (flattened_attributes[k] << (v)) || (flattened_attributes[k] = [flattened_attributes[k], v])) || flattened_attributes[k] = v } } flattened_attributes end
get_hostname()
click to toggle source
# File lib/enc/collins_helper/node/node_asset.rb, line 59 def get_hostname begin value = get_safe_attribute('HOSTNAME') rescue KeyError raise NoHostname, 'The node does not have a hostname tag set' end value end
get_multi_attribute(attribute)
click to toggle source
# File lib/enc/collins_helper/node/node_asset.rb, line 133 def get_multi_attribute(attribute) get_multi_attribute_with_index(attribute).values end
get_multi_attribute_with_index(attribute)
click to toggle source
# File lib/enc/collins_helper/node/node_asset.rb, line 128 def get_multi_attribute_with_index(attribute) Hash[extract(extras, 'ATTRIBS').inject({}){ |hash, (k, v)| hash.merge(k.to_i => v[attribute]) if v.has_key?(attribute) }.sort_by {|k,_| k} ] end
get_puppet_environment()
click to toggle source
# File lib/enc/collins_helper/node/node_asset.rb, line 86 def get_puppet_environment begin value = get_safe_attribute('PUPPET_ENVIRONMENT') rescue KeyError value = 'production' end value end
get_roles()
click to toggle source
# File lib/enc/collins_helper/node/node_asset.rb, line 95 def get_roles get_multi_attribute('CONFIGURATION_ROLES') end
get_roles_as_string()
click to toggle source
# File lib/enc/collins_helper/node/node_asset.rb, line 99 def get_roles_as_string get_roles.join(',') end
get_roles_by_index()
click to toggle source
# File lib/enc/collins_helper/node/node_asset.rb, line 103 def get_roles_by_index get_multi_attribute_with_index('CONFIGURATION_ROLES') end
get_safe_attribute(attribute)
click to toggle source
# File lib/enc/collins_helper/node/node_asset.rb, line 122 def get_safe_attribute(attribute) value = get_attribute(attribute) raise KeyError unless value value end
has_attribute?(attribute)
click to toggle source
# File lib/enc/collins_helper/node/node_asset.rb, line 50 def has_attribute?(attribute) begin get_safe_attribute(attribute) rescue KeyError return false end true end
is_valid?()
click to toggle source
# File lib/enc/collins_helper/node/node_asset.rb, line 35 def is_valid? begin get_hostname get_deployment_environment get_datacenter rescue NoHostname, NoDeploymentEnvironment, NoDatacenter return false end true end
uses_enc?()
click to toggle source
# File lib/enc/collins_helper/node/node_asset.rb, line 46 def uses_enc? has_attribute?('PUPPET_ENC') && VALID_ENC_FLAGS.include?(get_attribute('PUPPET_ENC').to_s.downcase) end