# Path: libcloudforensics/errors.py
# class ResourceNotFoundError(LCFError):
#   """Error when an issue with non-existent resource is encountered."""
# 
# Path: libcloudforensics/errors.py
# class ResourceCreationError(LCFError):
#   """Error when an issue with creating a new resource is encountered."""
# 
# Path: libcloudforensics/providers/aws/internal/common.py
# EC2_SERVICE = 'ec2'
# 
# Path: libcloudforensics/providers/aws/internal/account.py
# class AWSAccount:
#   def __init__(self,
#                default_availability_zone: str,
#                aws_profile: Optional[str] = None,
#                aws_access_key_id: Optional[str] = None,
#                aws_secret_access_key: Optional[str] = None,
#                aws_session_token: Optional[str] = None) -> None:
#   def ec2(self) -> ec2.EC2:
#   def ebs(self) -> ebs.EBS:
#   def kms(self) -> kms.KMS:
#   def s3(self) -> s3.S3:
#   def iam(self) -> iam.IAM:
#   def ClientApi(self,
#                 service: str,
#                 region: Optional[str] = None) -> 'botocore.client.EC2':  # pylint: disable=no-member
#   def ResourceApi(self,
#                   service: str,
#                   # The return type doesn't exist until Runtime, therefore we
#                   # need to ignore the type hint
#                   # pylint: disable=line-too-long
#                   region: Optional[str] = None) -> 'boto3.resources.factory.ec2.ServiceResource':  # type: ignore
# 
# Path: libcloudforensics/logging_utils.py
# def _GenerateColorSequences() -> List[str]:
#   def __init__(self,
#                colorize: bool = True,
#                random_color: bool = False,
#                **kwargs: str) -> None:
#   def format(self, record: logging.LogRecord) -> str:
# def SetUpLogger(name: str, no_newline: bool = False) -> None:
# def GetLogger(name: str) -> logging.Logger:
# COLOR_SEQS = _GenerateColorSequences()
# RESET_SEQ = '\u001b[0m'
# BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = COLOR_SEQS[8:16]
# BG_RED = '\u001b[41m'  # Red background
# BOLD = '\u001b[1m'  # Bold / bright modifier
# LOG_FORMAT = ('[%(asctime)s] [{0:s}{color:s}%(name)-20s{1:s}] %(levelname)-8s'
#               ' %(message)s')
# LEVEL_COLOR_MAP = {
#     'WARNING': YELLOW,
#     'INFO': WHITE,
#     'DEBUG': BLUE,
#     'CRITICAL': BOLD + BG_RED + WHITE,
#     'ERROR': RED
# }
# class Formatter(logging.Formatter):
# 
# Path: tests/providers/aws/aws_cli.py
# class AWSCLIHelper:
#   def PrepareStartAnalysisVmCmd(
#       vm_name: str,
#       zone: str,
#       attach_volumes: Optional[List[str]] = None) -> str:
#   def PrepareCreateVolumeCopyCmd(
#       zone: str,
#       dst_zone: Optional[str] = None,
#       instance_id: Optional[str] = None,
#       volume_id: Optional[str] = None) -> str:
#   def PrepareListImagesCmd(
#       zone: str,
#       qfilter: Optional[str] = None) -> str:
# 
# Path: tests/scripts/utils.py
# def ReadProjectInfo(keys: List[str]) -> Dict[str, str]:


import subprocess
import typing
import unittest
import warnings
import botocore
from time import sleep
from libcloudforensics.errors import ResourceNotFoundError
from libcloudforensics.errors import ResourceCreationError
from libcloudforensics.providers.aws.internal.common import EC2_SERVICE
from libcloudforensics.providers.aws.internal import account
from libcloudforensics import logging_utils
from tests.providers.aws import aws_cli
from tests.scripts import utils
    logger.info(output)
    try:
      return cls.aws.ec2.GetInstancesByName(vm_name)[0]
    except ResourceNotFoundError as error:
      raise ResourceNotFoundError(
          'Failed finding created VM in account profile {0:s}'.format(
              cls.aws.aws_profile), __name__) from error

  @classmethod
  @typing.no_type_check
  def _RunCreateVolumeCopy(
      cls, zone, dst_zone=None, instance_id=None, volume_id=None):
    cmd = cls.cli.PrepareCreateVolumeCopyCmd(
        zone, dst_zone=dst_zone, instance_id=instance_id, volume_id=volume_id)
    try:
      output = subprocess.check_output(
          cmd.split(), stderr=subprocess.STDOUT, shell=False)
    except subprocess.CalledProcessError as error:
      raise ResourceCreationError('Failed copying volume', __name__) from error
    if not output:
      raise ResourceCreationError(
          'Could not find volume copy result', __name__)
    volume_copy_name = output.decode('utf-8').split(' ')[-1]
    volume_copy_name = volume_copy_name[:volume_copy_name.rindex('copy') + 4]
    logger.info(output)
    logger.info("Volume successfully copied to {0:s}".format(
        volume_copy_name))
    try:
      return cls.aws.ebs.GetVolumesByName(volume_copy_name)[0]
    except ResourceNotFoundError as error:

