cmdb 3.0.0rc2 → 3.0.0rc3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2a5fa442a4ff4b6f1d7955c804b79bce26e232a5
4
- data.tar.gz: 60a350561cc2612639f53f41b1427b6d0ace23b0
3
+ metadata.gz: 4c44ac22f3267a7961d7034b5a0b56fc34a7bd7a
4
+ data.tar.gz: dfd4eaded71592fe0f8be7aa5b2a552610972811
5
5
  SHA512:
6
- metadata.gz: 949bf77549aefdbdb499e4d9ab818d826f5359d026dd8bd4bf6f0046a809e69e54369cb975962c3ef0c367a8ab5e3106731050dacf71eec5149fc109fec4a6a7
7
- data.tar.gz: f5e1c629aa3135b333c89cffa7e2f1d1a4a05266c4be9fea75e3ba0cc5751c1f89c4f70d4859b464b205bb570ff67b0fb26cb898b925fa7896ccf3e96da99a53
6
+ metadata.gz: 7910184801caaa46c493b1eca7094bf9d5107741cb20c67f1fdaaa537b99652634c528b7d9864865ddd0cdb8e902cb0373fd829ca70fb595ff39c075f1fa3541
7
+ data.tar.gz: 5779dece8e9682d685a148a6c5315a10aae3b6be19d2505e02ed0beb923895a3b8c931068e97322874f9fbd63f8379bbc704c9a503de39fedb03003961692dae
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cmdb (3.0.0rc2)
4
+ cmdb (3.0.0rc3)
5
5
  trollop (~> 2.0)
6
6
 
7
7
  GEM
data/Rakefile CHANGED
@@ -23,8 +23,8 @@ task :sandbox do
23
23
  compose = Docker::Compose::Session.new
24
24
  compose.up 'consul', detached:true
25
25
  mapper = Docker::Compose::Mapper.new(compose)
26
- url = mapper.map('consul://consul:8500/sandbox')
26
+ uri = mapper.map('consul://consul:8500/sandbox')
27
27
 
28
28
  lib = File.expand_path('../lib', __FILE__)
29
- exec "bin/shell --source=#{url}"
29
+ exec "bin/shell --source=#{uri}"
30
30
  end
data/exe/cmdb CHANGED
@@ -90,15 +90,15 @@ rescue CMDB::BadKey => e
90
90
  CMDB.log.fatal "CMDB: Bad Key: malformed CMDB key '#{e.key}'"
91
91
  exit 1
92
92
  rescue CMDB::BadValue => e
93
- CMDB.log.fatal "CMDB: Bad Value: illegal value for CMDB key '#{e.key}' in source #{e.url}"
93
+ CMDB.log.fatal "CMDB: Bad Value: illegal value for CMDB key '#{e.key}' in source #{e.uri}"
94
94
  exit 2
95
95
  rescue CMDB::BadData => e
96
- CMDB.log.fatal "CMDB: Bad Data: malformed CMDB data in source #{e.url}"
96
+ CMDB.log.fatal "CMDB: Bad Data: malformed CMDB data in source #{e.uri}"
97
97
  exit 3
98
98
  rescue CMDB::ValueConflict => e
99
99
  CMDB.log.fatal "CMDB: Value Conflict: #{e.message}"
100
100
  e.sources.each do |s|
101
- CMDB.log.fatal " - #{s.url}"
101
+ CMDB.log.fatal " - #{s.uri}"
102
102
  end
103
103
  exit 4
104
104
  rescue CMDB::NameConflict => e
data/lib/cmdb.rb CHANGED
@@ -41,17 +41,17 @@ module CMDB
41
41
 
42
42
  # A value of an unsupported type was encountered in the CMDB or filesystem.
43
43
  class BadValue < Error
44
- # @return [URI] filesystem or network location of the bad value
45
- attr_reader :url
44
+ # @return [URI] source that contains the bad data
45
+ attr_reader :uri
46
46
 
47
47
  # @return [String] the name of the key that contained the bad value
48
48
  attr_reader :key
49
49
 
50
- # @param [URI] url filesystem or network location of the bad value
50
+ # @param [URI] source that contains the bad value
51
51
  # @param [String] key CMDB key name under which the bad value was found
52
52
  # @param [Object] value the bad value itself
53
- def initialize(url, key, value, desc=nil)
54
- @url = url
53
+ def initialize(uri, key, value, desc=nil)
54
+ @uri = uri
55
55
  @key = key
56
56
  msg = "Unsupported #{value.class.name} value"
57
57
  msg << ": #{desc}" if desc
@@ -61,13 +61,13 @@ module CMDB
61
61
 
62
62
  # Malformed data was encountered in the CMDB or filesystem.
63
63
  class BadData < Error
64
- # @return [URI] filesystem or network location of the bad data
65
- attr_reader :url
64
+ # @return [URI] source that contains the bad data
65
+ attr_reader :uri
66
66
 
67
- # @param [URI] url filesystem or network location of the bad value
67
+ # @param [URI] source that contains the bad value
68
68
  # @param [String] context brief description of where data was found e.g. 'CMDB data file' or 'input config file'
69
- def initialize(url, context = nil)
70
- @url = url
69
+ def initialize(uri, context = nil)
70
+ @uri = uri
71
71
  super("Malformed data encountered #{(' in ' + context) if context}")
72
72
  end
73
73
  end
data/lib/cmdb/source.rb CHANGED
@@ -1,24 +1,25 @@
1
1
  module CMDB
2
2
  class Source
3
- # Determine the prefix of all keys provided by this source. No two sources can share
4
- # a prefix (other than nil) and no source's prefix can be a prefix of any other
5
- # source's prefix.
3
+ # @return [URI] logical description of this source
4
+ attr_reader :uri
5
+
6
+ # The dot-notation prefix of all keys provided by this source. No two
7
+ # sources can share a prefix (other than nil) and no source's prefix can
8
+ # be a prefix of any other source's prefix.
6
9
  #
7
- # Some sources have no common prefix, in which case this reader returns nil.
10
+ # Some sources have no prefix, in which case this reader returns nil.
8
11
  #
9
- # @return [nil,String] common and unique dot-notation prefix of this source's keys, if any
12
+ # @return [nil,String] unique dot-notation prefix of all this source's keys, if any
10
13
  attr_reader :prefix
11
14
 
12
- # @return [URI] a URL describing where this source's data comes from
13
- attr_reader :url
14
-
15
15
  # Construct a source given a URI that identifies both the type of
16
16
  # source (consul, file or environment) and its location if applicable.
17
17
  # Choose a suitable prefix for the source based on the URI contents.
18
18
  #
19
- # If you pass a fragment as part of the URI, then the fragment becomes the
20
- # prefix. Otherwise, the final path component becomes the prefix, unless
21
- # it is empty in which case the first hostname component becomes the prefix.
19
+ # This method accepts a special URI notation that is specific to the cmdb
20
+ # gem; in this notation, the scheme of the URI specifies the type of source
21
+ # (consul, file, etc) and the other components describe how to locate the
22
+ # source.
22
23
  #
23
24
  # @param [String,URI] location of source
24
25
  #
@@ -55,15 +56,11 @@ module CMDB
55
56
 
56
57
  case uri.scheme
57
58
  when 'consul'
58
- curi = uri.dup
59
- curi.scheme = 'http'
60
- curi.port ||= 8500
61
- curi.path = ''
62
- Source::Consul.new(URI.parse(curi.to_s), prefix)
59
+ Source::Consul.new(uri, prefix)
63
60
  when 'file'
64
- Source::File.new(uri.path, prefix)
61
+ Source::File.new(uri, prefix)
65
62
  when 'memory'
66
- Source::Memory.new({},nil)
63
+ Source::Memory.new({},prefix)
67
64
  else
68
65
  raise ArgumentError, "Unrecognized URL scheme '#{uri.scheme}'"
69
66
  end
@@ -81,6 +78,17 @@ module CMDB
81
78
  sources
82
79
  end
83
80
 
81
+ # Construct a new Source.
82
+ #
83
+ # @param [String,URI] uri logical description of this source
84
+ # @param [String] prefix unique dot-notation prefix of all this source's keys, if any
85
+ # @raise [URI::InvalidURIError] if an invalid string is passed
86
+ def initialize(uri, prefix)
87
+ uri = URI.parse(uri) if uri.is_a?(String)
88
+ @uri = uri
89
+ @prefix = prefix
90
+ end
91
+
84
92
  private
