OpenStack-API开发

介绍两种OpenStack-API(Java版)——jcoulds && openstack4j

Jclouds

1.介绍

jclouds -该API提供云计算环境的可移植抽象层以及云规范特性,支持包括 Amazon, VMWare, Azure, and OpenStack 等云计算平台,其中针对OpenStack,目前支持Nova、cinder、glance、Neutron、Keystone、swift等API接口,但是不支持Ceilometer的API接口。

2.使用与Jar包

官方文档——强烈推荐
http://jclouds.apache.org/guides/openstack/

jcloudsjar包使用maven安装下载管理即可;

3.举例

import com.google.common.collect.ImmutableSet;
import com.google.common.io.Closeables;
import com.google.inject.Module;
import org.jclouds.ContextBuilder;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.openstack.nova.v2_0.NovaApi;
import org.jclouds.openstack.nova.v2_0.domain.Server;
import org.jclouds.openstack.nova.v2_0.features.ServerApi;

import java.io.Closeable;
import java.io.IOException;
import java.util.Set;

public class JCloudsNova implements Closeable {
    private final NovaApi novaApi;
    private final Set<String> regions;

    public static void main(String[] args) throws IOException {
        JCloudsNova jcloudsNova = new JCloudsNova();

        try {
            jcloudsNova.listServers();
            jcloudsNova.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jcloudsNova.close();
        }
    }

    public JCloudsNova() {
        Iterable<Module> modules = ImmutableSet.<Module>of(new SLF4JLoggingModule());

        String provider = "openstack-nova";
        String identity = "demo:demo"; // 项目管理员和用户
        String credential = "devstack";// 用户密码

        novaApi = ContextBuilder.newBuilder(provider)
                .endpoint("http://xxx.xxx.xxx.xxx:5000/v2.0/")//API地址
                .credentials(identity, credential)
                .modules(modules)
                .buildApi(NovaApi.class);
        regions = novaApi.getConfiguredRegions();
    }

    private void listServers() {
        for (String region : regions) {
            ServerApi serverApi = novaApi.getServerApi(region);

            System.out.println("Servers in " + region);

            for (Server server : serverApi.listInDetail().concat()) {
                System.out.println(" " + server);
            }
        }
    }

    public void close() throws IOException {
        Closeables.close(novaApi, true);
    }
}

4.测试效果:

比较稳定,而且API文档很全,教好理解;

OpenStack4j

1.介绍

针对OpenStack的Web-API,包含Keystone、Nova、Neutron、Glance、Cinder
、Swift、Ceilometer、Heat的API支持,更新速度快,有问题需求及时联系Github可以得到解决。

2.使用与Jar包

官方文档——http://www.openstack4j.com/learn/
http://www.openstack4j.com/javadoc/
OpenStack4j -jar包使用提供的 openstack4j-2.0.1-withdeps-sources.jar即可,注意使用的jdk最好为1.7以上,jdk1.7最佳。java ee 5 或Java ee 6 均可。(使用Myeclipse时,jdk版本为1.6,容易报 version 51 错误。)

3.举例:

        List<String> openStackNetworkList=new ArrayList<String>();
        openStackNetworkList.add("df6effa2-a1b5-400a-8ab1-457673591aa1");
        String openstackImageId = ""
        String openstackFlavorId= "";
        String openstackServerName = "";
        String openstackZone = "nova:compute2";


        ServerCreate sc = Builders.server().name(openstackServerName)
                                           .flavor(openstackFlavorId)
                                           .image(openstackImageId)
                                           .networks(openStackNetworkList)
                                           .availabilityZone(openstackZone)
                                           .build();

        // Boot the Server
        OSClient os = OSFactory.builder()
        .endpoint("http://x.x.x.x:5000/v2.0/")
        .credentials("xxx","xxx")
        .tenantName("xxx")
        .authenticate();

        Server server = os.compute().servers().boot(sc);

4.使用效果

API简单易用;而且支持Ceilometer和Heat,推荐使用。

你可能感兴趣的:(api,开发,环境,openstack)