• CLI

sdk
  1. Help Center
  2. CLI
  3. Developer Guide
  4. SDK
  5. JAVA
  6. Getting Started

Getting Started

Prerequisites

  1. You have obtained required API documents.

    Log in to the following website to obtain the API documents:

    https://docs.otc.t-systems.com/

    With these documents, you can obtain the OpenStack APIs and related parameters supported by the cloud platform.

  2. You have obtained a cloud platform account and provisioned all required services.
  3. You have installed JDK. (The Java SDK is applicable to JDK1.7+. You are advised to use JDK1.8.)

SDK Acquisition and Installation

You can download the JAR file from the GitHub website and import the package to the interactive development environment (IDE).

https://github.com/huawei/cloud-sdk-release/tree/master/java-sdk

Take Eclipse as an example. After creating a Java project, perform the following steps to import the JAR file to the new project:
  1. Copy the downloaded JAR file to the project folder.
  2. Open the project in Eclipse, right-click the project, and choose Properties.
  3. In the displayed dialog box, click Java Build Path. On the Libraries tab, click Add JARs to add the downloaded JAR file.
  4. Click OK.

The services involved in this document use the same JAR file.

How to Use

Set parameters, initialize the SDK client, and invoke the SDK to access the service API.

package demo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.huawei.openstack4j.openstack.OSFactory;
import com.huawei.openstack4j.api.OSClient.OSClientV3;
import com.huawei.openstack4j.core.transport.Config;
import com.huawei.openstack4j.model.common.Identifier;
import com.huawei.openstack4j.model.compute.Server;

public class Demo {
public static void main(String[] args) {

// Set the authentication parameters.
String authUrl = "https://iam.example.com/v3"; //endpoint Url
String user = "replace-with-your-username"; // Username
String password = "replace-with-your-password"; //Password
String projectId = "replace-with-your-projectId"; //Project ID
String userDomainId = "replace-with-your-domainId"; //Domain ID

// Initialize the client.
OSClientV3 os = OSFactory.builderV3()
.endpoint(authUrl)
.credentials(user, password, Identifier.byId(userDomainId))
.scopeToProject(Identifier.byId(projectId)).authenticate();

// Set query parameters.
Map filter = new HashMap();
// Put the parameters that need to be entered into the filter.
filter.put("limit", "3");
 
// Invoke the interface for querying the VM List.
List<!--? extends Server--> serverList = os.compute().servers().list(filter);
if(serverList.size() > 0) {
System.out.println("get serverList success, size = " + serverList.size());
for (Server server : serverList) {
System.out.println(server);
}
} else {
System.out.println("no server exists.");
}
}
}
  • example in the preceding code is in the Region.Cloud platform domain name format. For details about the parameters, see here.

Typical Process for Creating an ECS

You can follow the steps below to create an ECS, bind an elastic IP address (EIP) to the ECS, and attach disks to the ECS.

Procedure

Description

1

Connect IAM (Keystone) to AUTH.

2

Obtain your image ID.

  • Option 1: Use the public images (recommended).
  • Option 2: Use OpenStack Glance v2 to download your images. The cloud platform supports VHD, ZVHD, QCOW2, and VMDK images.

3

Make a call to the VPC API to obtain the network ID. For details, see Creating a VPC and Subnet.

4

Create an ECS. For details, see Creating an ECS.

5

(Optional) Bind an EIP to the ECS. For details, see Binding an EIP to an ECS.

6

(Optional) Attach disks to ECS. For details, see Attaching a Volume to the ECS.