Skip to content

Commit

Permalink
Ensure we can update indexes when only a one of read/write capacity i…
Browse files Browse the repository at this point in the history
…s updated.
  • Loading branch information
tremble committed Oct 9, 2021
1 parent 9b68256 commit 5603d4e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
32 changes: 19 additions & 13 deletions plugins/modules/dynamodb_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ def _decode_index(index_data, attributes, type_prefix=''):
index_map.update(_decode_primary_index(index_data))

throughput = index_data.get('provisioned_throughput', {})
index_map['provisioned_throughput'] = throughput
if throughput:
index_map['read_capacity'] = throughput.get('read_capacity_units')
index_map['write_capacity'] = throughput.get('write_capacity_units')
Expand Down Expand Up @@ -379,9 +380,13 @@ def compatability_results(current_table):
global_indexes = current_table.get('_global_index_map', {})
local_indexes = current_table.get('_local_index_map', {})
for index in global_indexes:
indexes.append(global_indexes[index])
idx = dict(global_indexes[index])
idx.pop('provisioned_throughput', None)
indexes.append(idx)
for index in local_indexes:
indexes.append(local_indexes[index])
idx = dict(local_indexes[index])
idx.pop('provisioned_throughput', None)
indexes.append(idx)

compat_results = dict(
hash_key_name=hash_key_name,
Expand Down Expand Up @@ -543,13 +548,16 @@ def _primary_index_changes(current_table):
return changed


def _throughput_changes(current_table):
def _throughput_changes(current_table, params=None):

if not params:
params = module.params

throughput = current_table.get('provisioned_throughput', {})
read_capacity = throughput.get('read_capacity_units', None)
_read_capacity = module.params.get('read_capacity') or read_capacity
_read_capacity = params.get('read_capacity') or read_capacity
write_capacity = throughput.get('write_capacity_units', None)
_write_capacity = module.params.get('write_capacity') or write_capacity
_write_capacity = params.get('write_capacity') or write_capacity

if (read_capacity != _read_capacity) or (write_capacity != _write_capacity):
return dict(
Expand Down Expand Up @@ -679,11 +687,7 @@ def _global_index_changes(current_table):
# rather than dropping other changes on the floor
_current = current_global_index_map[name]
_new = global_index_map[name]
change = dict()
if _new['read_capacity'] != _current['read_capacity']:
change['ReadCapacityUnits'] = _new['read_capacity']
if _new['write_capacity'] != _current['write_capacity']:
change['WriteCapacityUnits'] = _new['write_capacity']
change = dict(_throughput_changes(_current, _new))
if change:
update = dict(
IndexName=name,
Expand Down Expand Up @@ -746,9 +750,8 @@ def _update_table(current_table):
try:
_update_table_with_long_retry(GlobalSecondaryIndexUpdates=[index], AttributeDefinitions=changes['AttributeDefinitions'])
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="Failed to update table",
changes=changes,
additional_global_index_changes=additional_global_index_changes)
module.fail_json_aws(e, msg="Failed to update table", changes=changes,
additional_global_index_changes=additional_global_index_changes)

if module.params.get('wait'):
wait_exists()
Expand Down Expand Up @@ -957,6 +960,9 @@ def main():

results['changed'] = changed
if table:
# These are used to pass computed data about, not needed for users
table.pop('_global_index_map', None)
table.pop('_local_index_map', None)
results['table'] = table

module.exit_json(**results)
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/targets/dynamodb_table/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ index_updated:
type: global_include
read_capacity: 3
write_capacity: 3
- name: AnotherIndex
type: global_all
read_capacity: 4


tags_default:
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/targets/dynamodb_table/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,10 @@
- update_index.write_capacity == 3
- update_index.tags == tags_default

- name: Pause to allow index to finish updating
pause:
seconds: 20

- name: Update table update index - idempotent - check_mode
dynamodb_table:
state: present
Expand Down

0 comments on commit 5603d4e

Please sign in to comment.