terraform {
  required_providers {
    docker = {
      source = "kreuzwerker/docker"
      version = "3.0.1"
    }
  }
}

provider "docker" {
      host    = "unix:///var/run/docker.sock"
}


resource "docker_image" "webup-image" {
  name = "${var.webup_image_name}-${var.webup_payara_version}"
  build {
    context = "./docker"
    build_args = {
    	webup_payara_version = var.webup_payara_version
    	webup_asadmin_Xms = var.webup_asadmin_Xms,
	    webup_asadmin_Xmx = var.webup_asadmin_Xmx,
	    webup_asadmin_MetaspaceSize = var.webup_asadmin_MetaspaceSize,
	    webup_asadmin_MaxMetaspaceSize = var.webup_asadmin_MaxMetaspaceSize,
	    webup_asadmin_rejectClientInitiatedRenegotiation = var.webup_asadmin_rejectClientInitiatedRenegotiation,
	    webup_asadmin_classloadingDelegate = var.webup_asadmin_classloadingDelegate,
	    webup_asadmin_GC = var.webup_asadmin_GC,
		webup_asadmin_stringDuplication = var.webup_asadmin_stringDuplication,
		webup_asadmin_disableExplicitGC = var.webup_asadmin_disableExplicitGC,
		webup_asadmin_autodeployEnabled = var.webup_asadmin_autodeployEnabled,
		webup_asadmin_dynamicReload = var.webup_asadmin_dynamicReload,
		webup_asadmin_poolResizeQuantity = var.webup_asadmin_poolResizeQuantity,
		webup_asadmin_maxPoolSize = var.webup_asadmin_maxPoolSize,
		webup_asadmin_steadyPoolSize = var.webup_asadmin_steadyPoolSize,
		webup_asadmin_minThreadPoolSize = var.webup_asadmin_minThreadPoolSize,
		webup_asadmin_maxThreadPoolSize = var.webup_asadmin_maxThreadPoolSize,
		webup_asadmin_httpListener1Enabled = var.webup_asadmin_httpListener1Enabled,
		webup_asadmin_httpListener2Enabled = var.webup_asadmin_httpListener2Enabled,
		webup_asadmin_httpMaxConnections = var.webup_asadmin_httpMaxConnections,
		webup_asadmin_httpTimeoutSeconds = var.webup_asadmin_httpTimeoutSeconds,
		webup_asadmin_httpFileCacheMaxAgeSeconds = var.webup_asadmin_httpFileCacheMaxAgeSeconds,
		webup_asadmin_httpFileCacheEnabled = var.webup_asadmin_httpFileCacheMaxAgeSeconds,
		webup_asadmin_acceptorThreads = var.webup_asadmin_acceptorThreads,
		webup_asadmin_httpThreadPoolMinThreadPoolSize = var.webup_asadmin_httpThreadPoolMinThreadPoolSize,
		webup_asadmin_httpThreadPoolMaxThreadPoolSize = var.webup_asadmin_httpThreadPoolMaxThreadPoolSize,
		webup_asadmin_loggingGFFileHandlerLogtoFile = var.webup_asadmin_loggingGFFileHandlerLogtoFile,
		webup_asadmin_loggingGFFileHandlerRotationLimitInBytes = var.webup_asadmin_loggingGFFileHandlerRotationLimitInBytes,
		webup_asadmin_loggingGFFileHandlerMaxHistoryFiles = var.webup_asadmin_loggingGFFileHandlerMaxHistoryFiles,
		webup_asadmin_hazelcastEnabled = var.webup_asadmin_hazelcastEnabled,
		webup_asadmin_hazelcastDynamic = var.webup_asadmin_hazelcastDynamic,
		webup_asadmin_httpMaxFormPostSizeBytes = var.webup_asadmin_httpMaxFormPostSizeBytes,
		webup_asadmin_httpXpoweredBy = var.webup_asadmin_httpXpoweredBy,
		webup_asadmin_httpSchemeMapping = var.webup_asadmin_httpSchemeMapping,
		webup_asadmin_disableJSPServletDevelopment = var.webup_asadmin_disableJSPServletDevelopment,
		webup_asadmin_JSPServletStrAsCharArray = var.webup_asadmin_JSPServletStrAsCharArray
		webup_asadmin_dumpIfOutOfMemory = var.webup_asadmin_dumpIfOutOfMemory
		webup_asadmin_jfr = var.webup_asadmin_jfr
    }
  }
}

resource "docker_container" "webup-container" {

  image   = "${var.webup_image_name}-${var.webup_payara_version}"
  restart = "unless-stopped"
  name    = var.webup_container_name
  network_mode = var.webup_network_mode 

  ports {
    internal = "8080"
    ip = "127.0.0.1"
    external = var.webup_http_external_port
  }

  ports {
    internal = "8081"
    ip = "127.0.0.1"
    external = var.webup_https_external_port
  }
 
  ports {
    internal = "4848"
    ip = "127.0.0.1"
    external = var.webup_asadmin_external_port
  }
    
  volumes {
    volume_name    = var.webup_share_0
    container_path = "/mnt/share_0"
  }

  volumes {
    volume_name    = var.webup_share_1
    container_path = "/mnt/share_1"
  }

  volumes {
    volume_name    = var.webup_share_2
    container_path = "/mnt/share_2"
  }

  volumes {
    volume_name    = var.webup_share_3
    container_path = "/mnt/share_3"
  }

  volumes {
    volume_name    = var.webup_share_4
    container_path = "/mnt/share_4"
  }
  
  volumes {
    volume_name    = var.webup_share_5
    container_path = "/mnt/share_5"
  }
  
  volumes {
    volume_name    = var.webup_share_6
    container_path = "/mnt/share_6"
  }
  
  volumes {
    volume_name    = var.webup_share_7
    container_path = "/mnt/share_7"
  }
  
  volumes {
    volume_name    = var.webup_share_8
    container_path = "/mnt/share_8"
  }
  
  volumes {
    volume_name    = var.webup_share_9
    container_path = "/mnt/share_9"
  }
  
  volumes {
    volume_name    = var.webup_share_10
    container_path = "/mnt/share_10"
  }
  
  volumes {
    volume_name    = var.webup_share_11
    container_path = "/mnt/share_11"
  }
  
  volumes {
    volume_name    = var.webup_share_12
    container_path = "/mnt/share_12"
  }
  
  volumes {
    volume_name    = var.webup_share_13
    container_path = "/mnt/share_13"
  }
  
  volumes {
    volume_name    = var.webup_share_14
    container_path = "/mnt/share_14"
  }
  
  volumes {
    volume_name    = var.webup_share_15
    container_path = "/mnt/share_15"
  }
  
  volumes {
    volume_name    = var.webup_share_16
    container_path = "/mnt/share_16"
  }
  
  volumes {
    volume_name    = var.webup_share_17
    container_path = "/mnt/share_17"
  }
  
  volumes {
    volume_name    = var.webup_share_18
    container_path = "/mnt/share_18"
  }  
  
  volumes {
    volume_name    = var.webup_share_19
    container_path = "/mnt/share_19"
  }
  
  volumes {
    container_path = "/opt/payara/etc/webup"
    host_path      = var.webup_etc_volume
  }
  
  volumes {
    container_path = "/opt/payara/var/webup"
    host_path      = var.webup_var_volume
  }

  volumes {
    container_path = "/opt/payara/deployments"
    host_path      = var.webup_deployments_volume
  }  

  volumes {
    container_path = "/opt/payara/etc/webup-postboot"
    host_path      = var.webup_payara_postboot_volume
  }  
  
  volumes {
    container_path = "/opt/payara/appserver/glassfish/domains/domain1/logs"
    host_path      = var.webup_payara_logs_volume
  }
  
  depends_on = [docker_image.webup-image, docker_volume.shared-volumes]

}

resource "docker_volume" "shared-volumes" {
  count       = length(var.webup_shares)
  driver_opts = var.webup_shares[element(keys(var.webup_shares), count.index)]
  name        = element(keys(var.webup_shares), count.index)
}

