Module: Smash::CloudPowers::AwsResources

Includes:
Auth, Helper, Zenv
Included in:
Smash::CloudPowers, Delegator, SelfAwareness, Storage, Synapse::Broadcast, Synapse::Pipe, Synapse::Queue, Synapse::Queue::Board
Defined in:
lib/cloud_powers/aws_resources.rb

Instance Method Summary collapse

Methods included from Zenv

#env_vars, #file_tree_search, #i_vars, #project_root, #project_root=, #system_vars, #zfind

Methods included from Helper

#attr_map!, #available_resources, #called_from, #create_logger, #deep_modify_keys_with, #format_error_message, #log_file, #logger, #modify_keys_with, #smart_retry, #task_path, #task_require_path, #to_camel, #to_hyph, #to_i_var, #to_pascal, #to_ruby_file_name, #to_snake, #update_message_body, #valid_json?, #valid_url?

Methods included from Auth

creds, region

Instance Method Details

#ec2(opts = {}) ⇒ Object

Get or create an EC2 client and cache that client so that a Context is more well tied together

Parameters

  • opts Hash (optional)
    • stub_responses - defaulted to false but it can be overriden with the desired responses for local testing
    • region - defaulted to use the #region() method
    • AWS::Credentials object, which will also scour the context and environment for your keys

Returns AWS::EC2::Client

Example images = ec2.describe_images images.first

=> 'asdf'



35
36
37
38
39
40
41
42
43
44
# File 'lib/cloud_powers/aws_resources.rb', line 35

def ec2(opts = {})
  config = {
    stub_responses: false,
    region: region,
    credentials: Auth.creds
  }
  config = config.merge(opts.select { |k| config.key?(k) })

  @ec2 ||= Aws::EC2::Client.new(config)
end

#image(name, opts = {}) ⇒ Object

Get an image using a name and filters functionality from EC2. The name is required but the filter defaults to search for the tag with the key aminame because this is the key that most Nodes will search for, when they gather an AMI to start with.

Parameters

  • opts [Hash]
    • stub_responses: defaulted to false but it can be overriden with the desired responses for local testing
    • region: defaulted to use the #region() method
    • AWS::Credentials object, which will also scour the context and environment for your keys

Returns Aws::EC2::Image



58
59
60
61
62
63
64
65
# File 'lib/cloud_powers/aws_resources.rb', line 58

def image(name, opts = {})
  config = {
    filters: [{ name: 'tag:aminame', values: [name.to_s] }]
  }
  config = config.merge(opts.select { |k| config.key?(k) })

  ec2(opts).describe_images(config).images.first
end

#kinesis(opts = {}) ⇒ Object

Get or create an Kinesis client and cache that client so that a Context is more well tied together

Parameters

  • opts Hash
    • stub_responses: defaulted to false but it can be overriden with the desired responses for local testing
    • region: defaulted to use the #region() method
    • AWS::Credentials object, which will also scour the context and environment for your keys

Returns AWS::Kinesis client

Example pipe_to('somePipe') { update_body(status: 'waHoo') } # uses Aws::Kinesis::Client.put_recor()

=> sequence_number: '1676151970'



81
82
83
84
85
86
87
88
89
90
# File 'lib/cloud_powers/aws_resources.rb', line 81

def kinesis(opts = {})
  config = {
    stub_responses: false,
    region: region,
    credentials: Auth.creds
  }
  config = config.merge(opts.select { |k| config.key?(k) })

  @kinesis ||= Aws::Kinesis::Client.new(config)
end

#region ⇒ Object

Get the region from the environment/context or use a default region for AWS API calls.

Returns String



16
17
18
# File 'lib/cloud_powers/aws_resources.rb', line 16

def region
  zfind(:aws_region) || 'us-west-2'
end

#s3(opts = {}) ⇒ Object

Get or create an S3 client and cache that client so that a Context is more well tied together

Parameters

  • opts Hash
    • stub_responses: defaulted to false but it can be overriden with the desired responses for local testing
    • region: defaulted to use the #region() method
    • AWS::Credentials object, which will also scour the context and environment for your keys

Returns AWS::S3 client

Example expect(s3.head_bucket('exampleBucket')).to be_empty

passing test



106
107
108
109
110
111
112
113
114
115
# File 'lib/cloud_powers/aws_resources.rb', line 106

def s3(opts = {})
  config = {
    stub_responses: false,
    region: region,
    credentials: Auth.creds
  }
  config = config.merge(opts.select { |k| config.key?(k) })

  @s3 ||= Aws::S3::Client.new(config)
end

#sns(opts = {}) ⇒ Object

Get or create an SNS client and cache that client so that a Context is more well tied together Parameters

  • opts Hash
    • stub_responses: defaulted to false but it can be overriden with the desired responses for local testing
    • region: defaulted to use the #region() method
    • AWS::Credentials object, which will also scour the context and environment for your keys

Returns AWS::SNS client

Example create_channel!('testBroadcast') # uses Aws::SNS::Client

=> true



130
131
132
133
134
135
136
137
138
139
# File 'lib/cloud_powers/aws_resources.rb', line 130

def sns(opts = {})
  config = {
    stub_responses: false,
    region: region,
    credentials: Auth.creds
  }
  config = config.merge(opts.select { |k| config.key?(k) })

  @sns ||= Aws::SNS::Client.new(config)
end

#sqs(opts = {}) ⇒ Object

Get or create an SQS client and cache that client so that a Context is more well tied together

Parameters

  • opts Hash
    • stub_responses: defaulted to false but it can be overriden with the desired responses for local testing
    • region: defaulted to use the #region() method
    • AWS::Credentials object, which will also scour the context and environment for your keys

Returns AWS::SQS client

Example create_queue('someQueue') # Uses Aws::SQS::Client



154
155
156
157
158
159
160
161
162
# File 'lib/cloud_powers/aws_resources.rb', line 154

def sqs(opts = {})
  config = {
    stub_responses: false,
    credentials: Auth.creds
  }
  config = config.merge(opts.select { |k| config.key?(k) })

  @sqs ||= Aws::SQS::Client.new(config)
end