resource "grafana_data_source" "datasources_ignore_and_protect" {
  for_each = local.datasources_ignore_and_protect

  name         = each.value.name
  access_mode  = each.value.access_mode
  type         = each.value.type
  uid          = each.value.uid
  url          = lookup(each.value, "url", null)
  username     = lookup(each.value, "username", null)
  is_default   = each.value.is_default
  org_id       = var.org_id
  http_headers = each.value.http_headers

  json_data_encoded = jsonencode(
    merge(
      each.value.json_data,
      {
        basicAuth        = lookup(each.value, "basic_auth", false),
        basicAuthUser    = lookup(each.value, "basic_auth_user", null),
        protocol         = lookup(each.value.json_data, "protocol", "http"),
        database         = lookup(each.value.json_data, "database", null),
        maxOpenConns     = tonumber(lookup(each.value.json_data, "maxOpenConns", null)),
        maxIdleConns     = tonumber(lookup(each.value.json_data, "maxIdleConns", null)),
        maxIdleConnsAuto = lookup(each.value.json_data, "maxIdleConnsAuto", null),
        connMaxLifetime  = tonumber(lookup(each.value.json_data, "connMaxLifetime", null))
      }
    )
  )

  secure_json_data_encoded = jsonencode(
    merge(
      each.value.secure_json_data,
      {
        basicAuthPassword = lookup(each.value.secure_json_data, "basicAuthPassword", null),
        password          = lookup(each.value.secure_json_data, "password", null)
      }
    )
  )

  lifecycle {
    ignore_changes  = [name, url, access_mode, is_default, json_data_encoded, secure_json_data_encoded]
    prevent_destroy = true
  }
}

resource "grafana_data_source" "datasources_ignore_only" {
  for_each = local.datasources_ignore_only

  name         = each.value.name
  access_mode  = each.value.access_mode
  type         = each.value.type
  uid          = each.value.uid
  url          = lookup(each.value, "url", null)
  username     = lookup(each.value, "username", null)
  is_default   = each.value.is_default
  org_id       = var.org_id
  http_headers = each.value.http_headers

  json_data_encoded = jsonencode(
    merge(
      each.value.json_data,
      {
        basicAuth        = lookup(each.value, "basic_auth", false),
        basicAuthUser    = lookup(each.value, "basic_auth_user", null),
        protocol         = lookup(each.value.json_data, "protocol", "http"),
        database         = lookup(each.value.json_data, "database", null),
        maxOpenConns     = tonumber(lookup(each.value.json_data, "maxOpenConns", null)),
        maxIdleConns     = tonumber(lookup(each.value.json_data, "maxIdleConns", null)),
        maxIdleConnsAuto = lookup(each.value.json_data, "maxIdleConnsAuto", null),
        connMaxLifetime  = tonumber(lookup(each.value.json_data, "connMaxLifetime", null))
      }
    )
  )

  secure_json_data_encoded = jsonencode(
    merge(
      each.value.secure_json_data,
      {
        basicAuthPassword = lookup(each.value.secure_json_data, "basicAuthPassword", null),
        password          = lookup(each.value.secure_json_data, "password", null)
      }
    )
  )

  lifecycle {
    ignore_changes = [name, url, access_mode, is_default, json_data_encoded, secure_json_data_encoded]
  }
}

resource "grafana_data_source" "datasources_protect_only" {
  for_each = local.datasources_protect_only

  name         = each.value.name
  access_mode  = each.value.access_mode
  type         = each.value.type
  uid          = each.value.uid
  url          = lookup(each.value, "url", null)
  username     = lookup(each.value, "username", null)
  is_default   = each.value.is_default
  org_id       = var.org_id
  http_headers = each.value.http_headers

  json_data_encoded = jsonencode(
    merge(
      each.value.json_data,
      {
        basicAuth        = lookup(each.value, "basic_auth", false),
        basicAuthUser    = lookup(each.value, "basic_auth_user", null),
        protocol         = lookup(each.value.json_data, "protocol", "http"),
        database         = lookup(each.value.json_data, "database", null),
        maxOpenConns     = tonumber(lookup(each.value.json_data, "maxOpenConns", null)),
        maxIdleConns     = tonumber(lookup(each.value.json_data, "maxIdleConns", null)),
        maxIdleConnsAuto = lookup(each.value.json_data, "maxIdleConnsAuto", null),
        connMaxLifetime  = tonumber(lookup(each.value.json_data, "connMaxLifetime", null))
      }
    )
  )

  secure_json_data_encoded = jsonencode(
    merge(
      each.value.secure_json_data,
      {
        basicAuthPassword = lookup(each.value.secure_json_data, "basicAuthPassword", null),
        password          = lookup(each.value.secure_json_data, "password", null)
      }
    )
  )

  lifecycle {
    prevent_destroy = true
  }
}

resource "grafana_data_source" "datasources_standard" {
  for_each = local.datasources_standard

  name         = each.value.name
  access_mode  = each.value.access_mode
  type         = each.value.type
  uid          = each.value.uid
  url          = lookup(each.value, "url", null)
  username     = lookup(each.value, "username", null)
  is_default   = each.value.is_default
  org_id       = var.org_id
  http_headers = each.value.http_headers


  json_data_encoded = jsonencode(
    merge(
      each.value.json_data,
      {
        basicAuth        = lookup(each.value, "basic_auth", false),
        basicAuthUser    = lookup(each.value, "basic_auth_user", null),
        protocol         = lookup(each.value.json_data, "protocol", "http"),
        database         = lookup(each.value.json_data, "database", null),
        maxOpenConns     = tonumber(lookup(each.value.json_data, "maxOpenConns", null)),
        maxIdleConns     = tonumber(lookup(each.value.json_data, "maxIdleConns", null)),
        maxIdleConnsAuto = lookup(each.value.json_data, "maxIdleConnsAuto", null),
        connMaxLifetime  = tonumber(lookup(each.value.json_data, "connMaxLifetime", null))
      }
    )
  )

  secure_json_data_encoded = jsonencode(
    merge(
      each.value.secure_json_data,
      {
        basicAuthPassword = lookup(each.value.secure_json_data, "basicAuthPassword", null),
        password          = lookup(each.value.secure_json_data, "password", null)
      }
    )
  )
}
