package org.wildfly.swarm.topology.openshift.runtime;

import com.openshift.restclient.ClientFactory;
import com.openshift.restclient.IClient;
import com.openshift.restclient.NoopSSLCertificateCallback;
import com.openshift.restclient.authorization.TokenAuthorizationStrategy;
import org.jboss.msc.service.Service;
import org.jboss.msc.service.ServiceName;
import org.jboss.msc.service.StartContext;
import org.jboss.msc.service.StartException;
import org.jboss.msc.service.StopContext;
import org.wildfly.swarm.container.Environment;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class ClientService implements Service<IClient> {

    public static final ServiceName SERVICE_NAME = OpenShiftTopologyConnector.SERVICE_NAME.append("client");

    private IClient client;

    @Override
    public void start(StartContext context) throws StartException {
        try {
            this.client = openshiftClient();
            // Trigger an API call to ensure we fail-fast if we can't talk
            // to the API
            this.client.getCurrentUser().getName();
        } catch (IOException ex) {
            throw new StartException(ex);
        }
    }

    @Override
    public void stop(StopContext context) {
        this.client = null;
    }

    private IClient openshiftClient() throws IOException {
        String kubeHost = Environment.serviceHost("kubernetes");
        int kubePort = Environment.servicePort("kubernetes");

        Path tokenFile = Paths.get("/var/run/secrets/kubernetes.io/serviceaccount/token");
        String scheme = "http";
        String token = null;
        if (Files.exists(tokenFile)) {
            token = new String(Files.readAllBytes(tokenFile));
            scheme = "https";
        }

        IClient client = new ClientFactory().create(scheme + "://" + kubeHost + ":" + kubePort, new NoopSSLCertificateCallback());
        if (token != null) {
            client.setAuthorizationStrategy(new TokenAuthorizationStrategy(token));
        }

        return client;
    }

    @Override
    public IClient getValue() throws IllegalStateException, IllegalArgumentException {
        return this.client;
    }
}

@@ -1400,36 +1400,24 @@
  kubeHost = 
-Environment.
 serviceHost(
@@ -1458,20 +1458,8 @@
 t = 
-Environment.
 serv
@@ -2196,11 +2196,507 @@
 t;%0A    %7D
+%0A%0A    protected String serviceHost(String serviceName) %7B%0A        String envName = serviceName.replace(%22-%22, %22_%22).toUpperCase() + %22_SERVICE_HOST%22;%0A        return System.getenv(envName);%0A    %7D%0A%0A    public static int servicePort(String serviceName) %7B%0A        String envName = serviceName.replace(%22-%22, %22_%22).toUpperCase() + %22_SERVICE_PORT%22;%0A        String envPort = System.getenv(envName);%0A        if (envPort == null) %7B%0A            return -1;%0A        %7D%0A%0A        return Integer.parseInt(envPort);%0A    %7D
 %0A%7D%0A

Fix compilation error from missing methods during repo split