Sha256: f013dcf1b4932431a6d57733447d57cb2ac47003676f500dfbde9d9dc158231f

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 KB

Contents

require 'chef_fs/file_system/base_fs_object'
require 'chef_fs/file_system/not_found_error'
require 'chef/role'
require 'chef/node'

module ChefFS
  module FileSystem
    class RestListEntry < BaseFSObject
      def initialize(name, parent, exists = nil)
        super(name, parent)
        @exists = exists
      end

      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

      def environment
        parent.environment
      end

      def exists?
        if @exists.nil?
          @exists = parent.children.any? { |child| child.name == name }
        end
        @exists
      end

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

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

      def rest
        parent.rest
      end

      def content_type
        :json
      end

      def write(contents)
        rest.put_rest(api_path, contents)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
knife-essentials-0.4 lib/chef_fs/file_system/rest_list_entry.rb
knife-essentials-0.3.1 lib/chef_fs/file_system/rest_list_entry.rb