Browse files

Better validation if container exists when creating agents

  • Loading branch information...
1 parent 3de2545 commit fe8bb4d461cd90bca5e487ea36e1d1a1404db4bd @davsclaus davsclaus committed Feb 7, 2012
View
25 fabric/fabric-commands/src/main/java/org/fusesource/fabric/commands/ContainerCreate.java
@@ -37,25 +37,30 @@
@Override
protected Object doExecute() throws Exception {
- if (url == null && parent == null) {
- throw new Exception("Either an url or a parent must be specified");
- }
+ // validate input before creating containers
+ preCreateContainer(name);
+
if (url == null && parent != null) {
url = "child://" + parent;
}
- // validate profiles exists before creating
- doValidateProfiles();
+ Container[] containers = fabricService.createContainers(url, name, isEnsembleServer, debugContainer, number);
+ // and set its profiles and versions after creation
+ postCreateContainer(containers);
+ return null;
+ }
+
+ @Override
+ protected void preCreateContainer(String name) {
+ super.preCreateContainer(name);
// validate number is not out of bounds
if (number < 1 || number > 99) {
throw new IllegalArgumentException("The number of containers must be between 1 and 99.");
}
- Container[] containers = fabricService.createContainers(url, name, isEnsembleServer, debugContainer, number);
- // and set its profiles and versions after creation
- postCreateContainer(containers);
- return null;
+ if (url == null && parent == null) {
+ throw new IllegalArgumentException("Either an url or a parent must be specified");
+ }
}
-
}
View
18 ...ic/fabric-commands/src/main/java/org/fusesource/fabric/commands/ContainerCreateChild.java
@@ -33,14 +33,9 @@
@Override
protected Object doExecute() throws Exception {
- // validate profiles exists before creating
- doValidateProfiles();
+ // validate input before creating containers
+ preCreateContainer(name);
- // validate number is not out of bounds
- if (number < 1 || number > 99) {
- throw new IllegalArgumentException("The number of containers must be between 1 and 99.");
- }
-
// okay create child container
String url = "child://" + parent;
Container[] containers = fabricService.createContainers(url, name, isEnsembleServer, debugContainer, number);
@@ -49,4 +44,13 @@ protected Object doExecute() throws Exception {
return null;
}
+ @Override
+ protected void preCreateContainer(String name) {
+ super.preCreateContainer(name);
+
+ // validate number is not out of bounds
+ if (number < 1 || number > 99) {
+ throw new IllegalArgumentException("The number of containers must be between 1 and 99.");
+ }
+ }
}
View
20 ...ic/fabric-commands/src/main/java/org/fusesource/fabric/commands/ContainerCreateCloud.java
@@ -58,14 +58,8 @@
@Override
protected Object doExecute() throws Exception {
- // validate profiles exists before creating
- doValidateProfiles();
-
- // validate number is not out of bounds
- if (number < 1 || number > 999) {
- // for cloud we accept 3 digits
- throw new IllegalArgumentException("The number of containers must be between 1 and 999.");
- }
+ // validate input before creating containers
+ preCreateContainer(name);
CreateJCloudsContainerArguments args = new CreateJCloudsContainerArguments();
args.setEnsembleServer(isEnsembleServer);
@@ -92,4 +86,14 @@ protected Object doExecute() throws Exception {
return null;
}
+ @Override
+ protected void preCreateContainer(String name) {
+ super.preCreateContainer(name);
+
+ // validate number is not out of bounds
+ if (number < 1 || number > 999) {
+ // for cloud we accept 3 digits
+ throw new IllegalArgumentException("The number of containers must be between 1 and 999.");
+ }
+ }
}
View
18 fabric/fabric-commands/src/main/java/org/fusesource/fabric/commands/ContainerCreateSsh.java
@@ -49,13 +49,8 @@
@Override
protected Object doExecute() throws Exception {
- // validate profiles exists before creating
- doValidateProfiles();
-
- // validate number is not out of bounds
- if (number < 1 || number > 99) {
- throw new IllegalArgumentException("The number of containers must be between 1 and 99.");
- }
+ // validate input before creating containers
+ preCreateContainer(name);
CreateSshContainerArguments args = new CreateSshContainerArguments();
args.setEnsembleServer(isEnsembleServer);
@@ -82,4 +77,13 @@ protected Object doExecute() throws Exception {
return null;
}
+ @Override
+ protected void preCreateContainer(String name) {
+ super.preCreateContainer(name);
+
+ // validate number is not out of bounds
+ if (number < 1 || number > 99) {
+ throw new IllegalArgumentException("The number of containers must be between 1 and 99.");
+ }
+ }
}
View
50 ...commands/src/main/java/org/fusesource/fabric/commands/support/ContainerCreateSupport.java
@@ -44,13 +44,43 @@
return names;
}
- protected void postCreateContainer(Container[] children) {
+ /**
+ * Pre validates input before creating the container(s)
+ *
+ * @param name the name of the container to create
+ * @throws IllegalArgumentException is thrown if input is invalid
+ */
+ protected void preCreateContainer(String name) throws IllegalArgumentException {
+ Container existing = getContainer(name);
+ if (existing != null) {
+ throw new IllegalArgumentException("A container with name " + name + " already exists.");
+ }
+
+ // get the profiles for the given version
+ Version ver = version != null ? fabricService.getVersion(version) : fabricService.getDefaultVersion();
+ Profile[] profiles = ver.getProfiles();
+
+ // validate profiles exists before creating a new container
+ List<String> names = getProfileNames();
+ for (String profile : names) {
+ if (!hasProfile(profiles, profile, ver)) {
+ throw new IllegalArgumentException("Profile " + profile + " with version " + ver.getName() + " does not exist.");
+ }
+ }
+ }
+
+ /**
+ * Post logic after the containers have been created.
+ *
+ * @param containers the created containers
+ */
+ protected void postCreateContainer(Container[] containers) {
Version ver = version != null ? fabricService.getVersion(version) : fabricService.getDefaultVersion();
List<String> names = getProfileNames();
try {
Profile[] profiles = getProfiles(version, names);
- for (Container child : children) {
+ for (Container child : containers) {
log.trace("Setting version " + ver.getName() + " on container " + child.getId());
child.setVersion(ver);
log.trace("Setting profiles " + Arrays.asList(profiles) + " on container " + child.getId());
@@ -61,21 +91,7 @@ protected void postCreateContainer(Container[] children) {
}
if (log.isDebugEnabled()) {
- log.debug("postCreateContainer completed for " + Arrays.asList(children) + " containers.");
- }
- }
-
- protected void doValidateProfiles() {
- // get the profiles for the given version
- Version ver = version != null ? fabricService.getVersion(version) : fabricService.getDefaultVersion();
- Profile[] profiles = ver.getProfiles();
-
- // validate profiles exists before creating a new container
- List<String> names = getProfileNames();
- for (String profile : names) {
- if (!hasProfile(profiles, profile, ver)) {
- throw new IllegalArgumentException("Profile " + profile + " with version " + ver.getName() + " does not exist.");
- }
+ log.debug("postCreateContainer completed for " + Arrays.asList(containers) + " containers.");
}
}

0 comments on commit fe8bb4d

Please sign in to comment.