class Fog::Compute::IBM::Mock
Public Class Methods
data()
click to toggle source
# File lib/fog/ibm/compute.rb, line 83 def self.data @data ||= Hash.new do |hash, key| hash[key] = { :instances => {}, :images => populate_images, :keys => {}, :locations => populate_locations, :private_keys => {}, :addresses => {} } end end
new(options={})
click to toggle source
# File lib/fog/ibm/compute.rb, line 109 def initialize(options={}) @ibm_username = options[:ibm_username] @ibm_password = options[:ibm_password] @data = self.class.data[@ibm_username] end
populate_images()
click to toggle source
# File lib/fog/ibm/compute.rb, line 115 def self.populate_images images = {} images["20010001"] = { "name"=>"SUSE Linux Enterprise Server 11 SP1 for x86", "manifest"=>"https://www-147.ibm.com/cloud/enterprise/ram.ws/RAMSecure/artifact/{6CD09CE4-E99B-D72F-6C29-233C9B2A1676}/1.0/parameters.xml", "state"=>1, "visibility"=>"PUBLIC", "owner"=>"SYSTEM", "platform"=>"SUSE Linux Enterprise Server/11 SP1", "location"=>"41", "createdTime"=>1282466781000, "supportedInstanceTypes"=> [{"detail"=>"Copper - 32 bit (vCPU: 1, RAM: 2 GiB, Disk: 60 GiB)", "label"=>"Copper 32 bit", "price"=>{"rate"=>0.095, "unitOfMeasure"=>"UHR ", "countryCode"=>"897", "effectiveDate"=>-1, "currencyCode"=>"USD", "pricePerQuantity"=>1}, "id"=>"COP32.1/2048/60"}, {"detail"=>"Bronze - 32 bit (vCPU: 1, RAM: 2 GiB, Disk: 235 GiB)", "label"=>"Bronze 32 bit", "price"=>{"rate"=>0.115, "unitOfMeasure"=>"UHR ", "countryCode"=>"897", "effectiveDate"=>-1, "currencyCode"=>"USD", "pricePerQuantity"=>1}, "id"=>"BRZ32.1/2048/60*175"}, {"detail"=>"Silver - 32 bit (vCPU: 2, RAM: 4 GiB, Disk: 410 GiB)", "label"=>"Silver 32 bit", "price"=>{"rate"=>0.2, "unitOfMeasure"=>"UHR ", "countryCode"=>"897", "effectiveDate"=>-1, "currencyCode"=>"USD", "pricePerQuantity"=>1}, "id"=>"SLV32.2/4096/60*350"}, {"detail"=>"Gold - 32 bit (vCPU: 4, RAM: 4 GiB, Disk: 410 GiB)", "label"=>"Gold 32 bit", "price"=>{"rate"=>0.33, "unitOfMeasure"=>"UHR ", "countryCode"=>"897", "effectiveDate"=>-1, "currencyCode"=>"USD", "pricePerQuantity"=>1}, "id"=>"GLD32.4/4096/60*350"}], "productCodes"=>["rtpSr7dKs9ARDmuPy6WPgV"], "documentation"=>"https://www-147.ibm.com/cloud/enterprise/ram.ws/RAMSecure/artifact/{6CD09CE4-E99B-D72F-6C29-233C9B2A1676}/1.0/GettingStarted.html", "id"=>"20010001", "description"=>"Suse Linux 32 bit" } images end
populate_locations()
click to toggle source
# File lib/fog/ibm/compute.rb, line 151 def self.populate_locations locations = {} locations["41"] = { "state"=>1, "location"=>"RTP", "capabilities"=>[ {"entries"=>{"EXT3"=>["ext3"], "RAW"=>["raw"]}, "id"=>"oss.storage.format"}, {"entries"=>{}, "id"=>"oss.instance.spec.i386"}, {"entries"=>{}, "id"=>"oss.instance.spec.x86_64"}, {"entries"=>{}, "id"=>"oss.storage.availabilityarea"}], "name"=>"Raleigh, U.S.A", "id"=>"41", "description"=>"This data center is located in Raleigh, North Carolina, U.S.A. The services provided are: Guest Instances, Image Capture, Persistent Storage, Reserved IP, Private VLAN/VPN." } locations["61"] = { "state"=>1, "location"=>"EHN", "capabilities"=>[ {"entries"=>{"EXT3"=>["ext3"], "RAW"=>["raw"]}, "id"=>"oss.storage.format"}, {"entries"=>{}, "id"=>"oss.instance.spec.i386"}, {"entries"=>{}, "id"=>"oss.instance.spec.x86_64"}, {"entries"=>{}, "id"=>"oss.storage.availabilityarea"}], "name"=>"Ehningen, Germany", "id"=>"61", "description"=>"This data center is located in Ehningen(near Baden-Wurttemberg), Germany. The services provided are: Guest Instances, Image Capture, Persistent Storage, Reserved IP, Private VLAN/VPN." } locations["82"] = { "state"=>1, "location"=>"us-co-dc1", "capabilities"=>[ {"entries"=>{"EXT3"=>["ext3"], "RAW"=>["raw"]}, "id"=>"oss.storage.format"}, {"entries"=>{}, "id"=>"oss.instance.spec.i386"}, {"entries"=>{}, "id"=>"oss.instance.spec.x86_64"}, {"entries"=>{}, "id"=>"oss.storage.availabilityarea"}], "name"=>"Boulder1, U.S.A", "id"=>"82", "description"=>"This data center is located in Boulder(near Denver), Colorado, U.S.A. The services provided are: Guest Instances, Image Capture, Persistent Storage, Reserved IP, Private VLAN/VPN." } locations["101"] = { "state"=>1, "location"=>"ca-on-dc1", "capabilities"=>[ {"entries"=>{"EXT3"=>["ext3"], "RAW"=>["raw"]}, "id"=>"oss.storage.format"}, {"entries"=>{}, "id"=>"oss.instance.spec.i386"}, {"entries"=>{}, "id"=>"oss.instance.spec.x86_64"}, {"entries"=>{}, "id"=>"oss.storage.availabilityarea"}], "name"=>"Markham, Canada", "id"=>"101", "description"=>"This data center is located in Markham(near Toronto), Ontario, Canada. The services provided are: Guest Instances, Image Capture, Persistent Storage, Reserved IP, Private VLAN/VPN." } locations["121"] = { "state"=>1, "location"=>"ap-jp-dc1", "capabilities"=>[ {"entries"=>{"EXT3"=>["ext3"], "RAW"=>["raw"]}, "id"=>"oss.storage.format"}, {"entries"=>{}, "id"=>"oss.instance.spec.i386"}, {"entries"=>{}, "id"=>"oss.instance.spec.x86_64"}, {"entries"=>{}, "id"=>"oss.storage.availabilityarea"}], "name"=>"Makuhari, Japan", "id"=>"121", "description"=>"This data center is located in Makuhari(near Tokoyo), Japan. The services provided are: Guest Instances, Image Capture, Persistent Storage, Reserved IP, Private VLAN/VPN." } locations["141"] = { "state"=>1, "location"=>"ap-sg-dc1", "capabilities"=>[ {"entries"=>{"EXT3"=>["ext3"], "RAW"=>["raw"]}, "id"=>"oss.storage.format"}, {"entries"=>{}, "id"=>"oss.instance.spec.i386"}, {"entries"=>{}, "id"=>"oss.instance.spec.x86_64"}, {"entries"=>{}, "id"=>"oss.storage.availabilityarea"}], "name"=>"Singapore, Singapore", "id"=>"141", "description"=>"This data center is located in Singapore. The services provided are: Guest Instances, Image Capture, Persistent Storage, Reserved IP, Private VLAN/VPN." } locations end
reset()
click to toggle source
# File lib/fog/ibm/compute.rb, line 96 def self.reset @data = nil end
Public Instance Methods
activate_instance(instance_id)
click to toggle source
Sets instance status to Active if it's not already set.
# File lib/fog/ibm/requests/compute/get_instance.rb, line 61 def activate_instance(instance_id) self.data[:instances][instance_id]["status"] = 5 unless instance_active? instance_id end
address_exists?(address_id)
click to toggle source
# File lib/fog/ibm/requests/compute/delete_address.rb, line 36 def address_exists?(address_id) self.data[:addresses].key? address_id end
clone_image(image_id, name, description)
click to toggle source
# File lib/fog/ibm/requests/compute/clone_image.rb, line 29 def clone_image(image_id, name, description) response = Excon::Response.new if image_exists? image_id id = Fog::IBM::Mock.instance_id self.data[:images][id] = self.data[:images][image_id].merge('id' => id, 'name' => name, 'description' => description) response.status = 200 response.body = { "ImageID" => id } else response.status = 404 end response end
create_address(location_id, offering_id="20001223", options={})
click to toggle source
# File lib/fog/ibm/requests/compute/create_address.rb, line 35 def create_address(location_id, offering_id="20001223", options={}) address = Fog::IBM::Mock.create_address(location_id, offering_id, options) self.data[:addresses][address['id']] = address response = Excon::Response.new response.status = 200 response.body = address response end
create_image(instance_id, name, description)
click to toggle source
# File lib/fog/ibm/requests/compute/create_image.rb, line 37 def create_image(instance_id, name, description) response = Excon::Response.new if instance_exists? instance_id image = Fog::IBM::Mock.private_image(name, description) self.data[:images][image["id"]] = image response.status = 200 response.body = image else response.status = 404 end response end
create_instance(name, image_id, instance_type, location, options={})
click to toggle source
# File lib/fog/ibm/requests/compute/create_instance.rb, line 62 def create_instance(name, image_id, instance_type, location, options={}) response = Excon::Response.new # Since we want to test error conditions, we have a little regex that traps specially formed # instance type strings. case name when /FAIL:\ (\d{3})/ response.status = $1.to_i raise Excon::Errors.status_error({:expects => 200}, response) else instance = Fog::IBM::Mock.create_instance(name, image_id, instance_type, location, options) self.data[:instances][instance['id']] = instance response.status = 200 response.body = {"instances" => [ instance ]} response end end
create_key(name, public_key=nil)
click to toggle source
SmartCloud returns the private key when create_key
is called We need to store both the private and public key for later use
# File lib/fog/ibm/requests/compute/create_key.rb, line 34 def create_key(name, public_key=nil) response = Excon::Response.new response.status = 200 attributes = { "keyName" => name, "lastModifiedTime" => Fog::IBM::Mock.launch_time, "default" => false, "instanceIds" => [], } if public_key.nil? private_key = Fog::IBM::Mock.key_material public_key = private_key.public_key response.body = attributes.merge("keyMaterial" => private_key.to_s) else response.body = { 'success' => true } end self.data[:keys][name] = attributes.merge("keyMaterial" => public_key.to_s) self.data[:private_keys][name] = attributes.merge("keyMaterial" => private_key.to_s) response end
data()
click to toggle source
# File lib/fog/ibm/compute.rb, line 100 def data self.class.data[@ibm_username] end
delete_address(address_id)
click to toggle source
# File lib/fog/ibm/requests/compute/delete_address.rb, line 24 def delete_address(address_id) response = Excon::Response.new if address_exists? address_id self.data[:addresses].delete address_id response.status = 200 response.body = { "success" => true } else response.status = 404 end response end
delete_image(image_id)
click to toggle source
# File lib/fog/ibm/requests/compute/delete_image.rb, line 23 def delete_image(image_id) response = Excon::Response.new # TODO: We should probably check that an image is deleteable. # i.e. that the user has appropriate permissions if image_exists? image_id self.data[:images].delete image_id response.status = 200 response.body = {"success"=>true} else response.status = 404 end response end
delete_instance(instance_id)
click to toggle source
# File lib/fog/ibm/requests/compute/delete_instance.rb, line 24 def delete_instance(instance_id) response = Excon::Response.new if deleteable? instance_id # remove from memoreeeez. self.data[:instances].delete instance_id response.body = { 'success' => true } response.status = 200 else # TODO: we should really return a 412 if the instance is in an invalid state, and a 404 if it doesn't exist. response.status = 404 end response end
delete_key(key_name)
click to toggle source
# File lib/fog/ibm/requests/compute/delete_key.rb, line 23 def delete_key(key_name) response = Excon::Response.new if key_exists? key_name self.data[:keys].delete(key_name) response.status = 200 response.body = {"success"=>true} else response.status = 404 end response end
deleteable?(instance_id)
click to toggle source
we can't delete the instance if it doesn't exist, or is in an invalid state.
# File lib/fog/ibm/requests/compute/delete_instance.rb, line 39 def deleteable?(instance_id) return false unless instance_exists? instance_id instance = self.data[:instances][instance_id] return false if [0, 1, 7, 14, 15].include?(instance["status"].to_i) true end
get_image(image_id)
click to toggle source
# File lib/fog/ibm/requests/compute/get_image.rb, line 44 def get_image(image_id) response = Excon::Response.new if image_exists? image_id response.status = 200 response.body = self.data[:images][image_id] else response.status = 404 end response end
get_image_agreement(image_id)
click to toggle source
TODO: Fix this so they work.
# File lib/fog/ibm/requests/compute/get_image_agreement.rb, line 30 def get_image_agreement(image_id) response = Excon::Response.new response.status = 200 response.body = {"text"=> "test, operating system is SUSE Linux Enterprise Server/11 SP1 - English\n\nYour access to and use of the Service, including all selected options, are governed by the terms of the Agreement signed between your Enterprise and IBM. Each Service is also governed by one or more Attachments (including, for example, Image Terms Attachments). Applicable Attachments are part of the Agreement between you and IBM and include Attachments for Services you acquire after the Agreement was signed. The Agreement also references applicable IBM and third party end user license agreements that govern the use of IBM software and operating system software provided as part of an Image.\n\nYou are responsible for complying with the terms of the Agreement (including applicable Attachments) and applicable license agreements. You may review the terms for the Service by 1) obtaining information regarding the Agreement from your Account Administrator and 2) accessing the Asset Catalog to review specific Image Terms and end user license agreements for IBM and third party software provided as part of an Image. ", "id"=>"20020159", "attachments"=> [{"label"=>"Service Description for Developement & Test Service", "url"=> "https://www-147.ibm.com/cloud/enterprise/static/internal_user_agreement.pdf", "type"=>0}, {"label"=>"Smart Business on the IBM Public Cloud Agreement", "url"=> "https://www-147.ibm.com/cloud/enterprise/static/internal_user_agreement.pdf", "type"=>1}, {"label"=> "End User License for SUSE 10.2 Linux Enterprise Server software", "url"=> "https://www.novell.com/licensing/eula/sles_10/sles_10_english.pdf", "type"=>2}, {"label"=> "End User License for SUSE 11.0 Linux Enterprise Server software", "url"=>"https://www.novell.com/licensing/eula/sles_11/sles_11_en.pdf", "type"=>2}, {"label"=>"End User License for RedHat Linux RHEL software", "url"=>"https://www.redhat.com/licenses/", "type"=>2}]} response end
get_image_manifest(image_id)
click to toggle source
TODO: Create a data store for this.
# File lib/fog/ibm/requests/compute/get_image_manifest.rb, line 25 def get_image_manifest(image_id) response = Excon::Response.new response.status = 200 response.body = {"manifest"=> "<?xml version=\"1.0\" encoding=\"UTF-8\"?><parameters xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"platform:/resource/com.ibm.ccl.devcloud.client/schema/parameters.xsd\">\n\t<firewall>\n\t\t<rule>\n\t\t\t<source>0.0.0.0/0</source>\n\t\t\t<minport>1</minport>\n\t\t\t<maxport>65535</maxport>\n\t\t</rule>\n\t</firewall>\n</parameters>"} response end
get_instance(instance_id)
click to toggle source
# File lib/fog/ibm/requests/compute/get_instance.rb, line 43 def get_instance(instance_id) response = Excon::Response.new if instance_exists? instance_id activate_instance(instance_id) # Set it to Active if it's not running response.status = 200 response.body = self.data[:instances][instance_id] else raise Fog::Compute::IBM::NotFound end response end
get_key(key_name)
click to toggle source
# File lib/fog/ibm/requests/compute/get_key.rb, line 28 def get_key(key_name) response = Excon::Response.new if key_exists? key_name response.status = 200 response.body = self.data[:keys][key_name] else response.status = 404 end response end
get_location(location_id)
click to toggle source
# File lib/fog/ibm/requests/compute/get_location.rb, line 34 def get_location(location_id) response = Excon::Response.new if location_exists? location_id response.status = 200 response.body = self.data[:locations][location_id] else response.status = 404 end response end
get_request(request_id)
click to toggle source
# File lib/fog/ibm/requests/compute/get_request.rb, line 44 def get_request(request_id) response = Excon::Response.new response.status = 200 response.body = {"instances"=> [{"name"=>"test from fog", "location"=>"101", "keyName"=>"mykey", "primaryIP"=> {"type"=>0, "ip"=>"42.42.42.42 ", "hostname"=>"42.42.42.42 "}, "productCodes"=>[], "requestId"=>"75364", "imageId"=>"20020159", "launchTime"=>1304012220770, "id"=>"75064", "volumes"=>[], "instanceType"=>"SLV32.2/4096/60*350", "requestName"=>"test from fog", "secondaryIP"=>[], "status"=>1, "software"=> [{"name"=>"SUSE Linux Enterprise Server", "type"=>"OS", "version"=>"11 SP1"}], "expirationTime"=>1367084229205, "owner"=>"user@example.com"}]} response end
instance_active?(instance_id)
click to toggle source
Checks if an instance is Active
# File lib/fog/ibm/requests/compute/get_instance.rb, line 66 def instance_active?(instance_id) self.data[:instances][instance_id]["status"] == 5 end
instance_exists?(instance_id)
click to toggle source
Checks if an instance exists
# File lib/fog/ibm/requests/compute/get_instance.rb, line 56 def instance_exists?(instance_id) self.data[:instances].key? instance_id end
key_exists?(name)
click to toggle source
# File lib/fog/ibm/requests/compute/get_key.rb, line 39 def key_exists?(name) self.data[:keys].key? name end
list_address_offerings()
click to toggle source
# File lib/fog/ibm/requests/compute/list_address_offerings.rb, line 35 def list_address_offerings response = Excon::Response.new response.status = 200 response.body = {"addresses"=> [{"price"=> {"pricePerQuantity"=>1, "effectiveDate"=>1302566400000, "rate"=>0.01, "countryCode"=>"897", "unitOfMeasure"=>"UHR", "currencyCode"=>"USD"}, "location"=>"101", "ipType"=>0, "id"=>"20001223"}]} response end
list_addresses()
click to toggle source
# File lib/fog/ibm/requests/compute/list_addresses.rb, line 32 def list_addresses # Loop through addresses and update states and values if they aren't set self.data[:addresses].values.each do |address| address['state'] = 2 if address['state'] == 0 address['ip'] = Fog::IBM::Mock.ip_address if address['ip'].empty? address['mode'] = 0 unless address.key? 'mode' address['hostname'] = Fog::IBM::Mock.hostname unless address.key? 'hostname' address['type'] = 1 unless address.key? 'type' end response = Excon::Response.new response.status = 200 response.body = { 'addresses' => self.data[:addresses].values } response end
list_images()
click to toggle source
# File lib/fog/ibm/requests/compute/list_images.rb, line 45 def list_images response = Excon::Response.new response.status = 200 response.body = {'images' => self.data[:images].values} response end
list_instances()
click to toggle source
# File lib/fog/ibm/requests/compute/list_instances.rb, line 44 def list_instances response = Excon::Response.new response.status = 200 response.body = { 'instances' => self.data[:instances].values } response end
list_keys()
click to toggle source
# File lib/fog/ibm/requests/compute/list_keys.rb, line 44 def list_keys response = Excon::Response.new response.status = 200 response.body = {'keys' => self.data[:keys].values} response end
list_locations()
click to toggle source
# File lib/fog/ibm/requests/compute/list_locations.rb, line 24 def list_locations response = Excon::Response.new response.status = 200 response.body = { "locations" => self.data[:locations].values } response end
list_vlans()
click to toggle source
# File lib/fog/ibm/requests/compute/list_vlans.rb, line 31 def list_vlans response = Excon::Response.new response.status = 200 response.body = {"vlan"=> [{"location"=>"101", "id"=>"75321", "name"=>"FOG-VLAN1"}]} response end
location_exists?(location_id)
click to toggle source
# File lib/fog/ibm/requests/compute/get_location.rb, line 45 def location_exists?(location_id) self.data[:locations].key? location_id end
modify_instance(instance_id, params={})
click to toggle source
# File lib/fog/ibm/requests/compute/modify_instance.rb, line 37 def modify_instance(instance_id, params={}) response = Excon::Response.new if instance_exists? instance_id if params['state'] == 'restart' self.data[:instances][instance_id]["status"] = "8" response.status = 200 response.body = { "success" => true } elsif params['type'] == 'attach' || params['type'] == 'detach' if Fog::Storage[:ibm].volume_exists?(params['volume_id']) # TODO: Update the instance in the data hash, assuming IBM ever gets this feature working properly. response.status = 415 else response.status = 404 end elsif params['name'] self.data[:instances][instance_id]["name"] = params['name'] response.status = 200 response.body = { "success" => true } elsif params['expirationTime'] self.data[:instances][instance_id]["expirationTime"] = params['expirationTime'] response.status = 200 response.body = { 'expirationTime' => params['expirationTime'] } end else response.status = 404 end response end
modify_key(key_name, params={})
click to toggle source
# File lib/fog/ibm/requests/compute/modify_key.rb, line 25 def modify_key(key_name, params={}) response = Excon::Response.new if key_exists? key_name if params['public_key'] self.data[:keys][key_name]['keyMaterial'] = public_key self.data[:keys][key_name]['lastModifiedTime'] = Fog::IBM::Mock.launch_time end if params['default'] self.data[:keys].values.each do |key| key['default'] = false end self.data[:keys][key_name]['default'] = true end response.status = 200 response.body = {"success"=>true} else response.status = 404 end response end
reset_data()
click to toggle source
# File lib/fog/ibm/compute.rb, line 104 def reset_data self.class.data.delete(@ibm_username) @data = self.class.data[@ibm_username] end
Private Instance Methods
image_exists?(image_id)
click to toggle source
# File lib/fog/ibm/requests/compute/get_image.rb, line 57 def image_exists?(image_id) self.data[:images].key? image_id end