class CIMI::Model::Machine

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Public Class Methods

attach_volumes(volumes, context) click to toggle source
# File lib/cimi/models/machine.rb, line 101
def self.attach_volumes(volumes, context)
  volumes.each do |vol|
    context.driver.attach_storage_volume(context.credentials,
    {:id=>vol[:volume].name, :instance_id=>context.params[:id], :device=>vol[:attachment_point]})
  end
  self.find(context.params[:id], context)
end
create_entity_metadata(context) click to toggle source
# File lib/cimi/models/machine.rb, line 91
def self.create_entity_metadata(context)
  cimi_entity = self.name.split("::").last
  metadata = CIMI::Model::EntityMetadata.metadata_from_deltacloud_features(cimi_entity, :instances, context)
  unless metadata.includes_attribute?(:name)
    metadata.attributes << {:name=>"name", :required=>"false",
                 :constraints=>"Determined by the cloud provider", :type=>"xs:string"}
  end
  metadata
end
create_from_json(body, context) click to toggle source
# File lib/cimi/models/machine.rb, line 51
def self.create_from_json(body, context)
  json = JSON.parse(body)
  hardware_profile_id = xml['machineTemplate']['machineConfig']["href"].split('/').last
  image_id = xml['machineTemplate']['machineImage']["href"].split('/').last
  instance = context.create_instance(context.credentials, image_id, { :hwp_id => hardware_profile_id })
  from_instance(instance, context)
end
create_from_xml(body, context) click to toggle source
# File lib/cimi/models/machine.rb, line 59
def self.create_from_xml(body, context)
  xml = XmlSimple.xml_in(body)
  machine_template = xml['machineTemplate'][0]
  hardware_profile_id = machine_template['machineConfig'][0]["href"].split('/').last
  image_id = machine_template['machineImage'][0]["href"].split('/').last
  additional_params = {}
  additional_params[:name] =xml['name'][0] if xml['name']
  if machine_template.has_key? 'machineAdmin'
    additional_params[:keyname] = machine_template['machineAdmin'][0]["href"].split('/').last
  end
  instance = context.driver.create_instance(context.credentials, image_id, {
    :hwp_id => hardware_profile_id
  }.merge(additional_params))
  from_instance(instance, context)
end
delete!(id, context) click to toggle source
# File lib/cimi/models/machine.rb, line 87
def self.delete!(id, context)
  context.driver.destroy_instance(context.credentials, id)
end
detach_volumes(volumes, context) click to toggle source
# File lib/cimi/models/machine.rb, line 109
def self.detach_volumes(volumes, context)
  volumes.each do |vol|
    context.driver.detach_storage_volume(context.credentials, {:id=>vol[:volume].name, :instance_id => context.params[:id]})
  end
  self.find(context.params[:id], context)
end
find(id, context) click to toggle source
# File lib/cimi/models/machine.rb, line 39
def self.find(id, context)
  instances = []
  if id == :all
    instances = context.driver.instances(context.credentials)
    instances.map { |instance| from_instance(instance, context) }.compact
  else
    instance = context.driver.instance(context.credentials, :id => id)
    raise CIMI::Model::NotFound unless instance
    from_instance(instance, context)
  end
end

Public Instance Methods

perform(action, context, &block) click to toggle source
# File lib/cimi/models/machine.rb, line 75
def perform(action, context, &block)
  begin
    if context.driver.send(:"#{action.name}_instance", context.credentials, self.name)
      block.callback :success
    else
      raise "Operation failed to execute on given Machine"
    end
  rescue => e
    block.callback :failure, e.message
  end
end