我正在为自动缩放策略实施编辑操作,但它返回错误"无效的访客模板,内部错误 策略需要单个操作"。 策略只有一个操作,但它仍然返回错误。
这是我在Java中的示例代码。如果发现任何缺陷,请通知我。
谢谢
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.softlayer.api.ApiClient;
import com.softlayer.api.RestApiClient;
import com.softlayer.api.service.Account;
import com.softlayer.api.service.Location;
import com.softlayer.api.service.container.virtual.guest.Configuration;
import com.softlayer.api.service.container.virtual.guest.configuration.Option;
import com.softlayer.api.service.hardware.Router;
import com.softlayer.api.service.location.Datacenter;
import com.softlayer.api.service.network.application.delivery.controller.loadbalancer.VirtualIpAddress;
import com.softlayer.api.service.network.application.delivery.controller.loadbalancer.VirtualServer;
import com.softlayer.api.service.network.application.delivery.controller.loadbalancer.health.Check;
import com.softlayer.api.service.network.application.delivery.controller.loadbalancer.health.check.Type;
import com.softlayer.api.service.provisioning.Hook;
import com.softlayer.api.service.scale.Asset;
import com.softlayer.api.service.scale.Group;
import com.softlayer.api.service.scale.LoadBalancer;
import com.softlayer.api.service.scale.Policy;
import com.softlayer.api.service.scale.group.Log;
import com.softlayer.api.service.scale.network.Vlan;
import com.softlayer.api.service.scale.policy.Action;
import com.softlayer.api.service.scale.policy.Trigger;
import com.softlayer.api.service.scale.policy.action.Scale;
import com.softlayer.api.service.scale.policy.trigger.OneTime;
import com.softlayer.api.service.scale.policy.trigger.Repeating;
import com.softlayer.api.service.scale.policy.trigger.ResourceUse;
import com.softlayer.api.service.scale.policy.trigger.resourceuse.Watch;
import com.softlayer.api.service.security.ssh.Key;
import com.softlayer.api.service.virtual.Guest;
import com.softlayer.api.service.virtual.disk.Image;
import com.softlayer.api.service.virtual.guest.block.Device;
import com.softlayer.api.service.virtual.guest.network.Component;
public class EditPolicy{
private static String user = "set me";
private static String apiKey = "set me";
private static ApiClient client = new RestApiClient().withCredentials(user, apiKey);
public static void main(String[] args) {
editScaleGroup();
}
private void editScaleGroup() {
Group.Service groupService = Group.service(client, 1164865l);
/**
* Network Vlans
*/
ArrayList<Long> networkVlans = new ArrayList<Long>();
networkVlans.add(1148299l); // Private VlanID
networkVlans.add(1148297l); // Public VlanID
/**
* Define SoftLayer_Scale_Group object that you wish to create
*/
Group templateObject = new Group();
templateObject.setName("TestGroup0905");
templateObject.setRegionalGroupId(22l);
templateObject.setTerminationPolicyId(1l);
// Unit : SEC, MIN, HOUR, DAY
templateObject.setCooldown(getSec(28l, "MIN"));
templateObject.setMaximumMemberCount(5l);
templateObject.setMinimumMemberCount(1l);
templateObject.setSuspendedFlag(false);
// Define SoftLayer_Virtual_Guest
Guest virtualGuestMemberTemplate = new Guest();
virtualGuestMemberTemplate.setHostname("myhost");
virtualGuestMemberTemplate.setDomain("test.com");
virtualGuestMemberTemplate.setMaxMemory(10l);
virtualGuestMemberTemplate.setStartCpus(1l);
Device block = new Device();
block.setDevice("0");
Image image = new Image();
image.setCapacity(25l);
block.setDiskImage(image);
virtualGuestMemberTemplate.getBlockDevices().add(block);
// Define Location
Location location = new Location();
location.setName("dal01");
virtualGuestMemberTemplate.setDatacenter(location);
// Define Hourly billing and local disk : "SAN"
virtualGuestMemberTemplate.setHourlyBillingFlag(true);
virtualGuestMemberTemplate.setLocalDiskFlag(false);
// Network Components
Component networkComponent = new Component();
networkComponent.setMaxSpeed(10l);
virtualGuestMemberTemplate.getNetworkComponents().add(networkComponent);
// OS
virtualGuestMemberTemplate.setOperatingSystemReferenceCode("CENTOS_LATEST");
// Private Network Only
virtualGuestMemberTemplate.setPrivateNetworkOnlyFlag(false);
// Network Vlans
groupService.setMask("mask[networkVlans]");
Group scaleGroupEdit = groupService.getObject();
for (Vlan v : scaleGroupEdit.getNetworkVlans()) {
Vlan.Service vlanService = Vlan.service(client, v.getId());
vlanService.deleteObject();
}
List<Vlan> vlans = new ArrayList<Vlan>();
Vlan.Service vlanService = Vlan.service(client);
for (int i = 0; i < networkVlans.size(); i++) {
Vlan vlan = new Vlan();
vlan.setNetworkVlanId(networkVlans.get(i));
vlan.setScaleGroupId(scaleGroupEdit.getId());
Vlan vlanObject = vlanService.createObject(vlan);
vlans.add(vlanObject);
}
templateObject.getNetworkVlans().clear();
templateObject.getNetworkVlans().addAll(vlans);
// Set Balance Across Multiple Vlans
templateObject.setBalancedTerminationFlag(false);
// Adding Virtual Guest member template to the template
templateObject.setVirtualGuestMemberTemplate(virtualGuestMemberTemplate);
// Define Load Balancer with list.
LoadBalancer lBalancer = new LoadBalancer();
lBalancer.setDeleteFlag(false);
lBalancer.setPort(82L);
// Set Server Id
lBalancer.setVirtualServerId(259993l);
// Define Type
Type type = new Type();
type.setKeyname("TCP");
Check healthCheck = new Check();
healthCheck.setType(type);
lBalancer.setHealthCheck(healthCheck);
// Adding Load Balancer to the template
templateObject.getLoadBalancers().add(lBalancer);
LoadBalancer lBalancer1 = new LoadBalancer();
lBalancer1.setDeleteFlag(false);
lBalancer1.setPort(21L);
// Set Server Id
lBalancer1.setVirtualServerId(259993l);
// Define Type
Type type1 = new Type();
type1.setKeyname("TCP");
Check healthCheck1 = new Check();
healthCheck1.setType(type1);
lBalancer1.setHealthCheck(healthCheck1);
// Adding Load Balancer to the template
templateObject.getLoadBalancers().add(lBalancer1);
// Define Policy with list
Policy pObject = new Policy();
pObject.setCooldown(getSec(30L, "MIN"));
pObject.setName("My policy");
pObject.setId(180155L);
// Define Action for the policy
Scale scaleActions = new Scale();
scaleActions.setScaleType("PERCENT");
scaleActions.setAmount(5L);
pObject.getScaleActions().clear();
pObject.getScaleActions().add(scaleActions);
// Repeating
Repeating repeating = new Repeating();
List<String> dayList = new ArrayList<>();
dayList.add("MON");
dayList.add("TUE");
String hour = "09";
String days = "";
for (String day : dayList) {
if (!days.equals("")) {
days.concat(",");
}
days = days.concat(day);
}
String schedule = "0 " + hour + " ? * " + days + " *";
repeating.setSchedule(schedule);
pObject.getRepeatingTriggers().add(repeating);
// ResourceUse
ResourceUse resourceUse = new ResourceUse();
Watch watch = new Watch();
watch.setMetric("host.network.frontend.in.rate");
watch.setAlgorithm("EWMA");
watch.setOperator(">");
watch.setPeriod(9300L);
watch.setValue("0");
resourceUse.getWatches().add(watch);
Watch watch1 = new Watch();
watch1.setMetric("host.network.backend.out.rate");
watch1.setAlgorithm("EWMA");
watch1.setOperator(">");
watch1.setPeriod(8700L);
watch1.setValue("0");
resourceUse.getWatches().add(watch1);
Watch watch2 = new Watch();
watch2.setMetric("host.cpu.percent");
watch2.setAlgorithm("EWMA");
watch2.setOperator(">");
watch2.setPeriod(8400L);
watch2.setValue("80");
resourceUse.getWatches().add(watch2);
pObject.getResourceUseTriggers().add(resourceUse);
// Add Policy to the template
templateObject.getPolicies().add(pObject);
// Create Object
Boolean result = false;
try {
result = groupService.editObject(templateObject);
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println("Edit result : " + result);
}
}
答案 0 :(得分:1)
我看到 scaleGroup:1164865有两个针对策略的scaleActions:180155 ,在您的代码中,您正在尝试清理scaleActions并添加一个新的。这是问题,遗憾的是,删除scaleActions是不可能的,因为您当前有两个并且它会引发问题。
目前,SoftLayer报告了一个问题,但开发人员需要一些时间来解决这个问题。
<强>更新强>
删除了额外的scaleActions,请注意避免使用2或0 scaleActions创建/编辑策略(我建议这样做,因为我们已经知道了这个问题)。应该使用1个scaleActions创建/编辑策略。请再试一次。