package com.containersol.minimesos.mesos;

import com.containersol.minimesos.cluster.MesosCluster;
import com.containersol.minimesos.cluster.MesosDns;
import com.containersol.minimesos.config.MesosDNSConfig;
import com.containersol.minimesos.integrationtest.container.AbstractContainer;
import com.containersol.minimesos.docker.DockerClientFactory;
import com.github.dockerjava.api.command.CreateContainerCmd;
import com.github.dockerjava.api.model.ExposedPort;
import com.github.dockerjava.api.model.InternetProtocol;
import com.github.dockerjava.api.model.PortBinding;

import java.util.HashMap;
import java.util.Map;

import static com.containersol.minimesos.util.EnvironmentBuilder.newEnvironment;

/**
 * Mesos DNS automatically registers and deregisters Mesos services.
 */
public class MesosDnsContainer extends AbstractContainer implements MesosDns {

    private static final String DNS_PORT = "5353";

    private static final String DOMAIN = "mm";

    private MesosDNSConfig config;

    public MesosDnsContainer(MesosCluster cluster, String uuid, String containerId) {
        this(cluster, uuid, containerId, new MesosDNSConfig());
    }

    private MesosDnsContainer(MesosCluster cluster, String uuid, String containerId, MesosDNSConfig config) {
        super(cluster, uuid, containerId, config);
        this.config = config;
    }

    public MesosDnsContainer(MesosDNSConfig mesosDNS) {
        super(mesosDNS);
        this.config = mesosDNS;
    }

    @Override
    public String getRole() {
        return "mesosdns";
    }

    @Override
    protected CreateContainerCmd dockerCommand() {
        return DockerClientFactory.build()
            .createContainerCmd(config.getImageName() + ":" + config.getImageTag())
            .withEnv(newEnvironment()
                .withValues(getMesosDNSEnvVars())
                .createEnvironment())
            .withCmd("-v=2", "-config=/etc/mesos-dns/config.json")
            .withExposedPorts(new ExposedPort(Integer.valueOf(DNS_PORT), InternetProtocol.UDP))
            .withName(getName());
    }

    @Override
    protected int getServicePort() {
        return Integer.valueOf(DNS_PORT);
    }

    private Map<String,String> getMesosDNSEnvVars() {
        Map<String, String> mesosDNSEnvVars = new HashMap<>();
        mesosDNSEnvVars.put("MESOS_DNS_ZK", getCluster().getZooKeeper().getFormattedZKAddress() + "/mesos");
        mesosDNSEnvVars.put("MESOS_DNS_DOMAIN", DOMAIN);
        mesosDNSEnvVars.put("MESOS_DNS_PORT", DNS_PORT);
        return mesosDNSEnvVars;
    }
}

@@ -515,60 +515,8 @@
 col;
-%0Aimport com.github.dockerjava.api.model.PortBinding;
 %0A%0Aim
@@ -897,24 +897,80 @@
 IN = %22mm%22;%0A%0A
+    private static final String REFRESH_SECONDS = %221%22;%0A%0A
     private 
@@ -2504,24 +2504,99 @@
  DNS_PORT);%0A
+        mesosDNSEnvVars.put(%22MESOS_DNS_REFRESH_SECONDS%22, REFRESH_SECONDS);%0A
         retu

Set refresh seconds so Marathon apps are detected quicker.