Class: ChefZero::DataStore::V1ToV2Adapter

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

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, options = {}) ⇒ V1ToV2Adapter

Returns a new instance of V1ToV2Adapter


6
7
8
9
10
11
# File 'lib/chef_zero/data_store/v1_to_v2_adapter.rb', line 6

def initialize(real_store, single_org, options = {})
  @real_store = real_store
  @single_org = single_org
  @options = options
  clear
end

Instance Attribute Details

#real_storeObject (readonly)

Returns the value of attribute real_store


13
14
15
# File 'lib/chef_zero/data_store/v1_to_v2_adapter.rb', line 13

def real_store
  @real_store
end

#single_orgObject (readonly)

Returns the value of attribute single_org


14
15
16
# File 'lib/chef_zero/data_store/v1_to_v2_adapter.rb', line 14

def single_org
  @single_org
end

Instance Method Details

#clearObject


16
17
18
# File 'lib/chef_zero/data_store/v1_to_v2_adapter.rb', line 16

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

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

Raises:


29
30
31
32
33
34
35
36
# File 'lib/chef_zero/data_store/v1_to_v2_adapter.rb', line 29

def create(path, name, data, *options)
  raise DataNotFoundError.new(path) if skip_organizations?(path)
  raise "Cannot create #{name} at #{path} with V1ToV2Adapter: only handles a single org named #{single_org}." if skip_organizations?(path, name)
  raise DataAlreadyExistsError.new(path + [ name ]) if path.size < 2
  fix_exceptions do
    real_store.create(path[2..-1], name, data, *options)
  end
end

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

Raises:


20
21
22
23
24
25
26
27
# File 'lib/chef_zero/data_store/v1_to_v2_adapter.rb', line 20

def create_dir(path, name, *options)
  raise DataNotFoundError.new(path) if skip_organizations?(path)
  raise "Cannot create #{name} at #{path} with V1ToV2Adapter: only handles a single org named #{single_org}." if skip_organizations?(path, name)
  raise DataAlreadyExistsError.new(path + [ name ]) if path.size < 2
  fix_exceptions do
    real_store.create_dir(path[2..-1], name, *options)
  end
end

#delete(path, *options) ⇒ Object

Raises:


61
62
63
64
65
66
# File 'lib/chef_zero/data_store/v1_to_v2_adapter.rb', line 61

def delete(path, *options)
  raise DataNotFoundError.new(path) if skip_organizations?(path) && !options.include?(:recursive)
  fix_exceptions do
    real_store.delete(path[2..-1])
  end
end

#delete_dir(path, *options) ⇒ Object

Raises:


68
69
70
71
72
73
# File 'lib/chef_zero/data_store/v1_to_v2_adapter.rb', line 68

def delete_dir(path, *options)
  raise DataNotFoundError.new(path) if skip_organizations?(path) && !options.include?(:recursive)
  fix_exceptions do
    real_store.delete_dir(path[2..-1], *options)
  end
end

#exists?(path) ⇒ Boolean

Returns:

  • (Boolean)

88
89
90
91
92
93
# File 'lib/chef_zero/data_store/v1_to_v2_adapter.rb', line 88

def exists?(path)
  return false if skip_organizations?(path)
  fix_exceptions do
    real_store.exists?(path[2..-1])
  end
end

#exists_dir?(path) ⇒ Boolean

Returns:

  • (Boolean)

95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/chef_zero/data_store/v1_to_v2_adapter.rb', line 95

def exists_dir?(path)
  return false if skip_organizations?(path)
  if path == []
    true
  elsif path == [ 'organizations' ] || path == [ 'users' ]
    true
  else
    return false if skip_organizations?(path)
    fix_exceptions do
      real_store.exists_dir?(path[2..-1])
    end
  end
end

#get(path, request = nil) ⇒ Object

Raises:


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/chef_zero/data_store/v1_to_v2_adapter.rb', line 38

def get(path, request=nil)
  raise DataNotFoundError.new(path) if skip_organizations?(path)
  fix_exceptions do
    # Make it so build_uri will include /organizations/ORG inside the V1 data store
    if request && request.rest_base_prefix.size == 0
      old_base_uri = request.base_uri
      request.base_uri = File.join(request.base_uri, 'organizations', single_org)
    end
    begin
      real_store.get(path[2..-1], request)
    ensure
      request.base_uri = old_base_uri if request && request.rest_base_prefix.size == 0
    end
  end
end

#list(path) ⇒ Object

Raises:


75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/chef_zero/data_store/v1_to_v2_adapter.rb', line 75

def list(path)
  raise DataNotFoundError.new(path) if skip_organizations?(path)
  if path == []
    [ 'organizations' ]
  elsif path == [ 'organizations' ]
    [ single_org ]
  else
    fix_exceptions do
      real_store.list(path[2..-1])
    end
  end
end

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

Raises:


54
55
56
57
58
59
# File 'lib/chef_zero/data_store/v1_to_v2_adapter.rb', line 54

def set(path, data, *options)
  raise DataNotFoundError.new(path) if skip_organizations?(path)
  fix_exceptions do
    real_store.set(path[2..-1], data, *options)
  end
end