Class: Cloudutil::AWS::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudutil/aws/core.rb

Overview

Class containing common methods and attributes for interfacing with AWS

Direct Known Subclasses

EC2

Constant Summary collapse

DEFAULT_REGION =
'us-east-1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Core

Initialize an instance of this class. Will accept options as a hash, or an array, with hash form being preferred. There are no required parameters for this method.

Array args must be in the order of: AWS region, AWS access key, AWS secret key.

Supported hash option keys are:

- config (an pre-built AWS::config object to use for API calls)
- region (the AWS region to make API calls to)
- access_key (the AWS access key to use for API authentication)
- secret_key (the AWS secret key to use for API authentication)

If no AWS::config object was passed, this class’s config method will automatically be called to create one



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cloudutil/aws/core.rb', line 27

def initialize(*args)
  if args.respond_to? :to_h
    _process_hash_init(args.to_h)
  elsif args[0].respond_to? :to_h
    _process_hash_init(args[0].to_h)
  else
    _process_array_init(args.flatten)
  end

  config(true) if @config.nil?
end

Instance Attribute Details

#access_keyObject

Getter for the ‘access_key’ attribute, will use values of the AWS_ACCESS_KEY_ID or AWS_ACCESS_KEY env vars, if not explicitly set



48
49
50
51
52
53
54
# File 'lib/cloudutil/aws/core.rb', line 48

def access_key
  if !@access_key || @access_key.length < 1
    @access_key = ENV['AWS_ACCESS_KEY_ID'] || ENV['AWS_ACCESS_KEY'] || nil
  end

  @access_key
end

#regionObject

Getter for the ‘region’ attribute, uses DEFAULT_REGION if not explicitly set



41
42
43
44
# File 'lib/cloudutil/aws/core.rb', line 41

def region
  @region = DEFAULT_REGION if !@region || @region.length < 1
  @region
end

#secret_keyObject

Getter for the ‘secret_key’ attribute, will use values of the AWS_SECRET_ACCESS_KEY or AWS_SECRET_KEY env vars, if not explicitly set



58
59
60
61
62
63
64
# File 'lib/cloudutil/aws/core.rb', line 58

def secret_key
  if !@secret_key || @secret_key.length < 1
    @secret_key = ENV['AWS_SECRET_ACCESS_KEY'] || ENV['AWS_SECRET_KEY'] || nil
  end

  @secret_key
end

Instance Method Details

#config(reconfig = false) ⇒ Object

Create (if not already existing) and return an AWS::config object. If creating a new AWS::config object, will make calls to this class’s region, access_key, and secret_key methods to populate the config data



69
70
71
72
73
74
75
# File 'lib/cloudutil/aws/core.rb', line 69

def config(reconfig = false)
  if !@config || reconfig
    @config = ::AWS.config(region: region, access_key_id: access_key, secret_access_key: secret_key)
  end

  @config
end