Class: Vra::Resource
- Inherits:
-
Object
- Object
- Vra::Resource
- Defined in:
- lib/vra/resource.rb
Overview
rubocop:disable ClassLength
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#resource_data ⇒ Object
readonly
Returns the value of attribute resource_data.
Instance Method Summary collapse
- #action_id_by_name(name) ⇒ Object
- #action_request_payload(action_id) ⇒ Object
- #actions ⇒ Object
- #catalog_id ⇒ Object
- #catalog_item ⇒ Object
- #catalog_name ⇒ Object
- #description ⇒ Object
- #destroy ⇒ Object
- #fetch_resource_data ⇒ Object (also: #refresh)
-
#initialize(client, opts) ⇒ Resource
constructor
A new instance of Resource.
- #ip_addresses ⇒ Object
- #machine_in_provisioned_state? ⇒ Boolean
- #machine_off? ⇒ Boolean
- #machine_on? ⇒ Boolean
- #machine_status ⇒ Object
- #machine_turning_off? ⇒ Boolean
- #machine_turning_on? ⇒ Boolean
- #name ⇒ Object
- #network_interfaces ⇒ Object
- #organization ⇒ Object
- #owner_ids ⇒ Object
- #owner_names ⇒ Object
- #poweroff ⇒ Object
- #poweron ⇒ Object
- #shutdown ⇒ Object
- #status ⇒ Object
- #submit_action_request(action_id) ⇒ Object
- #subtenant_id ⇒ Object
- #subtenant_name ⇒ Object
- #tenant_id ⇒ Object
- #tenant_name ⇒ Object
- #vm? ⇒ Boolean
Constructor Details
#initialize(client, opts) ⇒ Resource
Returns a new instance of Resource.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# 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 |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
24 25 26 |
# File 'lib/vra/resource.rb', line 24 def client @client end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
24 25 26 |
# File 'lib/vra/resource.rb', line 24 def id @id end |
#resource_data ⇒ Object (readonly)
Returns the value of attribute resource_data.
24 25 26 |
# File 'lib/vra/resource.rb', line 24 def resource_data @resource_data end |
Instance Method Details
#action_id_by_name(name) ⇒ Object
179 180 181 182 183 184 185 186 |
# File 'lib/vra/resource.rb', line 179 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) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/vra/resource.rb', line 216 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 ⇒ Object
171 172 173 174 175 176 177 |
# File 'lib/vra/resource.rb', line 171 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 ⇒ Object
98 99 100 |
# File 'lib/vra/resource.rb', line 98 def catalog_id catalog_item['id'] end |
#catalog_item ⇒ Object
92 93 94 95 96 |
# File 'lib/vra/resource.rb', line 92 def catalog_item return {} if resource_data['catalogItem'].nil? resource_data['catalogItem'] end |
#catalog_name ⇒ Object
102 103 104 |
# File 'lib/vra/resource.rb', line 102 def catalog_name catalog_item['label'] end |
#description ⇒ Object
58 59 60 |
# File 'lib/vra/resource.rb', line 58 def description resource_data['description'] end |
#destroy ⇒ Object
188 189 190 191 192 193 |
# File 'lib/vra/resource.rb', line 188 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 ⇒ Object Also known as: refresh
47 48 49 50 51 |
# File 'lib/vra/resource.rb', line 47 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 |
#ip_addresses ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/vra/resource.rb', line 159 def ip_addresses return if !vm? || network_interfaces.nil? addrs = [] network_interfaces.each do |nic| next unless nic.key?('NETWORK_ADDRESS') addrs << nic['NETWORK_ADDRESS'] end addrs end |
#machine_in_provisioned_state? ⇒ Boolean
137 138 139 |
# File 'lib/vra/resource.rb', line 137 def machine_in_provisioned_state? machine_status == 'MachineProvisioned' end |
#machine_off? ⇒ Boolean
125 126 127 |
# File 'lib/vra/resource.rb', line 125 def machine_off? machine_status == 'Off' end |
#machine_on? ⇒ Boolean
121 122 123 |
# File 'lib/vra/resource.rb', line 121 def machine_on? machine_status == 'On' end |
#machine_status ⇒ Object
114 115 116 117 118 119 |
# File 'lib/vra/resource.rb', line 114 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? ⇒ Boolean
133 134 135 |
# File 'lib/vra/resource.rb', line 133 def machine_turning_off? %w(TurningOff ShuttingDown).include?(machine_status) end |
#machine_turning_on? ⇒ Boolean
129 130 131 |
# File 'lib/vra/resource.rb', line 129 def machine_turning_on? machine_status == 'TurningOn' || machine_status == 'MachineActivated' end |
#name ⇒ Object
54 55 56 |
# File 'lib/vra/resource.rb', line 54 def name resource_data['name'] end |
#network_interfaces ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/vra/resource.rb', line 141 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 ⇒ Object
70 71 72 73 74 |
# File 'lib/vra/resource.rb', line 70 def organization return {} if resource_data['organization'].nil? resource_data['organization'] end |
#owner_ids ⇒ Object
106 107 108 |
# File 'lib/vra/resource.rb', line 106 def owner_ids resource_data['owners'].map { |x| x['ref'] } end |
#owner_names ⇒ Object
110 111 112 |
# File 'lib/vra/resource.rb', line 110 def owner_names resource_data['owners'].map { |x| x['value'] } end |
#poweroff ⇒ Object
202 203 204 205 206 207 |
# File 'lib/vra/resource.rb', line 202 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 ⇒ Object
209 210 211 212 213 214 |
# File 'lib/vra/resource.rb', line 209 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 ⇒ Object
195 196 197 198 199 200 |
# File 'lib/vra/resource.rb', line 195 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 ⇒ Object
62 63 64 |
# File 'lib/vra/resource.rb', line 62 def status resource_data['status'] end |
#submit_action_request(action_id) ⇒ Object
239 240 241 242 243 244 |
# File 'lib/vra/resource.rb', line 239 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 ⇒ Object
84 85 86 |
# File 'lib/vra/resource.rb', line 84 def subtenant_id organization['subtenantRef'] end |
#subtenant_name ⇒ Object
88 89 90 |
# File 'lib/vra/resource.rb', line 88 def subtenant_name organization['subtenantLabel'] end |
#tenant_id ⇒ Object
76 77 78 |
# File 'lib/vra/resource.rb', line 76 def tenant_id organization['tenantRef'] end |
#tenant_name ⇒ Object
80 81 82 |
# File 'lib/vra/resource.rb', line 80 def tenant_name organization['tenantLabel'] end |
#vm? ⇒ Boolean
66 67 68 |
# File 'lib/vra/resource.rb', line 66 def vm? %w(Infrastructure.Virtual Infrastructure.Cloud).include?(resource_data['resourceTypeRef']['id']) end |