Class: CMDB::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/cmdb/source.rb

Direct Known Subclasses

File, Network

Defined Under Namespace

Classes: Consul, File, Memory, Network

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, prefix) ⇒ Source

Construct a new Source.

Parameters:

  • uri (String, URI)

    logical description of this source

  • prefix (String)

    unique dot-notation prefix of all this source’s keys, if any

Raises:

  • (URI::InvalidURIError)

    if an invalid string is passed



86
87
88
89
90
# File 'lib/cmdb/source.rb', line 86

def initialize(uri, prefix)
  uri = URI.parse(uri) if uri.is_a?(String)
  @uri = uri
  @prefix = prefix
end

Instance Attribute Details

#prefixnil, String (readonly)

The dot-notation prefix of all keys provided by this source. No two sources can share a prefix (other than nil) and no source’s prefix can be a prefix of any other source’s prefix.

Some sources have no prefix, in which case this reader returns nil.

Returns:

  • (nil, String)

    unique dot-notation prefix of all this source’s keys, if any



13
14
15
# File 'lib/cmdb/source.rb', line 13

def prefix
  @prefix
end

#uriURI (readonly)

Returns logical description of this source.

Returns:

  • (URI)

    logical description of this source



4
5
6
# File 'lib/cmdb/source.rb', line 4

def uri
  @uri
end

Class Method Details

.create(uri) ⇒ Object

Construct a source given a URI that identifies both the type of source (consul, file or environment) and its location if applicable. Choose a suitable prefix for the source based on the URI contents.

This method accepts a special URI notation that is specific to the cmdb gem; in this notation, the scheme of the URI specifies the type of source (consul, file, etc) and the other components describe how to locate the source.

Examples:

environment source

CMDB::Source.create('env')

JSON source

CMDB::Source.create('file://awesome.json')

YAML source

CMDB::Source.create('file://awesome.yml')

consul source at the root of the k/v tree with prefix “otherhost” (probably not desirable!)

CMDB::Source.create('consul://otherhost.example.com')

consul source at the root of the k/v tree with prefix “myapp”

CMDB::Source.create('consul://otherhost.example.com#myapp')

consul source whose keys are drawn from a subtree of the k/v with prefix “interesting.”

CMDB::Source.create('consul://localhost/interesting')

consul source with nonstandard location and port and prefix “my-kv”

CMDB::Source.create('consul://my-kv:18500')

Parameters:

  • location (String, URI)

    of source

Raises:

  • ArgumentError if URL scheme is unrecognized



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cmdb/source.rb', line 48

def self.create(uri)
  uri = URI.parse(uri) if uri.is_a?(String)

  if !uri.path.nil? && !uri.path.empty?
    prefix = ::File.basename(uri.path, '.*')
  else
    prefix = nil
  end

  case uri.scheme
  when 'consul'
    Source::Consul.new(uri, prefix)
  when 'file'
    Source::File.new(uri, prefix)
  when 'memory'
    Source::Memory.new({},prefix)
  else
    raise ArgumentError, "Unrecognized URL scheme '#{uri.scheme}'"
  end
end

.detectArray

Test for the presence of some default sources and return any that exist.

Returns:

  • (Array)

    a set of initialized CMDB sources



72
73
74
75
76
77
78
79
# File 'lib/cmdb/source.rb', line 72

def self.detect
  sources = []

  consul = create('consul://localhost')
  sources << consul if consul.ping

  sources
end