Last active
January 1, 2016 09:19
-
-
Save ryanrhanson/8124352 to your computer and use it in GitHub Desktop.
Lists all instances on an account, prompts you to enter an ID for one of them. Creates an image template from this instance, and loops through all templates on an account until it finds one with a matching transaction id.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require './api.rb' | |
require 'date' | |
slapi = AuthApi.new | |
#api.rb and slapi are used in another file to handle my connection to the SoftLayer API. | |
#Feel free to include that information in this file if you wish. | |
begin | |
$acct_service = SoftLayer::Service.new("SoftLayer_Account", | |
:username => slapi.name, | |
:api_key => slapi.key) | |
cci_service = SoftLayer::Service.new("SoftLayer_Virtual_Guest", | |
:username => slapi.name, | |
:api_key => slapi.key) | |
virtlist = $acct_service.getVirtualGuests() | |
virtlist.each do |virt| | |
puts "%s - %s" % [virt["fullyQualifiedDomainName"], virt["id"]] | |
end | |
puts "Enter an ID: " | |
virtId = gets.chomp.to_i | |
puts "You selected %s" % [virtId] | |
now = Time.now.strftime("%d/%m/%Y %H:%M") | |
templateName = "RHanson - #{now}" | |
cci_ref = cci_service.object_with_id(virtId) | |
block_devices = cci_ref.getBlockDevices() | |
cci_ref.createArchiveTransaction( | |
templateName, | |
[{"id" => block_devices[0]['id']}], | |
"Template capture from #{virtId} at #{now}", | |
) | |
activeTxn = cci_ref.getActiveTransaction() | |
txnId = activeTxn["id"] | |
puts "Transaction #{txnId} created!" | |
for i in 1..21 | |
puts "Attempt ##{i}" | |
puts "Sleeping 10 seconds..." | |
sleep(10) | |
templateMask = { | |
"blockDeviceTemplateGroups" => { | |
"id" => "", | |
"name" => "", | |
"note" => "", | |
"transactionId" => "", | |
"createDate" => "", | |
"parent" => "", | |
"globalIdentifier" => "", | |
}, | |
} | |
imageTemplates = $acct_service.object_mask(templateMask).getBlockDeviceTemplateGroups() | |
imageTemplates.each do |template| | |
if template['transactionId'] == txnId | |
puts "=====" | |
puts "Found our template!" | |
puts "ID: %s" % [template['id']] | |
puts "Name: %s" % [template['name']] | |
puts "Note: %s" % [template['note']] | |
puts "Transaction ID: %s" % [template['transactionId']] | |
puts "Creation Date: %s" % [template['createDate']] | |
puts "GUID: %s" % [template['parent']['globalIdentifier']] | |
puts "=====" | |
exit | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment