/**
 * Copyright 2024 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 {
  # aggregate clusters into a single map and add 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.default[pcc_name].id,
          null
        )
      })
    }
  ]...)
  vmw_network = (
    var.vmw_network_config.create
    ? try(google_vmwareengine_network.default[0], null)
    : try(data.google_vmwareengine_network.default[0], null)
  )
}

moved {
  from = google_vmwareengine_network.private_cloud_network
  to   = google_vmwareengine_network.default
}

# network

resource "google_vmwareengine_network" "default" {
  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" "default" {
  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"
}

moved {
  from = google_vmwareengine_network_policy.vmw_engine_network_policies
  to   = google_vmwareengine_network_policy.default
}

# network policy

resource "google_vmwareengine_network_policy" "default" {
  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
  }
}

moved {
  from = google_vmwareengine_network_peering.vmw_engine_network_peerings
  to   = google_vmwareengine_network_peering.default
}

# network peerings

resource "google_vmwareengine_network_peering" "default" {
  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.routes_config.export
  export_custom_routes_with_public_ip = each.value.routes_config.public_export
  import_custom_routes                = each.value.routes_config.import
  import_custom_routes_with_public_ip = each.value.routes_config.public_import
  peer_network = trimprefix(
    each.value.peer_network,
    "https://www.googleapis.com/compute/v1/"
  )
  peer_network_type = (
    each.value.peer_to_vmware_engine_network
    ? "VMWARE_ENGINE_NETWORK"
    : "STANDARD"
  )
  vmware_engine_network = local.vmw_network.id
}

moved {
  from = google_vmwareengine_private_cloud.vmw_engine_private_clouds
  to   = google_vmwareengine_private_cloud.default
}

# private cloud

resource "google_vmwareengine_private_cloud" "default" {
  for_each    = var.vmw_private_cloud_configs
  project     = var.project_id
  location    = each.value.zone
  name        = "${var.prefix}-${each.key}"
  description = each.value.description
  type = (
    each.value.management_cluster_config.node_count == 1
    ? "TIME_LIMITED"
    : "STANDARD"
  )
  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
    }
  }
}

moved {
  from = google_vmwareengine_cluster.vmw_engine_additional_clusters
  to   = google_vmwareengine_cluster.default
}

# cluster

resource "google_vmwareengine_cluster" "default" {
  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
  }
}