# Path: libcloudforensics/errors.py
# class ResourceNotFoundError(LCFError):
#   """Error when an issue with non-existent resource is encountered."""
# 
# Path: libcloudforensics/errors.py
# class ResourceCreationError(LCFError):
#   """Error when an issue with creating a new resource is encountered."""
# 
# Path: libcloudforensics/providers/aws/internal/common.py
# EC2_SERVICE = 'ec2'
# 
# Path: libcloudforensics/providers/aws/internal/account.py
# class AWSAccount:
#   def __init__(self,
#                default_availability_zone: str,
#                aws_profile: Optional[str] = None,
#                aws_access_key_id: Optional[str] = None,
#                aws_secret_access_key: Optional[str] = None,
#                aws_session_token: Optional[str] = None) -> None:
#   def ec2(self) -> ec2.EC2:
#   def ebs(self) -> ebs.EBS:
#   def kms(self) -> kms.KMS:
#   def s3(self) -> s3.S3:
#   def iam(self) -> iam.IAM:
#   def ClientApi(self,
#                 service: str,
#                 region: Optional[str] = None) -> 'botocore.client.EC2':  # pylint: disable=no-member
#   def ResourceApi(self,
#                   service: str,
#                   # The return type doesn't exist until Runtime, therefore we
#                   # need to ignore the type hint
#                   # pylint: disable=line-too-long
#                   region: Optional[str] = None) -> 'boto3.resources.factory.ec2.ServiceResource':  # type: ignore
# 
# Path: libcloudforensics/logging_utils.py
# def _GenerateColorSequences() -> List[str]:
#   def __init__(self,
#                colorize: bool = True,
#                random_color: bool = False,
#                **kwargs: str) -> None:
#   def format(self, record: logging.LogRecord) -> str:
# def SetUpLogger(name: str, no_newline: bool = False) -> None:
# def GetLogger(name: str) -> logging.Logger:
# COLOR_SEQS = _GenerateColorSequences()
# RESET_SEQ = '\u001b[0m'
# BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = COLOR_SEQS[8:16]
# BG_RED = '\u001b[41m'  # Red background
# BOLD = '\u001b[1m'  # Bold / bright modifier
# LOG_FORMAT = ('[%(asctime)s] [{0:s}{color:s}%(name)-20s{1:s}] %(levelname)-8s'
#               ' %(message)s')
# LEVEL_COLOR_MAP = {
#     'WARNING': YELLOW,
#     'INFO': WHITE,
#     'DEBUG': BLUE,
#     'CRITICAL': BOLD + BG_RED + WHITE,
#     'ERROR': RED
# }
# class Formatter(logging.Formatter):
# 
# Path: tests/providers/aws/aws_cli.py
# class AWSCLIHelper:
#   def PrepareStartAnalysisVmCmd(
#       vm_name: str,
#       zone: str,
#       attach_volumes: Optional[List[str]] = None) -> str:
#   def PrepareCreateVolumeCopyCmd(
#       zone: str,
#       dst_zone: Optional[str] = None,
#       instance_id: Optional[str] = None,
#       volume_id: Optional[str] = None) -> str:
#   def PrepareListImagesCmd(
#       zone: str,
#       qfilter: Optional[str] = None) -> str:
# 
# Path: tests/scripts/utils.py
# def ReadProjectInfo(keys: List[str]) -> Dict[str, str]:


# Path: tests/providers/aws/e2e_cli.py
import subprocess
import typing
import unittest
import warnings
import botocore
from time import sleep
from libcloudforensics.errors import ResourceNotFoundError
from libcloudforensics.errors import ResourceCreationError
from libcloudforensics.providers.aws.internal.common import EC2_SERVICE
from libcloudforensics.providers.aws.internal import account
from libcloudforensics import logging_utils
from tests.providers.aws import aws_cli
from tests.scripts import utils

    logger.info(output)
    try:
      return cls.aws.ec2.GetInstancesByName(vm_name)[0]
    except ResourceNotFoundError as error:
      raise ResourceNotFoundError(
          'Failed finding created VM in account profile {0:s}'.format(
              cls.aws.aws_profile), __name__) from error

  @classmethod
  @typing.no_type_check
  def _RunCreateVolumeCopy(
      cls, zone, dst_zone=None, instance_id=None, volume_id=None):
    cmd = cls.cli.PrepareCreateVolumeCopyCmd(
        zone, dst_zone=dst_zone, instance_id=instance_id, volume_id=volume_id)
    try:
      output = subprocess.check_output(
          cmd.split(), stderr=subprocess.STDOUT, shell=False)
    except subprocess.CalledProcessError as error:
      raise ResourceCreationError('Failed copying volume', __name__) from error
    if not output:
      raise ResourceCreationError(
          'Could not find volume copy result', __name__)
    volume_copy_name = output.decode('utf-8').split(' ')[-1]
    volume_copy_name = volume_copy_name[:volume_copy_name.rindex('copy') + 4]
    logger.info(output)
    logger.info("Volume successfully copied to {0:s}".format(
        volume_copy_name))
    try:
      return cls.aws.ebs.GetVolumesByName(volume_copy_name)[0]
    except ResourceNotFoundError as error:

      raise ResourceNotFoundError(