Class: Bcome::Node::Factory

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/objects/node/factory.rb

Constant Summary collapse

CONFIG_PATH =
'bcome'.freeze
DEFAULT_CONFIG_NAME =
'networks.yml'.freeze
INVENTORY_KEY =
'inventory'.freeze
COLLECTION_KEY =
'collection'.freeze
SUBSELECT_KEY =
'inventory-subselect'.freeze
BCOME_RC_FILENAME =
'.bcomerc'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#estateObject (readonly)

Returns the value of attribute estate.



5
6
7
# File 'lib/objects/node/factory.rb', line 5

def estate
  @estate
end

Instance Method Details

#bucketObject



14
15
16
# File 'lib/objects/node/factory.rb', line 14

def bucket
  @bucket ||= {}
end

#config_file_nameObject



27
28
29
# File 'lib/objects/node/factory.rb', line 27

def config_file_name
  @config_file_name ||= ENV['CONF'] ? ENV['CONF'] : DEFAULT_CONFIG_NAME
end

#config_pathObject



23
24
25
# File 'lib/objects/node/factory.rb', line 23

def config_path
  "#{CONFIG_PATH}/#{config_file_name}"
end

#create_node(config, parent = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/objects/node/factory.rb', line 45

def create_node(config, parent = nil)
  raise Bcome::Exception::InvalidNetworkConfig, 'missing config type' unless config[:type]

  klass = klass_for_view_type[config[:type]]

  raise Bcome::Exception::InvalidNetworkConfig, "invalid config type #{config[:type]}" unless klass

  node = klass.new(views: config, parent: parent)
  create_tree(node, config[:views]) if config[:views] && config[:views].any?
  parent.resources << node if parent

  bucket[node.keyed_namespace] = node

  node
end

#create_tree(context_node, views) ⇒ Object



31
32
33
# File 'lib/objects/node/factory.rb', line 31

def create_tree(context_node, views)
  views.each { |config| create_node(config, context_node) }
end

#estate_configObject



83
84
85
# File 'lib/objects/node/factory.rb', line 83

def estate_config
  @estate_config ||= reformat_config(load_estate_config)
end

#init_treeObject



18
19
20
21
# File 'lib/objects/node/factory.rb', line 18

def init_tree
  @estate = create_node(estate_config)
  @estate
end

#is_running_deprecated_configs?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/objects/node/factory.rb', line 103

def is_running_deprecated_configs?
  File.exist?("bcome/config/platform.yml")
end

#is_valid_view_type?(view_type) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/objects/node/factory.rb', line 79

def is_valid_view_type?(view_type)
  klass_for_view_type.keys.include?(view_type)
end

#klass_for_view_typeObject



71
72
73
74
75
76
77
# File 'lib/objects/node/factory.rb', line 71

def klass_for_view_type
  {
    COLLECTION_KEY => ::Bcome::Node::Collection,
    INVENTORY_KEY => ::Bcome::Node::Inventory::Defined,
    SUBSELECT_KEY => ::Bcome::Node::Inventory::Subselect
  }
end

#load_estate_configObject



93
94
95
96
97
98
99
100
101
# File 'lib/objects/node/factory.rb', line 93

def load_estate_config
  config = YAML.load_file(config_path).deep_symbolize_keys
  return config
rescue ArgumentError, Psych::SyntaxError => e
  raise Bcome::Exception::InvalidNetworkConfig, 'Invalid yaml in config' + e.message
rescue Errno::ENOENT
  raise Bcome::Exception::DeprecationWarning if is_running_deprecated_configs?
  raise Bcome::Exception::MissingNetworkConfig, config_path
end

#reformat_config(config) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/objects/node/factory.rb', line 35

def reformat_config(config)
  conf = ::Bcome::ConfigFactory.new
  config.each do |crumb, data|
    validate_view(crumb, data)
    crumbs = Bcome::Parser::BreadCrumb.parse(crumb)
    conf.add_crumbs(crumbs, data)
  end
  conf.flattened
end

#rewrite_estate_config(data) ⇒ Object



87
88
89
90
91
# File 'lib/objects/node/factory.rb', line 87

def rewrite_estate_config(data)
  File.open(config_path, 'w') do |file|
    file.write data.to_yaml
  end
end

#validate_view(breadcrumb, data) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/objects/node/factory.rb', line 61

def validate_view(breadcrumb, data)
  unless data && data[:type]
    raise Bcome::Exception::InvalidNetworkConfig, "Missing namespace type for for namespace '#{breadcrumb}'"
  end

  unless is_valid_view_type?(data[:type])
    raise Bcome::Exception::InvalidNetworkConfig, "Invalid View Type '#{data[:type]}' for namespace '#{breadcrumb}'. Expecting View Type to be one of: #{klass_for_view_type.keys.join(', ')}"
  end
end