def __call__(self, i): v = i.get('tags', {}).get(self.tag, None) if v is None: return False if ':' not in v or '@' not in v: return False msg, tgt = v.rsplit(':', 1) action, action_date_str = tgt.strip().split('@', 1) if action != self.op: return False try: action_date = parse(action_date_str) except Exception: self.log.error("could not parse tag:%s value:%s on %s" % (self.tag, v, i['InstanceId'])) return False # current_date must match timezones with the parsed date string if action_date.tzinfo: action_date = action_date.astimezone(self.tz) current_date = now(tz=self.tz) else: current_date = now() return current_date >= ( action_date - timedelta(days=self.skew, hours=self.skew_hours))
def test_tag_filter(self): date = now().strftime('%Y-%m-%d') date_future = (now() + datetime.timedelta(days=1)).strftime('%Y-%m-%d') resources = [ tools.get_resource( {'custodian_status': 'TTL: stop@{0}'.format(date)}), tools.get_resource( {'custodian_status': 'TTL: stop@{0}'.format(date_future)}) ] self._test_filter_scenario(resources, 1)
def __call__(self, i): v = i.get('tags', {}).get(self.tag, None) if v is None: return False if ':' not in v or '@' not in v: return False msg, tgt = v.rsplit(':', 1) action, action_date_str = tgt.strip().split('@', 1) if action != self.op: return False try: action_date = parse(action_date_str) except Exception: self.log.warning("could not parse tag:%s value:%s on %s" % (self.tag, v, i['InstanceId'])) if action_date.tzinfo: # if action_date is timezone aware, set to timezone provided action_date = action_date.astimezone(self.tz) self.current_date = now(tz=self.tz) return self.current_date >= ( action_date - timedelta(days=self.skew, hours=self.skew_hours))
def __call__(self, i): v = i.get('tags', {}).get(self.tag, None) if v is None: return False if ':' not in v or '@' not in v: return False msg, tgt = v.rsplit(':', 1) action, action_date_str = tgt.strip().split('@', 1) if action != self.op: return False try: action_date = parse(action_date_str) except Exception: self.log.warning("could not parse tag:%s value:%s on %s" % ( self.tag, v, i['InstanceId'])) if action_date.tzinfo: # if action_date is timezone aware, set to timezone provided action_date = action_date.astimezone(self.tz) self.current_date = now(tz=self.tz) return self.current_date >= ( action_date - timedelta(days=self.skew, hours=self.skew_hours))
def test_different_op_returns_no_resource(self): date = now().strftime('%Y-%m-%d') resources = [ tools.get_resource( {'custodian_status': 'TTL: delete@{0}'.format(date)}) ] self._test_filter_scenario(resources, 0)
def test_custom_tag_filter(self): date = now().strftime('%Y-%m-%d') resources = [ tools.get_resource({'custom_status': 'TTL: stop@{0}'.format(date)}) ] filter_definition = {'op': 'stop', 'tag': 'custom_status'} self._test_filter_scenario(resources, 1, filter_definition)
def process(self, resources, event=None): from c7n_azure.utils import now if self.current_date is None: self.current_date = now() self.tag = self.data.get('tag', DEFAULT_TAG) self.op = self.data.get('op', 'stop') self.skew = self.data.get('skew', 0) self.skew_hours = self.data.get('skew_hours', 0) self.tz = zoneinfo.gettz(Time.TZ_ALIASES.get(self.data.get('tz', 'utc'))) return super(TagActionFilter, self).process(resources, event)
def test_mark_for_op(self): self._run_policy([{'type': 'mark-for-op', 'tag': 'cctest_mark', 'op': 'delete', 'msg': '{op}, {action_date}', 'days': self.DAYS}]) expected_date = utils.now() + datetime.timedelta(days=self.DAYS) expected = 'delete, ' + expected_date.strftime('%Y/%m/%d') self.assertEqual(self._get_tags().get('cctest_mark'), expected)
def process(self, resources, event=None): from c7n_azure.utils import now if self.current_date is None: self.current_date = now() self.tag = self.data.get('tag', DEFAULT_TAG) self.op = self.data.get('op', 'stop') self.skew = self.data.get('skew', 0) self.skew_hours = self.data.get('skew_hours', 0) self.tz = tzutils.gettz(Time.TZ_ALIASES.get(self.data.get('tz', 'utc'))) return super(TagActionFilter, self).process(resources, event)
def test_timezone_in_datestring(self): tz = Time.get_tz('America/Santiago') date = (now(tz) - datetime.timedelta(hours=1)).strftime('%Y/%m/%d %H%M %Z') resources = [ tools.get_resource( {'custodian_status': 'TTL: stop@{0}'.format(date)}) ] self._test_filter_scenario(resources, 1)
def generate_timestamp(self, days, hours): from c7n_azure.utils import now n = now(tz=self.tz) if days is None or hours is None: # maintains default value of days being 4 if nothing is provided days = 4 action_date = (n + timedelta(days=days, hours=hours)) if hours > 0: action_date_string = action_date.strftime('%Y/%m/%d %H%M %Z') else: action_date_string = action_date.strftime('%Y/%m/%d') return action_date_string
def generate_timestamp(self, days, hours): from c7n_azure.utils import now n = now(tz=self.tz) if days is None or hours is None: # maintains default value of days being 4 if nothing is provided days = 4 action_date = (n + timedelta(days=days, hours=hours)) if hours > 0: action_date_string = action_date.strftime('%Y/%m/%d %H%M %Z') else: action_date_string = action_date.strftime('%Y/%m/%d') return action_date_string
def test_mark_for_op(self, update_resource_tags): self.patch(TagDelayedAction, 'type', 'mark-for-op') action = self._get_action({'op': 'stop', 'days': self.DAYS}) resource = tools.get_resource(self.existing_tags) action.process([resource]) tags = tools.get_tags_parameter(update_resource_tags) date = (utils.now(tz=action.tz) + datetime.timedelta(days=self.DAYS)).strftime('%Y/%m/%d') expected_value = TagDelayedAction.default_template.format(op='stop', action_date=date) expected_tags = self.existing_tags.copy() expected_tags.update({'custodian_status': expected_value}) self.assertEqual(tags, expected_tags)