Class: Deltacloud::Drivers::Sbc::SbcDriver

Inherits:
BaseDriver
  • Object
show all
Defined in:
lib/deltacloud/drivers/sbc/sbc_driver.rb

Overview

31 January 2011

Constant Summary

Constants inherited from BaseDriver

BaseDriver::MEMBER_SHOW_METHODS, BaseDriver::STATE_MACHINE_OPTS

Instance Method Summary collapse

Methods inherited from BaseDriver

#address, #api_provider, #blob, #bucket, #catched_exceptions_list, #configured_providers, constraints, define_hardware_profile, define_instance_states, driver_name, feature, features, #filter_hardware_profiles, #filter_on, #find_hardware_profile, #firewall, #hardware_profile, #hardware_profiles, hardware_profiles, #has_capability?, #has_feature?, has_feature?, #image, #instance, #instance_actions_for, #instance_state_machine, instance_state_machine, #key, #name, #realm, #storage_snapshot, #storage_volume, #supported_collections

Methods included from Exceptions

exception_from_status, exceptions, included, logger, #safely

Instance Method Details

#create_instance(credentials, image_id, opts = {}) ⇒ Object

Creates an instance



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/deltacloud/drivers/sbc/sbc_driver.rb', line 73

def create_instance(credentials, image_id, opts={})
  sbc_client = new_client(credentials)

  # Copy opts to body; keywords are mapped later
  body = opts.dup
  body.delete('image_id')
  body.delete('hwp_id')
  body.delete('realm_id')

  # Lookup image if nil; tries to avoids extra lookup
  if @last_image.nil? || @last_image['id'] != opts[:image_id]
    @last_image = sbc_client.list_images(image_id).map[0]
  end

  # Map DeltaCloud keywords to SBC
  body['imageID'] = opts[:image_id]
  body['location'] = opts[:realm_id] || @last_image['location']
  if opts[:hwp_id]
    body['instanceType'] = opts[:hwp_id].gsub('-', '/')
  else
    body['instanceType'] = @last_image['supportedInstanceTypes'][0]['id']
  end

  if not body['name']
    body['name'] = Time.now.to_i.to_s
  end

  # Submit instance, parse response
  convert_instance(sbc_client.create_instance(body).map[0])
end

#destroy_instance(credentials, instance_id) ⇒ Object

Destroys an instance



124
125
126
127
128
# File 'lib/deltacloud/drivers/sbc/sbc_driver.rb', line 124

def destroy_instance(credentials, instance_id)
  sbc_client = new_client(credentials)
  sbc_client.delete_instance(instance_id)
  instance(credentials, instance_id)
end

#images(credentials, opts = {}) ⇒ Object

Retrieves images



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/deltacloud/drivers/sbc/sbc_driver.rb', line 32

def images(credentials, opts={})
  sbc_client = new_client(credentials)
  opts ||= {}
  images = []
  images = sbc_client.list_images(opts[:id]).map do |image|
    # Cache image for create_instance; hwp is image-specific. In the
    # current flow of the server, images is always called before a
    # create_instance, making this caching profitable
    @last_image = image
    convert_image(image)
  end
  images = filter_on(images, :architecture, opts)
  images = filter_on(images, :owner_id, opts)
  images
end

#instances(credentials, opts = {}) ⇒ Object

Retrieves instances



61
62
63
64
65
66
67
68
# File 'lib/deltacloud/drivers/sbc/sbc_driver.rb', line 61

def instances(credentials, opts={})
  sbc_client = new_client(credentials)
  opts ||= {}
  instances = []
  instances = sbc_client.list_instances(opts[:id]).map do |instance|
    convert_instance(instance)
  end
end

#realms(credentials, opts = {}) ⇒ Object

Retrieves realms



51
52
53
54
55
56
# File 'lib/deltacloud/drivers/sbc/sbc_driver.rb', line 51

def realms(credentials, opts={})
  sbc_client = new_client(credentials)
  doc = sbc_client.list_locations
  realms = doc.xpath('ns2:DescribeLocationsResponse/Location').map { |loc| convert_location(loc) }
  realms = filter_on(realms, :id, opts)
end

#reboot_instance(credentials, instance_id) ⇒ Object

Reboots an instance



107
108
109
110
111
# File 'lib/deltacloud/drivers/sbc/sbc_driver.rb', line 107

def reboot_instance(credentials, instance_id)
  sbc_client = new_client(credentials)
  sbc_client.reboot_instance(instance_id)
  instance(credentials, instance_id)
end

#stop_instance(credentials, instance_id) ⇒ Object

Stops an instance



116
117
118
119
# File 'lib/deltacloud/drivers/sbc/sbc_driver.rb', line 116

def stop_instance(credentials, instance_id)
  # Stop not supported; rebooting
  reboot_instance(credentials, instance_id)
end