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

acts_as_root_entity, all, #filter_attributes, list

Methods included from Deltacloud::Helpers::Database

#current_db, #current_provider, #delete_attributes_for, #extract_attribute_value, #get_entity, #load_attributes_for, #provides?, #store_attributes_for, #test_environment?

Methods included from Deltacloud::Helpers::Drivers

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

Methods inherited from Resource

<<, #[], #[]=, add_attributes!, all_uri, base_schema, base_schema_cloned?, clone_base_schema, #filter_by, #filter_by_arr_index, #filter_by_arr_range, from_json, from_xml, inherited, #initialize, parse, #prepare, resource_uri, #to_json, to_json, #to_xml, to_xml, xml_tag_name

Methods included from Schema::DSL

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

Constructor Details

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

Class Method Details

.attach_volume(volume, location, context) ⇒ Object

returns the newly attach machine_volume



125
126
127
128
129
# File 'lib/cimi/models/machine.rb', line 125

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

.create_from_json(body, context) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cimi/models/machine.rb', line 53

def self.create_from_json(body, context)
  json = JSON.parse(body)
  machine_template = json['machineTemplate']
  if !machine_template['href'].nil?
    template = current_db.machine_templates.first(:id => machine_template['href'].split('/').last)
    raise 'Could not find the MachineTemplate' if template.nil?
    hardware_profile_id = template.machine_config.split('/').last
    image_id = template.machine_image.split('/').last
  else
    hardware_profile_id = machine_template['machineConfig']["href"].split('/').last
    image_id = machine_template['machineImage']["href"].split('/').last
    if machine_template.has_key? 'credential'
      additional_params[:keyname] = machine_template['credential']["href"].split('/').last
    end
  end

  additional_params = {}
  additional_params[:name] = json['name'] if json['name']
  instance = context.driver.create_instance(context.credentials, image_id, {
    :hwp_id => hardware_profile_id
  }.merge(additional_params))

  # Store attributes that are not supported by the backend cloud to local
  # database:
  store_attributes_for(instance, json)
  from_instance(instance, context)
end

.create_from_xml(body, context) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/cimi/models/machine.rb', line 81

def self.create_from_xml(body, context)
  xml = XmlSimple.xml_in(body)
  if xml['machineTemplate'][0]['href']
    template = current_db.machine_templates_dataset.first(:id => xml['machineTemplate'][0]['href'].split('/').last)
    hardware_profile_id = template.machine_config.split('/').last
    image_id = template.machine_image.split('/').last
  else
    machine_template = xml['machineTemplate'][0]
    hardware_profile_id = machine_template['machineConfig'].first["href"].split('/').last
    image_id = machine_template['machineImage'].first["href"].split('/').last
    if machine_template.has_key? 'credential'
      additional_params[:keyname] = machine_template['credential'][0]["href"].split('/').last
    end
  end
  additional_params = {}
  additional_params[:name] = xml['name'][0] if xml['name']
  instance = context.driver.create_instance(context.credentials, image_id, {
    :hwp_id => hardware_profile_id
  }.merge(additional_params))

  # Store attributes that are not supported by the backend cloud to local
  # database:
  entity = store_attributes_for(instance, xml)
  from_instance(instance, context, entity.to_hash)
end

.delete!(id, context) ⇒ Object



119
120
121
122
# File 'lib/cimi/models/machine.rb', line 119

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

.detach_volume(volume, location, context) ⇒ Object

returns the machine_volume_collection for the given machine



132
133
134
135
136
# File 'lib/cimi/models/machine.rb', line 132

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

Instance Method Details

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



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/cimi/models/machine.rb', line 107

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