Getting Started with Java SDK
Welcome to use HUAWEI CLOUD developer tools (Java SDK). Java SDK allows you to easily access cloud services using codes.
This tutorial describes how to install and use Java SDK and provides examples to help you quickly get started.
The supported Java SDK is developed based on OpenStack4j.
Supported Cloud Services
The Java SDK supports the following cloud services:
- Identity and Access Management (IAM)
- Image Management Service (IMS)
- Virtual Private Cloud (VPC)
- Elastic Cloud Server (ECS)
- Elastic Volume Service (EVS)
- Auto Scaling (AS)
- Cloud Eye (CES)
- Domain Name Service (DNS)
- Elastic Load Balancing (ELB)
- Volume Backup Service (VBS)
- Cloud Trace Service (CTS)
- Key Management System (KMS)
- Anti-DDoS
- Distributed Message Service (DMS)
- MapReduce Service (MRS)
- Content Delivery Network (CDN)
- Tag Management Service (TMS)
- Enterprise Management (EPS)
- Relational Database Service (RDS)
Prerequisites
- You have obtained a cloud platform account and provisioned all required services.
- You have installed JDK. The Java SDK is applicable to JDK1.8 and later versions. You are advised to use JDK1.8.
SDK Acquisition and Installation
Add the following Maven dependency to the pom.xml file to install the Java SDK:
<dependency> <groupId>com.huawei</groupId> <artifactId>openstack4j</artifactId> <version>1.0.12</version> </dependency>
For the latest version supported by Java SDK, see here.
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. For details about the parameters, see Table 1.
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";//endpointUrl
String user = "replace-with-your-username";//username
String password = "replace-with-your-password";//user password
String projectId = "replace-with-your-projectId";//project ID
String userDomainId = "replace-with-your-domainId";//account 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<String, String> filter = new HashMap<String, String>();
// 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.");
}
}
}
|
Parameter |
Description |
Example Value |
|---|---|---|
|
authUrl |
Specifies the endpoint of the IAM service. example in https://iam.example.com/v3 indicates Region.Cloud platform domain name. For details about the parameter, see here. |
https://iam.cn-north-1.myhuaweicloud.com/v3 |
|
user |
Specifies the IAM username. For details about how to obtain the username, see How Do I Obtain the IAM Username, Account ID, and Project ID?. |
N/A |
|
password |
Specifies the IAM user password. |
N/A |
|
projectId |
Specifies the project ID. For details about how to obtain the project ID, see How Do I Obtain the IAM Username, Account ID, and Project ID?. |
None |
|
userDomainId |
Specifies the account ID. For details about how to obtain the account ID, see How Do I Obtain the IAM Username, Account ID, and Project ID?. |
None |
Last Article: Java
Next Article: Using the Java SDK
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.