Permalink
Browse files

benn-cs pull request

  • Loading branch information...
1 parent 3b76c34 commit caef3168aadb64e2af4e0c2db741b9cb7eaf6653 @woorea committed Jan 21, 2013
@@ -0,0 +1,78 @@
+package org.openstack.common.command;
+
+import java.lang.reflect.Field;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.ws.rs.client.WebTarget;
+
+import org.codehaus.jackson.annotate.JsonProperty;
+
+public class AbsOpenStackCmd<T> {
+
+ private T query;
+
+ protected AbsOpenStackCmd(T query)
+ {
+ this.setQuery(query);
+ }
+
+ /**
+ * @param query the query to set
+ */
+ public void setQuery(T query) {
+ this.query = query;
+ }
+
+ /**
+ * @return the query
+ */
+ public T getQuery() {
+ return query;
+ }
+
+ private String getFieldValue(Field field, T target)
+ {
+ try {
+ field.setAccessible(true);
+ Object obj = field.get(target);
+ if (obj == null)
+ return null;
+ return obj.toString();
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private Map<String, String> getObjectParamMap(T target)
+ {
+ Map<String, String> resultMap = new HashMap<String, String>();
+ if (target == null)
+ return resultMap;
+
+ Field[] paramMap = target.getClass().getDeclaredFields();
+ for (Field field : paramMap) {
+ String resultName = field.getName();
+ String value = getFieldValue(field, target);
+ if (value != null) {
+ JsonProperty prop = field.getAnnotation(JsonProperty.class);
+ if (prop != null) {
+ resultMap.put(prop.value(), getFieldValue(field, target));
+ } else {
+ resultMap.put(resultName, getFieldValue(field, target));
+ }
+ }
+ }
+
+ return resultMap;
+ }
+
+ public WebTarget queryParam(WebTarget target) {
+ Map<String, String> map = getObjectParamMap(this.query);
+ for (String fieldName : map.keySet()) {
+ target = target.queryParam(fieldName, map.get(fieldName));
+ }
+ return target;
+ }
+
+}
@@ -19,10 +19,18 @@
<artifactId>nova-client</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
+<<<<<<< HEAD
<dependency>
<groupId>org.openstack</groupId>
<artifactId>swift-client</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
+=======
+ <dependency>
+ <groupId>org.openstack</groupId>
+ <artifactId>quantum-client</artifactId>
+ <version>2.0.0-SNAPSHOT</version>
+ </dependency>
+>>>>>>> 31181c9f4644eeb9e8c66f77eedc72d58909ad6f
</dependencies>
</project>
@@ -0,0 +1,40 @@
+package org.openstack.examples.keystone;
+
+import org.openstack.keystone.KeystoneClient;
+import org.openstack.keystone.api.Authenticate;
+import org.openstack.keystone.api.CreateTenant;
+import org.openstack.keystone.api.DeleteTenant;
+import org.openstack.keystone.model.Access;
+import org.openstack.keystone.model.Tenant;
+import org.openstack.keystone.utils.KeystoneUtils;
+
+public class KeystoneCreateTenant {
+
+ private static final String KEYSTONE_AUTH_URL = "http://10.1.245.150:5000/v2.0";
+
+ private static final String KEYSTONE_USERNAME = "admin";
+
+ private static final String KEYSTONE_PASSWORD = "admin";
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ KeystoneClient keystone = new KeystoneClient(KEYSTONE_AUTH_URL);
+ //access with unscoped token
+ Access access = keystone.execute(Authenticate.withPasswordCredentials(KEYSTONE_USERNAME, KEYSTONE_PASSWORD));
+
+ access = keystone.execute(Authenticate.withToken(access.getToken().getId()).withTenantName("admin"));
+
+ Tenant tenant = new Tenant();
+ tenant.setName("benn.cs");
+ tenant.setDescription("benn.cs");
+ tenant.setEnabled(true);
+ //Get the adminURL client and use the token got above
+ keystone = new KeystoneClient(KeystoneUtils.findEndpointURL(access.getServiceCatalog(), "identity", null, "admin"),
+ access.getToken().getId());
+ tenant = keystone.execute(new CreateTenant(tenant));
+ System.out.println(tenant);
+ keystone.execute(new DeleteTenant(tenant.getId()));
+ }
+}
@@ -0,0 +1,53 @@
+package org.openstack.examples.network;
+
+import org.openstack.keystone.KeystoneClient;
+import org.openstack.keystone.api.Authenticate;
+import org.openstack.keystone.api.ListTenants;
+import org.openstack.keystone.model.Access;
+import org.openstack.keystone.model.Tenants;
+import org.openstack.keystone.utils.KeystoneUtils;
+import org.openstack.quantum.api.NetworkCore;
+import org.openstack.quantum.client.QuantumClient;
+import org.openstack.quantum.model.Network;
+import org.openstack.quantum.model.Networks;
+
+public class QuantumListNetworks {
+
+ private static final String KEYSTONE_AUTH_URL = "http://10.1.245.150:5000/v2.0";
+
+ private static final String KEYSTONE_USERNAME = "admin";
+
+ private static final String KEYSTONE_PASSWORD = "admin";
+
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ KeystoneClient keystone = new KeystoneClient(KEYSTONE_AUTH_URL);
+ // access with unscoped token
+ Access access = keystone.execute(Authenticate.withPasswordCredentials(
+ KEYSTONE_USERNAME, KEYSTONE_PASSWORD));
+ // use the token in the following requests
+ keystone.setToken(access.getToken().getId());
+
+ Tenants tenants = keystone.execute(new ListTenants());
+ // try to exchange token using the first tenant
+ if (tenants.getList().size() > 0) {
+ // access with tenant
+ access = keystone.execute(Authenticate.withToken(access.getToken().getId())
+ .withTenantId(tenants.getList().get(0).getId()));
+
+ QuantumClient quantumClient = new QuantumClient(KeystoneUtils
+ .findEndpointURL(access.getServiceCatalog(), "network", null, "public"),
+ access.getToken().getId());
+
+ Networks networks = quantumClient.execute(NetworkCore.listNetworks());
+ for (Network network : networks) {
+ System.out.println(network);
+ }
+ } else {
+ System.out.println("No tenants found!");
+ }
+ }
+}
@@ -0,0 +1,68 @@
+package org.openstack.examples.network;
+
+import org.openstack.keystone.KeystoneClient;
+import org.openstack.keystone.api.Authenticate;
+import org.openstack.keystone.api.ListTenants;
+import org.openstack.keystone.model.Access;
+import org.openstack.keystone.model.Tenants;
+import org.openstack.keystone.utils.KeystoneUtils;
+import org.openstack.quantum.api.SubnetsCore;
+import org.openstack.quantum.api.query.NetworkQuery;
+import org.openstack.quantum.client.QuantumClient;
+import org.openstack.quantum.model.Network;
+import org.openstack.quantum.model.Networks;
+import org.openstack.quantum.model.Subnet;
+import org.openstack.quantum.model.Subnets;
+
+public class QuantumQueryNetworks {
+
+ private static final String KEYSTONE_AUTH_URL = "http://10.1.245.150:5000/v2.0";
+
+ private static final String KEYSTONE_USERNAME = "admin";
+
+ private static final String KEYSTONE_PASSWORD = "admin";
+
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ KeystoneClient keystone = new KeystoneClient(KEYSTONE_AUTH_URL);
+ // access with unscoped token
+ Access access = keystone.execute(Authenticate.withPasswordCredentials(
+ KEYSTONE_USERNAME, KEYSTONE_PASSWORD));
+ // use the token in the following requests
+ keystone.setToken(access.getToken().getId());
+
+ Tenants tenants = keystone.execute(new ListTenants());
+ // try to exchange token using the first tenant
+ if (tenants.getList().size() > 0) {
+ // access with tenant
+ access = keystone.execute(Authenticate.withToken(access.getToken().getId())
+ .withTenantId(tenants.getList().get(0).getId()));
+
+ QuantumClient quantumClient = new QuantumClient(KeystoneUtils
+ .findEndpointURL(access.getServiceCatalog(), "network", null, "public"),
+ access.getToken().getId());
+
+ Network networkQuery = new Network();
+ networkQuery.setName("benn.cs");
+ networkQuery.setAdminStateUp(true);
+ Networks networks = quantumClient.execute(NetworkQuery.queryNetworks(networkQuery));
+
+ for (Network network : networks) {
+ System.out.println(network);
+ }
+
+ Subnet subnetQuery = new Subnet();
+ subnetQuery.setIpversion(Subnet.IpVersion.IPV4);
+ Subnets Subnets = quantumClient.execute(NetworkQuery.querySubnets(subnetQuery));
+ for (Subnet subnet : Subnets) {
+ System.out.println(subnet);
+ }
+
+ } else {
+ System.out.println("No tenants found!");
+ }
+ }
+}
@@ -0,0 +1,24 @@
+package org.openstack.quantum.api.networks;
+
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.MediaType;
+
+import org.openstack.common.command.AbsOpenStackCmd;
+import org.openstack.quantum.client.QuantumCommand;
+import org.openstack.quantum.model.Network;
+import org.openstack.quantum.model.Networks;
+
+public class QueryNetworks extends AbsOpenStackCmd<Network> implements QuantumCommand<Networks> {
+
+ public QueryNetworks(Network network) {
+ super(network);
+ }
+
+ public Networks execute(WebTarget target)
+ {
+ target = target.path("v2.0").path("networks");
+ target = queryParam(target);
+ return target.request(MediaType.APPLICATION_JSON).get(Networks.class);
+ }
+
+}
@@ -0,0 +1,24 @@
+package org.openstack.quantum.api.ports;
+
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.MediaType;
+
+import org.openstack.common.command.AbsOpenStackCmd;
+import org.openstack.quantum.client.QuantumCommand;
+import org.openstack.quantum.model.Port;
+import org.openstack.quantum.model.Ports;
+
+public class QueryPorts extends AbsOpenStackCmd<Port> implements QuantumCommand<Ports> {
+
+ public QueryPorts(Port port) {
+ super(port);
+ }
+
+ public Ports execute(WebTarget target)
+ {
+ target = target.path("v2.0").path("ports");
+ target = queryParam(target);
+ return target.request(MediaType.APPLICATION_JSON).get(Ports.class);
+ }
+
+}
@@ -0,0 +1,23 @@
+package org.openstack.quantum.api.query;
+
+import org.openstack.quantum.api.networks.QueryNetworks;
+import org.openstack.quantum.api.ports.QueryPorts;
+import org.openstack.quantum.api.subnets.QuerySubnets;
+import org.openstack.quantum.model.Network;
+import org.openstack.quantum.model.Port;
+import org.openstack.quantum.model.Subnet;
+
+public class NetworkQuery {
+
+ public static QueryNetworks queryNetworks(Network network) {
+ return new QueryNetworks(network);
+ }
+
+ public static QueryPorts queryPorts(Port port) {
+ return new QueryPorts(port);
+ }
+
+ public static QuerySubnets querySubnets(Subnet subnet) {
+ return new QuerySubnets(subnet);
+ }
+}
@@ -0,0 +1,24 @@
+package org.openstack.quantum.api.subnets;
+
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.MediaType;
+
+import org.openstack.common.command.AbsOpenStackCmd;
+import org.openstack.quantum.client.QuantumCommand;
+import org.openstack.quantum.model.Subnet;
+import org.openstack.quantum.model.Subnets;
+
+public class QuerySubnets extends AbsOpenStackCmd<Subnet> implements QuantumCommand<Subnets> {
+
+ public QuerySubnets(Subnet subnet) {
+ super(subnet);
+ }
+
+ public Subnets execute(WebTarget target)
+ {
+ target = target.path("v2.0").path("subnets");
+ target = queryParam(target);
+ return target.request(MediaType.APPLICATION_JSON).get(Subnets.class);
+ }
+
+}
Oops, something went wrong.

0 comments on commit caef316

Please sign in to comment.