This topic describes how to check the validity period of a service instance by calling the CheckOutLicense operation.
Limits
One of the following requirements must be met:
A Compute Nest service is configured for custom sales.
A Compute Nest service is listed on Alibaba Cloud Marketplace.
Verification principle
For resources created in Compute Nest, Compute Nest adds tags such as service instance ID (ServiceInstanceId) and service ID (ServiceId) to the resources. CheckOutLicense is used to determine the service instance to which a resource belongs based on the resource tag.
Service providers need to obtain the
ServiceIdvalue of the corresponding service instance from the Compute Nest console.When you call the
CheckOutLicenseoperation, theServiceIdparameter is passed to Compute Nest. Compute Nest checks whether the content passed in matches the content in the resource.
Call example
Create an Elastic Compute Service (ECS) instance and call the CheckOutLicense operation based on the limits.
Obtain ECS region information.
Before you call the CheckOutLicense operation, obtain the ID of the region in which the ECS instance and its application are deployed. The obtained region ID is used in subsequent steps and properly kept by the service provider.
Access the following URL to obtain the region ID:
curl http://100.100.100.200/latest/meta-data/region-idSample response that contains the region ID:
cn-hangzhou
Obtain
ServiceIdfrom the console.
Sample request.
In this example, a call is made in the China (Hangzhou) region. Replace the region ID with the actual region ID.
# Replace the ServiceId value according to your actual situation. curl -H "Content-Type: application/json" -XPOST https://cn-hangzhou.axt.aliyun.com/computeNest/license/check_out_license -d '{"ServiceId":"service-8fff945fe6844906****"}'Sample response.
{ "code":200, "requestId":"6af1efb7-c59c-4cee-9094-e1e3bbefb639", "instanceId":"i-0jl957dfri612gxxxxxx", "result":{ "RequestId":"B22723B7-FC31-18F5-A33E-1AF4C82736AA", "ServiceInstanceId":"si-0f14037f30c14292****", "LicenseMetadata":"{\"TemplateName\":\"Custom_Image_Ecs\",\"SpecificationName\":\"\",\"CustomData\":\"xxxx\"}", "TrialType":"NotTrial", "Token":"58d4574bd0d967bb431cd8936b5e80c4", "ExpireTime":"2024-08-28T06:27:08Z", "ServiceId":"service-8fff945fe6844906****", "Components":"{\"package_version\":\"yuncode55xxxxxxxx\",\"SystemDiskSize\":\"40\",\"DataDiskSize\":\"100\"}" } }The following table describes the parameters.
Parameter
Description
Example
ServiceInstanceId
The ID of the service instance.
si-0f14037f30c14292****
ServiceId
The service ID.
service-8fff945fe6844906****
ExpireTime
The expiration time of the service instance.
2024-08-28T06:27:08Z
LicenseMetadata
The metadata.
This data needs to be defined in the custom sales configuration.
{\"TemplateName\":\"Custom_Image_Ecs\",\"SpecificationName\":\"\",\"CustomData\":\"xxxx\"}
Components
The additional billing item information from the Alibaba Cloud Marketplace.
{\"package_version\":\"yuncode55xxxxxxxx\",\"SystemDiskSize\":\"40\",\"DataDiskSize\":\"100\"}
Sample code
Python
import requests
import json
import hashlib
import time
from urllib.request import urlopen
def get_region_id():
"""Obtaim the region ID (such as cn-hangzhou) by using the Alibaba Cloud metadata service"""
try:
with urlopen(
"http://100.100.100.200/latest/meta-data/region-id",
timeout=2
) as response:
return response.read().decode().strip()
except Exception as e:
print(f"Failed to get region ID: {str(e)}", file=sys.stderr)
sys.exit(1)
def checkout_license():
# Dynamically get the region ID and build the URL
region_id = get_region_id()
url = f"https://{region_id}.axt.aliyun.com/computeNest/license/check_out_license"
# Send POST request
try:
response = requests.post(
url,
json={
# Optional parameter
# "ServiceId": "service-ec9cbf77f9be443db938"
},
headers={"Content-Type": "application/json"}
)
print(f"Request URL: {url}")
print(f"Status Code: {response.status_code}")
print(f"Response: {response.text}")
except Exception as e:
print(f"Request Failed: {str(e)}", file=sys.stderr)
if __name__ == "__main__":
import sys
# Call the function
checkout_license()
Example.

Java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
public class CheckoutLicense {
public static void main(String[] args) {
try {
// === Dynamically obtian the region ID === #
String regionId = getRegionId();
System.out.println("Detected Region ID: " + regionId);
String checkoutLicenseString = "{}";
// Optional parameter
// String checkoutLicenseString = "{\"ServiceId\": \"service-ec9cbf77f9be443db938\"}";
// === Send a POST request === #
String urlStr = "https://" + regionId + ".axt.aliyun.com/computeNest/license/check_out_license";
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
try (OutputStream os = conn.getOutputStream()) {
byte[] input = checkoutLicenseString.getBytes("UTF-8");
os.write(input, 0, input.length);
}
// === Read the response === #
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder response = new StringBuilder();
String responseLine;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine);
}
conn.disconnect();
System.out.println("Response: " + response.toString());
System.out.println("Request URL: " + urlStr);
} catch (Exception e) {
e.printStackTrace();
}
}
// === Dynamically obtain the region ID (Alibaba Cloud metadata service) === #
private static String getRegionId() throws Exception {
String regionIdUrl = "http://100.100.100.200/latest/meta-data/region-id";
HttpURLConnection conn = (HttpURLConnection) new URL(regionIdUrl).openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(2000); // 2 seconds timeout
conn.setReadTimeout(2000);
try (BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream()))) {
return in.readLine().trim();
}
}
}
Example.
