def get_keys_id_ls_to_delete()

in gitops/env/stage2/configs/mtk-connect/mtk-connect-post-key/configure-key.py [0:0]


def get_keys_id_ls_to_delete(key_list, current_key):
  """
  Searches the key_list and finds ale the keysthat are two days older than the current key (current_key variable),
  returns list of key's ids.
  """   
  key_id_ls = []
  threshold_date = 0

  # Get creation date of current_key
  try:
    for key in key_list:
      if key["key"][:8] == current_key[:8]:
        threshold_date = datetime.datetime.strptime(key["creationTime"], "%Y-%m-%dT%H:%M:%S.%fZ")
        
        # Calculate what is the threshold datefor keys deletion
        threshold_date -= relativedelta(days=KEY_DEL_TIME_DELTA)
        print(f"keys older or equal to will be deleted: {threshold_date.date()}\n")
        break
  except Exception as e:
    print(f"Exception occured when getting key id. \n\tException: {e}")

  # Create list of keys id that older or equal than threshold date 
  for key in key_list:
    key_creation_time = datetime.datetime.strptime(key["creationTime"], "%Y-%m-%dT%H:%M:%S.%fZ")
    if key_creation_time.date() <= threshold_date.date():
      key_id_ls.append(key["id"])

  return key_id_ls