terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~>3.0"
    }
  }
}

variable "tidb_count" {
  description = "Number of TiDB instances"
  type        = number
  default     = 9
}

variable "tikv_count" {
  description = "Number of TiKV instances"
  type        = number
  default     = 36
}

variable "pd_count" {
  description = "Number of PD instances"
  type        = number
  default     = 9
}

provider "azurerm" {
  subscription_id = "efe59dff-e62f-4356-b93d-de124c967a1f" # prod
  # subscription_id = "99549169-6cee-4263-8491-924a3011ee31" # dev
  features {}
}

resource "azurerm_resource_group" "quota-verification" {
  name     = "quota-verification-resources"
  location = "East US 2"
  # location = "East US"
}

resource "azurerm_virtual_network" "vnet" {
  name                = "quota-verification-vnet"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.quota-verification.location
  resource_group_name = azurerm_resource_group.quota-verification.name
}

resource "azurerm_subnet" "subnet" {
  name                 = "quota-verification-subnet"
  resource_group_name  = azurerm_resource_group.quota-verification.name
  virtual_network_name = azurerm_virtual_network.vnet.name
  address_prefixes     = ["10.0.1.0/24"]
}

resource "azurerm_network_interface" "tidb_eni" {
  count               = var.tidb_count
  name                = "tidb-nic-${count.index}"
  location            = azurerm_resource_group.quota-verification.location
  resource_group_name = azurerm_resource_group.quota-verification.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.subnet.id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_linux_virtual_machine" "tidb" {
  count               = var.tidb_count
  name                = "tidb-vm-${count.index}"
  location            = azurerm_resource_group.quota-verification.location
  resource_group_name = azurerm_resource_group.quota-verification.name
  network_interface_ids = [
    azurerm_network_interface.tidb_eni[count.index].id
  ]
  size = "Standard_D16as_v5"
  zone = count.index % 3 + 1

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

  source_image_reference {
    publisher = "Canonical"
    offer     = "0001-com-ubuntu-server-focal"
    sku       = "20_04-lts-gen2"
    version   = "latest"
  }

  disable_password_authentication = false
  admin_username                  = "adminuser"
  admin_password                  = "sd98awQWIej13214!"
}

resource "azurerm_network_interface" "tikv_eni" {
  count               = var.tikv_count
  name                = "tikv-nic-${count.index}"
  location            = azurerm_resource_group.quota-verification.location
  resource_group_name = azurerm_resource_group.quota-verification.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.subnet.id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_linux_virtual_machine" "tikv" {
  count               = var.tikv_count
  name                = "tikv-vm-${count.index}"
  location            = azurerm_resource_group.quota-verification.location
  resource_group_name = azurerm_resource_group.quota-verification.name
  network_interface_ids = [
    azurerm_network_interface.tikv_eni[count.index].id
  ]
  size = "Standard_D16as_v5"
  zone = count.index % 3 + 1

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

  source_image_reference {
    publisher = "Canonical"
    offer     = "0001-com-ubuntu-server-focal"
    sku       = "20_04-lts-gen2"
    version   = "latest"
  }

  disable_password_authentication = false
  admin_username                  = "adminuser"
  admin_password                  = "sd98awQWIej13214!"
}

resource "azurerm_network_interface" "pd_eni" {
  count               = var.pd_count
  name                = "pd-nic-${count.index}"
  location            = azurerm_resource_group.quota-verification.location
  resource_group_name = azurerm_resource_group.quota-verification.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.subnet.id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_linux_virtual_machine" "pd" {
  count               = var.pd_count
  name                = "pd-vm-${count.index}"
  location            = azurerm_resource_group.quota-verification.location
  resource_group_name = azurerm_resource_group.quota-verification.name
  network_interface_ids = [
    azurerm_network_interface.pd_eni[count.index].id
  ]
  size = "Standard_D4as_v5"
  zone = count.index % 3 + 1

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

  source_image_reference {
    publisher = "Canonical"
    offer     = "0001-com-ubuntu-server-focal"
    sku       = "20_04-lts-gen2"
    version   = "latest"
  }

  disable_password_authentication = false
  admin_username                  = "adminuser"
  admin_password                  = "sd98awQWIej13214!"
}