class Fog::Baremetal::OpenStack::Real

Attributes

current_tenant[R]
current_user[R]

Public Class Methods

new(options={}) click to toggle source
# File lib/fog/openstack/baremetal.rb, line 247
def initialize(options={})
  @openstack_auth_token = options[:openstack_auth_token]

  unless @openstack_auth_token
    missing_credentials = Array.new
    @openstack_api_key  = options[:openstack_api_key]
    @openstack_username = options[:openstack_username]

    missing_credentials << :openstack_api_key  unless @openstack_api_key
    missing_credentials << :openstack_username unless @openstack_username
    raise ArgumentError, "Missing required arguments: #{missing_credentials.join(', ')}" unless missing_credentials.empty?
  end

  @openstack_tenant               = options[:openstack_tenant]
  @openstack_auth_uri             = URI.parse(options[:openstack_auth_url])
  @openstack_management_url       = options[:openstack_management_url]
  @openstack_must_reauthenticate  = false
  @openstack_service_type         = options[:openstack_service_type] || ['baremetal']
  @openstack_service_name         = options[:openstack_service_name]
  @openstack_endpoint_type        = options[:openstack_endpoint_type] || 'adminURL'
  @openstack_region               = options[:openstack_region]

  @connection_options = options[:connection_options] || {}

  @current_user = options[:current_user]
  @current_tenant = options[:current_tenant]

  authenticate

  @persistent = options[:persistent] || false
  @connection = Fog::Core::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
end

Public Instance Methods

create_chassis(attributes) click to toggle source

Create a new chassis

Attributes ===

description = Free text description of the chassis extra = Record arbitrary key/value metadata. Can be specified multiple times

# File lib/fog/openstack/requests/baremetal/create_chassis.rb, line 10
def create_chassis(attributes)
  desired_options = [
    :description,
    :extra
  ]

  # Filter only allowed creation attributes
  data = attributes.select { |key, value| desired_options.include?(key.to_sym) }

  request(
    :body => Fog::JSON.encode(data),
    :expects => [200,201],
    :method => 'POST',
    :path => 'chassis'
  )
end
create_node(attributes) click to toggle source

Create a new node

Attributes ===

chassis_uuid = UUID of the chassis that this node belongs to driver = Driver used to control the node [REQUIRED] driver_info = Key/value pairs used by the driver, such as out-of-band management credentials. Can be

specified multiple times

extra = Record arbitrary key/value metadata. Can be specified multiple times uuid = Unique UUID for the node properties = Key/value pairs describing the physical characteristics of the node. This is exported to

Nova and used by the scheduler. Can be specified multiple times
# File lib/fog/openstack/requests/baremetal/create_node.rb, line 16
def create_node(attributes)
  desired_options = [
    :chassis_uuid,
    :driver,
    :driver_info,
    :extra,
    :uuid,
    :properties
  ]

  # Filter only allowed creation attributes
  data = attributes.select { |key, value| desired_options.include?(key.to_sym) }

  request(
    :body => Fog::JSON.encode(data),
    :expects => [200,201],
    :method => 'POST',
    :path => 'nodes'
  )
end
create_port(attributes) click to toggle source

Create a new port

Attributes ===

address = MAC Address for this port extra = Record arbitrary key/value metadata. Can be specified multiple times node_uuid = UUID of the node that this port belongs to

# File lib/fog/openstack/requests/baremetal/create_port.rb, line 11
def create_port(attributes)
  desired_options = [
    :address,
    :extra,
    :node_uuid
  ]

  # Filter only allowed creation attributes
  data = attributes.select { |key, value| desired_options.include?(key.to_sym) }

  request(
    :body => Fog::JSON.encode(data),
    :expects => [200,201],
    :method => 'POST',
    :path => 'ports'
  )
end
credentials() click to toggle source
# File lib/fog/openstack/baremetal.rb, line 280
def credentials
  { :provider                 => 'openstack',
    :openstack_auth_url       => @openstack_auth_uri.to_s,
    :openstack_auth_token     => @auth_token,
    :openstack_management_url => @openstack_management_url,
    :current_user             => @current_user,
    :current_tenant           => @current_tenant }
end
delete_chassis(chassis_uuid) click to toggle source
# File lib/fog/openstack/requests/baremetal/delete_chassis.rb, line 5
def delete_chassis(chassis_uuid)
  data = { :chassis_uuid => chassis_uuid }
  request(
      :body => Fog::JSON.encode(data),
      :expects  => [200, 204],
      :method   => 'DELETE',
      :path     => 'chassis'
  )
end
delete_node(node_uuid) click to toggle source
# File lib/fog/openstack/requests/baremetal/delete_node.rb, line 5
def delete_node(node_uuid)
  data = { :node_uuid => node_uuid }
  request(
      :body => Fog::JSON.encode(data),
      :expects  => [200, 204],
      :method   => 'DELETE',
      :path     => 'nodes'
  )
end
delete_port(port_uuid) click to toggle source
# File lib/fog/openstack/requests/baremetal/delete_port.rb, line 5
def delete_port(port_uuid)
  data = { :port_uuid => port_uuid }
  request(
      :body => Fog::JSON.encode(data),
      :expects  => [200, 204],
      :method   => 'DELETE',
      :path     => 'ports'
  )
end
get_chassis(chassis_uuid) click to toggle source
# File lib/fog/openstack/requests/baremetal/get_chassis.rb, line 5
def get_chassis(chassis_uuid)
  request(
      :expects => [200, 204],
      :method  => 'GET',
      :path    => "chassis/#{chassis_uuid}"
  )
end
get_driver(driver_name) click to toggle source
# File lib/fog/openstack/requests/baremetal/get_driver.rb, line 5
def get_driver(driver_name)
  request(
      :expects => [200, 204],
      :method  => 'GET',
      :path    => "drivers/#{driver_name}"
  )
end
get_driver_properties(driver_name) click to toggle source
# File lib/fog/openstack/requests/baremetal/get_driver_properties.rb, line 5
def get_driver_properties(driver_name)
  data = { :driver_name => driver_name }
  request(
      :body => Fog::JSON.encode(data),
      :expects => [200, 204],
      :method  => 'GET',
      :path    => "drivers/properties"
  )
end
get_node(node_id) click to toggle source
# File lib/fog/openstack/requests/baremetal/get_node.rb, line 5
def get_node(node_id)
  request(
      :expects => [200, 204],
      :method  => 'GET',
      :path    => "nodes/#{node_id}"
  )
end
get_port(port_id) click to toggle source
# File lib/fog/openstack/requests/baremetal/get_port.rb, line 5
def get_port(port_id)
  request(
      :expects => [200, 204],
      :method  => 'GET',
      :path    => "ports/#{port_id}"
  )
end
list_chassis(parameters=nil) click to toggle source
# File lib/fog/openstack/requests/baremetal/list_chassis.rb, line 5
def list_chassis(parameters=nil)
  if parameters
    query = parameters.each { |k, v| parameters[k] = URI::encode(v) }
  else
    query = {}
  end

  request(
    :expects => [200, 204],
    :method  => 'GET',
    :path    => 'chassis',
    :query   => query
  )
end
list_chassis_detailed(parameters=nil) click to toggle source
# File lib/fog/openstack/requests/baremetal/list_chassis_detailed.rb, line 5
def list_chassis_detailed(parameters=nil)
  if parameters
    query = parameters.each { |k, v| parameters[k] = URI::encode(v) }
  else
    query = {}
  end

  request(
    :expects => [200, 204],
    :method  => 'GET',
    :path    => 'chassis/detail',
    :query   => query
  )
end
list_drivers() click to toggle source
# File lib/fog/openstack/requests/baremetal/list_drivers.rb, line 5
def list_drivers
  request(
    :expects => [200, 204],
    :method  => 'GET',
    :path    => 'drivers'
  )
end
list_nodes(parameters=nil) click to toggle source
# File lib/fog/openstack/requests/baremetal/list_nodes.rb, line 5
def list_nodes(parameters=nil)
  if parameters
    query = parameters.each { |k, v| parameters[k] = URI::encode(v) }
  else
    query = {}
  end

  request(
    :expects => [200, 204],
    :method  => 'GET',
    :path    => 'nodes',
    :query   => query
  )
end
list_nodes_detailed(parameters=nil) click to toggle source
# File lib/fog/openstack/requests/baremetal/list_nodes_detailed.rb, line 5
def list_nodes_detailed(parameters=nil)
  if parameters
    query = parameters.each { |k, v| parameters[k] = URI::encode(v) }
  else
    query = {}
  end

  request(
    :expects => [200, 204],
    :method  => 'GET',
    :path    => 'nodes/detail',
    :query   => query
  )
end
list_ports(parameters=nil) click to toggle source
# File lib/fog/openstack/requests/baremetal/list_ports.rb, line 5
def list_ports(parameters=nil)
  if parameters
    query = parameters.each { |k, v| parameters[k] = URI::encode(v) }
  else
    query = {}
  end

  request(
    :expects => [200, 204],
    :method  => 'GET',
    :path    => 'ports',
    :query   => query
  )
end
list_ports_detailed(parameters=nil) click to toggle source
# File lib/fog/openstack/requests/baremetal/list_ports_detailed.rb, line 5
def list_ports_detailed(parameters=nil)
  if parameters
    query = parameters.each { |k, v| parameters[k] = URI::encode(v) }
  else
    query = {}
  end

  request(
    :expects => [200, 204],
    :method  => 'GET',
    :path    => 'ports/detail',
    :query   => query
  )
