class ProfitBricks::Server

Server class

Public Class Methods

create(datacenter_id, options = {}) click to toggle source

Create a new server.

# File lib/profitbricks/server.rb, line 124
def create(datacenter_id, options = {})
  entities = {}

  # Retrieve volumes collection if present and generate appropriate JSON.
  if options.key?(:volumes)
    entities[:volumes] = collect_entities(options.delete(:volumes))
  end

  # Retrieve nics collection if present and generate appropriate JSON.
  if options.key?(:nics)
    entities[:nics] = collect_entities(options.delete(:nics))
  end

  response = ProfitBricks.request(
    method: :post,
    path: "/datacenters/#{datacenter_id}/servers",
    expects: 202,
    body: { properties: options, entities: entities }.to_json
  )
  add_parent_identities(response)
  instantiate_objects(response)
end
get(datacenter_id, server_id) click to toggle source
# File lib/profitbricks/server.rb, line 158
def get(datacenter_id, server_id)
  response = ProfitBricks.request(
    method: :get,
    path: "/datacenters/#{datacenter_id}/servers/#{server_id}",
    expects: 200
  )
  add_parent_identities(response)
  instantiate_objects(response)
end
list(datacenter_id) click to toggle source

List all servers by datacenter.

# File lib/profitbricks/server.rb, line 148
def list(datacenter_id)
  response = ProfitBricks.request(
    method: :get,
    path: "/datacenters/#{datacenter_id}/servers",
    expects: 200
  )
  add_parent_identities(response)
  instantiate_objects(response)
end

Private Class Methods

collect_entities(entities) click to toggle source
# File lib/profitbricks/server.rb, line 171
def self.collect_entities(entities)
  if entities.is_a?(Array) && entities.length > 0
    items = []
    entities.each do |entity|
      if entity.key?(:firewallrules)
        subentities = collect_entities(entity.delete(:firewallrules))
        items << {
          properties: entity,
          entities: { firewallrules: subentities }
        }
      else
        items << { properties: entity }
      end
    end
    { items: items }
  end
end

Public Instance Methods

attach_cdrom(cdrom_id) click to toggle source
# File lib/profitbricks/server.rb, line 85
def attach_cdrom(cdrom_id)
  response = ProfitBricks.request(
    method: :post,
    path: "/datacenters/#{datacenterId}/servers/#{id}/cdroms",
    expects: 202,
    body: { id: cdrom_id }.to_json
  )
  self.class.instantiate_objects(response)
end
attach_volume(volume_id) click to toggle source

Attach volume to server.

# File lib/profitbricks/server.rb, line 56
def attach_volume(volume_id)
  volume = ProfitBricks::Volume.get(datacenterId, nil, volume_id)
  volume.attach(id)
end
create_nic(options = {}) click to toggle source

Create server NIC.

# File lib/profitbricks/server.rb, line 114
def create_nic(options = {})
  ProfitBricks::NIC.create(datacenterId, id, options)
end
delete() click to toggle source

Delete the server.

# File lib/profitbricks/server.rb, line 5
def delete
  response = ProfitBricks.request(
    method: :delete,
    path: "/datacenters/#{datacenterId}/servers/#{id}",
    expects: 202
  )
  self.requestId = response[:requestId]
  self
end
detach_cdrom(cdrom_id) click to toggle source
# File lib/profitbricks/server.rb, line 95
def detach_cdrom(cdrom_id)
  ProfitBricks.request(
    method: :delete,
    path: "/datacenters/#{datacenterId}/servers/#{id}/cdroms/#{cdrom_id}",
    expects: 202
  )
end
detach_volume(volume_id) click to toggle source

Detach volume from server.

# File lib/profitbricks/server.rb, line 62
def detach_volume(volume_id)
  volume = ProfitBricks::Volume.get(datacenterId, nil, volume_id)
  volume.detach(id)
end
get_cdrom(cdrom_id) click to toggle source
# File lib/profitbricks/server.rb, line 76
def get_cdrom(cdrom_id)
  response = ProfitBricks.request(
    method: :get,
    path: "/datacenters/#{datacenterId}/servers/#{id}/cdroms/#{cdrom_id}",
    expects: 200
  )
  self.class.instantiate_objects(response)
end
get_nic(nic_id) click to toggle source

Retrieve server NIC.

# File lib/profitbricks/server.rb, line 109
def get_nic(nic_id)
  ProfitBricks::NIC.get(datacenterId, id, nic_id)
end
Also aliased as: nic
get_volume(volume_id) click to toggle source

Retrieve server volume.

# File lib/profitbricks/server.rb, line 51
def get_volume(volume_id)
  ProfitBricks::Volume.get(datacenterId, id, volume_id)
end
list_cdroms() click to toggle source
# File lib/profitbricks/server.rb, line 67
def list_cdroms
  response = ProfitBricks.request(
    method: :get,
    path: "/datacenters/#{datacenterId}/servers/#{id}/cdroms",
    expects: 200
  )
  self.class.instantiate_objects(response)
end
list_nics() click to toggle source

List server NICs.

# File lib/profitbricks/server.rb, line 104
def list_nics
  ProfitBricks::NIC.list(datacenterId, id)
end
Also aliased as: nics
list_volumes() click to toggle source

List server volumes.

# File lib/profitbricks/server.rb, line 46
def list_volumes
  ProfitBricks::Volume.list(datacenterId, id)
end
nic(nic_id)
Alias for: get_nic
nics()
Alias for: list_nics
reboot() click to toggle source

Reboot the server.

# File lib/profitbricks/server.rb, line 41
def reboot
  server_control('reboot')
end
start() click to toggle source

Start the server.

# File lib/profitbricks/server.rb, line 31
def start
  server_control('start')
end
stop() click to toggle source

Stop the server.

# File lib/profitbricks/server.rb, line 36
def stop
  server_control('stop')
end
update(options = {}) click to toggle source

Update the server.

# File lib/profitbricks/server.rb, line 16
def update(options = {})
  response = ProfitBricks.request(
    method: :patch,
    path: "/datacenters/#{datacenterId}/servers/#{id}",
    expects: 202,
    body: options.to_json
  )
  if response
    self.requestId = response['requestId']
    @properties = @properties.merge(response['properties'])
  end
  self
end

Private Instance Methods

server_control(operation) click to toggle source
# File lib/profitbricks/server.rb, line 189
def server_control(operation)
  ProfitBricks.request(
    method: :post,
    path: "/datacenters/#{datacenterId}/servers/#{id}/#{operation}",
    headers: { 'Content-Type' => 'application/x-www-form-urlencoded' },
    expects: 202
  )
end