是否有用于在SoftLayer中订购性能块存储的示例代码?

时间:2015-11-19 19:15:16

标签: php api ibm-cloud-infrastructure

我有一个PHP代码,我以前称为VerifyOrder用于Endurance Block存储,它可以正常工作。我原以为它只适用于性能块存储,只需更改价格ID即可。但它似乎并不那么简单。我从SoftLayer看到它使用以下价格ID:40678,40688,40798 所以我将上面的价格ID列表和包ID替换为222。 但我最终得到这样的错误:

PHP Fatal error:  Uncaught exception 'Exception' with message 'There was an error querying the SoftLayer API: Invalid pr
ice Block Storage (Performance) (40678) provided on the order container.'

我不知道为什么。 在SoftLayer中是否有任何验证性能块存储订单顺序的良好工作样本?

以下是我对Endurance存储的基本代码流程。

$priceIds = array (
        45064,
        45104,
        45074,
        45124,
        46156 
);

$orderPrices = array();
foreach ($priceIds as $priceId){
    $price = new stdClass();
    $price->id = $priceId;
    $orderPrices[] = $price;
}   

$osFormatType = new stdClass();
$osFormatType->id = 12;
$osFormatType->keyName = 'LINUX';

$orderTemplate = new stdClass();
$orderTemplate->location     = $location;
$orderTemplate->packageId    = $packageId;
$orderTemplate->osFormatType = $osFormatType;
$orderTemplate->complexType  = 'SoftLayer_Container_Product_Order_Network_Storage_Enterprise';
$orderTemplate->prices       = $orderPrices;
$orderTemplate->quantity     = 1;

$productOrderClient = SoftLayer_SoapClient::getClient
(
        'SoftLayer_Product_Order',
        null,
        $apiUsername,
        $apiKey
);

$my_template = new SoapVar($orderTemplate, SOAP_ENC_OBJECT, 'SoftLayer_Container_Product_Order_Network_Storage_Enterprise', 'http://api.service.softlayer.com/soap/v3/');

$result = $productOrderClient->verifyOrder($my_template);

1 个答案:

答案 0 :(得分:1)

要订购Performance Block Storage,请使用包222.以下是一个示例:

<?php
require_once dirname(__FILE__) . "/SoftLayer/SoapClient.class.php";

$username = "set me";
$apiKey = "set me";

$location = 449506; // Frankfurt 2
$packageId = 222; // The packageID for order Performance
$quantity = 1;

// Create a SoftLayer API client object for "SoftLayer_Product_Order" service
$client = SoftLayer_SoapClient::getClient('SoftLayer_Product_Order', null, $username, $apiKey);

/**
 * Build a skeleton SoftLayer_Product_Item_Price objects. These objects contain
 * much more than ids, but SoftLayer's ordering system only needs the price's id
 * to know what you want to order.
 * To get the list of valid prices for the package
 * use the SoftLayer_Product_Package:getItemPrices method
 */
$prices = array(
    40672, // Block Storage (Performance)
    62875, // 500 GB Storage Space
    61509 // 500 IOPS
);

$orderPrices = array();
foreach ($prices as $priceId) {
    $price = new stdClass();
    $price -> id = $priceId;
    $orderPrices[] = $price;
}

$osFormatType = new stdClass();
$osFormatType -> id = 12;
$osFormatType -> keyName = "LINUX";

// Build a SoftLayer_Container_Product_Order_Network_Storage_Enterprise object containing
// the order you want to place.
$orderContainer = new stdClass();
$orderContainer -> location = $location;
$orderContainer -> packageId = $packageId;
$orderContainer -> prices = $orderPrices;
$orderContainer -> quantity = $quantity;
$orderContainer -> osFormatType = $osFormatType;

// Re-declare the order template as a SOAP variable, so the SoftLayer ordering system knows what type of order you're placing.
$orderTemplate = new SoapVar($orderContainer, SOAP_ENC_OBJECT, 'SoftLayer_Container_Product_Order_Network_PerformanceStorage_Iscsi', 'http://api.service.softlayer.com/soap/v3/');

try {
    /**
     * We will use verifyOrder() method to check for errors. Replace this with placeOrder() when you are ready to order.
     * @see http://sldn.softlayer.com/reference/services/SoftLayer_Product_Order/verifyOrder
     */
    $result = $client -> verifyOrder($orderTemplate);
    print_r($result);
} catch(Exception $e) {
    echo "Unable to order Consistent Performance Block Storage" . $e -> getMessage();
}

为您的帐户获取有效的价格ID:

https://[username]:[apikey]@api.softlayer.com/rest/v3/SoftLayer_Product_Package/ 222/getItemPrices?objectMask=mask[id,item[keyName,description],pricingLocationGroup[locations[id, name, longName]]]

注意:具有locationGroupId = null的price id被视为“标准价格”,API将在内部切换客户的价格。但我们建议首先执行verifyOrder,以查看所需订单是否正常(费用可能会有所不同)。 此外,您可以查看: http://sldn.softlayer.com/blog/cmporter/Location-based-Pricing-and-You