openstack java sdk开发包

int32位 posted @ Mar 23, 2016 12:39:00 AM in openstack , 2757 阅读
转载请注明:http://krystism.is-programmer.com/若有错误,请多多指正,谢谢!

What's openstack ?

OpenStack是一个云平台管理的项目,由几个主要的组件组合起来,旨在为公共及私有云的建设与管理提供平台。它是由 Rackspace 和 NASA 共同开发的云计算平台,帮助服务商和企业内部实现类似于 Amazon EC2 和 S3 的云基础架构服务(Infrastructure as a Service, IaaS),具体查看openstack官网

About this project

这个项目旨在写一个方便调用、方便扩展的openstack开发包。它是根据openstack api封装的java开发库,最初应实验室需求而开发。目前已经花了陆陆续续近3个月的时间,仅仅实现了基本功能,还有很多bug,以及功能尚未实现。

项目地址:https://github.com/krystism/openstack-java-sdk

How to use ?

首先使用maven构建本项目:

mvn package

如果需要把依赖包一块打进一个整体包,使用一下命令:

mvn assembly:assembly

项目运行需要配置文件,配置文件路径由系统变量OPENSTACK_CONF_PATH指定,默认为/etc/openstack,参考配置文件说明 我学习了openstack官方python库的设计架构,尽力做到调用简单,方便.以下是demo:

    OpenstackSession session = OpenstackSession.getSession("username", "password");// get session
    Nova nova = session.getNovaClient(); // get nova client
    // get a flavor list, print their name
    for (Flavor flavor : nova.flavors.list()) {
        System.out.println(flavor.getName());
    }
    // create a new server
    Server server = new Server();
    server.setName("demo");
    server.setImageRef("imageId");
    server.setFlavorRef("flavorId");
    // some other config 
    nova.servers.create(server); // call create method to execute.

Join me !

由于时间有限,加上本人代码功底尚浅,需要完美完成所有功能实在是感觉力不从心。希望有更多的大牛能够加入一起完成,并且指出我目前工作的不足,甚至重构整个代码,我真诚的感谢每一位热心的朋友!

下面讲讲我目前如何扩展新功能:

Step 1:创建model

model就是实体对应的java bean,接收请求过来的数据一般是json,即要把json表示的对象转化成的java对象。你可以完全由自己来定义自己的bean,我的所有bean都继承AbstractEntity,属性由Property注解,构造方法传递一个JSONObject对象,一般直接调用基类的构造方法就能完成JSONObject到java bean的转化。以下是model demo:

