# Database Migration Service requires the below IAM Roles to be created before
# replication instances can be created. See the DMS Documentation for
# additional information: https://docs.aws.amazon.com/dms/latest/userguide/CHAP_Security.html#CHAP_Security.APIRole
#  * dms-vpc-role
#  * dms-cloudwatch-logs-role
#  * dms-access-for-endpoint

data "aws_iam_policy_document" "dms_assume_role" {
  statement {
    actions = ["sts:AssumeRole"]

    principals {
      identifiers = [
        "dms.amazonaws.com",
        "dms.eu-west-1.amazonaws.com",
      ]
      type = "Service"
    }
  }
}

resource "aws_iam_role" "dms-access-for-endpoint" {
  assume_role_policy = data.aws_iam_policy_document.dms_assume_role.json
  name               = "dms-access-for-endpoint"
}

resource "aws_iam_role_policy_attachment" "dms-access-for-endpoint-AmazonDMSRedshiftS3Role" {
  policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonDMSRedshiftS3Role"
  role       = aws_iam_role.dms-access-for-endpoint.name
}

resource "aws_iam_role_policy_attachment" "dms-access-for-endpoint-secrets" {
  policy_arn = "arn:aws:iam::322697034778:policy/whitelist-secret-reader"
  role       = aws_iam_role.dms-access-for-endpoint.name
}

resource "aws_iam_role_policy_attachment" "dms-access-for-endpoint-s3" {
  policy_arn = module.iam_s3_dms_replica_rw.policy.arn
  role       = aws_iam_role.dms-access-for-endpoint.name
}

resource "aws_iam_role" "dms-cloudwatch-logs-role" {
  assume_role_policy = data.aws_iam_policy_document.dms_assume_role.json
  name               = "dms-cloudwatch-logs-role"
}

resource "aws_iam_role_policy_attachment" "dms-cloudwatch-logs-role-AmazonDMSCloudWatchLogsRole" {
  policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonDMSCloudWatchLogsRole"
  role       = aws_iam_role.dms-cloudwatch-logs-role.name
}

resource "aws_iam_role" "dms-vpc-role" {
  assume_role_policy = data.aws_iam_policy_document.dms_assume_role.json
  name               = "dms-vpc-role"
}

resource "aws_iam_role_policy_attachment" "dms-vpc-role-AmazonDMSVPCManagementRole" {
  policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonDMSVPCManagementRole"
  role       = aws_iam_role.dms-vpc-role.name
}


resource "aws_dms_replication_subnet_group" "this" {
  replication_subnet_group_description = "replication subnet group"
  replication_subnet_group_id          = "${local.name_prefix}-group1"

  subnet_ids = [
    "subnet-4d36ad04",
    "subnet-6e0ad535",
  ]

  tags = {
    project                  = "Infrastructure",
    service                  = "DMS",
    plat_env_project_service = "${local.aggregated_tag}_INFRA_DMS"
  }

  depends_on = [
    aws_iam_role_policy_attachment.dms-access-for-endpoint-AmazonDMSRedshiftS3Role,
    aws_iam_role_policy_attachment.dms-cloudwatch-logs-role-AmazonDMSCloudWatchLogsRole,
    aws_iam_role_policy_attachment.dms-vpc-role-AmazonDMSVPCManagementRole
  ]
}

