---
"Test geoip processor with defaults":
  - do:
      ingest.put_pipeline:
        id: "my_pipeline"
        body:  >
          {
            "description": "_description",
            "processors": [
              {
                "geoip" : {
                  "field" : "field1"
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      index:
        index: test
        type: test
        id: 1
        pipeline: "my_pipeline"
        body: {field1: "128.101.101.101"}

  - do:
      get:
        index: test
        type: test
        id: 1
  - match: { _source.field1: "128.101.101.101" }
  - length: { _source.geoip: 5 }
  - match: { _source.geoip.city_name: "Minneapolis" }
  - match: { _source.geoip.country_iso_code: "US" }
  - match: { _source.geoip.location.lon: -93.2166 }
  - match: { _source.geoip.location.lat: 44.9759 }
  - match: { _source.geoip.region_name: "Minnesota" }
  - match: { _source.geoip.continent_name: "North America" }

---
"Test geoip processor with fields":
  - do:
      cluster.health:
          wait_for_status: green

  - do:
      ingest.put_pipeline:
        id: "my_pipeline"
        body:  >
          {
            "description": "_description",
            "processors": [
              {
                "geoip" : {
                  "field" : "field1",
                  "properties" : ["city_name", "country_iso_code", "ip", "location", "timezone", "country_name", "region_name", "continent_name"]
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      index:
        index: test
        type: test
        id: 1
        pipeline: "my_pipeline"
        body: {field1: "128.101.101.101"}

  - do:
      get:
        index: test
        type: test
        id: 1
  - match: { _source.field1: "128.101.101.101" }
  - length: { _source.geoip: 8 }
  - match: { _source.geoip.city_name: "Minneapolis" }
  - match: { _source.geoip.country_iso_code: "US" }
  - match: { _source.geoip.ip: "128.101.101.101" }
  - match: { _source.geoip.location.lon: -93.2166 }
  - match: { _source.geoip.location.lat: 44.9759 }
  - match: { _source.geoip.timezone: "America/Chicago" }
  - match: { _source.geoip.country_name: "United States" }
  - match: { _source.geoip.region_name: "Minnesota" }
  - match: { _source.geoip.continent_name: "North America" }

---
"Test geoip processor with different database file - GeoLite2-Country":
  - do:
      ingest.put_pipeline:
        id: "my_pipeline"
        body:  >
          {
            "description": "_description",
            "processors": [
              {
                "geoip" : {
                  "field" : "field1",
                  "database_file" : "GeoLite2-Country.mmdb.gz"
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      index:
        index: test
        type: test
        id: 1
        pipeline: "my_pipeline"
        body: {field1: "128.101.101.101"}

  - do:
      get:
        index: test
        type: test
        id: 1
  - match: { _source.field1: "128.101.101.101" }
  - length: { _source.geoip: 2 }
  - match: { _source.geoip.country_iso_code: "US" }
  - match: { _source.geoip.continent_name: "North America" }

---
"Test geoip processor with geopoint mapping (both missing and including location)":
  - do:
      indices.create:
        index: test
        body:  >
          {
            "mappings" : {
              "test" : {
                "properties" : {
                  "geoip.location" : {
                    "type": "geo_point"
                  }
                }
              }
            }
          }
  - match: { acknowledged: true }

  - do:
      ingest.put_pipeline:
        id: "my_pipeline"
        body:  >
          {
            "description": "_description",
            "processors": [
              {
                "geoip" : {
                  "field" : "field1"
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      index:
        index: test
        type: test
        id: 1
        pipeline: "my_pipeline"
        body: { field1: "80.231.5.0" }

  - do:
      get:
        index: test
        type: test
        id: 1
  - match: { _source.field1: "80.231.5.0" }
  - is_false: _source.geoip

  - do:
      index:
        index: test
        type: test
        id: 2
        pipeline: "my_pipeline"
        body: { field1: "128.101.101.101" }

  - do:
      get:
        index: test
        type: test
        id: 2
  - match: { _source.field1: "128.101.101.101" }
  - length: { _source.geoip: 5 }
  - match: { _source.geoip.city_name: "Minneapolis" }
  - match: { _source.geoip.country_iso_code: "US" }
  - match: { _source.geoip.location.lon: -93.2166 }
  - match: { _source.geoip.location.lat: 44.9759 }
  - match: { _source.geoip.region_name: "Minnesota" }
  - match: { _source.geoip.continent_name: "North America" }

---
"Test geoip processor with different database file - GeoLite2-ASN":
  - do:
      ingest.put_pipeline:
        id: "my_pipeline"
        body:  >
          {
            "description": "_description",
            "processors": [
              {
                "geoip" : {
                  "field" : "field1",
                  "database_file" : "GeoLite2-ASN.mmdb.gz"
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      index:
        index: test
        type: test
        id: 1
        pipeline: "my_pipeline"
        body: {field1: "82.170.213.79"}

  - do:
      get:
        index: test
        type: test
        id: 1
  - match: { _source.field1: "82.170.213.79" }
  - length: { _source.geoip: 3 }
  - match: { _source.geoip.ip: "82.170.213.79" }
  - match: { _source.geoip.asn: 5615 }
  - match: { _source.geoip.organization_name: "KPN B.V." }
