Class: Kitchen::Driver::Aws::CfClient

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

Overview

A class for creating and managing the EC2 client connection

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, retry_limit = nil, ssl_verify_peer = true) ⇒ CfClient

rubocop:disable Metrics/ParameterLists



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/kitchen/driver/aws/cf_client.rb', line 27

def initialize( # rubocop:disable Metrics/ParameterLists

  region,
  profile_name = nil,
  access_key_id = nil,
  secret_access_key = nil,
  session_token = nil,
  http_proxy = nil,
  retry_limit = nil,
  ssl_verify_peer = true
)
  creds = self.class.get_credentials(
    profile_name, access_key_id, secret_access_key, session_token, region
  )
  ::Aws.config.update(
    region: region,
    credentials: creds,
    http_proxy: http_proxy,
    ssl_verify_peer: ssl_verify_peer
  )
  ::Aws.config.update(retry_limit: retry_limit) unless retry_limit.nil?
end

Class Method Details

.default_shared_credentials?Boolean

rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

Returns:

  • (Boolean)


87
88
89
90
91
# File 'lib/kitchen/driver/aws/cf_client.rb', line 87

def self.default_shared_credentials?
  ::Aws::SharedCredentials.new.loadable?
rescue ::Aws::Errors::NoSuchProfileError
  false
end

.get_credentials(profile_name, access_key_id, secret_access_key, session_token, region, options = {}) ⇒ Object

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/kitchen/driver/aws/cf_client.rb', line 52

def self.get_credentials(profile_name, access_key_id, secret_access_key, session_token,
                         region, options = {})
  source_creds =
    if access_key_id && secret_access_key
      ::Aws::Credentials.new(access_key_id, secret_access_key, session_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 profile_name
      ::Aws::SharedCredentials.new(profile_name: profile_name)
    elsif default_shared_credentials?
      ::Aws::SharedCredentials.new
    else
      ::Aws::InstanceProfileCredentials.new(retries: 1)
    end

  if options[:assume_role_arn] && options[:assume_role_session_name]
    sts = ::Aws::STS::Client.new(credentials: source_creds, region: region)

    assume_role_options = (options[:assume_role_options] || {}).merge(
      client: sts,
      role_arn: options[:assume_role_arn],
      role_session_name: options[:assume_role_session_name]
    )

    ::Aws::AssumeRoleCredentials.new(assume_role_options)
  else
    source_creds
  end
end

Instance Method Details

#clientObject



110
111
112
# File 'lib/kitchen/driver/aws/cf_client.rb', line 110

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

#create_stack(options) ⇒ Object



93
94
95
# File 'lib/kitchen/driver/aws/cf_client.rb', line 93

def create_stack(options)
  resource.create_stack(options)
end

#delete_stack(stack_name) ⇒ Object



105
106
107
108
# File 'lib/kitchen/driver/aws/cf_client.rb', line 105

def delete_stack(stack_name)
  s = resource.stack(stack_name)
  s.delete
end

#get_stack(stack_name) ⇒ Object



97
98
99
# File 'lib/kitchen/driver/aws/cf_client.rb', line 97

def get_stack(stack_name)
  resource.stack(stack_name)
end

#get_stack_events(stack_name) ⇒ Object



101
102
103
# File 'lib/kitchen/driver/aws/cf_client.rb', line 101

def get_stack_events(stack_name)
  client.describe_stack_events(stack_name: stack_name)
end

#resourceObject



114
115
116
# File 'lib/kitchen/driver/aws/cf_client.rb', line 114

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