Skip to content

Instantly share code, notes, and snippets.

@krames
Last active December 17, 2015 10:28
Show Gist options
  • Select an option

  • Save krames/5594781 to your computer and use it in GitHub Desktop.

Select an option

Save krames/5594781 to your computer and use it in GitHub Desktop.
Concise introduction to the Rackspace Ruby SDK

#Ruby SDK

Rather than reinvent the wheel, Rackspace choose to use the popular fog gem for its SDK. Fog provides both an ActiveRecord like model interface as well as a request interface for efficent access to cloud resources.

Installation

To install Fog via RubyGems run the following command:

gem install fog

Example

This example demonstrates provisioning two next generation servers and displays their corresponding information:

require 'fog'

# Create a connection to the Cloud Servers Service and authenticate
service = Fog::Compute.new({
	:provider            => 'Rackspace',         # Rackspace Fog provider
	:version             => :v2,                 # Use Next Gen Cloud Servers
	:rackspace_region    => :ord                # Defaults to :dfw
})

# pick the first flavor 
flavor1 = service.flavors.first

# pick the first Ubuntu image
image1 = service.images.find {|image| image.name =~ /Ubuntu/}

# create server one
server1 = service.servers.create(:name => "one", 
                                 :flavor_id => flavor1.id, 
                                 :image_id => image1.id)

# pick a 4 GB instance
flavor2 = service.flavors.find {|flavor| flavor.name =~ /4GB/}

# pick the first Windows Server 2012 image
image2 = service.images.find {|image| image.name =~ /Windows Server 2012/}
                            
# create server two
server2 = service.servers.create(:name => "two", 
	    	                 :flavor_id => flavor2.id, 
        	                 :image_id => image2.id)

# The initial server object contains minimal information. 
# Reloading server gives us the most up to date server information
server1.reload
server2.reload

# print out server one details
puts "===[SERVER 1]==================="
server1.attributes.each do |key, value|
	puts "#{key} : #{value}"
end
	
puts "\n\n"

# print out server two details
puts "===[SERVER 2]==================="
server2.attributes.each do |key, value|
	puts "#{key} : #{value}"
end

puts "\n\n"

puts "Please delete these servers via the cloud control panel (https://mycloud.rackspace.com/) to avoid being charged.\n\n"	
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment