当前位置: 首页 > 工具软件 > OpenStack4j > 使用案例 >

openstack4j 源码_openstack4j a java sample demo

於鸿羲
2023-12-01

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); // 获取access中 id [token=Token

}

tenants = keystone.execute(new ListTenants());// 这个是获取keystoen数据库表中tenant表数据

if (tenants.getList().size() > 0) {

authentication = new Authentication();

Token token = new Token();

token.setId(tokenTemp);

authentication.setToken(token); // 就是上面获取的id:95d304ab72a14933be27ec2601355146

//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();//由于系统中还没有创建floatingip 所以目前出错

GetFloatIpModel();

//CreateServer();// 可以创建 实例,不过在这里绑定floating ip 没有起作用

}

public static void printObject(Object obj){

System.out.println(obj.getClass()+"   "+obj);

}

//------------查看平台-------

//查看平台中正在运行的server

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(){

//通过novaclient查看平台中的image

Images images = novaClient.execute(ImagesCore.listImages());

for(Image image :images){

System.out.println(image);

}

}

public static void ListKeyPairs(){

//查看keypair

KeyPairs keypairs = novaClient.execute(KeyPairsExtension.listKeyPairs());

for(KeyPair keypair:keypairs){

System.out.println(keypair);

}

}

public static void ListSecurityGroup(){

//查看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(){

}

//------获得信息------

//获得SecuritGroup model

public static SecurityGroup GetSecurityGroup(int index){

SecurityGroup sg = null ;

SecurityGroups sgs = novaClient.execute(SecurityGroupsExtension.listSecurityGroups());

List alist = sgs.getList();

if(alist.size() >index-1){

sg = (SecurityGroup)alist.get(index);

}

return sg;

}

//获得Flavor model

public static Flavor GetFlavorModel(){

Flavor flavor  = null;

Flavors flavors = novaClient.execute(FlavorsCore.listFlavors());

List flavorList = flavors.getList();

if(flavorList.size()>0){

flavor = (Flavor)flavorList.get(0);

}

printObject(flavor);

return flavor;

}

//获得key model

public static KeyPair GetKeyPariModel(){

KeyPair keypair = null;

KeyPairs keypairs = novaClient.execute(KeyPairsExtension.listKeyPairs());

List keypairList = keypairs.getList();

if(keypairList.size()>0){

keypair = keypairList.get(0);

}

printObject(keypair);

return keypair;

}

//获得 image model

public static Image GetImageModel(){

Image image =null;

Images images = novaClient.execute(ImagesCore.listImages());

List imageList = images.getList();

if(imageList.size() >0){

image = imageList.get(0);

}

printObject(image);

return image;

}

//获得floatp model

public  static FloatingIp GetFloatIpModel(){

FloatingIp floatingIp = null;

FloatingIps floatingIps = novaClient.execute(FloatingIpsExtension.listFloatingIps());

List floatingIpList = floatingIps.getList();

if(floatingIpList.size() >0 ){

floatingIp = floatingIpList.get(0);

}

printObject(floatingIp);

return floatingIp;

}

// ---------------------创建--------------

//create security group

public static void CreateSecurityGroup (){

SecurityGroup securityGroup = novaClient.execute(SecurityGroupsExtension.createSecurityGroup("GroupSecrityForLostTest", "This securityGroup is 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(){ //  创建一个keypair

KeyPair keyPair = novaClient.execute(KeyPairsExtension.createKeyPair("mykeypair"));//keypair name 所必须唯一的

System.out.println(keyPair.getPrivateKey());

}

//创建 flavor?? 貌似不可以通过api 创建

// 创建一个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);

}

}

 类似资料: