Skip to content

Instantly share code, notes, and snippets.

@softlayer
Created June 24, 2010 23:42
Show Gist options
  • Save softlayer/452156 to your computer and use it in GitHub Desktop.
Save softlayer/452156 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
# Order a Cloud Computing Instance based on an existing CCI
#
# Build a SoftLayer_Container_Product_Order_Virtaul_Guest object for a new
# CloudLayer Computing Instance based on an existing CCI and disk template image
# on your account and pass it to the SoftLayer_Product_Order API service to
# order it. See below for more details.
#
# This assumes you're using the SoftLayer API Perl client
# <http://github.com/softlayer/softlayer-api-perl-client>.
#
# Important manual pages:
# http://sldn.softlayer.com/reference/datatypes/SoftLayer_Container_Product_Order_Virtual_Guest
# http://sldn.softlayer.com/reference/datatypes/SoftLayer_Virtual_Guest
# http://sldn.softlayer.com/reference/datatypes/SoftLayer_Product_Item_Price
# http://sldn.softlayer.com/reference/services/SoftLayer_Product_Order/verifyOrder
# http://sldn.softlayer.com/reference/services/SoftLayer_Product_Order/placeOrder
#
# License: http://sldn.softlayer.com/article/License
# Author: SoftLayer Technologies, Inc. <[email protected]>
# Set this to the path of the SoftLayer API Perl client.
use lib '/path/to/client/';
use SoftLayer::API::SOAP;
use strict;
use Data::Dumper;
# Your SoftLayer API username and key.
#
# Generate an API key at the SoftLayer Customer Portal:
# https://manage.softlayer.com/Administrative/apiKeychain
my $apiUsername = 'set me!';
my $apiKey = 'set me too!';
# The id number of the CCI you wish to duplicate.
#
# Grab a list of your account's CCIs by calling the getVirtualGuests() method
# in the SoftLayer_Account API service if you don't know your CCI's id.
my $cciId = 1234;
# The type of CCI you wish to create.
#
# Set this to either "HOURLY" or "MONTHLY" depending on how you'd like to be
# billed for your new CCI.
my $billingType = 'HOURLY';
# The disk template you'd like to install on your CCI.
#
# Your order template will set the physical aspects of your new CCI. It sets
# things like the number of cores, amount of RAM, network speeds, and the like.
# The contents of the CCI are determined by your disk template. If you don't
# know your template id call the getBlockDeviceTemplateGroups() in the
# SoftLayer_Account API service to get your saved templates.
my $imageTemplateId = 1234;
# The id of the datacenter you wish to provision your new CCI in.
#
# Specify the id of the datacenter you want your instance to go in. You can get
# a list of these from the getDatacenters() method in the
# SoftLayer_Location_Datacenter API service or use the following:
# 3: Dallas
# 18171: Seattle
# 37473: Washington DC
my $locationId = 3;
# The hostname and domain names of the new CCIs you wish to order.
#
# You can create multiple CCIs from your instance. Define one hash for each new
# CCI you wish to order.
my @newCcis = (
{
'hostname' => 'testhost',
'domain' => 'example.org'
}
);
# Connect to the SoftLayer_Virtual_Guest API service.
my $client = SoftLayer::API::SOAP->new('SoftLayer_Virtual_Guest', $cciId, $apiUsername, $apiKey);
# Retrieve the order used to create your CCI.
my $orderTemplate = $client->getOrderTemplate($billingType);
# Error out if we couldn't retrieve our order template.
if ($orderTemplate->fault) {
die 'Unable to retrieve order template. ' . $orderTemplate->faultstring;
}
$orderTemplate = $orderTemplate->result;
# Modify our order template with our new CCIs' information.
$orderTemplate->{sourceVirtualGuestId} = undef;
$orderTemplate->{imageTemplateId} = $imageTemplateId;
$orderTemplate->{location} = $locationId;
$orderTemplate->{quantity} = scalar(@newCcis);
$orderTemplate->{virtualGuests} = [ @newCcis ];
# The SoftLayer API's ordering system requires your order's price records
# packaged like a SoftLayer_Item_Price object, but you only have to specify that
# item's id for the ordering system to know what you want. Re-organize your
# order prices into an array of hashes, each with a single 'id' key populated.
my @orderTemplatePrices;
for my $i (0 .. $#{$orderTemplate->{prices}}) {
push(@orderTemplatePrices, bless({
'id' => $orderTemplate->{prices}->[$i]->{id},
}, 'slapi:SoftLayer_Product_Item_Price'));
};
$orderTemplate->{prices} = [ @orderTemplatePrices ];
# Bless the order template as a SoftLayer_Container_Product_Order_Virtual_Guest
# SOAP object. Our ordering system determines what you're trying to order based
# on the type or order container you send to it.
$orderTemplate = bless($orderTemplate, 'slapi:SoftLayer_Container_Product_Order_Virtual_Guest');
# Re-Declare the API client to use the SoftLayer_Product_Order API service.
$client = SoftLayer::API::SOAP->new('SoftLayer_Product_Order', undef, $apiUsername, $apiKey);
# verifyOrder() will check your order for errors. Replace this with a call to
# placeOrder() when you're ready to order. Both calls return a receipt object
# that you can use for your records.
#
# Once your order is placed it'll go through SoftLayer's provisioning process.
# When it's done you'll have a new SoftLayer_Virtual_Guest object and CCI ready
# to use.
my $receipt = $client->verifyOrder($orderTemplate);
if ($receipt->fault) {
die 'There was an error in your order. ' . $receipt->faultstring;
}
# Pretty print the order receipt.
print Dumper($receipt->result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment