terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/fivetran/smart/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

resource "fivetran_proxy_agent" "proxy_agent" {
  display_name = "sme-smart-proxy-agent"
  group_region = "GCP_US_EAST4"
}

resource "fivetran_group" "group" {
  name = "${upper(var.environment)}_SMART"
}

resource "fivetran_destination" "destination" {
  group_id         = fivetran_group.group.id
  service          = "snowflake"
  time_zone_offset = "0"
  region           = "GCP_US_EAST4"
  run_setup_tests  = "true"

  config {
    auth            = "KEY_PAIR"
    host            = "orchard.snowflakecomputing.com"
    port            = 443
    user            = "PROD_FIVETRAN"
    database        = "SMART"
    connection_type = "Directly"
    private_key     = "dummy"
  }

  lifecycle {
    ignore_changes = [
      config.private_key
    ]
  }
}

resource "fivetran_connector" "connector" {
  group_id          = fivetran_group.group.id
  service           = "oracle"
  networking_method = "ProxyAgent"
  proxy_agent_id    = fivetran_proxy_agent.proxy_agent.id

  destination_schema {
    prefix = "smart"
  }

  # The SID is "DBOPSMA1" but the Fivetran provider does not appear to support this option.
  # Updated via the Fivetran UI after the connector was created.
  config {
    host             = "10.11.123.36"
    port             = "1481"
    user             = "hub_red"
    password         = "dummy"
  }

  lifecycle {
    ignore_changes = [
      config.password
    ]
  }
}

resource "fivetran_connector_schedule" "connector_schedule" {
  connector_id      = fivetran_connector.connector.id
  sync_frequency    = 60
  pause_after_trial = false
}