end
patch_chassis(chassis_uuid, patch) click to toggle source

Patch a chassis

parameter example:

{:op=> 'replace', :path => “/extra/placement”, :value => “somewhere”}

Patch parameter, list of jsonpatch ===

op = Operations: 'add', 'replace' or 'remove' path = Attributes to add/replace or remove (only PATH is necessary on remove),

e.g. /extra/placement

value = Value to set

# File lib/fog/openstack/requests/baremetal/patch_chassis.rb, line 15
def patch_chassis(chassis_uuid, patch)
  request(
    :body => Fog::JSON.encode(patch),
    :expects => 200,
    :method => 'PATCH',
    :path => "chassis/#{chassis_uuid}"
  )
end
patch_node(node_uuid, patch) click to toggle source

Patch a node

parameter example:

{:op=> 'replace', :path => “/driver”, :value => “pxe_ssh”}

Patch parameter, list of jsonpatch ===

op = Operations: 'add', 'replace' or 'remove' path = Attributes to add/replace or remove (only PATH is necessary on remove),

e.g. /driver_info/ipmi_address

value = Value to set

# File lib/fog/openstack/requests/baremetal/patch_node.rb, line 15
def patch_node(node_uuid, patch)
  request(
    :body => Fog::JSON.encode(patch),
    :expects => 200,
    :method => 'PATCH',
    :path => "nodes/#{node_uuid}"
  )
end
patch_port(port_uuid, patch) click to toggle source

Patch a port

parameter example:

{:op=> 'replace', :path => “/driver_info/ipmi_address”, :value => “192.0.2.1”}

Patch parameter, list of jsonpatch ===

op = Operations: 'add', 'replace' or 'remove' path = Attributes to add/replace or remove (only PATH is necessary on remove),

e.g. /driver_info/ipmi_address

value = Value to set

# File lib/fog/openstack/requests/baremetal/patch_port.rb, line 15
def patch_port(port_uuid, patch)
  request(
    :body => Fog::JSON.encode(patch),
    :expects => 200,
    :method => 'PATCH',
    :path => "ports/#{port_uuid}"
  )
end
reload() click to toggle source
# File lib/fog/openstack/baremetal.rb, line 289
def reload
  @connection.reset
end
request(params) click to toggle source
# File lib/fog/openstack/baremetal.rb, line 293
def request(params)
  begin
    response = @connection.request(params.merge({
      :headers  => {
        'Content-Type' => 'application/json',
        'X-Auth-Token' => @auth_token
      }.merge!(params[:headers] || {}),
      :path     => "#{@path}/#{params[:path]}"#,
    }))
  rescue Excon::Errors::Unauthorized => error
    if error.response.body != 'Bad username or password' # token expiration
      @openstack_must_reauthenticate = true
      authenticate
      retry
    else # bad credentials
      raise error
    end
  rescue Excon::Errors::HTTPStatusError => error
    raise case error
    when Excon::Errors::NotFound
      Fog::Compute::OpenStack::NotFound.slurp(error)
    else
      error
    end
  end
  unless response.body.empty?
    response.body = Fog::JSON.decode(response.body)
  end
  response
end

Private Instance Methods

authenticate() click to toggle source
# File lib/fog/openstack/baremetal.rb, line 326
def authenticate
  if !@openstack_management_url || @openstack_must_reauthenticate
    options = {
      :openstack_tenant   => @openstack_tenant,
      :openstack_api_key  => @openstack_api_key,
      :openstack_username => @openstack_username,
      :openstack_auth_uri => @openstack_auth_uri,
      :openstack_region   => @openstack_region,
      :openstack_auth_token => @openstack_must_reauthenticate ? nil : @openstack_auth_token,
      :openstack_service_type => @openstack_service_type,
      :openstack_service_name => @openstack_service_name,
      :openstack_endpoint_type => @openstack_endpoint_type
    }

    credentials = Fog::OpenStack.authenticate_v2(options, @connection_options)

    @current_user = credentials[:user]
    @current_tenant = credentials[:tenant]

    @openstack_must_reauthenticate = false
    @auth_token = credentials[:token]
    @openstack_management_url = credentials[:server_management_url]
    uri = URI.parse(@openstack_management_url)
  else
    @auth_token = @openstack_auth_token
    uri = URI.parse(@openstack_management_url)
  end

  @host   = uri.host
  @path   = uri.path
  @path.sub!(/\/$/, '')
  unless @path.match(SUPPORTED_VERSIONS)
    @path = "/" + Fog::OpenStack.get_supported_version(SUPPORTED_VERSIONS,
                                                       uri,
                                                       @auth_token,
                                                       @connection_options)
  end
  @port   = uri.port
  @scheme = uri.scheme
  true
end