BCC instance
A BCC instance refers to an active cloud server instance on Baidu AI Cloud, serving as a management entity for advanced features like billing and permission control.
Create an instance (by specifying instance package specifications)
You can create BCC instances with the following code, including general-purpose BCC instances, storage-optimized BCC instances, compute-optimized BCC instances, big data BCC instances, GPU-based BCC instances and FPGA-based BCC instances:
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.Billing;
7import com.baidubce.services.bcc.model.CreateCdsModel;
8import com.baidubce.services.bcc.model.Reservation;
9import com.baidubce.services.bcc.model.StorageType;
10import com.baidubce.services.bcc.model.instance.CreateInstanceRequest;
11import com.baidubce.services.bcc.model.instance.CreateInstanceResponse;
12import java.util.ArrayList;
13import java.util.List;
14import static com.baidubce.services.bcc.model.instance.FpgaCardType.KU115;
15import static com.baidubce.services.bcc.model.instance.GpuCardType.P4;
16public class Main {
17 public static void main(String[] args) {
18 String endpoint = "bcc.bj.baidubce.com";
19 String ak = "ak";
20 String sk = "sk";
21 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
22 .withProtocol(Protocol.HTTP)
23 .withCredentials(new DefaultBceCredentials(ak, sk))
24 .withEndpoint(endpoint);
25 BccClient client = new BccClient(bccClientConfiguration);
26 CreateInstanceRequest instanceRequest = new CreateInstanceRequest();
27// Image ID
28 instanceRequest.setImageId("m-***");
29// Type of virtual machine instance to be created
30 instanceRequest.setSpec("bcc.g5.c1m4");
31// Size of system disk for virtual machine instance to be created, in GB. The default is 40 GB, with a range of [40, 2048]GB. Sizes exceeding 40 GB are charged according to cloud disk server prices. Note that the specified size of system disk must meet the minimum disk space requirement of the image used.
32 instanceRequest.setRootDiskSizeInGb(20);
33// Medium of the system disk for the virtual machine instance to be created. By default, an SSD cloud disk is used
34 instanceRequest.setRootDiskStorageType("enhanced_ssd_pl1");
35// Specify zone information. By default, it is empty and automatically selected by the system. The list of availability zones can be queried by calling the Query Availability Zone List API. The naming convention for zoneName is lowercase "country-region-availability zone sequence", such as "cn-bj-a" for Beijing availability zone A.
36 instanceRequest.setZoneName("cn-bj-a");
37// Name of the virtual machine. By default, no name is specified. If a name is specified: For batch creation, the name serves as a prefix. The backend will append a suffix, generated as follows: name{-sequence number}.
38 instanceRequest.setName("instance-***");
39 List<CreateCdsModel> createCdsList = new ArrayList<CreateCdsModel>();
40 createCdsList.add(new CreateCdsModel()
41 .withCdsSizeInGB(50)
42 .withStorageType("enhanced_ssd_pl1"));
43// List of CDS disks to be created
44 instanceRequest.setCreateCdsList(createCdsList);
45// Public network bandwidth, in Mbps. It must be an integer between 0 and 200. A value of 0 indicates that no public IP will be assigned, with a default of 0 Mbps
46 instanceRequest.setNetworkCapacityInMbps(10);
47// Count of virtual machine instances to be created (purchased) in batch, which must be an integer greater than 0 (optional). The default is 1
48 instanceRequest.setPurchaseCount(1);
49// Hostname of the virtual machine. By default, no hostname is specified. If a hostname is specified: For batch creation, the hostname serves as a prefix. The backend will append a suffix, generated as follows: name{-sequence number}.
50 instanceRequest.setHostname("hostn***");
51// Whether to automatically generate sequential suffixes for name and hostname (optional parameter) Yes: true No: false
52 instanceRequest.setAutoSeqSuffix(true);
53// Administrator password for the instance to be created: 8-16 characters; it must contain English letters, numbers and symbols (limited to !@#$%^*()) simultaneously.
54 instanceRequest.setAdminPass("***");
55// Order and billing-related parameters
56 instanceRequest.setBilling(new Billing()
57 .withPaymentTiming("Postpaid"));
58// Specify subnet information. If empty, it is the default subnet
59 instanceRequest.setSubnetId("sbn-***");
60// Specify security group information. If empty, it is the default security group
61 instanceRequest.setSecurityGroupId("g-***");
62// Whether the specified tag for the instance to be created needs to be associated with an existing tag key; default is false. Note: When the value is true, ensure that the tag key already exists
63 instanceRequest.setRelationTag(true);
64// Automatic snapshot policy ID
65 instanceRequest.setAspId("asp-***");
66// Specify IDs of deployment groups where the instance is located
67 instanceRequest.setDeployId("dset-***");
68 List<String> eniIds = new ArrrayList();
69 eniIds.add("eni-***");
70// Optional parameter. A list of short IDs for elastic network interface cards to be mounted. During batch creation, elastic network interface cards will be evenly distributed among instances based on the count provided. Ensure that the count of elastic network interface card IDs provided is an integer multiple of the count of instances and that the count of ENIs assigned to each instance does not exceed the maximum count that can be mounted by the instance. Elastic baremetal compute (EBC) instances are not currently supported. The maximum number of eniIds is 50,000.
71 instanceRequest.setEniIds(eniIds);
72 CreateInstanceResponse instance = client.createInstance(instanceRequest);
73 System.out.println(instance.getInstanceIds());
74 }
75}
- Creating a BCC request is an asynchronous process. A return code of 200 indicates that the order has been generated, and the progress of creating the BCC virtual machine can be monitored by checking the returned instance ID information.
- This API is used to create one or more identical virtual machine instances.
- Creating an instance requires real-name verification. Those who have not completed the verification can navigate to the Security Certification section in the Baidu Open Cloud Official Website Console to complete real-name certification.
- To create a postpaid instance, the account's cash balance combined with general coupons must exceed 100. For prepaid instances, the account's cash balance must at least match the cost of the instance.
- Batch creation is supported. If any instance fails during batch creation, all instances will be rolled back, resulting in no successful creations. If CDS is included in the creation, it will also be rolled back.
- By default, an instance can attach up to five cloud disk servers.
- When creating CDS disks and temporary data disks, the disk capacity must be an exact multiple of 5.
- Instance creation allows for the creation and addition of temporary data disks, but does not support creating or adding temporary data disks independently.
- Temporary data disks cannot be attached, detached, or deleted after creation.
- The maximum size of a temporary data disk for a general-purpose instance is capped at 500 GB.
- When specifying a subnet and security group during creation, both must be specified together or left unspecified. The chosen subnet and security group must belong to the same VPC. If neither is specified, the default subnet and security group will be used.
- Specify the public IP bandwidth during creation, with charges based on the selected bandwidth.
- The creation API is asynchronous. Instance status can be queried using the Query Instance Details API.
- Each instance is limited to purchasing a maximum of one temporary data disk.
- The default type for a temporary data disk in an instance is hp1.
- When creating a storage-optimized instance, you need to purchase a temporary data disk. Specify its size through the ephemeralDisks field. The default is an NVMe-type data disk, so you don't need to enter a value unless a specific size is required.
- For detailed instructions on creating requests, please refer to the BCC API documentation [Create Instance (by Specified Instance Package Specifications)](BCC/API Reference/Instance Related Interfaces/Create an instance (by specifying instance package specifications).md) )
Create an instance (deprecated; it is recommended to use the "Create Instance" API instead (by specifying instance package specifications))
You can create BCC instances with the following code, including dedicated instances, general-purpose type I instances, general-purpose type II instances, storage-optimized BCC instances and compute-optimized BCC instances;
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.Billing;
7import com.baidubce.services.bcc.model.CreateCdsModel;
8import com.baidubce.services.bcc.model.Reservation;
9import com.baidubce.services.bcc.model.StorageType;
10import com.baidubce.services.bcc.model.instance.CreateInstanceRequest;
11import com.baidubce.services.bcc.model.instance.CreateInstanceResponse;
12import java.util.ArrayList;
13import java.util.List;
14import static com.baidubce.services.bcc.model.instance.FpgaCardType.KU115;
15import static com.baidubce.services.bcc.model.instance.GpuCardType.P4;
16public class Main {
17 public static void main(String[] args) {
18 String endpoint = "bcc.bj.baidubce.com";
19 String ak = "ak";
20 String sk = "sk";
21 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
22 .withProtocol(Protocol.HTTP)
23 .withCredentials(new DefaultBceCredentials(ak, sk))
24 .withEndpoint(endpoint);
25 BccClient client = new BccClient(bccClientConfiguration);
26 CreateInstanceRequest instanceRequest = new CreateInstanceRequest();
27// Image ID
28 instanceRequest.setImageId("m-***");
29// Type of virtual machine instance to be created
30 instanceRequest.setInstanceType("N6");
31// CPU core count for the VM instance to be created
32 instanceRequest.setCpuCount(1);
33// Memory capacity for virtual machine instance to be created, in GB
34 instanceRequest.setMemoryCapacityInGB(4);
35// Size of system disk for virtual machine instance to be created, in GB. The default is 40 GB, with a range of [40, 2048]GB. Sizes exceeding 40 GB are charged according to cloud disk server prices. Note that the specified size of system disk must meet the minimum disk space requirement of the image used.
36 instanceRequest.setRootDiskSizeInGb(20);
37// Medium of the system disk for the virtual machine instance to be created. By default, an SSD cloud disk is used
38 instanceRequest.setRootDiskStorageType("enhanced_ssd_pl1");
39// Specify zone information. By default, it is empty and automatically selected by the system. The list of availability zones can be queried by calling the Query Availability Zone List API. The naming convention for zoneName is lowercase "country-region-availability zone sequence", such as "cn-bj-a" for Beijing availability zone A.
40 instanceRequest.setZoneName("cn-bj-a");
41// Name of the virtual machine. By default, no name is specified. If a name is specified: For batch creation, the name serves as a prefix. The backend will append a suffix, generated as follows: name{-sequence number}.
42 instanceRequest.setName("instance-***");
43 List<CreateCdsModel> createCdsList = new ArrayList<CreateCdsModel>();
44 createCdsList.add(new CreateCdsModel()
45 .withCdsSizeInGB(50)
46 .withStorageType("enhanced_ssd_pl1"));
47// List of CDS disks to be created
48 instanceRequest.setCreateCdsList(createCdsList);
49// Public network bandwidth, in Mbps. It must be an integer between 0 and 200. A value of 0 indicates that no public IP will be assigned, with a default of 0 Mbps
50 instanceRequest.setNetworkCapacityInMbps(10);
51// Count of virtual machine instances to be created (purchased) in batch, which must be an integer greater than 0 (optional). The default is 1
52 instanceRequest.setPurchaseCount(1);
53// Hostname of the virtual machine. By default, no hostname is specified. If a hostname is specified: For batch creation, the hostname serves as a prefix. The backend will append a suffix, generated as follows: name{-sequence number}.
54 instanceRequest.setHostname("hostn***");
55// Whether to automatically generate sequential suffixes for name and hostname (optional parameter) Yes: true No: false
56 instanceRequest.setAutoSeqSuffix(true);
57// Administrator password for the instance to be created: 8-16 characters; it must contain English letters, numbers and symbols (limited to !@#$%^*()) simultaneously.
58 instanceRequest.setAdminPass("***");
59// Order and billing-related parameters
60 instanceRequest.setBilling(new Billing()
61 .withPaymentTiming("Postpaid"));
62// Specify subnet information. If empty, it is the default subnet
63 instanceRequest.setSubnetId("sbn-***");
64// Specify security group information. If empty, it is the default security group
65 instanceRequest.setSecurityGroupId("g-***");
66// Whether the specified tag for the instance to be created needs to be associated with an existing tag key; default is false. Note: When the value is true, ensure that the tag key already exists
67 instanceRequest.setRelationTag(true);
68// Automatic snapshot policy ID
69 instanceRequest.setAspId("asp-***");
70// Specify IDs of deployment groups where the instance is located
71 instanceRequest.setDeployId("dset-***");
72 CreateInstanceResponse instance = client.createInstance(instanceRequest);
73 System.out.println(instance.getInstanceIds());
74 }
75}
- Creating a BCC request is an asynchronous process. A return code of 200 indicates that the order has been generated, and the progress of creating the BCC virtual machine can be monitored by checking the returned instance ID information.
- This API is used to create one or more identical virtual machine instances.
- Creating an instance requires real-name verification. Those who have not completed the verification can navigate to the Security Certification section in the Baidu Open Cloud Official Website Console to complete real-name certification.
- To create a postpaid instance, the account's cash balance combined with general coupons must exceed 100. For prepaid instances, the account's cash balance must at least match the cost of the instance.
- Batch creation is supported. If any instance fails during batch creation, all instances will be rolled back, resulting in no successful creations. If CDS is included in the creation, it will also be rolled back.
- By default, an instance can attach up to five cloud disk servers.
- When creating CDS disks and temporary data disks, the disk capacity must be an exact multiple of 5.
- Instance creation allows for the creation and addition of temporary data disks, but does not support creating or adding temporary data disks independently.
- Temporary data disks cannot be attached, detached, or deleted after creation.
- The maximum size of a temporary data disk for a general-purpose instance is capped at 500 GB.
- When specifying a subnet and security group during creation, both must be specified together or left unspecified. The chosen subnet and security group must belong to the same VPC. If neither is specified, the default subnet and security group will be used.
- Specify the public IP bandwidth during creation, with charges based on the selected bandwidth.
- The creation API is asynchronous. Instance status can be queried using the Query Instance Details API.
- This API enables you to designate a dedicated server for instance creation. Dedicated instances are not charged. These instances allow only the creation of temporary disks and the specification of disk types through ephemeralDisks.
- Each instance is limited to purchasing a maximum of one temporary data disk.
- The default type for a temporary data disk in an instance is hp1.
- Use the instanceType field to select the type of virtual machine (VM) you want to create. To see the currently supported VM types, refer to InstanceType. The parameters (instanceType, cpuCount, and memoryCapacityInGB) define the required machine type and configuration.
- When creating a storage-optimized instance, you need to purchase a temporary data disk. Specify its size through the ephemeralDisks field. The default is an NVMe-type data disk, so you don't need to enter a value unless a specific size is required.
- For detailed instructions on creating requests, please refer to the BCC API documentation [Create Instance](BCC/API Reference/Instance Related Interfaces/Create instance.md)
- You need to use specified configurations (including CPU, memory, local data disk, FPGA card type and a dedicated image) to create an FPGA BCC virtual machine. For details, please refer to the BCC API documentation [Optional Specification Configuration of FPGA BCC](BCC/API Reference/Appendix.md#FPGA-purpose BCC Optional Specification Configuration)
- You need to use specified configurations (including CPU, memory, local data disk and GPU card type) to create an GPU BCC virtual machine. For details, please refer to the BCC API documentation[Optional Specification Configuration of GPU BCC](BCC/API Reference/Appendix.md#GPU-purpose BCC Optional Specification Configuration).
Create a spot instance
You can be used to create BCC spot instances, including general-purpose type I and general-purpose type III instances with the following code:
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.Billing;
7import com.baidubce.services.bcc.model.CreateCdsModel;
8import com.baidubce.services.bcc.model.instance.CreateInstanceRequest;
9import com.baidubce.services.bcc.model.instance.CreateInstanceResponse;
10import java.util.ArrayList;
11import java.util.List;
12public class Main {
13 public static void main(String[] args) {
14 String endpoint = "bcc.bj.baidubce.com";
15 String ak = "ak";
16 String sk = "sk";
17 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
18 .withProtocol(Protocol.HTTP)
19 .withCredentials(new DefaultBceCredentials(ak, sk))
20 .withEndpoint(endpoint);
21 BccClient client = new BccClient(bccClientConfiguration);
22 CreateInstanceRequest instanceRequest = new CreateInstanceRequest();
23// Image ID
24 instanceRequest.setImageId("m-***");
25// Type of virtual machine instance to be created
26 instanceRequest.setSpec("bcc.g5.c1m4");
27// Spot instance bidding model; market price: "market" custom: "custom".
28 instanceRequest.setBidModel("market");
29// CPU core count for the VM instance to be created
30 instanceRequest.setCpuCount(1);
31// Memory capacity for virtual machine instance to be created, in GB
32 instanceRequest.setMemoryCapacityInGB(4);
33// Size of system disk for virtual machine instance to be created, in GB. The default is 40 GB, with a range of [40, 2048]GB. Sizes exceeding 40 GB are charged according to cloud disk server prices. Note that the specified size of system disk must meet the minimum disk space requirement of the image used.
34 instanceRequest.setRootDiskSizeInGb(20);
35// Medium of the system disk for the virtual machine instance to be created. By default, an SSD cloud disk is used
36 instanceRequest.setRootDiskStorageType("enhanced_ssd_pl1");
37// Specify zone information. By default, it is empty and automatically selected by the system. The list of availability zones can be queried by calling the Query Availability Zone List API. The naming convention for zoneName is lowercase "country-region-availability zone sequence", such as "cn-bj-a" for Beijing availability zone A.
38 instanceRequest.setZoneName("cn-bj-a");
39// Name of the virtual machine. By default, no name is specified. If a name is specified: For batch creation, the name serves as a prefix. The backend will append a suffix, generated as follows: name{-sequence number}.
40 instanceRequest.setName("instance-***");
41 List<CreateCdsModel> createCdsList = new ArrayList<CreateCdsModel>();
42 createCdsList.add(new CreateCdsModel()
43 .withCdsSizeInGB(50)
44 .withStorageType("enhanced_ssd_pl1"));
45// List of CDS disks to be created
46 instanceRequest.setCreateCdsList(createCdsList);
47// Public network bandwidth, in Mbps. It must be an integer between 0 and 200. A value of 0 indicates that no public IP will be assigned, with a default of 0 Mbps
48 instanceRequest.setNetworkCapacityInMbps(10);
49// Count of virtual machine instances to be created (purchased) in batch, which must be an integer greater than 0 (optional). The default is 1
50 instanceRequest.setPurchaseCount(1);
51// Hostname of the virtual machine. By default, no hostname is specified. If a hostname is specified: For batch creation, the hostname serves as a prefix. The backend will append a suffix, generated as follows: name{-sequence number}.
52 instanceRequest.setHostname("host***");
53// Whether to automatically generate sequential suffixes for name and hostname (optional parameter) Yes: true No: false
54 instanceRequest.setAutoSeqSuffix(true);
55// Administrator password for the instance to be created: 8-16 characters; it must contain English letters, numbers and symbols (limited to !@#$%^*()) simultaneously.
56 instanceRequest.setAdminPass("***");
57// Order and billing-related parameters
58 instanceRequest.setBilling(new Billing()
59 .withPaymentTiming("Postpaid"));
60// Specify subnet information. If empty, it is the default subnet
61 instanceRequest.setSubnetId("sbn-***");
62// Specify security group information. If empty, it is the default security group
63 instanceRequest.setSecurityGroupId("g-***");
64// Whether the specified tag for the instance to be created needs to be associated with an existing tag key; default is false. Note: When the value is true, ensure that the tag key already exists
65 instanceRequest.setRelationTag(true);
66// Automatic snapshot policy ID
67 instanceRequest.setAspId("asp-***");
68// Specify IDs of deployment groups where the instance is located
69 instanceRequest.setDeployId("dset-***");
70 CreateInstanceResponse instance = client.createBidInstance(instanceRequest);
71 System.out.println(instance.getInstanceIds());
72 }
73}
- Submitting a BCC creation request is an asynchronous process. A 200 response indicates that the order has been generated, and the creation progress of the BCC virtual machine can then be monitored by querying the returned spot instance ID.
- This API is designed to create one or more identical virtual machine spot instances.
- Creating a spot instance requires real-name authentication. Users who have not completed authentication can do so under the security certification tab in the Baidu Open Cloud Official Website Console.
- To create a postpaid spot instance, the account's cash balance combined with general vouchers must exceed 100. For prepaid spot instances, the account's cash balance must be at least equal to the instance cost.
- Batch creation is supported. If any instance fails during batch creation, all instances will be rolled back, resulting in no successful creations. If CDS is included in the creation, it will also be rolled back.
- By default, an instance can attach up to five cloud disk servers.
- When creating CDS disks and temporary data disks, the disk capacity must be an exact multiple of 5.
- The maximum size of a temporary data disk for a general-purpose instance is capped at 500 GB.
- When specifying a subnet and security group during creation, both must be specified together or left unspecified. The chosen subnet and security group must belong to the same VPC. If neither is specified, the default subnet and security group will be used.
- Specify the public IP bandwidth during creation, with charges based on the selected bandwidth.
- The creation API is asynchronous. Instance status can be queried using the }Query Instance Details API )
Query spot instance packages
You can query BCC virtual machine spot instance packages with the following code
1public static void listBidFlavor(BccClient bccClient) {
2 // Execute operation to query spot instance packages
3 ListBccBidFlavorResponse listBccBidFlavorResponse = bccClient.listBidFlavor();
4 for(ZoneResource zoneResource: listBccBidFlavorResponse.getZoneResources()) {
5 System.out.println(zoneResource.getZoneName());
6 }
7}
Query the market price of spot instances
You can query the market price of spot instances with the following code
1public static void getBidInstancePrice(BccClient bccClient) {
2 GetBidInstancePriceRequest getBidInstancePriceRequest = new GetBidInstancePriceRequest();
3 // Set spot instance type to be queried as general-purpose BCC type I
4 getBidInstancePriceRequest.setInstanceType(InstanceType.N1.name());
5 // Set spot instance type to be queried as general-purpose BCC type III
6 getBidInstancePriceRequest.setInstanceType(InstanceType.N3.name());
7 // Set count of CPU cores for spot instance to be queried
8 getBidInstancePriceRequest.setCpuCount(cpuCount);
9 // Set memory size in GB for spot instance to be queried
10 getBidInstancePriceRequest.setMemoryCapacityInGB(memoryCapacityInGB);
11 // Set system disk size for spot instance to be queried
12 getBidInstancePriceRequest.setRootDiskSizeInGb(rootDiskSizeInGb);
13 // Set system disk medium for spot instance to be queried
14 getBidInstancePriceRequest.setRootDiskStorageType(rootDiskStorageType);
15
16 List<CreateCdsModel> createCdsModelList = new ArrayList<CreateCdsModel>();
17 // Create a high-performance blank CDS data disk
18 createCdsModelList.add(new CreateCdsModel().withStorageType(StorageType.hp1).withCdsSizeInGB(5));
19 // Set list of CDS disks for spot instance to be queried
20 getBidInstancePriceRequest.setCreateCdsList(createCdsList);
21 // Set count of spot instances to be queried for purchase
22 getBidInstancePriceRequest.setPurchaseCount(purchaseCount);
23 // Execute operations to query market price of spot instances
24 GetBidInstancePriceResponse getBidInstancePriceResponse = bccClient.getBidInstancePrice(getBidInstancePriceRequest);
25
26 // Retrieve total price of spot instances to be queried
27 System.out.println(getBidInstancePriceResponse.getMoney());
28 // Retrieve count of spot instances to be queried for purchase
29 System.out.println(getBidInstancePriceResponse.getCount());
30 // Retrieve unit price of spot instances to be queried
31 System.out.println(getBidInstancePriceResponse.getPerMoney());
32}
Cancel a spot instance order
You can cancel a spot instance order with the following code
1public static void cancelBidOrder(BccClient bccClient) {
2 CancelBidOrderRequest cancelBidOrderRequest = new CancelBidOrderRequest();
3 // Set order ID of spot instance to be canceled
4 cancelBidOrderRequest.setOrderId();
5 // Execute operations to cancel spot instance orders
6 CancelBidOrderResponse cancelBidOrderResponse = client.cancelBidOrder(cancelBidOrderRequest);
7 cancelBidOrderResponse.getOrderId();
8}
Query instance list
You can query the list of BCC virtual machine instances with the following code, supporting filtering by intranet IP, dedicated server ID and availability zone name:
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.model.InstanceModel;
5import com.baidubce.services.bcc.model.instance.ListInstancesRequest;
6import com.baidubce.services.bcc.model.instance.ListInstancesResponse;
7public class ListInstances {
8 public static void main(String[] args) {
9 // Set your AK, SK and the endpoint to be accessed
10 String endpoint = "bcc.bj.baidubce.com";
11 String ak = "ak";
12 String sk = "sk";
13 // Set default configuration
14 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration().withProtocol(Protocol.HTTP).withCredentials(new DefaultBceCredentials(ak, sk)).withEndpoint(endpoint);
15 // Create a BCC client
16 BccClient client = new BccClient(bccClientConfiguration);
17 ListInstancesRequest listInstancesRequest = new ListInstancesRequest();
18 // Starting position of batch list query, which is a system-generated string
19 listInstancesRequest.setMarker("i-***");
20 // Maximum number of items included per page, maximum: 1,000, default: 1,000
21 listInstancesRequest.setMaxKeys(1);
22 // Intranet IP
23 listInstancesRequest.setInternalIp("192.168.***.***");
24 // Dedicated server ID
25 listInstancesRequest.setDedicatedHostId("d-***");
26 // Availability zone information
27 listInstancesRequest.setZoneName("cn-bj-d");
28 // Multiple tags, separated by commas, format: tagKey:tagValue or tagKey
29 listInstancesRequest.setKeypairId("***");
30 ListInstancesResponse listInstancesResponse = client.listInstances(listInstancesRequest);
31 for (InstanceModel instanceModel : listInstancesResponse.getInstances()) {
32 System.out.println(instanceModel);
33 }
34 }
35}
Query the list of BCC instances eligible for no charge for stopped instances
You can query a list of instances in terms of no charge for stopped instances with the following code, supporting filtering by intranet IP, key pair ID and availability zone name:
1public static void getInstanceNoChargeList(BccClient bccClient) {
2 ListGetInstanceNoChargeRequest listGetInstanceNoChargeRequest = listGetInstanceNoChargeRequest();
3 // Set pagination flag
4 listGetInstanceNoChargeRequest.setMarker(instanceId);
5 // Set size of data returned per page, defaulting to 1,000, maximum 1,000
6 listGetInstanceNoChargeRequest.setMaxKeys(maxKeys);
7 // Query by internalIp
8 listGetInstanceNoChargeRequest.setInternalIp(internalIp);
9 // Query by keypairId
10 listGetInstanceNoChargeRequest.setKeypairId(keypairId);
11 // Query by availability zone name
12 listGetInstanceNoChargeRequest.setZoneName(zoneName);
13 // Execute the API to query non-chargeable instances that can be shut down
14 ListInstancesResponse response = bccClient.getInstanceNoChargeList(listGetInstanceNoChargeRequest);
15 for(InstanceModel instanceModel: response.getInstances()){
16 System.out.println(instanceModel.getId());
17 }
18}
Query instance details
You can query the detailed information of the specified BCC virtual machine with the following code:
1public static void getInstanceDetail(BccClient bccClient) {
2 // Query detailed information on a specified BCC virtual machine
3 InstanceModel instanceModel = bccClient.getInstance(instanceId).getInstance();
4 System.out.println(instanceModel.getName());
5}
Query the instance list by the instance ID
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.model.instance.ListInstanceByIdsRequest;
5import com.baidubce.services.bcc.model.instance.ListInstancesResponse;
6import java.util.Arrays;
7public class Main {
8 public static void main(String[] args) {
9 // Set your AK, SK and the endpoint to be accessed
10 String endpoint = "bcc.bj.baidubce.com";
11 String ak = "ak";
12 String sk = "sk";
13 // Set default configuration
14 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration().withProtocol(Protocol.HTTP)
15 .withCredentials(new DefaultBceCredentials(ak, sk)).withEndpoint(endpoint);
16 // Create a BCC client
17 BccClient client = new BccClient(bccClientConfiguration);
18 // Query instance list by instance ID
19 ListInstanceByIdsRequest request = new ListInstanceByIdsRequest()
20 .withInstanceIds(Arrays.asList("i-sZcdKAZR", "i-CH47zOoW"));
21 ListInstancesResponse response = client.listInstanceByIds(request);
22 System.out.println(response);
23 }
24}
Start an instance
You can start a specified BCC virtual machine instance with the following code:
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4public class StartInstance {
5 public static void main(String[] args) {
6 // Set your AK, SK and the endpoint to be accessed
7 String endpoint = "http://bcc.bj.baidubce.com";
8 String ak = "ak";
9 String sk = "sk";
10 // Set default configuration
11 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
12 .withProtocol(Protocol.HTTP)
13 .withCredentials(new DefaultBceCredentials(ak, sk))
14 .withEndpoint(endpoint);
15 // Create a BCC client
16 BccClient client = new BccClient(bccClientConfiguration);
17 // Set BCC virtual machine ID
18 String instanceId = "i-***";
19 // Execute BCC modification operation
20 client.startInstance(instanceId);
21 }
22}
After a successful API call, instances enter the Starting status. Instance status must be Stopped for this API to return successfully; otherwise, a 409 error will be returned.
Stop an instance
You can stop a specified BCC virtual machine instance with the following code:
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.instance.StopInstanceRequest;
7public class StopInstance {
8 public static void main(String[] args) {
9 // Set your AK, SK and the endpoint to be accessed
10 String endpoint = "http://bcc.bj.baidubce.com";
11 String ak = "ak";
12 String sk = "sk";
13 // Set default configuration
14 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
15 .withProtocol(Protocol.HTTP)
16 .withCredentials(new DefaultBceCredentials(ak, sk))
17 .withEndpoint(endpoint);
18 // Create a BCC client
19 BccClient client = new BccClient(bccClientConfiguration);
20 StopInstanceRequest stopInstanceRequest = new StopInstanceRequest();
21 // Set BCC virtual machine ID
22 stopInstanceRequest.setInstanceId("i-***");
23 // Set whether to force stopping, which is equivalent to a power-off operation and may result in the loss of data not written to the disk in the instance OS
24 stopInstanceRequest.setForceStop(Boolean.FALSE);
25 // Set whether to charge for stopped instances. TRUE means no charge for stopped instances, FALSE means charge for stopped instances. Note: Only users in the allow list can enable no charge for stopped instances
26 stopInstanceRequest.setStopWithNoCharge(Boolean.FALSE);
27 // Execute operation to stop the BCC virtual machine
28 client.stopInstance(stopInstanceRequest);
29 }
30}
- Once the instance has been successfully stopped, the system will transition to the "Stopped" status in the background.
- This operation is only applicable to instances in the Running status; otherwise, a 409 error will occur.
- The instance supports forced stopping, similar to a power-off operation, which might lead to the loss of data that hasn't been saved to the disk in the instance's OS.
Stop instances in batch
You can stop a specified BCC virtual machine instance in batch with the following code:
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import java.util.Arrays;
7import com.baidubce.services.bcc.model.instance.BatchStopInstanceRequest;
8public class StopInstance {
9 public static void main(String[] args) {
10 // Set your AK, SK and the endpoint to be accessed
11 String endpoint = "http://bcc.bj.baidubce.com";
12 String ak = "ak";
13 String sk = "sk";
14 // Set default configuration
15 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
16 .withProtocol(Protocol.HTTP)
17 .withCredentials(new DefaultBceCredentials(ak, sk))
18 .withEndpoint(endpoint);
19 // Create a BCC client
20 BccClient client = new BccClient(bccClientConfiguration);
21 BatchStopInstanceRequest stopInstanceRequest = new BatchStopInstanceRequest();
22 // Set BCC virtual machine IDs, with no more than 100 elements in the list
23 stopInstanceRequest.setInstanceIds(Arrays.asList("i-KEmEve**","i-0nPl9W**"));
24 // Set whether to force stopping, which is equivalent to a power-off operation and may result in the loss of data not written to the disk in the instance OS
25 stopInstanceRequest.setForceStop(Boolean.FALSE);
26 // Set whether to charge for stopped instances. TRUE means no charge for stopped instances, FALSE means charge for stopped instances. Note: Only users in the allow list can enable no charge for stopped instances
27 stopInstanceRequest.setStopWithNoCharge(Boolean.FALSE);
28 // Execute operation to stop the BCC virtual machine
29 client.batchStopInstance(stopInstanceRequest);
30 }
31}
- Once the instance has been successfully stopped, the system will transition to the "Stopped" status in the background.
- This operation is only applicable to instances in the Running status; otherwise, a 409 error will occur.
- The instance supports forced stopping, similar to a power-off operation, which might lead to the loss of data that hasn't been saved to the disk in the instance's OS.
Reboot an instance
You can reboot a specified BCC virtual machine instance with the following code:
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.model.instance.RebootInstanceRequest;
5public class RebootInstance {
6 public static void main(String[] args) {
7 // Set your AK, SK and the endpoint to be accessed
8 String endpoint = "http://bcc.bj.baidubce.com";
9 String ak = "ak";
10 String sk = "sk";
11 // Set default configuration
12 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
13 .withProtocol(Protocol.HTTP)
14 .withCredentials(new DefaultBceCredentials(ak, sk))
15 .withEndpoint(endpoint);
16 // Create a BCC client
17 BccClient client = new BccClient(bccClientConfiguration);
18 // Set BCC virtual machine ID
19 String instanceId = "i-***";
20 // Execute BCC modification operation
21 client.rebootInstance(new RebootInstanceRequest().withInstanceId(instanceId));
22 }
23}
- This operation is only applicable to instances in the Running status; otherwise, a 409 error will occur.
- After a successful API call, the instances will enter the Starting status.
- It supports forced reboot, which is similar to a power failure reboot on a traditional server and may lead to the loss of data not saved to the disk from the instance's OS.
Modify the instance password
You can modify the administrator password of a specified virtual machine with the following code:
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.instance.ModifyInstancePasswordRequest;
7public class Main {
8 public static void main(String[] args) {
9 // Set your AK, SK and the endpoint to be accessed
10 String endpoint = "bcc.bj.baidubce.com";
11 String ak = "ak";
12 String sk = "sk";
13 // Set default configuration
14 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
15 .withProtocol(Protocol.HTTP)
16 .withCredentials(new DefaultBceCredentials(ak, sk))
17 .withEndpoint(endpoint);
18 // Create a BCC client
19 BccClient bccClient = new BccClient(bccClientConfiguration);
20 ModifyInstancePasswordRequest modifyInstancePasswordRequest = new ModifyInstancePasswordRequest();
21 // Set BCC virtual machine ID
22 modifyInstancePasswordRequest.setInstanceId("i-***");
23 // Set new passwords for BCC virtual machine
24 modifyInstancePasswordRequest.setAdminPass("***");
25
26 bccClient.modifyInstancePassword(modifyInstancePasswordRequest);
27 }
28}
This API can only be used for instances in the Running or Stopped state; otherwise, a 409 error will be returned.
Modify instance attributes
Modify instance attributes
You can modify the attributes of a specified virtual machine with the following code, currently only supporting modification of the name attribute of the BCC virtual machine:
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.model.instance.ModifyInstanceAttributesRequest;
5public class ModifyAttributeInstance {
6 public static void main(String[] args) {
7 // Set your AK, SK and the endpoint to be accessed
8 String endpoint = "http://bcc.bj.baidubce.com";
9 String ak = "ak";
10 String sk = "sk";
11 // Set default configuration
12 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
13 .withProtocol(Protocol.HTTP)
14 .withCredentials(new DefaultBceCredentials(ak, sk))
15 .withEndpoint(endpoint);
16 // Create a BCC client
17 BccClient client = new BccClient(bccClientConfiguration);
18 // Set BCC virtual machine ID
19 String instanceId = "i-***";
20 // Set description of virtual machine
21 String newName = "newInstanceName";
22 // Execute BCC modification operation
23 client.modifyInstanceAttributes(
24 new ModifyInstanceAttributesRequest()
25 .withInstanceId(instanceId)
26 .withName(newName)
27 );
28 }
29}
Modify instance description
You can modify the description information of a specific instance with the following code:
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.model.instance.ModifyInstanceDescRequest;
5public class ModifyDescInstance {
6 public static void main(String[] args) {
7 // Set your AK, SK and the endpoint to be accessed
8 String endpoint = "http://bcc.bj.baidubce.com";
9 String ak = "ak";
10 String sk = "sk";
11 // Set default configuration
12 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
13 .withProtocol(Protocol.HTTP)
14 .withCredentials(new DefaultBceCredentials(ak, sk))
15 .withEndpoint(endpoint);
16 // Create a BCC client
17 BccClient client = new BccClient(bccClientConfiguration);
18 // Set BCC virtual machine ID
19 String instanceId = "i-***";
20 // Set description of virtual machine
21 String newDesc = "modify desc";
22 // Execute BCC modification operation
23 client.modifyInstanceDesc(
24 new ModifyInstanceDescRequest()
25 .withInstanceId(instanceId)
26 .withDesc(newDesc)
27 );
28 }
29}
This API can only be called for instances that are either Running or Stopped. If not, a 409 error will be returned.
Reinstall an instance
You can rebuild a specified BCC virtual machine instance using an image with the following code:
1package com.baidubce.services.bcc;
2import com.baidubce.BceClientConfiguration;
3import com.baidubce.Protocol;
4import com.baidubce.auth.DefaultBceCredentials;
5import com.baidubce.services.bcc.model.instance.RebuildInstanceRequest;
6public class RebuildInstance {
7 public static void main(String[] args) {
8// Set your AK, SK and the endpoint to be accessed
9 String endpoint = "bcc.bj.baidubce.com";
10 String ak = "ak";
11 String sk = "sk";
12// Set default configuration
13 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration().withProtocol(Protocol.HTTP).withCredentials(new DefaultBceCredentials(ak, sk)).withEndpoint(endpoint);
14// Create a BCC client
15 BccClient client = new BccClient(bccClientConfiguration);
16 RebuildInstanceRequest req = new RebuildInstanceRequest();
17// Specified instance ID
18 req.setInstanceId("i-***");
19// Specified image ID
20 req.setImageId("m-***");
21// Machine password (8-16 characters; it must contain English letters, numbers and symbols (limited to !@#$%^*()) simultaneously). The password must be encrypted for transmission
22 req.setAdminPass("***");
23// Key pair ID to be bound to the instance being reinstalled. Either the adminPass or keypairId parameter must be specified
24 req.setKeypairId("k-***");
25
26 client.rebuildInstance(req);
27 }
28}
After reinstallation, any snapshots based on the original system disk will be automatically deleted, but custom images based on the original system disk will be retained.
Reinstall instance (batch reinstallation)
You can rebuild a specified BCC virtual machine instance in batch using an image with the following code:
1package com.baidubce.services.bcc;
2import com.baidubce.BceClientConfiguration;
3import com.baidubce.Protocol;
4import com.baidubce.auth.DefaultBceCredentials;
5import com.baidubce.services.bcc.model.instance.RebuildBatchInstanceRequest;
6import java.util.ArrayList;
7import java.util.List;
8public class BatchRebuildInstances {
9 public static void main(String[] args) {
10// Set your AK, SK and the endpoint to be accessed
11 String endpoint = "bcc.bj.baidubce.com";
12 String ak = "ak";
13 String sk = "sk";
14// Set default configuration
15 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration().withProtocol(Protocol.HTTP).withCredentials(new DefaultBceCredentials(ak, sk)).withEndpoint(endpoint);
16// Create a BCC client
17 BccClient client = new BccClient(bccClientConfiguration);
18 RebuildBatchInstanceRequest req = new RebuildBatchInstanceRequest();
19 List<String> instanceIds = new ArrayList<String>();
20 instanceIds.add("i-***");
21 instanceIds.add("i-***");
22// The list of specified instance IDs, maximum 100
23 req.setInstanceIds(instanceIds);
24// Specified image ID
25 req.setImageId("m-***");
26// Machine password (8-16 characters; it must contain English letters, numbers and symbols (limited to !@#$%^*()) simultaneously). The password must be encrypted for transmission
27 req.setAdminPass("***");
28// Key pair ID to be bound to the instance being reinstalled. Either the adminPass or keypairId parameter must be specified
29 req.setKeypairId("k-***");
30 client.rebuildBatchInstance(req);
31 }
32}
After reinstallation, any snapshots based on the original system disk will be automatically deleted, but custom images based on the original system disk will be retained.
Shift charge - convert to prepay
You can convert the billing mode of an instance from postpay to prepay with the following code
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.instance.ChangeToPrepaidRequest;
7import com.baidubce.services.bcc.model.instance.ChangeToPrepaidResponse;
8public class Main {
9 public static void main(String[] args) {
10 // Set your AK, SK and the endpoint to be accessed
11 String endpoint = "bcc.bj.baidubce.com";
12 String ak = "ak";
13 String sk = "sk";
14 // Set default configuration
15 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
16 .withProtocol(Protocol.HTTP)
17 .withCredentials(new DefaultBceCredentials(ak, sk))
18 .withEndpoint(endpoint);
19 // Create a BCC client
20 BccClient client = new BccClient(bccClientConfiguration);
21 ChangeToPrepaidRequest changeToPrepaidRequest = new ChangeToPrepaidRequest();
22 changeToPrepaidRequest.setInstanceId("i-lC39****"); // Virtual machine ID
23 changeToPrepaidRequest.setDuration(2); // Prepaid duration in months
24 ChangeToPrepaidResponse response = client.changeToPrepaid(changeToPrepaidRequest);
25 System.out.println(response.getOrderId()); // Order ID
26 }
27}
Release an instance
For BCC virtual machine instances with expired postpay (Postpaid) or prepay (Prepaid) billing modes, you can release them with the following code:
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6public class Main {
7 public static void main(String[] args) {
8 // Set your AK, SK and the endpoint to be accessed
9 String endpoint = "bcc.bj.baidubce.com";
10 String ak = "ak";
11 String sk = "sk";
12 // Set default configuration
13 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
14 .withProtocol(Protocol.HTTP)
15 .withCredentials(new DefaultBceCredentials(ak, sk))
16 .withEndpoint(endpoint);
17 // Create a BCC client
18 BccClient client = new BccClient(bccClientConfiguration);
19 client.releaseInstance("i-eRnq****"); // ID of the instance to be released
20 }
21}
- Release a single cloud server instance. Upon release, the physical resources used by the instance are reclaimed, and all associated data will be permanently deleted and cannot be restored.
- Only postpaid instances or expired prepaid instances are eligible for release.
- Once an instance is released, any associated CDS disks will be automatically detached, and snapshots based on these CDS disks will be retained.
- After the instance is released, snapshots of the original system disk will be deleted automatically, but custom images created from the original system disk will remain available.
Release instance (release via POST request)
- Release a single cloud server instance. Upon release, the physical resources used by the instance are reclaimed, and all associated data will be permanently deleted and cannot be restored.
- By default, only the instance and its system disks are released. Users can choose to release the attached EIPs and data disks as well, though this applies only to all attached disks together and not to individual ones.
1package com.baidubce.services.bcc;
2import com.baidubce.BceClientConfiguration;
3import com.baidubce.Protocol;
4import com.baidubce.auth.DefaultBceCredentials;
5import com.baidubce.services.bcc.model.instance.ReleaseInstanceByPostRequest;
6public class ReleaseInstance {
7 public static void main(String[] args) {
8 // Set your AK, SK and the endpoint to be accessed
9 String endpoint = "bcc.bj.baidubce.com";
10 String ak = "ak";
11 String sk = "sk";
12 // Set default configuration
13 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration().withProtocol(Protocol.HTTP).withCredentials(new DefaultBceCredentials(ak, sk)).withEndpoint(endpoint);
14 // Create a BCC client
15 BccClient client = new BccClient(bccClientConfiguration);
16 ReleaseInstanceByPostRequest req = new ReleaseInstanceByPostRequest();
17 // Instance ID
18 req.setInstanceId("i-***");
19 // Whether to release both EIPs and data disks currently attached on instance
20 req.setRelatedReleaseFlag(true);
21 // Whether to release cloud disk server snapshots
22 req.setDeleteCdsSnapshotFlag(true);
23 client.releaseInstanceByPost(req);
24 }
25 }
Release instances in batch
- Release cloud server instances in bulk. Once released, the physical resources allocated to the instances are reclaimed, and all associated data is permanently lost and unrecoverable.
- By default, only the instance and its system disks are released. Users can choose to release the attached EIPs and data disks as well, though this applies only to all attached disks together and not to individual ones.
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.instance.ReleaseMultipleInstancesRequest;
7import java.util.ArrayList;
8import java.util.List;
9public class Main {
10 public static void main(String[] args) {
11 // Set your AK, SK and the endpoint to be accessed
12 String endpoint = "bcc.bj.baidubce.com";
13 String ak = "ak";
14 String sk = "sk";
15 // Set default configuration
16 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
17 .withProtocol(Protocol.HTTP)
18 .withCredentials(new DefaultBceCredentials(ak, sk))
19 .withEndpoint(endpoint);
20 // Create a BCC client
21 BccClient bccClient = new BccClient(bccClientConfiguration);
22 ReleaseMultipleInstancesRequest releaseMultipleInstancesRequest = new ReleaseMultipleInstancesRequest();
23 // Set list of instance IDs to be released, with no more than 100 elements in the list
24 List<String> instanceIds = new ArrayList<String>();
25 instanceIds.add("i-***");
26 instanceIds.add("i-***");
27 releaseMultipleInstancesRequest.setInstanceIds(instanceIds);
28 // Whether to release both EIPs and data disks currently attached on instance
29 releaseMultipleInstancesRequest.setRelatedReleaseFlag(true);
30 // Whether to release cloud disk server snapshots
31 releaseMultipleInstancesRequest.setDeleteCdsSnapshotFlag(true);
32 // Whether to move the instance to the recycle bin upon instance release
33 releaseMultipleInstancesRequest.setBccRecycleFlag(true);
34 // Whether to delete the associated ENI upon instance release
35 releaseMultipleInstancesRequest.setDeleteRelatedEnisFlag(true);
36 bccClient.releaseMultipleInstanceByPost(releaseMultipleInstancesRequest);
37 }
38}
Release prepaid instance (POST)
Release a prepaid cloud server instance. Once released, the physical resources allocated to the instance are reclaimed, and all associated data is permanently deleted and unrecoverable.
By default, only the cloud server instance and its system disk are released during the release process. Users can opt to release the attached EIP and data disks as well. However, associated EIP and data disks must either be fully released or fully retained; partial release is not supported.
When choosing to release the EIP and data disks together, cloud disk server snapshots can also be included for release. If data disks are retained, cloud disk server snapshots cannot be included in the release process.
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.instance.ReleasePrepaidInstanceRequest;
7import java.util.ArrayList;
8import java.util.List;
9public class Main {
10 public static void main(String[] args) {
11 // Set your AK, SK and the endpoint to be accessed
12 String endpoint = "bcc.bj.baidubce.com";
13 String ak = "ak";
14 String sk = "sk";
15 // Set default configuration
16 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
17 .withProtocol(Protocol.HTTP)
18 .withCredentials(new DefaultBceCredentials(ak, sk))
19 .withEndpoint(endpoint);
20 // Create a BCC client
21 BccClient bccClient = new BccClient(bccClientConfiguration);
22 ReleasePrepaidInstanceRequest releasePrepaidInstanceRequest = new ReleasePrepaidInstanceRequest();
23 // Set list of instance IDs to be released
24 releasePrepaidInstanceRequest.setInstanceId("i-***");
25 // Whether to release the EIPs and data disks currently attached on the instance at the same time (the deleteCdsSnapshotFlag field will only be valid if this field is true. If this field is false, the value of the deleteCdsSnapshotFlag field is invalid)
26 releasePrepaidInstanceRequest.setRelatedReleaseFlag(true);
27 // Whether to release cloud disk server snapshots
28 releasePrepaidInstanceRequest.setDeleteCdsSnapshotFlag(true);
29 // Whether to delete the associated ENI upon instance release
30 releasePrepaidInstanceRequest.setDeleteRelatedEnisFlag(true);
31 bccClient.releasePrepaidInstanceByPost(releasePrepaidInstanceRequest);
32 }
33}
Instance scaling
Support scaling the CPU and memory of BCC virtual machines. Refer to the following code for details:
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.instance.ResizeInstanceRequest;
7public class Main {
8 public static void main(String[] args) {
9 // Set your AK, SK and the endpoint to be accessed
10 String endpoint = "bcc.bj.baidubce.com";
11 String ak = "ak";
12 String sk = "sk";
13 // Set default configuration
14 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
15 .withProtocol(Protocol.HTTP)
16 .withCredentials(new DefaultBceCredentials(ak, sk))
17 .withEndpoint(endpoint);
18 // Create a BCC client
19 BccClient client = new BccClient(bccClientConfiguration);
20 ResizeInstanceRequest resizeInstanceRequest = new ResizeInstanceRequest();
21 resizeInstanceRequest.setInstanceId("i-lC39****"); // ID of the instance to be resized
22 resizeInstanceRequest.setCpuCount(4); // Count of CPU cores
23 resizeInstanceRequest.setMemoryCapacityInGB(16); // Memory size in GB
24 resizeInstanceRequest.setEnableJumboFrame(false); // Set whether to enable Jumbo frames. true indicates that Jumbo frames are enabled, while false indicates that they are disabled. The default is empty. When empty: If resizing to a specification that does not support Jumbo frames, Jumbo frames will not be enabled; if resizing from a specification that does not support Jumbo frames to one that does, Jumbo frames will not be enabled; if resizing between specifications that support Jumbo frames, if Jumbo frames are enabled before resizing, they will remain enabled after resizing; if they are not enabled before resizing, they will remain disabled after resizing.
25 client.resizeInstance(resizeInstanceRequest);
26 }
27}
- When the instance is prepaid, downscaling operations are not allowed
- When the instance is postpaid, elastic scaling is allowed
- Scaling operations can only be performed when the instance is in the Running or Stopped status
- The instance will be rebooted once after scaling.
- As this is an asynchronous API, you can check the scaling status by the Query Instance Details API is completed
- For dedicated instances, you can specify CPU, memory and temporary disk size. For the temporary disk size of dedicated instances, Only expansion is supported, while contraction is not supported. For details, please refer to the API documentation [Instance Scaling](BCC/API Reference/Instance Related Interfaces/Instance scaling.md)
Instance scaling (by specified instance package specifications)
Scale a specified virtual machine. Scaling operations for CPU and memory are supported by updating the instance's package specifications.
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.instance.ResizeInstanceRequest;
7public class Main {
8 public static void main(String[] args) {
9 // Set your AK, SK and the endpoint to be accessed
10 String endpoint = "bcc.bj.baidubce.com";
11 String ak = "ak";
12 String sk = "sk";
13 // Set default configuration
14 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
15 .withProtocol(Protocol.HTTP)
16 .withCredentials(new DefaultBceCredentials(ak, sk))
17 .withEndpoint(endpoint);
18 // Create a BCC client
19 BccClient bccClient = new BccClient(bccClientConfiguration);
20 ResizeInstanceRequest resizeInstanceRequest = new ResizeInstanceRequest();
21 // Set BCC virtual machine ID
22 resizeInstanceRequest.setInstanceId("i-***");
23 // Set new package specifications
24 resizeInstanceRequest.setSpec("bcc.g5.c4m16");
25 // Set whether to enable Jumbo frames. true indicates that Jumbo frames are enabled, while false indicates that they are disabled. The default is empty. When empty: If resizing to a specification that does not support Jumbo frames, Jumbo frames will not be enabled; if resizing from a specification that does not support Jumbo frames to one that does, Jumbo frames will not be enabled; if resizing between specifications that support Jumbo frames, if Jumbo frames are enabled before resizing, they will remain enabled after resizing; if they are not enabled before resizing, they will remain disabled after resizing.
26 resizeInstanceRequest.setEnableJumboFrame(false);
27 bccClient.resizeInstanceBySpec(resizeInstanceRequest);
28 }
29}
- Scaling operations can only be performed when the instance is in the Running or Stopped status
- The instance will be rebooted once after scaling.
- As this is an asynchronous API, you can check the scaling status by the Query Instance Details API is completed
Add an instance to a security group
You can add a specified BCC virtual machine instance to a designated security group with the following code:
1public static void bindInstanceToSecurityGroup(BccClient bccClient) {
2 BindSecurityGroupRequest bindSecurityGroupRequest = new BindSecurityGroupRequest();
3 // Set security group ID
4 bindSecurityGroupRequest.setSecurityGroupId(securityGroupId);
5 // Set BCC virtual machine instance ID
6 bindSecurityGroupRequest.setInstanceId(instanceId);
7 // Execute operations to add the instance to the security group
8 bccClient.bindInstanceToSecurityGroup(bindSecurityGroupRequest);
9}
Each instance can be associated with a maximum of 10 security groups.
Remove an instance from a security group
You can remove a specified instance from a designated security group with the following code:
1public static void unbindInstanceFromSecurityGroup(BccClient bccClient) {
2 UnbindSecurityGroupRequest unbindSecurityGroupRequest = new UnbindSecurityGroupRequest();
3 // Set security group ID
4 unbindSecurityGroupRequest.setSecurityGroupId(securityGroupId);
5 // Set BCC virtual machine instance ID
6 unbindSecurityGroupRequest.setInstanceId(instanceId);
7 // Execute operations to remove the instance from the security group
8 bccClient.unbindInstanceFromSecurityGroup(unbindSecurityGroupRequest);
9}
- Each instance must be associated with at least one security group, with the default being the system's default security group.
- If an instance is part of only one security group, attempting to remove it from this group will cause a 403 error.
Query the VNC address of the instance
You can query the VNC URL of a specified BCC virtual machine instance with the following code:
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.model.instance.GetInstanceVncResponse;
5public class GetInstanceVnc {
6 public static void main(String[] args) {
7 // Set your AK, SK and the endpoint to be accessed
8 String endpoint = "bcc.bj.baidubce.com";
9 String ak = "ak";
10 String sk = "sk";
11 // Set default configuration
12 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration().withProtocol(Protocol.HTTP).withCredentials(new DefaultBceCredentials(ak, sk)).withEndpoint(endpoint);
13 // Create a BCC client
14 BccClient client = new BccClient(bccClientConfiguration);
15 // i-*** represents virtual machine instance ID
16 GetInstanceVncResponse response = client.getInstanceVnc("i-***");
17 System.out.println(response.getVncUrl());
18 }
19}
- The VNC address becomes invalid after one use
- The URL address is valid for 10 minutes
Renew an instance
Renewing a BCC virtual machine extends its expiration period. You can renew the disk with the following code:
1package com.baidubce.services.bcc;
2import com.baidubce.BceClientConfiguration;
3import com.baidubce.Protocol;
4import com.baidubce.auth.DefaultBceCredentials;
5import com.baidubce.services.bcc.model.Billing;
6import com.baidubce.services.bcc.model.Reservation;
7import com.baidubce.services.bcc.model.instance.PurchaseReservedInstanceRequeset;
8import com.baidubce.services.bcc.model.instance.PurchaseReservedInstanceResponse;
9public class PurchaseReservedInstance {
10 public static void main(String[] args) {
11 // Set your AK, SK and the endpoint to be accessed
12 String endpoint = "bcc.bj.baidubce.com";
13 String ak = "ak";
14 String sk = "sk";
15 // Set default configuration
16 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration().withProtocol(Protocol.HTTP).withCredentials(new DefaultBceCredentials(ak, sk)).withEndpoint(endpoint);
17 // Create a BCC client
18 BccClient client = new BccClient(bccClientConfiguration);
19 PurchaseReservedInstanceRequeset req = new PurchaseReservedInstanceRequeset();
20 // Virtual machine instance ID
21 req.setInstanceId("i-***");
22 // Idempotency Token, an ASCII string with a length not exceeding 64 bits
23 req.setClientToken("random-uuid");
24 // Order details
25 req.setBilling(new Billing()
26 // Renewal method; this API is for renewing prepaid instances.
27 .withPaymentTiming("Prepaid")
28 // Renewal period and unit, unit in months
29 .withReservation(new Reservation()
30 // Renewal period
31 .withReservationLength(1))
32 );
33 // Set the instance's associated renewal flag, defaulting to an empty string
34 req.setRelatedRenewFlag("CDS");
35
36 PurchaseReservedInstanceResponse res = client.purchaseReservedInstance(req);
37 System.out.println(res);
38 }
39}
- Renewal actions cannot be performed on a BCC virtual machine instance while it's being scaled.
- If the instance was suspended due to unpaid debt, it will automatically reboot after the renewal process is successfully completed.
- This API operates asynchronously.
- Dedicated instances do not support renewal functionality.
Bind a tag to an instance
You can bind tags to an instance with the following code
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.TagModel;
7import com.baidubce.services.bcc.model.instance.BindTagsRequest;
8import java.util.ArrayList;
9import java.util.List;
10public class Main {
11 public static void main(String[] args) {
12 // Set your AK, SK and the endpoint to be accessed
13 String endpoint = "bcc.bj.baidubce.com";
14 String ak = "ak";
15 String sk = "sk";
16 // Set default configuration
17 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
18 .withProtocol(Protocol.HTTP)
19 .withCredentials(new DefaultBceCredentials(ak, sk))
20 .withEndpoint(endpoint);
21 // Create a BCC client
22 BccClient bccClient = new BccClient(bccClientConfiguration);
23 BindTagsRequest bindTagsRequest = new BindTagsRequest();
24 // Set the tag to be bound
25 List<TagModel> changeTags = new ArrayList<TagModel>();
26 TagModel tagModel = new TagModel().withTagKey("Key***").withTagValue("Value***");
27 changeTags.add(tagModel);
28 bindTagsRequest.setChangeTags(changeTags);
29 // Set corresponding instance ID
30 bindTagsRequest.setInstanceId("i-***");
31
32 bccClient.bindInstanceToTags(bindTagsRequest);
33 }
34}
- Assign tags to an instance to facilitate classification and searching.
Unbind a tag from an instance
You can unbind tags from an instance with the following code
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.TagModel;
7import com.baidubce.services.bcc.model.instance.UnbindTagsRequest;
8import java.util.ArrayList;
9import java.util.List;
10public class Main {
11 public static void main(String[] args) {
12 // Set your AK, SK and the endpoint to be accessed
13 String endpoint = "bcc.bj.baidubce.com";
14 String ak = "ak";
15 String sk = "sk";
16 // Set default configuration
17 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
18 .withProtocol(Protocol.HTTP)
19 .withCredentials(new DefaultBceCredentials(ak, sk))
20 .withEndpoint(endpoint);
21 // Create a BCC client
22 BccClient bccClient = new BccClient(bccClientConfiguration);
23 UnbindTagsRequest unbindTagsRequest = new UnbindTagsRequest();
24 // Set the tag to be bound
25 List<TagModel> changeTags = new ArrayList<TagModel>();
26 TagModel tagModel = new TagModel().withTagKey("Key***").withTagValue("Value***");
27 changeTags.add(tagModel);
28 unbindTagsRequest.setChangeTags(changeTags);
29 // Set corresponding instance ID
30 unbindTagsRequest.setInstanceId("i-***");
31
32 bccClient.unbindInstanceFromTags(unbindTagsRequest);
33 }
34}
- Remove tags that are defined on an instance.
- You can unbind multiple tags at the same time.
Change the instance subnet
1package com.baidubce.services.bcc;
2import com.baidubce.BceClientConfiguration;
3import com.baidubce.Protocol;
4import com.baidubce.auth.DefaultBceCredentials;
5import com.baidubce.services.bcc.model.instance.ChangeInstanceSubnetRequest;
6public class ChangeSubnet {
7 public static void main(String[] args) {
8 // Set your AK, SK and the endpoint to be accessed
9 String endpoint = "bcc.bj.baidubce.com";
10 String ak = "ak";
11 String sk = "sk";
12 // Set default configuration
13 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration().withProtocol(Protocol.HTTP).withCredentials(new DefaultBceCredentials(ak, sk)).withEndpoint(endpoint);
14 // Create a BCC client
15 BccClient client = new BccClient(bccClientConfiguration);
16 ChangeInstanceSubnetRequest req = new ChangeInstanceSubnetRequest();
17 // Virtual machine instance ID
18 req.setInstanceId("i-***");
19 // Subnet ID after change
20 req.setSubnetId("sbn-***");
21 // Whether to reboot after change
22 req.setReboot(true);
23 // Change regular security group, which takes effect only when changing the VPC and cannot be set simultaneously with EnterpriseSecurityGroupIds
24 req.setSecurityGroupIds(Collections.singletonList("g-***"));
25 // Change enterprise security group, which takes effect only when changing the VPC and cannot be set simultaneously with SecurityGroupIds
26 req.setEnterpriseSecurityGroupIds(Collections.singletonList("esg-***"));
27 client.updateInstanceSubnet(req);
28 }
29}
- Change the subnet to which an instance belongs. By default, the instance will automatically reboot after a subnet change. However, users can opt out of this. Subnet changes are only supported within the same availability zone (AZ), and cross-AZ or cross-VPC changes are not allowed. When switching from a normal subnet to a NAT-dedicated subnet, you must manually unbind the EIP first.
Query instance package specifications
Query the list of instance package specifications. When creating a virtual machine, it is recommended to specify the desired machine type and configuration using the parameter (spec).
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.flavor.ListFlavorSpecRequest;
7import com.baidubce.services.bcc.model.flavor.ListBccFlavorSpecResponse;
8public class Main {
9 public static void main(String[] args) {
10 // Set your AK, SK and the endpoint to be accessed
11 String endpoint = "bcc.bj.baidubce.com";
12 String ak = "ak";
13 String sk = "sk";
14 // Set default configuration
15 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
16 .withProtocol(Protocol.HTTP)
17 .withCredentials(new DefaultBceCredentials(ak, sk))
18 .withEndpoint(endpoint);
19 // Create a BCC client
20 BccClient bccClient = new BccClient(bccClientConfiguration);
21 ListFlavorSpecRequest listFlavorSpecRequest = new ListFlavorSpecRequest();
22 // Set availability zone
23 listFlavorSpecRequest.setZoneName("cn-bj-d");
24
25 ListBccFlavorSpecResponse response = bccClient.listFlavorSpec(listFlavorSpecRequest);
26 System.out.println(response);
27 }
28}
- By default, fetch the instance package specifications for all availability zones in the current region. To retrieve specifications for a particular availability zone, set the appropriate parameter.
Query instance package price
Query the pricing information associated with the instance resource package specifications.
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.instance.BccPriceRequest;
7import com.baidubce.services.bcc.model.instance.BccPriceResponse;
8public class Main {
9 public static void main(String[] args) {
10 // Set your AK, SK and the endpoint to be accessed
11 String endpoint = "bcc.bj.baidubce.com";
12 String ak = "ak";
13 String sk = "sk";
14 // Set default configuration
15 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
16 .withProtocol(Protocol.HTTP)
17 .withCredentials(new DefaultBceCredentials(ak, sk))
18 .withEndpoint(endpoint);
19 // Create a BCC client
20 BccClient bccClient = new BccClient(bccClientConfiguration);
21 BccPriceRequest bccPriceRequest = new BccPriceRequest();
22 // Set instance family
23 bccPriceRequest.setSpecId("g5");
24 // Set instance package specifications
25 bccPriceRequest.setSpec("bcc.g5.c2m8");
26 // Set availability zone
27 bccPriceRequest.setZoneName("cn-bj-d");
28 // Set charge type
29 bccPriceRequest.setPaymentTiming("prepay");
30 // Set purchase period (in months)
31 bccPriceRequest.setPurchaseLength(2);
32 // Set purchase count
33 bccPriceRequest.setPurchaseCount(2);
34
35 BccPriceResponse response = bccClient.getPriceBySpec(bccPriceRequest);
36 System.out.println(response);
37 }
38}
- By setting the instance family, you can obtain the prices of all packages under the current instance family.
- Set instance package specifications to retrieve the price of the current package
- Bill type: Postpaid and Prepaid
Query resizing specifications
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.model.instance.ListAvailableResizeSpecRequest;
5import com.baidubce.services.bcc.model.instance.ListAvailableResizeSpecResponse;
6public class Main {
7 public static void main(String[] args) {
8 // Set your AK, SK and the endpoint to be accessed
9 String endpoint = "bcc.bj.baidubce.com";
10 String ak = "ak";
11 String sk = "sk";
12 // Set default configuration
13 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
14 .withProtocol(Protocol.HTTP)
15 .withCredentials(new DefaultBceCredentials(ak, sk))
16 .withEndpoint(endpoint);
17 // Create a BCC client
18 BccClient client = new BccClient(bccClientConfiguration);
19 ListAvailableResizeSpecRequest request = new ListAvailableResizeSpecRequest()
20 .withSpec("bcc.ic4.c1m1").withZone("cn-bj-a");
21 ListAvailableResizeSpecResponse response = client.listAvailableResizeSpec(request);
22 System.out.println(response);
23 }
24}
Retrieve network interface card information for an instance
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.model.instance.ListInstanceEnisRequest;
5public class Main {
6 public static void main(String[] args) {
7 // Set your AK, SK and the endpoint to be accessed
8 String endpoint = "bcc.bj.baidubce.com";
9 String ak = "ak";
10 String sk = "sk";
11 // Set default configuration
12 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
13 .withProtocol(Protocol.HTTP)
14 .withCredentials(new DefaultBceCredentials(ak, sk))
15 .withEndpoint(endpoint);
16 // Create a BCC client
17 BccClient client = new BccClient(bccClientConfiguration);
18 // Set instance ID
19 ListInstanceEnisRequest request = new ListInstanceEnisRequest().withInstanceId("i-CH47****");
20 System.out.println(client.listRecycleInstance());
21 }
22}
Change the instance VPC
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.model.AbstractBceResponse;
5import com.baidubce.services.bcc.model.instance.ChangeVpcRequest;
6import java.util.Arrays;
7public class Main {
8 public static void main(String[] args) {
9 // Set your AK, SK and the endpoint to be accessed
10 String endpoint = "bcc.bj.baidubce.com";
11 String ak = "ak";
12 String sk = "sk";
13 // Set default configuration
14 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
15 .withProtocol(Protocol.HTTP)
16 .withCredentials(new DefaultBceCredentials(ak, sk))
17 .withEndpoint(endpoint);
18 // Create a BCC client
19 BccClient client = new BccClient(bccClientConfiguration);
20 // Change VPC
21 ChangeVpcRequest request = new ChangeVpcRequest()
22 .withInstanceId("i-sZcd****")
23 .withSubnetId("sbn-3rgdxbbn****")
24 .withSecurityGroupIds(Arrays.asList("g-63vdycz*****"));
25 AbstractBceResponse response = client.changeVpc(request);
26 System.out.println(response.getMetadata().getBceRequestId());
27 }
28}
- Instances will be rebooted in the startup status, but they will not be rebooted in the stopped status
Convert to prepay in batch
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.model.instance.BatchChangeToPrepaidRequest;
5import com.baidubce.services.bcc.model.instance.BatchChangeToPrepaidResponse;
6import com.baidubce.services.bcc.model.instance.ChangeToPrepaidRequest;
7import java.util.ArrayList;
8import java.util.List;
9public class Main {
10 public static void main(String[] args) {
11 // Set your AK, SK and the endpoint to be accessed
12 String endpoint = "bcc.bj.baidubce.com";
13 String ak = "ak";
14 String sk = "sk";
15 // Set default configuration
16 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
17 .withProtocol(Protocol.HTTP)
18 .withCredentials(new DefaultBceCredentials(ak, sk))
19 .withEndpoint(endpoint);
20 // Create a BCC client
21 BccClient client = new BccClient(bccClientConfiguration);
22 // Specify the virtual machine IDs for shifting charge and the duration of the subscription (in months), with no more than 100 virtual machines
23 BatchChangeToPrepaidRequest request = new BatchChangeToPrepaidRequest();
24 List<ChangeToPrepaidRequest> configList = new ArrayList<>();
25 configList.add(new ChangeToPrepaidRequest().withInstanceId("i-sZcdKAZR").withDuration(1));
26 configList.add(new ChangeToPrepaidRequest().withInstanceId("i-CH47zOoW").withDuration(1));
27 request.setConfig(configList);
28
29 BatchChangeToPrepaidResponse response = client.batchChangeToPrepaid(request);
30 System.out.println(response);
31 }
32}
Query recycle bin instance list
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.model.instance.ListInstancesResponse;
5import com.baidubce.services.bcc.model.instance.ListRecycleInstanceRequest;
6import java.util.Calendar;
7import java.util.Date;
8public class Main {
9 public static void main(String[] args) {
10 // Set your AK, SK and the endpoint to be accessed
11 String endpoint = "bcc.bj.baidubce.com";
12 String ak = "ak";
13 String sk = "sk";
14 // Set default configuration
15 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
16 .withProtocol(Protocol.HTTP)
17 .withCredentials(new DefaultBceCredentials(ak, sk))
18 .withEndpoint(endpoint);
19 // Create a BCC client
20 BccClient client = new BccClient(bccClientConfiguration);
21 // Query postpaid instances that enter the recycle bin within a specified time range
22 Date today = new Date();
23 Calendar calendar = Calendar.getInstance();
24 calendar.setTime(today);
25 calendar.add(Calendar.DATE, -1);
26 Date lastDay = calendar.getTime();
27 ListRecycleInstanceRequest request = new ListRecycleInstanceRequest()
28 .withRecycleBegin(lastDay)
29 .withRecycleEnd(today);
30 .withPaymentTiming(PaymentTiming.Postpaid)
31 ListInstancesResponse response = client.listRecycleInstance(request);
32 System.out.println(response);
33 }
34}
Release recycle bin instances
You can release a specified instance from the recycle bin with the following code
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.model.instance.ListInstancesResponse;
5import com.baidubce.services.bcc.model.instance.ListRecycleInstanceRequest;
6import java.util.Calendar;
7import java.util.Date;
8public class Main {
9 public static void main(String[] args) {
10 // Set your AK, SK and the endpoint to be accessed
11 String endpoint = "bcc.bj.baidubce.com";
12 String ak = "ak";
13 String sk = "sk";
14 // Set default configuration
15 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
16 .withProtocol(Protocol.HTTP)
17 .withCredentials(new DefaultBceCredentials(ak, sk))
18 .withEndpoint(endpoint);
19 // Create a BCC client
20 BccClient client = new BccClient(bccClientConfiguration);
21 client.deleteRecycledInstance("i-***");
22 }
23}
Remove the relationship between an instance and its deployment group
You can delete the relationship between an instance and a deployment group with the following code
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.model.instance.ListInstancesResponse;
5import com.baidubce.services.bcc.model.instance.ListRecycleInstanceRequest;
6import java.util.Calendar;
7import java.util.Date;
8public class Main {
9 public static void main(String[] args) {
10 // Set your AK, SK and the endpoint to be accessed
11 String endpoint = "bcc.bj.baidubce.com";
12 String ak = "ak";
13 String sk = "sk";
14 // Set default configuration
15 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
16 .withProtocol(Protocol.HTTP)
17 .withCredentials(new DefaultBceCredentials(ak, sk))
18 .withEndpoint(endpoint);
19 // Create a BCC client
20 BccClient client = new BccClient(bccClientConfiguration);
21 DeleteInstanceDeploysetRequest deleteInstanceDeploysetRequest = new DeleteInstanceDeploysetRequest();
22 deleteInstanceDeploysetRequest
23 .withDeployId("dset-NXyUzFou")
24 .withInstanceIdList(Collections.singletonList("i-pKq2Bnhf"));
25 client.deleteInstanceDeploySet(deleteInstanceDeploysetRequest);
26 }
27}
Add IPv6
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.model.instance.InstanceIpv6Request;
5import com.baidubce.services.bcc.model.instance.InstanceAddIpv6Response;
6public class Main {
7 public static void main(String[] args) {
8 // Set your AK, SK and the endpoint to be accessed
9 String endpoint = "bcc.bj.baidubce.com";
10 String ak = "ak";
11 String sk = "sk";
12 // Set default configuration
13 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
14 .withProtocol(Protocol.HTTP)
15 .withCredentials(new DefaultBceCredentials(ak, sk))
16 .withEndpoint(endpoint);
17 // Create a BCC client
18 BccClient client = new BccClient(bccClientConfiguration);
19 // Add IPv6
20 InstanceIpv6Request request = new InstanceIpv6Request();
21 request.setInstanceId("i-0nPl9WFJ");
22 request.setIpv6Address("2400:da00:e003:0:41c:4100:0:5");
23 request.setReboot(false);
24 InstanceAddIpv6Response response = client.addIpv6(request);
25 System.out.println(response);
26 }
27}
Release IPv6
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.model.instance.InstanceIpv6Request;
5public class Main {
6 public static void main(String[] args) {
7 // Set your AK, SK and the endpoint to be accessed
8 String endpoint = "bcc.bj.baidubce.com";
9 String ak = "ak";
10 String sk = "sk";
11 // Set default configuration
12 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
13 .withProtocol(Protocol.HTTP)
14 .withCredentials(new DefaultBceCredentials(ak, sk))
15 .withEndpoint(endpoint);
16 // Create a BCC client
17 BccClient client = new BccClient(bccClientConfiguration);
18 // Release IPv6
19 InstanceIpv6Request request = new InstanceIpv6Request();
20 request.setInstanceId("i-0nPl9WFJ");
21 request.setIpv6Address("2400:da00:e003:0:41c:4100:0:5");
22 request.setReboot(false);
23 client.delIpv6(request);
24 }
25}
Add secondary IPs to the primary network interface card in batch
You can add secondary IPs to the primary network interface card for BCC in batch with the following code
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.model.instance.BatchAddIpRequest;
5public class Main {
6 public static void main(String[] args) {
7 // Set your AK, SK and the endpoint to be accessed
8 String endpoint = "bcc.bj.baidubce.com";
9 String ak = "ak";
10 String sk = "sk";
11 // Set default configuration
12 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
13 .withProtocol(Protocol.HTTP)
14 .withCredentials(new DefaultBceCredentials(ak, sk))
15 .withEndpoint(endpoint);
16 // Create a BCC client
17 BccClient client = new BccClient(bccClientConfiguration);
18 // Allocate IPv6 to the primary network interface card, with no more than 100 IPs
19 BatchAddIpRequest request = new BatchAddIpRequest();
20 request.setInstanceId("i-udU7HS0a");
21 request.setAllocateMultiIpv6Addr(true);
22 request.setSecondaryPrivateIpAddressCount(2);
23 client.batchAddIp(request);
24 }
25}
Delete secondary IP address from the primary network interface card in batch
You can delete secondary IP address from the primary network interface card for BCC in batch with the following code
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.model.instance.BatchDeleteIpRequest;
5import java.util.Collections;
6public class Main {
7 public static void main(String[] args) {
8 // Set your AK, SK and the endpoint to be accessed
9 String endpoint = "bcc.bj.baidubce.com";
10 String ak = "ak";
11 String sk = "sk";
12 // Set default configuration
13 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
14 .withProtocol(Protocol.HTTP)
15 .withCredentials(new DefaultBceCredentials(ak, sk))
16 .withEndpoint(endpoint);
17 // Create a BCC client
18 BccClient client = new BccClient(bccClientConfiguration);
19 // Release IPv6 to the primary network interface card, maximum 100 IPs
20 InstanceIpv6Request request = new InstanceIpv6Request();
21 request.setInstanceId("i-0nPl9WFJ");
22 request.setPrivateIps(Collections.singletonList("2400:da00:e003:0:41c:4100:0:5"));
23 client.batchDeleteIp(request);
24 }
25}
Create automatic renewal rules
You can create automatic renewal rules for BCC with the following code
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.model.instance.BatchDeleteIpRequest;
5import java.util.Collections;
6public class Main {
7 public static void main(String[] args) {
8 // Set your AK, SK and the endpoint to be accessed
9 String endpoint = "bcc.bj.baidubce.com";
10 String ak = "ak";
11 String sk = "sk";
12 // Set default configuration
13 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
14 .withProtocol(Protocol.HTTP)
15 .withCredentials(new DefaultBceCredentials(ak, sk))
16 .withEndpoint(endpoint);
17 // Create a BCC client
18 BccClient client = new BccClient(bccClientConfiguration);
19 // Create auto-renewal rules
20 client.createAutoRenewRule(new BccAutoRenewRequest()
21 .withInstanceId("i-dl2s537H")
22 .withRenewTimeUnit("month")
23 .withRenewTime(1)
24 .withRenewEip(true)
25 .withRenewCds(true));
26 }
27}
Delete automatic renewal rules
You can delete auto-renewal rules for BCC with the following code
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.model.instance.BatchDeleteIpRequest;
5import java.util.Collections;
6public class Main {
7 public static void main(String[] args) {
8 // Set your AK, SK and the endpoint to be accessed
9 String endpoint = "bcc.bj.baidubce.com";
10 String ak = "ak";
11 String sk = "sk";
12 // Set default configuration
13 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
14 .withProtocol(Protocol.HTTP)
15 .withCredentials(new DefaultBceCredentials(ak, sk))
16 .withEndpoint(endpoint);
17 // Create a BCC client
18 BccClient client = new BccClient(bccClientConfiguration);
19 // Delete auto-renewal rules
20 client.deleteAutoRenewRule(new BccAutoRenewRequest()
21 .withInstanceId("i-dl2s537H")
22 .withRenewEip(true)
23 .withRenewCds(true));
24 }
25}
Query the custom data of the instance
You can query the custom data of the instance with the following code
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.model.instance.BatchDeleteIpRequest;
5import java.util.Collections;
6public class Main {
7 public static void main(String[] args) {
8 // Set your AK, SK and the endpoint to be accessed
9 String endpoint = "bcc.bj.baidubce.com";
10 String ak = "ak";
11 String sk = "sk";
12 // Set default configuration
13 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
14 .withProtocol(Protocol.HTTP)
15 .withCredentials(new DefaultBceCredentials(ak, sk))
16 .withEndpoint(endpoint);
17 // Create a BCC client
18 BccClient client = new BccClient(bccClientConfiguration);
19 // Set request body and send request
20 GetInstanceUserDataAttrRequest request = new GetInstanceUserDataAttrRequest();
21 request.withInstanceId("i-b34ycow2");
22 GetInstanceUserDataAttrResponse response = client.getInstanceUserDataAttribute(request);
23 toJsonPrettyString("getInstanceUserDataAttribute", response);
24 }
25}
