/**
 * Copyright 2022 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
 # This file is only for testing purposes

# Creating 2 VMs, one in each service project
module "vm1" {
  source     = "../../../../modules/compute-vm"
  project_id = module.project-service-1.project_id
  region     = var.region
  name       = "test-vm"
  network_interfaces = [{
    network    = module.shared-vpc.self_link
    subnetwork = module.shared-vpc.subnet_self_links["${var.region}/subnet-01"]
    nat        = false
    addresses  = null
  }]
  tags = ["test-dns"]
}

module "vm2" {
  source     = "../../../../modules/compute-vm"
  project_id = module.project-service-2.project_id
  region     = var.region
  name       = "test-vm"
  network_interfaces = [{
    network    = module.shared-vpc.self_link
    subnetwork = module.shared-vpc.subnet_self_links["${var.region}/subnet-01"]
    nat        = false
    addresses  = null
  }]
  tags = ["test-dns"]
}

# Creating firewall rule for ICMP and SSH
resource "google_compute_firewall" "test-firewall" {
  name    = "test-firewall"
  project = module.project-host.project_id
  network = module.shared-vpc.self_link

  allow {
    protocol = "icmp"
  }

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

  target_tags = ["test-dns"]
}

# Creating DNS records
resource "google_dns_record_set" "record-vm1" {
  name         = "test.${var.teams[0]}.${var.dns_domain}."
  # Extracting project ID from the VPC self link
  project  = regex("/projects/(.*?)/.*", module.cloud-dns.teams_dns_networks[var.teams[0]].network.self_link)[0]
  managed_zone = var.teams[0]
  type         = "A"
  ttl          = 300

  rrdatas = [module.vm1.internal_ips[0]]
}

resource "google_dns_record_set" "record-vm2" {
  name         = "test.${var.teams[1]}.${var.dns_domain}."
  # Extracting project ID from the VPC self link
  project  = regex("/projects/(.*?)/.*", module.cloud-dns.teams_dns_networks[var.teams[1]].network.self_link)[0]
  managed_zone = var.teams[1]
  type         = "A"
  ttl          = 300

  rrdatas = [module.vm2.internal_ips[0]]
}

# Next step: SSH to the instances and ensure you can resolve the above DNS records

/* Example:
legranda@test-vm1:~$ ping test.appteam1.prod.internal
PING test.appteam1.prod.internal (10.10.1.2) 56(84) bytes of data.
64 bytes from test-vm1.europe-west1-c.c.test-dns-app-team1.internal (10.10.1.2): icmp_seq=1 ttl=64 time=0.020 ms
64 bytes from test-vm1.europe-west1-c.c.test-dns-app-team1.internal (10.10.1.2): icmp_seq=2 ttl=64 time=0.031 ms
^C
--- test.appteam1.prod.internal ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1029ms
rtt min/avg/max/mdev = 0.020/0.025/0.031/0.007 ms
legranda@test-vm1:~$ ping test.appteam2.prod.internal
PING test.appteam2.prod.internal (10.10.1.3) 56(84) bytes of data.
64 bytes from test-vm2.europe-west1-c.c.test-dns-app-team2.internal (10.10.1.3): icmp_seq=1 ttl=64 time=1.43 ms
64 bytes from test-vm2.europe-west1-c.c.test-dns-app-team2.internal (10.10.1.3): icmp_seq=2 ttl=64 time=0.207 ms
^C
--- test.appteam2.prod.internal ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.207/0.821/1.436/0.615 ms
*/