diff options
author | ChrisC <cc697w@intl.att.com> | 2017-01-31 13:57:24 +0100 |
---|---|---|
committer | ChrisC <cc697w@intl.att.com> | 2017-01-31 14:55:11 +0100 |
commit | 2e984294ac28c6f2ede290c38164c5d536ccaf4a (patch) | |
tree | 5eba5a929b7a961c98749fa69e03cfea58e1a724 /openstack-examples/src/main/java/com | |
parent | 86c0f28c8ed469486b64d6422dc53e3a7bcc8adb (diff) |
Initial OpenECOMP MSO OpenStack SDK lib commit
Change-Id: Ieaacb2b2c0dcc469669880e73f0cda9fa59a6c5a
Signed-off-by: ChrisC <cc697w@intl.att.com>
Diffstat (limited to 'openstack-examples/src/main/java/com')
18 files changed, 982 insertions, 0 deletions
diff --git a/openstack-examples/src/main/java/com/woorea/openstack/examples/ExamplesConfiguration.java b/openstack-examples/src/main/java/com/woorea/openstack/examples/ExamplesConfiguration.java new file mode 100644 index 0000000..056eae5 --- /dev/null +++ b/openstack-examples/src/main/java/com/woorea/openstack/examples/ExamplesConfiguration.java @@ -0,0 +1,34 @@ +package com.woorea.openstack.examples; + + +import com.woorea.openstack.base.client.OpenStackSimpleTokenProvider; +import com.woorea.openstack.keystone.Keystone; +import com.woorea.openstack.keystone.model.Tenant; + +public class ExamplesConfiguration { + + public static final String KEYSTONE_AUTH_URL = "https://region-a.geo-1.identity.hpcloudsvc.com:35357/v3"; + + public static final String KEYSTONE_USERNAME = ""; + + public static final String KEYSTONE_PASSWORD = ""; + + public static final String KEYSTONE_ENDPOINT = "https://region-a.geo-1.identity.hpcloudsvc.com:35357/v3"; + + public static final String TENANT_NAME = "admin"; + + public static final String NOVA_ENDPOINT = "http://compute/v2"; + + public static final String CEILOMETER_ENDPOINT = ""; + + public static void main(String[] args) { + Keystone client = new Keystone(KEYSTONE_ENDPOINT); + client.setTokenProvider(new OpenStackSimpleTokenProvider("secret0")); + client.tenants().delete("36c481aec1d54fc49190c92c3ef6840a").execute(); + Tenant tenant = client.tenants().create(new Tenant("new_api")).execute(); + System.out.println(tenant); + System.out.println(client.tenants().list().execute()); + client.tenants().delete(tenant.getId()).execute(); + } + +} diff --git a/openstack-examples/src/main/java/com/woorea/openstack/examples/compute/NovaCreateServer.java b/openstack-examples/src/main/java/com/woorea/openstack/examples/compute/NovaCreateServer.java new file mode 100644 index 0000000..643f14b --- /dev/null +++ b/openstack-examples/src/main/java/com/woorea/openstack/examples/compute/NovaCreateServer.java @@ -0,0 +1,90 @@ +package com.woorea.openstack.examples.compute; + + +import com.woorea.openstack.base.client.OpenStackSimpleTokenProvider; +import com.woorea.openstack.examples.ExamplesConfiguration; +import com.woorea.openstack.keystone.Keystone; +import com.woorea.openstack.keystone.model.Access; +import com.woorea.openstack.keystone.model.Tenants; +import com.woorea.openstack.nova.Nova; +import com.woorea.openstack.nova.model.Flavors; +import com.woorea.openstack.nova.model.Images; +import com.woorea.openstack.nova.model.KeyPairs; +import com.woorea.openstack.nova.model.Server; +import com.woorea.openstack.nova.model.ServerForCreate; + +public class NovaCreateServer { + + /** + * @param args + */ + public static void main(String[] args) { + Keystone keystone = new Keystone(ExamplesConfiguration.KEYSTONE_AUTH_URL); + // access with unscoped token + Access access = keystone + .tokens() + .authenticate() + .withUsernamePassword(ExamplesConfiguration.KEYSTONE_USERNAME, ExamplesConfiguration.KEYSTONE_PASSWORD) + .execute(); + + // use the token in the following requests + keystone.token(access.getToken().getId()); + + Tenants tenants = keystone.tenants().list().execute(); + + // try to exchange token using the first tenant + if (tenants.getList().size() > 0) { + + access = keystone.tokens().authenticate() + .withToken(access.getToken().getId()) + .withTenantId(tenants.getList().get(0).getId()).execute(); + + // NovaClient novaClient = new + // NovaClient(KeystoneUtils.findEndpointURL(access.getServiceCatalog(), + // "compute", null, "public"), access.getToken().getId()); + Nova nova = new Nova(ExamplesConfiguration.NOVA_ENDPOINT.concat(tenants + .getList().get(0).getId())); + nova.setTokenProvider(new OpenStackSimpleTokenProvider(access.getToken() + .getId())); + // novaClient.enableLogging(Logger.getLogger("nova"), 100 * 1024); + // create a new keypair + // KeyPair keyPair = + // novaClient.execute(KeyPairsExtension.createKeyPair("mykeypair")); + // System.out.println(keyPair.getPrivateKey()); + + // create security group + // SecurityGroup securityGroup = + // novaClient.execute(SecurityGroupsExtension.createSecurityGroup("mysecuritygroup", + // "description")); + + // novaClient.execute(SecurityGroupsExtension.createSecurityGroupRule(securityGroup.getId(), + // "UDP", 9090, 9092, "0.0.0.0/0")); + // novaClient.execute(SecurityGroupsExtension.createSecurityGroupRule(securityGroup.getId(), + // "TCP", 8080, 8080, "0.0.0.0/0")); + + KeyPairs keysPairs = nova.keyPairs().list().execute(); + + Images images = nova.images().list(true).execute(); + + Flavors flavors = nova.flavors().list(true).execute(); + + ServerForCreate serverForCreate = new ServerForCreate(); + serverForCreate.setName("woorea"); + serverForCreate.setFlavorRef(flavors.getList().get(0).getId()); + serverForCreate.setImageRef(images.getList().get(1).getId()); + serverForCreate.setKeyName(keysPairs.getList().get(0).getName()); + serverForCreate.getSecurityGroups() + .add(new ServerForCreate.SecurityGroup("default")); + // serverForCreate.getSecurityGroups().add(new + // ServerForCreate.SecurityGroup(securityGroup.getName())); + + Server server = nova.servers().boot(serverForCreate).execute(); + System.out.println(server); + + } else { + System.out.println("No tenants found!"); + } + + } + +} diff --git a/openstack-examples/src/main/java/com/woorea/openstack/examples/compute/NovaListFlavors.java b/openstack-examples/src/main/java/com/woorea/openstack/examples/compute/NovaListFlavors.java new file mode 100644 index 0000000..893192d --- /dev/null +++ b/openstack-examples/src/main/java/com/woorea/openstack/examples/compute/NovaListFlavors.java @@ -0,0 +1,51 @@ +package com.woorea.openstack.examples.compute; + + +import com.woorea.openstack.examples.ExamplesConfiguration; +import com.woorea.openstack.keystone.Keystone; +import com.woorea.openstack.keystone.model.Access; +import com.woorea.openstack.keystone.model.Tenants; +import com.woorea.openstack.keystone.model.authentication.TokenAuthentication; +import com.woorea.openstack.keystone.model.authentication.UsernamePassword; +import com.woorea.openstack.nova.Nova; +import com.woorea.openstack.nova.model.Flavor; +import com.woorea.openstack.nova.model.Flavors; + +public class NovaListFlavors { + + /** + * @param args + */ + public static void main(String[] args) { + Keystone keystone = new Keystone(ExamplesConfiguration.KEYSTONE_AUTH_URL); + Access access = keystone.tokens().authenticate( + new UsernamePassword(ExamplesConfiguration.KEYSTONE_USERNAME, ExamplesConfiguration.KEYSTONE_PASSWORD)) + .execute(); + + //use the token in the following requests + keystone.token(access.getToken().getId()); + + Tenants tenants = keystone.tenants().list().execute(); + + //try to exchange token using the first tenant + if(tenants.getList().size() > 0) { + + access = keystone.tokens().authenticate(new TokenAuthentication(access.getToken().getId())).withTenantId(tenants.getList().get(0).getId()).execute(); + + //NovaClient novaClient = new NovaClient(KeystoneUtils.findEndpointURL(access.getServiceCatalog(), "compute", null, "public"), access.getToken().getId()); + Nova novaClient = new Nova(ExamplesConfiguration.NOVA_ENDPOINT.concat("/").concat(tenants.getList().get(0).getId())); + novaClient.token(access.getToken().getId()); + //novaClient.enableLogging(Logger.getLogger("nova"), 100 * 1024); + + Flavors flavors = novaClient.flavors().list(true).execute(); + for(Flavor flavor : flavors) { + System.out.println(flavor); + } + + } else { + System.out.println("No tenants found!"); + } + + } + +} diff --git a/openstack-examples/src/main/java/com/woorea/openstack/examples/compute/NovaListImages.java b/openstack-examples/src/main/java/com/woorea/openstack/examples/compute/NovaListImages.java new file mode 100644 index 0000000..edf2f63 --- /dev/null +++ b/openstack-examples/src/main/java/com/woorea/openstack/examples/compute/NovaListImages.java @@ -0,0 +1,52 @@ +package com.woorea.openstack.examples.compute; + + +import com.woorea.openstack.examples.ExamplesConfiguration; +import com.woorea.openstack.keystone.Keystone; +import com.woorea.openstack.keystone.model.Access; +import com.woorea.openstack.keystone.model.Tenants; +import com.woorea.openstack.keystone.model.authentication.TokenAuthentication; +import com.woorea.openstack.keystone.model.authentication.UsernamePassword; +import com.woorea.openstack.nova.Nova; +import com.woorea.openstack.nova.model.Image; +import com.woorea.openstack.nova.model.Images; + +public class NovaListImages { + + /** + * @param args + */ + public static void main(String[] args) { + + Keystone keystone = new Keystone(ExamplesConfiguration.KEYSTONE_AUTH_URL); + Access access = keystone.tokens().authenticate(new UsernamePassword(ExamplesConfiguration.KEYSTONE_USERNAME, ExamplesConfiguration.KEYSTONE_PASSWORD)).execute(); + + //use the token in the following requests + keystone.token(access.getToken().getId()); + + Tenants tenants = keystone.tenants().list().execute(); + + //try to exchange token using the first tenant + if(tenants.getList().size() > 0) { + + access = keystone.tokens().authenticate(new TokenAuthentication(access.getToken().getId())) + .withTenantId(tenants.getList().get(0).getId()) + .execute(); + + //NovaClient novaClient = new NovaClient(KeystoneUtils.findEndpointURL(access.getServiceCatalog(), "compute", null, "public"), access.getToken().getId()); + Nova novaClient = new Nova(ExamplesConfiguration.NOVA_ENDPOINT.concat("/").concat(tenants.getList().get(0).getId())); + novaClient.token(access.getToken().getId()); + //novaClient.enableLogging(Logger.getLogger("nova"), 100 * 1024); + + Images images = novaClient.images().list(true).execute(); + for(Image image : images) { + System.out.println(image); + } + + } else { + System.out.println("No tenants found!"); + } + + } + +} diff --git a/openstack-examples/src/main/java/com/woorea/openstack/examples/compute/NovaListServers.java b/openstack-examples/src/main/java/com/woorea/openstack/examples/compute/NovaListServers.java new file mode 100644 index 0000000..cab24e1 --- /dev/null +++ b/openstack-examples/src/main/java/com/woorea/openstack/examples/compute/NovaListServers.java @@ -0,0 +1,38 @@ +package com.woorea.openstack.examples.compute; + + +import com.woorea.openstack.examples.ExamplesConfiguration; +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; +import com.woorea.openstack.nova.model.Server; +import com.woorea.openstack.nova.model.Servers; + +public class NovaListServers { + + /** + * @param args + */ + public static void main(String[] args) { + Keystone keystone = new Keystone(ExamplesConfiguration.KEYSTONE_AUTH_URL); + Access access = keystone.tokens().authenticate(new UsernamePassword(ExamplesConfiguration.KEYSTONE_USERNAME, ExamplesConfiguration.KEYSTONE_PASSWORD)) + .withTenantName("demo") + .execute(); + + //use the token in the following requests + keystone.token(access.getToken().getId()); + + //NovaClient novaClient = new NovaClient(KeystoneUtils.findEndpointURL(access.getServiceCatalog(), "compute", null, "public"), access.getToken().getId()); + Nova novaClient = new Nova(ExamplesConfiguration.NOVA_ENDPOINT.concat("/").concat(access.getToken().getTenant().getId())); + novaClient.token(access.getToken().getId()); + //novaClient.enableLogging(Logger.getLogger("nova"), 100 * 1024); + + Servers servers = novaClient.servers().list(true).execute(); + for(Server server : servers) { + System.out.println(server); + } + + } + +} diff --git a/openstack-examples/src/main/java/com/woorea/openstack/examples/compute/NovaStopStartServer.java b/openstack-examples/src/main/java/com/woorea/openstack/examples/compute/NovaStopStartServer.java new file mode 100644 index 0000000..df4d44b --- /dev/null +++ b/openstack-examples/src/main/java/com/woorea/openstack/examples/compute/NovaStopStartServer.java @@ -0,0 +1,40 @@ +package com.woorea.openstack.examples.compute; + +import com.woorea.openstack.examples.ExamplesConfiguration; +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; +import com.woorea.openstack.nova.api.ServersResource; +import com.woorea.openstack.nova.model.Servers; + +public class NovaStopStartServer { + public static void main(String[] args) throws InterruptedException { + Keystone keystone = new Keystone(ExamplesConfiguration.KEYSTONE_AUTH_URL); + Access access = keystone.tokens().authenticate(new UsernamePassword(ExamplesConfiguration.KEYSTONE_USERNAME, ExamplesConfiguration.KEYSTONE_PASSWORD)) + .withTenantName(ExamplesConfiguration.TENANT_NAME) + .execute(); + + //use the token in the following requests + keystone.token(access.getToken().getId()); + + Nova novaClient = new Nova(ExamplesConfiguration.NOVA_ENDPOINT.concat("/").concat(access.getToken().getTenant().getId())); + novaClient.token(access.getToken().getId()); + + Servers servers = novaClient.servers().list(true).execute(); + if(servers.getList().size() > 0) { + + // Server has to be in activated state. + ServersResource.StopServer stopServer = novaClient.servers().stop(servers.getList().get(0).getId()); + stopServer.endpoint(ExamplesConfiguration.NOVA_ENDPOINT); + stopServer.execute(); + + // Wait until server shutdown. Or 400 error occurs. + Thread.sleep(5000); + + ServersResource.StartServer startServer = novaClient.servers().start(servers.getList().get(0).getId()); + startServer.endpoint(ExamplesConfiguration.NOVA_ENDPOINT); + startServer.execute(); + } + } +} diff --git a/openstack-examples/src/main/java/com/woorea/openstack/examples/glance/GlanceListImages.java b/openstack-examples/src/main/java/com/woorea/openstack/examples/glance/GlanceListImages.java new file mode 100644 index 0000000..ea687aa --- /dev/null +++ b/openstack-examples/src/main/java/com/woorea/openstack/examples/glance/GlanceListImages.java @@ -0,0 +1,83 @@ +package com.woorea.openstack.examples.glance; + +import com.woorea.openstack.glance.model.ImageDownload; +import com.woorea.openstack.glance.model.ImageUpload; +import com.woorea.openstack.keystone.utils.KeystoneTokenProvider; + +import com.woorea.openstack.examples.ExamplesConfiguration; +import com.woorea.openstack.glance.Glance; +import com.woorea.openstack.glance.model.Image; +import com.woorea.openstack.glance.model.Images; +import com.woorea.openstack.keystone.model.Access; +import com.woorea.openstack.keystone.model.Access.Service; +import com.woorea.openstack.keystone.model.Access.Service.Endpoint; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + +public class GlanceListImages { + + protected static String IMAGE_CONTENT = "Hello World!"; + + /** + * @param args + */ + public static void main(String[] args) { + KeystoneTokenProvider keystone = new KeystoneTokenProvider( + ExamplesConfiguration.KEYSTONE_AUTH_URL, + ExamplesConfiguration.KEYSTONE_USERNAME, + ExamplesConfiguration.KEYSTONE_PASSWORD); + + Access access = keystone.getAccessByTenant(ExamplesConfiguration.TENANT_NAME); + + Service glanceService = null; + + for (Service service : access.getServiceCatalog()) { + if (service.getType().equals("image")) { + glanceService = service; + break; + } + } + + if (glanceService == null) { + throw new RuntimeException("Glance service not found"); + } + + for (Endpoint endpoint : glanceService.getEndpoints()) { + Glance glance = new Glance(endpoint.getPublicURL() + "/v1"); + glance.setTokenProvider(keystone + .getProviderByTenant(ExamplesConfiguration.TENANT_NAME)); + + // Creating a new image + Image newImage = new Image(); + newImage.setDiskFormat("raw"); + newImage.setContainerFormat("bare"); + newImage.setName("os-java-glance-test"); + newImage = glance.images().create(newImage).execute(); + + // Uploading image + ImageUpload uploadImage = new ImageUpload(newImage); + uploadImage.setInputStream(new ByteArrayInputStream(IMAGE_CONTENT.getBytes())); + glance.images().upload(newImage.getId(), uploadImage).execute(); + + // Downloading the image and displaying the image content + try { + byte[] imgContent = new byte[IMAGE_CONTENT.length()]; + ImageDownload downloadImage = glance.images().download(newImage.getId()).execute(); + downloadImage.getInputStream().read(imgContent, 0, imgContent.length); + System.out.println(new String(imgContent)); + } catch (IOException e) { + e.printStackTrace(); + } + + Images images = glance.images().list(false).execute(); + + for (Image image : images) { + System.out.println(glance.images().show(image.getId()).execute()); + } + + glance.images().delete(newImage.getId()).execute(); + } + } + +} diff --git a/openstack-examples/src/main/java/com/woorea/openstack/examples/heat/HeatListStacks.java b/openstack-examples/src/main/java/com/woorea/openstack/examples/heat/HeatListStacks.java new file mode 100644 index 0000000..ee9fe44 --- /dev/null +++ b/openstack-examples/src/main/java/com/woorea/openstack/examples/heat/HeatListStacks.java @@ -0,0 +1,76 @@ +package com.woorea.openstack.examples.heat; + +/* + * Modifications copyright (c) 2017 AT&T Intellectual Property + */ + +import com.woorea.openstack.examples.ExamplesConfiguration; +import com.woorea.openstack.heat.Heat; +import com.woorea.openstack.heat.model.CreateStackParam; +import com.woorea.openstack.heat.model.Stack; +import com.woorea.openstack.keystone.model.Access; +import com.woorea.openstack.keystone.utils.KeystoneTokenProvider; +import com.woorea.openstack.keystone.utils.KeystoneUtils; + +import java.util.Collections; + +public class HeatListStacks { + + private static String TEMPLATE = "{\n" + + " \"HeatTemplateFormatVersion\": \"2012-12-12\",\n" + + " \"Parameters\": {},\n" + + " \"Mappings\": {},\n" + + " \"Resources\": {\n" + + " \"my-test-server\": {\n" + + " \"Type\": \"OS::Nova::Server\",\n" + + " \"Properties\": {\n" + + " \"flavor\": \"m1.small\",\n" + + " \"image\": \"centos:latest\"\n" + + " }\n" + + " }\n" + + " }\n" + + "}"; + + /** + * @param args + */ + public static void main(String[] args) throws InterruptedException { + KeystoneTokenProvider keystone = new KeystoneTokenProvider( + ExamplesConfiguration.KEYSTONE_ENDPOINT, + ExamplesConfiguration.KEYSTONE_USERNAME, + ExamplesConfiguration.KEYSTONE_PASSWORD + ); + + Access access = keystone.getAccessByTenant(ExamplesConfiguration.TENANT_NAME); + + String endpointURL = KeystoneUtils.findEndpointURL(access.getServiceCatalog(), "orchestration", null, "public"); + + + Heat heat = new Heat(endpointURL); + heat.setTokenProvider(keystone + .getProviderByTenant(ExamplesConfiguration.TENANT_NAME)); + + CreateStackParam param = new CreateStackParam(); + param.setStackName("helloWorld"); + param.setTimeoutMinutes(1); + param.setParameters(Collections.<String, Object>emptyMap()); + param.setTemplate(TEMPLATE); + + System.out.printf("Create: " + heat.getStacks().create(param).execute()); + Thread.sleep(3000); + + for (Stack s : heat.getStacks().list().execute()) { + System.out.println(s.getDescription()); + System.out.println(s.getId()); + System.out.println(s.getStackName()); + System.out.println(s.getStackStatus()); + System.out.println(s.getCreationTime()); + System.out.println(s.getUpdatedTime()); + System.out.println(s.getLinks()); + + System.out.println(heat.getStacks().byName(s.getStackName()).execute()); + + + } + } +} diff --git a/openstack-examples/src/main/java/com/woorea/openstack/examples/hpcloud/Keystone3Authentication.java b/openstack-examples/src/main/java/com/woorea/openstack/examples/hpcloud/Keystone3Authentication.java new file mode 100644 index 0000000..5481a15 --- /dev/null +++ b/openstack-examples/src/main/java/com/woorea/openstack/examples/hpcloud/Keystone3Authentication.java @@ -0,0 +1,34 @@ +package com.woorea.openstack.examples.hpcloud; + + +import com.woorea.openstack.base.client.OpenStackResponse; +import com.woorea.openstack.examples.ExamplesConfiguration; +import com.woorea.openstack.keystone.v3.model.Authentication; +import com.woorea.openstack.keystone.v3.model.Authentication.Identity; +import com.woorea.openstack.keystone.v3.Keystone; +import com.woorea.openstack.keystone.v3.model.Token; + +public class Keystone3Authentication { + + /** + * @param args + */ + public static void main(String[] args) { + Keystone keystone = new Keystone(ExamplesConfiguration.KEYSTONE_AUTH_URL); + + Authentication auth = new Authentication(); + auth.setIdentity(Identity.password(ExamplesConfiguration.KEYSTONE_USERNAME, ExamplesConfiguration.KEYSTONE_PASSWORD)); + + OpenStackResponse response = keystone.tokens().authenticate(auth).request(); + + String tokenId = response.header("X-Subject-Token"); + + Token token = response.getEntity(Token.class); + + System.out.println(tokenId); + + System.out.println(token); + + } + +} diff --git a/openstack-examples/src/main/java/com/woorea/openstack/examples/hpcloud/KeystoneAuthentication.java b/openstack-examples/src/main/java/com/woorea/openstack/examples/hpcloud/KeystoneAuthentication.java new file mode 100644 index 0000000..1caffea --- /dev/null +++ b/openstack-examples/src/main/java/com/woorea/openstack/examples/hpcloud/KeystoneAuthentication.java @@ -0,0 +1,29 @@ +package com.woorea.openstack.examples.hpcloud; + + +import com.woorea.openstack.examples.ExamplesConfiguration; +import com.woorea.openstack.keystone.Keystone; +import com.woorea.openstack.keystone.model.Access; + +public class KeystoneAuthentication { + + private static final String KEYSTONE_AUTH_URL = "https://region-a.geo-1.identity.hpcloudsvc.com:35357/v2.0"; + + /** + * @param args + */ + public static void main(String[] args) { + Keystone keystone = new Keystone(KEYSTONE_AUTH_URL); + + // access with unscoped token + Access access = keystone + .tokens() + .authenticate() + .withUsernamePassword(ExamplesConfiguration.KEYSTONE_USERNAME, ExamplesConfiguration.KEYSTONE_PASSWORD) + .execute(); + + System.out.println(access); + + } + +} diff --git a/openstack-examples/src/main/java/com/woorea/openstack/examples/keystone/KeystoneCreateTenant.java b/openstack-examples/src/main/java/com/woorea/openstack/examples/keystone/KeystoneCreateTenant.java new file mode 100644 index 0000000..9bd3399 --- /dev/null +++ b/openstack-examples/src/main/java/com/woorea/openstack/examples/keystone/KeystoneCreateTenant.java @@ -0,0 +1,36 @@ +package com.woorea.openstack.examples.keystone; + + +import com.woorea.openstack.examples.ExamplesConfiguration; +import com.woorea.openstack.keystone.Keystone; +import com.woorea.openstack.keystone.model.Access; +import com.woorea.openstack.keystone.model.Tenant; +import com.woorea.openstack.keystone.model.authentication.TokenAuthentication; +import com.woorea.openstack.keystone.model.authentication.UsernamePassword; + +public class KeystoneCreateTenant { + + /** + * @param args + */ + public static void main(String[] args) { + Keystone keystone = new Keystone(ExamplesConfiguration.KEYSTONE_AUTH_URL); + //access with unscoped token + Access access = keystone.tokens().authenticate( + new UsernamePassword(ExamplesConfiguration.KEYSTONE_USERNAME, ExamplesConfiguration.KEYSTONE_PASSWORD)) + .execute(); + + access = keystone.tokens().authenticate(new TokenAuthentication(access.getToken().getId())).withTenantName("admin").execute(); + + 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 Keystone("http://keystone.x.org/v2.0"); + keystone.token(access.getToken().getId()); + tenant = keystone.tenants().create(tenant).execute(); + System.out.println(tenant); + keystone.tenants().delete(tenant.getId()); + } +} diff --git a/openstack-examples/src/main/java/com/woorea/openstack/examples/keystone/KeystoneCreateUser.java b/openstack-examples/src/main/java/com/woorea/openstack/examples/keystone/KeystoneCreateUser.java new file mode 100644 index 0000000..bde2440 --- /dev/null +++ b/openstack-examples/src/main/java/com/woorea/openstack/examples/keystone/KeystoneCreateUser.java @@ -0,0 +1,38 @@ +package com.woorea.openstack.examples.keystone; + + +import com.woorea.openstack.base.client.OpenStackSimpleTokenProvider; +import com.woorea.openstack.examples.ExamplesConfiguration; +import com.woorea.openstack.keystone.Keystone; +import com.woorea.openstack.keystone.model.Access; +import com.woorea.openstack.keystone.model.User; +import com.woorea.openstack.keystone.model.authentication.UsernamePassword; + +public class KeystoneCreateUser { + + /** + * @param args + */ + public static void main(String[] args) { + Keystone keystone = new Keystone(ExamplesConfiguration.KEYSTONE_AUTH_URL); + //access with unscoped token + Access access = keystone.tokens() + .authenticate(new UsernamePassword(ExamplesConfiguration.KEYSTONE_USERNAME, ExamplesConfiguration.KEYSTONE_PASSWORD)) + .withTenantName("admin") + .execute(); + + User user = new User(); + user.setEmail("luis@woorea.es"); + user.setUsername("luis.gervaso"); + user.setPassword("password.0"); + user.setName("Luis"); + user.setEnabled(Boolean.TRUE); + + keystone = new Keystone("http://keystone.x.org/v2.0"); + keystone.setTokenProvider(new OpenStackSimpleTokenProvider(access.getToken().getId())); + //keystone.enableLogging(Logger.getLogger("keystone"), 10000); + user = keystone.users().create(user).execute(); + System.out.println(user); + keystone.users().delete(user.getId()).execute(); + } +} diff --git a/openstack-examples/src/main/java/com/woorea/openstack/examples/metering/v2/TestAll.java b/openstack-examples/src/main/java/com/woorea/openstack/examples/metering/v2/TestAll.java new file mode 100644 index 0000000..c9d0c18 --- /dev/null +++ b/openstack-examples/src/main/java/com/woorea/openstack/examples/metering/v2/TestAll.java @@ -0,0 +1,62 @@ +package com.woorea.openstack.examples.metering.v2; + +import java.util.List; + + +import com.woorea.openstack.base.client.OpenStackSimpleTokenProvider; +import com.woorea.openstack.ceilometer.Ceilometer; +import com.woorea.openstack.ceilometer.v2.model.Meter; +import com.woorea.openstack.ceilometer.v2.model.Statistics; +import com.woorea.openstack.examples.ExamplesConfiguration; +import com.woorea.openstack.keystone.Keystone; +import com.woorea.openstack.keystone.model.Access; +import com.woorea.openstack.keystone.model.authentication.UsernamePassword; + +public class TestAll { + + /** + * @param args + */ + public static void main(String[] args) { + Keystone keystone = new Keystone(ExamplesConfiguration.KEYSTONE_AUTH_URL); + Access access = keystone.tokens() + .authenticate(new UsernamePassword(ExamplesConfiguration.KEYSTONE_USERNAME, ExamplesConfiguration.KEYSTONE_PASSWORD)) + .withTenantName("admin") + .execute(); + + Ceilometer ceilometer = new Ceilometer(ExamplesConfiguration.CEILOMETER_ENDPOINT); + ceilometer.setTokenProvider(new OpenStackSimpleTokenProvider(access.getToken().getId())); + + /* + List<Resource> resources = ceilometer.execute(new ResourceList().eq("resource_id", "23b55841eedd41e99d5f3f32149ca086")); + + + for(Resource r : resources) { + Resource resource = ceilometer.execute(new ResourceShow().id(r.getResource())); + } + */ + + /* + //List<Meter> meters = ceilometer.meters().list().execute(); //execute(new MeterList().eq("project_id", "948eeb593acd4223ad572c49e1ef5709")); + + + for(Meter m : meters) { + System.out.println(m); + +// List<Sample> samples = ceilometer.execute(new MeterShow().name(m.getName())); +// for(Sample s : samples) { +// System.out.println("\t" + s); +// } + + List<Statistics> stats = ceilometer.meters().statistics().execute(); // (new MeterStatistics().name(m.getName())); + for(Statistics s : stats) { + System.out.println("\t\t" + s); + } + + + } + */ + + } + +} diff --git a/openstack-examples/src/main/java/com/woorea/openstack/examples/network/QuantumListNetworks.java b/openstack-examples/src/main/java/com/woorea/openstack/examples/network/QuantumListNetworks.java new file mode 100644 index 0000000..8e218c2 --- /dev/null +++ b/openstack-examples/src/main/java/com/woorea/openstack/examples/network/QuantumListNetworks.java @@ -0,0 +1,47 @@ +package com.woorea.openstack.examples.network; + +import com.woorea.openstack.keystone.utils.KeystoneUtils; + +import com.woorea.openstack.base.client.OpenStackSimpleTokenProvider; +import com.woorea.openstack.examples.ExamplesConfiguration; +import com.woorea.openstack.keystone.Keystone; +import com.woorea.openstack.keystone.model.Access; +import com.woorea.openstack.keystone.model.Tenants; +import com.woorea.openstack.keystone.model.authentication.TokenAuthentication; +import com.woorea.openstack.keystone.model.authentication.UsernamePassword; +import com.woorea.openstack.quantum.Quantum; +import com.woorea.openstack.quantum.model.Network; +import com.woorea.openstack.quantum.model.Networks; + +public class QuantumListNetworks { + + /** + * @param args + */ + public static void main(String[] args) { + Keystone keystone = new Keystone(ExamplesConfiguration.KEYSTONE_AUTH_URL); + // access with unscoped token + Access access = keystone.tokens().authenticate( + new UsernamePassword(ExamplesConfiguration.KEYSTONE_USERNAME, ExamplesConfiguration.KEYSTONE_PASSWORD)) + .execute(); + // use the token in the following requests + keystone.setTokenProvider(new OpenStackSimpleTokenProvider(access.getToken().getId())); + + Tenants tenants = keystone.tenants().list().execute(); + // try to exchange token using the first tenant + if (tenants.getList().size() > 0) { + // access with tenant + access = keystone.tokens().authenticate(new TokenAuthentication(access.getToken().getId())).withTenantId(tenants.getList().get(0).getId()).execute(); + + Quantum quantum = new Quantum(KeystoneUtils.findEndpointURL(access.getServiceCatalog(), "network", null, "public")); + quantum.setTokenProvider(new OpenStackSimpleTokenProvider(access.getToken().getId())); + + Networks networks = quantum.networks().list().execute(); + for (Network network : networks) { + System.out.println(network); + } + } else { + System.out.println("No tenants found!"); + } + } +} diff --git a/openstack-examples/src/main/java/com/woorea/openstack/examples/network/QuantumNetworkCreate.java b/openstack-examples/src/main/java/com/woorea/openstack/examples/network/QuantumNetworkCreate.java new file mode 100644 index 0000000..7f4dff0 --- /dev/null +++ b/openstack-examples/src/main/java/com/woorea/openstack/examples/network/QuantumNetworkCreate.java @@ -0,0 +1,108 @@ +package com.woorea.openstack.examples.network; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +import com.woorea.openstack.keystone.utils.KeystoneUtils; + +import com.woorea.openstack.base.client.OpenStackSimpleTokenProvider; +import com.woorea.openstack.examples.ExamplesConfiguration; +import com.woorea.openstack.keystone.Keystone; +import com.woorea.openstack.keystone.model.Access; +import com.woorea.openstack.keystone.model.Tenant; +import com.woorea.openstack.keystone.model.Tenants; +import com.woorea.openstack.keystone.model.authentication.TokenAuthentication; +import com.woorea.openstack.keystone.model.authentication.UsernamePassword; +import com.woorea.openstack.quantum.Quantum; +import com.woorea.openstack.quantum.api.NetworksResource; +import com.woorea.openstack.quantum.api.NetworksResource.Create; +import com.woorea.openstack.quantum.model.Network; +import com.woorea.openstack.quantum.model.NetworkForCreate; +import com.woorea.openstack.quantum.model.Networks; +import com.woorea.openstack.quantum.model.Port; +import com.woorea.openstack.quantum.model.PortForCreate; +import com.woorea.openstack.quantum.model.Router; +import com.woorea.openstack.quantum.model.RouterForAddInterface; +import com.woorea.openstack.quantum.model.RouterForCreate; +import com.woorea.openstack.quantum.model.Subnet; +import com.woorea.openstack.quantum.model.SubnetForCreate; +import com.woorea.openstack.quantum.model.Subnets; + +public class QuantumNetworkCreate { + + /** + * @param args + */ + public static void main(String[] args) { + Keystone keystone = new Keystone( + ExamplesConfiguration.KEYSTONE_AUTH_URL); + // access with unscoped token + Access access = keystone + .tokens() + .authenticate( + new UsernamePassword( + ExamplesConfiguration.KEYSTONE_USERNAME, + ExamplesConfiguration.KEYSTONE_PASSWORD)) + .execute(); + // use the token in the following requests + keystone.setTokenProvider(new OpenStackSimpleTokenProvider(access + .getToken().getId())); + keystone.token(access.getToken().getId()); + Tenants tenants = keystone.tenants().list().execute(); + // try to exchange token using the first tenant + + if (tenants.getList().size() > 0) { + // access with tenant + Network network = new Network(); + access = keystone + .tokens() + .authenticate( + new TokenAuthentication(access.getToken().getId())) + .withTenantId("tenantId").execute(); + Quantum quantum = new Quantum(KeystoneUtils.findEndpointURL( + access.getServiceCatalog(), "network", null, "public")); + quantum.setTokenProvider(new OpenStackSimpleTokenProvider(access + .getToken().getId())); + NetworkForCreate netcreate = new NetworkForCreate(); + netcreate.setTenantId("tenantId"); + netcreate.setName("net2"); + netcreate.setAdminStateUp(true); + + network = quantum.networks().create(netcreate).execute(); + + // Creating Subnet + try { + Subnet sub = new Subnet(); + SubnetForCreate subnet = new SubnetForCreate(); + subnet.setCidr(""); + subnet.setName(""); + subnet.setNetworkId(network.getId()); + subnet.setIpVersion(4); + sub = quantum.subnets().create(subnet).execute(); + RouterForCreate routerForCreate = new RouterForCreate(); + routerForCreate.setName("routerName"); + routerForCreate.setTenantId("tenantId"); + Router router = quantum.routers().create(routerForCreate) + .execute(); + RouterForAddInterface routerForAdd = new RouterForAddInterface(); + routerForAdd.setSubnetId(sub.getId()); + routerForAdd.setRouterId(router.getId()); + quantum.routers().addInterface(routerForAdd).execute(); + + // System.out.println(sub); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + + Networks networks = quantum.networks().list().execute(); + + for (Network network1 : networks) { + System.out.println(network1); + } + } else { + System.out.println("No tenants found!"); + } + + } +} diff --git a/openstack-examples/src/main/java/com/woorea/openstack/examples/network/QuantumQueryNetworks.java b/openstack-examples/src/main/java/com/woorea/openstack/examples/network/QuantumQueryNetworks.java new file mode 100644 index 0000000..1a427ea --- /dev/null +++ b/openstack-examples/src/main/java/com/woorea/openstack/examples/network/QuantumQueryNetworks.java @@ -0,0 +1,59 @@ +package com.woorea.openstack.examples.network; + +import com.woorea.openstack.keystone.utils.KeystoneUtils; + +import com.woorea.openstack.base.client.OpenStackSimpleTokenProvider; +import com.woorea.openstack.examples.ExamplesConfiguration; +import com.woorea.openstack.keystone.Keystone; +import com.woorea.openstack.keystone.model.Access; +import com.woorea.openstack.keystone.model.Tenants; +import com.woorea.openstack.keystone.model.authentication.TokenAuthentication; +import com.woorea.openstack.keystone.model.authentication.UsernamePassword; +import com.woorea.openstack.quantum.Quantum; +import com.woorea.openstack.quantum.model.Network; + +public class QuantumQueryNetworks { + + /** + * @param args + */ + public static void main(String[] args) { + Keystone keystone = new Keystone(ExamplesConfiguration.KEYSTONE_AUTH_URL); + // access with unscoped token + Access access = keystone.tokens().authenticate( + new UsernamePassword(ExamplesConfiguration.KEYSTONE_USERNAME, ExamplesConfiguration.KEYSTONE_PASSWORD)) + .execute(); + // use the token in the following requests + keystone.setTokenProvider(new OpenStackSimpleTokenProvider(access.getToken().getId())); + + Tenants tenants = keystone.tenants().list().execute(); + // try to exchange token using the first tenant + if (tenants.getList().size() > 0) { + // access with tenant + access = keystone.tokens().authenticate(new TokenAuthentication(access.getToken().getId())).withTenantId(tenants.getList().get(0).getId()).execute(); + + Quantum quantumClient = new Quantum(KeystoneUtils.findEndpointURL(access.getServiceCatalog(), "network", null, "public")); + quantumClient.setTokenProvider(new OpenStackSimpleTokenProvider(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!"); + } + } +} diff --git a/openstack-examples/src/main/java/com/woorea/openstack/examples/objectstore/SwiftExample.java b/openstack-examples/src/main/java/com/woorea/openstack/examples/objectstore/SwiftExample.java new file mode 100644 index 0000000..089163c --- /dev/null +++ b/openstack-examples/src/main/java/com/woorea/openstack/examples/objectstore/SwiftExample.java @@ -0,0 +1,90 @@ +package com.woorea.openstack.examples.objectstore; + +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import com.woorea.openstack.keystone.utils.KeystoneUtils; + +import com.woorea.openstack.base.client.OpenStackSimpleTokenProvider; +import com.woorea.openstack.examples.ExamplesConfiguration; +import com.woorea.openstack.keystone.Keystone; +import com.woorea.openstack.keystone.model.Access; +import com.woorea.openstack.keystone.model.Tenants; +import com.woorea.openstack.keystone.model.authentication.TokenAuthentication; +import com.woorea.openstack.keystone.model.authentication.UsernamePassword; +import com.woorea.openstack.swift.Swift; +import com.woorea.openstack.swift.model.ObjectDownload; +import com.woorea.openstack.swift.model.ObjectForUpload; + +public class SwiftExample { + + private static final File TEST_FILE = new File("pom.xml"); + + /** + * @param args + */ + public static void main(String[] args) throws Exception { + Keystone keystone = new Keystone(ExamplesConfiguration.KEYSTONE_AUTH_URL); + //access with unscoped token + Access access = keystone.tokens().authenticate( + new UsernamePassword(ExamplesConfiguration.KEYSTONE_USERNAME, ExamplesConfiguration.KEYSTONE_PASSWORD)) + .execute(); + + //use the token in the following requests + keystone.setTokenProvider(new OpenStackSimpleTokenProvider(access.getToken().getId())); + + Tenants tenants = keystone.tenants().list().execute(); + + //try to exchange token using the first tenant + if(tenants.getList().size() > 0) { + + access = keystone.tokens().authenticate(new TokenAuthentication(access.getToken().getId())).withTenantId(tenants.getList().get(0).getId()).execute(); + + Swift swift = new Swift(KeystoneUtils.findEndpointURL(access.getServiceCatalog(), "object-store", null, "public")); + swift.setTokenProvider(new OpenStackSimpleTokenProvider(access.getToken().getId())); + + //swiftClient.execute(new DeleteContainer("navidad2")); + + swift.containers().create("navidad2").execute(); + + System.out.println(swift.containers().list()); + + ObjectForUpload upload = new ObjectForUpload(); + upload.setContainer("navidad2"); + upload.setName("example2"); + upload.setInputStream(new FileInputStream(TEST_FILE)); + swift.containers().container("navidad2").upload(upload).execute(); + +// System.out.println(swiftClient.execute(new ListObjects("navidad2", new HashMap<String, String>() {{ +// put("path", ""); +// }})).get(0).getContentType()); + + + ObjectDownload download = swift.containers().container("navidad").download("example2").execute(); + write(download.getInputStream(), "example2"); + } + + } + + private static void write(InputStream is, String path) { + try { + OutputStream stream = new BufferedOutputStream(new FileOutputStream(path)); + int bufferSize = 1024; + byte[] buffer = new byte[bufferSize]; + int len = 0; + while ((len = is.read(buffer)) != -1) { + stream.write(buffer, 0, len); + } + stream.close(); + } catch(IOException e) { + throw new RuntimeException(e.getMessage(), e); + } + + } + +} diff --git a/openstack-examples/src/main/java/com/woorea/openstack/examples/simple/OpenStackSimpleClient.java b/openstack-examples/src/main/java/com/woorea/openstack/examples/simple/OpenStackSimpleClient.java new file mode 100644 index 0000000..c2e8023 --- /dev/null +++ b/openstack-examples/src/main/java/com/woorea/openstack/examples/simple/OpenStackSimpleClient.java @@ -0,0 +1,15 @@ +package com.woorea.openstack.examples.simple; + + +public class OpenStackSimpleClient { + + /** + * @param args + */ + public static void main(String[] args) { +// OpenStackClient client = new OpenStackClient(ExamplesConfiguration.KEYSTONE_AUTH_URL); +// Access access = client.request("/tokens").execute("POST", Entity.json("{\"auth\":{\"passwordCredentials\":{\"username\":\"\",\"password\":\"\"}}}"), Access.class); +// System.out.println(access); + } + +} |