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, #save

Methods included from Deltacloud::Helpers::Database

#current_db, #current_provider, #provides?, #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 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, 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

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



140
141
142
143
144
# File 'lib/cimi/models/machine.rb', line 140

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
80
81
82
83
84
85
86
# File 'lib/cimi/models/machine.rb', line 53

def self.create_from_json(body, context)
  json = JSON.parse(body)
  additional_params={}
  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
  if machine_template.has_key? "initialState"
    additional_params[:initial_state] = machine_template["initialState"].strip
  end
  additional_params[:name] = json['name'] if json['name']
  additional_params[:realm_id] = json['realm'] if json['realm']
  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:
  machine = from_instance(instance, context)
  machine.name = json['name'] || machine.name
  machine.description = json['description']
  machine.extract_properties!(json)
  machine.save
  machine
end

.create_from_xml(body, context) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/cimi/models/machine.rb', line 88

def self.create_from_xml(body, context)
  xml = XmlSimple.xml_in(body)
  additional_params = {}
  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
  if xml["machineTemplate"][0].has_key? "initialState"
    additional_params[:initial_state] = xml["machineTemplate"][0]["initialState"].first.strip
  end
  additional_params[:name] = xml['name'][0] if xml['name']
  additional_params[:realm_id] = xml['realm'][0] if xml['realm']
  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:
  machine = from_instance(instance, context)
  machine.name = xml['name'] || machine.name
  machine.description = xml['description']
  machine.extract_properties!(xml)
  machine.save
  machine
end

.delete!(id, context) ⇒ Object



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

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



147
148
149
150
151
# File 'lib/cimi/models/machine.rb', line 147

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



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/cimi/models/machine.rb', line 122

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