Class: ChefZero::DataStore::DefaultFacade

Inherits:
InterfaceV2 show all
Defined in:
lib/chef_zero/data_store/default_facade.rb

Overview

The DefaultFacade exists to layer defaults on top of an existing data store. When you create an org, you just create the directory itself: the rest of the org (such as environments/_default) will not actually exist anywhere, but when you get(/organizations/org/environments/_default), the DefaultFacade will create one for you on the fly.

acls in particular are instantiated on the fly using this method.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from InterfaceV2

#interface_version

Methods inherited from InterfaceV1

#interface_version

Constructor Details

#initialize(real_store, single_org, osc_compat, superusers = nil) ⇒ DefaultFacade

Returns a new instance of DefaultFacade


16
17
18
19
20
# File 'lib/chef_zero/data_store/default_facade.rb', line 16

def initialize(real_store, single_org, osc_compat, superusers = nil)
  @real_store = real_store
  @default_creator = ChefData::DefaultCreator.new(self, single_org, osc_compat, superusers)
  clear
end

Instance Attribute Details

#default_creatorObject (readonly)

Returns the value of attribute default_creator


23
24
25
# File 'lib/chef_zero/data_store/default_facade.rb', line 23

def default_creator
  @default_creator
end

#real_storeObject (readonly)

Returns the value of attribute real_store


22
23
24
# File 'lib/chef_zero/data_store/default_facade.rb', line 22

def real_store
  @real_store
end

Instance Method Details

#clearObject


25
26
27
28
# File 'lib/chef_zero/data_store/default_facade.rb', line 25

def clear
  real_store.clear if real_store.respond_to?(:clear)
  default_creator.clear
end

#create(path, name, data, *options) ⇒ Object


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/chef_zero/data_store/default_facade.rb', line 49

def create(path, name, data, *options)
  if default_creator.exists?(path + [ name ]) && !options.include?(:create_dir)
    raise DataAlreadyExistsError.new(path + [name])
  end

  begin
    real_store.create(path, name, data, *options)
  rescue DataNotFoundError
    if default_creator.exists?(path)
      real_store.create(path, name, data, :create_dir, *options)
    else
      raise
    end
  end

  options_hash = options.last.is_a?(Hash) ? options.last : {}
  default_creator.created(path + [ name ], options_hash[:requestor], options.include?(:recursive))
end

#create_dir(path, name, *options) ⇒ Object


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/chef_zero/data_store/default_facade.rb', line 30

def create_dir(path, name, *options)
  if default_creator.exists?(path + [ name ]) && !options.include?(:recursive)
    raise DataAlreadyExistsError.new(path + [name])
  end

  begin
    real_store.create_dir(path, name, *options)
  rescue DataNotFoundError
    if default_creator.exists?(path)
      real_store.create_dir(path, name, :recursive, *options)
    else
      raise
    end
  end

  options_hash = options.last.is_a?(Hash) ? options.last : {}
  default_creator.created(path + [ name ], options_hash[:requestor], options.include?(:recursive))
end

#delete(path, *options) ⇒ Object


100
101
102
103
104
105
106
107
108
109
# File 'lib/chef_zero/data_store/default_facade.rb', line 100

def delete(path, *options)
  deleted = default_creator.deleted(path)
  begin
    real_store.delete(path)
  rescue DataNotFoundError
    if !deleted
      raise
    end
  end
end

#delete_dir(path, *options) ⇒ Object


111
112
113
114
115
116
117
118
119
120
# File 'lib/chef_zero/data_store/default_facade.rb', line 111

def delete_dir(path, *options)
  deleted = default_creator.deleted(path)
  begin
    real_store.delete_dir(path, *options)
  rescue DataNotFoundError
    if !deleted
      raise
    end
  end
end

#exists?(path) ⇒ Boolean

Returns:

  • (Boolean)

140
141
142
# File 'lib/chef_zero/data_store/default_facade.rb', line 140

def exists?(path)
  real_store.exists?(path) || default_creator.exists?(path)
end

#exists_dir?(path) ⇒ Boolean

Returns:

  • (Boolean)

144
145
146
# File 'lib/chef_zero/data_store/default_facade.rb', line 144

def exists_dir?(path)
  real_store.exists_dir?(path) || default_creator.exists?(path)
end

#get(path, request = nil) ⇒ Object


68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/chef_zero/data_store/default_facade.rb', line 68

def get(path, request=nil)
  begin
    real_store.get(path, request)
  rescue DataNotFoundError
    result = default_creator.get(path)
    if result
      FFI_Yajl::Encoder.encode(result, :pretty => true)
    else
      raise
    end
  end
end

#list(path) ⇒ Object


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/chef_zero/data_store/default_facade.rb', line 122

def list(path)
  default_results = default_creator.list(path)
  begin
    real_results = real_store.list(path)
    if default_results
      (real_results + default_results).uniq
    else
      real_results
    end
  rescue DataNotFoundError
    if default_results
      default_results
    else
      raise
    end
  end
end

#set(path, data, *options) ⇒ Object


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/chef_zero/data_store/default_facade.rb', line 81

def set(path, data, *options)
  begin
    real_store.set(path, data, *options)
  rescue DataNotFoundError
    if options.include?(:create_dir) ||
       options.include?(:create) && default_creator.exists?(path[0..-2]) ||
       default_creator.exists?(path)
      real_store.set(path, data, :create, :create_dir, *options)
    else
      raise
    end
  end

  if options.include?(:create)
    options_hash = options.last.is_a?(Hash) ? options.last : {}
    default_creator.created(path, options_hash[:requestor], options.include?(:create_dir))
  end
end