1. Help Center
  2. CLI
  3. Developer Guide
  4. SDK
  5. JAVA
  6. ECS Java SDK Demo

ECS Java SDK Demo

Creating an ECS

  1. Obtain a flavor ID.

    You can run the os.compute().flavors().list() command to query all flavors and use a qualified flavor ID to create an ECS.

  2. Create a security group.

    For details about how to create a security group, see section How Can I Create a Security Group?.

    Optionally, you can use OpenStack4j to create a security group based on the following code:

    org.openstack4j.model.network.SecurityGroup sg = os.networking().securitygroup().create(Builders.securityGroup() 
    SecurityGroupRule rule = Builders.securityGroupRule() 
    SecurityGroupRule sgrule = os.networking().securityrule().create(rule);

    Security group rules vary, but need to meet network communication requirements.

  3. Create a key pair.

    For details about how to create a private key pair, see section How Can I Create a Key Pair on the Console?.

    Optionally, you can create a key pair using OpenStack4j based on the following code:

    String testPublicKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDdNdUHJHvVaz7ObMKKm4EfqtN8COmcbT7GrCsfrlxz95nUc8GlAK51VaQjqGU5RwIKPcOfrkJTcXst//pgx7PyzrWrmFlOIjaY8e9HxLQcz2IzrbrbM8TJhB+I3cApdpwsTqGwjW1xzcMgSrqB0BT7gU4mau0I7Z50RszAhYUVpwGk5OpZGxcXSBQSdSr/KKI6BuMNJYtugGn5dmr9Ddf99TLbIleLYjmqB0rMjNKHPUxEYMLtixKvXp0qNFfShu7bDp7e2TjhGY8wpda0kC2dDGQJKE187N+A3hp2XBZ2UjTcjAVa3C+dwHzHCZd6/yAfJoENe2fDRVnb8cMgm/UX Generated-by-Nova"; 
    Keypair keypair = os.compute().keypairs().create("keyPairName", testPublicKey);
  4. Create an ECS.

    You can use the following code to create an ECS. You can use interface WaitForServerStatus to continuously query the ECS status until the ECS is in the specified status or the query times out. You can modify the parameters as required. In the following example, the timeout interval is set to 10 minutes.

    private void createTestServer() throws IOException { 
    ArrayList networkList = new ArrayList(); 
    ServerCreate serverCreate = Builders.server().name("vm-name") 
    Server server = os.compute().servers().boot(serverCreate); 
    os.compute().servers().waitForServerStatus(server.getId(), Server.Status.ACTIVE, 10, TimeUnit.MINUTES); 
Table 1 Parameter description



Example Value


Specifies the ID of the flavor.



Specifies the ID of the image.


Binding an EIP to an ECS

  1. Query the ECS port ID.

    You can query the ECS port ID, which is used for creating the EIP, based on the following code.

    List<? extends InterfaceAttachment> nicID = os.compute().servers().interfaces().list(server.getId()); 
    String port_id = nicID.get(0).getPortId();
  2. Create the EIP.

    You can create the EIP based on the following code.

    As ECS creation takes some time, you need to check the ECS status.

    NetFloatingIP fip = os.networking().floatingip().create(Builders.netFloatingIP().floatingNetworkId("external_network_id").portId(port_id).build()); 
    if (os.networking().floatingip().get(fip.getId()).getStatus().equals("ACTIVE")) { 
    System.out.println("EIP API responsed Success"); 
    // judge fip is created successfully 
    int count = 1; 
    boolean createFlat = false; 
    while (count < 10) { 
    if (os.networking().floatingip().get(fip.getId()).getStatus().toString().equals("ACTIVE")) { 
    System.out.println("Confirmed EIP Create Success"); 
    createFlat = true; 
    if (!createFlat) { 
    System.out.println("EIP is not successfully created"); 

    external_network_id indicates the external network ID. For details, see section External Network.

    The EIP is automatically bound to the ECS after being created because you specify the ECS port ID to create the EIP.

Unbinding an EIP from an ECS

You can unbind an EIP from an ECS based on the following code.

The unbinding operation takes several seconds, and the EIP can be deleted only after it is unbound. An error message may be reported if you delete an EIP during the unbinding operation.

// after the fip is ACTIVE,Do the remove action 
ActionResponse removeAction = os.compute().floatingIps().removeFloatingIP(server, fip.getFloatingIpAddress()); 
if (removeAction.isSuccess()) { 
System.out.println("Remove Response Action Success"); 

// judge the EIP disassociate successfully 
int j = 1; 
boolean removeFlag = false; 
while (j < 10) { 
if (os.networking().floatingip().get(fip.getId()).getPortId() == null 
&& os.networking().floatingip().get(fip.getId()).getStatus().toString().equals("DOWN")) { 
removeFlag = true; 
System.out.println("Confirmed disassociate successfuly"); 
if (!removeFlag) { 
System.out.println("Disassociate failure"); 

Deleting an ECS

ECS deletion operation consumes some time, and during the process, the port is not deleted immediately.

You need to query the ECS status based on the ECS ID. If null is returned, the ECS is deleted, and you can delete the network.

ActionResponse deleteRespone = os.compute().servers().delete(server.getId()); 
if (deleteRespone.isSuccess()) { 
System.out.println("Delete Server action response :" + deleteRespone); 

int i = 1; 
boolean deleteServerStatus = false; 
while (i < 10) { 
if (os.compute().servers().get(server.getId()) == null) { 
deleteServerStatus = true; 
System.out.println("Confirmed delete server"); 

if (!deleteServerStatus) { 
System.out.println("Delete Server failed"); 

Querying the ECS Status

You can query the ECS status based on the following code:


(Optional) Modifying the ECS Flavor

After the flavor is modified, you can roll back the modification or make the modification take effect.

  1. Modify the flavor.
    os.compute().servers().resize(serverId, flavorId)
  2. Confirm the modification.
  3. Roll back the modification. Rollback cannot be performed if you have already made the modification take effect.

Restarting an ECS

Run the os.compute().servers().reboot(serverID, RebootType.SOFT) command to restart an ECS.

Stopping an ECS

Run the os.compute().servers().action(serverID, Action.STOP) command to stop an ECS.


An image is the OS of a VM and it is a series of files used to create or rebuild servers. By default, carriers provide preset OS images, but you can also create custom images from the ECSs.

List images.

import java.util.List;
import com.huawei.openstack4j.model.compute.Image;

List<? extends Image> imgList = os.compute().images().list();
if(imgList.size() > 0) {
 System.out.println("getImageList success, size = " + imgList.size());
}else {
 System.out.println("getImageList failed");

Obtain details of a specified image.

import com.huawei.openstack4j.model.compute.Image;

// get image by id
Image image = os.compute().images().get("imgId");
if(null != image) {
 System.out.println("getImage success, name = " + image.getName());
}else {
 System.out.println("getImage failed");

Query details of images.

import java.util.List;
import com.huawei.openstack4j.model.compute.Image;

List<? extends Image> imgListDetail = os.compute().images().list(true);
if(imgListDetail.size() > 0) {
 System.out.println("getImageListDetail success, size = " + imgListDetail.size());
}else {
 System.out.println("getImageListDetail failed");


import java.util.List;
import com.huawei.openstack4j.model.compute.Image;

List<? extends Image> imgListAll = os.compute().images().list(false);
if(imgListAll.size() > 0) {
 System.out.println("getImageListAll success, size = " + imgListAll.size());
}else {
 System.out.println("getImageListAll failed");

Delete an image.

import com.huawei.openstack4j.model.common.ActionResponse;
// delete image by id
ActionResponse rep = os.compute().images().delete("imgId");
if(rep.isSuccess()) {
 System.out.println("deleteImage success");
}else {
 System.out.println("deleteImage failed");

Server Group

Create a server group.

import com.huawei.openstack4j.model.compute.ServerGroup;

ServerGroup serverGroup = os.compute().serverGroups().create("servergroup_name", "anti-affinity");
if(null != serverGroup) {
 System.out.println("create serverGroup success, id = " + serverGroup.getId());
}else {
 System.out.println("create serverGroup failed");

Query details about a specified server group.

import com.huawei.openstack4j.model.compute.ServerGroup;

// get ServerGroup by id
ServerGroup serverGroupInfo = os.compute().serverGroups().get("serverGroupId");
if(null != serverGroupInfo) {
 System.out.println("get serverGroupInfo success, name = " + serverGroupInfo.getName());
}else {
 System.out.println("get serverGroupInfo failed");

Query the server group list.

import com.huawei.openstack4j.model.compute.ServerGroup;
import java.util.List;

List<? extends ServerGroup> list = os.compute().serverGroups().list();
if(list.size() > 0) {
 System.out.println("get ServerGroupList success, size = " + list.size());
}else {
 System.out.println("get ServerGroupList failed");

Delete a server group.

import com.huawei.openstack4j.model.common.ActionResponse;

// delete ServerGroup by id
ActionResponse rep = os.compute().serverGroups().delete("serverGroupId");
if(rep.isSuccess()) {
 System.out.println("deleteserverGroup success");
}else {
 System.out.println("deleteserverGroup failed");

Attaching or Detaching NICs

Attach a NIC to a server.

import com.huawei.openstack4j.model.compute.InterfaceAttachment;

// attach the specified port to the server
InterfaceAttachment newAttach = os.compute().servers().interfaces().create("serId", "portId");
if(null != newAttach) {
 attachmentId = newAttach.getPortId();
 System.out.println("create InterfaceAttachment success, portStatus = " + newAttach.getPortState());
}else {
 System.out.println("create InterfaceAttachment failed");

List server NICs.

import com.huawei.openstack4j.model.compute.InterfaceAttachment;
import java.util.List;

// get InterfaceAttachment list by serverId
List<? extends InterfaceAttachment> list = os.compute().servers().interfaces().list("serId");
if(list.size() > 0) {
 System.out.println("get InterfaceAttachmentList success, size = " + list.size());
}else {
 System.out.println("get InterfaceAttachmentList failed");

Query details about a specified NIC.

import com.huawei.openstack4j.model.compute.InterfaceAttachment;

// get server attachment by attachmentId
InterfaceAttachment gotAttach = os.compute().servers().interfaces().get("serId", "attachmentId");
if(null != gotAttach) {
 System.out.println("get InterfaceAttachment success, AttachmentId = " + gotAttach.getPortId());
}else {
 System.out.println("get InterfaceAttachment failed");

Detach a NIC.

import com.huawei.openstack4j.model.common.ActionResponse;

// delete a specified attachmentId
ActionResponse rep = os.compute().servers().interfaces().detach("serId", "attachmentId");
if(rep.isSuccess()) {
 InterfaceAttachment delAttach = os.compute().servers().interfaces().get("serId", "attachmentId");
 System.out.println("detach success, deletedAttachment = " + delAttach);
}else {
 System.out.println("detach failed");


Obtain the AZ list.

import java.util.List;
import com.huawei.openstack4j.model.compute.ext.AvailabilityZone;

List<? extends AvailabilityZone> zoneList = os.compute().zones().list();
if(zoneList.size() > 0) {
 System.out.println("get zoneList success, size = " + zoneList.size());
}else {
 System.out.println("get zoneList failed");