Push bookeeping of names and ids down a level in deployer

The controller doesn't need to know the mappings, and it helps
to keep them closer to the actual AppDeployer, so in a future
change we can use the app names to look up functions.
This commit is contained in:
Dave Syer
2017-07-03 14:54:44 +01:00
parent 6d6c7ef8b3
commit 42a50861c0
3 changed files with 85 additions and 78 deletions

View File

@@ -17,7 +17,6 @@ package org.springframework.cloud.function.deployer;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
@@ -47,10 +46,6 @@ public class FunctionAdminController implements CommandLineRunner {
private final FunctionExtractingFunctionCatalog deployer;
private Map<String, String> deployed = new LinkedHashMap<>();
private Map<String, String> names = new LinkedHashMap<>();
@Autowired
public FunctionAdminController(FunctionExtractingFunctionCatalog deployer) {
this.deployer = deployer;
@@ -64,26 +59,13 @@ public class FunctionAdminController implements CommandLineRunner {
}
@DeleteMapping(path = "/{name}")
public Map<String, Object> undeploy(@PathVariable String name) throws Exception {
String id = names.get(name);
if (id == null) {
// TODO: Convert to 404
throw new IllegalStateException("No such app");
}
deployer.undeploy(id);
names.remove(name);
deployed.remove(id);
return Collections.singletonMap("id", id);
public Object undeploy(@PathVariable String name) throws Exception {
return deployer.undeploy(name);
}
@GetMapping({ "", "/" })
public Map<String, Object> deployed() {
Map<String, Object> result = new LinkedHashMap<>();
for (String name : names.keySet()) {
String id = names.get(name);
result.put(name, new DeployedArtifact(name, id, deployed.get(id)));
}
return result;
return deployer.deployed();
}
@Override
@@ -100,50 +82,7 @@ public class FunctionAdminController implements CommandLineRunner {
AppDeploymentRequest request = new AppDeploymentRequest(definition, resource,
Collections.singletonMap(AppDeployer.GROUP_PROPERTY_KEY, "functions"),
Arrays.asList(args));
String deployed = deployer.deploy(request);
this.deployed.put(deployed, path);
this.names.put(name, deployed);
String deployed = deployer.deploy(name, request);
return deployed;
}
}
class DeployedArtifact {
private String name;
private String id;
private String path;
public DeployedArtifact() {
}
public DeployedArtifact(String name, String id, String path) {
this.name = name;
this.id = id;
this.path = path;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}

View File

@@ -15,7 +15,9 @@
*/
package org.springframework.cloud.function.deployer;
import java.util.HashSet;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
@@ -36,10 +38,12 @@ public class FunctionExtractingFunctionCatalog
private static Log logger = LogFactory
.getLog(FunctionExtractingFunctionCatalog.class);
private final Set<String> deployed = new HashSet<>();
private ThinJarAppDeployer deployer;
private Map<String, String> deployed = new LinkedHashMap<>();
private Map<String, String> names = new LinkedHashMap<>();
public FunctionExtractingFunctionCatalog() {
this("thin", "slim");
}
@@ -119,15 +123,29 @@ public class FunctionExtractingFunctionCatalog
return (String) inspect(function, "getName");
}
public String deploy(AppDeploymentRequest request) {
String id = deployer.deploy(request);
deployed.add(id);
public String deploy(String name, AppDeploymentRequest request) {
String id = this.deployer.deploy(request);
try {
this.deployed.put(id, request.getResource().getURI().toString());
}
catch (IOException e) {
throw new IllegalStateException("Cannot locate resource for " + name, e);
}
this.names.put(name, id);
return id;
}
public void undeploy(String id) {
deployer.undeploy(id);
deployed.remove(id);
public DeployedArtifact undeploy(String name) {
String id = this.names.get(name);
if (id == null) {
// TODO: Convert to 404
throw new IllegalStateException("No such app");
}
this.deployer.undeploy(id);
this.deployed.remove(id);
this.names.remove(name);
String path = this.deployed.remove(id);
return new DeployedArtifact(id, name, path);
}
private Object inspect(Object arg, String method) {
@@ -152,8 +170,8 @@ public class FunctionExtractingFunctionCatalog
}
private Object invoke(Class<?> type, String method, Object... arg) {
for (String id : deployed) {
Object catalog = deployer.getBean(id, type);
for (String id : this.deployed.keySet()) {
Object catalog = this.deployer.getBean(id, type);
if (catalog == null) {
continue;
}
@@ -175,4 +193,54 @@ public class FunctionExtractingFunctionCatalog
return null;
}
public Map<String, Object> deployed() {
Map<String, Object> result = new LinkedHashMap<>();
for (String name : this.names.keySet()) {
String id = this.names.get(name);
result.put(name, new DeployedArtifact(name, id, this.deployed.get(id)));
}
return result;
}
}
class DeployedArtifact {
private String name;
private String id;
private String path;
public DeployedArtifact() {
}
public DeployedArtifact(String name, String id, String path) {
this.name = name;
this.id = id;
this.path = path;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}

View File

@@ -63,7 +63,7 @@ public class FunctionExtractingFunctionCatalogTests {
@AfterClass
public static void close() {
if (id != null) {
deployer.undeploy(id);
deployer.undeploy("sample");
}
}
@@ -95,7 +95,7 @@ public class FunctionExtractingFunctionCatalogTests {
Collections.emptyMap());
AppDeploymentRequest request = new AppDeploymentRequest(definition, resource,
Collections.emptyMap(), Arrays.asList(args));
String deployed = deployer.deploy(request);
String deployed = deployer.deploy("sample", request);
return deployed;
}