Class: Seiso::ImportMaster

Inherits:
Object
  • Object
show all
Defined in:
lib/seiso/import_master.rb,
lib/seiso/import_master/mappers/node_mapper.rb,
lib/seiso/import_master/mappers/service_mapper.rb,
lib/seiso/import_master/importers/base_importer.rb,
lib/seiso/import_master/importers/node_importer.rb,
lib/seiso/import_master/importers/simple_importer.rb,
lib/seiso/import_master/validators/base_validator.rb,
lib/seiso/import_master/validators/node_validator.rb,
lib/seiso/import_master/importers/service_importer.rb,
lib/seiso/import_master/mappers/master_item_mapper.rb,
lib/seiso/import_master/validators/service_validator.rb,
lib/seiso/import_master/errors/invalid_document_error.rb,
lib/seiso/import_master/mappers/service_instance_mapper.rb,
lib/seiso/import_master/importers/service_instance_importer.rb

Overview

Imports Seiso data master files into Seiso.

Author

Willie Wheeler ([email protected])

Copyright

Copyright © 2014-2015 Expedia, Inc.

License

Apache 2.0

Defined Under Namespace

Modules: Errors, Importers, Mappers, Validators

Instance Method Summary collapse

Constructor Details

#initialize(seiso_settings) ⇒ ImportMaster

Initializes the importer with a Seiso connector.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/seiso/import_master.rb', line 18

def initialize(seiso_settings)
  @loaders = {
    'json' => ->(file) { JSON.parse(IO.read(file)) },
    'yaml' => ->(file) { YAML.load_file file }
  }

  # TODO Add other validators
  @validators = {
    'nodes' => Validators::NodeValidator.new,
    'services' => Validators::ServiceValidator.new
  }

  basic_mapper = Mappers::MasterItemMapper.new
  node_mapper = Mappers::NodeMapper.new
  service_mapper = Mappers::ServiceMapper.new
  service_instance_mapper = Mappers::ServiceInstanceMapper.new

  seiso = Seiso::Connector.new seiso_settings
  uri_factory_v1 = uri_factory_v1 seiso_settings
  uri_factory_v2 = uri_factory_v2 seiso_settings
  rest_connector_v1 = rest_connector(seiso_settings, "application/json")
  rest_connector_v2 = rest_connector(seiso_settings, "application/hal+json")

  @simple_importer = Importers::SimpleImporter.new(basic_mapper, seiso, uri_factory_v1, rest_connector_v1)

  # TODO Clean this up. Just use v2 URI factory and connector.
  @importers = {
    'nodes' => Importers::NodeImporter.new(node_mapper, uri_factory_v1, rest_connector_v1),
    'services' => Importers::ServiceImporter.new(
        basic_mapper,
        service_mapper,
        uri_factory_v1,
        uri_factory_v2,
        rest_connector_v1,
        rest_connector_v2),
    'service-instances' => Importers::ServiceInstanceImporter.new(
        service_instance_mapper,
        uri_factory_v1,
        uri_factory_v2,
        seiso,
        rest_connector_v1,
        rest_connector_v2)
  }
end

Instance Method Details

#import_file(file, format = 'json') ⇒ Object

Imports a data master file. Legal formats are ‘json’ (default) and ‘yaml’.

Raises:

  • (ArgumentError)


74
75
76
77
78
79
80
81
82
# File 'lib/seiso/import_master.rb', line 74

def import_file(file, format = 'json')
  loader = @loaders[format]
  raise ArgumentError, "Illegal format: #{format}" if loader.nil?
  doc = loader.call(file)
  type = doc['type']
  validator = @validators[type]
  validator.validate(doc) unless validator.nil?
  (@importers[type] || @simple_importer).import doc
end

#import_files(files, format = 'json') ⇒ Object

Imports a list of master files in order. Legal formats are ‘json’ (default) and ‘yaml’.



64
65
66
67
68
69
70
71
# File 'lib/seiso/import_master.rb', line 64

def import_files(files, format = 'json')
  loop do
    file = files.pop
    puts "Processing: #{file}"
    import_file(file, format)
    break if files.empty?
  end
end