Class: OpenStack::Nova::Compute::Flavor

Inherits:
BaseDetail show all
Defined in:
lib/open_stack/nova/compute/flavor.rb

Overview

An OpenStack Floating Ip

Attributes

  • name - The name of the flavor

  • ram - Amount of RAM (MBytes)

  • disk - Amount of storage (GBytes)

  • swap - Amount of swap storage (MBytes)

  • vcpus - Virtual CPUs

  • rxtx_factor - Traffic shaping (?)

  • ephemeral_disk - Ephemeral storage amount (GByte)

  • is_public - True if the flavor is public

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseDetail

collection_path, collection_path_create, #collection_path_create

Methods inherited from Base

site, site=

Methods inherited from Common

collection_path, custom_method_collection_url, element_path

Methods inherited from Base

headers

Methods inherited from ActiveResource::Base

#load

Constructor Details

#initialize(attributes = {}, persisted = false) ⇒ Flavor

:notnew:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/open_stack/nova/compute/flavor.rb', line 71

def initialize(attributes = {}, persisted = false) # :notnew:
  attributes = attributes.with_indifferent_access
  new_attributes = {
      :id => attributes[:id],
      :name => attributes[:name],
      :ram => attributes[:ram],
      :disk => attributes[:disk],
      :swap => attributes[:swap],
      :vcpus => attributes[:vcpus],
      :rxtx_factor => attributes[:rxtx_factor],
      :ephemeral_disk => attributes[:'OS-FLV-EXT-DATA:ephemeral'] || attributes[:ephemeral_disk],
      :is_public => attributes[:'os-flavor-access:is_public'] || attributes[:is_public]
  }
  super(new_attributes, persisted)

  self
end

Class Method Details

.applicable_for_image(image) ⇒ Object

Returns a list of Flavor that can be used with the given Image

Attributes

  • image - An OpenStack::Nova::Compute::Image instance or an Image id



142
143
144
145
146
147
148
149
150
# File 'lib/open_stack/nova/compute/flavor.rb', line 142

def self.applicable_for_image(image)
  image_instance = image.is_a?(OpenStack::Nova::Compute::Image) ? image : Image.find(image)

  constraints = {}
  constraints[:ram] = image.min_ram if image_instance.min_ram > 0
  constraints[:disk] = image.min_disk if image_instance.min_disk > 0

  find_by_constraints constraints
end

.find_all_by_name(name) ⇒ Object

Returns a list of Flavor for a given name

Attributes

  • name - A string



113
114
115
# File 'lib/open_stack/nova/compute/flavor.rb', line 113

def self.find_all_by_name(name)
  all.reject { |flavor| flavor.name != name }
end

.find_by_constraints(constraints = {}) ⇒ Object

Returns a list of Flavor that can be used with the given constraints

Attributes

  • constraints - Hash of constraints. Valid keys are: :ram, :vcpus, :disk



129
130
131
132
133
134
135
136
# File 'lib/open_stack/nova/compute/flavor.rb', line 129

def self.find_by_constraints(constraints = {})
  constraints = constraints.with_indifferent_access
  constraints[:ram] ||= -1.0/0.0
  constraints[:vcpus] ||= -1.0/0.0
  constraints[:disk] ||= -1.0/0.0

  self.all.select { |flavor| flavor.ram >= constraints[:ram] and flavor.vcpus >= constraints[:vcpus] and flavor.disk >= constraints[:disk] }
end

.find_by_name(name) ⇒ Object

Returns the first Flavor for a given name

Attributes

  • name - A string



121
122
123
# File 'lib/open_stack/nova/compute/flavor.rb', line 121

def self.find_by_name(name)
  all.detect { |flavor| flavor.name == name }
end

Instance Method Details

#descriptionObject

Returns a human-friendly description for this Flavor



153
154
155
# File 'lib/open_stack/nova/compute/flavor.rb', line 153

def description
  "#{vcpus} vCPU - #{ram} MB RAM - #{disk} GB Disk"
end

#encode(options = {}) ⇒ Object

Overloads ActiveRecord::encode method



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/open_stack/nova/compute/flavor.rb', line 90

def encode(options={}) # :nodoc: Custom encoding to deal with openstack API
  to_encode = {
      :flavor => {
          :name => name,
          :ram => ram,
          :disk => disk,
          :swap => swap,
          :vcpus => vcpus
      }
  }

  # Optional attributes (openstack will not accept empty attribute for update/create)
  to_encode[:flavor][:'OS-FLV-EXT-DATA:ephemeral'] = ephemeral_disk if ephemeral_disk.present?
  to_encode[:flavor][:'os-flavor-access:is_public'] = is_public if is_public.present?
  to_encode[:flavor][:rxtx_factor] = rxtx_factor if rxtx_factor.present?

  to_encode.send("to_#{self.class.format.extension}", options)
end