Search code examples
ibm-cloud-infrastructure

Add a loadbalancer in scale group SL


I'm developing a scale group edit function. There are 2 loadbalancers. One is an existed LB and the other one is an added LB while editing. When the sample code is executed, it returns an error, "Health check type required" even though the health check type has been already set. Please check my sample code below and let me know if you find any doubt.

public void editObject(){

    Long groupId = 1213185L;
    Group.Service groupService = Group.service(client, groupId);

    /**
     * Group Configuration Group Details
     */
    String groupName = "EditTest";
    Long regionId= 1025L ; //"as-kor-west-1"
    String datacenter = "seo01";
    Long terminationPolicyId = 1L;

    /**
     * Define SoftLayer_Scale_Group object that you wish to create
     */
    Group templateObject = new Group();
    templateObject.setName(groupName);
    templateObject.setRegionalGroupId(regionId);
    templateObject.setTerminationPolicyId(terminationPolicyId);

    templateObject.setCooldown(1800L);
    templateObject.setMaximumMemberCount(5L);
    templateObject.setMinimumMemberCount(0L);
    templateObject.setSuspendedFlag(false);


    /**
     * Member Configuration Member Details
     */
    String hostname = "edittest";
    String domain = "test.com";


    // Define SoftLayer_Virtual_Guest
    Guest virtualGuestMemberTemplate = new Guest();
    virtualGuestMemberTemplate.setHostname(hostname);
    virtualGuestMemberTemplate.setDomain(domain);

    virtualGuestMemberTemplate.setMaxMemory(1024L);
    virtualGuestMemberTemplate.setStartCpus(1L);


    //Define Storage
    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(datacenter);
    virtualGuestMemberTemplate.setDatacenter(location);

    // Define Hourly billing and local disk
    virtualGuestMemberTemplate.setHourlyBillingFlag(true);
    virtualGuestMemberTemplate.setLocalDiskFlag(false);

    // Network Components
    Component networkComponent = new Component();
    networkComponent.setMaxSpeed(10L);

    virtualGuestMemberTemplate.getNetworkComponents().add(networkComponent);
    virtualGuestMemberTemplate.setOperatingSystemReferenceCode("CENTOS_LATEST");

    // Private Network Only
    virtualGuestMemberTemplate.setPrivateNetworkOnlyFlag(false);

    // Set Balance Across Multiple Vlans
    templateObject.setBalancedTerminationFlag(false);

    // Adding Virtual Guest member template to the template
    templateObject.setVirtualGuestMemberTemplate(virtualGuestMemberTemplate);

    //***************************************************************
    // Define Load Balancer1 to Edit a load balancer already created
    //***************************************************************
    LoadBalancer lBalancer = new LoadBalancer();
    lBalancer.setDeleteFlag(false);
    lBalancer.setId(112103L);

    // Set Server Id
    lBalancer.setVirtualServerId(292559L);
    lBalancer.setVirtualIpAddressId(57137607L);  // Virtual IP ID
    lBalancer.setPort(20L);

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

    //**************************************************************
    // Define Load Balancer2 to add a load balancer while editing.
    //**************************************************************
    LoadBalancer lBalancer2 = new LoadBalancer();
    lBalancer2.setDeleteFlag(false);

    // Set Server Id
    lBalancer2.setVirtualServerId(292673L);
    lBalancer2.setVirtualIpAddressId(57137609L);  // Virtual IP ID
    lBalancer2.setPort(23L);

    // Define Type
    Type type2 = new Type();
    type2.setKeyname("Default");

    Check healthCheck2 = new Check();
    healthCheck2.setType(type2);

    lBalancer2.setHealthCheck(healthCheck2);

    // Adding Load Balancer to the template
    templateObject.getLoadBalancers().add(lBalancer2);

    // Edit Object
    Boolean result = groupService.editObject(templateObject);
    System.out.println("result :: " + result);

}

Solution

  • As I see, the issue is due to this line:

    type2.setKeyname("Default");
    

    You need to send the keyname using uppercase:

    type2.setKeyname("DEFAULT");
    

    The following method will help to get all type availables:

    SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_Health_Check_Type::getAllObjects

    Regarding to you second question, you are right, you need to set deleteFlag as True

    Here an example:
    
    package com.softlayer.api.scaleGroups;
    
    import com.softlayer.api.ApiClient;
    import com.softlayer.api.RestApiClient;
    import com.softlayer.api.service.scale.Group;
    import com.softlayer.api.service.scale.LoadBalancer;
    
    /**
     * This script delete load balancer from scale group
     *
     * Important Manual Page:
     * http://sldn.softlayer.com/reference/datatypes/SoftLayer_Scale_Group
     * http://sldn.softlayer.com/reference/services/SoftLayer_Scale_Group/editObject
     *
     * @license <http://sldn.softlayer.com/article/License>
     * @authon SoftLayer Technologies, Inc. <sldn@softlayer.com>
     * @version 0.2.2 (master branch)
     */
    public class deleteLoadBalancer {
        public deleteLoadBalancer(){
    
            // define you SoftLayer's username and apiKey
            String username = "set me";
            String apiKey = "set me";
            // Create Client
            ApiClient client=new RestApiClient().withCredentials(username, apiKey);
            // Define Scale Group Identifier
            Long groupId = 1213877L;
            // Define the load balancer's identifier that you wish to delete
            Long loadBalancerId = 112703L;
            // Create Scale Group Service
            Group.Service groupService = Group.service(client, groupId);
            // Define a mask to get additional properties
            groupService.withNewMask().loadBalancers().healthCheck().type();
            try {
    
                Group scalegroup = groupService.getObject();
                for (LoadBalancer loadBalancer : scalegroup.getLoadBalancers()) {
                    if (loadBalancer.getId().equals(loadBalancerId)) {
                        loadBalancer.setDeleteFlag(true);
                    }
                }
    
                boolean result = groupService.editObject(scalegroup);
                System.out.println(result);
            } catch (Exception e){
            System.out.println("Error: " + e);}
    
        }
    
        public static void main(String args[])
        {
            new deleteLoadBalancer();
        }
    }