Skip to content

Commit

Permalink
changing property name
Browse files Browse the repository at this point in the history
keep_for_days is removed instead use ami_min_days
  • Loading branch information
pmg1991 committed May 15, 2018
1 parent af9b56b commit 0bc35bd
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
6 changes: 3 additions & 3 deletions amicleaner/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(self, args):
self.from_ids = args.from_ids
self.full_report = args.full_report
self.force_delete = args.force_delete
self.keep_for_days = args.keep_for_days
self.ami_min_days = args.ami_min_days

self.mapping_strategy = {
"key": self.mapping_key,
Expand Down Expand Up @@ -87,7 +87,7 @@ def prepare_candidates(self, candidates_amis=None):
if not group_name:
report["no-tags (excluded)"] = amis
else:
reduced = c.reduce_candidates(amis, self.keep_previous, self.keep_for_days)
reduced = c.reduce_candidates(amis, self.keep_previous, self.ami_min_days)
if reduced:
report[group_name] = reduced
candidates.extend(reduced)
Expand Down Expand Up @@ -144,7 +144,7 @@ def print_defaults(self):
print(TERM.green("mapping_values : {0}".format(self.mapping_values)))
print(TERM.green("excluded_mapping_values : {0}".format(self.excluded_mapping_values)))
print(TERM.green("keep_previous : {0}".format(self.keep_previous)))
print(TERM.green("keep_for_days : {0}".format(self.keep_for_days)))
print(TERM.green("ami_min_days : {0}".format(self.ami_min_days)))

@staticmethod
def print_version():
Expand Down
6 changes: 3 additions & 3 deletions amicleaner/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def tags_values_to_string(tags, filters=None):

return ".".join(sorted(tag_values))

def reduce_candidates(self, mapped_candidates_ami, keep_previous=0, keep_for_days=-1):
def reduce_candidates(self, mapped_candidates_ami, keep_previous=0, ami_min_days=-1):

"""
Given a array of AMIs to clean this function return a subsequent
Expand All @@ -244,12 +244,12 @@ def reduce_candidates(self, mapped_candidates_ami, keep_previous=0, keep_for_day
result_amis = []
result_amis.extend(mapped_candidates_ami)

if keep_for_days > 0:
if ami_min_days > 0:
for ami in mapped_candidates_ami:
f_date = datetime.strptime(ami.creation_date, '%Y-%m-%dT%H:%M:%S.%fZ')
present = datetime.now()
delta = present - f_date
if delta.days < keep_for_days:
if delta.days < ami_min_days:
result_amis.remove(ami)

mapped_candidates_ami = result_amis
Expand Down
2 changes: 1 addition & 1 deletion amicleaner/resources/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@

# Number of days amis to keep based on creation date and grouping strategy
# not including the ami currently running by an ec2 instance
KEEP_FOR_DAYS = -1
AMI_MIN_DAYS = -1
8 changes: 4 additions & 4 deletions amicleaner/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from prettytable import PrettyTable

from .resources.config import KEEP_PREVIOUS, KEEP_FOR_DAYS
from .resources.config import KEEP_PREVIOUS, AMI_MIN_DAYS


class Printer(object):
Expand Down Expand Up @@ -112,10 +112,10 @@ def parse_args(args):
action="store_true",
help="Check and clean orphaned snapshots")

parser.add_argument("--keep-for-days",
dest='keep_for_days',
parser.add_argument("--ami-min-days",
dest='ami_min_days',
type=int,
default=KEEP_FOR_DAYS,
default=AMI_MIN_DAYS,
help="Number of days AMI to keep excluding those "
"currently being running")

Expand Down
12 changes: 6 additions & 6 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_deletion():

@mock_ec2
@mock_autoscaling
def test_deletion_keep_for_days():
def test_deletion_ami_min_days():
""" Test deletion methods """

# creating tests objects
Expand All @@ -90,7 +90,7 @@ def test_deletion_keep_for_days():
parser = parse_args(
[
'--keep-previous', '0',
'--keep-for-days', '1',
'--ami-min-days', '1',
'--mapping-key', 'name',
'--mapping-values', 'test-ami']
)
Expand All @@ -106,7 +106,7 @@ def test_deletion_keep_for_days():
parser = parse_args(
[
'--keep-previous', '0',
'--keep-for-days', '10000',
'--ami-min-days', '10000',
'--mapping-key', 'name',
'--mapping-values', 'test-ami']
)
Expand Down Expand Up @@ -165,7 +165,7 @@ def test_parse_args_no_args():
assert parser.mapping_key is None
assert parser.mapping_values is None
assert parser.keep_previous is 4
assert parser.keep_for_days is -1
assert parser.ami_min_days is -1


def test_parse_args():
Expand All @@ -181,8 +181,8 @@ def test_parse_args():
assert parser.mapping_key == "tags"
assert len(parser.mapping_values) == 2

parser = parse_args(['--keep-for-days', '10', '--full-report'])
assert parser.keep_for_days == 10
parser = parse_args(['--ami-min-days', '10', '--full-report'])
assert parser.ami_min_days == 10
assert parser.full_report is True


Expand Down

0 comments on commit 0bc35bd

Please sign in to comment.