Class: CIMI::Model::Machine

Inherits:
Base show all
Defined in:
lib/cimi/models/machine.rb

Overview

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.

Constant Summary

Constants included from Deltacloud::Helpers::Database

Deltacloud::Helpers::Database::DATABASE_COLLECTIONS

Constants inherited from Resource

Resource::CMWG_NAMESPACE

Instance Attribute Summary

Attributes inherited from Resource

#attribute_values

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#[]=, #destroy, #extract_properties!, #initialize, #ref_id, #save

Methods included from Deltacloud::Helpers::Database

#current_db, #current_provider, #provides?

Methods included from Deltacloud::Helpers::Drivers

#driver, #driver_class, #driver_class_name, #driver_name, #driver_source_name, #driver_symbol, included, #provider_name

Methods included from CollectionMethods

#acts_as_root_entity, #all, #collection_class, #collection_class=, #list

Methods included from Helpers::SelectBaseMethods

#select_attributes

Methods inherited from Resource

#[], #[]=, add_attributes!, all_uri, #base_id, base_schema, base_schema_cloned?, clone_base_schema, from_json, from_xml, inherited, #initialize, parse, #prepare, required_attributes, resource_uri, to_json, #to_json, to_xml, #to_xml, #validate!, xml_tag_name

Methods included from Schema::DSL

#array, #collection, #hash, #href, #ref, #resource_attr, #scalar, #struct, #text

Methods included from Helpers::FilterResourceMethods

#filter_by, #parse_filter_opts

Methods included from Helpers::SelectResourceMethods

#select_by, #select_by_arr_index, #select_by_arr_range

Constructor Details

This class inherits a constructor from CIMI::Model::Base

Class Method Details

.attach_volume(volume, location, context) ⇒ Object

returns the newly attach machine_volume



71
72
73
74
75
# File 'lib/cimi/models/machine.rb', line 71

def self.attach_volume(volume, location, context)
  context.driver.attach_storage_volume(context.credentials,
   {:id=>volume, :instance_id=>context.params[:id], :device=>location})
  CIMI::Model::MachineVolume.find(context.params[:id], context, volume)
end

.convert_instance_actions(instance, context) ⇒ Object



149
150
151
152
153
154
155
156
157
158
# File 'lib/cimi/models/machine.rb', line 149

def self.convert_instance_actions(instance, context)
  actions = instance.actions.collect do |action|
    action = :restart if action == :reboot
    name = action
    name = :delete if action == :destroy # In CIMI destroy operation become delete
    { :href => context.send(:"#{action}_machine_url", instance.id), :rel => "http://schemas.dmtf.org/cimi/1/action/#{name}" }
  end
  actions <<  { :href => context.send(:"machine_images_url"), :rel => "http://schemas.dmtf.org/cimi/1/action/capture" } if instance.can_create_image?
  actions
end

.convert_instance_addresses(instance) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/cimi/models/machine.rb', line 136

def self.convert_instance_addresses(instance)
  (instance.public_addresses + instance.private_addresses).collect do |address|
    {
      :hostname => address.is_hostname? ? address : nil,
      :mac_address => address.is_mac? ? address : nil,
      :state => 'Active',
      :protocol => 'IPv4',
      :address => address.is_ipv4? ? address : nil,
      :allocation => 'Static'
    }
  end
end

.convert_instance_cpu(profile, context) ⇒ Object



121
122
123
124
125
126
127
128
# File 'lib/cimi/models/machine.rb', line 121

def self.convert_instance_cpu(profile, context)
  cpu_override = profile.overrides.find { |p, v| p == :cpu }
  if cpu_override.nil?
    CIMI::Model::MachineConfiguration.find(profile.id, context).cpu
  else
    cpu_override[1]
  end
end

.convert_instance_memory(profile, context) ⇒ Object



130
131
132
133
134
# File 'lib/cimi/models/machine.rb', line 130

def self.convert_instance_memory(profile, context)
  machine_conf = CIMI::Model::MachineConfiguration.find(profile.name, context)
  memory_override = profile.overrides.find { |p, v| p == :memory }
  memory_override.nil? ? machine_conf.memory.to_i : context.to_kibibyte(memory_override[1].to_i,"MB")
end

.convert_instance_state(state) ⇒ Object

FIXME: This will convert ‘RUNNING’ state to ‘STARTED’ which is defined in CIMI (p65)



113
114
115
116
117
118
119
# File 'lib/cimi/models/machine.rb', line 113

def self.convert_instance_state(state)
  case state
    when "RUNNING" then "STARTED"
    when "PENDING" then "CREATING" #aruba only exception... could be "STARTING" here
    else state
  end
end

.convert_storage_volumes(instance, context) ⇒ Object



160
161
162
163
164
# File 'lib/cimi/models/machine.rb', line 160

def self.convert_storage_volumes(instance, context)
  instance.storage_volumes ||= [] #deal with nilpointers
  instance.storage_volumes.map{|vol| {:href=>context.volume_url(vol.keys.first),
                                     :initial_location=>vol.values.first} }
end

.delete!(id, context) ⇒ Object



65
66
67
68
# File 'lib/cimi/models/machine.rb', line 65

def self.delete!(id, context)
  context.driver.destroy_instance(context.credentials, id)
  new(:id => id).destroy
end

.detach_volume(volume, location, context) ⇒ Object

returns the machine_volume_collection for the given machine



78
79
80
81
82
# File 'lib/cimi/models/machine.rb', line 78

def self.detach_volume(volume, location, context)
  context.driver.detach_storage_volume(context.credentials,
   {:id=>volume, :instance_id=>context.params[:id], :device=>location})
  CIMI::Model::MachineVolume.collection_for_instance(context.params[:id], context)
end

.find(id, context) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cimi/models/machine.rb', line 41

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

.from_instance(instance, context) ⇒ Object



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/cimi/models/machine.rb', line 84

def self.from_instance(instance, context)
  cpu =  memory = (instance.instance_profile.id == "opaque")? "n/a" : nil
  machine_conf = CIMI::Model::MachineConfiguration.find(instance.instance_profile.name, context)
  machine_spec = {
    :name => instance.name,
    :created => instance.launch_time.nil? ? Time.now.xmlschema : Time.parse(instance.launch_time.to_s).xmlschema,
    :description => "No description set for Machine #{instance.name}",
    :id => context.machine_url(instance.id),
    :state => convert_instance_state(instance.state),
    :cpu => cpu || convert_instance_cpu(instance.instance_profile, context),
    :memory => memory || convert_instance_memory(instance.instance_profile, context),
    :disks => { :href => context.machine_url(instance.id)+"/disks"},
    :volumes => { :href=>context.machine_url(instance.id)+"/volumes"},
    :operations => convert_instance_actions(instance, context)
  }
  if context.expand? :disks
    machine_spec[:disks] = CIMI::Model::Disk.find(instance, machine_conf, context, :all)
  end
  if context.expand? :volumes
    machine_spec[:volumes] = CIMI::Model::MachineVolume.find(instance.id, context, :all)
  end
  machine_spec[:realm] = instance.realm_id if instance.realm_id
  machine_spec[:machine_image] = { :href => context.machine_image_url(instance.image_id) } if instance.image_id
  self.new(machine_spec)
end

Instance Method Details

#perform(action, context, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cimi/models/machine.rb', line 53

def perform(action, context, &block)
  begin
    if context.driver.send(:"#{action.name}_instance", context.credentials, self.id.split("/").last)
      block.callback :success
    else
      raise "Operation failed to execute on given Machine"
    end
  rescue => e
    block.callback :failure, e.message
  end
end