Java SDK call examples

Updated at:

Use an SDK to call an OpenAPI operation and create an ACK managed cluster.

Step 1: Review the OpenAPI documentation

Before you call an OpenAPI operation, read Create an ACK managed cluster by calling an API operation to learn about the parameters and permissions required for the operation. For more parameters, see API overview.

Step 2: Create a Resource Access Management (RAM) user and grant permissions

Call this operation by using an Alibaba Cloud account, a RAM user, or a RAM role. For information about the differences between these identities, see Identity.

Important

An Alibaba Cloud account has access permissions for all API operations. (Recommended) Create and use a RAM user for API calls or routine O&M.

  1. Create a RAM user.

    1. Use your Alibaba Cloud account to log on to the RAM console.

    2. In the left-side navigation pane, choose Credential Management > User.

    3. On the User page, click Create User.

    4. On the Create User page, set Logon Name and Display Name, and set Access Method to Console Password Logon.

    5. Click OK.

      After the RAM user is created, record the logon name and password. When you call an OpenAPI operation, use this RAM user to log on to the Alibaba Cloud OpenAPI Developer Portal.

  2. Grant the AliyunCSFullAccess permission to the RAM user. For instructions, see Manage RAM user permissions.

    AliyunCSFullAccess: Grants permissions to manage Container Service for Kubernetes.

    AliyunCSReadOnlyAccess: Grants read-only access to Container Service for Kubernetes.

    If you need to create a custom policy, see RAM authorization.

  3. Go to the details page of the RAM user. On the Authentication tab, click Create AccessKey. For instructions, see Create an AccessKey.

Step 3: Call an OpenAPI operation

This example uses a RAM user to call CreateCluster and create an ACK managed Pro cluster. It uses the Java SDK. SDKs for other programming languages work similarly. For more information, see Container Service for Kubernetes (ACK) SDK. Other calling methods can also be selected based on your business requirements. For details, see Calling methods.

Important

CreateCluster provisions a real ACK managed Pro cluster, which incurs a cluster management fee and charges for the resources that the cluster creates, such as worker nodes, elastic IP addresses (EIPs), and Server Load Balancer (SLB) instances. Charges continue to accrue until the cluster is deleted. Delete the cluster after you complete this example. For more information, see Billing overview.

Configure environment variables

Before you call the operation, configure environment variables to obtain access credentials.

Download the SDK sample code

  1. Go to CreateCluster.

  2. In the Parameters section on the left side, specify the required parameters, and then click Initiate Call.

    • name—Example value: test

    • region_id—Example value: cn-beijing

    • cluster_type—Example value: ManagedKubernetes

    • cluster_spec—Example value: ack.pro.small

    • kubernetes_version—Example value: 1.30.1-aliyun.1

    • vpcid—Example value: vpc-2zedl8cyb7tnkaux1****

    • container_cidr—Example value: 10.0.0.0/8

    • service_cidr—Example value: 172.21.0.0/20

    • vswitch_ids—Example value: vsw-2ze7hfp0ah8rk1nz9****

  3. On the SDK Sample tab on the right side, select SDK V2.0 and an SDK language, and then click Download Project. After the download is complete, decompress the package locally.

    (Recommended) Use SDK V2.0. For details, see Differences between SDK V1.0 and SDK V2.0.

    Request parameters such as name, region_id, cluster_type, cluster_spec, and kubernetes_version are specified on the Parameters tab on the left. The code pane on the right then generates SDK sample code in the corresponding language in real time.

Run the SDK sample code

  1. Open IntelliJ IDEA, click File->Open, select the decompressed project folder, and wait for Maven to install the dependencies automatically.

  2. Run the sample code.

    Double-click Sample to open it. After you confirm that no errors are reported, run the code.

  3. View the result.

    In the console at the bottom of the page, search for statusCode. If "statusCode":202 is returned, the call is successful and cluster creation starts. In the ACK console, view the new cluster on the Clusters page.

    {
      "headers": {
        "content-type": "application/json;charset=utf-8",
        "access-control-expose-headers": "*",
        "x-acs-trace-id": "30c74bc83a7fae0a081e5f5846be703e"
      },
      "statusCode": 202,
      "body": {
        "clusterId": "c6104ee21d6304ef8..."
      }
    }