fbpx

softlayer-api-ruby-client で Object Storage を利用する #softlayer

この記事は1年以上前に投稿されました。情報が古い可能性がありますので、ご注意ください。

Object Storage とは

SoftLayer のストレージにはさまざまな種類がありますが、本記事では Object Storage を取り扱います。
以前の記事 でも触れましたが、Object Storage とはデータをオブジェクトとして扱うアーキテクチャです。ファイルツリーでデータを扱うファイルシステムや、 セクタやトラックのブロックとしてデータを扱うブロックストレージとは異なります。Object Storage は比較的安価に大量の非構造化データを扱うことができるため、さまざまな用途への活用が進んでいます。
SoftLayer の Object Storage は OpenStack Swift をベースとしています。

softlayer-api-ruby-client で Object Storage を注文する

Object Storage も softlayer-api-ruby-client を用いて注文が可能です。SoftLayer_Product_Order::placeOrder を用いますが、やはりマジックナンバーが必要です。先の記事のプログラムで Object Storage の Package ID を調べてみました。


:
(中略)
:
{"firstOrderStepId"=>1,
"id"=>206,
"isActive"=>1,
"name"=>"Object Storage",
"unitSize"=>nil},
:
(中略)
:

Object Storage の Package ID は 206 とわかりました。

次に Object Storage の Price ID を確認しましょう。これも 先の記事のプログラムのように確認します。


Required Category "Cloud Storage" -- 74:
16984 -- Object Storage - Pay as you go

これで Object Storage を注文するために必要な ID が揃いました。
なお、SoftLayer の Object Storage は利用したぶんだけ課金される仕組みになっています。

softlayer-api-ruby-client で Object Storage の注文内容を検証する

Object Storage の注文内容は SoftLayer_Container_Product_Order データタイプで構成されています。次のような形になるでしょう。


product_order = {
'complexType' => 'SoftLayer_Container_Product_Order',
'quantity' => 1,
'packageId' => 206, # 206 is 'Object Storage' package ID
'useHourlyPricing' => false,
'prices' => [
{ 'id' => 16984 }, # "Object Storage - Pay as you go"
]
}

先の記事と同様に、SoftLayer_Product_Order::verifyOrder で注文内容を検証しましょう。


#!/usr/bin/ruby

require 'softlayer_api'
require 'pp'

#
# fill your SoftLayer API username and API key
#
api_username = ENV['SOFTLAYER_API_USERNAME'] || 'YOUR_SOFTLAYER_API_USERNAME'
api_key = ENV['SOFTLAYER_API_KEY'] || 'YOUR_SOFTLAYER_API_KEY'

#
# https://sldn.softlayer.com/reference/datatypes/SoftLayer_Container_Product_Order
#
product_order = {
'complexType' => 'SoftLayer_Container_Product_Order',
'quantity' => 1,
'packageId' => 206, # 206 is 'Object Storage' package ID
'useHourlyPricing' => false,
'prices' => [
{ 'id' => 16984 }, # "Object Storage - Pay as you go"
]
}

#
# http://sldn.softlayer.com/reference/services/SoftLayer_Product_Order
#
product_order_service = SoftLayer::Service.new( 'SoftLayer_Product_Order',
:username => api_username,
:api_key => api_key,
)

#
# http://sldn.softlayer.com/reference/services/SoftLayer_Product_Order/verifyOrder
#
begin
result = product_order_service.verifyOrder( product_order )
pp result
puts "The order was verified successfully"
rescue => error_reason
puts "The order could not be verified by the server: #{error_reason}"
exit
end

exit


__END__

これを実行すると次の結果が得られます。


{"bigDataOrderFlag"=>false,
"billingOrderItemId"=>nil,
"containerSplHash"=>"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"currencyShortName"=>"USD",
"extendedHardwareTesting"=>nil,
"imageTemplateId"=>nil,
"isManagedOrder"=>0,
"location"=>"138124",
"locationObject"=>
{"id"=>138124,
"longName"=>"Dallas 5",
"name"=>"dal05",
"activePresaleEvents"=>[]},
"packageId"=>206,
"postTaxRecurring"=>"0",
"postTaxRecurringHourly"=>"0",
"postTaxRecurringMonthly"=>"0",
"postTaxSetup"=>"0",
"preTaxRecurring"=>"0",
"preTaxRecurringHourly"=>"0",
"preTaxRecurringMonthly"=>"0",
"preTaxSetup"=>"0",
"presetId"=>nil,
"prices"=>
[{"currentPriceFlag"=>nil,
"id"=>16984,
"itemId"=>4069,
"laborFee"=>"0",
"onSaleFlag"=>nil,
"oneTimeFee"=>"0",
"oneTimeFeeTax"=>"0",
"proratedRecurringFee"=>"0",
"proratedRecurringFeeTax"=>"0",
"quantity"=>nil,
"recurringFee"=>"0",
"recurringFeeTax"=>"0",
"setupFee"=>"0",
"sort"=>0,
"categories"=>
[{"categoryCode"=>"hub",
"id"=>74,
"name"=>"Cloud Storage",
"quantityLimit"=>1}],
"item"=>
{"capacity"=>"0",
"description"=>"Object Storage - Pay as you go",
"id"=>4069,
"softwareDescriptionId"=>nil,
"units"=>"GIGABYTE",
"upgradeItemId"=>nil,
"bundle"=>
[{"bundleItemId"=>4069,
"id"=>5252,
"itemPriceId"=>16985,
"category"=>
{"categoryCode"=>"hub_bandwidth",
"id"=>75,
"name"=>"Cloud Storage Bandwidth",
"quantityLimit"=>0},
"itemPrice"=>
{"currentPriceFlag"=>nil,
"id"=>16985,
"itemId"=>4070,
"laborFee"=>"0",
"onSaleFlag"=>nil,
"oneTimeFee"=>"0",
"quantity"=>nil,
"recurringFee"=>"0",
"setupFee"=>"0",
"sort"=>0,
"item"=>
{"capacity"=>"0",
"description"=>"Object Storage Bandwidth - Pay as you go",
"id"=>4070,
"softwareDescriptionId"=>nil,
"units"=>"GIGABYTE",
"upgradeItemId"=>nil}}},
{"bundleItemId"=>4069,
"id"=>5275,
"itemPriceId"=>16986,
"category"=>
{"categoryCode"=>"cdn_bandwidth",
"id"=>87,
"name"=>"CDN Bandwidth",
"quantityLimit"=>1},
"itemPrice"=>
{"currentPriceFlag"=>nil,
"id"=>16986,
"itemId"=>4071,
"laborFee"=>"0",
"onSaleFlag"=>nil,
"oneTimeFee"=>"0",
"quantity"=>nil,
"recurringFee"=>"0",
"setupFee"=>"0",
"sort"=>0,
"item"=>
{"capacity"=>"0",
"description"=>"Object Storage CDN Bandwidth - Pay as you go",
"id"=>4071,
"softwareDescriptionId"=>nil,
"units"=>"GB",
"upgradeItemId"=>nil}}}],
"itemCategory"=>
{"categoryCode"=>"hub",
"id"=>74,
"name"=>"Cloud Storage",
"quantityLimit"=>1}}}],
"primaryDiskPartitionId"=>nil,
"privateCloudOrderFlag"=>false,
"proratedInitialCharge"=>"0",
"proratedOrderTotal"=>"0",
"quantity"=>1,
"resourceGroupId"=>nil,
"resourceGroupTemplateId"=>nil,
"sendQuoteEmailFlag"=>nil,
"serverCoreCount"=>nil,
"sourceVirtualGuestId"=>nil,
"sshKeys"=>[],
"stepId"=>nil,
"storageGroups"=>[],
"totalRecurringTax"=>"0",
"totalSetupTax"=>"0",
"useHourlyPricing"=>false}
The order was verified successfully

注文内容が問題ないことがわかります。

softlayer-api-ruby-client で Object Storage を注文する

では、検証した注文内容を実際に注文してみましょう。
次のプログラムになります。


#!/usr/bin/ruby

require 'softlayer_api'
require 'pp'

#
# fill your SoftLayer API username and API key
#
api_username = ENV['SOFTLAYER_API_USERNAME'] || 'YOUR_SOFTLAYER_API_USERNAME'
api_key = ENV['SOFTLAYER_API_KEY'] || 'YOUR_SOFTLAYER_API_KEY'

#
# https://sldn.softlayer.com/reference/datatypes/SoftLayer_Container_Product_Order
#
product_order = {
'complexType' => 'SoftLayer_Container_Product_Order',
'quantity' => 1,
'packageId' => 206, # 206 is 'Object Storage' package ID
'useHourlyPricing' => false,
'prices' => [
{ 'id' => 16984 }, # "Object Storage - Pay as you go"
]
}

#
# http://sldn.softlayer.com/reference/services/SoftLayer_Product_Order
#
product_order_service = SoftLayer::Service.new( 'SoftLayer_Product_Order',
:username => api_username,
:api_key => api_key,
)

#
# http://sldn.softlayer.com/reference/services/SoftLayer_Product_Order/verifyOrder
#
begin
result = product_order_service.verifyOrder( product_order )
puts "The order was verified successfully"
rescue => error_reason
puts "The order could not be verified by the server: #{error_reason}"
exit
end

#
# http://sldn.softlayer.com/reference/services/SoftLayer_Product_Order/placeOrder
#
begin
result = product_order_service.placeOrder( product_order )
puts "The order was placed successfully"
pp result
rescue => error_reason
puts "The order could not be placed by the server: #{error_reason}"
exit
end

