SDK Overview

ZStack provides Java SDK and Python SDK supports. To use the corresponding features, call ZStack APIs by using the SDK method.

To call ZStack APIs by using the SDK method, pay attention to the following issues:
  • Java SDK compatible version: Java 8.
  • Python SDK compatible version: Python 2.7.
  • Format of sdk dataformat: YY-MM-DD hh:mm:ss, such as 2019-03-08 19:23:00.

Environment Preparation

Prepare the following software tools:
  • Java Development Tool (IntelliJ IDEA)

    According to your own usage, download and install the appropriate Java development tool, such as IntelliJ IDEA and Eclipse, and complete the initialization job. This topic uses IntelliJ IDEA as an example.

  • Java JDK Tool

    Install the Java JDK tool in advance. We recommend that you use Java JDK 8.

  • ZStack Java SDK JAR Dependency Package
    Before you use the ZStack SDK, obtain the Java SDK JAR dependency package in advance. The package is stored in ZStack Installation Package.
    • ZStack Installation Package
      • File name: ZStack-installer-3.10.0.bin
      • Download address: Click here.
    • Java SDK Dependency Package
      • File name: sdk-3.10.0.jar.
      • Storage path: /usr/local/zstack/apache-tomcat/webapps/zstack/WEB-INF/lib/sdk-3.10.0.jar.
  • Third-Party JAR Depended by SDK
    The detailed information about the JAR dependency package is as follows:
    <dependencies>
        <dependency>
            <groupId>org.zstack</groupId>
            <artifactId>sdk</artifactId>
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.5.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.1</version>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.3</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.9</version>
        </dependency>
    </dependencies>

SDK Use

After the preparations are completed, refer to the following procedure to use ZStack SDK:
  1. Create a JAVA Maven project on IDEA, as shown in JAVA Maven Project.
    Figure 1. JAVA Maven Project


  2. Add the SDK JAR dependency package to the External Libraries project, and add the third-party package to the pom.xml file, as shown in Add Dependency Package.
    Figure 2. Add Dependency Package


  3. Refer to SDK samples in ZStack API Reference to call APIs.

SDK Usage Sample

This topic provides an example of how to query a VM instance to describe the usage of ZStack SDK. For information about the method, see the following codes:
package org.zstack;
 
import org.zstack.sdk.*;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.io.UnsupportedEncodingException;
 
/**
 * Display how to use ZStack Java SDK to check a VM list.*/
public class ZStackSDKDemo {
    public static void main(String[] args) {
        String zstackServerHostname = "Enter the IP address of ZStack management node.";
        String accountName = "Enter ZStack account.";
        String password = "Enter ZStack account password.";
 
        ZSClient.configure(
                new ZSConfig.Builder()
                        .setHostname(zstackServerHostname)
                        .setPort(8080)
                        .setContextPath("zstack")
                        .build()
        );
 
        String sessionId = getSessionByLoginAccount(accountName, password);
 
        QueryVmInstanceAction action = new QueryVmInstanceAction();
        action.sessionId = sessionId;
        QueryVmInstanceAction.Result result = action.call();
        result.throwExceptionIfError();
 
        List<VmInstanceInventory> vmList = result.value.getInventories();
        System.out.println(String.format("QueryVmInstanceAction action succeeded, and %s VM instances are detected.",
                vmList != null ? vmList.size() : 0));
    }
 
    private static String getSessionByLoginAccount(String accountName, String password) {
        LogInByAccountAction action = new LogInByAccountAction();
        action.accountName = accountName;
        action.password = encryptToSHA512(password);
 
        LogInByAccountAction.Result result = action.call();
        result.throwExceptionIfError();
        System.out.println("Login succeeded.");
        return result.value.getInventory().getUuid();
    }
 
    private static String encryptToSHA512(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-512");
            md.reset();
            md.update(input.getBytes("utf8"));
            BigInteger bigInteger = new BigInteger(1, md.digest());
            return String.format("%0128x", bigInteger);
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
 
}
Specifically, pay attention to the follow aspects:
  1. In the following the codes, enter the correct parameters, namely the IP address of ZStack management node, ZStack account, and ZStack account password, to the corresponding locations.
    public static void main(String[] args) {
    String zstackServerHostname = "Enter the IP address of ZStack management node.";
    String accountName = "Enter ZStack account.";
    String password = "Enter ZStack accnount password.";
    ZSClient.configure(
             new ZSConfig.Builder()
                     .setHostname(zstackServerHostname)
                     .setPort(8080)
                     .setContextPath("zstack")
                     .build()
    );
  2. The following codes are the SDK information of APIs that you call. When you call different APIs, enter the corresponding SDK information. For more information, see the API Reference.
    QueryVmInstanceAction action = new QueryVmInstanceAction();
     action.sessionId = sessionId;
     QueryVmInstanceAction.Result result = action.call();
     result.throwExceptionIfError();
  3. After you completed encoding, compile and execute the codes.
Download

Already filled the basic info? Click here.

Enter at least 2 characters.
Invalid mobile number.
Enter at least 4 characters.
Invalid email address.
Wrong code. Try again. Send Code Resend Code (60s)

The download link will be sent to your email. Make sure the address you provided is valid and correct.

Download

Not filled the basic info yet? Click here.

Invalid email address or mobile number.

Email Us

contact@zstack.io
ZStack certification training consulting
Enter at least 2 characters.
Invalid mobile number.
Enter at least 4 characters.
Invalid email address.
Wrong code. Try again. Send Code Resend Code (60s)

Email Us

contact@zstack.io

The download link is sent to your email address.

If you don't see it, check your spam folder, subscription folder, or AD folder. After receiving the email, click the URL to download the documentation.

The download link is sent to your email address.

If you don't see it, check your spam folder, subscription folder, or AD folder.
Or click on the URL below. (For Internet Explorer, right-click the URL and save it.)

Thank you for using ZStack products and services.

Submit successfully.

We'll connect soon.

Thank you for choosing ZStack products and services.

Back to Top