cmdb 3.0.0rc3 → 3.0.0rc4

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: 4c44ac22f3267a7961d7034b5a0b56fc34a7bd7a
4
- data.tar.gz: dfd4eaded71592fe0f8be7aa5b2a552610972811
3
+ metadata.gz: c88c245ee9bc31f0fe4f261b3d688402b0f06be1
4
+ data.tar.gz: 0e6b981474487959202b1bc16a85e5411754c818
5
5
  SHA512:
6
- metadata.gz: 7910184801caaa46c493b1eca7094bf9d5107741cb20c67f1fdaaa537b99652634c528b7d9864865ddd0cdb8e902cb0373fd829ca70fb595ff39c075f1fa3541
7
- data.tar.gz: 5779dece8e9682d685a148a6c5315a10aae3b6be19d2505e02ed0beb923895a3b8c931068e97322874f9fbd63f8379bbc704c9a503de39fedb03003961692dae
6
+ metadata.gz: a613be0d9de98744ba0b86d799d6f3685e46632a8d8373cb6ba1b1b473b326a26ce6b95912be9b60f31f5090ca2a883784fdf89d0e3362a9a5acbc6d15953887
7
+ data.tar.gz: 337b285e2ed120e8b543fe4cc0e1c9e0e1af759589aee4072c022bc76a793272b934185c12db54b2bd9860fe2a325c5c879423f4b38b45423f08ebde01d29b16
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cmdb (3.0.0rc3)
4
+ cmdb (3.0.0rc4)
5
5
  trollop (~> 2.0)
6
6
 
7
7
  GEM
data/Rakefile CHANGED
@@ -23,8 +23,9 @@ 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
- uri = mapper.map('consul://consul:8500/sandbox')
26
+ source1 = mapper.map('consul://consul:8500/sandbox/apples')
27
+ source2 = mapper.map('consul://consul:8500/sandbox/oranges')
27
28
 
28
29
  lib = File.expand_path('../lib', __FILE__)
29
- exec "bin/shell --source=#{uri}"
30
+ exec "bin/shell --source=#{source1} --source=#{source2}"
30
31
  end
@@ -127,6 +127,24 @@ module CMDB
127
127
 
128
128
  module_function
129
129
 
130
+ # Split a dotted-notation CMDB key into its constituent parts and return an
131
+ # Array. Optionally limit the amount of splitting done by passing parts > 0.
132
+ #
133
+ # @return [Array]
134
+ # @param [String] key dotted-notation key
135
+ # @param [Integer] parts number of total parts to return
136
+ def split(key, parts=0)
137
+ key.split(SEPARATOR, parts)
138
+ end
139
+
140
+ # Transform a list of key components into a dotted-notation key.
141
+ #
142
+ # @return [String]
143
+ def join(*pieces)
144
+ pieces.flatten!
145
+ pieces.join(SEPARATOR)
146
+ end
147
+
130
148
  def log
131
149
  unless @log
132
150
  @log = Logger.new($stderr)
@@ -72,6 +72,7 @@ cmdb shell
72
72
  if subpath.include?(ALT_SEPARATOR) || subpath =~ NAVIGATION
73
73
  # filesystem-like subpath
74
74
  # apply Unix-style directory navigation shortcuts
75
+ # ge rid of successive //// and other oddities
75
76
  pieces = subpath.split(ALT_SEPARATOR).select { |p| !p.nil? && !p.empty? }
76
77
  pieces.each do |piece|
77
78
  case piece
@@ -81,14 +82,15 @@ cmdb shell
81
82
  end
82
83
  end
83
84
 
84
- result.join(CMDB::SEPARATOR)
85
+ CMDB.join(result)
85
86
  else
86
- pieces = subpath.split(CMDB::SEPARATOR).select { |p| !p.nil? && !p.empty? }
87
87
  # standard dotted notation
88
+ # get rid of successive .... and other oddities
89
+ pieces = CMDB.split(subpath).select { |p| !p.nil? && !p.empty? }
88
90
  result += pieces
89
91
  end
90
92
 
91
- result.join(CMDB::SEPARATOR)
93
+ CMDB.join(result)
92
94
  end
93
95
 
94
96
  private
@@ -48,19 +48,21 @@ module CMDB
48
48
 
49
49
  # Set the value of a CMDB key.
50
50
  #
51
- # @return [Source,ni] the source that accepted the write, if any
51
+ # @return [Source,nil] the source that accepted the write, if any
52
52
  # @raise [BadKey] if the key name is malformed
53
53
  def set(key, value)
54
54
  raise BadKey.new(key) unless key =~ VALID_KEY
55
55
 
56
- @sources.reverse.each do |s|
56
+ wrote = nil
57
+ @sources.each do |s|
57
58
  if s.respond_to?(:set)
58
- s.set(key, value)
59
- return s
59
+ if s.set(key, value)
60
+ wrote = s
61
+ break
62
+ end
60
63
  end
61
64
  end
62
-
63
- nil
65
+ wrote
64
66
  end
65
67
 
66
68
  # Enumerate all of the keys in the CMDB.
@@ -77,13 +79,15 @@ module CMDB
77
79
  self
78
80
  end
79
81
 
80
- def search(prefix)
81
- prefix = Regexp.new('^' + Regexp.escape(prefix)) unless prefix.is_a?(Regexp)
82
+ # @return [Hash] all keys/values that match query
83
+ # @param [String,Regexp] query key name prefix or pattern to search for
84
+ def search(query)
85
+ query = Regexp.new('^' + Regexp.escape(query)) unless query.is_a?(Regexp)
82
86
  result = {}
