基于公共映像在Softlayer上创建VM(使用Java API客户端)

时间:2016-05-03 16:44:54

标签: java ibm-cloud-infrastructure

我正在使用Java Client连接软层API。我可以使用以下代码使用操作系统创建一个新的VM。

guest.setHostname("vstest2");
guest.setDomain("softlayer.com");
guest.setStartCpus(2L);
guest.setHourlyBillingFlag(true);
guest.setLocalDiskFlag(true);
guest.setOperatingSystemReferenceCode("UBUNTU_14_64");

但我无法通过已经存在的公众形象来创建新的虚拟机。

guest.setHostname("vstest2");
guest.setDomain("softlayer.com");
guest.setStartCpus(2L);
guest.setHourlyBillingFlag(true);
guest.setLocalDiskFlag(true);

Group blockDevice = new Group();
blockDevice.setGlobalIdentifier("ce3f5ea3-893a-4992-ad14-5bcd99d9b32a");
guest.setBlockDeviceTemplateGroup(blockDevice);

请使用公共图片帮助创建新VM。我得到的错误是

Caused by: com.softlayer.api.ApiException$Internal: Invalid value provided for 'blockDevices'. Block devices may not be provided when using an image template.(code: SoftLayer_Exception_InvalidValue, status: 500)

我只想基于公共图片模板创建一个新的VM。但无法找到办法。

1 个答案:

答案 0 :(得分:0)

我能够使用全局标识符订购VSI: ce3f5ea3-893a-4992-ad14-5bcd99d9b32a

这是我使用的java脚本:

package VirtualGuest;

import com.softlayer.api.ApiClient;
import com.softlayer.api.RestApiClient;
import com.softlayer.api.service.Location;
import com.softlayer.api.service.virtual.Guest;
import com.softlayer.api.service.virtual.guest.block.device.template.Group;

/**
 * Created by Ruber Cuellar on 5/3/2016.
 */
public class CreateObject {

    public CreateObject(){
        // Declare username and api key
        String username = "set me";
        String apiKey = "set me";
        // Get Api Client and service
        ApiClient client = new RestApiClient().withCredentials(username, apiKey);
        Guest.Service guestService = Guest.service(client);

        Guest guest = new Guest();
        guest.setHostname("rcvtest-3");
        guest.setDomain("softlayer.com");
        guest.setStartCpus(2L);
        guest.setHourlyBillingFlag(true);
        guest.setLocalDiskFlag(true);
        guest.setMaxMemory(1L);
        // Setting datacenter
        Location newLocation = new Location();
        newLocation.setName("sjc03");
        guest.setDatacenter(newLocation);
        // Setting image template
        Group blockDevice = new Group();
        blockDevice.setGlobalIdentifier("ce3f5ea3-893a-4992-ad14-5bcd99d9b32a");
        guest.setBlockDeviceTemplateGroup(blockDevice);

    try{
        Guest result = guestService.createObject(guest);
        System.out.println(result.getId());

    } catch (Exception e)
    {
        System.out.println("Error: " + e);
    }
}
    public static void main(String [] args)
    {
        new CreateObject();
    }
}

尝试进行双重检查,或者您能提供您正在尝试的完整代码吗?