# Path: stacker_blueprints/policies.py
# def s3_arn(bucket):
#     if isinstance(bucket, AWSHelperFn):
#         return Sub('arn:aws:s3:::${Bucket}', Bucket=bucket)
#     else:
#         return 'arn:aws:s3:::%s' % bucket
# 
# def write_to_cloudwatch_logs_stream_statements(log_group_name,
#                                                log_stream_name):
#     return [
#         Statement(
#             Effect=Allow,
#             Action=[logs.PutLogEvents],
#             Resource=[log_stream_arn(log_group_name, log_stream_name)]
#         )
#     ]
# 
# Path: stacker_blueprints/cloudwatch_logs.py
# LOG_RETENTION_STRINGS = [str(x) for x in LOG_RETENTION_VALUES]
# 
# def validate_cloudwatch_log_retention(value):
#     if value not in LOG_RETENTION_VALUES:
#         raise ValueError(
#             "%d is not a valid retention period. Must be one of: %s" % (
#                 value,
#                 ', '.join(LOG_RETENTION_STRINGS)
#             )
#         )
#     return value


from awacs.aws import (
    Allow,
    Condition,
    Policy,
    Statement,
    StringEquals,
    StringLike,
)
from awacs.helpers.trust import make_simple_assume_statement
from stacker.blueprints.base import Blueprint
from troposphere import (
    iam,
    logs,
    firehose,
    Join,
    GetAtt,
    Output,
    Ref,
    Sub,
)
from ..policies import (
    s3_arn,
    write_to_cloudwatch_logs_stream_statements,
)
from ..cloudwatch_logs import (
    LOG_RETENTION_STRINGS,
    validate_cloudwatch_log_retention,
)
import awacs.logs
import awacs.s3
import awacs.firehose
import awacs.kms
LOG_GROUP = "LogGroup"
S3_LOG_STREAM = "S3LogStream"
ROLE = "Role"

REGION = Ref("AWS::Region")
NOVALUE = Ref("AWS::NoValue")


def make_simple_assume_policy(*principals):
    return Policy(
        Statement=[
            make_simple_assume_statement(*principals)
        ]
    )


def s3_write_statements(bucket_name):
    return [
        Statement(
            Effect=Allow,
            Action=[
                awacs.s3.AbortMultipartUpload,
                awacs.s3.GetBucketLocation,
                awacs.s3.GetObject,
                awacs.s3.ListBucket,
                awacs.s3.ListBucketMultipartUploads,
                awacs.s3.PutObject,
            ],
            Resource=[
                s3_arn(bucket_name),

# Path: stacker_blueprints/policies.py
# def s3_arn(bucket):
#     if isinstance(bucket, AWSHelperFn):
#         return Sub('arn:aws:s3:::${Bucket}', Bucket=bucket)
#     else:
#         return 'arn:aws:s3:::%s' % bucket
# 
# def write_to_cloudwatch_logs_stream_statements(log_group_name,
#                                                log_stream_name):
#     return [
#         Statement(
#             Effect=Allow,
#             Action=[logs.PutLogEvents],
#             Resource=[log_stream_arn(log_group_name, log_stream_name)]
#         )
#     ]
# 
# Path: stacker_blueprints/cloudwatch_logs.py
# LOG_RETENTION_STRINGS = [str(x) for x in LOG_RETENTION_VALUES]
# 
# def validate_cloudwatch_log_retention(value):
#     if value not in LOG_RETENTION_VALUES:
#         raise ValueError(
#             "%d is not a valid retention period. Must be one of: %s" % (
#                 value,
#                 ', '.join(LOG_RETENTION_STRINGS)
#             )
#         )
#     return value


# Path: stacker_blueprints/firehose/base.py
from awacs.aws import (
    Allow,
    Condition,
    Policy,
    Statement,
    StringEquals,
    StringLike,
)
from awacs.helpers.trust import make_simple_assume_statement
from stacker.blueprints.base import Blueprint
from troposphere import (
    iam,
    logs,
    firehose,
    Join,
    GetAtt,
    Output,
    Ref,
    Sub,
)
from ..policies import (
    s3_arn,
    write_to_cloudwatch_logs_stream_statements,
)
from ..cloudwatch_logs import (
    LOG_RETENTION_STRINGS,
    validate_cloudwatch_log_retention,
)
import awacs.logs
import awacs.s3
import awacs.firehose
import awacs.kms

LOG_GROUP = "LogGroup"
S3_LOG_STREAM = "S3LogStream"
ROLE = "Role"

REGION = Ref("AWS::Region")
NOVALUE = Ref("AWS::NoValue")


def make_simple_assume_policy(*principals):
    return Policy(
        Statement=[
            make_simple_assume_statement(*principals)
        ]
    )


def s3_write_statements(bucket_name):
    return [
        Statement(
            Effect=Allow,
            Action=[
                awacs.s3.AbortMultipartUpload,
                awacs.s3.GetBucketLocation,
                awacs.s3.GetObject,
                awacs.s3.ListBucket,
                awacs.s3.ListBucketMultipartUploads,
                awacs.s3.PutObject,
            ],
            Resource=[
                s3_arn(bucket_name),

                s3_arn(Join("/", [bucket_name, "*"]))