class Vra::Resource
Attributes
client[R]
id[R]
resource_data[R]
Public Class Methods
by_name(client, name)
click to toggle source
@param client [Vra::Client] @param name [String] - the hostname of the client you wish to lookup @preturn [Vra::Resource] - return nil if not found, otherwise the resource associated with the name
# File lib/vra/resource.rb, line 50 def self.by_name(client, name) raise ArgumentError.new("name cannot be nil") if name.nil? raise ArgumentError.new("client cannot be nil") if client.nil? Resources.all(client).find { |r| r.name.downcase =~ /#{name.downcase}/ } end
new(client, opts)
click to toggle source
# File lib/vra/resource.rb, line 26 def initialize(client, opts) @client = client @id = opts[:id] @resource_data = opts[:data] @resource_actions = [] if @id.nil? && @resource_data.nil? raise ArgumentError, "must supply an id or a resource data hash" end if !@id.nil? && !@resource_data.nil? raise ArgumentError, "must supply an id OR a resource data hash, not both" end if @resource_data.nil? fetch_resource_data else @id = @resource_data["id"] end end
Public Instance Methods
action_id_by_name(name)
click to toggle source
# File lib/vra/resource.rb, line 205 def action_id_by_name(name) return if actions.nil? action = actions.find { |x| x["name"] == name } return if action.nil? action["id"] end
action_request_payload(action_id)
click to toggle source
# File lib/vra/resource.rb, line 242 def action_request_payload(action_id) { "@type" => "ResourceActionRequest", "resourceRef" => { "id" => @id, }, "resourceActionRef" => { "id" => action_id, }, "organization" => { "tenantRef" => tenant_id, "tenantLabel" => tenant_name, "subtenantRef" => subtenant_id, "subtenantLabel" => subtenant_name, }, "state" => "SUBMITTED", "requestNumber" => 0, "requestData" => { "entries" => [], }, } end
actions()
click to toggle source
# File lib/vra/resource.rb, line 197 def actions # if this Resource instance was created with data from a "all_resources" fetch, # it is likely missing operations data because the vRA API is not pleasant sometimes. fetch_resource_data if resource_data["operations"].nil? resource_data["operations"] end
catalog_id()
click to toggle source
# File lib/vra/resource.rb, line 108 def catalog_id catalog_item["id"] end
catalog_item()
click to toggle source
# File lib/vra/resource.rb, line 102 def catalog_item return {} if resource_data["catalogItem"].nil? resource_data["catalogItem"] end
catalog_name()
click to toggle source
# File lib/vra/resource.rb, line 112 def catalog_name catalog_item["label"] end
description()
click to toggle source
# File lib/vra/resource.rb, line 68 def description resource_data["description"] end
destroy()
click to toggle source
# File lib/vra/resource.rb, line 214 def destroy action_id = action_id_by_name("Destroy") raise Vra::Exception::NotFound, "No destroy action found for resource #{@id}" if action_id.nil? submit_action_request(action_id) end
fetch_resource_data()
click to toggle source
# File lib/vra/resource.rb, line 57 def fetch_resource_data @resource_data = client.get_parsed("/catalog-service/api/consumer/resources/#{@id}") rescue Vra::Exception::HTTPNotFound raise Vra::Exception::NotFound, "resource ID #{@id} does not exist" end
Also aliased as: refresh
ip_addresses()
click to toggle source
# File lib/vra/resource.rb, line 169 def ip_addresses return if !vm? || network_interfaces.nil? addrs = [] request_id = @resource_data["requestId"] print "Waiting For vRA to collect the IP" loop do resource_views = @client.http_get("/catalog-service/api/consumer/requests/#{request_id}/resourceViews") JSON.parse(resource_views.body)["content"].each do |content| if content.key?("data") && !(content["data"]["ip_address"].nil? || content["data"]["ip_address"] == "") addrs << content["data"]["ip_address"] end end break unless addrs.empty? sleep 10 end addrs end
machine_in_provisioned_state?()
click to toggle source
# File lib/vra/resource.rb, line 147 def machine_in_provisioned_state? machine_status == "MachineProvisioned" end
machine_off?()
click to toggle source
# File lib/vra/resource.rb, line 135 def machine_off? machine_status == "Off" end
machine_on?()
click to toggle source
# File lib/vra/resource.rb, line 131 def machine_on? machine_status == "On" end
machine_status()
click to toggle source
# File lib/vra/resource.rb, line 124 def machine_status status = resource_data["resourceData"]["entries"].find { |x| x["key"] == "MachineStatus" } raise "No MachineStatus entry available for resource" if status.nil? status["value"]["value"] end
machine_turning_off?()
click to toggle source
# File lib/vra/resource.rb, line 143 def machine_turning_off? %w{TurningOff ShuttingDown}.include?(machine_status) end
machine_turning_on?()
click to toggle source
# File lib/vra/resource.rb, line 139 def machine_turning_on? machine_status == "TurningOn" || machine_status == "MachineActivated" end
name()
click to toggle source
# File lib/vra/resource.rb, line 64 def name resource_data["name"] end
network_interfaces()
click to toggle source
# File lib/vra/resource.rb, line 151 def network_interfaces return unless vm? network_list = resource_data["resourceData"]["entries"].find { |x| x["key"] == "NETWORK_LIST" } return if network_list.nil? network_list["value"]["items"].each_with_object([]) do |item, nics| nic = {} item["values"]["entries"].each do |entry| key = entry["key"] value = entry["value"]["value"] nic[key] = value end nics << nic end end
organization()
click to toggle source
# File lib/vra/resource.rb, line 80 def organization return {} if resource_data["organization"].nil? resource_data["organization"] end
owner_ids()
click to toggle source
# File lib/vra/resource.rb, line 116 def owner_ids resource_data["owners"].map { |x| x["ref"] } end
owner_names()
click to toggle source
# File lib/vra/resource.rb, line 120 def owner_names resource_data["owners"].map { |x| x["value"] } end
poweroff()
click to toggle source
# File lib/vra/resource.rb, line 228 def poweroff action_id = action_id_by_name("Power Off") raise Vra::Exception::NotFound, "No power-off action found for resource #{@id}" if action_id.nil? submit_action_request(action_id) end
poweron()
click to toggle source
# File lib/vra/resource.rb, line 235 def poweron action_id = action_id_by_name("Power On") raise Vra::Exception::NotFound, "No power-on action found for resource #{@id}" if action_id.nil? submit_action_request(action_id) end
shutdown()
click to toggle source
# File lib/vra/resource.rb, line 221 def shutdown action_id = action_id_by_name("Shutdown") raise Vra::Exception::NotFound, "No shutdown action found for resource #{@id}" if action_id.nil? submit_action_request(action_id) end
status()
click to toggle source
# File lib/vra/resource.rb, line 72 def status resource_data["status"] end
submit_action_request(action_id)
click to toggle source
# File lib/vra/resource.rb, line 265 def submit_action_request(action_id) payload = action_request_payload(action_id).to_json response = client.http_post("/catalog-service/api/consumer/requests", payload) request_id = response.location.split("/")[-1] Vra::Request.new(client, request_id) end
subtenant_id()
click to toggle source
# File lib/vra/resource.rb, line 94 def subtenant_id organization["subtenantRef"] end
subtenant_name()
click to toggle source
# File lib/vra/resource.rb, line 98 def subtenant_name organization["subtenantLabel"] end
tenant_id()
click to toggle source
# File lib/vra/resource.rb, line 86 def tenant_id organization["tenantRef"] end
tenant_name()
click to toggle source
# File lib/vra/resource.rb, line 90 def tenant_name organization["tenantLabel"] end
vm?()
click to toggle source
# File lib/vra/resource.rb, line 76 def vm? %w{Infrastructure.Virtual Infrastructure.Cloud}.include?(resource_data["resourceTypeRef"]["id"]) end