{% for svc in services %}
resource "google_cloud_run_v2_service" "{{ svc.key }}" {
  name     = "{{ svc.name }}"
  location = var.region
  project  = var.project_id

  template {
    service_account = google_service_account.orbit.email

    scaling {
      min_instance_count = {{ svc.spec.min_instances }}
      max_instance_count = {{ svc.spec.max_instances }}
    }

    vpc_access {
      connector = google_vpc_access_connector.orbit.id
      egress    = "PRIVATE_RANGES_ONLY"
    }

    containers {
      image = "{{ svc.image }}"

      resources {
        limits = {
          cpu    = "{{ svc.spec.cpu }}"
          memory = "{{ svc.spec.memory }}"
        }
      }

      env {
        name  = "ASTROMESH_DATABASE_URL"
        value = "postgresql+asyncpg://astromesh:${random_password.db_password.result}@/astromesh?host=/cloudsql/${google_sql_database_instance.main.connection_name}"
      }
      env {
        name  = "ASTROMESH_REDIS_URL"
        value = "redis://${google_redis_instance.main.host}:${google_redis_instance.main.port}"
      }

      volume_mounts {
        name       = "cloudsql"
        mount_path = "/cloudsql"
      }
    }

    volumes {
      name = "cloudsql"
      cloud_sql_instance {
        instances = [google_sql_database_instance.main.connection_name]
      }
    }
  }
}

{% endfor %}
