01、使用OpenStack4j快速开发对外接口

在项目中增加maven依赖包

<dependency>
  <groupId>org.pacesys</groupId>
  <artifactId>openstack4j</artifactId>
  <version>3.0.3</version>
  <classifier>withdeps</classifier>
</dependency>

通过权限认证

import org.openstack4j.api.OSClient.OSClientV3;
import org.openstack4j.openstack.OSFactory;
import org.openstack4j.model.common.Identifier;

# use Identifier.byId("domainId") or Identifier.byName("example-domain")
Identifier domainIdentifier = Identifier.byId("domainId");

# unscoped authentication
# as the username is not unique across domains you need to provide the domainIdentifier
OSClientV3 os = OSFactory.builderV3()
                       .endpoint("http://127.0.0.1:5000/v3")
                       .credentials("admin","sample", domainIdentifier)
                       .authenticate();

# project scoped authentication
OSClientV3 os = OSFactory.builderV3()
                    .endpoint("http://127.0.0.1:5000/v3")
                    .credentials("admin", "secret", Identifier.byName("example-domain"))
                    .scopeToProject(Identifier.byId(projectIdentifier))
                    .authenticate();

# domain scoped authentication
# using the unique userId does not require a domainIdentifier
OSClientV3 os = OSFactory.builderV3()
                    .endpoint("http://127.0.0.1:5000/v3")
                    .credentials("userId", "secret")
                    .scopeToDomain(Identifier.byId(domainIdentifier))
                    .authenticate();
        
# Scoping to a project just by name isn't possible as the project name is only unique within a domain. 
# You can either use this as the id of the project is unique across domains             
OSClientV3 os = OSFactory.builderV3()
                    .endpoint("http://127.0.0.1:5000/v3")
                    .credentials("userId", "secret")
                    .scopeToProject(Identifier.byName(projectName), Identifier.byName(domainName))
                    .authenticate();

# Or alternatively
OSClientV3 os = OSFactory.builderV3()
                    .endpoint("http://127.0.0.1:5000/v3")
                    .credentials("userId", "secret")
                    .scopeToDomain(Identifier.byName(domainName))
                    .authenticate();

常用模块调用

// Find all Users
List<? extends User> users = os.identity().users().list();
// List all Tenants
List<? extends Tenant> tenants = os.identity().tenants().list();
// Find all Compute Flavors
List<? extends Flavor> flavors = os.compute().flavors().list();
// Find all running Servers
List<? extends Server> servers = os.compute().servers().list();
// Suspend a Server
os.compute().servers().action("serverId", Action.SUSPEND);
// List all Networks
List<? extends Network> networks = os.networking().network().list();
// List all Subnets
List<? extends Subnet> subnets = os.networking().subnet().list();
// List all Routers
List<? extends Router> routers = os.networking().router().list();
// List all Images (Glance)
List<? extends Image> images = os.images().list();
// Download the Image Data
InputStream is = os.images().getAsStream("imageId");
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 前言 在Java项目开发中,项目的编译、测试、打包等是比较繁琐的,属于重复劳动的工作,浪费人力和时间成本。以往开发...
    JourWon阅读 1,181评论 0 1
  • 1.Maven介绍 Maven是一个基于项目对象模型(POM)的概念的纯java开发的开源的项目管理工具。主要用来...
    蒲公英爱上阅读 1,181评论 0 0
  • Maven的基本了解 什么是Maven? Maven就是Apache下的一个开源项目。它是用纯java开发的。是一...
    Bcome阅读 3,154评论 0 7
  • maven是一款优秀的服务构建工具,基于约定优于配置原则,提供标准的服务构建流程。maven的优点不仅限于服务构建...
    zhipingChen阅读 4,080评论 0 4
  • 乳酪般的心融化在晶亮中 (一倩) 柔滑的不敢去触碰 唯恐惊了那片宁静 就这样 在晶亮的玻璃外 欣赏着,徘徊着 乳酪...
    天下行者一心阅读 248评论 0 5

友情链接更多精彩内容