Search code examples
javaibm-cloud-infrastructure

Creating a VM on Softlayer based on public image ( using Java API client)


I am using Java Client to connect with soft layer API. I am able to create a new VM with the OS using the below code.

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

But I am unable to create a new VM through already exisitng public image.

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

Please help in creating a new VM by using a public image. The error I got is

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)

I simply want to create a new VM based on the public image template. But unable to find a way to do it.


Solution

  • I was able to order a VSI using the global identifier: ce3f5ea3-893a-4992-ad14-5bcd99d9b32a

    Here the java script that I used:

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

    Try to do double check or can you provide the full code that you are trying, please?