def Cleanup(self, vm):
    """Clean up RDS instances, cleanup the extra subnet created for the
       creation of the RDS instance.

    Args:
      vm: The VM that was used as the test client, which also stores states
          for clean-up.
    """

    # Now, we can delete the DB instance. vm.db_instance_id is the id to call.
    # We need to keep querying the status of the deletion here before we let
    # this go. RDS DB deletion takes some time to finish. And we have to
    # wait until this DB is deleted before we proceed because this DB holds
    # references to various other resources: subnet groups, subnets, vpc, etc.
    delete_db_cmd = util.AWS_PREFIX + [
        'rds',
        'delete-db-instance',
        '--db-instance-identifier', vm.db_instance_id,
        '--skip-final-snapshot']

    logging.info('Deleting db instance %s...', vm.db_instance_id)

    # Note below, the status of this deletion command is validated below in the
    # loop. both stdout and stderr are checked.
    stdout, stderr, _ = vm_util.IssueCommand(delete_db_cmd)
    logging.info('Request to delete the DB has been issued, stdout:\n%s\n'
                 'stderr:%s\n', stdout, stderr)

    status_query_cmd = util.AWS_PREFIX + [
        'rds',
        'describe-db-instances',
        '--db-instance-id', vm.db_instance_id]

    db_status = None
    for status_query_count in xrange(1, DB_STATUS_QUERY_LIMIT + 1):
      try:
        response = json.loads(stdout)
      except __HOLE__:
        # stdout cannot be parsed into json, it might simply be empty because
        # deletion has been completed.
        break

      db_status = _RDSParseDBInstanceStatus(response)
      if db_status == 'deleting':
        logging.info('DB is still in the deleting state, status_query_count '
                     'is %d', status_query_count)
        # Wait for a few seconds and query status
        time.sleep(DB_STATUS_QUERY_INTERVAL)
        stdout, stderr, _ = vm_util.IssueCommand(status_query_cmd)
      else:
        logging.info('DB deletion status is no longer in deleting, it is %s',
                     db_status)
        break
    else:
      logging.warn('DB is still in deleting state after long wait, bail.')

    db_instance_deletion_failed = False
    if db_status == 'deleted' or re.findall('DBInstanceNotFound', stderr):
      # Sometimes we get a 'deleted' status from DB status query command,
      # but even more times, the DB status query command would fail with
      # an "not found" error, both are positive confirmation that the DB has
      # been deleted.
      logging.info('DB has been successfully deleted, got confirmation.')
    else:
      # We did not get a positive confirmation that the DB is deleted even after
      # long wait, we have to bail. But we will log an error message, and
      # then raise an exception at the end of this function so this particular
      # run will show as a failed run to the user and allow them to examine
      # the logs
      db_instance_deletion_failed = True
      logging.error(
          'RDS DB instance %s failed to be deleted, we did not get '
          'final confirmation from stderr, which is:\n %s', vm.db_instance_id,
          stderr)

    if hasattr(vm, 'db_subnet_group_name'):
      delete_db_subnet_group_cmd = util.AWS_PREFIX + [
          'rds',
          'delete-db-subnet-group',
          '--db-subnet-group-name', vm.db_subnet_group_name]
      stdout, stderr, _ = vm_util.IssueCommand(delete_db_subnet_group_cmd)
      logging.info('Deleted the db subnet group. stdout is:\n%s, stderr: \n%s',
                   stdout, stderr)

    if hasattr(vm, 'extra_subnet_for_db'):
      vm.extra_subnet_for_db.Delete()

    if db_instance_deletion_failed:
      raise DBStatusQueryError('Failed to get confirmation of DB instance '
                               'deletion! Check the log for details!')
dataset/ETHPy150Open GoogleCloudPlatform/PerfKitBenchmarker/perfkitbenchmarker/linux_benchmarks/mysql_service_benchmark.py/RDSMySQLBenchmark.Cleanup