How to use SDK
Last Update: 2024/06/27
The following are examples of SDK using (taking runInstances and describeInstances for example).
Description of the examples: To apply for a VM and keep tracing the condition of the order until the VM is installed.
package chttl.cloud.hws; import java.util.List; import chttl.cloud.hws.auth.HwsBasicCredential; import chttl.cloud.hws.auth.HwsCredential; import chttl.cloud.hws.exception.HiCloudClientException; import chttl.cloud.hws.exception.HiCloudServerException; import chttl.cloud.hws.services.caas.VirtualMachineService; import chttl.cloud.hws.services.caas.model.DescribeInstancesRequest; import chttl.cloud.hws.services.caas.model.DescribeInstancesResponse; import chttl.cloud.hws.services.caas.model.RunInstancesRequest; import chttl.cloud.hws.services.caas.model.RunInstancesResponse; import chttl.cloud.hws.services.caas.model.VirtualMachineEntry; public class ExampleApp { public static void main(String[] args) throws HiCloudClientException, HiCloudServerException { // To add ID String accessKey = "U0U0MU5UQXhNREF3TVRFek5qSTVPRFkxTURneU1UWT0"; String secretKey = "WWpJNU16a3pOV1JsWWpNeU5HVXdOMkkxTURNd1lUbG1OMlEwTXpSaFptST0"; HwsCredential credential = new HwsBasicCredential(accessKey, secretKey); // To add ID (using file Property) // HwsCredential credential = new HwsPropertyCredential(); // To build Client End HiCloudWebServiceClient client = new HiCloudWebServiceClient("hws.hicloud.hinet.net", credential); // If Proxy Server is used, it can be set up through the following methods. client.getConfiguration().setProxyServerIp("proxy.cht.com.tw"); client.getConfiguration().setProxyServerPort(8080); // To build VM Service Proxy and deal with parameter transmitting and data Verification VirtualMachineService serviceProxy = client.newServiceProxy(VirtualMachineService.class); // Step 1: To add VM // To add Run Instance Request. RunInstancesRequest request = new RunInstancesRequest(); request.setImageId("hi-cuium8up"); request.setCount(1); request.setInstanceName("Test HA"); request.setMonitoringEnabled(false); request.setInstanceType("HC1.S.WIN"); request.setRegionId("region-tw-1"); // To call for CaaS / CVPC API to build VM and send it back RunInstancesResponse runInstancesResult = serviceProxy.runInstances(request); // To show the result of Run Instances Response System.out.println(runInstancesResult); // Step 2: To inquire VM data // To add Describe Instance Request DescribeInstancesRequest describeRequest = new DescribeInstancesRequest(); // To inquire VM order condition according to the uuid sent back by RunInstance describeRequest.setOrderUuid(runInstancesResult.getOrderUuidList()); boolean isProvisionFinished = false; // To keep inquire for the VM data until it is installed while (!isProvisionFinished) { // To call for CaaS / CVPC API to inquire for VM and send back DescribeInstancesResponse DescribeInstancesResponse describeInstancesResult = serviceProxy.describeInstances(describeRequest); // To show VM order condition System.out.println(describeInstancesResult); List<VirtualMachineEntry> instances = describeInstancesResult.getInstanceList(); for (VirtualMachineEntry instance : instances) { if (!instance.getProvisionStatus().equals("provisioning")) { isProvisionFinished = true; } } try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
Directions
- To download hiCloud CaaS SDK
- Related information on hicloud CaaS / CVPC API
- VM: hws.hicloud.hinet.net
- Port: 443
- HTTP protocol: HTTPS
-
To acquire ID, please go to User Portal to get Access Key and Secret Key, for example:
Access Key: U0U0MU5UQXhNREF3TVRFek5qSTVPRFkxTURneU1UWT0
Secret key: WWpJNU16a3pOV1JsWWpNeU5HVXdOMkkxTURNd1lUbG1OMlEwTXpSaFptST0 - To register ID in the following two methods.
-
To annouce it in Java Code.
String accessKey = "U0U0MU5UQXhNREF3TVRFek5qSTVPRFkxTURneU1UWT0"; String secretKey = "WWpJNU16a3pOV1JsWWpNeU5HVXdOMkkxTURNd1lUbG1OMlEwTXpSaFptST0"; HwsCredential credential = new HwsBasicCredential(accessKey, secretKey);
-
To announce the ID in the category of HwsPropertyCredential.
accessKey = U0U0MU5UQXhNREF3TVRFek5qSTVPRFkxTURneU1UWT0 secretKey = WWpJNU16a3pOV1JsWWpNeU5HVXdOMkkxTURNd1lUbG1OMlEwTXpSaFptST0
To announce the ID in the category of HwsPropertyCredential.HwsCredential credential = new HwsPropertyCredential();
- To build the information on the server.
-
To build HiCloud Service Client
HiCloudWebServiceClient client = new HiCloudWebServiceClient("hws.hicloud.hinet.net", credential);
-
If Proxy Server is used, it can be set up in the following ways.
client.getConfiguration().setProxyServerIp("proxy.cht.com.tw"); client.getConfiguration().setProxyServerPort(8080);
- To build hiCloud Caas and transmit designated parameter to hicloud CaaS / CVPC API, the following service can be made:
-
VM: For related methods and parameters, please see Java developer center > hicloud Caas > VM
// To build Client End HiCloudWebServiceClient client = new HiCloudWebServiceClient("hws.hicloud.hinet.net", credential); // To build Virtual Machine Service Proxy VirtualMachineService serviceProxy = client.newServiceProxy(VirtualMachineService.class); // To add Run Instance Request RunInstancesRequest request = new RunInstancesRequest(); request.setImageId("hi-cuium8up"); request.setCount(1); request.setInstanceName("Test HA"); request.setMonitoringEnabled(false); request.setInstanceType("HC1.S.WIN"); // To call for CaaS / CVPC API to build VM and send back RunInstancesResponse RunInstancesResponse runInstancesResult = serviceProxy.runInstances(request);
-
Server Load Balance: For related methods and parameters, please see Java developer center > hicloud CaaS > load balance
// To build Client End HiCloudWebServiceClient client = new HiCloudWebServiceClient("hws.hicloud.hinet.net", credential); // To build proxy for server load balancer ServerLoadBalancerService serviceProxy = client.newServiceProxy(ServerLoadBalancerService.class); CreateLoadBalancerPolicyRequest request = new CreateLoadBalancerPolicyRequest(); // To build server load balancer group for VM List<String> instanceIds = new ArrayList<String>(); instanceIds.add("BV55010001000A"); instanceIds.add("BV55010001000B"); request.setInstanceId(instanceIds); // To set up SLB service Port List<Integer> slbPorts = new ArrayList<Integer>(); slbPorts.add(Integer.valueOf(80)); slbPorts.add(Integer.valueOf(443)); request.setPort(slbPorts); // To build SLB Policy CreateLoadBalancerPolicyResponse slbResult = serviceProxy.createLoadBalancerPolicy(request);
-
VM Monitor: For related methods and parameters, please see Java developer center > hicloud CaaS > VM monitoring
// To build Client End HiCloudWebServiceClient client = new HiCloudWebServiceClient("hws.hicloud.hinet.net", credential); // To build proxy for Cloud Watch MonitorService serviceProxy = client.newServiceProxy(MonitorService.class); // To add GetInstancesStats Request GetInstancesStatsRequest request =new GetInstancesStatsRequest(); // To set up VM Instance ID for inquiry List<String> instanceIds = new ArrayList<String>(); instanceIds.add("BV55010001000A"); instanceIds.add("BV55010001000B"); request.setInstanceId(instanceIds); // To acquire VM monitoring data GetInstancesStatsResponse instancesStatsResult = serviceProxy.getInstancesStats(request);
- The exceptions in hicloud CaaS / CVPC API would be defined by all the methods in HiCloud SDK. They are HiCloudServerExceptionand HiCloudClientException respectively.
-
- For related error codes, please see Error Codes.
- HiCloudClientException reveals all the exceptions happened at Client End during execution.