Class: Awscli::DynamoDB::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/awscli/dynamo.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Table

Returns a new instance of Table.



4
5
6
# File 'lib/awscli/dynamo.rb', line 4

def initialize(connection)
  @conn = connection
end

Instance Method Details

#create(options) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/awscli/dynamo.rb', line 22

def create(options)
  key_schema, provisioned_throughput = {}, {}
  abort 'Invalid key type' unless %w(N NS S SS).include?(options[:pk_type])
  key_schema['HashKeyElement'] = {
    'AttributeName' => options[:pk_name],
    'AttributeType' => options[:pk_type]
  }
  if options[:rk_name]
    abort '--rk_type is required if --rk-name is passed' unless options[:rk_name]
    abort 'Invalid key type' unless %w(N NS S SS).include?(options[:rk_type])
    key_schema['RangeKeyElement'] = {
      'AttributeName' => options[:rk_name],
      'AttributeType' => options[:rk_type]
    }
  end
  provisioned_throughput['ReadCapacityUnits'] = options[:read_capacity]
  provisioned_throughput['WriteCapacityUnits'] = options[:write_capacity]
  @conn.create_table(options[:name], key_schema, provisioned_throughput)
rescue Excon::Errors::BadRequest
  puts "[Error] Failed to create table. #{parse_excon_error_response($!)}"
else
  puts "Create table #{options[:name]} successfully."
end

#delete(table_name) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/awscli/dynamo.rb', line 52

def delete(table_name)
  @conn.delete_table(table_name)
rescue Excon::Errors::BadRequest
  puts "[Error] Failed to delete table. #{parse_excon_error_response($!)}"
else
  puts "Delete table #{table_name} successfully."
end

#describe(table_name) ⇒ Object



46
47
48
49
50
# File 'lib/awscli/dynamo.rb', line 46

def describe(table_name)
  puts @conn.describe_table(table_name).body.to_yaml
rescue Excon::Errors::BadRequest
  puts 'Table not found'
end

#list(options) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/awscli/dynamo.rb', line 8

def list(options)
  opts = {}
  table_data = []
  opts['Limit'] = options[:limit] if options[:limit]
  @conn.list_tables(opts).body['TableNames'].each do |table|
    table_data << { :table => table }
  end
  if table_data.empty?
    puts 'No tables found'
  else
    Formatador.display_table(table_data)
  end
end

#update(options) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/awscli/dynamo.rb', line 60

def update(options)
  provisioned_throughput = {}
  provisioned_throughput['ReadCapacityUnits'] = options[:read_capacity]
  provisioned_throughput['WriteCapacityUnits'] = options[:write_capacity]
  @conn.update_table(options[:name], provisioned_throughput)
rescue Excon::Errors::BadRequest
  puts "[Error] Failed to update table. #{parse_excon_error_response($!)}"
else
  puts "Table #{options[:name]} provisioned capacity updated successfully."
end