Openstack4j a Java Sample demo

Source: Internet
Author: User

This is A sample Demo

Package edu.hnu.lost.openstack.test;


Import java.util.List;

Import javax.ws.rs.client.Entity;

Import org.openstack.common.client.AbstractOpenStackClient;
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.Authentication;
Import Org.openstack.keystone.model.Authentication.Token;
Import org.openstack.keystone.model.Tenants;
Import Org.openstack.keystone.utils.KeystoneUtils;
Import org.openstack.nova.NovaClient;
Import Org.openstack.nova.api.FlavorsCore;
Import Org.openstack.nova.api.ImagesCore;
Import Org.openstack.nova.api.ServersCore;
Import org.openstack.nova.api.extensions.FloatingIpDnsExtension;
Import org.openstack.nova.api.extensions.FloatingIpsExtension;
Import org.openstack.nova.api.extensions.KeyPairsExtension;
Import org.openstack.nova.api.extensions.NetworksExtension;
Import org.openstack.nova.api.extensions.SecurityGroupsExtension;
Import Org.openstack.nova.model.Flavor;
Import org.openstack.nova.model.Flavors;
Import org.openstack.nova.model.FloatingIp;
Import Org.openstack.nova.model.FloatingIps;
Import Org.openstack.nova.model.Image;
Import Org.openstack.nova.model.Images;
Import Org.openstack.nova.model.KeyPair;
Import Org.openstack.nova.model.KeyPairs;
Import Org.openstack.nova.model.Network;
Import Org.openstack.nova.model.Networks;
Import Org.openstack.nova.model.SecurityGroup;
Import org.openstack.nova.model.SecurityGroups;
Import Org.openstack.nova.model.Server;
Import Org.openstack.nova.model.ServerForCreate;
Import Org.openstack.nova.model.Servers;



