Class: Chef::ChefFS::FileSystem::RestListEntry

Inherits:
BaseFSObject show all
Defined in:
lib/chef/chef_fs/file_system/rest_list_entry.rb

Direct Known Subclasses

DataBagItem

Instance Attribute Summary

Attributes inherited from BaseFSObject

#name, #parent, #path

Instance Method Summary collapse

Methods inherited from BaseFSObject

#can_have_child?, #child, #dir?, #path_for_printing, #root

Constructor Details

#initialize(name, parent, exists = nil) ⇒ RestListEntry

Returns a new instance of RestListEntry.



28
29
30
31
# File 'lib/chef/chef_fs/file_system/rest_list_entry.rb', line 28

def initialize(name, parent, exists = nil)
  super(name, parent)
  @exists = exists
end

Instance Method Details

#api_pathObject



33
34
35
36
37
38
39
# File 'lib/chef/chef_fs/file_system/rest_list_entry.rb', line 33

def api_path
  if name.length < 5 || name[-5,5] != ".json"
    raise "Invalid name #{path}: must end in .json"
  end
  api_child_name = name[0,name.length-5]
  "#{parent.api_path}/#{api_child_name}"
end

#chef_objectObject



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/chef/chef_fs/file_system/rest_list_entry.rb', line 72

def chef_object
  begin
    # REST will inflate the Chef object using json_class
    rest.get_rest(api_path)
  rescue Net::HTTPServerException
    if $!.response.code == "404"
      raise Chef::ChefFS::FileSystem::NotFoundError.new($!), "#{path_for_printing} not found"
    else
      raise
    end
  end
end

#compare_to(other) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/chef/chef_fs/file_system/rest_list_entry.rb', line 85

def compare_to(other)
  begin
    other_value = other.read
  rescue Chef::ChefFS::FileSystem::NotFoundError
    return [ nil, nil, :none ]
  end
  begin
    value = chef_object.to_hash
  rescue Chef::ChefFS::FileSystem::NotFoundError
    return [ false, :none, other_value ]
  end
  are_same = (value == Chef::JSONCompat.from_json(other_value, :create_additions => false))
  [ are_same, Chef::JSONCompat.to_json_pretty(value), other_value ]
end

#delete(recurse) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/chef/chef_fs/file_system/rest_list_entry.rb', line 56

def delete(recurse)
  begin
    rest.delete_rest(api_path)
  rescue Net::HTTPServerException
    if $!.response.code == "404"
      raise Chef::ChefFS::FileSystem::NotFoundError.new($!), "#{path_for_printing} not found"
    else
      raise
    end
  end
end

#environmentObject



41
42
43
# File 'lib/chef/chef_fs/file_system/rest_list_entry.rb', line 41

def environment
  parent.environment
end

#exists?Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
# File 'lib/chef/chef_fs/file_system/rest_list_entry.rb', line 45

def exists?
  if @exists.nil?
    begin
      @exists = parent.children.any? { |child| child.name == name }
    rescue Chef::ChefFS::FileSystem::NotFoundError
      @exists = false
    end
  end
  @exists
end

#readObject



68
69
70
# File 'lib/chef/chef_fs/file_system/rest_list_entry.rb', line 68

def read
  Chef::JSONCompat.to_json_pretty(chef_object.to_hash)
end

#restObject



100
101
102
# File 'lib/chef/chef_fs/file_system/rest_list_entry.rb', line 100

def rest
  parent.rest
end

#write(file_contents) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/chef/chef_fs/file_system/rest_list_entry.rb', line 104

def write(file_contents)
  json = Chef::JSONCompat.from_json(file_contents).to_hash
  base_name = name[0,name.length-5]
  if json['name'] != base_name
    raise "Name in #{path_for_printing}/#{name} must be '#{base_name}' (is '#{json['name']}')"
  end
  begin
    rest.put_rest(api_path, json)
  rescue Net::HTTPServerException
    if $!.response.code == "404"
      raise Chef::ChefFS::FileSystem::NotFoundError.new($!), "#{path_for_printing} not found"
    else
      raise
    end
  end
end