Class: Fog::Compute::Softlayer::Server

Inherits:
Fog::Compute::Server
  • Object
show all
Defined in:
lib/fog/softlayer/models/compute/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Server

Returns a new instance of Server.

Raises:

  • (Exception)


49
50
51
52
53
54
# File 'lib/fog/softlayer/models/compute/server.rb', line 49

def initialize(attributes = {})
  # Forces every request inject bare_metal parameter
  raise Exception if attributes[:collection].nil? and attributes['bare_metal'].nil?
  super(attributes)
  set_defaults
end

Instance Method Details

#add_tags(tags) ⇒ Object

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
# File 'lib/fog/softlayer/models/compute/server.rb', line 56

def add_tags(tags)
  requires :id
  raise ArgumentError, "Tags argument for #{self.class.name}##{__method__} must be Array." unless tags.is_a?(Array)
  tags.each do |tag|
    service.tags.new(:resource_id => self.id, :name => tag).save
  end
  self.reload
  true
end

#bare_metalObject



70
71
72
# File 'lib/fog/softlayer/models/compute/server.rb', line 70

def bare_metal
  @bare_metal
end

#bare_metal?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/fog/softlayer/models/compute/server.rb', line 66

def bare_metal?
  bare_metal
end

#datacenterObject



79
80
81
# File 'lib/fog/softlayer/models/compute/server.rb', line 79

def datacenter
  attributes[:datacenter][:name] unless attributes[:datacenter].nil?
end

#datacenter=(name) ⇒ Object



74
75
76
77
# File 'lib/fog/softlayer/models/compute/server.rb', line 74

def datacenter=(name)
  name = name['name'] if name.is_a?(Hash)
  attributes[:datacenter] = { :name => name }
end

#delete_tags(tags) ⇒ Object

Raises:

  • (ArgumentError)


83
84
85
86
87
88
89
90
91
# File 'lib/fog/softlayer/models/compute/server.rb', line 83

def delete_tags(tags)
  requires :id
  raise ArgumentError, "Tags argument for #{self.class.name}##{__method__} must be Array." unless tags.is_a?(Array)
  tags.each do |tag|
    service.tags.new(:resource_id => self.id, :name => tag).destroy
  end
  self.reload
  true
end

#destroyObject



93
94
95
96
97
98
# File 'lib/fog/softlayer/models/compute/server.rb', line 93

def destroy
  requires :id
  request = bare_metal? ? :delete_bare_metal_server : :delete_vm
  response = service.send(request, self.id)
  response.body
end

#dns_nameObject



100
101
102
# File 'lib/fog/softlayer/models/compute/server.rb', line 100

def dns_name
  fqdn
end

#image_idObject



108
109
110
# File 'lib/fog/softlayer/models/compute/server.rb', line 108

def image_id
  attributes[:image_id][:globalIdentifier] unless attributes[:image_id].nil?
end

#image_id=(uuid) ⇒ Object



104
105
106
# File 'lib/fog/softlayer/models/compute/server.rb', line 104

def image_id=(uuid)
  attributes[:image_id] = {:globalIdentifier => uuid}
end

#key_pairsObject



160
161
162
# File 'lib/fog/softlayer/models/compute/server.rb', line 160

def key_pairs
  attributes[:key_pairs]
end

#key_pairs=(keys) ⇒ Object

Raises:

  • (ArgumentError)


164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/fog/softlayer/models/compute/server.rb', line 164

def key_pairs=(keys)
  raise ArgumentError, "Argument #{local_variables.first.to_s} for #{self.class.name}##{__method__} must be Array." unless keys.is_a?(Array)
  attributes[:key_pairs] = []
  keys.map do |key|
    key = self.symbolize_keys(key) if key.is_a?(Hash)
    unless key.is_a?(Fog::Compute::Softlayer::KeyPair) or (key.is_a?(Hash) and key[:id])
      raise ArgumentError, "Elements of keys array for #{self.class.name}##{__method__} must be a Hash with key 'id', or Fog::Compute::Softlayer::KeyPair"
    end
    key = service.key_pairs.get(key[:id]) unless key.is_a?(Fog::Compute::Softlayer::KeyPair)
    attributes[:key_pairs] << key
  end
end

#nameObject



116
117
118
# File 'lib/fog/softlayer/models/compute/server.rb', line 116

def name
  attributes[:hostname]
end

#name=(set) ⇒ Object



112
113
114
# File 'lib/fog/softlayer/models/compute/server.rb', line 112

def name=(set)
  attributes[:hostname] = set
end

#os_codeObject



144
145
146
# File 'lib/fog/softlayer/models/compute/server.rb', line 144

def os_code
  attributes['operatingSystem']['softwareLicense']['softwareDescription']['referenceCode'] if attributes['operatingSystem']
end

#pre_saveObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/fog/softlayer/models/compute/server.rb', line 120

def pre_save
  extract_flavor
  validate_attributes
  if self.vlan
    attributes[:vlan] = { :networkVlan => { :id => self.vlan.id } }
  end
  if self.private_vlan
    attributes[:private_vlan] = { :networkVlan => { :id => self.private_vlan.id } }
  end
  if self.key_pairs
    attributes[:key_pairs].map! { |key| { :id => key.id } }
  end
  remap_attributes(attributes, attributes_mapping)
  clean_attributes
end

#private_ipObject

maintain backward compatibility with <0.3.13



136
137
138
# File 'lib/fog/softlayer/models/compute/server.rb', line 136

