summaryrefslogtreecommitdiff
path: root/lib/chef_zero/data_store/memory_store.rb
blob: c7e5b7fd95a35ea442d3a260496b751ee8e801f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#
# Author:: John Keiser (<jkeiser@opscode.com>)
# Copyright:: Copyright (c) 2013 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'chef_zero/data_store/data_already_exists_error'
require 'chef_zero/data_store/data_not_found_error'

module ChefZero
  module DataStore
    class MemoryStore
      def initialize
        clear
      end

      def clear
        @data = {}

        create_dir([], 'organizations')
      end

      def create_org
        # Create containers
        org = {
          'clients' => {
            'chef-validator' => '{ "validator": true }',
            'chef-webui' => '{ "admin": true }'
          },
          'cookbooks' => {},
          'data' => {},
          'environments' => {
            '_default' => '{ "description": "The default Chef environment" }'
          },
          'file_store' => {
            'checksums' => {}
          },
          'nodes' => {},
          'roles' => {},
          'sandboxes' => {},
          'users' => {
            'admin' => '{ "admin": "true" }'
          }
        }
      end

      def create_dir(path, name, *options)
        parent = _get(path, options.include?(:recursive))

        if parent.has_key?(name)
          if !options.include?(:recursive)
            raise DataAlreadyExistsError.new(path + [name])
          end
        else
          _create_dir(path, parent, name)
        end
      end

      def create(path, name, data, *options)
        if !data.is_a?(String)
          raise "set only works with strings"
        end

        parent = _get(path, options.include?(:create_dir))

        if parent.has_key?(name)
          raise DataAlreadyExistsError.new(path + [name])
        end
        parent[name] = data
      end

      def get(path, request=nil)
        value = _get(path)
        if value.is_a?(Hash)
          raise "get() called on directory #{path.join('/')}"
        end
        value
      end

      def set(path, data, *options)
        if !data.is_a?(String)
          raise "set only works with strings: #{path} = #{data.inspect}"
        end

        # Get the parent
        parent = _get(path[0..-2], options.include?(:create_dir))

        if !options.include?(:create) && !parent[path[-1]]
          raise DataNotFoundError.new(path)
        end
        parent[path[-1]] = data
      end

      def delete(path)
        parent = _get(path[0,path.length-1])
        if !parent.has_key?(path[-1])
          raise DataNotFoundError.new(path)
        end
        if !parent[path[-1]].is_a?(String)
          raise "delete only works with strings: #{path}"
        end
        parent.delete(path[-1])
      end

      def delete_dir(path, *options)
        parent = _get(path[0,path.length-1])
        if !parent.has_key?(path[-1])
          raise DataNotFoundError.new(path)
        end
        if !parent[path[-1]].is_a?(Hash)
          raise "delete_dir only works with directories: #{path}"
        end
        parent.delete(path[-1])
      end

      def list(path)
        dir = _get(path)
        if !dir.is_a? Hash
          raise "list only works with directories (#{path} = #{dir.class}"
        end
        dir.keys.sort
      end

      def exists?(path)
        begin
          get(path)
          return true
        rescue DataNotFoundError
          return false
        end
      end

      def exists_dir?(path)
        begin
          dir = _get(path)
          if !dir.is_a? Hash
            raise "exists_dir? only works with directories (#{path} = #{dir.class}"
          end
          return true
        rescue DataNotFoundError
          return false
        end
      end

      private

      def _get(path, create_dir=false)
        value = @data
        path.each_with_index do |path_part, index|
          if !value.has_key?(path_part)
            if create_dir
              _create_dir(path[0,index], value, path_part)
            else
              raise DataNotFoundError.new(path[0,index+1])
            end
          end
          value = value[path_part]
        end
        value
      end

      def _create_dir(parent_path, parent, name)
        if parent_path == [ 'organizations' ]
          parent[name] = create_org
        else
          parent[name] = {}
        end
      end
    end
  end
end