Class: ChefZero::ChefData::DefaultCreator

Inherits:
Object
  • Object
show all
Defined in:
lib/chef_zero/chef_data/default_creator.rb

Overview

The DefaultCreator creates default values when you ask for them.

  • It relies on created and deleted being called when things get created and deleted, so that it knows the owners of said objects and knows to eliminate default values on delete.

  • get, list and exists? get data.

Constant Summary collapse

PERMISSIONS =
%w(create read update delete grant)
DEFAULT_SUPERUSERS =
%w(pivotal)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, single_org, osc_compat, superusers = nil) ⇒ DefaultCreator


13
14
15
16
17
18
19
# File 'lib/chef_zero/chef_data/default_creator.rb', line 13

def initialize(data, single_org, osc_compat, superusers = nil)
  @data = data
  @single_org = single_org
  @osc_compat = osc_compat
  @superusers = superusers || DEFAULT_SUPERUSERS
  clear
end

Instance Attribute Details

#creatorsObject (readonly)

Returns the value of attribute creators


24
25
26
# File 'lib/chef_zero/chef_data/default_creator.rb', line 24

def creators
  @creators
end

#dataObject (readonly)

Returns the value of attribute data


21
22
23
# File 'lib/chef_zero/chef_data/default_creator.rb', line 21

def data
  @data
end

#deleted(path) ⇒ Object (readonly)

Returns the value of attribute deleted


25
26
27
# File 'lib/chef_zero/chef_data/default_creator.rb', line 25

def deleted
  @deleted
end

#osc_compatObject (readonly)

Returns the value of attribute osc_compat


23
24
25
# File 'lib/chef_zero/chef_data/default_creator.rb', line 23

def osc_compat
  @osc_compat
end

#single_orgObject (readonly)

Returns the value of attribute single_org


22
23
24
# File 'lib/chef_zero/chef_data/default_creator.rb', line 22

def single_org
  @single_org
end

Instance Method Details

#clearObject


30
31
32
33
# File 'lib/chef_zero/chef_data/default_creator.rb', line 30

def clear
  @creators = { [] => @superusers }
  @deleted = {}
end

#created(path, creator, create_parents) ⇒ Object


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/chef_zero/chef_data/default_creator.rb', line 54

def created(path, creator, create_parents)
  # If a parent has been deleted, we will need to clear that.
  deleted_index = nil
  0.upto(path.size-1) do |index|
    deleted_index = index if @deleted[path[0..index]]
  end

  # Walk up the tree, setting the creator on anything that doesn't exist
  # (anything that is either deleted or was never created)
  while (deleted_index && path.size > deleted_index) || !@creators[path]
    @creators[path] = creator ? [ creator ] : []
    @deleted.delete(path)
    # Only do this once if create_parents is false
    break if !create_parents || path.size == 0

    path = path[0..-2]
  end
end

#deleted?(path) ⇒ Boolean


47
48
49
50
51
52
# File 'lib/chef_zero/chef_data/default_creator.rb', line 47

def deleted?(path)
  1.upto(path.size) do |index|
    return true if @deleted[path[0..-index]]
  end
  false
end

#exists?(path) ⇒ Boolean


141
142
143
144
145
# File 'lib/chef_zero/chef_data/default_creator.rb', line 141

def exists?(path)
  return true if path.size == 0
  parent_list = list(path[0..-2])
  parent_list && parent_list.include?(path[-1])
end

#get(path) ⇒ Object


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
# File 'lib/chef_zero/chef_data/default_creator.rb', line 77

def get(path)
  return nil if deleted?(path)

  result = case path[0]
  when 'acls'
    # /acls/*
    object_path = AclPath.get_object_path(path)
    if data_exists?(object_path)
      default_acl(path)
    end

  when 'containers'
    if path.size == 2 && exists?(path)
      {}
    end

  when 'users'
    if path.size == 2 && data.exists?(path)
      # User is empty user
      {}
    end

  when 'organizations'
    if path.size >= 2
      # /organizations/*/**
      if data.exists_dir?(path[0..1])
        get_org_default(path)
      end
    end
  end

  result
end

#list(path) ⇒ Object


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
# File 'lib/chef_zero/chef_data/default_creator.rb', line 111

def list(path)
  return nil if deleted?(path)

  if path.size == 0
    return %w(containers users organizations acls)
  end

  case path[0]
  when 'acls'
    if path.size == 1
      [ 'root' ] + (data.list(path + [ 'containers' ]) - [ 'organizations' ])
    else
      data.list(AclPath.get_object_path(path))
    end

  when 'containers'
    [ 'containers', 'users', 'organizations' ]

  when 'users'
    superusers

  when 'organizations'
    if path.size == 1
      single_org ? [ single_org ] : []
    elsif path.size >= 2 && data.exists_dir?(path[0..1])
      list_org_default(path)
    end
  end
end

#superusersObject


73
74
75
# File 'lib/chef_zero/chef_data/default_creator.rb', line 73

def superusers
  @creators[[]]
end