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");
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值