Support for default CF login params provider. Use CF CLI provider as

default for manifesr yaml
This commit is contained in:
BoykoAlex
2017-06-30 18:03:08 -04:00
parent e8425e6026
commit f522a39047
8 changed files with 119 additions and 38 deletions

View File

@@ -53,5 +53,7 @@ public interface CfClientConfig {
/**
* Default CF Client configuration
*/
final static CfClientConfig DEFAULT = new CfClientConfigImpl();
static CfClientConfig createDefault() {
return new CfClientConfigImpl();
}
}

View File

@@ -43,9 +43,16 @@ public class CfJsonParamsProvider implements ClientParamsProvider {
private static final String ORG_NAME = "OrgName";
private static final String SPACE_NAME = "SpaceName";
private Supplier<Collection<CFClientParams>> paramsSupplier;
private static final String PROP_NO_TARGETS_FOUND = "noTargetsFound";
private static final String PROP_UNAUTHORISED = "unauthorised";
private static final String PROP_NO_NETWORK_CONNECTION = "noNetworkConnection";
private static final String PROP_NO_ORG_SPACE = "noOrgSpace";
public CfJsonParamsProvider(List<?> json) {
private Supplier<Collection<CFClientParams>> paramsSupplier;
private Map<String, String> messages;
public CfJsonParamsProvider(List<?> json, Map<String, String> messages) {
this.messages = messages;
this.paramsSupplier = Suppliers.memoize(() -> json
.stream()
.filter(o -> o instanceof Map<?, ?>)
@@ -56,7 +63,11 @@ public class CfJsonParamsProvider implements ClientParamsProvider {
@Override
public Collection<CFClientParams> getParams() throws NoTargetsException, ExecutionException {
return paramsSupplier.get();
Collection<CFClientParams> params = paramsSupplier.get();
if (params == null || params.isEmpty()) {
throw new NoTargetsException(getMessages().noTargetsFound());
}
return params;
}
private static CFClientParams parseCfClientParams(Map<?, ?> userData) {
@@ -82,21 +93,33 @@ public class CfJsonParamsProvider implements ClientParamsProvider {
@Override
public String noTargetsFound() {
if (messages != null && messages.containsKey(PROP_NO_TARGETS_FOUND)) {
return messages.get(PROP_NO_TARGETS_FOUND);
}
return NO_TARGETS_FOUND_MESSAGE;
}
@Override
public String unauthorised() {
if (messages != null && messages.containsKey(PROP_UNAUTHORISED)) {
return messages.get(PROP_UNAUTHORISED);
}
return NO_NETWORK_CONNECTION;
}
@Override
public String noNetworkConnection() {
if (messages != null && messages.containsKey(PROP_NO_NETWORK_CONNECTION)) {
return messages.get(PROP_NO_NETWORK_CONNECTION);
}
return NO_NETWORK_CONNECTION;
}
@Override
public String noOrgSpace() {
if (messages != null && messages.containsKey(PROP_NO_ORG_SPACE)) {
return messages.get(PROP_NO_ORG_SPACE);
}
return NO_ORG_SPACE;
}

View File

@@ -33,7 +33,7 @@ public class MockCfCli {
public final CloudFoundryClientFactory factory = mock(CloudFoundryClientFactory.class);
public final ClientRequests client = mock(ClientRequests.class);
public final CfClientConfig cfClientConfig = CfClientConfig.DEFAULT;
public final CfClientConfig cfClientConfig = CfClientConfig.createDefault();
public final ClientParamsProvider paramsProvider = mock(ClientParamsProvider.class);
public final CfCliProviderMessages actualCfCliMessages = new CfCliProviderMessages();

View File

@@ -10,18 +10,24 @@
*******************************************************************************/
package org.springframework.ide.vscode.manifest.yaml;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import org.springframework.ide.vscode.commons.cloudfoundry.client.ClientTimeouts;
import org.springframework.ide.vscode.commons.cloudfoundry.client.CloudFoundryClientFactory;
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CFClientParams;
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CFParamsProviderMessages;
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CFTargetCache;
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CfCliParamsProvider;
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CfClientConfig;
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CfJsonParamsProvider;
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.ClientParamsProvider;
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.NoTargetsException;
import org.springframework.ide.vscode.commons.cloudfoundry.client.v2.DefaultCloudFoundryClientFactoryV2;
import org.springframework.ide.vscode.commons.languageserver.completion.VscodeCompletionEngineAdapter;
@@ -33,7 +39,6 @@ import org.springframework.ide.vscode.commons.languageserver.reconcile.IReconcil
import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer;
import org.springframework.ide.vscode.commons.languageserver.util.SimpleTextDocumentService;
import org.springframework.ide.vscode.commons.languageserver.util.SimpleWorkspaceService;
import org.springframework.ide.vscode.commons.languageserver.util.TextDocumentContentChange;
import org.springframework.ide.vscode.commons.util.text.LanguageId;
import org.springframework.ide.vscode.commons.util.text.TextDocument;
import org.springframework.ide.vscode.commons.yaml.ast.YamlASTProvider;
@@ -50,6 +55,8 @@ import org.springframework.ide.vscode.commons.yaml.schema.YamlSchema;
import org.springframework.ide.vscode.commons.yaml.structure.YamlStructureProvider;
import org.yaml.snakeyaml.Yaml;
import com.google.common.collect.ImmutableList;
public class ManifestYamlLanguageServer extends SimpleLanguageServer {
private Yaml yaml = new Yaml();
@@ -60,15 +67,17 @@ public class ManifestYamlLanguageServer extends SimpleLanguageServer {
private final LazyCompletionResolver completionResolver = new LazyCompletionResolver(); //Set to null to disable lazy resolving
private final LanguageId FALLBACK_YML_ID = LanguageId.of("yml");
final private ClientParamsProvider defaultClientParamsProvider;
public ManifestYamlLanguageServer() {
this(DefaultCloudFoundryClientFactoryV2.INSTANCE, CfClientConfig.DEFAULT);
this(DefaultCloudFoundryClientFactoryV2.INSTANCE, CfCliParamsProvider.getInstance());
}
public ManifestYamlLanguageServer(CloudFoundryClientFactory cfClientFactory, CfClientConfig cfClientConfig) {
public ManifestYamlLanguageServer(CloudFoundryClientFactory cfClientFactory, ClientParamsProvider defaultClientParamsProvider) {
super("vscode-manifest-yaml");
this.cfClientFactory = cfClientFactory;
this.cfClientConfig=cfClientConfig;
this.cfClientConfig=CfClientConfig.createDefault();
this.defaultClientParamsProvider = defaultClientParamsProvider;
SimpleTextDocumentService documents = getTextDocumentService();
SimpleWorkspaceService workspace = getWorkspaceService();
@@ -117,12 +126,59 @@ public class ManifestYamlLanguageServer extends SimpleLanguageServer {
workspace.onDidChangeConfiguraton(settings -> {
Object cfClientParamsObj = settings.getProperty("cfClientParams");
if (cfClientParamsObj instanceof List<?>) {
cfClientConfig.setClientParamsProvider(new CfJsonParamsProvider((List<?>) cfClientParamsObj));
if (cfClientParamsObj instanceof Map<?,?>) {
applyCfLoginParameterSettings((Map<?,?>) cfClientParamsObj);
}
});
}
public CfClientConfig getCfClientConfig() {
return cfClientConfig;
}
@SuppressWarnings("unchecked")
private void applyCfLoginParameterSettings(Map<?, ?> cfClientParamsData) {
if (cfClientParamsData.get("parameters") instanceof List<?>) {
List<?> loginParams = (List<?>) cfClientParamsData.get("parameters");
CfJsonParamsProvider cfClientParamsProvider = new CfJsonParamsProvider(loginParams,
(Map<String, String>) cfClientParamsData.get("messages"));
cfClientConfig.setClientParamsProvider(new ClientParamsProvider() {
@Override
public Collection<CFClientParams> getParams() throws NoTargetsException, ExecutionException {
List<ClientParamsProvider> providers = ImmutableList.of(defaultClientParamsProvider, cfClientParamsProvider);
List<CFClientParams> params = new ArrayList<>();
for (ClientParamsProvider provider : providers) {
try {
params.addAll(provider.getParams());
} catch (NoTargetsException e) {
// ignore
}
}
if (params.isEmpty()) {
throw new NoTargetsException(getMessages().noTargetsFound());
}
return params;
}
@Override
public CFParamsProviderMessages getMessages() {
return cfClientParamsData.isEmpty() ? defaultClientParamsProvider.getMessages()
: cfClientParamsProvider.getMessages();
}
});
}
}
private void validateOnDocumentChange(IReconcileEngine engine, TextDocument doc) {
if (LanguageId.CF_MANIFEST.equals(doc.getLanguageId())
|| FALLBACK_YML_ID.equals(doc.getLanguageId())) {
@@ -174,7 +230,7 @@ public class ManifestYamlLanguageServer extends SimpleLanguageServer {
if (cfTargetCache == null) {
// Init CF client params provider if it's initilized
if (cfClientConfig.getClientParamsProvider() == null) {
cfClientConfig.setClientParamsProvider(CfCliParamsProvider.getInstance());
cfClientConfig.setClientParamsProvider(defaultClientParamsProvider );
}
CloudFoundryClientFactory clientFactory = cfClientFactory;
cfTargetCache = new CFTargetCache(cfClientConfig, clientFactory, new ClientTimeouts());

View File

@@ -47,7 +47,7 @@ public class ManifestYamlEditorTest {
@Before public void setup() throws Exception {
harness = new LanguageServerHarness(
()-> new ManifestYamlLanguageServer(cloudfoundry.factory, cloudfoundry.config),
()-> new ManifestYamlLanguageServer(cloudfoundry.factory, cloudfoundry.defaultParamsProvider),
LanguageId.CF_MANIFEST
);
harness.intialize(null);

View File

@@ -82,9 +82,9 @@ public class ManifestYamlLanguageServerTest {
assertThat(initResult.getCapabilities().getTextDocumentSync().getLeft()).isEqualTo(TextDocumentSyncKind.Incremental);
}
@Test public void cfClientParams1() throws Exception {
@Test public void changeCfClientParams() throws Exception {
MockCloudfoundry cloudfoundry = new MockCloudfoundry();
ManifestYamlLanguageServer manifestYamlLanguageServer = new ManifestYamlLanguageServer(cloudfoundry.factory, cloudfoundry.config);
ManifestYamlLanguageServer manifestYamlLanguageServer = new ManifestYamlLanguageServer(cloudfoundry.factory, cloudfoundry.defaultParamsProvider);
LanguageServerHarness harness = new LanguageServerHarness(
() -> manifestYamlLanguageServer,
@@ -92,15 +92,14 @@ public class ManifestYamlLanguageServerTest {
);
harness.intialize(null);
assertEquals(1, cloudfoundry.config.getClientParamsProvider().getParams().size());
assertEquals(1, manifestYamlLanguageServer.getCfClientConfig().getClientParamsProvider().getParams().size());
assertEquals(Arrays.asList("test.io"), manifestYamlLanguageServer.getCfTargets());
DidChangeConfigurationParams params = new DidChangeConfigurationParams();
params.setSettings(new ObjectMapper().readValue(getClass().getResourceAsStream("/cf-targets1.json"), Map.class));
manifestYamlLanguageServer.getWorkspaceService().didChangeConfiguration(params);
assertEquals(2, cloudfoundry.config.getClientParamsProvider().getParams().size());
assertEquals(Arrays.asList("api.system.demo-gcp.springapps.io", "api.run.pivotal.io"), manifestYamlLanguageServer.getCfTargets());
assertEquals(3, manifestYamlLanguageServer.getCfClientConfig().getClientParamsProvider().getParams().size());
assertEquals(Arrays.asList("test.io", "api.system.demo-gcp.springapps.io", "api.run.pivotal.io"), manifestYamlLanguageServer.getCfTargets());
}

View File

@@ -19,7 +19,7 @@ import org.springframework.ide.vscode.commons.cloudfoundry.client.ClientRequests
import org.springframework.ide.vscode.commons.cloudfoundry.client.CloudFoundryClientFactory;
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CFClientParams;
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CFCredentials;
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CfClientConfig;
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.util.ExceptionUtil;
@@ -32,17 +32,16 @@ public class MockCloudfoundry {
public final CloudFoundryClientFactory factory = mock(CloudFoundryClientFactory.class);
public final ClientRequests client = mock(ClientRequests.class);
public final CfClientConfig config = CfClientConfig.DEFAULT;
public final ClientParamsProvider defaultParamsProvider = mock(ClientParamsProvider.class);
public MockCloudfoundry() {
config.setClientParamsProvider(defaultParamsProvider);
try {
//program some default behavior into mocks... most tests will use this.
//other tests should 'reset' the mocks and reprogram them as needed.
when(factory.getClient(any(), any())).thenReturn(client);
when(defaultParamsProvider.getParams()).thenReturn(ImmutableList.of(DEFAULT_PARAMS));
when(defaultParamsProvider.getMessages()).thenReturn(CfCliParamsProvider.getInstance().getMessages());
} catch (Exception e) {
throw ExceptionUtil.unchecked(e);
}

View File

@@ -1,18 +1,20 @@
{
"cfClientParams": [
{
"Target": "https://api.system.demo-gcp.springapps.io",
"OrgName": "system",
"SpaceName": "p-dataflow",
"RefreshToken": "eyJhbGciOiJSUzI1N",
"SSLDisabled": true
},
{
"Target": "https://api.run.pivotal.io",
"OrgName": "test",
"SpaceName": "spring-testing",
"RefreshToken": "eyJhbGciOiJSUzI1N",
"SSLDisabled": true
}
]
"cfClientParams": {
"parameters": [
{
"Target": "https://api.system.demo-gcp.springapps.io",
"OrgName": "system",
"SpaceName": "p-dataflow",
"RefreshToken": "eyJhbGciOiJSUzI1N",
"SSLDisabled": true
},
{
"Target": "https://api.run.pivotal.io",
"OrgName": "test",
"SpaceName": "spring-testing",
"RefreshToken": "eyJhbGciOiJSUzI1N",
"SSLDisabled": true
}
]
}
}