def test_ec2_mark_zero(self): localtz = zoneinfo.gettz("America/New_York") dt = datetime.now(localtz) dt = dt.replace(year=2017, month=11, day=24, hour=7, minute=00) session_factory = self.replay_flight_data("test_ec2_mark_zero") session = session_factory(region="us-east-1") ec2 = session.client("ec2") resource = ec2.describe_instances(InstanceIds=["i-04d3e0630bd342566"])[ "Reservations" ][ 0 ][ "Instances" ][ 0 ] tags = [t["Value"] for t in resource["Tags"] if t["Key"] == "maid_status"] self.assertEqual(len(tags), 0) policy = self.load_policy( { "name": "ec2-mark-zero-days", "resource": "ec2", "filters": [{"tag:CreatorName": "joshuaroot"}], "actions": [{"type": "mark-for-op", "days": 0, "op": "terminate"}], }, session_factory=session_factory, ) resources = policy.run() self.assertEqual(len(resources), 1) self.assertEqual(resources[0]["InstanceId"], "i-04d3e0630bd342566") resource = ec2.describe_instances(InstanceIds=["i-04d3e0630bd342566"])[ "Reservations" ][ 0 ][ "Instances" ][ 0 ] tags = [t["Value"] for t in resource["Tags"] if t["Key"] == "maid_status"] result = datetime.strptime( tags[0].strip().split("@", 1)[-1], "%Y/%m/%d" ).replace( tzinfo=localtz ) self.assertEqual(result.date(), dt.date())
def test_ec2_mark_zero(self): localtz = zoneinfo.gettz("America/New_York") dt = datetime.now(localtz) dt = dt.replace(year=2017, month=11, day=24, hour=7, minute=00) session_factory = self.replay_flight_data("test_ec2_mark_zero") session = session_factory(region="us-east-1") ec2 = session.client("ec2") resource = ec2.describe_instances(InstanceIds=["i-04d3e0630bd342566"])[ "Reservations" ][ 0 ][ "Instances" ][ 0 ] tags = [t["Value"] for t in resource["Tags"] if t["Key"] == "maid_status"] self.assertEqual(len(tags), 0) policy = self.load_policy( { "name": "ec2-mark-zero-days", "resource": "ec2", "filters": [{"tag:CreatorName": "joshuaroot"}], "actions": [{"type": "mark-for-op", "days": 0, "op": "terminate"}], }, session_factory=session_factory, ) resources = policy.run() self.assertEqual(len(resources), 1) self.assertEqual(resources[0]["InstanceId"], "i-04d3e0630bd342566") resource = ec2.describe_instances(InstanceIds=["i-04d3e0630bd342566"])[ "Reservations" ][ 0 ][ "Instances" ][ 0 ] tags = [t["Value"] for t in resource["Tags"] if t["Key"] == "maid_status"] result = datetime.strptime( tags[0].strip().split("@", 1)[-1], "%Y/%m/%d" ).replace( tzinfo=localtz ) self.assertEqual(result.date(), dt.date())
def test_ec2_set_instance_profile_assocation(self): session_factory = self.replay_flight_data( 'test_ec2_set_instance_profile_association') policy = self.load_policy({ 'name': 'ec2-test-set-instance-profile-association', 'resource': 'ec2', 'filters': [ {'tag:Name': 'MissingInstanceProfile'}, {'IamInstanceProfile': 'absent'}], 'actions': [ {'type': 'set-instance-profile', 'name': 'ec2-default'}]}, session_factory=session_factory) resources = policy.run() self.assertGreaterEqual(len(resources), 1) ec2 = session_factory().client('ec2') resources = ec2.describe_instances( InstanceIds=[r['InstanceId'] for r in resources] ) for r in resources['Reservations']: for i in r['Instances']: self.assertIn('IamInstanceProfile', i) self.assertIn('Arn', i['IamInstanceProfile']) self.assertIn(':instance-profile/ec2-default', i['IamInstanceProfile']['Arn'])
def test_ec2_set_instance_profile_assocation(self): session_factory = self.replay_flight_data( "test_ec2_set_instance_profile_association" ) policy = self.load_policy( { "name": "ec2-test-set-instance-profile-association", "resource": "ec2", "filters": [ {"tag:Name": "MissingInstanceProfile"}, {"IamInstanceProfile": "absent"}, ], "actions": [{"type": "set-instance-profile", "name": "ec2-default"}], }, session_factory=session_factory, ) resources = policy.run() self.assertGreaterEqual(len(resources), 1) ec2 = session_factory().client("ec2") resources = ec2.describe_instances( InstanceIds=[r["InstanceId"] for r in resources] ) for r in resources["Reservations"]: for i in r["Instances"]: self.assertIn("IamInstanceProfile", i) self.assertIn("Arn", i["IamInstanceProfile"]) self.assertIn( ":instance-profile/ec2-default", i["IamInstanceProfile"]["Arn"] )
def test_ec2_set_instance_profile_assocation(self): session_factory = self.replay_flight_data( 'test_ec2_set_instance_profile_association') policy = self.load_policy( { 'name': 'ec2-test-set-instance-profile-association', 'resource': 'ec2', 'filters': [{ 'tag:Name': 'MissingInstanceProfile' }, { 'IamInstanceProfile': 'absent' }], 'actions': [{ 'type': 'set-instance-profile', 'name': 'ec2-default' }] }, session_factory=session_factory) resources = policy.run() self.assertGreaterEqual(len(resources), 1) ec2 = session_factory().client('ec2') resources = ec2.describe_instances( InstanceIds=[r['InstanceId'] for r in resources]) for r in resources['Reservations']: for i in r['Instances']: self.assertIn('IamInstanceProfile', i) self.assertIn('Arn', i['IamInstanceProfile']) self.assertIn(':instance-profile/ec2-default', i['IamInstanceProfile']['Arn'])
def test_ec2_mark_hours(self): localtz = zoneinfo.gettz('America/New_York') dt = datetime.now(localtz) dt = dt.replace(year=2018, month=2, day=20, hour=18, minute=00, second=0, microsecond=0) session_factory = self.replay_flight_data('test_ec2_mark_hours') session = session_factory(region='us-east-1') ec2 = session.client('ec2') policy = self.load_policy({ 'name': 'ec2-mark-5-hours', 'resource': 'ec2', 'filters': [ {'tag:hourly-mark': 'absent'}, {'tag:CreatorName': 'joshuaroot'}], 'actions': [{ 'type': 'mark-for-op', 'tag': 'hourly-mark', 'hours': 3, 'op': 'stop'}] }, session_factory=session_factory) resources = policy.run() self.assertEqual(len(resources), 1) resource = ec2.describe_instances( InstanceIds=[resources[0]['InstanceId']])[ 'Reservations'][0]['Instances'][0] tags = [ t['Value'] for t in resource['Tags'] if t['Key'] == 'hourly-mark'] result = datetime.strptime( tags[0].strip().split('@', 1)[-1], '%Y/%m/%d %H%M %Z').replace( tzinfo=localtz) self.assertEqual(result, dt)
def test_ec2_mark_hours(self): localtz = zoneinfo.gettz('America/New_York') dt = datetime.now(localtz) dt = dt.replace(year=2018, month=2, day=20, hour=18, minute=00) session_factory = self.replay_flight_data('test_ec2_mark_hours') session = session_factory(region='us-east-1') ec2 = session.client('ec2') policy = self.load_policy({ 'name': 'ec2-mark-5-hours', 'resource': 'ec2', 'filters': [ {'tag:hourly-mark': 'absent'}, {'tag:CreatorName': 'joshuaroot'}], 'actions': [{ 'type': 'mark-for-op', 'tag': 'hourly-mark', 'hours': 3, 'op': 'stop'}] }, session_factory=session_factory) resources = policy.run() self.assertEqual(len(resources), 1) resource = ec2.describe_instances( InstanceIds=[resources[0]['InstanceId']])[ 'Reservations'][0]['Instances'][0] tags = [ t['Value'] for t in resource['Tags'] if t['Key'] == 'hourly-mark'] result = datetime.strptime( tags[0].strip().split('@', 1)[-1], '%Y/%m/%d %H%M %Z').replace( tzinfo=localtz) self.assertEqual(result.date(), dt.date())
def test_ec2_mark_zero(self): localtz = zoneinfo.gettz('America/New_York') dt = datetime.now(localtz) dt = dt.replace(year=2017, month=11, day=24, hour=7, minute=00) session_factory = self.replay_flight_data('test_ec2_mark_zero') session = session_factory(region='us-east-1') ec2 = session.client('ec2') resource = ec2.describe_instances(InstanceIds=['i-04d3e0630bd342566'] )['Reservations'][0]['Instances'][0] tags = [ t['Value'] for t in resource['Tags'] if t['Key'] == 'maid_status' ] self.assertEqual(len(tags), 0) policy = self.load_policy( { 'name': 'ec2-mark-zero-days', 'resource': 'ec2', 'filters': [{ 'tag:CreatorName': 'joshuaroot' }], 'actions': [{ 'type': 'mark-for-op', 'days': 0, 'op': 'terminate' }] }, session_factory=session_factory) resources = policy.run() self.assertEqual(len(resources), 1) self.assertEqual(resources[0]['InstanceId'], 'i-04d3e0630bd342566') resource = ec2.describe_instances(InstanceIds=['i-04d3e0630bd342566'] )['Reservations'][0]['Instances'][0] tags = [ t['Value'] for t in resource['Tags'] if t['Key'] == 'maid_status' ] result = datetime.strptime(tags[0].strip().split('@', 1)[-1], '%Y/%m/%d').replace(tzinfo=localtz) self.assertEqual(result.date(), dt.date())
def test_ec2_mark_zero(self): localtz = zoneinfo.gettz('America/New_York') dt = datetime.now(localtz) dt = dt.replace(year=2017, month=11, day=24, hour=7, minute=00) session_factory = self.replay_flight_data('test_ec2_mark_zero') session = session_factory(region='us-east-1') ec2 = session.client('ec2') resource = ec2.describe_instances( InstanceIds=['i-04d3e0630bd342566'])[ 'Reservations'][0]['Instances'][0] tags = [ t['Value'] for t in resource['Tags'] if t['Key'] == 'maid_status'] self.assertEqual(len(tags), 0) policy = self.load_policy({ 'name': 'ec2-mark-zero-days', 'resource': 'ec2', 'filters': [{'tag:CreatorName': 'joshuaroot'}], 'actions': [{ 'type': 'mark-for-op', 'days': 0, 'op': 'terminate'}] }, session_factory=session_factory) resources = policy.run() self.assertEqual(len(resources), 1) self.assertEqual(resources[0]['InstanceId'], 'i-04d3e0630bd342566') resource = ec2.describe_instances( InstanceIds=['i-04d3e0630bd342566'])[ 'Reservations'][0]['Instances'][0] tags = [ t['Value'] for t in resource['Tags'] if t['Key'] == 'maid_status'] result = datetime.strptime( tags[0].strip().split('@', 1)[-1], '%Y/%m/%d').replace( tzinfo=localtz) self.assertEqual(result.date(), dt.date())
def test_ec2_mark_hours(self): localtz = zoneinfo.gettz("America/New_York") dt = datetime.now(localtz) dt = dt.replace( year=2018, month=2, day=20, hour=18, minute=00, second=0, microsecond=0 ) session_factory = self.replay_flight_data("test_ec2_mark_hours") session = session_factory(region="us-east-1") ec2 = session.client("ec2") policy = self.load_policy( { "name": "ec2-mark-5-hours", "resource": "ec2", "filters": [ {"tag:hourly-mark": "absent"}, {"tag:CreatorName": "joshuaroot"} ], "actions": [ { "type": "mark-for-op", "tag": "hourly-mark", "hours": 3, "op": "stop", } ], }, session_factory=session_factory, ) resources = policy.run() self.assertEqual(len(resources), 1) resource = ec2.describe_instances(InstanceIds=[resources[0]["InstanceId"]])[ "Reservations" ][ 0 ][ "Instances" ][ 0 ] tags = [t["Value"] for t in resource["Tags"] if t["Key"] == "hourly-mark"] result = datetime.strptime( tags[0].strip().split("@", 1)[-1], "%Y/%m/%d %H%M %Z" ).replace( tzinfo=localtz ) self.assertEqual(result, dt)
def test_ec2_mark_hours(self): localtz = zoneinfo.gettz("America/New_York") dt = datetime.now(localtz) dt = dt.replace( year=2018, month=2, day=20, hour=18, minute=00, second=0, microsecond=0 ) session_factory = self.replay_flight_data("test_ec2_mark_hours") session = session_factory(region="us-east-1") ec2 = session.client("ec2") policy = self.load_policy( { "name": "ec2-mark-5-hours", "resource": "ec2", "filters": [ {"tag:hourly-mark": "absent"}, {"tag:CreatorName": "joshuaroot"} ], "actions": [ { "type": "mark-for-op", "tag": "hourly-mark", "hours": 3, "op": "stop", } ], }, session_factory=session_factory, ) resources = policy.run() self.assertEqual(len(resources), 1) resource = ec2.describe_instances(InstanceIds=[resources[0]["InstanceId"]])[ "Reservations" ][ 0 ][ "Instances" ][ 0 ] tags = [t["Value"] for t in resource["Tags"] if t["Key"] == "hourly-mark"] result = datetime.strptime( tags[0].strip().split("@", 1)[-1], "%Y/%m/%d %H%M %Z" ).replace( tzinfo=localtz ) self.assertEqual(result, dt)
def test_ec2_set_instance_profile_assocation(self): session_factory = self.replay_flight_data( "test_ec2_set_instance_profile_association") policy = self.load_policy( { "name": "ec2-test-set-instance-profile-association", "resource": "ec2", "filters": [ { "tag:Name": "MissingInstanceProfile" }, { "IamInstanceProfile": "absent" }, ], "actions": [{ "type": "set-instance-profile", "name": "ec2-default" }], }, session_factory=session_factory, ) resources = policy.run() self.assertGreaterEqual(len(resources), 1) ec2 = session_factory().client("ec2") resources = ec2.describe_instances( InstanceIds=[r["InstanceId"] for r in resources]) for r in resources["Reservations"]: for i in r["Instances"]: self.assertIn("IamInstanceProfile", i) self.assertIn("Arn", i["IamInstanceProfile"]) self.assertIn(":instance-profile/ec2-default", i["IamInstanceProfile"]["Arn"])