Help Center > > Developer Guide> Java> Getting Started

Getting Started

Updated at: Oct 09, 2019 GMT+08:00

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.

Prerequisites

  1. You have obtained a cloud platform account and provisioned all required services.
  2. 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.");
        }
    }
}
Table 1 Parameter description

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

projectId

Specifies the project ID. For details about how to obtain the project ID, see How Can I Obtain domain_name, project_name, and project_id?.

None

userDomainId

Specifies the account ID. For details about how to obtain the account ID, see How Can I Obtain domain_name, project_name, and project_id?.

None

Did you find this page helpful?

Submit successfully!

Thank you for your feedback. Your feedback helps make our documentation better.

Failed to submit the feedback. Please try again later.

Which of the following issues have you encountered?







Please complete at least one feedback item.

Content most length 200 character

Content is empty.

OK Cancel