码迷,mamicode.com
首页 > Windows程序 > 详细

OpenStack-API开发

时间:2015-04-03 00:18:37      阅读:343      评论:0      收藏:0      [点我收藏+]

标签: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,推荐使用。

OpenStack-API开发

标签:openstack   环境   api   开发   

原文地址:http://blog.csdn.net/dapao123456789/article/details/44839177

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!