85
93
 
86
94
  # Check whether a key's prefix is suitable for this source.
@@ -8,6 +8,10 @@ module CMDB
8
8
  # Regular expression to match array values
9
9
  ARRAY_VALUE = /^\[(.*)\]$/
10
10
 
11
+ def initialize(uri, prefix)
12
+ super(uri, 8500, prefix)
13
+ end
14
+
11
15
  # Get a single key from consul. If the key is not found, return nil.
12
16
  #
13
17
  # @param [String] key dot-notation key
@@ -77,15 +81,13 @@ module CMDB
77
81
  false
78
82
  end
79
83
 
80
- private
81
-
82
84
  # Given a key's relative path, return its absolute REST path in the consul
83
- # kv, including prefix if appropriate.
84
- def path_to(subkey)
85
- p = '/v1/kv/'
86
- p << prefix << '/' if prefix
87
- p << subkey unless (subkey == '/' && p[-1] == '/')
88
- p
85
+ # kv, including the `/v1/kv/` prefix and any subkey path specified at
86
+ # initialize time.
87
+ #
88
+ # @param [String] subpath key path relative to base
89
+ def path_to(subpath)
90
+ ::File.join('/v1/kv/', @uri.path, subpath)
89
91
  end
90
92
  end
91
93
  end
@@ -10,32 +10,33 @@ module CMDB
10
10
  # source = Source::File.new('/tmp/my.yml') # contains a top-level stanza named "database"
11
11
  # source['my']['database']['host'] # => 'db1-1.example.com'
12
12
  class Source::File < Source
13
- # Construct a new Source::File from an input file.
14
- # @param [String,Pathname] filename path to a file
15
- # @param [String] prefix optional prefix of
13
+ # Read a data file whose location is specified by a file:// URI.
14
+ #
15
+ # @param [URI] uri logical description of this source
16
+ # @param [String] prefix unique dot-notation prefix of all this source's keys, if any
16
17
  # @raise [BadData] if the file's content is malformed
17
- def initialize(filename, prefix)
18
- @data = {}
19
- @prefix = prefix
20
- filename = ::File.expand_path(filename)
21
- @url = URI.parse("file://#{filename}")
22
- @extension = ::File.extname(filename)
23
- raw_bytes = ::File.read(filename)
18
+ def initialize(uri, prefix)
19
+ super(uri, prefix)
20
+ path = @uri.path
21
+ filename = ::File.basename(path)
22
+ extension = ::File.extname(filename)
23
+ raw_bytes = ::File.read(path)
24
24
  raw_data = nil
25
25
 
26
26
  begin
27
- case @extension
27
+ case extension
28
28
  when /jso?n?$/i
29
29
  raw_data = JSON.load(raw_bytes)
30
30
  when /ya?ml$/i
31
31
  raw_data = YAML.load(raw_bytes)
32
32
  else
33
- raise BadData.new(url, 'file with unknown extension; expected js(on) or y(a)ml')
33
+ raise BadData.new(@uri, 'file with unknown extension; expected js(on) or y(a)ml')
34
34
  end
35
35
  rescue StandardError
36
- raise BadData.new(url, 'CMDB data file')
36
+ raise BadData.new(@uri, 'CMDB data file')
37
37
  end
38
38
 
39
+ @data = {}
39
40
  flatten(raw_data, @prefix, @data)
40
41
  end
41
42
 
@@ -66,7 +67,7 @@ module CMDB
66
67
  when Array
67
68
  if value.any? { |e| e.is_a?(Hash) }
68
69
  # mismatched arrays: not allowed
69
- raise BadValue.new(url, key, value, 'hashes not allowed inside arrays')
70
+ raise BadValue.new(uri, key, value, 'hashes not allowed inside arrays')
70
71
  elsif value.all? { |e| e.is_a?(String) } ||
71
72
  value.all? { |e| e.is_a?(Numeric) } ||
72
73
  value.all? { |e| e == true } ||
@@ -74,13 +75,13 @@ module CMDB
74
75
  output[key] = value
75
76
  else
76
77
  # mismatched arrays: not allowed
77
- raise BadValue.new(url, key, value, 'mismatched/unsupported element types')
78
+ raise BadValue.new(uri, key, value, 'mismatched/unsupported element types')
78
79
  end