# Create a new replication instance
/* resource "aws_dms_replication_instance" "test" {
  allocated_storage          = 25
  apply_immediately          = true
  auto_minor_version_upgrade = true
  availability_zone          = "eu-west-1a"
  #engine_version               = "3.1.4"
  #kms_key_arn                  = "arn:aws:kms:us-east-1:123456789012:key/12345678-1234-1234-1234-123456789012"
  multi_az                     = false
  preferred_maintenance_window = "sun:10:30-sun:14:30"
  publicly_accessible          = true
  replication_instance_class   = "dms.t3.medium"
  replication_instance_id      = "${local.name_prefix}-dms"
  replication_subnet_group_id  = aws_dms_replication_subnet_group.this.id

  tags = {
    project                  = "Infrastructure",
    service                  = "DMS",
    plat_env_project_service = "${local.aggregated_tag}_INFRA_DMS"
  }

  vpc_security_group_ids = [
    aws_security_group.dms.id,
  ]

  depends_on = [
    aws_iam_role_policy_attachment.dms-access-for-endpoint-AmazonDMSRedshiftS3Role,
    aws_iam_role_policy_attachment.dms-cloudwatch-logs-role-AmazonDMSCloudWatchLogsRole,
    aws_iam_role_policy_attachment.dms-vpc-role-AmazonDMSVPCManagementRole
  ]
}

resource "aws_dms_endpoint" "postgres" {
  database_name = "wl_us_ultra"
  endpoint_id   = "${local.name_prefix}-whitelist-db"
  endpoint_type = "source"
  engine_name   = "postgres"
  ssl_mode      = "none"

  secrets_manager_access_role_arn = aws_iam_role.dms-access-for-endpoint.arn
  secrets_manager_arn             = "arn:aws:secretsmanager:eu-west-1:322697034778:secret:DatabaseCredsDMS-MDwmmT"

  tags = {
    project                  = "Infrastructure",
    service                  = "DMS",
    plat_env_project_service = "${local.aggregated_tag}_INFRA_DMS"
  }
} */
/*
resource "aws_dms_endpoint" "s3" {
  endpoint_id   = "${local.name_prefix}-s3-target"
  endpoint_type = "target"
  engine_name   = "s3"

  s3_settings {
    bucket_folder           = "wl_us_ultra"
    bucket_name             = module.s3_dms_replication.bucket.id
    compression_type        = "NONE"
    csv_delimiter           = "\\t"
    csv_row_delimiter       = "\\n"
    enable_statistics       = true
    timestamp_column_name   = "tx_timestamp"
    date_partition_enabled  = false
    service_access_role_arn = aws_iam_role.dms-access-for-endpoint.arn
  }

  tags = {
    project                  = "Infrastructure",
    service                  = "DMS",
    plat_env_project_service = "${local.aggregated_tag}_INFRA_DMS"
  }
}

# Create a new replication task
resource "aws_dms_replication_task" "this" {
  #cdc_start_time           = 1484346880
  migration_type           = "full-load-and-cdc"
  replication_instance_arn = aws_dms_replication_instance.test.replication_instance_arn
  replication_task_id      = "${local.name_prefix}-migration-task"
  source_endpoint_arn      = aws_dms_endpoint.postgres.endpoint_arn
  target_endpoint_arn      = aws_dms_endpoint.s3.endpoint_arn
  start_replication_task   = true

  replication_task_settings = jsonencode(
    {
      BeforeImageSettings = null
      ChangeProcessingDdlHandlingPolicy = {
        HandleSourceTableAltered   = true
        HandleSourceTableDropped   = true
        HandleSourceTableTruncated = true
      }
      ChangeProcessingTuning = {
        BatchApplyMemoryLimit         = 500
        BatchApplyPreserveTransaction = true
        BatchApplyTimeoutMax          = 30
        BatchApplyTimeoutMin          = 1
        BatchSplitSize                = 0
        CommitTimeout                 = 1
        MemoryKeepTime                = 60
        MemoryLimitTotal              = 1024
        MinTransactionSize            = 1000
        StatementCacheSize            = 50
      }
      CharacterSetSettings = null
      ControlTablesSettings = {
        ControlSchema                 = ""
        FullLoadExceptionTableEnabled = false
        HistoryTableEnabled           = false
        HistoryTimeslotInMinutes      = 5
        StatusTableEnabled            = false
        SuspendedTablesTableEnabled   = false
      }
      ErrorBehavior = {
        ApplyErrorDeletePolicy                      = "IGNORE_RECORD"
        ApplyErrorEscalationCount                   = 0
        ApplyErrorEscalationPolicy                  = "LOG_ERROR"
        ApplyErrorFailOnTruncationDdl               = false
        ApplyErrorInsertPolicy                      = "LOG_ERROR"
        ApplyErrorUpdatePolicy                      = "LOG_ERROR"
        DataErrorEscalationCount                    = 0
        DataErrorEscalationPolicy                   = "SUSPEND_TABLE"
        DataErrorPolicy                             = "LOG_ERROR"
        DataTruncationErrorPolicy                   = "LOG_ERROR"
        EventErrorPolicy                            = null
        FailOnNoTablesCaptured                      = true
        FailOnTransactionConsistencyBreached        = false
        FullLoadIgnoreConflicts                     = true
        RecoverableErrorCount                       = -1
        RecoverableErrorInterval                    = 5
        RecoverableErrorStopRetryAfterThrottlingMax = true
        RecoverableErrorThrottling                  = true
        RecoverableErrorThrottlingMax               = 1800
        TableErrorEscalationCount                   = 0
        TableErrorEscalationPolicy                  = "STOP_TASK"
        TableErrorPolicy                            = "SUSPEND_TABLE"
      }
      FailTaskWhenCleanTaskResourceFailed = false
      FullLoadSettings = {
        CommitRate                      = 10000
        CreatePkAfterFullLoad           = false
        MaxFullLoadSubTasks             = 8
        StopTaskCachedChangesApplied    = false
        StopTaskCachedChangesNotApplied = false
        TargetTablePrepMode             = "DO_NOTHING"
        TransactionConsistencyTimeout   = 600
      }
      Logging = {
        EnableLogging = false
        LogComponents = [
          {
            Id       = "TRANSFORMATION"
            Severity = "LOGGER_SEVERITY_DEFAULT"
          },
          {
            Id       = "SOURCE_UNLOAD"
            Severity = "LOGGER_SEVERITY_DEFAULT"
          },
          {
            Id       = "IO"
            Severity = "LOGGER_SEVERITY_DEFAULT"
          },
          {
            Id       = "TARGET_LOAD"
            Severity = "LOGGER_SEVERITY_DEFAULT"
          },
          {
            Id       = "PERFORMANCE"
            Severity = "LOGGER_SEVERITY_DEFAULT"
          },
          {
            Id       = "SOURCE_CAPTURE"
            Severity = "LOGGER_SEVERITY_DEFAULT"
          },
          {
            Id       = "SORTER"
            Severity = "LOGGER_SEVERITY_DEFAULT"
          },
          {
            Id       = "REST_SERVER"
            Severity = "LOGGER_SEVERITY_DEFAULT"
          },
          {
            Id       = "VALIDATOR_EXT"
            Severity = "LOGGER_SEVERITY_DEFAULT"
          },
          {
            Id       = "TARGET_APPLY"
            Severity = "LOGGER_SEVERITY_DEFAULT"
          },
          {
            Id       = "TASK_MANAGER"
            Severity = "LOGGER_SEVERITY_DEFAULT"
          },
          {
            Id       = "TABLES_MANAGER"
            Severity = "LOGGER_SEVERITY_DEFAULT"
          },
          {
            Id       = "METADATA_MANAGER"
            Severity = "LOGGER_SEVERITY_DEFAULT"
          },
          {
            Id       = "FILE_FACTORY"
            Severity = "LOGGER_SEVERITY_DEFAULT"
          },
          {
            Id       = "COMMON"
            Severity = "LOGGER_SEVERITY_DEFAULT"
          },
          {
            Id       = "ADDONS"
            Severity = "LOGGER_SEVERITY_DEFAULT"
          },
          {
            Id       = "DATA_STRUCTURE"
            Severity = "LOGGER_SEVERITY_DEFAULT"
          },
          {
            Id       = "COMMUNICATION"
            Severity = "LOGGER_SEVERITY_DEFAULT"
          },
          {
            Id       = "FILE_TRANSFER"
            Severity = "LOGGER_SEVERITY_DEFAULT"
          },
        ]
      }
      LoopbackPreventionSettings = null
      PostProcessingRules        = null
      StreamBufferSettings = {
        CtrlStreamBufferSizeInMB = 5
        StreamBufferCount        = 3
        StreamBufferSizeInMB     = 8
      }
      TTSettings = {
        EnableTT         = false
        TTRecordSettings = null
        TTS3Settings     = null
      }
      TargetMetadata = {
        BatchApplyEnabled            = false
        FullLobMode                  = false
        InlineLobMaxSize             = 0
        LimitedSizeLobMode           = true
        LoadMaxFileSize              = 0
        LobChunkSize                 = 0
        LobMaxSize                   = 1024
        ParallelApplyBufferSize      = 0
        ParallelApplyQueuesPerThread = 0
        ParallelApplyThreads         = 0
        ParallelLoadBufferSize       = 0
        ParallelLoadQueuesPerThread  = 0
        ParallelLoadThreads          = 0
        SupportLobs                  = true
        TargetSchema                 = ""
        TaskRecoveryTableEnabled     = false
      }
    }
  )

  table_mappings = jsonencode(
    {
      rules = [
        {
          filters = []
          object-locator = {
            schema-name = "public"
            table-name  = "sc_users"
          }
          rule-action = "include"
          rule-id     = "466928647"
          rule-name   = "050296212"
          rule-type   = "selection"
        },
        {
          filters = []
          object-locator = {
            schema-name = "public"
            table-name  = "sc_tracks_archived"
          }
          rule-action = "include"
          rule-id     = "964000425"
          rule-name   = "050296212"
          rule-type   = "selection"
        },
        {
          filters = []
          object-locator = {
            schema-name = "public"
            table-name  = "sc_track_stats_%"
          }
          rule-action = "include"
          rule-id     = "050303346"
          rule-name   = "050303346"
          rule-type   = "selection"
        },
        {
          filters = []
          object-locator = {
            schema-name = "public"
            table-name  = "sc_tracks"
          }
          rule-action = "include"
          rule-id     = "050296212"
          rule-name   = "050296212"
          rule-type   = "selection"
        },
        {
          filters = []
          object-locator = {
            schema-name = "public"
            table-name  = "sc_artists_stats"
          }
          rule-action = "include"
          rule-id     = "050285663"
          rule-name   = "050285663"
          rule-type   = "selection"
        },
        {
          filters = []
          object-locator = {
            schema-name = "public"
            table-name  = "yt_channels"
          }
          rule-action = "include"
          rule-id     = "050275583"
          rule-name   = "050275583"
          rule-type   = "selection"
        },
        {
          filters = []
          object-locator = {
            schema-name = "public"
            table-name  = "yt_channel_videos"
          }
          rule-action = "include"
          rule-id     = "050259451"
          rule-name   = "050259451"
          rule-type   = "selection"
        },
        {
          filters = []
          object-locator = {
            schema-name = "public"
            table-name  = "yt_artist_channels"
          }
          rule-action = "include"
          rule-id     = "050246806"
          rule-name   = "050246806"
          rule-type   = "selection"
        },
        {
          filters = []
          object-locator = {
            schema-name = "public"
            table-name  = "yt_artist_channel_stats"
          }
          rule-action = "include"
          rule-id     = "050238638"
          rule-name   = "050238638"
          rule-type   = "selection"
        },
        {
          filters = []
          object-locator = {
            schema-name = "public"
            table-name  = "yt_videos"
          }
          rule-action = "include"
          rule-id     = "050221295"
          rule-name   = "050221295"
          rule-type   = "selection"
        },
        {
          filters = []
          object-locator = {
            schema-name = "public"
            table-name  = "yt_video_statistics"
          }
          rule-action = "include"
          rule-id     = "050194464"
          rule-name   = "050194464"
          rule-type   = "selection"
        },
      ]
    }
  )

  tags = {
    project                  = "Infrastructure",
    service                  = "DMS",
    plat_env_project_service = "${local.aggregated_tag}_INFRA_DMS"
  }
} */
