ZStack Cloud provides Java SDK and Python SDK supports. To use the corresponding features, call ZStack Cloud APIs by using the SDK method.
sdk dataformat
: YY-MM-DD hh:mm:ss, such as 2019-03-08 19:23:00.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.
Install the Java JDK tool in advance. We recommend that you use Java JDK 8.
<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>
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);
}
}
}
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()
);
QueryVmInstanceAction action = new QueryVmInstanceAction();
action.sessionId = sessionId;
QueryVmInstanceAction.Result result = action.call();
result.throwExceptionIfError();
Email Us
contact@zstack.ioEmail Us
contact@zstack.ioThe 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.Thank you for using ZStack products and services.
Submit successfully.
We'll connect soon.Thank you for choosing ZStack products and services.