public class Testopenstack {

private static final String Keystone_auth_url = "http://192.168.1.2:35357/v2.0";

private static final String keystone_username = "admin";

private static final String Keystone_password = "admin";

private static final String Keystone_token = "4c57a86f453d4045a90e1b14ddfb8b81";

private static authentication authentication;
private static Keystoneclient Keystone;
Private staticaccess access;
Private Staticnovaclient novaclient;
private static tenants tenants;
private static String tokentemp;



public static void Init () {
Keystone = new Keystoneclient (Keystone_auth_url);
Abstractopenstackclient client = new Abstractopenstackclient (
Keystone_auth_url);
Access = Client
. Request ("/tokens")
. Execute (
"POST",
Entity.json ("{\" auth\ ": {\" passwordcredentials\ ": {\" username\ ": \" "
+ Keystone_username
+ "\", \ "password\": \ ""
+ Keystone_password
+ "\"},\ "tenantid\": \ ""
+ Keystone_token + "\"} "), Access.class);
Tokentemp = Access.gettoken (). GetId ();
if (tokentemp! = null) {
Keystone.settoken (tokentemp); Get the ID in Access [token=token
}
Tenants = Keystone.execute (new listtenants ());//This is to get tenant table data in a Keystoen database table
if (Tenants.getlist (). Size () > 0) {
authentication = new authentication ();
Token token = new token ();
Token.setid (tokentemp);
Authentication.settoken (token); Is the id:95d304ab72a14933be27ec2601355146 that was obtained above
Authentication.settenantid (Tenants.getlist (). Get (0). GetId ());
Authentication.settenantid (Keystone_token);
Access = Keystone.execute (new authenticate (authentication));
Novaclient = new Novaclient (Keystoneutils.findendpointurl (
Access.getservicecatalog (), "compute", NULL, "public"),
TOKENTEMP);
}else {
System.out.println ("No tenants found!");
}
}



public static void Main (string[] args) {
Init ();
Listservice ();//
ListImage ();
Listsecuritygroup ();
Listflavor ();
Getflavormodel ();
Listserverdetail ();
Showservice ();
Listkeypairs ();
Getkeyparimodel ();
ListImage ();
Getimagemodel ();
LISTFLOATINGIP ()///Because the system has not yet created FLOATINGIP so there is an error
Getfloatipmodel ();
Createserver ();//You can create an instance, but here the binding floating IP does not work
}


public static void PrintObject (Object obj) {
System.out.println (Obj.getclass () + "" +obj);
}

------------View Platform-------
View running servers in the platform
public static void Listservice () {
Servers Servers = Novaclient.execute (Serverscore.listservers ());
for (Server server:servers) {
SYSTEM.OUT.PRINTLN (server);
}
}
public static void Listserverdetail () {
Servers Servers = Novaclient.execute (Serverscore.listservers (true));
for (Server server:servers) {
PrintObject (server);
}
}
public static void Showservice () {
Server server = Novaclient.execute (Serverscore.showserver ("1f70831c-503d-4375-8b31-f9042929969b"));
PrintObject (server);
}

public static void ListImage () {
Viewing the image in the platform via Novaclient
Images Images = Novaclient.execute (Imagescore.listimages ());
for (Image image:images) {
SYSTEM.OUT.PRINTLN (image);
}
}
public static void Listkeypairs () {
View KeyPair
Keypairs keypairs = Novaclient.execute (Keypairsextension.listkeypairs ());
for (KeyPair keypair:keypairs) {
System.out.println (KeyPair);
}
}
public static void Listsecuritygroup () {
View Securitygroup
Securitygroups securitygroups = Novaclient.execute (Securitygroupsextension.listsecuritygroups ());
for (Securitygroup sg:securitygroups) {
SYSTEM.OUT.PRINTLN (SG);
}
}

public static void Listflavor () {
Flavors flavors = Novaclient.execute (Flavorscore.listflavors ());
for (Flavor flavor:flavors) {
System.out.println (Flavor.getid ());
}
}
public static void Listfloatingip () {

Floatingips floatingips = Novaclient.execute (Floatingipsextension.listfloatingips ());
for (Floatingip floatingip:floatingips) {
PrintObject (FLOATINGIP);
}
}
public static void Listfixip () {
}
------Get information------
Get Securitgroup Model
public static securitygroup getsecuritygroup (int index) {
Securitygroup sg = NULL;
Securitygroups sgs = Novaclient.execute (Securitygroupsextension.listsecuritygroups ());
List<securitygroup> alist = Sgs.getlist ();
if (Alist.size () >index-1) {
sg = (securitygroup) alist.get (index);
}
return SG;
}
Get Flavor Model
public static Flavor Getflavormodel () {
Flavor Flavor = null;
Flavors flavors = Novaclient.execute (Flavorscore.listflavors ());
list<flavor> flavorlist = Flavors.getlist ();
if (Flavorlist.size () >0) {
Flavor = (flavor) flavorlist.get (0);
}
PrintObject (flavor);
return flavor;
}
Get the key model
public static KeyPair Getkeyparimodel () {
KeyPair KeyPair = null;
Keypairs keypairs = Novaclient.execute (Keypairsextension.listkeypairs ());
list<keypair> keypairlist = Keypairs.getlist ();
if (Keypairlist.size () >0) {
KeyPair = keypairlist.get (0);
}
PrintObject (KeyPair);
return keypair;
}
Get Image model
public static Image Getimagemodel () {
Image Image =null;
Images Images = Novaclient.execute (Imagescore.listimages ());
list<image> imageList = Images.getlist ();
if (Imagelist.size () >0) {
Image = Imagelist.get (0);
}
PrintObject (image);
return image;
}
Get FLOATP Model
public static FLOATINGIP Getfloatipmodel () {
FLOATINGIP FLOATINGIP = null;
Floatingips floatingips = Novaclient.execute (Floatingipsextension.listfloatingips ());
list<floatingip> floatingiplist = Floatingips.getlist ();
if (Floatingiplist.size () >0) {
FLOATINGIP = floatingiplist.get (0);
}
PrintObject (FLOATINGIP);
return FLOATINGIP;
}
---------------------Create--------------
Create security Group
public static void Createsecuritygroup () {
Securitygroup Securitygroup = Novaclient.execute (Securitygroupsextension.createsecuritygroup (" Groupsecrityforlosttest "," This securitygroup is the Create for lost, just for Test "));
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 "));
System.out.println (Securitygroup);
}

Create KeyPair
public static void Createkeypair () {//Create a KeyPair
KeyPair KeyPair = Novaclient.execute (Keypairsextension.createkeypair ("Mykeypair"));//keypair name must be unique
System.out.println (Keypair.getprivatekey ());
}
Create Flavor?? It seems that it is not possible to create via API



Create a server
public static void Createserver () {

Serverforcreate serverforcreate = new Serverforcreate ();
Serverforcreate.setname ("Createbylost");
Serverforcreate.setflavorref (Getflavormodel (). GetId ());
Serverforcreate.setimageref (Getimagemodel (). GetId ());
Serverforcreate.setkeyname (Getkeyparimodel (). GetName ());
Serverforcreate.setaccessipv4 (Getfloatipmodel (). GetIP ());
Serverforcreate.getsecuritygroups (). Add (New Serverforcreate.securitygroup ("Default"));
Serverforcreate.getsecuritygroups (). Add (New Serverforcreate.securitygroup (Getsecuritygroup (0). GetName ()));
Server server = Novaclient.execute (Serverscore.createserver (serverforcreate));
SYSTEM.OUT.PRINTLN (server);
}

}

Openstack4j a Java Sample demo

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.