exit


__END__

これを実行すると次の結果が得られます。


The order was verified successfully
The order was placed successfully
{"orderDate"=>"2014-06-19T17:30:12+09:00",
"orderDetails"=>
{"bigDataOrderFlag"=>false,
:
(中略)
:
"billingOrderItemId"=>nil,
"containerSplHash"=>"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"currencyShortName"=>"USD",
"extendedHardwareTesting"=>nil,
"imageTemplateId"=>nil,
"isManagedOrder"=>0,
"itemCategoryQuestionAnswers"=>[],
"location"=>"138124",
"locationObject"=>
{"id"=>138124,
"longName"=>"Dallas 5",
"name"=>"dal05",
"activePresaleEvents"=>[]},
"packageId"=>206,
"paymentType"=>"ADD_TO_BALANCE",
"postTaxRecurring"=>"0",
"postTaxRecurringHourly"=>"0",
"postTaxRecurringMonthly"=>"0",
"postTaxSetup"=>"0",
"preTaxRecurring"=>"0",
"preTaxRecurringHourly"=>"0",
"preTaxRecurringMonthly"=>"0",
"preTaxSetup"=>"0",
"presetId"=>nil,
"prices"=>
[{"currentPriceFlag"=>nil,
"id"=>16984,
"itemId"=>4069,
"laborFee"=>"0",
"onSaleFlag"=>nil,
"oneTimeFee"=>"0",
"oneTimeFeeTax"=>"0",
"proratedRecurringFee"=>"0",
"proratedRecurringFeeTax"=>"0",
"quantity"=>nil,
"recurringFee"=>"0",
"recurringFeeTax"=>"0",
"setupFee"=>"0",
"sort"=>0,
"categories"=>
[{"categoryCode"=>"hub",
"id"=>74,
"name"=>"Cloud Storage",
"quantityLimit"=>1}],
"item"=>
{"capacity"=>"0",
"description"=>"Object Storage - Pay as you go",
"id"=>4069,
"softwareDescriptionId"=>nil,
"units"=>"GIGABYTE",
"upgradeItemId"=>nil,
"bundle"=>
[{"bundleItemId"=>4069,
"id"=>5252,
"itemPriceId"=>16985,
"category"=>
{"categoryCode"=>"hub_bandwidth",
"id"=>75,
"name"=>"Cloud Storage Bandwidth",
"quantityLimit"=>0},
"itemPrice"=>
{"currentPriceFlag"=>nil,
"id"=>16985,
"itemId"=>4070,
"laborFee"=>"0",
"onSaleFlag"=>nil,
"oneTimeFee"=>"0",
"quantity"=>nil,
"recurringFee"=>"0",
"setupFee"=>"0",
"sort"=>0,
"categories"=>
[{"categoryCode"=>"hub_bandwidth",
"id"=>75,
"name"=>"Cloud Storage Bandwidth",
"quantityLimit"=>0}],
"item"=>
{"capacity"=>"0",
"description"=>"Object Storage Bandwidth - Pay as you go",
"id"=>4070,
"softwareDescriptionId"=>nil,
"units"=>"GIGABYTE",
"upgradeItemId"=>nil,
"itemCategory"=>
{"categoryCode"=>"hub_bandwidth",
"id"=>75,
"name"=>"Cloud Storage Bandwidth",
"quantityLimit"=>0}}}},
{"bundleItemId"=>4069,
"id"=>5275,
"itemPriceId"=>16986,
"category"=>
{"categoryCode"=>"cdn_bandwidth",
"id"=>87,
"name"=>"CDN Bandwidth",
"quantityLimit"=>1},
"itemPrice"=>
{"currentPriceFlag"=>nil,
"id"=>16986,
"itemId"=>4071,
"laborFee"=>"0",
"onSaleFlag"=>nil,
"oneTimeFee"=>"0",
"quantity"=>nil,
"recurringFee"=>"0",
"setupFee"=>"0",
"sort"=>0,
"categories"=>
[{"categoryCode"=>"cdn_bandwidth",
"id"=>87,
"name"=>"CDN Bandwidth",
"quantityLimit"=>1}],
"item"=>
{"capacity"=>"0",
"description"=>"Object Storage CDN Bandwidth - Pay as you go",
"id"=>4071,
"softwareDescriptionId"=>nil,
"units"=>"GB",
"upgradeItemId"=>nil,
"itemCategory"=>
{"categoryCode"=>"cdn_bandwidth",
"id"=>87,
"name"=>"CDN Bandwidth",
"quantityLimit"=>1}}}}],
"itemCategory"=>
{"categoryCode"=>"hub",
"id"=>74,
"name"=>"Cloud Storage",
"quantityLimit"=>1}}}],
"primaryDiskPartitionId"=>nil,
"privateCloudOrderFlag"=>false,
"properties"=>[],
"proratedInitialCharge"=>"0",
"proratedOrderTotal"=>"0",
"quantity"=>1,
"resourceGroupId"=>nil,
"resourceGroupTemplateId"=>nil,
"sendQuoteEmailFlag"=>nil,
"serverCoreCount"=>nil,
"sourceVirtualGuestId"=>nil,
"sshKeys"=>[],
"stepId"=>nil,
"storageGroups"=>[],
"totalRecurringTax"=>"0",
"totalSetupTax"=>"0",
"useHourlyPricing"=>false},
"orderId"=>XXXXX68,
"placedOrder"=>
{"accountId"=>XXXX78,
"createDate"=>"2014-06-19T17:30:11+09:00",
"id"=>XXXXX68,
"impersonatingUserRecordId"=>nil,
"modifyDate"=>nil,
"orderQuoteId"=>nil,
"orderTypeId"=>4,
"presaleEventId"=>nil,
"privateCloudOrderFlag"=>false,
"status"=>"PENDING_AUTO_APPROVAL",
"userRecordId"=>XXXX42,
"account"=>
:
(中略)
:
"items"=>
[{"categoryCode"=>"hub",
"description"=>"Object Storage - Pay as you go",
"id"=>XXXXXX66,
"itemId"=>4069,
"itemPriceId"=>"16984",
"laborFee"=>"0",
"laborFeeTaxRate"=>"0",
"oneTimeFee"=>"0",
"oneTimeFeeTaxRate"=>"0",
"parentId"=>nil,
"promoCodeId"=>nil,
"quantity"=>nil,
"recurringFee"=>"0",
"setupFee"=>"0",
"setupFeeDeferralMonths"=>12,
"setupFeeTaxRate"=>"0",
"bundledItems"=>
[{"categoryCode"=>"hub_bandwidth",
"description"=>"Object Storage Bandwidth - Pay as you go",
"id"=>XXXXXX68,
"itemId"=>4070,
"itemPriceId"=>"16985",
"laborFee"=>"0",
"laborFeeTaxRate"=>"0",
"oneTimeFee"=>"0",
"oneTimeFeeTaxRate"=>"0",
"parentId"=>XXXXXX66,
"promoCodeId"=>nil,
"quantity"=>nil,
"recurringFee"=>"0",
"setupFee"=>"0",
"setupFeeDeferralMonths"=>12,
"setupFeeTaxRate"=>"0",
"category"=>
{"categoryCode"=>"hub_bandwidth",
"id"=>75,
"name"=>"Cloud Storage Bandwidth",
"quantityLimit"=>0},
"children"=>[],
"order"=>nil,
"storageGroups"=>[]},
{"categoryCode"=>"cdn_bandwidth",
"description"=>"Object Storage CDN Bandwidth - Pay as you go",
"id"=>XXXXXX70,
"itemId"=>4071,
"itemPriceId"=>"16986",
"laborFee"=>"0",
"laborFeeTaxRate"=>"0",
"oneTimeFee"=>"0",
"oneTimeFeeTaxRate"=>"0",
"parentId"=>XXXXXX66,
"promoCodeId"=>nil,
"quantity"=>nil,
"recurringFee"=>"0",
"setupFee"=>"0",
"setupFeeDeferralMonths"=>12,
"setupFeeTaxRate"=>"0",
"category"=>
{"categoryCode"=>"cdn_bandwidth",
"id"=>87,
"name"=>"CDN Bandwidth",
"quantityLimit"=>1},
"children"=>[],
"order"=>nil,
"storageGroups"=>[]}],
"category"=>
{"categoryCode"=>"hub",
"id"=>74,
"name"=>"Cloud Storage",
"quantityLimit"=>1},
"children"=>
[{"categoryCode"=>"hub_bandwidth",
"description"=>"Object Storage Bandwidth - Pay as you go",
"id"=>XXXXXX68,
"itemId"=>4070,
"itemPriceId"=>"16985",
"laborFee"=>"0",
"laborFeeTaxRate"=>"0",
"oneTimeFee"=>"0",
"oneTimeFeeTaxRate"=>"0",
"parentId"=>XXXXXX66,
"promoCodeId"=>nil,
"quantity"=>nil,
"recurringFee"=>"0",
"setupFee"=>"0",
"setupFeeDeferralMonths"=>12,
"setupFeeTaxRate"=>"0",
"category"=>
{"categoryCode"=>"hub_bandwidth",
"id"=>75,
"name"=>"Cloud Storage Bandwidth",
"quantityLimit"=>0},
"children"=>[],
"order"=>nil,
"storageGroups"=>[]},
{"categoryCode"=>"cdn_bandwidth",
"description"=>"Object Storage CDN Bandwidth - Pay as you go",
"id"=>XXXXXX70,
"itemId"=>4071,
"itemPriceId"=>"16986",
"laborFee"=>"0",
"laborFeeTaxRate"=>"0",
"oneTimeFee"=>"0",
"oneTimeFeeTaxRate"=>"0",
"parentId"=>XXXXXXX66,
"promoCodeId"=>nil,
"quantity"=>nil,
"recurringFee"=>"0",
"setupFee"=>"0",
"setupFeeDeferralMonths"=>12,
"setupFeeTaxRate"=>"0",
"category"=>
{"categoryCode"=>"cdn_bandwidth",
"id"=>87,
"name"=>"CDN Bandwidth",
"quantityLimit"=>1},
"children"=>[],
"order"=>nil,
"storageGroups"=>[]}],
"location"=>{"id"=>138124, "longName"=>"Dallas 5", "name"=>"dal05"},
"order"=>nil,
"storageGroups"=>[]},
{"categoryCode"=>"hub_bandwidth",
"description"=>"Object Storage Bandwidth - Pay as you go",
"id"=>XXXXXX68,
"itemId"=>4070,
"itemPriceId"=>"16985",
"laborFee"=>"0",
"laborFeeTaxRate"=>"0",
"oneTimeFee"=>"0",
"oneTimeFeeTaxRate"=>"0",
"parentId"=>XXXXXX66,
"promoCodeId"=>nil,
"quantity"=>nil,
"recurringFee"=>"0",
"setupFee"=>"0",
"setupFeeDeferralMonths"=>12,
"setupFeeTaxRate"=>"0",
"category"=>
{"categoryCode"=>"hub_bandwidth",
"id"=>75,
"name"=>"Cloud Storage Bandwidth",
"quantityLimit"=>0},
"children"=>[],
"order"=>nil,
"storageGroups"=>[]},
{"categoryCode"=>"cdn_bandwidth",
"description"=>"Object Storage CDN Bandwidth - Pay as you go",
"id"=>XXXXXX70,
"itemId"=>4071,
"itemPriceId"=>"16986",
"laborFee"=>"0",
"laborFeeTaxRate"=>"0",
"oneTimeFee"=>"0",
"oneTimeFeeTaxRate"=>"0",
"parentId"=>XXXXXX66,
"promoCodeId"=>nil,
"quantity"=>nil,
"recurringFee"=>"0",
"setupFee"=>"0",
"setupFeeDeferralMonths"=>12,
"setupFeeTaxRate"=>"0",
"category"=>
{"categoryCode"=>"cdn_bandwidth",
"id"=>87,
"name"=>"CDN Bandwidth",
"quantityLimit"=>1},
"children"=>[],
"order"=>nil,
"storageGroups"=>[]}],
"orderTopLevelItems"=>
[{"categoryCode"=>"hub",
"description"=>"Object Storage - Pay as you go",
"id"=>XXXXXX66,
"itemId"=>4069,
"itemPriceId"=>"16984",
"laborFee"=>"0",
"laborFeeTaxRate"=>"0",
"oneTimeFee"=>"0",
"oneTimeFeeTaxRate"=>"0",
"parentId"=>nil,
"promoCodeId"=>nil,
"quantity"=>nil,
"recurringFee"=>"0",
"setupFee"=>"0",
"setupFeeDeferralMonths"=>12,
"setupFeeTaxRate"=>"0",
"bundledItems"=>
[{"categoryCode"=>"hub_bandwidth",
"description"=>"Object Storage Bandwidth - Pay as you go",
"id"=>XXXXXX68,
"itemId"=>4070,
"itemPriceId"=>"16985",
"laborFee"=>"0",
"laborFeeTaxRate"=>"0",
"oneTimeFee"=>"0",
"oneTimeFeeTaxRate"=>"0",
"parentId"=>XXXXXX66,
"promoCodeId"=>nil,
"quantity"=>nil,
"recurringFee"=>"0",
"setupFee"=>"0",
"setupFeeDeferralMonths"=>12,
"setupFeeTaxRate"=>"0",
"category"=>
{"categoryCode"=>"hub_bandwidth",
"id"=>75,
"name"=>"Cloud Storage Bandwidth",
"quantityLimit"=>0},
"children"=>[],
"order"=>nil,
"storageGroups"=>[]},
{"categoryCode"=>"cdn_bandwidth",
"description"=>"Object Storage CDN Bandwidth - Pay as you go",
"id"=>XXXXXX70,
"itemId"=>4071,
"itemPriceId"=>"16986",
"laborFee"=>"0",
"laborFeeTaxRate"=>"0",
"oneTimeFee"=>"0",
"oneTimeFeeTaxRate"=>"0",
"parentId"=>XXXXXX66,
"promoCodeId"=>nil,
"quantity"=>nil,
"recurringFee"=>"0",
"setupFee"=>"0",
"setupFeeDeferralMonths"=>12,
"setupFeeTaxRate"=>"0",
"category"=>
{"categoryCode"=>"cdn_bandwidth",
"id"=>87,
"name"=>"CDN Bandwidth",
"quantityLimit"=>1},
"children"=>[],
"order"=>nil,
"storageGroups"=>[]}],
"category"=>
{"categoryCode"=>"hub",
"id"=>74,
"name"=>"Cloud Storage",
"quantityLimit"=>1},
"children"=>
[{"categoryCode"=>"hub_bandwidth",
"description"=>"Object Storage Bandwidth - Pay as you go",
"id"=>XXXXXX68,
"itemId"=>4070,
"itemPriceId"=>"16985",
"laborFee"=>"0",
"laborFeeTaxRate"=>"0",
"oneTimeFee"=>"0",
"oneTimeFeeTaxRate"=>"0",
"parentId"=>XXXXXX66,
"promoCodeId"=>nil,
"quantity"=>nil,
"recurringFee"=>"0",
"setupFee"=>"0",
"setupFeeDeferralMonths"=>12,
"setupFeeTaxRate"=>"0",
"category"=>
{"categoryCode"=>"hub_bandwidth",
"id"=>75,
"name"=>"Cloud Storage Bandwidth",
"quantityLimit"=>0},
"children"=>[],
"order"=>nil,
"storageGroups"=>[]},
{"categoryCode"=>"cdn_bandwidth",
"description"=>"Object Storage CDN Bandwidth - Pay as you go",
"id"=>XXXXXX70,
"itemId"=>4071,
"itemPriceId"=>"16986",
"laborFee"=>"0",
"laborFeeTaxRate"=>"0",
"oneTimeFee"=>"0",
"oneTimeFeeTaxRate"=>"0",
"parentId"=>XXXXXX66,
"promoCodeId"=>nil,
"quantity"=>nil,
"recurringFee"=>"0",
"setupFee"=>"0",
"setupFeeDeferralMonths"=>12,
"setupFeeTaxRate"=>"0",
"category"=>
{"categoryCode"=>"cdn_bandwidth",
"id"=>87,
"name"=>"CDN Bandwidth",
"quantityLimit"=>1},
"children"=>[],
"order"=>nil,
"storageGroups"=>[]}],
"location"=>{"id"=>138124, "longName"=>"Dallas 5", "name"=>"dal05"},
"order"=>nil,
"storageGroups"=>[]}],
:
(中略)
:

このように Object Storage が注文できました。

softlayer-api-ruby-client で Object Storage を確認する

ポータルの Object Storage から確認することもできますが、softlayer-api-ruby-client からも確認してみましょう。
SoftLayer_Account サービスの SoftLayer_Account::getHubNetworkStorage で Object Storage の一覧が確認できます。


#!/usr/bin/ruby

require 'softlayer_api'
require 'pp'

#
# fill your SoftLayer API username and API key
#
api_username = ENV['SOFTLAYER_API_USERNAME'] || 'YOUR_SOFTLAYER_API_USERNAME'
api_key = ENV['SOFTLAYER_API_KEY'] || 'YOUR_SOFTLAYER_API_KEY'

#
# http://sldn.softlayer.com/reference/services/SoftLayer_Account
#
account_service = SoftLayer::Service.new( 'SoftLayer_Account',
:username => api_username,
:api_key => api_key,
)

#
# result
#
pp account_service.getHubNetworkStorage


__END__

このプログラムを実行すると、次のように先に注文した Object Storage の内容が取得できます。


[{"accountId"=>XXXX78,
"capacityGb"=>5000,
"createDate"=>"2014-06-19T17:30:19+09:00",
"guestId"=>nil,
"hardwareId"=>nil,
"hostId"=>nil,
"id"=>XXXXX96,
"nasType"=>"HUB",
"password"=>"",
"serviceProviderId"=>1,
"upgradableFlag"=>true,
"username"=>"SLOSxxxx78-4",
"properties"=>
[{"createDate"=>"2014-06-19T17:30:55+09:00",
"modifyDate"=>nil,
"value"=>"9762",
"volumeId"=>XXXXX96,
"type"=>
{"description"=>"CDN account associated with this storage record",
"keyname"=>"CDN_ACCOUNT_ID",
"name"=>"CDN Account Id"}},
{"createDate"=>"2014-06-19T17:30:20+09:00",
"modifyDate"=>nil,
"value"=>"AUTH_XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"volumeId"=>XXXXX96,
"type"=>
{"description"=>"Internal identifier used by Swift to track accounts",
"keyname"=>"SWIFT_ACCOUNT_RESOURCE_IDENTIFIER",
"name"=>"Swift Account Resource Identifier"}},
{"createDate"=>"2014-06-19T17:30:20+09:00",
"modifyDate"=>nil,
"value"=>"SLxxxx78",
"volumeId"=>XXXXX96,
"type"=>
{"description"=>"swift account master username",
"keyname"=>"MASTER_USERNAME",
"name"=>"masterUsername"}},
{"createDate"=>"2014-06-19T17:30:21+09:00",
"modifyDate"=>nil,
"value"=>
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"volumeId"=>XXXXX96,
"type"=>
{"description"=>"swift account master password",
"keyname"=>"MASTER_PASSWORD",
"name"=>"masterPassword"}},
{"createDate"=>"2014-06-19T17:30:25+09:00",
"modifyDate"=>nil,
"value"=>"612",
"volumeId"=>XXXXX96,
"type"=>
{"description"=>
"The id of other Swift cluster resource records this account is associated with",
"keyname"=>"SWIFT_CLUSTER_STORAGE_RESOURCE",
"name"=>"Swift Cluster Storage Resource"}},
{"createDate"=>"2014-06-19T17:30:26+09:00",
"modifyDate"=>nil,
"value"=>"611",
"volumeId"=>XXXXX96,
"type"=>
{"description"=>
"The id of other Swift cluster resource records this account is associated with",
"keyname"=>"SWIFT_CLUSTER_STORAGE_RESOURCE",
"name"=>"Swift Cluster Storage Resource"}},
{"createDate"=>"2014-06-19T17:30:22+09:00",
"modifyDate"=>nil,
"value"=>"600",
"volumeId"=>XXXXX96,
"type"=>
{"description"=>
"The id of other Swift cluster resource records this account is associated with",
"keyname"=>"SWIFT_CLUSTER_STORAGE_RESOURCE",
"name"=>"Swift Cluster Storage Resource"}}],
"serviceResourceBackendIpAddress"=>
"https://dal05.objectstorage.service.networklayer.com/auth/v1.0/",
"serviceResourceName"=>"OBJECT_STORAGE_DAL05",
"vendorName"=>"Swift"}]

Object Storage を Ruby プログラムから利用する

注文した Object Storage はさまざまなプログラミング言語から利用できますが、ここでは Ruby プログラムから利用する softlayer-object-storage-ruby を用いてみましょう。

2014年6月25日現在、softlayer-object-storage-ruby は正式にリリースされておらず、またバグが含まれているので、次の手順で SoftLayer 公式 ではなく フォーク から取得してインストールしてください。


% git clone https://github.com/cl-lab-k/softlayer-object-storage-ruby
Cloning into 'softlayer-object-storage-ruby'...
remote: Reusing existing pack: 48, done.
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 52 (delta 0), reused 2 (delta 0)
Unpacking objects: 100% (52/52), done.
Checking connectivity... done.
%

% cd softlayer-object-storage-ruby
%

% gem build softlayer-object-storage.gemspec
WARNING: no homepage specified
WARNING: description and summary are identical
Successfully built RubyGem
Name: softlayer-object-storage
Version: 0.0.1
File: softlayer-object-storage-0.0.1.gem
%


% gem install softlayer-object-storage-0.0.1.gem --no-rdoc --no-ri
Installing gem softlayer-object-storage-0.0.1
Successfully installed softlayer-object-storage-0.0.1
1 gem installed
%

では、softlayer-object-storage-ruby を利用したサンプルプログラムを作成してみます。
動作の流れとしては、次のようにします。

  1. コンテナの一覧を表示する (SoftLayer/ObjectStorage/Connection:containers)
  2. コンテナを追加する (SoftLayer/ObjectStorage/Connection#create_container)
  3. 再度、コンテナの一覧を表示する。
  4. オブジェクト(疑似ディレクトリ: Pseudo-hierarchical folders and directories) を作成する (SoftLayer/ObjectStorage/Container#create_object, SoftLayer/ObjectStorage/StorageObject#write)
  5. オブジェクトの一覧を表示する (SoftLayer/ObjectStorage/Container#objects)
  6. 指定したファイルをオブジェクトとしてアップロードする (SoftLayer/ObjectStorage/StorageObject#load_from_filename)
  7. 再度、オブジェクトの一覧を表示する。
  8. アップロードしたファイルをダウンロードする (SoftLayer/ObjectStorage/StorageObject#save_to_filename)
  9. アップロードしたファイルとダウンロードしたファイルを比較する (fileutils/FileUtils#cmp)

なお、対象となる Object Storage のユーザ名を指定しておく必要があります。ここでは username で示されている SLOSxxxx78-4 がそれに当たります。
また、プログラムに引数として与える適当なファイルを準備してください。


#!/usr/bin/ruby

require 'softlayer/object_storage'
require 'fileutils'
require 'pp'

#
# fill your SoftLayer API username and API key
#
api_username = ENV['SOFTLAYER_API_USERNAME'] || 'YOUR_SOFTLAYER_API_USERNAME'
api_key = ENV['SOFTLAYER_API_KEY'] || 'YOUR_SOFTLAYER_API_KEY'

#
# fill your SoftLayer Object Storage username
#
object_storage_username = ENV['SOFTLAYER_OBJECT_STORAGE_USERNAME'] || 'YOUR_SOFTLAYER_OBJECT_STORAGE_USERNAME'

obj_str_conn = SoftLayer::ObjectStorage::Connection.new( {
:username => object_storage_username + ':' + api_username,
:api_key => api_key,
:datacenter => :dal05 # Dallas
} )

#
# specify file
#
file = ARGV.shift

#
# list container
#
# http://rdoc.info/github/softlayer/softlayer-object-storage-ruby/master/SoftLayer/ObjectStorage/Connection:containers
#
print 'list container: '
pp obj_str_conn.list_containers

#
# create container
#
# http://rdoc.info/github/softlayer/softlayer-object-storage-ruby/master/SoftLayer/ObjectStorage/Connection#create_container-instance_method
#
container = obj_str_conn.create_container( 'MyContainer' )
print 'create container: '
pp container
print 'create container: '
pp obj_str_conn.list_containers

#
# create (pseudo) directory
#
# http://docs.openstack.org/api/openstack-object-storage/1.0/content/pseudo-hierarchical-folders-directories.html
#
# http://rdoc.info/github/softlayer/softlayer-object-storage-ruby/master/SoftLayer/ObjectStorage/Container#create_object-instance_method
# http://rdoc.info/github/softlayer/softlayer-object-storage-ruby/master/SoftLayer/ObjectStorage/StorageObject#write-instance_method
#
obj_str_dir = container.create_object( 'tmp' )
obj_str_dir.write( nil, 'content-type' => 'application/directory' )
print 'create object: '
pp obj_str_dir

#
# list objects
#
# http://rdoc.info/github/softlayer/softlayer-object-storage-ruby/master/SoftLayer/ObjectStorage/Container#objects-instance_method
#
print 'list objects: '
pp container.objects
print 'list objects: '
pp container.objects( :limit => 1 )
print 'list objects: '
pp container.objects( :path => 'tmp' )

#
# upload file
#
# http://rdoc.info/github/softlayer/softlayer-object-storage-ruby/master/SoftLayer/ObjectStorage/StorageObject#load_from_filename-instance_method
#
obj_str_up = container.create_object( 'tmp/up_' + File.basename( file ) )
obj_str_up.load_from_filename( file )
print 'upload file: '
pp obj_str_up

#
# list object (again)
#
print 'list objects: '
pp container.objects
print 'list objects: '
pp container.objects( :limit => 1 )
print 'list objects: '
pp container.objects( :path => 'tmp' )

#
# download uploaded file
#
# http://rdoc.info/github/softlayer/softlayer-object-storage-ruby/master/SoftLayer/ObjectStorage/StorageObject#save_to_filename-instance_method
#
obj_str_dl = container.object( 'tmp/up_' + File.basename( file ) )
obj_str_dl.save_to_filename( '/tmp/dl_' + File.basename( file ) )
print 'download file: '
pp obj_str_dl

#
# compare file
#
print 'compare file: '
pp FileUtils.cmp( file, '/tmp/dl_' + File.basename( file ) )

exit


__END__

このプログラムを実行すると、次のような結果となります。


list container: ["Container"]
create container: MyContainer
create container: ["Container", "MyContainer"]
create object: tmp
list objects: ["tmp"]
list objects: ["tmp"]
list objects: ["tmp"]
upload file: tmp/up_cl-sl.png
list objects: ["tmp", "tmp/up_cl-sl.png"]
list objects: ["tmp"]
list objects: ["tmp", "tmp/up_cl-sl.png"]
download file: tmp/up_cl-sl.png
compare file: true

Object Storage にコンテナやオブジェクトを作成し、ファイルをアップロード、ダウンロード、そして比較して問題のないことが確認できました。

まとめ

Object Storage は大量の非構造化データを安価に堅牢に扱うことのできるストレージです。
是非活用してみてください。

なお、先に述べた通り、softlayer-object-storage-ruby は2014年6月25日現在正式にリリースされておらず、本記事では検証できない項目も多数ありましたが、引き続き修正を働きかけていきます。

Author

Chef・Docker・Mirantis製品などの技術要素に加えて、会議の進め方・文章の書き方などの業務改善にも取り組んでいます。「Chef活用ガイド」共著のほか、Debian Official Developerもやっています。

Daisuke Higuchiの記事一覧

新規CTA