Beispiel #1
0
def delete_sdb_domain(sdb_domain_name: str,
                      region: Optional[str] = None,
                      quiet: bool = True) -> None:
    sdb_client = cast(SimpleDBClient, session.client("sdb",
                                                     region_name=region))
    sdb_client.delete_domain(DomainName=sdb_domain_name)
    printq(f'SBD Domain: "{sdb_domain_name}" successfully deleted.', quiet)
Beispiel #2
0
def delete_iam_instance_profile(
    instance_profile_name: str, region: Optional[str] = None, quiet: bool = True
) -> None:
    iam_resource = cast(IAMServiceResource, session.resource("iam", region_name=region))
    instance_profile = iam_resource.InstanceProfile(instance_profile_name)
    if instance_profile.roles is not None:
        for role in instance_profile.roles:
            printq(f'Now dissociating role: {role.name} from instance profile {instance_profile_name}', quiet)
            instance_profile.remove_role(RoleName=role.name)
    instance_profile.delete()
    printq(f'Instance profile "{instance_profile_name}" successfully deleted.', quiet)
Beispiel #3
0
def delete_iam_instance_profile(instance_profile_name: str,
                                region: Optional[str] = None,
                                quiet: bool = True):
    iam_resource = aws.resource('iam', region_name=region)
    instance_profile = iam_resource.InstanceProfile(instance_profile_name)
    for role in instance_profile.roles:
        printq(
            f'Now dissociating role: {role.name} from instance profile {instance_profile_name}',
            quiet)
        instance_profile.remove_role(RoleName=role.name)
    instance_profile.delete()
    printq(f'Instance profile "{instance_profile_name}" successfully deleted.',
           quiet)
Beispiel #4
0
def delete_s3_bucket(bucket: str,
                     region: Optional[str],
                     quiet: bool = True) -> None:
    """
    Delete the given S3 bucket.
    """
    printq(f'Deleting s3 bucket in region "{region}": {bucket}', quiet)
    s3_client = cast(S3Client, session.client('s3', region_name=region))
    s3_resource = cast(S3ServiceResource,
                       session.resource('s3', region_name=region))

    paginator = s3_client.get_paginator('list_object_versions')
    try:
        for response in paginator.paginate(Bucket=bucket):
            # Versions and delete markers can both go in here to be deleted.
            # They both have Key and VersionId, but there's no shared base type
            # defined for them in the stubs to express that. See
            # <https://github.com/vemel/mypy_boto3_builder/issues/123>. So we
            # have to do gymnastics to get them into the same list.
            to_delete: List[Dict[str, Any]] = cast(List[Dict[str, Any]], response.get('Versions', [])) + \
                                              cast(List[Dict[str, Any]], response.get('DeleteMarkers', []))
            for entry in to_delete:
                printq(
                    f"    Deleting {entry['Key']} version {entry['VersionId']}",
                    quiet)
                s3_client.delete_object(Bucket=bucket,
                                        Key=entry['Key'],
                                        VersionId=entry['VersionId'])
        s3_resource.Bucket(bucket).delete()
        printq(f'\n * Deleted s3 bucket successfully: {bucket}\n\n', quiet)
    except s3_client.exceptions.NoSuchBucket:
        printq(f'\n * S3 bucket no longer exists: {bucket}\n\n', quiet)
Beispiel #5
0
def delete_iam_role(
    role_name: str, region: Optional[str] = None, quiet: bool = True
) -> None:
    from boto.iam.connection import IAMConnection
    # TODO: the Boto3 type hints are a bit oversealous here; they want hundreds
    # of overloads of the client-getting methods to exist based on the literal
    # string passed in, to return exactly the right kind of client or resource.
    # So we end up having to wrap all the calls in casts, which kind of defeats
    # the point of a nice fluent method you can call with the name of the thing
    # you want; we should have been calling iam_client() and so on all along if
    # we wanted MyPy to be able to understand us. So at some point we should
    # consider revising our API here to be less annoying to explain to the type
    # checker.
    iam_client = cast(IAMClient, session.client('iam', region_name=region))
    iam_resource = cast(IAMServiceResource, session.resource('iam', region_name=region))
    boto_iam_connection = IAMConnection()
    role = iam_resource.Role(role_name)
    # normal policies
    for attached_policy in role.attached_policies.all():
        printq(f'Now dissociating policy: {attached_policy.policy_name} from role {role.name}', quiet)
        role.detach_policy(PolicyArn=attached_policy.arn)
    # inline policies
    for inline_policy in role.policies.all():
        printq(f'Deleting inline policy: {inline_policy.policy_name} from role {role.name}', quiet)
        # couldn't find an easy way to remove inline policies with boto3; use boto
        boto_iam_connection.delete_role_policy(role.name, inline_policy.policy_name)
    iam_client.delete_role(RoleName=role_name)
    printq(f'Role {role_name} successfully deleted.', quiet)
Beispiel #6
0
def delete_s3_bucket(bucket: str, region: Optional[str], quiet: bool = True):
    printq(f'Deleting s3 bucket in region "{region}": {bucket}', quiet)
    s3_client = aws.client('s3', region_name=region)
    s3_resource = aws.resource('s3', region_name=region)

    paginator = s3_client.get_paginator('list_object_versions')
    for response in paginator.paginate(Bucket=bucket):
        versions = response.get('Versions', []) + response.get(
            'DeleteMarkers', [])
        for version in versions:
            printq(
                f"    Deleting {version['Key']} version {version['VersionId']}",
                quiet)
            s3_client.delete_object(Bucket=bucket,
                                    Key=version['Key'],
                                    VersionId=version['VersionId'])
    s3_resource.Bucket(bucket).delete()
    printq(f'\n * Deleted s3 bucket successfully: {bucket}\n\n', quiet)
Beispiel #7
0
def delete_iam_role(role_name: str,
                    region: Optional[str] = None,
                    quiet: bool = True):
    from boto.iam.connection import IAMConnection
    iam_client = aws.client('iam', region_name=region)
    iam_resource = aws.resource('iam', region_name=region)
    boto_iam_connection = IAMConnection()
    role = iam_resource.Role(role_name)
    # normal policies
    for attached_policy in role.attached_policies.all():
        printq(
            f'Now dissociating policy: {attached_policy.name} from role {role.name}',
            quiet)
        role.detach_policy(PolicyName=attached_policy.name)
    # inline policies
    for attached_policy in role.policies.all():
        printq(
            f'Deleting inline policy: {attached_policy.name} from role {role.name}',
            quiet)
        # couldn't find an easy way to remove inline policies with boto3; use boto
        boto_iam_connection.delete_role_policy(role.name, attached_policy.name)
    iam_client.delete_role(RoleName=role_name)
    printq(f'Role {role_name} successfully deleted.', quiet)
Beispiel #8
0
def delete_sdb_domain(sdb_domain_name: str,
                      region: Optional[str] = None,
                      quiet: bool = True):
    sdb_client = aws.client('sdb', region_name=region)
    sdb_client.delete_domain(DomainName=sdb_domain_name)
    printq(f'SBD Domain: "{sdb_domain_name}" successfully deleted.', quiet)