Class: Cloudpatrol::Task::DynamoDB

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudpatrol/task/dynamo_db.rb

Instance Method Summary collapse

Constructor Details

#initialize(cred, table_name) ⇒ DynamoDB

Returns a new instance of DynamoDB.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/cloudpatrol/task/dynamo_db.rb', line 7

def initialize cred, table_name
  @gate = ::AWS::DynamoDB.new(cred)
  @table = @gate.tables[table_name]

  unless @table.exists?
    puts "Creating DynamoDB table \"#{table_name}\", wait a while..."
    @table = @gate.tables.create(table_name, 1, 1)
    sleep 1 while @table.status == :creating
    puts "Table created"
  end
rescue
  @table = nil
end

Instance Method Details

#log(action, response) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cloudpatrol/task/dynamo_db.rb', line 21

def log action, response
  raise unless @table

  if @table.status == :active
    item = @table.items.create(
      id: SecureRandom.uuid,
      action: action.to_s,
      response: response.to_s,
      time: Time.now.to_s
      )
  else
    nil
  end
rescue
  false
end