Code cleanup for CF support in vscode
This commit is contained in:
@@ -1,63 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2017 Pivotal, Inc.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* Pivotal, Inc. - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.inject.Provider;
|
||||
|
||||
/**
|
||||
* Resolves Cloud Foundry client parameters from params providers (for example,
|
||||
* a provider would be a cf CLI config parser that parses params from the CLI
|
||||
* config.json file)
|
||||
*
|
||||
*/
|
||||
public class CFClientParamsFactory {
|
||||
|
||||
public static final CFClientParamsFactory INSTANCE = new CFClientParamsFactory();
|
||||
|
||||
private List<Provider<List<CFClientParams>>> providers = new ArrayList<>();
|
||||
|
||||
private CFClientParamsFactory() {
|
||||
// For now, only support cf CLI.
|
||||
// Maybe in the future other ways of retrieving client params can be
|
||||
// supported
|
||||
// in addition to the cf CLI config.
|
||||
addProvider(new CfCliParamsProvider());
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds provider to the front of the registered providers such that it would
|
||||
* be called first when a request is made for CF client params
|
||||
*
|
||||
* @param provider
|
||||
*/
|
||||
public void addProvider(Provider<List<CFClientParams>> provider) {
|
||||
if (provider != null) {
|
||||
providers.add(provider);
|
||||
}
|
||||
}
|
||||
|
||||
public List<CFClientParams> getParams() {
|
||||
|
||||
// Start from the last provider, which is the highest priority
|
||||
for (int i = providers.size() - 1; i >= 0; i--) {
|
||||
List<CFClientParams> params = providers.get(i).get();
|
||||
if (params != null && !params.isEmpty()) {
|
||||
return params;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,23 +14,22 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.ide.vscode.commons.cloudfoundry.client.v2.ClientRequests;
|
||||
import org.springframework.ide.vscode.commons.cloudfoundry.client.v2.CloudFoundryClientFactory;
|
||||
import org.springframework.ide.vscode.commons.cloudfoundry.client.v2.DefaultCloudFoundryClientFactoryV2;
|
||||
import org.springframework.ide.vscode.commons.cloudfoundry.client.v2.ClientTimeouts;
|
||||
import org.springframework.ide.vscode.commons.cloudfoundry.client.v2.CloudFoundryClientFactory;
|
||||
|
||||
/**
|
||||
* Creates targets given a client parameters factory and a client factory.
|
||||
* Creates targets given a client parameters provider and a client factory.
|
||||
*
|
||||
*/
|
||||
public class CFTargetsFactory {
|
||||
|
||||
private final CloudFoundryClientFactory clientFactory;
|
||||
private final CFClientParamsFactory paramsFactory;
|
||||
private final ClientParamsProvider paramsProvider;
|
||||
private final ClientTimeouts timeouts;
|
||||
|
||||
public CFTargetsFactory(CFClientParamsFactory paramsFactory, CloudFoundryClientFactory clientFactory, ClientTimeouts timeouts) {
|
||||
public CFTargetsFactory(ClientParamsProvider paramsProvider, CloudFoundryClientFactory clientFactory, ClientTimeouts timeouts) {
|
||||
this.clientFactory = clientFactory;
|
||||
this.paramsFactory = paramsFactory;
|
||||
this.paramsProvider = paramsProvider;
|
||||
this.timeouts = timeouts;
|
||||
}
|
||||
|
||||
@@ -40,7 +39,7 @@ public class CFTargetsFactory {
|
||||
* @throws Exception
|
||||
*/
|
||||
public List<CFTarget> getTargets() throws Exception {
|
||||
List<CFClientParams> allParams = paramsFactory.getParams();
|
||||
List<CFClientParams> allParams = paramsProvider.getParams();
|
||||
List<CFTarget> targets = new ArrayList<>();
|
||||
if (allParams != null) {
|
||||
for (CFClientParams parameters : allParams) {
|
||||
@@ -52,6 +51,10 @@ public class CFTargetsFactory {
|
||||
}
|
||||
return targets;
|
||||
}
|
||||
|
||||
public String noTargetsMessage() {
|
||||
return paramsProvider.noParamsAvailableMessage();
|
||||
}
|
||||
|
||||
protected String getTargetName(CFClientParams params) {
|
||||
return labelFromCfApi(params.getApiUrl());
|
||||
@@ -66,12 +69,4 @@ public class CFTargetsFactory {
|
||||
return cfApiUrl;
|
||||
}
|
||||
}
|
||||
|
||||
public static CFTargetsFactory createDefaultV2TargetsFactory(ClientTimeouts timeouts) {
|
||||
CloudFoundryClientFactory clientFactory = DefaultCloudFoundryClientFactoryV2.INSTANCE;
|
||||
CFClientParamsFactory paramsFactory = CFClientParamsFactory.INSTANCE;
|
||||
|
||||
return new CFTargetsFactory(paramsFactory, clientFactory, timeouts);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,8 +18,6 @@ import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Provider;
|
||||
|
||||
import org.springframework.ide.vscode.commons.util.ExternalCommand;
|
||||
import org.springframework.ide.vscode.commons.util.ExternalProcess;
|
||||
|
||||
@@ -31,7 +29,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class CfCliParamsProvider implements Provider<List<CFClientParams>> {
|
||||
public class CfCliParamsProvider implements ClientParamsProvider {
|
||||
|
||||
public static final String TARGET = "Target";
|
||||
public static final String REFRESH_TOKEN = "RefreshToken";
|
||||
@@ -42,8 +40,14 @@ public class CfCliParamsProvider implements Provider<List<CFClientParams>> {
|
||||
|
||||
private static Logger logger = Logger.getLogger(CfCliParamsProvider.class.getName());
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.
|
||||
* ClientParamsProvider#getParams()
|
||||
*/
|
||||
@Override
|
||||
public List<CFClientParams> get() {
|
||||
public List<CFClientParams> getParams() {
|
||||
try {
|
||||
File file = getConfigJsonFile();
|
||||
if (file != null) {
|
||||
@@ -76,6 +80,17 @@ public class CfCliParamsProvider implements Provider<List<CFClientParams>> {
|
||||
return null;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.
|
||||
* ClientParamsProvider#noParamsAvailableMessage()
|
||||
*/
|
||||
@Override
|
||||
public String noParamsAvailableMessage() {
|
||||
return "No Cloud Foundry targets. Please use cf CLI to configure and login to a Cloud Foundry target.";
|
||||
}
|
||||
|
||||
private File getConfigJsonFile() throws IOException, InterruptedException {
|
||||
// Support Unix systems for now
|
||||
if (!System.getProperty("os.name").toLowerCase().startsWith("win")) {
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2017 Pivotal, Inc.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* Pivotal, Inc. - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ClientParamsProvider {
|
||||
|
||||
List<CFClientParams> getParams();
|
||||
|
||||
String noParamsAvailableMessage();
|
||||
|
||||
}
|
||||
@@ -650,7 +650,7 @@ public class DefaultClientRequestsV2 implements ClientRequests {
|
||||
// ? stopApp(appName)
|
||||
// : restartApp(appName)
|
||||
// );
|
||||
throw new Error("Application push not currently supported in CF vscode");
|
||||
throw ExceptionUtil.notImplemented("Application push not currently supported in CF vscode");
|
||||
}
|
||||
|
||||
// private DefaultClientRequestsV1 v1() throws Exception {
|
||||
|
||||
@@ -16,23 +16,29 @@ import java.util.List;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CFClientParamsFactory;
|
||||
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CFTarget;
|
||||
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CFTargetsFactory;
|
||||
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CfCliParamsProvider;
|
||||
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.ClientParamsProvider;
|
||||
import org.springframework.ide.vscode.commons.cloudfoundry.client.v2.ClientTimeouts;
|
||||
import org.springframework.ide.vscode.commons.cloudfoundry.client.v2.CloudFoundryClientFactory;
|
||||
import org.springframework.ide.vscode.commons.cloudfoundry.client.v2.DefaultCloudFoundryClientFactoryV2;
|
||||
|
||||
public class CFClientTest {
|
||||
|
||||
/*
|
||||
* Not meant to be run in a build yet as there is no CF harness to read CF
|
||||
* params. Just keeping this tests for local development.
|
||||
*/
|
||||
@Ignore
|
||||
@Test
|
||||
public void testGetBuildpacksFromCliParamsTarget() throws Exception {
|
||||
|
||||
|
||||
@Ignore @Test public void testGetBuildpacksFromCliParamsTarget() throws Exception {
|
||||
|
||||
CFClientParamsFactory paramsFactory = CFClientParamsFactory.INSTANCE;
|
||||
ClientParamsProvider cliProvider = new CfCliParamsProvider();
|
||||
CloudFoundryClientFactory clientFactory = DefaultCloudFoundryClientFactoryV2.INSTANCE;
|
||||
ClientTimeouts timeouts = ClientTimeouts.DEFAULT_TIMEOUTS;
|
||||
|
||||
CFTargetsFactory targets = new CFTargetsFactory(paramsFactory, clientFactory, ClientTimeouts.DEFAULT_TIMEOUTS);
|
||||
CFTargetsFactory targets = new CFTargetsFactory(cliProvider, clientFactory, timeouts);
|
||||
CFTarget target = targets.getTargets().get(0);
|
||||
|
||||
List<CFBuildpack> buildPacks = target.getBuildpacks();
|
||||
|
||||
@@ -76,7 +76,7 @@ public class ExceptionUtil {
|
||||
return new ExecutionException(cause);
|
||||
}
|
||||
|
||||
public static Object exception(String message, Throwable error) {
|
||||
public static Exception exception(String message, Throwable error) {
|
||||
if (message != null) {
|
||||
// Wrap only if there is an additional message
|
||||
return new ExecutionException(message, error);
|
||||
@@ -84,5 +84,4 @@ public class ExceptionUtil {
|
||||
return exception(error);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user