Пример #1
0
def object_exists(bucket: str, key: str) -> bool:
    try:
        boto3_resource("s3").Object(bucket, key).load()
    except ClientError as e:
        if e.response["Error"]["Code"] == "404":
            return False
        else:
            raise
    else:
        return True
Пример #2
0
def delete_sec_group(sec_group: str) -> None:
    ec2 = boto3_resource("ec2")
    try:
        sgroup = ec2.SecurityGroup(sec_group)
        if sgroup.ip_permissions:
            sgroup.revoke_ingress(IpPermissions=sgroup.ip_permissions)
        try:
            sgroup.delete()
        except botocore.exceptions.ClientError as ex:
            error: Dict[str, Any] = ex.response["Error"]
            if f"resource {sec_group} has a dependent object" not in error[
                    "Message"]:
                raise
            time.sleep(60)
            _logger.warning(
                f"Waiting 60 seconds to have {sec_group} free of dependents.")
            sgroup.delete()
    except botocore.exceptions.ClientError as ex:
        error = ex.response["Error"]
        if f"The security group '{sec_group}' does not exist" not in error[
                "Message"]:
            _logger.warning(
                f"Ignoring security group {sec_group} because it does not exist anymore."
            )
        elif f"resource {sec_group} has a dependent object" not in error[
                "Message"]:
            _logger.warning(
                f"Ignoring security group {sec_group} because it has a dependent object"
            )
        else:
            raise
Пример #3
0
def _network_interface(vpc_id: str) -> None:
    client = boto3_client("ec2")
    ec2 = boto3_resource("ec2")
    for i in client.describe_network_interfaces(Filters=[{
            "Name": "vpc-id",
            "Values": [vpc_id]
    }])["NetworkInterfaces"]:
        try:
            network_interface = ec2.NetworkInterface(i["NetworkInterfaceId"])
            if "Interface for NAT Gateway" not in network_interface.description:
                _logger.debug(
                    f"Forgotten NetworkInterface: {i['NetworkInterfaceId']}.")
                if network_interface.attachment is not None and network_interface.attachment[
                        "Status"] == "attached":
                    attempts: int = 0
                    while network_interface.attachment is None or network_interface.attachment[
                            "Status"] != "detached":
                        if attempts >= 10:
                            _logger.debug(
                                f"Ignoring NetworkInterface: {i['NetworkInterfaceId']} after 10 detach attempts."
                            )
                            break
                        _detach_network_interface(i["NetworkInterfaceId"],
                                                  network_interface)
                        attempts += 1
                        time.sleep(3)
                    else:
                        network_interface.delete()
                        _logger.debug(
                            f"NetWorkInterface {i['NetworkInterfaceId']} deleted."
                        )
        except botocore.exceptions.ClientError as ex:
            error: Dict[str, Any] = ex.response["Error"]
            if "is currently in use" in error["Message"]:
                _logger.warning(
                    f"Ignoring NetWorkInterface {i['NetworkInterfaceId']} because it stills in use."
                )
            elif "does not exist" in error["Message"]:
                _logger.warning(
                    f"Ignoring NetWorkInterface {i['NetworkInterfaceId']} because it does not exist anymore."
                )
            elif "You are not allowed to manage" in error["Message"]:
                _logger.warning(
                    f"Ignoring NetWorkInterface {i['NetworkInterfaceId']} because you are not allowed to manage."
                )
            elif "You do not have permission to access the specified resource" in error[
                    "Message"]:
                _logger.warning(
                    f"Ignoring NetWorkInterface {i['NetworkInterfaceId']} "
                    "because you do not have permission to access the specified resource."
                )
            else:
                raise
Пример #4
0
 def fetch_properties(self) -> None:
     try:
         ec2 = boto3_resource("ec2")
         subnet = ec2.Subnet(self.subnet_id)
         self.cidr_block = str(subnet.cidr_block)
         self.availability_zone = str(subnet.availability_zone)
         self.vpc_id = str(subnet.vpc_id)
         self._fetch_route_table_id()
         _logger.debug("Properties from subnet %s successfully fetched.",
                       self.subnet_id)
     except botocore.exceptions.ClientError:
         _logger.debug(
             "Unable to fetch properties from subnet (%s) right now.",
             self.subnet_id)
Пример #5
0
 def _fetch_vpc_cidr(self) -> None:
     ec2 = boto3_resource("ec2")
     vpc = ec2.Vpc(self.vpc_id)
     self.vpc_cidr_block = str(vpc.cidr_block)