/**
 * Copyright 2023 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.
 */

locals {
  # Creates a map of additional clusters objects, including their parent private cloud
  additional_cluster_configs = merge(
    [for pcc_name, pcc in var.vmw_private_cloud_configs
      : { for cluster_name, cluster in pcc.additional_cluster_configs
        : "${cluster_name}" => merge(
          cluster,
          { parent = try(google_vmwareengine_private_cloud.vmw_engine_private_clouds[pcc_name].id, null) }
        )
      }
  ]...)
  vmw_network = (
    var.vmw_network_config.create
    ? try(google_vmwareengine_network.private_cloud_network.0, null)
    : try(data.google_vmwareengine_network.private_cloud_network.0, null)
  )
}

resource "google_vmwareengine_network" "private_cloud_network" {
  provider    = google-beta
  count       = var.vmw_network_config.create ? 1 : 0
  project     = var.project_id
  name        = "${var.prefix}-${var.vmw_network_config.name}"
  description = var.vmw_network_config.description
  location    = "global"
  type        = "STANDARD"
}

data "google_vmwareengine_network" "private_cloud_network" {
  provider = google-beta
  count    = var.vmw_network_config.create ? 0 : 1
  project  = var.project_id
  name     = "${var.prefix}-${var.vmw_network_config.name}"
  location = "global"
}

resource "google_vmwareengine_network_policy" "vmw_engine_network_policies" {
  provider              = google-beta
  for_each              = var.vmw_network_config.network_policies
  project               = var.project_id
  name                  = "${var.prefix}-${each.key}"
  description           = each.value.description
  edge_services_cidr    = each.value.edge_services_cidr
  location              = each.value.region
  vmware_engine_network = local.vmw_network.id

  external_ip {
    enabled = each.value.expose_on_internet
  }

  internet_access {
    enabled = each.value.outbound_internet_access
  }
}

resource "google_vmwareengine_network_peering" "vmw_engine_network_peerings" {
  provider                            = google-beta
  for_each                            = var.vmw_network_peerings
  project                             = var.project_id
  name                                = "${var.prefix}-${each.key}"
  description                         = each.value.description
  export_custom_routes                = each.value.export_custom_routes
  export_custom_routes_with_public_ip = each.value.export_custom_routes_with_public_ip
  import_custom_routes                = each.value.import_custom_routes
  import_custom_routes_with_public_ip = each.value.import_custom_routes_with_public_ip
  peer_network                        = each.value.peer_network
  peer_network_type                   = each.value.peer_to_vmware_engine_network ? "VMWARE_ENGINE_NETWORK" : "STANDARD"
  vmware_engine_network               = local.vmw_network.id
}

resource "google_vmwareengine_private_cloud" "vmw_engine_private_clouds" {
  provider    = google-beta
  for_each    = var.vmw_private_cloud_configs
  project     = var.project_id
  location    = each.value.zone
  name        = "${var.prefix}-${each.key}"
  description = each.value.description

  network_config {
    management_cidr       = each.value.cidr
    vmware_engine_network = local.vmw_network.id
  }

  management_cluster {
    cluster_id = "${var.prefix}-${each.key}-${each.value.management_cluster_config.name}"
    node_type_configs {
      node_type_id      = each.value.management_cluster_config.node_type_id
      node_count        = each.value.management_cluster_config.node_count
      custom_core_count = each.value.management_cluster_config.custom_core_count
    }
  }
}

resource "google_vmwareengine_cluster" "vmw_engine_additional_clusters" {
  provider = google-beta
  for_each = local.additional_cluster_configs
  name     = "${var.prefix}-${each.key}"
  parent   = each.value.parent
  node_type_configs {
    custom_core_count = each.value.custom_core_count
    node_count        = each.value.node_count
    node_type_id      = each.value.node_type_id
  }
}