83
87
 
84
88
  @sources.each do |s|
85
89
  s.each_pair do |k, v|
86
- result[k] = v if k =~ prefix
90
+ result[k] = v if k =~ query
87
91
  end
88
92
  end
89
93
 
@@ -65,7 +65,7 @@ module CMDB::Shell
65
65
 
66
66
  def cd(path)
67
67
  pwd = @shell.expand_path(path)
68
- @shell.pwd = pwd.split(::CMDB::SEPARATOR)
68
+ @shell.pwd = CMDB.split(pwd)
69
69
  pwd.to_sym
70
70
  end
71
71
  alias chdir cd
@@ -10,6 +10,9 @@ module CMDB
10
10
 
11
11
  def initialize(uri, prefix)
12
12
  super(uri, 8500, prefix)
13
+ useless = uri.path.split('/')
14
+ useless.shift ; useless.pop # del initial "" and final word (aka prefix)
15
+ @useless = CMDB.join(useless)
13
16
  end
14
17
 
15
18
  # Get a single key from consul. If the key is not found, return nil.
@@ -33,16 +36,26 @@ module CMDB
33
36
  end
34
37
 
35
38
  # Set a single key in consul. If value is nil, then delete the key
36
- # entirely from consul.
39
+ # entirely from consul. If this source cannot accept the write due to
40
+ # key name limitations, return nil.
37
41
  #
42
+ # @return [true,nil]
38
43
  # @param [String] key dot-notation key
39
44
  # @param [Object] value new value of key
45
+ # @raise [CMDB:Error] if the write fails atthe consul server
40
46
  def set(key, value)
47
+ return nil unless prefixed?(key)
41
48
  key = dot_to_slash(key)
42
49
  if value.nil?
43
- http_delete path_to(key)
50
+ status = http_delete path_to(key)
51
+ else
52
+ status = http_put path_to(key), value
53
+ end
54
+
55
+ if status >= 200 && status < 300
56
+ true
44
57
  else
45
- http_put path_to(key), value
58
+ raise CMDB::Error.new("Consul put/delete failed with status #{status}")
46
59
  end
47
60
  end
48
61
 
@@ -64,6 +77,7 @@ module CMDB
64
77
 
65
78
  result.each do |item|
66
79
  key = slash_to_dot(item['Key'])
80
+ key.sub(@useless,'')
67
81
  next unless item['Value']
68
82
  value = json_parse(Base64.decode64(item['Value']))
69
83
  yield(key, value)
@@ -89,5 +103,14 @@ module CMDB
89
103
  def path_to(subpath)
90
104
  ::File.join('/v1/kv/', @uri.path, subpath)
91
105
  end
106
+
107
+ # Transform a subpath into a key name. Account for base-path prefix if
108
+ # necessary.
109
+ def slash_to_dot(path)
110
+ dot = super
111
+ dot.sub!(@useless,'')
112
+ dot=dot[1..-1] if dot[0] == '.'
113
+ dot
114
+ end
92
115
  end
93
116
  end
@@ -60,7 +60,7 @@ module CMDB
60
60
 
61
61
  def flatten(data, prefix, output)
62
62
  data.each_pair do |key, value|
63
- key = "#{prefix}#{CMDB::SEPARATOR}#{key}"
63
+ key = CMDB.join(prefix, key)
64
64
  case value
65
65
  when Hash
66
66
  flatten(value, key, output)
@@ -99,24 +99,19 @@ module CMDB
99
99
  end
100
100
 
101
101
  # Convert dotted notation to slash-separated notation without an initial
102
- # slash. Remove prefix if appropriate; raise an error if the key name
103
- # does not begin with this source's prefix.
102
+ # slash. Remove prefix if it is present in the dotted-notation key.
104
103
  def dot_to_slash(key)
105
- unless prefixed?(key)
106
- raise CMDB::BadKey.new(key, "Keys of this source must begin with #{prefix}.")
107
- end
108
- pieces = key.split(CMDB::SEPARATOR)
109
- pieces.shift
104
+ pieces = CMDB.split(key)
105
+ pieces.shift if pieces[0] == @prefix
110
106
  pieces.join('/')
111
107
  end
112
108
 
113
- # Convert a slash-separated URI path or subpath to dotted notation. If there is an initial
114
- # slash, discard it. Prepend source's prefix to key name if not already present.
109
+ # Convert a slash-separated URI path or subpath to dotted notation,
110
+ # discarding initial slash. Does not account for prefix in any way!
115
111
  def slash_to_dot(path)
116
112
  pieces = path.split('/')
117
113
  pieces.shift if pieces[0].empty?
118
- pieces.unshift(prefix) unless prefix.nil? || pieces[0] == prefix
119
- pieces.join(CMDB::SEPARATOR)
114
+ CMDB.join(pieces)
120
115
  end
121
116
  end
122
117
  end
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module CMDB
3
- VERSION = '3.0.0rc3'.freeze
3
+ VERSION = '3.0.0rc4'.freeze
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cmdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0rc3
4
+ version: 3.0.0rc4
5
5
  platform: ruby
6
6
  authors:
7
7
  - RightScale
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-24 00:00:00.000000000 Z
11
+ date: 2016-08-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: trollop