diff options
Diffstat (limited to 'openstack-console')
28 files changed, 1349 insertions, 0 deletions
diff --git a/openstack-console/pom.xml b/openstack-console/pom.xml new file mode 100644 index 0000000..2990671 --- /dev/null +++ b/openstack-console/pom.xml @@ -0,0 +1,78 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.openecomp.mso</groupId> + <artifactId>openstack-java-sdk</artifactId> + <version>1.0.0-SNAPSHOT</version> + </parent> + <groupId>org.openecomp.mso.libs.openstack-java-sdk</groupId> + <artifactId>openstack-console</artifactId> + <name>OpenStack Console</name> + <description>OpenStack Console</description> + <dependencies> + <dependency> + <groupId>commons-cli</groupId> + <artifactId>commons-cli</artifactId> + <version>1.2</version> + </dependency> + <dependency> + <groupId>jline</groupId> + <artifactId>jline</artifactId> + <version>2.10</version> + </dependency> + <dependency> + <groupId>org.openecomp.mso</groupId> + <artifactId>keystone-client</artifactId> + <version>1.0.0-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.openecomp.mso</groupId> + <artifactId>nova-client</artifactId> + <version>1.0.0-SNAPSHOT</version> + </dependency> + </dependencies> + <profiles> + <profile> + <id>jersey</id> + <dependencies> + <dependency> + <groupId>org.openecomp.mso</groupId> + <artifactId>jersey-connector</artifactId> + <version>3.0.0-SNAPSHOT</version> + </dependency> + </dependencies> + </profile> + <profile> + <id>jersey2</id> + <dependencies> + <dependency> + <groupId>org.openecomp.mso</groupId> + <artifactId>jersey2-connector</artifactId> + <version>3.0.0-SNAPSHOT</version> + </dependency> + </dependencies> + </profile> + <profile> + <id>resteasy</id> + <dependencies> + <dependency> + <groupId>org.openecomp.mso</groupId> + <artifactId>resteasy-connector</artifactId> + <version>3.0.0-SNAPSHOT</version> + </dependency> + </dependencies> + </profile> + </profiles> + <build> + <plugins> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>exec-maven-plugin</artifactId> + <version>1.2.1</version> + <configuration> + <mainClass>org.openecomp.mso.openstack.console.Main</mainClass> + </configuration> + </plugin> + </plugins> + </build> +</project>
\ No newline at end of file diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/Command.java b/openstack-console/src/main/java/com/woorea/openstack/console/Command.java new file mode 100644 index 0000000..4c96e25 --- /dev/null +++ b/openstack-console/src/main/java/com/woorea/openstack/console/Command.java @@ -0,0 +1,21 @@ +package com.woorea.openstack.console; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Options; + + +public abstract class Command { + + protected String name; + + public Command(String name) { + this.name = name; + } + + public abstract void execute(Console console, CommandLine args); + + public Options getOptions() { + return new Options(); + } + +} diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/CommandLineHelper.java b/openstack-console/src/main/java/com/woorea/openstack/console/CommandLineHelper.java new file mode 100644 index 0000000..6e148ce --- /dev/null +++ b/openstack-console/src/main/java/com/woorea/openstack/console/CommandLineHelper.java @@ -0,0 +1,71 @@ +package com.woorea.openstack.console; + +import java.util.StringTokenizer; +import java.util.Vector; + +public class CommandLineHelper { + + public static String[] parse(String input) { + if (input == null || input.length() == 0) { + //no command? no string + return new String[0]; + } + // parse with a simple finite state machine + + final int normal = 0; + final int inQuote = 1; + final int inDoubleQuote = 2; + int state = normal; + StringTokenizer tok = new StringTokenizer(input, "\"\' ", true); + Vector v = new Vector(); + StringBuffer current = new StringBuffer(); + boolean lastTokenHasBeenQuoted = false; + + while (tok.hasMoreTokens()) { + String nextTok = tok.nextToken(); + switch (state) { + case inQuote: + if ("\'".equals(nextTok)) { + lastTokenHasBeenQuoted = true; + state = normal; + } else { + current.append(nextTok); + } + break; + case inDoubleQuote: + if ("\"".equals(nextTok)) { + lastTokenHasBeenQuoted = true; + state = normal; + } else { + current.append(nextTok); + } + break; + default: + if ("\'".equals(nextTok)) { + state = inQuote; + } else if ("\"".equals(nextTok)) { + state = inDoubleQuote; + } else if (" ".equals(nextTok)) { + if (lastTokenHasBeenQuoted || current.length() != 0) { + v.addElement(current.toString()); + current = new StringBuffer(); + } + } else { + current.append(nextTok); + } + lastTokenHasBeenQuoted = false; + break; + } + } + if (lastTokenHasBeenQuoted || current.length() != 0) { + v.addElement(current.toString()); + } + if (state == inQuote || state == inDoubleQuote) { + throw new RuntimeException("unbalanced quotes in " + input); + } + String[] args = new String[v.size()]; + v.copyInto(args); + return args; + } + +} diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/Commands.java b/openstack-console/src/main/java/com/woorea/openstack/console/Commands.java new file mode 100644 index 0000000..e8b39dc --- /dev/null +++ b/openstack-console/src/main/java/com/woorea/openstack/console/Commands.java @@ -0,0 +1,31 @@ +package com.woorea.openstack.console; + +import java.util.Map; + +import org.apache.commons.cli.CommandLine; + +public class Commands { + + public static final Command EXIT = new Command("exit") { + + @Override + public void execute(Console console, CommandLine args) { + console.exit(); + } + + }; + + public static final Command SET = new Command("set") { + + @Override + public void execute(Console console, CommandLine args) { + if(args.getArgs().length == 2) { + console.setProperty(args.getArgs()[0], args.getArgs()[1]); + } else { + console.properties(); + } + } + + }; + +} diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/Console.java b/openstack-console/src/main/java/com/woorea/openstack/console/Console.java new file mode 100644 index 0000000..13b7fbf --- /dev/null +++ b/openstack-console/src/main/java/com/woorea/openstack/console/Console.java @@ -0,0 +1,111 @@ +package com.woorea.openstack.console; + +import java.io.IOException; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Map; +import java.util.Properties; +import java.util.Set; + +import jline.UnsupportedTerminal; +import jline.console.ConsoleReader; +import jline.console.completer.Completer; +import jline.console.completer.StringsCompleter; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.GnuParser; +import org.apache.commons.cli.HelpFormatter; + +public class Console { + + private Properties properties; + + private ConsoleReader reader; + + private Environment environment; + + private HelpFormatter helpFormatter = new HelpFormatter(); + + private static final CommandLineParser PARSER = new GnuParser(); + + public Console(Environment environment, Properties properties) { + this.properties = properties; + this.environment = environment; + } + + public void start() throws IOException { + if(System.console() == null) { + reader = new ConsoleReader(System.in, System.out, new UnsupportedTerminal()); + } else { + reader = new ConsoleReader(); + } + do { + String line = reader.readLine(environment.getPrompt()); + execute(line); + } while(true); + } + + public void execute(String line) { + String[] tokens = CommandLineHelper.parse(line); + if(tokens.length > 0) { + Command command = environment.commands.get(tokens[0]); + if(command != null) { + try { + CommandLine args = Console.PARSER.parse(command.getOptions(), Arrays.copyOfRange(tokens, 1, tokens.length)); + command.execute(this, args); + } catch (Exception e) { + e.printStackTrace(); + helpFormatter.printHelp(command.name, command.getOptions()); + } + } + } + } + + public void setEnvironment(Environment environment) { + Set<Completer> completers = new HashSet<Completer>(reader.getCompleters()); + for(Completer c : completers) { + reader.removeCompleter(c); + } + Set<String> commands = new HashSet<String>(); + for(Map.Entry<String,Command> c : environment.commands.entrySet()) { + commands.add(c.getKey()); + } + reader.addCompleter(new StringsCompleter(commands)); + this.environment = environment; + } + + public Environment getEnvironment() { + return this.environment; + } + + /** + * @return the properties + */ + public String getProperty(String name) { + return properties.getProperty(name); + } + + /** + * @return the properties + */ + public void setProperty(String name, Object value) { + properties.put(name, value); + } + + public void properties() { + for(Map.Entry<Object, Object> entry : properties.entrySet()) { + System.out.printf("%25s = %55s",entry.getKey(), entry.getValue()); + } + } + + public void exit() { + if(environment.parent == null) { + System.out.println("Goodbye"); + System.exit(1); + } else { + environment = environment.parent; + } + } + +} diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/Environment.java b/openstack-console/src/main/java/com/woorea/openstack/console/Environment.java new file mode 100644 index 0000000..ca0eee4 --- /dev/null +++ b/openstack-console/src/main/java/com/woorea/openstack/console/Environment.java @@ -0,0 +1,30 @@ +package com.woorea.openstack.console; +import java.util.Map; +import java.util.TreeMap; + + +public class Environment { + + protected final Environment parent; + + protected Map<String, Command> commands = new TreeMap<String, Command>(); + + public Environment(Environment parent) { + register(Commands.EXIT); + register(Commands.SET); + this.parent = parent; + } + + public Environment() { + this(null); + } + + public void register(Command command) { + commands.put(command.name, command); + } + + public String getPrompt() { + return "> "; + } + +} diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/Main.java b/openstack-console/src/main/java/com/woorea/openstack/console/Main.java new file mode 100644 index 0000000..d469e54 --- /dev/null +++ b/openstack-console/src/main/java/com/woorea/openstack/console/Main.java @@ -0,0 +1,27 @@ +package com.woorea.openstack.console; + +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Properties; + +import com.woorea.openstack.console.keystone.KeystoneEnvironment; +import com.woorea.openstack.console.nova.NovaEnvironment; + +public class Main { + + /** + * @param args + */ + public static void main(String[] args) throws IOException { + Environment environment = new Environment(); + environment.register(KeystoneEnvironment.KEYSTONE); + environment.register(NovaEnvironment.NOVA); + + Properties properties = new Properties(); + properties.load(new FileInputStream("src/main/resources/console.properties")); + + Console console = new Console(environment, properties); + console.start(); + } + +} diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneCommand.java b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneCommand.java new file mode 100644 index 0000000..f15ad24 --- /dev/null +++ b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneCommand.java @@ -0,0 +1,24 @@ +package com.woorea.openstack.console.keystone; + +import org.apache.commons.cli.CommandLine; + +import com.woorea.openstack.console.Command; +import com.woorea.openstack.console.Console; +import com.woorea.openstack.keystone.Keystone; + +public abstract class KeystoneCommand extends Command { + + public KeystoneCommand(String name) { + super(name); + } + + @Override + public void execute(Console console, CommandLine args) { + KeystoneEnvironment environment = (KeystoneEnvironment) console.getEnvironment(); + execute(environment.CLIENT, args); + + } + + protected abstract void execute(Keystone keystone, CommandLine args); + +} diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneEnvironment.java b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneEnvironment.java new file mode 100644 index 0000000..0d0a6a2 --- /dev/null +++ b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneEnvironment.java @@ -0,0 +1,63 @@ +package com.woorea.openstack.console.keystone; + +import org.apache.commons.cli.CommandLine; + +import com.woorea.openstack.base.client.OpenStackSimpleTokenProvider; +import com.woorea.openstack.console.Command; +import com.woorea.openstack.console.Console; +import com.woorea.openstack.console.Environment; +import com.woorea.openstack.keystone.Keystone; +import com.woorea.openstack.keystone.model.Access; +import com.woorea.openstack.keystone.model.authentication.UsernamePassword; + +public class KeystoneEnvironment extends Environment { + + public final Keystone CLIENT; + + public static final Command KEYSTONE = new Command("keystone") { + + @Override + public void execute(Console console, CommandLine args) { + + Keystone client = new Keystone(console.getProperty("keystone.endpoint")); + + Access access = client.tokens() + .authenticate(new UsernamePassword( + console.getProperty("keystone.username"), + console.getProperty("keystone.password") + )) + .withTenantName(console.getProperty("keystone.tenant_name")) + .execute(); + + client.setTokenProvider(new OpenStackSimpleTokenProvider(access.getToken().getId())); + + KeystoneEnvironment environment = new KeystoneEnvironment(console.getEnvironment(), client); + + environment.register(new KeystoneTenantList()); + environment.register(new KeystoneTenantCreate()); + environment.register(new KeystoneTenantDelete()); + environment.register(new KeystoneUserList()); + environment.register(new KeystoneUserCreate()); + environment.register(new KeystoneUserDelete()); + environment.register(new KeystoneRoleList()); + environment.register(new KeystoneRoleDelete()); + environment.register(new KeystoneServiceList()); + console.setEnvironment(environment); + } + + }; + + public KeystoneEnvironment(Environment parent, Keystone client) { + super(parent); + CLIENT = client; + } + + /* (non-Javadoc) + * @see org.woorea.wsh.Environment#getPrompt() + */ + @Override + public String getPrompt() { + return "keystone> "; + } + +} diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneRoleCreate.java b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneRoleCreate.java new file mode 100644 index 0000000..2acdd76 --- /dev/null +++ b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneRoleCreate.java @@ -0,0 +1,69 @@ +package com.woorea.openstack.console.keystone; + +import java.util.Arrays; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Options; + +import com.woorea.openstack.console.utils.Column; +import com.woorea.openstack.console.utils.Table; +import com.woorea.openstack.console.utils.TableModel; +import com.woorea.openstack.keystone.Keystone; +import com.woorea.openstack.keystone.model.Role; + +public class KeystoneRoleCreate extends KeystoneCommand { + + public KeystoneRoleCreate() { + super( "role-create"); + } + + @Override + public void execute(Keystone keystone, CommandLine cmd) { + + Role role = new Role(); + role.setName(cmd.getOptionValue("name")); + role.setDescription(cmd.getOptionValue("description")); + if(cmd.getOptionValue("enabled") != null) { + role.setEnabled("True"); + } + + role = keystone.roles().create(role).execute(); + + Table t = new Table(new TableModel<Role>(Arrays.asList(role)) { + + @Override + public Column[] getHeaders() { + return new Column[]{ + new Column("id", 32, Column.ALIGN_LEFT), + new Column("name", 10, Column.ALIGN_LEFT), + new Column("description", 32, Column.ALIGN_LEFT), + new Column("enabled", 7, Column.ALIGN_LEFT) + }; + } + + @Override + public String[] getRow(Role tenant) { + return new String[]{ + tenant.getId(), + tenant.getName(), + tenant.getDescription(), + tenant.getEnabled().toString() + }; + } + }); + System.out.println(t.render()); + } + + /* (non-Javadoc) + * @see com.billingstack.commands.Command#getOptions() + */ + @Override + public Options getOptions() { + Options opts = super.getOptions(); + opts.addOption(null, "name", true, "tenant name"); + opts.addOption(null, "description", true, "tenant description"); + opts.addOption(null, "enabled", false, "enabled"); + return opts; + } + +} diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneRoleDelete.java b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneRoleDelete.java new file mode 100644 index 0000000..d3977b7 --- /dev/null +++ b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneRoleDelete.java @@ -0,0 +1,25 @@ +package com.woorea.openstack.console.keystone; + +import org.apache.commons.cli.CommandLine; + +import com.woorea.openstack.console.utils.ConsoleUtils; +import com.woorea.openstack.keystone.Keystone; + +public class KeystoneRoleDelete extends KeystoneCommand { + + public KeystoneRoleDelete() { + super("role-delete"); + } + + @Override + public void execute(Keystone keystone, CommandLine cmd) { + + String[] args = cmd.getArgs(); + if(args.length == 1) { + keystone.roles().delete(args[0]).execute(); + System.out.println(new ConsoleUtils().green("OK")); + } + + } + +} diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneRoleList.java b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneRoleList.java new file mode 100644 index 0000000..13b7175 --- /dev/null +++ b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneRoleList.java @@ -0,0 +1,48 @@ +package com.woorea.openstack.console.keystone; + +import org.apache.commons.cli.CommandLine; + +import com.woorea.openstack.console.utils.Column; +import com.woorea.openstack.console.utils.Table; +import com.woorea.openstack.console.utils.TableModel; +import com.woorea.openstack.keystone.Keystone; +import com.woorea.openstack.keystone.model.Role; +import com.woorea.openstack.keystone.model.Roles; + +public class KeystoneRoleList extends KeystoneCommand { + + public KeystoneRoleList() { + super("role-list"); + } + + @Override + public void execute(Keystone keystone, CommandLine cmd) { + + final Roles roles = keystone.roles().list().execute(); + + Table t = new Table(new TableModel<Role>(roles.getList()) { + + @Override + public Column[] getHeaders() { + return new Column[]{ + new Column("id", 32, Column.ALIGN_LEFT), + new Column("name", 10, Column.ALIGN_LEFT), + new Column("description", 32, Column.ALIGN_LEFT), + new Column("enabled", 7, Column.ALIGN_LEFT), + }; + } + + @Override + public String[] getRow(Role role) { + return new String[]{ + role.getId(), + role.getName(), + role.getDescription(), + role.getEnabled() + }; + } + }); + System.out.println(t.render()); + } + +} diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneServiceList.java b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneServiceList.java new file mode 100644 index 0000000..27542c1 --- /dev/null +++ b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneServiceList.java @@ -0,0 +1,48 @@ +package com.woorea.openstack.console.keystone; + +import org.apache.commons.cli.CommandLine; + +import com.woorea.openstack.console.utils.Column; +import com.woorea.openstack.console.utils.Table; +import com.woorea.openstack.console.utils.TableModel; +import com.woorea.openstack.keystone.Keystone; +import com.woorea.openstack.keystone.model.Service; +import com.woorea.openstack.keystone.model.Services; + +public class KeystoneServiceList extends KeystoneCommand { + + public KeystoneServiceList() { + super("service-list"); + } + + @Override + public void execute(Keystone keystone, CommandLine cmd) { + + final Services services = keystone.services().list().execute(); + + Table t = new Table(new TableModel<Service>(services.getList()) { + + @Override + public Column[] getHeaders() { + return new Column[]{ + new Column("id", 32, Column.ALIGN_LEFT), + new Column("type", 10, Column.ALIGN_LEFT), + new Column("name", 10, Column.ALIGN_LEFT), + new Column("description", 32, Column.ALIGN_LEFT) + }; + } + + @Override + public String[] getRow(Service service) { + return new String[]{ + service.getId(), + service.getType(), + service.getName(), + service.getDescription() + }; + } + }); + System.out.println(t.render()); + } + +} diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneTenantCreate.java b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneTenantCreate.java new file mode 100644 index 0000000..a8336ea --- /dev/null +++ b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneTenantCreate.java @@ -0,0 +1,69 @@ +package com.woorea.openstack.console.keystone; + +import java.util.Arrays; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Options; + +import com.woorea.openstack.console.utils.Column; +import com.woorea.openstack.console.utils.Table; +import com.woorea.openstack.console.utils.TableModel; +import com.woorea.openstack.keystone.Keystone; +import com.woorea.openstack.keystone.model.Tenant; + +public class KeystoneTenantCreate extends KeystoneCommand { + + public KeystoneTenantCreate() { + super("tenant-create"); + } + + @Override + public void execute(Keystone keystone, CommandLine cmd) { + + Tenant tenant = new Tenant(); + tenant.setName(cmd.getOptionValue("name")); + tenant.setDescription(cmd.getOptionValue("description")); + if(cmd.getOptionValue("enabled") != null) { + tenant.setEnabled(Boolean.TRUE); + } + + tenant = keystone.tenants().create(tenant).execute(); + + Table t = new Table(new TableModel<Tenant>(Arrays.asList(tenant)) { + + @Override + public Column[] getHeaders() { + return new Column[]{ + new Column("id", 32, Column.ALIGN_LEFT), + new Column("name", 10, Column.ALIGN_LEFT), + new Column("description", 32, Column.ALIGN_LEFT), + new Column("enabled", 7, Column.ALIGN_LEFT) + }; + } + + @Override + public String[] getRow(Tenant tenant) { + return new String[]{ + tenant.getId(), + tenant.getName(), + tenant.getDescription(), + tenant.getEnabled().toString() + }; + } + }); + System.out.println(t.render()); + } + + /* (non-Javadoc) + * @see com.billingstack.commands.Command#getOptions() + */ + @Override + public Options getOptions() { + Options opts = super.getOptions(); + opts.addOption(null, "name", true, "tenant name"); + opts.addOption(null, "description", true, "tenant description"); + opts.addOption(null, "enabled", false, "enabled"); + return opts; + } + +} diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneTenantDelete.java b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneTenantDelete.java new file mode 100644 index 0000000..a79da6a --- /dev/null +++ b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneTenantDelete.java @@ -0,0 +1,25 @@ +package com.woorea.openstack.console.keystone; + +import org.apache.commons.cli.CommandLine; + +import com.woorea.openstack.console.utils.ConsoleUtils; +import com.woorea.openstack.keystone.Keystone; + +public class KeystoneTenantDelete extends KeystoneCommand { + + public KeystoneTenantDelete() { + super("tenant-delete"); + } + + @Override + public void execute(Keystone keystone, CommandLine cmd) { + + String[] args = cmd.getArgs(); + if(args.length == 1) { + keystone.tenants().delete(args[0]).execute(); + System.out.println(new ConsoleUtils().green("OK")); + } + + } + +} diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneTenantList.java b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneTenantList.java new file mode 100644 index 0000000..24ff73b --- /dev/null +++ b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneTenantList.java @@ -0,0 +1,48 @@ +package com.woorea.openstack.console.keystone; + +import org.apache.commons.cli.CommandLine; + +import com.woorea.openstack.console.utils.Column; +import com.woorea.openstack.console.utils.Table; +import com.woorea.openstack.console.utils.TableModel; +import com.woorea.openstack.keystone.Keystone; +import com.woorea.openstack.keystone.model.Tenant; +import com.woorea.openstack.keystone.model.Tenants; + +public class KeystoneTenantList extends KeystoneCommand { + + public KeystoneTenantList() { + super("tenant-list"); + } + + @Override + public void execute(Keystone keystone, CommandLine args) { + + final Tenants tenants = keystone.tenants().list().execute(); + + Table t = new Table(new TableModel<Tenant>(tenants.getList()) { + + @Override + public Column[] getHeaders() { + return new Column[]{ + new Column("id", 32, Column.ALIGN_LEFT), + new Column("name", 32, Column.ALIGN_LEFT), + new Column("description", 32, Column.ALIGN_LEFT), + new Column("enabled", 7, Column.ALIGN_LEFT) + }; + } + + @Override + public String[] getRow(Tenant tenant) { + return new String[]{ + tenant.getId(), + tenant.getName(), + tenant.getDescription(), + tenant.getEnabled().toString() + }; + } + }); + System.out.println(t.render()); + } + +} diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneUserCreate.java b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneUserCreate.java new file mode 100644 index 0000000..2628767 --- /dev/null +++ b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneUserCreate.java @@ -0,0 +1,75 @@ +package com.woorea.openstack.console.keystone; + +import java.util.Arrays; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Options; + +import com.woorea.openstack.console.utils.Column; +import com.woorea.openstack.console.utils.Table; +import com.woorea.openstack.console.utils.TableModel; +import com.woorea.openstack.keystone.Keystone; +import com.woorea.openstack.keystone.model.User; + +public class KeystoneUserCreate extends KeystoneCommand { + + public KeystoneUserCreate() { + super("user-create"); + } + + @Override + public void execute(Keystone keystone, CommandLine cmd) { + + User user = new User(); + user.setName(cmd.getOptionValue("name")); + user.setPassword(cmd.getOptionValue("password")); + user.setEmail(cmd.getOptionValue("email")); + user.setTenantId(cmd.getOptionValue("tenant")); + if(cmd.getOptionValue("enabled") != null) { + user.setEnabled(Boolean.TRUE); + } + + user = keystone.users().create(user).execute(); + + Table t = new Table(new TableModel<User>(Arrays.asList(user)) { + + @Override + public Column[] getHeaders() { + return new Column[]{ + new Column("id", 32, Column.ALIGN_LEFT), + new Column("name", 10, Column.ALIGN_LEFT), + new Column("email", 22, Column.ALIGN_LEFT), + new Column("tenant", 32, Column.ALIGN_LEFT), + new Column("enabled", 7, Column.ALIGN_LEFT) + }; + } + + @Override + public String[] getRow(User user) { + return new String[]{ + user.getId(), + user.getName(), + user.getEmail(), + user.getTenantId(), + user.getEnabled().toString() + }; + } + }); + System.out.println(t.render()); + } + + /* (non-Javadoc) + * @see com.billingstack.commands.Command#getOptions() + */ + @Override + public Options getOptions() { + Options opts = super.getOptions(); + opts.addOption(null, "name", true, "user name"); + opts.addOption(null, "password", true, "user password"); + opts.addOption(null, "email", true, "user email"); + opts.addOption(null, "tenant", true, "tenant id"); + opts.addOption(null, "enabled", false, "enabled"); + return opts; + } + +} diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneUserDelete.java b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneUserDelete.java new file mode 100644 index 0000000..77deff4 --- /dev/null +++ b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneUserDelete.java @@ -0,0 +1,25 @@ +package com.woorea.openstack.console.keystone; + +import org.apache.commons.cli.CommandLine; + +import com.woorea.openstack.console.utils.ConsoleUtils; +import com.woorea.openstack.keystone.Keystone; + +public class KeystoneUserDelete extends KeystoneCommand { + + public KeystoneUserDelete() { + super("user-delete"); + } + + @Override + public void execute(Keystone keystone, CommandLine cmd) { + + String[] args = cmd.getArgs(); + if(args.length == 1) { + keystone.users().delete(args[0]).execute(); + System.out.println(new ConsoleUtils().green("OK")); + } + + } + +} diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneUserList.java b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneUserList.java new file mode 100644 index 0000000..90bcde4 --- /dev/null +++ b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneUserList.java @@ -0,0 +1,50 @@ +package com.woorea.openstack.console.keystone; + +import org.apache.commons.cli.CommandLine; + +import com.woorea.openstack.console.utils.Column; +import com.woorea.openstack.console.utils.Table; +import com.woorea.openstack.console.utils.TableModel; +import com.woorea.openstack.keystone.Keystone; +import com.woorea.openstack.keystone.model.User; +import com.woorea.openstack.keystone.model.Users; + +public class KeystoneUserList extends KeystoneCommand { + + public KeystoneUserList() { + super("user-list"); + } + + @Override + public void execute(Keystone keystone, CommandLine cmd) { + + final Users users = keystone.users().list().execute(); + + Table t = new Table(new TableModel<User>(users.getList()) { + + @Override + public Column[] getHeaders() { + return new Column[]{ + new Column("id", 32, Column.ALIGN_LEFT), + new Column("name", 10, Column.ALIGN_LEFT), + new Column("email", 22, Column.ALIGN_LEFT), + new Column("tenant", 32, Column.ALIGN_LEFT), + new Column("enabled", 7, Column.ALIGN_LEFT) + }; + } + + @Override + public String[] getRow(User user) { + return new String[]{ + user.getId(), + user.getName(), + user.getEmail(), + user.getTenantId(), + user.getEnabled().toString() + }; + } + }); + System.out.println(t.render()); + } + +} diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneUserShow.java b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneUserShow.java new file mode 100644 index 0000000..29a447f --- /dev/null +++ b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneUserShow.java @@ -0,0 +1,53 @@ +package com.woorea.openstack.console.keystone; + +import java.util.Arrays; + +import org.apache.commons.cli.CommandLine; + +import com.woorea.openstack.console.utils.Column; +import com.woorea.openstack.console.utils.Table; +import com.woorea.openstack.console.utils.TableModel; +import com.woorea.openstack.keystone.Keystone; +import com.woorea.openstack.keystone.model.User; + +public class KeystoneUserShow extends KeystoneCommand { + + public KeystoneUserShow() { + super("user-show"); + } + + @Override + public void execute(Keystone keystone, CommandLine cmd) { + + String[] args = cmd.getArgs(); + if(args.length == 1) { + User user = keystone.users().show(args[0]).execute(); + Table t = new Table(new TableModel<User>(Arrays.asList(user)) { + + @Override + public Column[] getHeaders() { + return new Column[]{ + new Column("id", 32, Column.ALIGN_LEFT), + new Column("name", 10, Column.ALIGN_LEFT), + new Column("email", 22, Column.ALIGN_LEFT), + new Column("tenant", 32, Column.ALIGN_LEFT), + new Column("enabled", 7, Column.ALIGN_LEFT) + }; + } + + @Override + public String[] getRow(User user) { + return new String[]{ + user.getId(), + user.getName(), + user.getEmail(), + user.getTenantId(), + user.getEnabled().toString() + }; + } + }); + System.out.println(t.render()); + } + } + +} diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/nova/NovaCommand.java b/openstack-console/src/main/java/com/woorea/openstack/console/nova/NovaCommand.java new file mode 100644 index 0000000..b406fbc --- /dev/null +++ b/openstack-console/src/main/java/com/woorea/openstack/console/nova/NovaCommand.java @@ -0,0 +1,25 @@ +package com.woorea.openstack.console.nova; + +import org.apache.commons.cli.CommandLine; + +import com.woorea.openstack.console.Command; +import com.woorea.openstack.console.Console; +import com.woorea.openstack.nova.Nova; + + +public abstract class NovaCommand extends Command { + + public NovaCommand(String name) { + super(name); + } + + @Override + public void execute(Console console, CommandLine args) { + NovaEnvironment environment = (NovaEnvironment) console.getEnvironment(); + execute(environment.CLIENT, args); + + } + + protected abstract void execute(Nova nova, CommandLine args); + +} diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/nova/NovaEnvironment.java b/openstack-console/src/main/java/com/woorea/openstack/console/nova/NovaEnvironment.java new file mode 100644 index 0000000..bb2e5c4 --- /dev/null +++ b/openstack-console/src/main/java/com/woorea/openstack/console/nova/NovaEnvironment.java @@ -0,0 +1,65 @@ +package com.woorea.openstack.console.nova; + +import org.apache.commons.cli.CommandLine; + +import com.woorea.openstack.base.client.OpenStackSimpleTokenProvider; +import com.woorea.openstack.console.Command; +import com.woorea.openstack.console.Console; +import com.woorea.openstack.console.Environment; +import com.woorea.openstack.keystone.Keystone; +import com.woorea.openstack.keystone.model.Access; +import com.woorea.openstack.keystone.model.authentication.UsernamePassword; +import com.woorea.openstack.nova.Nova; + +public class NovaEnvironment extends Environment { + + public final Nova CLIENT; + + public static final Command NOVA = new Command("nova") { + + @Override + public void execute(Console console, CommandLine args) { + + if(args.getArgs().length == 1) { + Keystone keystone = new Keystone((String) console.getProperty("keystone.endpoint")); + + Access access = keystone.tokens().authenticate( + new UsernamePassword( + console.getProperty("keystone.username"), + console.getProperty("keystone.password") + ) + ) + .withTenantName(console.getProperty("keystone.tenant_name")) + .execute(); + + System.out.println(console.getProperty("nova.endpoint")); + + Nova client = new Nova(console.getProperty("nova.endpoint")+args.getArgs()[0]); + client.setTokenProvider(new OpenStackSimpleTokenProvider(access.getToken().getId())); + + NovaEnvironment environment = new NovaEnvironment(console.getEnvironment(), client); + + environment.register(new NovaServerList()); + + console.setEnvironment(environment); + + } + + } + + }; + + public NovaEnvironment(Environment parent, Nova client) { + super(parent); + CLIENT = client; + } + + /* (non-Javadoc) + * @see org.woorea.wsh.Environment#getPrompt() + */ + @Override + public String getPrompt() { + return "nova> "; + } + +} diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/nova/NovaServerList.java b/openstack-console/src/main/java/com/woorea/openstack/console/nova/NovaServerList.java new file mode 100644 index 0000000..515ca2b --- /dev/null +++ b/openstack-console/src/main/java/com/woorea/openstack/console/nova/NovaServerList.java @@ -0,0 +1,44 @@ +package com.woorea.openstack.console.nova; + +import org.apache.commons.cli.CommandLine; + +import com.woorea.openstack.console.utils.Column; +import com.woorea.openstack.console.utils.Table; +import com.woorea.openstack.console.utils.TableModel; +import com.woorea.openstack.nova.Nova; +import com.woorea.openstack.nova.model.Server; +import com.woorea.openstack.nova.model.Servers; + +public class NovaServerList extends NovaCommand { + + public NovaServerList() { + super("list"); + } + + @Override + public void execute(Nova nova, CommandLine cmd) { + + final Servers servers = nova.servers().list(true).execute(); + + Table t = new Table(new TableModel<Server>(servers.getList()) { + + @Override + public Column[] getHeaders() { + return new Column[]{ + new Column("id", 32, Column.ALIGN_LEFT), + new Column("name", 10, Column.ALIGN_LEFT) + }; + } + + @Override + public String[] getRow(Server server) { + return new String[]{ + server.getId(), + server.getName() + }; + } + }); + System.out.println(t.render()); + } + +} diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/utils/Column.java b/openstack-console/src/main/java/com/woorea/openstack/console/utils/Column.java new file mode 100644 index 0000000..08ffa19 --- /dev/null +++ b/openstack-console/src/main/java/com/woorea/openstack/console/utils/Column.java @@ -0,0 +1,65 @@ +package com.woorea.openstack.console.utils; + +public class Column { + + public static final int ALIGN_LEFT = -1; + public static final int ALIGN_RIGHT = 1; + + private String name; + + private int size; + + private int align; + + public Column(String name, int size, int align) { + super(); + this.name = name; + this.size = size; + this.align = align; + } + + /** + * @return the name + */ + public String getName() { + return name; + } + + /** + * @param name the name to set + */ + public void setName(String name) { + this.name = name; + } + + /** + * @return the size + */ + public int getSize() { + return size; + } + + /** + * @param size the size to set + */ + public void setSize(int size) { + this.size = size; + } + + /** + * @return the align + */ + public int getAlign() { + return align; + } + + /** + * @param align the align to set + */ + public void setAlign(int align) { + this.align = align; + } + + + +} diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/utils/ConsoleUtils.java b/openstack-console/src/main/java/com/woorea/openstack/console/utils/ConsoleUtils.java new file mode 100644 index 0000000..8212db6 --- /dev/null +++ b/openstack-console/src/main/java/com/woorea/openstack/console/utils/ConsoleUtils.java @@ -0,0 +1,47 @@ +package com.woorea.openstack.console.utils; + +public class ConsoleUtils { + + public static final String RED = "\u001B[31m"; + + public static final String GREEN = "\u001B[32m"; + + public static final String YELLOW = "\u001B[33m"; + + public static final String END = "\u001B[0m"; + + private StringBuilder sb = new StringBuilder(); + + public ConsoleUtils append(String text) { + sb.append(text); + return this; + } + + public ConsoleUtils red(String text) { + sb.append(ConsoleUtils.RED).append(text).append(END); + return this; + } + + public ConsoleUtils green(String text) { + sb.append(ConsoleUtils.GREEN).append(text).append(END); + return this; + } + + public ConsoleUtils yellow(String text) { + sb.append(ConsoleUtils.YELLOW).append(text).append(END); + return this; + } + + public static void log(String text) { + System.out.println(new ConsoleUtils().yellow("| ").append(text)); + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return sb.toString(); + } + +} diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/utils/Table.java b/openstack-console/src/main/java/com/woorea/openstack/console/utils/Table.java new file mode 100644 index 0000000..b49ede5 --- /dev/null +++ b/openstack-console/src/main/java/com/woorea/openstack/console/utils/Table.java @@ -0,0 +1,81 @@ +package com.woorea.openstack.console.utils; + + +public class Table { + + private StringBuilder sb = new StringBuilder(); + + private TableModel<?> model; + + public Table(TableModel<?> model) { + this.model = model; + } + + public StringBuilder render() { + header(); + for(String[] row : model.getRows()) { + int i = 0; + for(String column : row) { + Column columnModel = model.getHeaders()[i]; + sb.append("| "); + if(column != null) { + if(Column.ALIGN_RIGHT == columnModel.getAlign()) { + for(int j = 0; j < columnModel.getSize() - column.length(); j++) { + sb.append(" "); + } + } + sb.append(column.length() <= columnModel.getSize() ? column : column.substring(0, columnModel.getSize())); + if(Column.ALIGN_LEFT == columnModel.getAlign()) { + for(int j = 0; j < columnModel.getSize() - column.length(); j++) { + sb.append(" "); + } + } + } else { + for(int k = 0; k < columnModel.getSize(); k++) { + sb.append(" "); + } + } + sb.append(" "); + i++; + } + sb.append("|\n"); + } + for(Column c : model.getHeaders()) { + sb.append("+"); + for(int i = 0; i < c.getSize() + 2; i++) { + sb.append("-"); + } + } + sb.append("+\n"); + return sb; + } + + public void header() { + for(Column c : model.getHeaders()) { + sb.append("+"); + for(int i = 0; i < c.getSize() + 2; i++) { + sb.append("-"); + } + } + sb.append("+\n"); + + for(Column c : model.getHeaders()) { + sb.append("| "); + sb.append(c.getName()); + for(int i = 0; i < c.getSize() - c.getName().length(); i++) { + sb.append(" "); + } + sb.append(" "); + } + sb.append("|\n"); + + for(Column c : model.getHeaders()) { + sb.append("+"); + for(int i = 0; i < c.getSize() + 2; i++) { + sb.append("-"); + } + } + sb.append("+\n"); + } + +} diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/utils/TableModel.java b/openstack-console/src/main/java/com/woorea/openstack/console/utils/TableModel.java new file mode 100644 index 0000000..f1ae84a --- /dev/null +++ b/openstack-console/src/main/java/com/woorea/openstack/console/utils/TableModel.java @@ -0,0 +1,25 @@ +package com.woorea.openstack.console.utils; + +import java.util.List; + +public abstract class TableModel<T> { + + protected List<T> data; + + public TableModel(List<T> data) { + this.data = data; + } + + public abstract Column[] getHeaders(); + + public final String[][] getRows() { + String[][] rows = new String[data.size()][]; + for(int i = 0; i < data.size(); i++) { + rows[i] = getRow(data.get(i)); + } + return rows; + } + + public abstract String[] getRow(T data); + +} diff --git a/openstack-console/src/main/resources/console.properties b/openstack-console/src/main/resources/console.properties new file mode 100644 index 0000000..01902e4 --- /dev/null +++ b/openstack-console/src/main/resources/console.properties @@ -0,0 +1,6 @@ +keystone.endpoint=http://keystone.stacksherpa.org/v2.0 +keystone.username=admin +keystone.password=secret0 +keystone.tenant_name=admin + +nova.endpoint=http://compute/v2/
\ No newline at end of file |