79
80
  when String, Numeric, TrueClass, FalseClass
80
81
  output[key] = value
81
82
  else
82
83
  # nil and anything else: not allowed
83
- raise BadValue.new(url, key, value)
84
+ raise BadValue.new(uri, key, value)
84
85
  end
85
86
  end
86
87
  end
@@ -10,8 +10,9 @@ module CMDB
10
10
 
11
11
  # Construct a new Source::Memory.
12
12
  def initialize(hash, prefix)
13
+ uri = URI.parse("memory:#{hash.object_id}")
14
+ super(uri, prefix)
13
15
  @hash = hash
14
- @prefix = prefix
15
16
  end
16
17
 
17
18
  # Get the value of key.
@@ -1,10 +1,21 @@
1
1
  module CMDB
2
+ # Abstract base class for sources that are backed by an HTTP k/v store.
3
+ # Contains reusable HTTP request logic.
2
4
  class Source::Network < Source
3
- # @param [URI] uri
5
+ # @return [URI] HTTP base location of this source (minus path)
6
+ attr_reader :http_url
7
+
8
+ # Construct a new HTTP source. The logical URI is transformed into an
9
+ # HTTP base URL by preserving the hostname, overriding the port (unless
10
+ # the URI already has a specific port), replacing the scheme with http
11
+ # and eliminating the path.
12
+ #
13
+ # @param [String,URI] uri logical description of this source
14
+ # @param [Integer] port default HTTP port if not specified in URI
4
15
  # @param [String] dot-notation prefix of all keys
5
- def initialize(uri, prefix)
6
- @uri = uri
7
- @prefix = prefix
16
+ def initialize(uri, port, prefix)
17
+ super(uri, prefix)
18
+ @http_url = URI.parse("http://%s:%s" % [@uri.host, @uri.port || port])
8
19
  end
9
20
 
10
21
  private
@@ -16,12 +27,12 @@ module CMDB
16
27
  # @param [String] path
17
28
  # @return [Integer,String]
18
29
  def http_get(path, query:nil, retries:3)
19
- @http ||= Net::HTTP.start(@uri.host, @uri.port)
20
- uri = @uri.dup
21
- uri.path = path
22
- uri.query = query unless query.nil? || query.empty?
30
+ @http ||= Net::HTTP.start(@http_url.host, @http_url.port)
31
+ url = @http_url.dup
32
+ url.path = path
33
+ url.query = query unless query.nil? || query.empty?
23
34
 
24
- request = Net::HTTP::Get.new uri
35
+ request = Net::HTTP::Get.new url
25
36
  response = @http.request request
26
37
 
27
38
  case response.code.to_i
@@ -47,11 +58,11 @@ module CMDB
47
58
  def http_put(path, entity)
48
59
  entity = JSON.dump(entity) unless entity.is_a?(String)
49
60
 
50
- @http ||= Net::HTTP.start(@uri.host, @uri.port)
51
- uri = @uri.dup
52
- uri.path = path
61
+ @http ||= Net::HTTP.start(@http_url.host, @http_url.port)
62
+ url = @http_url.dup
63
+ url.path = path
53
64
 
54
- request = Net::HTTP::Put.new uri
65
+ request = Net::HTTP::Put.new url
55
66
  request.body = entity
56
67
  response = @http.request request
57
68
 
@@ -64,11 +75,11 @@ module CMDB
64
75
  # @return [Integer] HTTP status code
65
76
  # @param [String] path
66
77
  def http_delete(path)
67
- @http ||= Net::HTTP.start(@uri.host, @uri.port)
68
- uri = @uri.dup
69
- uri.path = path
78
+ @http ||= Net::HTTP.start(@http_url.host, @http_url.port)
79
+ url = @http_url.dup
80
+ url.path = path
70
81
 
71
- request = Net::HTTP::Delete.new uri
82
+ request = Net::HTTP::Delete.new url
72
83
  response = @http.request request
73
84
 
74
85
  response.code.to_i
data/lib/cmdb/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module CMDB
3
- VERSION = '3.0.0rc2'.freeze
3
+ VERSION = '3.0.0rc3'.freeze
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cmdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0rc2
4
+ version: 3.0.0rc3
5
5
  platform: ruby
6
6
  authors:
7
7
  - RightScale