Class: ChefMetalFog::Providers::AWS::Credentials

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/chef_metal_fog/providers/aws/credentials.rb

Overview

Reads in a credentials file in Amazon’s download format and presents the credentials to you

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCredentials

Returns a new instance of Credentials.



9
10
11
# File 'lib/chef_metal_fog/providers/aws/credentials.rb', line 9

def initialize
  @credentials = {}
end

Class Method Details

.method_missing(name, *args, &block) ⇒ Object



73
74
75
# File 'lib/chef_metal_fog/providers/aws/credentials.rb', line 73

def self.method_missing(name, *args, &block)
  singleton.send(name, *args, &block)
end

.singletonObject



77
78
79
# File 'lib/chef_metal_fog/providers/aws/credentials.rb', line 77

def self.singleton
  @aws_credentials ||= Credentials.new
end

Instance Method Details

#[](name) ⇒ Object



26
27
28
# File 'lib/chef_metal_fog/providers/aws/credentials.rb', line 26

def [](name)
  @credentials[name]
end

#defaultObject



15
16
17
18
19
20
# File 'lib/chef_metal_fog/providers/aws/credentials.rb', line 15

def default
  if @credentials.size == 0
    raise "No credentials loaded!  Do you have a ~/.aws/config?"
  end
  @credentials[ENV['AWS_DEFAULT_PROFILE'] || 'default'] || @credentials.first[1]
end

#each(&block) ⇒ Object



30
31
32
# File 'lib/chef_metal_fog/providers/aws/credentials.rb', line 30

def each(&block)
  @credentials.each(&block)
end

#keysObject



22
23
24
# File 'lib/chef_metal_fog/providers/aws/credentials.rb', line 22

def keys
  @credentials.keys
end

#load_csv(credentials_csv_file) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/chef_metal_fog/providers/aws/credentials.rb', line 55

def load_csv(credentials_csv_file)
  CSV.new(File.open(credentials_csv_file), :headers => :first_row).each do |row|
    @credentials[row['User Name']] = {
      :name => row['User Name'],
      :user_name => row['User Name'],
      :aws_access_key_id => row['Access Key Id'],
      :aws_secret_access_key => row['Secret Access Key']
    }
  end
end

#load_defaultObject



66
67
68
69
70
71
# File 'lib/chef_metal_fog/providers/aws/credentials.rb', line 66

def load_default
  config_file = ENV['AWS_CONFIG_FILE'] || File.expand_path('~/.aws/config')
  if File.file?(config_file)
    load_ini(config_file)
  end
end

#load_ini(credentials_ini_file) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/chef_metal_fog/providers/aws/credentials.rb', line 34

def load_ini(credentials_ini_file)
  inifile = IniFile.load(File.expand_path(credentials_ini_file))
  if inifile
    inifile.each_section do |section|
      if section =~ /^\s*profile\s+(.+)$/ || section =~ /^\s*(default)\s*/
        profile_name = $1.strip
        profile = inifile[section].inject({}) do |result, pair|
          result[pair[0].to_sym] = pair[1]
          result
        end
        profile[:name] = profile_name
        @credentials[profile_name] = profile
      end
    end
  else
    # Get it to throw an error
    File.open(File.expand_path(credentials_ini_file)) do
    end
  end
end