def private_ip # maintain backward compatibility with <0.3.13
  private_ip_address
end

#private_vlanObject



148
149
150
# File 'lib/fog/softlayer/models/compute/server.rb', line 148

def private_vlan
  attributes[:private_vlan] ||= _get_private_vlan
end

#private_vlan=(value) ⇒ Object



152
153
154
155
156
157
158
# File 'lib/fog/softlayer/models/compute/server.rb', line 152

def private_vlan=(value)
  unless value.is_a?(Integer) or value.is_a?(Fog::Network::Softlayer::Network)
    raise ArgumentError, "vlan argument for #{self.class.name}##{__method__} must be Integer or Fog::Network::Softlayer::Network."
  end
  value = Fog::Network[:softlayer].networks.get(value) if value.is_a?(Integer)
  attributes[:private_vlan] = value
end

#public_ipObject

maintain backward compatibility with <0.3.13



140
141
142
# File 'lib/fog/softlayer/models/compute/server.rb', line 140

def public_ip # maintain backward compatibility with <0.3.13
  public_ip_address
end

#ram=(set) ⇒ Object



189
190
191
192
193
194
# File 'lib/fog/softlayer/models/compute/server.rb', line 189

def ram=(set)
  if set.is_a?(Array) and set.first['hardwareComponentModel']
    set = 1024 * set.first['hardwareComponentModel']['capacity'].to_i
  end
  attributes[:ram] = set
end

#ready?Boolean

Returns:

  • (Boolean)


205
206
207
208
209
210
211
# File 'lib/fog/softlayer/models/compute/server.rb', line 205

def ready?
  if bare_metal?
    state == "on"
  else
    state == "Running"
  end
end

#reboot(use_hard_reboot = true) ⇒ Object



213
214
215
# File 'lib/fog/softlayer/models/compute/server.rb', line 213

def reboot(use_hard_reboot = true)
  # TODO: implement
end

#saveObject

Note:

You should use servers.create to create servers instead calling this method directly

Creates server

  • requires attributes: :name, :domain, and :flavor_id OR (:cpu_count && :ram && :disks)

  • State Transitions

    • BUILD -> ACTIVE

    • BUILD -> ERROR (on error)

Raises:

  • (Fog::Errors::Error)


260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/fog/softlayer/models/compute/server.rb', line 260

def save
  raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if persisted?
  copy = self.dup
  copy.pre_save

  data = if bare_metal?
    service.create_bare_metal_server(copy.attributes).body
  else
    service.create_vm(copy.attributes).body.first
  end

  data.delete("bare_metal")
  merge_attributes(data)
  true
end

#shutdownObject



240
241
242
# File 'lib/fog/softlayer/models/compute/server.rb', line 240

def shutdown
  # TODO: implement
end

#snapshotObject



224
225
226
# File 'lib/fog/softlayer/models/compute/server.rb', line 224

def snapshot
  # TODO: implement
end

#ssh_passwordObject



217
218
219
220
221
222
# File 'lib/fog/softlayer/models/compute/server.rb', line 217

def ssh_password
  requires :id
  service_path = bare_metal? ? :hardware_server : :virtual_guest
  @sshpass ||= service.request(service_path, id, :query => 'objectMask=mask[id,operatingSystem.passwords[password]]').body
  @sshpass['operatingSystem']['passwords'][0]['password'] unless @sshpass['operatingSystem'].nil? or @sshpass['operatingSystem']['passwords'].empty?
end

#startObject



228
229
230
231
232
233
234
# File 'lib/fog/softlayer/models/compute/server.rb', line 228

def start
  # TODO: implement

  #requires :identity
  #service.start_server(identity)
  true
end

#stateObject



244
245
246
247
248
249
250
# File 'lib/fog/softlayer/models/compute/server.rb', line 244

def state
  if bare_metal?
    service.request(:hardware_server, "#{id}/getServerPowerState").body
  else
    service.request(:virtual_guest, "#{id}/getPowerState").body['name']
  end
end

#stopObject



236
237
238
# File 'lib/fog/softlayer/models/compute/server.rb', line 236

def stop
  # TODO: implement
end

#tagsObject



276
277
278
# File 'lib/fog/softlayer/models/compute/server.rb', line 276

def tags
  attributes[:tags].map { |i| i['tag']['name'] } if attributes[:tags]
end

#user_dataObject



201
202
203
# File 'lib/fog/softlayer/models/compute/server.rb', line 201

def user_data
  attributes[:user_data]
end

#user_data=(value) ⇒ Object



197
198
199
# File 'lib/fog/softlayer/models/compute/server.rb', line 197

def user_data=(value)
  attributes[:user_data] = [{'value' => value}]
end

#vlanObject



177
178
179
# File 'lib/fog/softlayer/models/compute/server.rb', line 177

def vlan
  attributes[:vlan] ||= _get_vlan
end

#vlan=(value) ⇒ Object



181
182
183
184
185
186
187
# File 'lib/fog/softlayer/models/compute/server.rb', line 181

def vlan=(value)
  unless value.is_a?(Integer) or value.is_a?(Fog::Network::Softlayer::Network)
    raise ArgumentError, "vlan argument for #{self.class.name}##{__method__} must be Integer or Fog::Network::Softlayer::Network."
  end
  value = Fog::Network[:softlayer].networks.get(value) if value.is_a?(Integer)
  attributes[:vlan] = value
end