Class: Fog::Hyperv::Collection

Inherits:
Collection
  • Object
show all
Defined in:
lib/fog/hyperv/collection.rb

Overview

Expanded Fog::Collection with Hyper-V specific configuration

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Collection

Returns a new instance of Collection.



23
24
25
26
27
28
29
30
# File 'lib/fog/hyperv/collection.rb', line 23

def initialize(attributes = {})
  @vm = attributes.delete(:vm)
  @computer = attributes.delete(:computer)
  @network_adapter = attributes.delete(:network_adapter)
  @cluster = attributes.delete(:cluster)

  super
end

Class Method Details

.get_method(method = nil) ⇒ Object

Define the service request that is used to retrieve data for this collection



7
8
9
# File 'lib/fog/hyperv/collection.rb', line 7

def self.get_method(method = nil)
  @get_method ||= method
end

.requires(*attr) ⇒ Object

Add required attributes for retrieving data for this collection



12
13
14
15
16
# File 'lib/fog/hyperv/collection.rb', line 12

def self.requires(*attr)
  @requires ||= []
  @requires += attr if attr.any?
  @requires
end

.requires?(req) ⇒ Boolean

Check if the collection requires a specific attribute

Returns:

  • (Boolean)


19
20
21
# File 'lib/fog/hyperv/collection.rb', line 19

def self.requires?(req)
  requires.include? req
end

Instance Method Details

#all(filters = {}) ⇒ Object

Retrieve all instances for the collection



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fog/hyperv/collection.rb', line 33

def all(filters = {})
  requires(*self.class.requires)

  data = service.send(method, **search_attributes, **filters)
  data ||= []
  # Hyper-V will either return an array or a single value depending on the number of entries found
  data = [data].flatten
  return clone.load(data) if filters.any?

  load data
  @loaded = true
  self
end

#get(**filters) ⇒ Object

Get a specific instance in the collection



48
49
50
51
52
53
54
# File 'lib/fog/hyperv/collection.rb', line 48

def get(**filters)
  new [service.send(method, **search_attributes, **filters)].flatten.first
rescue Fog::Hyperv::Errors::PSError => e
  raise Fog::Errors::NotFound, e if e.message =~ /Hyper-V was unable to find|^No .* is found/

  raise
end

#new(attributes = {}) ⇒ Object

Create a new instance in the collection



57
58
59
60
61
62
# File 'lib/fog/hyperv/collection.rb', line 57

def new(attributes = {})
  requires(*self.class.requires)

  attributes = attributes.attributes if attributes.is_a? Fog::Model
  super(creation_attributes.merge(attributes))
end