# VPC
resource "google_compute_network" "vpc" {
  name                    = "vpc"
  auto_create_subnetworks = false
}

# Sous-réseau
# front-end
resource "google_compute_subnetwork" "front-end" {
  name          = "front-end"
  network       = google_compute_network.vpc.id
  ip_cidr_range = "10.0.1.0/24"
  region        = "europe-west1"
}

# back-end
resource "google_compute_subnetwork" "back-end" {
  name          = "back-end"
  network       = google_compute_network.vpc.id
  ip_cidr_range = "10.0.1.0/24"
  region        = "europe-west1"
}

# data-base
resource "google_compute_subnetwork" "data-base" {
  name          = "data-base"
  network       = google_compute_network.vpc.id
  ip_cidr_range = "10.0.1.0/24"
  region        = "europe-west1"
}


# Règles de Pare-feu

#    - HTTP/HTTPS vers frontend
resource "google_compute_firewall" "allow-http" {
  name    = "allow-http"
  network = google_compute_network.vpc.id

  allow {
    protocol = "tcp"
    ports    = ["80", "443"]
  }

  source_ranges = ["0.0.0.0/0"]
  target_tags   = ["front-end"]
}

#    - SSH vers toutes les instances
resource "google_compute_firewall" "to-instances" {
  name    = "to-instances"
  network = google_compute_network.vpc.id

  allow {
    protocol = "tcp"
    ports    = ["22"]
  }

  source_ranges = ["0.0.0.0/0"]
}

#    - Port 8000 de frontend vers backend
resource "google_compute_firewall" "front-back" {
  name    = "front-back"
  network = google_compute_network.vpc.id

  allow {
    protocol = "tcp"
    ports    = ["8000"]
  }

  source_tags = ["front-end"]
  target_tags = ["back-end"]
}

#    - Port 3306 de backend vers database
resource "google_compute_firewall" "back-base" {
  name    = "back-base"
  network = google_compute_network.vpc.id

  allow {
    protocol = "tcp"
    ports    = ["3306"]
  }

  source_tags = ["back-end"]
  target_tags = ["data-base"]
}

