package org.kink_lang.jenkins.plugins.gce;

import java.io.IOException;
import java.util.Collections;

import hudson.model.Computer;
import hudson.model.Descriptor;
import hudson.model.TaskListener;
import hudson.slaves.AbstractCloudSlave;
import hudson.slaves.CloudSlaveRetentionStrategy;
import hudson.slaves.JNLPLauncher;
import hudson.slaves.NodeProperty;
import hudson.slaves.AbstractCloudComputer;

import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

public class PersistentSlave extends AbstractCloudSlave {

    /** Project of the slave. */
    private String project;

    /** Zone of the slave. */
    private String zone;

    @DataBoundConstructor
    public PersistentSlave(
            String project,
            String zone,
            String name,
            String desc,
            String numExecutors,
            String labelString) throws Descriptor.FormException, IOException{
        super(name, desc, "/tmp", numExecutors, Mode.NORMAL, labelString,
                new JNLPLauncher(),
                new CloudSlaveRetentionStrategy<Computer>(),
                Collections.<NodeProperty<PersistentSlave>>emptyList());
        this.project = project;
        this.zone = zone;
    }

    /**
     * Stores the project ID.
     */
    @DataBoundSetter
    public void setProject(String project) {
        this.project = project;
    }

    /**
     * Returns the project ID.
     */
    public String getProject() {
        return this.project;
    }

    /**
     * Stores the zone ID.
     */
    @DataBoundSetter
    public void setZone(String zone) {
        this.zone = zone;
    }

    /**
     * Returns the zone ID.
     */
    public String getZone() {
        return this.zone;
    }

    @Override
    public AbstractCloudComputer<PersistentSlave> createComputer() {
        return new PersistentComputer(this);
    }

    @Override
    protected void _terminate(TaskListener listener) throws IOException, InterruptedException {
        throw new UnsupportedOperationException();
    }

}

// vim: et sw=4 sts=4 fdm=marker

@@ -94,16 +94,63 @@
 ections;
+%0Aimport java.security.GeneralSecurityException;
 %0A%0Aimport
@@ -539,16 +539,362 @@
 etter;%0A%0A
+import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;%0Aimport com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;%0Aimport com.google.api.client.http.HttpTransport;%0Aimport com.google.api.client.json.JsonFactory;%0Aimport com.google.api.client.json.jackson2.JacksonFactory;%0Aimport com.google.api.services.compute.Compute;%0A%0A
 public c
@@ -2407,49 +2407,510 @@
    t
-hrow new UnsupportedOperationException();
+ry %7B%0A            HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();%0A            JsonFactory jsonFactory = new JacksonFactory();%0A            GoogleCredential credential = GoogleCredential.getApplicationDefault();%0A            Compute compute = new Compute.Builder(transport, jsonFactory, credential).build();%0A            compute.instances().stop(project, zone, name).execute();%0A        %7D catch (GeneralSecurityException gsex) %7B%0A            throw new RuntimeException(gsex);%0A        %7D
 %0A   

add PersistentSlave#_terminate