1

I am implementing an edit action for autoscale policy, but it returns error "Invalid guest template, internal error A single action is required for a policy". There is only one action for policy, but it still returns the error.

This is my sample code in Java. Please let me know if found any defect.

Thank you

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);

    }
}
Mike Oh
  • 151
  • 5

1 Answers1

1

As I see scaleGroup: 1164865 has two scaleActions for Policy: 180155 and in your code, you are trying to clean the scaleActions and adding a new one. This is the issue, unfortunately it's not possible to delete scaleActions because you have two currently and it raises the issue.

Currently, there is an issue reported in SoftLayer but it take some time to developers can fix this.


Updated

The extra scaleActions were deleted, please be careful to avoid create/edit Policy with 2 or 0 scaleActions (I recommend this because we have a known issue about this). A Policy just should be created/edited with 1 scaleActions. Please try again.