Class: Vra::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/vra/resource.rb

Constant Summary collapse

VM_TYPES =
%w[
  Cloud.vSphere.Machine
  Cloud.Machine
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, deployment_id, opts = {}) ⇒ Resource

Returns a new instance of Resource.

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/vra/resource.rb', line 31

def initialize(client, deployment_id, opts = {})
  @client           = client
  @deployment_id    = deployment_id
  @id               = opts[:id]
  @resource_data    = opts[:data]
  @resource_actions = []

  raise ArgumentError, 'must supply an id or a resource data hash' if @id.nil? && @resource_data.nil?
  raise ArgumentError, 'must supply an id OR a resource data hash, not both' if !@id.nil? && !@resource_data.nil?

  if @resource_data.nil?
    fetch_resource_data
  else
    @id = @resource_data['id']
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



29
30
31
# File 'lib/vra/resource.rb', line 29

def client
  @client
end

#deployment_idObject (readonly)

Returns the value of attribute deployment_id.



29
30
31
# File 'lib/vra/resource.rb', line 29

def deployment_id
  @deployment_id
end

#idObject (readonly)

Returns the value of attribute id.



29
30
31
# File 'lib/vra/resource.rb', line 29

def id
  @id
end

#resource_dataObject (readonly)

Returns the value of attribute resource_data.



29
30
31
# File 'lib/vra/resource.rb', line 29

def resource_data
  @resource_data
end

Instance Method Details

#fetch_resource_dataObject Also known as: refresh



48
49
50
51
52
# File 'lib/vra/resource.rb', line 48

def fetch_resource_data
  @resource_data = client.get_parsed("/deployment/api/deployments/#{deployment_id}/resources/#{id}")
rescue Vra::Exception::HTTPNotFound
  raise Vra::Exception::NotFound, "resource ID #{@id} does not exist"
end

#ip_addressObject



95
96
97
98
99
# File 'lib/vra/resource.rb', line 95

def ip_address
  return if !vm? || network_interfaces.nil?

  properties['address']
end

#nameObject



56
57
58
# File 'lib/vra/resource.rb', line 56

def name
  resource_data['name']
end

#network_interfacesObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/vra/resource.rb', line 80

def network_interfaces
  return unless vm?

  network_list = properties['networks']
  return if network_list.nil?

  network_list.each_with_object([]) do |item, nics|
    nics << {
      'NETWORK_NAME' => item['name'],
      'NETWORK_ADDRESS' => item['address'],
      'NETWORK_MAC_ADDRESS' => item['mac_address']
    }
  end
end

#owner_namesObject



72
73
74
# File 'lib/vra/resource.rb', line 72

def owner_names
  properties['Owner']
end

#project_idObject



76
77
78
# File 'lib/vra/resource.rb', line 76

def project_id
  properties['project']
end

#propertiesObject



64
65
66
# File 'lib/vra/resource.rb', line 64

def properties
  resource_data['properties']
end

#statusObject



60
61
62
# File 'lib/vra/resource.rb', line 60

def status
  resource_data['syncStatus']
end

#vm?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/vra/resource.rb', line 68

def vm?
  VM_TYPES.include?(resource_data['type'])
end