class Chef::Resource::RemoteFile

Public Class Methods

new(name, run_context = nil) click to toggle source
Calls superclass method Chef::Resource.new
# File lib/chef/resource/remote_file.rb, line 34
def initialize(name, run_context = nil)
  super
  @source = []
end

Public Instance Methods

after_created() click to toggle source
# File lib/chef/resource/remote_file.rb, line 103
def after_created
  validate_identity_platform(remote_user, remote_password, remote_domain)
  identity = qualify_user(remote_user, remote_password, remote_domain)
  remote_domain(identity[:domain])
  remote_user(identity[:user])
end
parse_source_args(args) click to toggle source
# File lib/chef/resource/remote_file.rb, line 61
def parse_source_args(args)
  if args.empty?
    nil
  elsif args[0].is_a?(Chef::DelayedEvaluator) && args.count == 1
    args[0]
  elsif args.any? { |a| a.is_a?(Chef::DelayedEvaluator) } && args.count > 1
    raise Exceptions::InvalidRemoteFileURI, "Only 1 source argument allowed when using a lazy evaluator"
  else
    Array(args).flatten
  end
end
qualify_user(specified_user, password = nil, specified_domain = nil) click to toggle source
# File lib/chef/resource/remote_file.rb, line 118
def qualify_user(specified_user, password = nil, specified_domain = nil)
  domain = specified_domain
  user = specified_user

  if specified_user.nil? && ! specified_domain.nil?
    raise ArgumentError, "The domain `#{specified_domain}` was specified, but no user name was given"
  end

  # if domain is provided in both username and domain
  if specified_user && ((specified_user.include? '\\') || (specified_user.include? "@")) && specified_domain
    raise ArgumentError, "The domain is provided twice. Username: `#{specified_user}`, Domain: `#{specified_domain}`. Please specify domain only once."
  end

  if ! specified_user.nil? && specified_domain.nil?
    # Splitting username of format: Domain\Username
    domain_and_user = user.split('\\')

    if domain_and_user.length == 2
      domain = domain_and_user[0]
      user = domain_and_user[1]
    elsif domain_and_user.length == 1
      # Splitting username of format: Username@Domain
      domain_and_user = user.split("@")
      if domain_and_user.length == 2
        domain = domain_and_user[1]
        user = domain_and_user[0]
      elsif domain_and_user.length != 1
        raise ArgumentError, "The specified user name `#{user}` is not a syntactically valid user name"
      end
    end
  end

  if ( password || domain ) && user.nil?
    raise ArgumentError, "A value for `password` or `domain` was specified without specification of a value for `user`"
  end

  { domain: domain, user: user }
end
source(*args) click to toggle source

source can take any of the following as arguments

  • A single string argument

  • Multiple string arguments

  • An array or strings

  • A delayed evaluator that evaluates to a string or array of strings

All strings must be parsable as URIs. source returns an array of strings.

# File lib/chef/resource/remote_file.rb, line 47
def source(*args)
  arg = parse_source_args(args)
  ret = set_or_return(:source,
                      arg,
                      { :callbacks => {
                          :validate_source => method(:validate_source),
                        } })
  if ret.is_a? String
    Array(ret)
  else
    ret
  end
end
use_conditional_get(true_or_false) click to toggle source

Disable or enable ETag and Last Modified conditional GET. Equivalent to

use_etag(true_or_false)
use_last_modified(true_or_false)
# File lib/chef/resource/remote_file.rb, line 78
def use_conditional_get(true_or_false)
  use_etag(true_or_false)
  use_last_modified(true_or_false)
end
validate_identity_platform(specified_user, password = nil, specified_domain = nil) click to toggle source
# File lib/chef/resource/remote_file.rb, line 110
def validate_identity_platform(specified_user, password = nil, specified_domain = nil)
  if node[:platform_family] == "windows"
    if specified_user && password.nil?
      raise ArgumentError, "A value for `remote_password` must be specified when a value for `user` is specified on the Windows platform"
    end
  end
end

Private Instance Methods

absolute_uri?(source) click to toggle source
# File lib/chef/resource/remote_file.rb, line 173
def absolute_uri?(source)
  Chef::Provider::RemoteFile::Fetcher.network_share?(source) || (source.kind_of?(String) && as_uri(source).absolute?)
rescue URI::InvalidURIError
  false
end
validate_source(source) click to toggle source
# File lib/chef/resource/remote_file.rb, line 161
def validate_source(source)
  source = Array(source).flatten
  raise ArgumentError, "#{resource_name} has an empty source" if source.empty?
  source.each do |src|
    unless absolute_uri?(src)
      raise Exceptions::InvalidRemoteFileURI,
        "#{src.inspect} is not a valid `source` parameter for #{resource_name}. `source` must be an absolute URI or an array of URIs."
    end
  end
  true
end