@Entity("volume_type")
public class VolumeType extends AbstractEntity {
    private static final long serialVersionUID = -6539238104579991330L;
    @Property("extra_specs")
    private JSONObject metadata;
    /*
    @Property("name")
    private String name;
    @Property("id")
    private String id;
    */
    public VolumeType() {
        super();
    }
    public VolumeType(JSONObject jsonObj) {
        super(jsonObj);
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    // some other getter and setter here
}

Step 2:创建bean管理接口

管理接口即对bean定义操作,这些操作可能根据api的版本不同有不同的实现。在设计接口时,我没有使用共同接口,也尚未使用接口继承,原因是很多实体bean对应的操作差异性很大,即使是相同的操作,参数也不完全相同。下面是一个管理接口demo:

public interface ServerManager {
    /**
     * Get a server.
     * @param id ID of the Server to get.
     * @return Server
     * @throws OperationException
     */
    Server get(String id) throws OperationException;
    /**
     * Get a list of servers.
     * @return List of server
     * @throws OperationException
     */
    List<Server> list() throws OperationException;
    /**
     * Stop(power off) the server
     * @param id The id of server to stop.
     * @throws OperationException
     */
    void stop(String id) throws OperationException;
    /**
     * Start(power on) the server,
     * @param id The id of server to start.
     * @throws OperationException
     */
     void start(String id) throws OperationException;
     /**
      * Reboot a server, for a software-lever reboot
      * @param id The ID of server to reboot.
      * @see reboot(String id, boolean hard)
      */
     void reboot(String id) throws OperationException;
     /**
      * update the name for a Server
      * @param id the id of server to rename
      * @param name the new name of the server.
      * @throws OperationException
      */
     void rename(String id, String name) throws OperationException;
     /**
      * Create (boot) a new Server.<br/>
      * <i>Remember</i> : You must set name, imageRef, flavorRef !
      * @param instance The new server you have created.
      * @return
      * @throws OperationException
      */
     Server create(Server instance) throws OperationException;
     /**
      * Delete (i.e shut down and delete the image) this server.
      * @param id The ID of server to delete.
      * @throws OperationException
      */
     void delete(String id) throws OperationException;
}

Step 3: 实现管理接口

接下来就是具体实现接口,可能会根据api版本不同而有不同的实现,实际应用时可以根据配置文件指定需要使用的版本。下面是一个实现Demo:

public class Flavors extends AbstractManager<Flavor> implements FlavorManager{
    private final String PREFIX = "/flavors";
    public Flavors(Authenticated credentical) {
        super(credentical, Flavor.class);
    }
    /**
     * Get a list of all flavors
     * @return A List , which holds flavors
     * @throws OperationException
     */
    @Override
    public List<Flavor> list() throws OperationException {
        return _list(PREFIX + "/detail");
    }
    /**
     * Get a specific Flavor.
     * @param id The ID of Flavor to get. 
     * @return Flavor
     * @throws OperationException
     */
    @Override
    public Flavor get(String id) throws OperationException {
        return _get(PREFIX + "/" + id);
    }
    /**
     * Delete a specific Flavor.
     * @param id The ID of Flavor to delete.
     * @throws OperationException
     */
    @Override
    public void delete(String id) throws OperationException {
        _delete("/flavors/" + id);
    }
    /**
     * create a new flavor for a tenant
     * @param flavor The flavor to create
     * @return The new flavor
     * @throws OperationException
     */
    @Override
    public Flavor create(Flavor flavor) throws OperationException {
        return _create("/flavors", flavor);
    }
}

Step 4: 注册新功能

注册新功能就是把相应的管理接口添加到client下(比如Nova, Glance等),目前我直接硬编码,实际操作应该根据配置文件选择api版本由工厂负责创建,以下是demo:

public class Nova {
    public final FlavorManager flavors;
    public final HypervisorManager hypervisors;
    public final ServerManager servers;
    public final KeyPairManager keypairs;
    public Nova(Authenticated credentical) {
        // bad work, don't do that!!
        flavors = new Flavors(credentical);
        hypervisors = new Hypervisors(credentical);
        servers = new Servers(credentical);
        keypairs = new KeyPairs(credentical);
    }

How to extend new features?

如何为已经实现的接口增加新功能,比如在请求前记录日志,或者使用cache。只需要增加装饰器即可!以下是Demo:

public class FlavorCachedManager implements FlavorManager {
    private FlavorManager flavors;
    public FlavorCachedManager(FlavorManager flavors) {
        this.flavors = flavors;
    }
    @Override
    public Flavor get(String id) throws OperationException {
        Flavor flavor = getFromCache(id);
        if (flavor == null) {
            flavor = flavors.get(id);
        }
        addToCache(flavor);
        return flavor;
    }

}

Copyright

The MIT License (MIT)

转载请注明:http://krystism.is-programmer.com/若有错误,请多多指正,谢谢!
  • 无匹配
  • 无匹配
전설 서구 说:
2021年3月04日 21:00

New web site is looking good. Thanks for the great effort. movies downloads sites

전설 서구 说:
2021年3月13日 18:21

I appreciated your work very thanks naadeali.com/

전설 서구 说:
2021年3月14日 15:50

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me. gogo anime

John 111 说:
2021年3月15日 02:22

What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. Thank you very much. jetez un œil ici

John 111 说:
2021年3月16日 13:41

I've been looking for info on this topic for a while. I'm happy this one is so great. Keep up the excellent work Check This Out

전설 서구 说:
2021年3月17日 18:33

Praėjus keliems, įstatymais numatytiems mėnesiams, valdžios institucijos pradės legendinių, 40 metų Añaza pakrantės peizažą gadinusių griaučių ardymą. tenerife world map

전설 서구 说:
2021年3月18日 19:11

Žinoma labai svarbus yra nusiteikimas. Kiekvienas, kuris tikrai nori vairuoti, turi didelę tikimybę, kad jam viskas puikiai klostysis. Visai kitaip sekasi tiems, kurie vairuoti nenori ar net bijo sėsti už automobilio vairo, todėl reikia suprasti, kad vidinė motyvacija mokytis vairuoti yra svarbiausia. Ne mažiau svarbu ir tai, kaip mokomasi. ket bilietai ir testai

John 111 说:
2021年3月19日 03:35

Search, compare, analyze and plan your holiday in Sydney. Book cheap flights to Sydney for your budget travel. Experience this wonderful city with its beautiful beaches, excellent night life and delicacies of all over the world. cheap flight deals

John 111 说:
2021年3月19日 03:40

Barcelona is a key European tourist destination with many tourist attractions. If you plan to make an economic trip to this coastal city, there are various tips that can assist you get an economical Barcelona flight. cheapo flights

biaakhan 说:
2021年3月30日 19:12

I really appreciate this wonderful post that you have provided for us. I assure this would be beneficial for most of the people. Health Fitness Post

John 111 说:
2021年3月30日 20:13

Great survey. I'm sure you're getting a great response. 澳洲高等教育留学

John 111 说:
2021年4月01日 20:29

Chicago is a popular destination for travelers. It doesn't matter whether you're coming for business or pleasure. This city offers some of the best hospitality, that you will experience anywhere. cheap ticket

John 111 说:
2021年4月06日 01:23

This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post. 토토갤러리

전설 서구 说:
2021年4月08日 16:14 I really thank you for the valuable info on this great subject and look forward to more great posts. Thanks a lot for enjoying this beauty article with me. I am appreciating it very much! Looking forward to another great article. Good luck to the author! All the best! ดูอนิเมะ
John 111 说:
2021年4月08日 18:03

I definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work. strongest testosterone booster

전설 서구 说:
2021年4月08日 19:25

I definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work. also check

John 111 说:
2021年4月09日 00:15

Today it is possible to book cheap travel and explore the world without spending too much. Booking online cheap flights from Canberra to any destination in Australia or overseas is fast and easy. cheapoflights

John 111 说:
2021年4月10日 22:55

I wanted to thank you for this excellent read!! I definitely loved every little bit of it. I have you bookmarked your site to check out the new stuff you post. penis enlargement

John 111 说:
2021年4月11日 13:51

Truly, this article is really one of the very best in the history of articles. I am a antique ’Article’ collector and I sometimes read some new articles if I find them interesting. And I found this one pretty fascinating and it should go into my collection. Very good work! 메이저사이트

John 111 说:
2021年4月12日 19:01

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info. testogen review

John 111 说:
2021年4月14日 18:45

Cheap Flights Find Cheap Flight For Your Searching for cheap airfare and Last Minte Travel deals your cheap flight deals are almost ready Pay the remaining amount up to 72 hours before your flight's departure. Save money & time on your next flight booking with cheap flights.com Average trip savings calculated based on the price. For domestic flights, mondays showed the highest average airline ticket prices and for international flights, avoid booking on fridays. Find Cheap Flights

John 111 说:
2021年4月23日 12:28

Your website is really cool and this is a great inspiring article. Cuet kuet ruet Admission

John 111 说:
2021年4月23日 12:41

I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work... how to find the dark web

Top SEO 说:
2021年4月24日 12:52

I most likely appreciating each and every bit of it. It is an incredible site and decent impart. I need to much obliged. Great employment! You all do an incredible blog, and have some extraordinary substance. Keep doing awesome. kuliah karyawan

Top SEO 说:
2021年4月24日 13:09

Nice to read your article! I am looking forward to sharing your adventures and experiences. women's clothes

Top SEO 说:
2021年4月24日 13:13

Thanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? I’ve a undertaking that I am simply now operating on, and I have been at the look out for such info. ロリラブドール

John 111 说:
2021年5月02日 13:10 What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. Thank you very much. Non Woven Fabric Factory
John 111 说:
2021年5月06日 03:14

A very awesome blog post. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. buy psilocin pills online

John 111 说:
2021年5月17日 19:54

I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much. 안전놀이터

John 111 说:
2021年5月21日 18:16

Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our. 먹튀검증

John 111 说:
2021年5月26日 19:07

A bullet proof vest can save your life in the nick of time. If gun fights happen to be a part of your life, you cannot think of getting out there without one of these vests. best place to order firearm

John 111 说:
2021年5月30日 15:04

The capital city of the world's richest country, Doha is the backbone of the Qatar's economy. Literally translated as 'The Big Tree', Doha has been at the center of all the major activities held in and done by Qatar. Being the host of the Asian Games, 2006 brought the city in the limelight, and it has been shining gloriously since then. Filled with an invigorating environment which gives equal importance to the rapid progress and old traditions, one can roam around old souqs and gorgeous malls and see the crux of Doha in both. Alessandro Bazzoni

John 111 说:
2021年5月30日 15:22

Discover the key to overcoming workplaces distractions and working in a more focused and productive way. Marshmallows just may hold the key to getting more done in less time despite the distractions around you from emails, texts and conversations. Alessandro Bazzoni

John 111 说:
2021年5月30日 15:33

Como is a small town that is situated in the Lombardy region of Italy which is located along the Italian Swiss border. This place served as the crossing point between the Central Europe and the Mediterranean Sea. The city is also known for the silk which is produced locally here. Alessandro Bazzoni

John 111 说:
2021年5月30日 15:43

The football equivalent of the Oscars swept into London on Tuesday as the first ever FIFpro awards were announced in front of renowned stars from the worlds of sport, music, screen and even our very own Marc Duffy (but the legality of how the ticket was gain is still under suspicion!) Alessandro Bazzoni

John 111 说:
2021年5月30日 15:55

Insurance Agency Websites, SEO, SEM, Social Media Marketing and Blogging - there is certainly a lot to keep up with these days in terms of staying current and reaching a given target market. Insurance agency marketing should funnel content toward a given target market. A primary step toward this goal is the creation of a quality blog. Once that is accomplished, and to ensure an agency remains ahead of the curve, agents should seriously consider vlogging as an important addition to their insurance agency web marketing strategy. PR Agencies in India

John 111 说:
2021年5月30日 16:04

Although the implants, made out of titanium alloy or ceramic, are now considered the Number 1 option to replace missing teeth but because they are surgically implanted in jaw you may encounter some problems and complications. As with any surgical procedure there are some potential downsides to dental implants that you should be aware of before considering it as an alternative for replacing your missing teeth. Dental Implants in India

John 111 说:
2021年5月31日 23:26

I appreciated your work very thanks 꽁머니사이트

먹튀검증 说:
2021年5月31日 23:49

Your articles are inventive. I am looking forward to reading the plethora of articles that you have linked here. Thumbs up!

마추자먹튀 说:
2021年6月10日 15:04

Great post, you have pointed out some fantastic points , I likewise think this s a very wonderful website.

John 111 说:
2021年6月11日 08:10

This is my first visit to your web journal! We are a group of volunteers and new activities in the same specialty. Website gave us helpful data to work. eco friendly storage bin

John 111 说:
2021年6月12日 12:40

Thanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? I’ve a undertaking that I am simply now operating on, and I have been at the look out for such info. slot online

John 111 说:
2021年6月15日 01:26

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info. sewing kit

John 111 说:
2021年6月15日 13:12

I was surfing the Internet for information and came across your blog. I am impressed by the information you have on this blog. It shows how well you understand this subject. dewacasino

John 111 说:
2021年6月16日 12:01

This is my first visit to your web journal! We are a group of volunteers and new activities in the same specialty. Website gave us helpful data to work. earthhershop

Top SEO 说:
2021年6月18日 08:29

check this link. its full of sex education. and also useful in your life. and help in your sex life better porno en paraguay 2021

John 111 说:
2021年6月21日 11:21

This particular papers fabulous, and My spouse and i enjoy each of the perform that you have placed into this. I’m sure that you will be making a really useful place. I has been additionally pleased. Good perform! nextclippingpath

John 111 说:
2021年6月22日 19:51

This blog is so nice to me. I will keep on coming here again and again. Visit my link as well.. extra long cobweb duster

John 111 说:
2021年6月25日 18:07

You make so many great points here that I read your article a couple of times. Your views are in accordance with my own for the most part. This is great content for your readers. 토토사이트검증

John 111 说:
2021年6月26日 18:54

I need to position my website very high in Google both in México luxury courses online

John 111 说:
2021年6月30日 23:18 I wanted to thank you for this excellent read!! I definitely loved every little bit of it. I have you bookmarked your site to check out the new stuff you post. 안전놀이터
Top SEO 说:
2021年9月21日 06:37

If you are looking for more information about flat rate locksmith Las Vegas check that right away. Uttarakhand 10th Question Paper

Top SEO 说:
2021年9月28日 14:10

I wanted to thank you for this excellent read!! I definitely loved every little bit of it. I have you bookmarked your site to check out the new stuff you post. Restaurant Business Consultant

MBOSE Board 12th Exa 说:
2021年10月11日 15:16

The Meghalaya Board of School Education has announced the MBOSE HSSLC model paper 2022. The board publishes MBOSE class 12 model papers in pdf format for English, Mathematics, Hindi, Physics, Chemistry, Accounting, History, and other courses. MBOSE Board 12th Exam Pattern 2022 Candidates should download these MBOSE Sample model papers Class 12 and practise for their test since they will gain an understanding of the exam format, such as the types of questions that will be asked, the marking scheme, and so on.

Top SEO 说:
2021年10月14日 16:31

foodbusinessexpert foods which has the foodbusinessexpert affect of physically changing persons chemistry. Once the foodbusinessexpert bodies chemistry has changed which affects emotional and mental association to eating comfort food and relaxation through repeated eating comfort food for through reinforcement of behavior with comfort food the mind learns to relax before it even starts to eat your foodbusinessexpert comfort food and that experience forms mental addicted attachment and behavior. Restaurant Management Tips


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter