Class: Deltacloud::Drivers::Gogrid::GogridDriver

Inherits:
BaseDriver
  • Object
show all
Defined in:
lib/deltacloud/drivers/gogrid/gogrid_driver.rb

Instance Method Summary collapse

Methods inherited from BaseDriver

#catched_exceptions_list, declare_feature, define_hardware_profile, define_instance_states, feature, feature_decl_for, feature_decls, #features, features, #filter_hardware_profiles, #filter_on, #find_hardware_profile, #hardware_profile, hardware_profiles, #hardware_profiles, #has_collection?, #image, #instance, #instance_actions_for, instance_state_machine, #instance_state_machine, #realm, #storage_snapshot, #storage_snapshots, #storage_volume, #storage_volumes

Instance Method Details

#create_instance(credentials, image_id, opts = nil) ⇒ Object



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
103
104
105
106
107
108
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 76

def create_instance(credentials, image_id, opts=nil)
  server_ram = nil
  if opts[:hwp_memory]
    mem = opts[:hwp_memory].to_i
    server_ram = (mem == 512) ? "512MB" : "#{mem / 1024}GB"
  else
    server_ram = "512MB"
  end
  client = new_client(credentials)
  name = (opts[:name] && opts[:name]!='') ? opts[:name] : get_random_instance_name
  safely do
    instance = client.request('grid/server/add', {
      'name' => name,
      'image' => image_id,
      'server.ram' => server_ram,
      'ip' => get_free_ip_from_realm(credentials, opts[:realm_id] || '1')
    })['list'].first
    if instance
       = (client, instance[:id])
      if ['username'] and ['password']
        instance['username'] = ['username']
        instance['password'] = ['password']
        inst = convert_instance(instance, credentials.user)
      else
        inst = convert_instance(instance, credentials.user)
        inst.authn_error = "Unable to fetch password"
      end
      return inst
    else
      return nil
    end
  end
end

#destroy_instance(credentials, id) ⇒ Object



162
163
164
165
166
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 162

def destroy_instance(credentials, id)
  safely do
    new_client(credentials).request('grid/server/delete', { 'name' => id})
  end
end

#images(credentials, opts = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 51

def images(credentials, opts=nil)
  imgs = []
  if opts and opts[:id]
    safely do
      imgs = [convert_image(new_client(credentials).request('grid/image/get', { 'id' => opts[:id] })['list'].first)]
    end
  else
    safely do
      imgs = new_client(credentials).request('grid/image/list', { 'state' => 'Available'})['list'].collect do |image|
        convert_image(image, credentials.user)
      end
    end
  end
  imgs = filter_on( imgs, :architecture, opts )
  imgs.sort_by{|e| [e.owner_id, e.description]}
end

#instances(credentials, opts = nil) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 122

def instances(credentials, opts=nil)
  require 'ap'
  instances = []
  if opts and opts[:id]
    begin
      client = new_client(credentials)
      instance = client.request('grid/server/get', { 'name' => opts[:id] })['list'].first
       = (client, instance['id'])
      if ['username'] and ['password']
        instance['username'] = ['username']
        instance['password'] = ['password']
        inst = convert_instance(instance, credentials.user)
      else
        inst = convert_instance(instance, credentials.user)
        inst.authn_error = "Unable to fetch password"
      end
      instances = [inst]
    rescue Exception => e
      if e.message == "400 Bad Request"
        # in the case of a VM that we just made, the grid/server/get method
        # throws a "400 Bad Request error".  In this case we try again by
        # getting a full listing a filtering on the id.  This could
        # potentially take a long time, but I don't see another way to get
        # information about a newly created instance
        instances = list_instances(credentials, opts[:id])
      end
    end
  else
    instances = list_instances(credentials, nil)
  end
  instances = filter_on( instances, :state, opts )
  instances
end

#key(credentials, opts = nil) ⇒ Object



180
181
182
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 180

def key(credentials, opts=nil)
  keys(credentials, opts).first
end

#keys(credentials, opts = nil) ⇒ Object



184
185
186
187
188
189
190
191
192
193
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 184

def keys(credentials, opts=nil)
  gogrid = new_client( credentials )
  creds = []
  safely do
    gogrid.request('support/password/list')['list'].each do |password|
      creds << convert_key(password)
    end
  end
  return creds
end

#list_instances(credentials, id) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 110

def list_instances(credentials, id)
  instances = []
  safely do
    new_client(credentials).request('grid/server/list')['list'].collect do |instance|
      if id.nil? or instance['name'] == id
        instances << convert_instance(instance, credentials.user)
      end
    end
  end
  instances
end

#realms(credentials, opts = nil) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 68

def realms(credentials, opts=nil)
  safely do
    new_client(credentials).request('common/lookup/list', { 'lookup' => 'ip.datacenter' })['list'].collect do |realm|
      convert_realm(realm)
    end
  end
end

#reboot_instance(credentials, id) ⇒ Object



156
157
158
159
160
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 156

def reboot_instance(credentials, id)
  safely do
    new_client(credentials).request('grid/server/power', { 'name' => id, 'power' => 'reboot'})
  end
end

#start_instance(credentials, id) ⇒ Object



174
175
176
177
178
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 174

def start_instance(credentials, id)
  safely do
    new_client(credentials).request('grid/server/power', { 'name' => id, 'power' => 'on'})
  end
end

#stop_instance(credentials, id) ⇒ Object



168
169
170
171
172
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 168

def stop_instance(credentials, id)
  safely do
    new_client(credentials).request('grid/server/power', { 'name' => id, 'power' => 'off'})
  end
end

#supported_collectionsObject



46
47
48
49
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 46

def supported_collections
  DEFAULT_COLLECTIONS.reject! { |c| [ :storage_volumes, :storage_snapshots ].include?(c) }
  DEFAULT_COLLECTIONS + [ :keys ]
end

#valid_credentials?(credentials) ⇒ Boolean

Returns:

  • (Boolean)


195
196
197
198
199
200
201
202
# File 'lib/deltacloud/drivers/gogrid/gogrid_driver.rb', line 195

def valid_credentials?(credentials)
  client = new_client(credentials)
  # FIXME: We need to do this call to determine if
  #        GoGrid is working with given credentials. There is no
  #        other way to check, if given credentials are valid or not.
  return false unless new_client(credentials).request('common/lookup/list', { 'lookup' => 'ip.datacenter' })
  true
end