在SL中编辑比例对象

时间:2016-08-09 12:15:00

标签: ibm-cloud-infrastructure

我正在尝试编辑一个缩放组,但是当我编辑Vlans时,它会返回一个异常。这是我测试过的代码。编辑方式与创建对象相同,但我已经将editId和policyId与editObject()一起添加。如果发现任何疑问,请告诉我。谢谢

    RestApiClient client = getSoftlayerClient();
    groupService = Group.service(client, group.getGroupId());

    /**
     * Network Vlans
     */
    Long[] networkVlans = { group.getPublicVlanId(), group.getPrivateVlanId() };

    /**
     * Define SoftLayer_Scale_Group object that you wish to create
     */
    Group templateObject = new Group();
    templateObject.setName(group.getGroupName());
    templateObject.setRegionalGroupId(group.getRegionId());
    templateObject.setTerminationPolicyId(group.gettPolicyId());

    // Unit : SEC, MIN, HOUR, DAY
    templateObject.setCooldown(getSec(group.getCooldownPeriod(), group.getCooldownUnit()));

    templateObject.setMaximumMemberCount(group.getMaxMember());
    templateObject.setMinimumMemberCount(group.getMinMember());

    templateObject.setSuspendedFlag(false);

    // Define SoftLayer_Virtual_Guest
    Guest virtualGuestMemberTemplate = new Guest();
    virtualGuestMemberTemplate.setHostname(group.getHostName());
    virtualGuestMemberTemplate.setDomain(group.getDomain());
    virtualGuestMemberTemplate.setMaxMemory(group.getMaxMemory());
    virtualGuestMemberTemplate.setStartCpus(group.getStartCpu());

    List<AutoScaleStorageVO> storageList = group.getStorageList();

    for (AutoScaleStorageVO storage : storageList) {
        Device block = new Device();
        block.setDevice(storage.getDiskOrder());
        Image image = new Image();
        image.setCapacity(storage.getCapacity());
        block.setDiskImage(image);
        virtualGuestMemberTemplate.getBlockDevices().add(block);
    }

    // Define Location

    if (group.getDatacenter() != "") {
        Location location = new Location();
        location.setName(group.getDatacenter());
        virtualGuestMemberTemplate.setDatacenter(location);
    }

    // Define Hourly billing and local disk
    virtualGuestMemberTemplate.setHourlyBillingFlag(true);
    if (storageList.get(0).getDiskType().equals("LOCAL")) {
        virtualGuestMemberTemplate.setLocalDiskFlag(true);
    } else {
        virtualGuestMemberTemplate.setLocalDiskFlag(false);
    }

    // Network Components
    Component networkComponent = new Component();
    networkComponent.setMaxSpeed(group.getMaxSpeed());

    virtualGuestMemberTemplate.getNetworkComponents().add(networkComponent);

    // OS
    virtualGuestMemberTemplate.setOperatingSystemReferenceCode(group.getOsCode());

    virtualGuestMemberTemplate.setPrivateNetworkOnlyFlag(group.isPrivateOnly());

    // Ssh key
    if (group.getSshKeyId() != null) {
        Key newKey = new Key();
        newKey.setId(group.getSshKeyId());
        virtualGuestMemberTemplate.getSshKeys().add(newKey);
    }

    // Provision Script
    if (group.getPostInstallUri() != "") {
        virtualGuestMemberTemplate.setPostInstallScriptUri(group.getPostInstallUri());
    }

    // Network Vlans : Edit vlans with new vlanIds.

        for (int i = 0; i <= networkVlans.length; i++) {
            Vlan vlan = new Vlan();
            vlan.setNetworkVlanId(networkVlans[i]);
            templateObject.getNetworkVlans().add(vlan);
        }

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

Boolean result = groupService.editObject(templateObject);

20:43:21.814 [http-bio-8181-exec-2]错误 - 无法编辑自动缩放组异常:2

1 个答案:

答案 0 :(得分:0)

试试这个:

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.softlayer.api.*;
import com.softlayer.api.service.scale.network.Vlan;
import com.softlayer.api.service.scale.Group;


public class EditScale {

    private static String user = "set me";

    private static String apiKey = "set me";

    private static ApiClient client = new RestApiClient().withCredentials(user, apiKey).withLoggingEnabled();

    private static Long idScaleGroup = new Long(11111);

    public static void main(String[] args) {

        Group.Service scaleGroupService = Group.service(client, idScaleGroup);
        scaleGroupService.setMask("mask[networkVlans]");


        Group scaleGroupEdit = scaleGroupService.getObject();

        for(Vlan v:scaleGroupEdit.getNetworkVlans()){
            Vlan.Service vlanService = Vlan.service(client, v.getId() );
            vlanService.deleteObject();
        }

        Vlan.Service vlanService = Vlan.service(client);

        Vlan templateObjectPrivate = new Vlan();
        templateObjectPrivate.setScaleGroupId(scaleGroupEdit.getId());
        templateObjectPrivate.setNetworkVlanId(2222222L);
        Vlan vlanPrivate = vlanService.createObject(templateObjectPrivate);

        Vlan templateObjectPublic = new Vlan();
        templateObjectPublic.setScaleGroupId(scaleGroupEdit.getId());
        templateObjectPublic.setNetworkVlanId(333333L);
        Vlan vlanPublic = vlanService.createObject(templateObjectPublic);

        List<Vlan> vlans = new ArrayList<Vlan>();
        vlans.add(vlanPrivate);
        vlans.add(vlanPublic);

        scaleGroupEdit.getNetworkVlans().clear();
        scaleGroupEdit.getNetworkVlans().addAll(vlans);

        Gson gson = new Gson();
        System.out.println(gson.toJson(scaleGroupService.editObject(scaleGroupEdit)));

    }

}