ns1-terraform / terraform-provider-ns1

Terraform NS1 provider
https://www.terraform.io/docs/providers/ns1/
Mozilla Public License 2.0
31 stars 63 forks source link

Feature Request - Auto-Generated feed ID when creating ns1_monitoringjob #285

Closed packet-blaster closed 1 year ago

packet-blaster commented 1 year ago

When you create a Monitor (ns1_monitoringjob), an auto-generated feed is created in the background. When you create DNS records that use Filter Chains, they need to reference these auto-generated IDs to dynamically use the resources you've created through terraform. This output is currently unavailable from the ns1_monitoringjob resource.

example ns1_record:

resource "ns1_record" "example_a" {
    zone                = ns1_zone.zone["example.com"].zone
    domain           = "example.com"
    type                = "A"
    ttl                    = 300
    use_client_subnet   = false

    answers {
        answer  = "1.1.1.1"
        meta    = {
            up = jsonencode({
                feed = "11111111111111111111111"  # Manually inputting monitoring feed ID here
                })
        }
    }

    filters {
        filter   = "up"
    }
    filters {
        filter = "select_first_n"

        config = {
            N = 1
        }
    }
}

What the meta field should be able to do:

    answers {
        answer  = "1.1.1.1"
        meta    = {
            up = jsonencode({
                feed = ns1_monitoringjob.example.feed
                })
        }
    }
packet-blaster commented 1 year ago

I was intending to get around this my using a data feed data source. But datafeed is also unavailable as a data source and cannot be looked up.

packet-blaster commented 1 year ago

Resolved after speaking with NS1. The proper way to handle this is to create the monitoring data source/feed along with the monitoring job.

example provided by NS1

resource "ns1_datasource" "tf_ds" {
    sourcetype = "nsone_monitoring"
  name = "terraform_datasource"
}
resource "ns1_notifylist" "tf_nl" {
  name = "terraform_notify_list"
  notifications {
    config = {
      sourceid = ns1_datasource.tf_ds.id
    }
    type = "datafeed"
  }
}

resource "ns1_monitoringjob" "tf_job" {
  name = "terraform monitor"
  active = true
  regions = ["sjc", "dal", "lga", "gru", "lhr", "syd", "ams", "nrt", "sin"]
  job_type = "http"
  frequency = 30
  rapid_recheck = true
  policy = "quorum"
  config = {
    url = "https://50.116.48.14/"
    virtual_host = "bigblack.dog"
    tls_add_verify = true
    # tls_skip_verify = false
  }
  notify_list = ns1_notifylist.tf_nl.id
  notify_failback = true
}

resource "ns1_datafeed" "tf_feed" {
  name = "tf monitor feed"
  source_id = ns1_datasource.tf_ds.id
  config = {
    jobid = ns1_monitoringjob.tf_job.id
  }
}

resource "ns1_record" "bbd_meta" {
  zone = ns1_zone.bbd.zone
  domain = "up-filter-feed.${ns1_zone.bbd.zone}"
  type = "A"
  answers {
    answer = "2.2.2.2"
    meta = {
      us_state = "NH"
      up = "{\"feed\":\"${ns1_datafeed.tf_feed.id}\"}"
    }
  }
  answers {
    answer = "1.1.1.1"
    meta = {
      up = true
      us_state = "CA"
    }
  }
  filters {
    filter = "up"
  }
  filters {
    filter = "geotarget_country"
  }
  filters {
    filter = "select_first_n"
    config = {
      N = 1
    }
  }
}