package org.safehaus.subutai.cli.container.manager;

import java.util.Arrays;
import java.util.Set;
import org.apache.felix.gogo.commands.Argument;
import org.apache.felix.gogo.commands.Command;
import org.apache.karaf.shell.console.OsgiCommandSupport;
import org.safehaus.subutai.api.agentmanager.AgentManager;
import org.safehaus.subutai.api.container.ContainerManager;
import org.safehaus.subutai.api.template.manager.TemplateManager;
import org.safehaus.subutai.shared.protocol.Agent;

@Command(scope = "container", name = "clone-many")
public class CloneMany extends OsgiCommandSupport {

    TemplateManager templateManager;
    ContainerManager containerManager;
    AgentManager agentManager;

    @Argument(index = 0, required = true)
    private String template;
    @Argument(index = 1, required = true)
    private int nodesCount;
    @Argument(index = 2, required = true)
    private String hosts;

    public void setTemplateManager(TemplateManager templateManager) {
        this.templateManager = templateManager;
    }

    public void setContainerManager(ContainerManager containerManager) {
        this.containerManager = containerManager;
    }

    public void setAgentManager(AgentManager agentManager) {
        this.agentManager = agentManager;
    }

    @Override
    protected Object doExecute() throws Exception {

        Agent a = agentManager.getAgentByHostname(hosts);
        Set<Agent> set = containerManager.clone("my_group", template, nodesCount, Arrays.asList(a));
        if(set.isEmpty()) System.out.println("Empty set");
        else System.out.println("Returned clones: " + set.size());
        return null;
    }

}

@@ -67,36 +67,9 @@
 til.
-Arrays;%0Aimport java.util.Set
+*
 ;%0Aim
@@ -1368,16 +1368,57 @@
 hosts);%0A
+        List%3CAgent%3E target = getHosts();%0A
         
@@ -1495,24 +1495,14 @@
 nt, 
-Arrays.asList(a)
+target
 );%0A 
@@ -1550,17 +1550,27 @@
 ln(%22
-Empty set
+Result set is empty
 %22);%0A
@@ -1664,10 +1664,358 @@
 %0A    %7D%0A%0A
+    private List%3CAgent%3E getHosts() %7B%0A        String%5B%5D arr = hosts.split(%22%5B%5C%5Cs;,%5D%22);%0A        List%3CAgent%3E agents = new ArrayList%3C%3E();%0A        for(String host : arr) %7B%0A            if(host.isEmpty()) continue;%0A            Agent a = agentManager.getAgentByHostname(host);%0A            if(a != null) agents.add(a);%0A        %7D%0A        return agents;%0A    %7D%0A%0A
 %7D%0A

fix clone cli to accept multiple target host names