Class: Comodule::Deployment::Helper::Aws::Service

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/comodule/deployment/helper/aws.rb

Instance Method Summary collapse

Methods included from Base

included

Constructor Details

#initialize(platform) ⇒ Service

Returns a new instance of Service.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/comodule/deployment/helper/aws.rb', line 18

def initialize(platform)
  self.owner = platform

  @aws_sdk_object = {}
  @access_credentials = access_credentials || {}
  @method_map = {}
  ::AWS.constants.each do |const_name|
    const = ::AWS.const_get(const_name)
    if defined?(const.new)
      @method_map[const_name.to_s.underscore.to_sym] = const
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object

Raises:

  • (ArgumentError)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/comodule/deployment/helper/aws.rb', line 68

def method_missing(method_name)
  if @method_map[method_name]
    return @aws_sdk_object[method_name] if @aws_sdk_object[method_name]

    iam = {}

    key_id = access_key_id(method_name)
    secret = secret_access_key(method_name)
    region = region(method_name)
    iam[:access_key_id] = key_id if key_id
    iam[:secret_access_key] = secret if secret
    iam[:region] = region if region && region.present?

    iam = (@access_credentials[method_name] || @access_credentials[:common] || {}).to_hash.merge(iam)

    validate_credential(method_name, iam)
    if !iam.empty?
      @aws_sdk_object[method_name] = @method_map[method_name].new(iam)
    else
      @aws_sdk_object[method_name] = @method_map[method_name].new
    end

    return @aws_sdk_object[method_name]
  end

  raise ArgumentError, "#{self.class.name} was missing AWS class #{method_name}."
end

Instance Method Details

#access_credentialsObject



32
33
34
# File 'lib/comodule/deployment/helper/aws.rb', line 32

def access_credentials
  config.aws_access_credentials || {}
end

#access_key_id(aws_resource_name) ⇒ Object



43
44
45
# File 'lib/comodule/deployment/helper/aws.rb', line 43

def access_key_id(aws_resource_name)
  search_credential_directive(aws_resource_name, :access_key_id)
end

#region(aws_resource_name) ⇒ Object



51
52
53
# File 'lib/comodule/deployment/helper/aws.rb', line 51

def region(aws_resource_name)
  search_credential_directive(aws_resource_name, :region)
end

#search_credential_directive(aws_resource_name, directive) ⇒ Object



36
37
38
39
40
41
# File 'lib/comodule/deployment/helper/aws.rb', line 36

def search_credential_directive(aws_resource_name, directive)
  (access_credentials[aws_resource_name] && access_credentials[aws_resource_name][directive]) ||
  (access_credentials[:common] && access_credentials[:common][directive]) ||
  access_credentials[directive] ||
  config["aws_#{directive}".to_sym]
end

#secret_access_key(aws_resource_name) ⇒ Object



47
48
49
# File 'lib/comodule/deployment/helper/aws.rb', line 47

def secret_access_key(aws_resource_name)
  search_credential_directive(aws_resource_name, :secret_access_key)
end

#validate_credential(aws_resource_name, iam) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/comodule/deployment/helper/aws.rb', line 55

def validate_credential(aws_resource_name, iam)
  case aws_resource_name
  when :cloud_formation, :rds, :auto_scaling
    if !iam || !iam[:region]
      raise ArgumentError, "Please specify aws_access_credentials.#{aws_resource_name}.region on your config.yml."
    end
  when :cloud_front, :s3
    iam.delete(:region)
  end

  iam
end