Class: Kitchen::Driver::Aws::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/kitchen/driver/aws/client.rb

Overview

A class for creating and managing the EC2 client connection

Author:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(region, profile_name = nil, access_key_id = nil, secret_access_key = nil, session_token = nil, http_proxy = nil) ⇒ Client

rubocop:disable Metrics/ParameterLists



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/kitchen/driver/aws/client.rb', line 35

def initialize( # rubocop:disable Metrics/ParameterLists
  region,
  profile_name = nil,
  access_key_id = nil,
  secret_access_key = nil,
  session_token = nil,
  http_proxy = nil
)
  creds = self.class.get_credentials(
    profile_name, access_key_id, secret_access_key, session_token
  )
  ::Aws.config.update(
    :region => region,
    :credentials => creds,
    :http_proxy => http_proxy
  )
end

Class Method Details

.get_credentials(profile_name, access_key_id, secret_access_key, session_token) ⇒ Object

Try and get the credentials from an ordered list of locations http://docs.aws.amazon.com/sdkforruby/api/index.html#Configuration rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/kitchen/driver/aws/client.rb', line 56

def self.get_credentials(profile_name, access_key_id, secret_access_key, session_token)
  shared_creds = ::Aws::SharedCredentials.new(:profile_name => profile_name)
  if access_key_id && secret_access_key
    ::Aws::Credentials.new(access_key_id, secret_access_key, session_token)
  # TODO: these are deprecated, remove them in the next major version
  elsif ENV["AWS_ACCESS_KEY"] && ENV["AWS_SECRET_KEY"]
    ::Aws::Credentials.new(
      ENV["AWS_ACCESS_KEY"],
      ENV["AWS_SECRET_KEY"],
      ENV["AWS_TOKEN"]
    )
  elsif ENV["AWS_ACCESS_KEY_ID"] && ENV["AWS_SECRET_ACCESS_KEY"]
    ::Aws::Credentials.new(
      ENV["AWS_ACCESS_KEY_ID"],
      ENV["AWS_SECRET_ACCESS_KEY"],
      ENV["AWS_SESSION_TOKEN"]
    )
  elsif shared_creds.loadable?
    shared_creds
  else
    ::Aws::InstanceProfileCredentials.new(:retries => 1)
  end
end

Instance Method Details

#clientObject



98
99
100
# File 'lib/kitchen/driver/aws/client.rb', line 98

def client
  @client ||= ::Aws::EC2::Client.new
end

#create_instance(options) ⇒ Object

rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



81
82
83
# File 'lib/kitchen/driver/aws/client.rb', line 81

def create_instance(options)
  resource.create_instances(options)[0]
end

#get_instance(id) ⇒ Object



85
86
87
# File 'lib/kitchen/driver/aws/client.rb', line 85

def get_instance(id)
  resource.instance(id)
end

#get_instance_from_spot_request(request_id) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/kitchen/driver/aws/client.rb', line 89

def get_instance_from_spot_request(request_id)
  resource.instances(
    :filters => [{
      :name => "spot-instance-request-id",
      :values => [request_id]
    }]
  ).to_a[0]
end

#resourceObject



102
103
104
# File 'lib/kitchen/driver/aws/client.rb', line 102

def resource
  @resource ||= ::Aws::EC2::Resource.new
end