Added support for CF dynamic values for domains

This commit is contained in:
nsingh
2017-02-10 12:12:54 -08:00
parent d12b9cfbe2
commit fbcbc47b98
14 changed files with 375 additions and 16 deletions

View File

@@ -0,0 +1,17 @@
/*******************************************************************************
* 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;
public interface CFDomain {
String getName();
}

View File

@@ -0,0 +1,52 @@
/*******************************************************************************
* 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;
class CFDomainImpl implements CFDomain {
private final String name;
public CFDomainImpl(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CFDomainImpl other = (CFDomainImpl) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}

View File

@@ -29,4 +29,7 @@ public class CFEntities {
/* dasboard Url */ null);
}
public static CFDomain createDomain(String name) {
return new CFDomainImpl(name);
}
}

View File

@@ -17,4 +17,6 @@ public interface ClientRequests {
List<CFBuildpack> getBuildpacks() throws Exception;
List<CFServiceInstance> getServices() throws Exception;
List<CFDomain> getDomains() throws Exception;
}

View File

@@ -15,6 +15,7 @@ import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import org.springframework.ide.vscode.commons.cloudfoundry.client.CFBuildpack;
import org.springframework.ide.vscode.commons.cloudfoundry.client.CFDomain;
import org.springframework.ide.vscode.commons.cloudfoundry.client.CFServiceInstance;
import org.springframework.ide.vscode.commons.cloudfoundry.client.ClientRequests;
@@ -37,15 +38,21 @@ public class CFTarget {
/*
* Cached information
*/
private final LoadingCache<String, List<CFBuildpack>> buildpacksCache;
private final LoadingCache<String, List<CFServiceInstance>> servicesCache;
private LoadingCache<String, List<CFBuildpack>> buildpacksCache;
private LoadingCache<String, List<CFServiceInstance>> servicesCache;
private LoadingCache<String, List<CFDomain>> domainCache;
private CFCallableContext callableContext;
public CFTarget(String targetName, CFClientParams params, ClientRequests requests, CFCallableContext callableContext) {
public CFTarget(String targetName, CFClientParams params, ClientRequests requests,
CFCallableContext callableContext) {
this.params = params;
this.requests = requests;
this.targetName = targetName;
this.callableContext = callableContext;
initCache(requests);
}
private void initCache(ClientRequests requests) {
CacheLoader<String, List<CFServiceInstance>> servicesLoader = new CacheLoader<String, List<CFServiceInstance>>() {
@Override
@@ -71,12 +78,23 @@ public class CFTarget {
};
this.buildpacksCache = CacheBuilder.newBuilder()
.expireAfterAccess(CFTargetCache.TARGET_EXPIRATION, TimeUnit.HOURS).build(buildpacksLoader);
CacheLoader<String, List<CFDomain>> domainLoader = new CacheLoader<String, List<CFDomain>>() {
@Override
public List<CFDomain> load(String key) throws Exception {
return runAndCheckForFailure(() -> requests.getDomains());
}
};
this.domainCache = CacheBuilder.newBuilder()
.expireAfterAccess(CFTargetCache.TARGET_EXPIRATION, TimeUnit.HOURS).build(domainLoader);
}
protected <T> T runAndCheckForFailure(Callable<T> callable) throws Exception {
return callableContext.checkConnection(callable);
}
public boolean hasConnectionError() {
return callableContext.hasConnectionError();
}
@@ -102,6 +120,11 @@ public class CFTarget {
String key = getName();
return this.servicesCache.get(key);
}
public List<CFDomain> getDomains() throws Exception {
String key = getName();
return this.domainCache.get(key);
}
public ClientRequests getClientRequests() {
return requests;

View File

@@ -11,10 +11,11 @@
package org.springframework.ide.vscode.commons.cloudfoundry.client.v2;
import org.cloudfoundry.operations.buildpacks.Buildpack;
import org.cloudfoundry.operations.services.ServiceInstance;
import org.cloudfoundry.operations.domains.Domain;
import org.cloudfoundry.operations.services.ServiceInstanceSummary;
import org.cloudfoundry.operations.services.ServiceInstanceType;
import org.springframework.ide.vscode.commons.cloudfoundry.client.CFBuildpack;
import org.springframework.ide.vscode.commons.cloudfoundry.client.CFDomain;
import org.springframework.ide.vscode.commons.cloudfoundry.client.CFEntities;
import org.springframework.ide.vscode.commons.cloudfoundry.client.CFServiceInstance;
@@ -32,6 +33,11 @@ public class CFWrappingV2 {
return CFEntities.createBuildpack(name);
}
public static CFDomain wrap(Domain domain) {
String name = domain.getName();
return CFEntities.createDomain(name);
}
public static CFServiceInstance wrap(ServiceInstanceSummary serviceInstance) {
String name = serviceInstance.getName();
String plan = serviceInstance.getPlan();

View File

@@ -18,6 +18,7 @@ import org.cloudfoundry.client.CloudFoundryClient;
import org.cloudfoundry.operations.CloudFoundryOperations;
import org.cloudfoundry.operations.DefaultCloudFoundryOperations;
import org.springframework.ide.vscode.commons.cloudfoundry.client.CFBuildpack;
import org.springframework.ide.vscode.commons.cloudfoundry.client.CFDomain;
import org.springframework.ide.vscode.commons.cloudfoundry.client.CFServiceInstance;
import org.springframework.ide.vscode.commons.cloudfoundry.client.ClientRequests;
import org.springframework.ide.vscode.commons.cloudfoundry.client.ClientTimeouts;
@@ -82,7 +83,22 @@ public class DefaultClientRequestsV2 implements ClientRequests {
)
);
}
@Override
public List<CFDomain> getDomains() throws Exception {
return ReactorUtils.get(timeouts.getBuildpacksTimeout(), CancelationTokens.NULL,
log("operations.domains.list",
_operations
.domains()
.list()
.map(CFWrappingV2::wrap)
.collectList()
.map(ImmutableList::copyOf)
)
);
}
@Override
public List<CFBuildpack> getBuildpacks() throws Exception {
return ReactorUtils.get(timeouts.getBuildpacksTimeout(), CancelationTokens.NULL,

View File

@@ -20,6 +20,7 @@ import java.util.concurrent.Callable;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CFParamsProviderMessages;
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CFTarget;
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CFTargetCache;
@@ -28,6 +29,8 @@ import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.Conne
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.NoTargetsException;
import org.springframework.ide.vscode.commons.util.ExceptionUtil;
import com.google.common.collect.ImmutableList;
public class CFClientTest {
MockCfCli cloudfoundry = new MockCfCli();
@@ -71,6 +74,54 @@ public class CFClientTest {
assertError(() -> target.getBuildpacks(), ConnectionException.class, expectedMessages.noNetworkConnection());
}
@Test
public void testBuildpacksFromTarget() throws Exception {
ClientRequests client = cloudfoundry.client;
CFBuildpack buildpack = Mockito.mock(CFBuildpack.class);
when(buildpack.getName()).thenReturn("java_buildpack");
when(client.getBuildpacks()).thenReturn(ImmutableList.of(buildpack));
CFTarget target = targetCache.getOrCreate().get(0);
assertEquals("java_buildpack", target.getBuildpacks().get(0).getName());
}
@Test
public void testDomainsFromTarget() throws Exception {
ClientRequests client = cloudfoundry.client;
CFDomain domain = Mockito.mock(CFDomain.class);
when(domain.getName()).thenReturn("cfapps.io");
when(client.getDomains()).thenReturn(ImmutableList.of(domain));
CFTarget target = targetCache.getOrCreate().get(0);
assertEquals("cfapps.io", target.getDomains().get(0).getName());
}
@Test
public void testServicesFromTarget() throws Exception {
ClientRequests client = cloudfoundry.client;
CFServiceInstance service = Mockito.mock(CFServiceInstance.class);
when(service.getName()).thenReturn("appdb");
when(service.getPlan()).thenReturn("spark");
when(service.getService()).thenReturn("cleardb");
when(client.getServices()).thenReturn(ImmutableList.of(service));
CFTarget target = targetCache.getOrCreate().get(0);
assertEquals("appdb", target.getServices().get(0).getName());
assertEquals("spark", target.getServices().get(0).getPlan());
assertEquals("cleardb", target.getServices().get(0).getService());
}
@Test
public void testNoServicesFromTarget() throws Exception {
ClientRequests client = cloudfoundry.client;
when(client.getServices()).thenReturn(ImmutableList.of());
CFTarget target = targetCache.getOrCreate().get(0);
assertTrue(target.getServices().isEmpty());
}
protected void assertError(Callable<?> callable, Class<? extends Throwable> expected, String expectedMessage)
throws Exception {
Throwable error = null;