parameters: (cleaner)
docstring: Deletes all policies from the client. It will construct a list of resources, delete them, then wait for
      each deletion.
      :param cleaner: A Cleaner object from the main cleanup.py script
      :return: None
docstring_summary: Deletes all policies from the client. It will construct a list of resources, delete them, then wait for
      each deletion.
      :param cleaner: A Cleaner object from the main cleanup.py script
      :return: None
docstring_tokens: Deletes all policies from the client . It will construct a list of resources delete them then wait for each deletion . : param cleaner : A Cleaner object from the main cleanup . py script : return : None
function: def delete_iam_policies(cleaner):
      """
      Deletes all policies from the client. It will construct a list of resources, delete them, then wait for
      each deletion.
      :param cleaner: A Cleaner object from the main cleanup.py script
      :return: None
      """
      print('\n\nlooking for iam policies with names or paths starting with one of {}'
            .format(cleaner.describe_prefixes()))
      iam_policy_arn_list = []
  
      # Construct list
      try:
          iam_policy_paginator = cleaner.iam.get_paginator('list_policies')
          iam_policy_page_iterator = iam_policy_paginator.paginate()
          
          for page in iam_policy_page_iterator:
              for policy_info in page['Policies']:
                  policy_path = policy_info['Path'][1:]  # remove leading /
                  if cleaner.has_prefix(policy_path) or cleaner.has_prefix(policy_info['PolicyName']):
                      print('  found policy {}'.format(policy_info['PolicyName']))
                      iam_policy_arn_list.append(policy_info['Arn'])
      except KeyError as e:
          print("      ERROR: Unexpected KeyError while deleting iam policies. {}".format(exception_utils.message(e)))
          return
      except ClientError as e:
          print("      ERROR: Unexpected error for paginator for the iam client. {}".format(exception_utils.message(e)))
          return
  
      # Delete iam policies
      for policy_arn in iam_policy_arn_list:
          print('    deleting policy {}'.format(policy_arn))
          try:
              cleaner.iam.delete_policy(PolicyArn=policy_arn)
          except ClientError as e:
              if e.response["Error"]["Code"] == "TooManyRequestsException":
                  print('    too many requests, sleeping...')
                  time.sleep(cleaner.wait_interval)
              else:
                  print('      ERROR: Failed to delete iam policy {0} due to {1}'.format(policy_arn,
                                                                                         exception_utils.message(e)))
                  cleaner.add_to_failed_resources("iam", policy_arn)
  
      # Wait for deletion
      for policy_arn in iam_policy_arn_list:
          try:
              cleanup_utils.wait_for(lambda: not __iam_policy_exists(cleaner, policy_arn),
                                     attempts=cleaner.wait_attempts,
                                     interval=cleaner.wait_interval,
                                     timeout_exception=cleanup_utils.WaitError)
          except cleanup_utils.WaitError:
              print('      ERROR. policy {0} was not deleted after timeout'.format(policy_arn))
              cleaner.add_to_failed_resources("iam", policy_arn)
          except ClientError as e:
              if e.response["Error"]["Code"] == "TooManyRequestsException":
                  print('    too many requests, sleeping...')
                  time.sleep(cleaner.wait_interval)
              else:
                  print("      ERROR: Unexpected error occurred waiting for policy {0} to delete due to {1}".format(
                      policy_arn, exception_utils.message(e)))
                  cleaner.add_to_failed_resources("iam", policy_arn)
function_tokens: def delete_iam_policies ( cleaner ) : print ( '\n\nlooking for iam policies with names or paths starting with one of {}' . format ( cleaner . describe_prefixes ( ) ) ) iam_policy_arn_list = [ ] # Construct list try : iam_policy_paginator = cleaner . iam . get_paginator ( 'list_policies' ) iam_policy_page_iterator = iam_policy_paginator . paginate ( ) for page in iam_policy_page_iterator : for policy_info in page [ 'Policies' ] : policy_path = policy_info [ 'Path' ] [ 1 : ] # remove leading / if cleaner . has_prefix ( policy_path ) or cleaner . has_prefix ( policy_info [ 'PolicyName' ] ) : print ( '  found policy {}' . format ( policy_info [ 'PolicyName' ] ) ) iam_policy_arn_list . append ( policy_info [ 'Arn' ] ) except KeyError as e : print ( "      ERROR: Unexpected KeyError while deleting iam policies. {}" . format ( exception_utils . message ( e ) ) ) return except ClientError as e : print ( "      ERROR: Unexpected error for paginator for the iam client. {}" . format ( exception_utils . message ( e ) ) ) return # Delete iam policies for policy_arn in iam_policy_arn_list : print ( '    deleting policy {}' . format ( policy_arn ) ) try : cleaner . iam . delete_policy ( PolicyArn = policy_arn ) except ClientError as e : if e . response [ "Error" ] [ "Code" ] == "TooManyRequestsException" : print ( '    too many requests, sleeping...' ) time . sleep ( cleaner . wait_interval ) else : print ( '      ERROR: Failed to delete iam policy {0} due to {1}' . format ( policy_arn , exception_utils . message ( e ) ) ) cleaner . add_to_failed_resources ( "iam" , policy_arn ) # Wait for deletion for policy_arn in iam_policy_arn_list : try : cleanup_utils . wait_for ( lambda : not __iam_policy_exists ( cleaner , policy_arn ) , attempts = cleaner . wait_attempts , interval = cleaner . wait_interval , timeout_exception = cleanup_utils . WaitError ) except cleanup_utils . WaitError : print ( '      ERROR. policy {0} was not deleted after timeout' . format ( policy_arn ) ) cleaner . add_to_failed_resources ( "iam" , policy_arn ) except ClientError as e : if e . response [ "Error" ] [ "Code" ] == "TooManyRequestsException" : print ( '    too many requests, sleeping...' ) time . sleep ( cleaner . wait_interval ) else : print ( "      ERROR: Unexpected error occurred waiting for policy {0} to delete due to {1}" . format ( policy_arn , exception_utils . message ( e ) ) ) cleaner . add_to_failed_resources ( "iam" , policy_arn )