预留实例券
更新时间:2024-07-02
创建预留实例券
使用以下代码给指定创建预留实例券:
import com.baidubce.BceClientConfiguration;
import com.baidubce.Protocol;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bcc.BccClient;
import com.baidubce.services.bcc.BccClientConfiguration;
import com.baidubce.services.bcc.model.TagModel;
import com.baidubce.services.bcc.model.reversed.CreateReservedInstanceResponse;
import com.baidubce.services.bcc.model.reversed.CreateReservedInstancesRequest;
import lombok.extern.slf4j.Slf4j;
import java.util.Collections;
public class ExampleCreateReservedInstance {
public static void main(String[] args) {
// 设置您的ak、sk和要访问的endpoint
String endpoint = "bcc.bj.baidubce.com";
String ak = "ak";
String sk = "sk";
// 设置默认配置
BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
.withProtocol(Protocol.HTTP)
.withCredentials(new DefaultBceCredentials(ak, sk))
.withEndpoint(endpoint);
// 创建bcc client
BccClient bccClient = new BccClient(bccClientConfiguration);
CreateReservedInstancesRequest request = new CreateReservedInstancesRequest();
request.setReservedInstanceName("bj-test");
request.setScope("AZ");
request.setZoneName("cn-bj-a");
request.setSpec("bcc.c5.c2m8");
request.setOfferingType("FullyPrepay");
request.setInstanceCount(1);
request.setReservedInstanceCount(1);
request.setReservedInstanceTime(1);
request.setReservedInstanceTimeUnit("month");
request.setAutoRenewTimeUnit("month");
request.setAutoRenewTime(1);
request.setAutoRenew(true);
TagModel tagModel = new TagModel();
tagModel.setTagKey("testKey");
tagModel.setTagValue("testValue");
request.setTags(Collections.singletonList(tagModel));
CreateReservedInstanceResponse response = bccClient.createReservedInstances(request);
}
}
修改预留实例券
使用以下代码给指定修改预留实例券属性:
import com.baidubce.BceClientConfiguration;
import com.baidubce.Protocol;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bcc.BccClient;
import com.baidubce.services.bcc.BccClientConfiguration;
import com.baidubce.services.bcc.model.reversed.ModifyReservedInstancesResponse;
import com.baidubce.services.bcc.model.reversed.ModifyReservedInstancesRequest;
import com.baidubce.services.bcc.model.reversed.ModifyReservedInstancesRequest.ReservedInstance;
import lombok.extern.slf4j.Slf4j;
import java.util.Collections;
public class ExampleModifyReservedInstances {
public static void main(String[] args) {
// 设置您的ak、sk和要访问的endpoint
String endpoint = "bcc.bj.baidubce.com";
String ak = "ak";
String sk = "sk";
// 设置默认配置
BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
.withProtocol(Protocol.HTTP)
.withCredentials(new DefaultBceCredentials(ak, sk))
.withEndpoint(endpoint);
// 创建bcc client
BccClient bccClient = new BccClient(bccClientConfiguration);
ModifyReservedInstancesRequest request = new ModifyReservedInstancesRequest();
// 修改名称
ReservedInstance modifyName = new ReservedInstance();
modifyName.setReservedInstanceId("r-qt4ItfUP");
modifyName.setReservedInstanceName("update-reserved-instance");
request.setReservedInstances(Collections.singletonList(modifyName));
ModifyReservedInstancesResponse response = bccClient.modifyReservedInstances(request);
}
}
绑定标签
使用以下代码给指定预留实例券绑定标签:
import com.baidubce.BceClientConfiguration;
import com.baidubce.Protocol;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bcc.BccClient;
import com.baidubce.services.bcc.BccClientConfiguration;
import com.baidubce.services.bcc.model.TagModel;
import com.baidubce.services.bcc.model.reversed.ReservedTagsRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ExampleBindReservedToTags {
public static void main(String[] args) {
// 设置您的ak、sk和要访问的endpoint
String endpoint = "bcc.bj.baidubce.com";
String ak = "ak";
String sk = "sk";
// 设置默认配置
BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
.withProtocol(Protocol.HTTP)
.withCredentials(new DefaultBceCredentials(ak, sk))
.withEndpoint(endpoint);
// 创建bcc client
BccClient bccClient = new BccClient(bccClientConfiguration);
ReservedTagsRequest reservedTagsRequest = new ReservedTagsRequest();
// 标签列表
List<TagModel> changeTags = new ArrayList<TagModel>();
TagModel tagModel = new TagModel().withTagKey("Key").withTagValue("Value");
changeTags.add(tagModel);
reservedTagsRequest.setChangeTags(changeTags);
// 预留实例券ID列表,,最多支持100个
reservedTagsRequest.withInstanceIds(Arrays.asList("r-oFpMXKhv", "r-HrztSVk0"));
bccClient.bindReservedInstanceToTags(reservedTagsRequest);
}
}
解绑标签
使用以下代码从指定预留实例券中解绑标签:
import com.baidubce.BceClientConfiguration;
import com.baidubce.Protocol;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bcc.BccClient;
import com.baidubce.services.bcc.BccClientConfiguration;
import com.baidubce.services.bcc.model.TagModel;
import com.baidubce.services.bcc.model.reversed.ReservedTagsRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ExampleUnbindReservedFromTags {
public static void main(String[] args) {
// 设置您的ak、sk和要访问的endpoint
String endpoint = "bcc.bj.baidubce.com";
String ak = "ak";
String sk = "sk";
// 设置默认配置
BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
.withProtocol(Protocol.HTTP)
.withCredentials(new DefaultBceCredentials(ak, sk))
.withEndpoint(endpoint);
// 创建bcc client
BccClient bccClient = new BccClient(bccClientConfiguration);
ReservedTagsRequest reservedTagsRequest = new ReservedTagsRequest();
// 标签列表
List<TagModel> changeTags = new ArrayList<TagModel>();
TagModel tagModel = new TagModel().withTagKey("Key").withTagValue("Value");
changeTags.add(tagModel);
reservedTagsRequest.setChangeTags(changeTags);
// 预留实例券ID列表,最多支持100个
reservedTagsRequest.withInstanceIds(Arrays.asList("r-oFpMXKhv", "r-HrztSVk0"));
bccClient.unbindReservedInstanceFromTags(reservedTagsRequest);
}
}