Class: Vra::Resource

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

Overview

rubocop:disable ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, opts) ⇒ Resource

Returns a new instance of Resource.



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

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

#clientObject (readonly)

Returns the value of attribute client.



25
26
27
# File 'lib/vra/resource.rb', line 25

def client
  @client
end

#idObject (readonly)

Returns the value of attribute id.



25
26
27
# File 'lib/vra/resource.rb', line 25

def id
  @id
end

#resource_dataObject (readonly)

Returns the value of attribute resource_data.



25
26
27
# File 'lib/vra/resource.rb', line 25

def resource_data
  @resource_data
end

Instance Method Details

#action_id_by_name(name) ⇒ Object



199
200
201
202
203
204
205
206
# File 'lib/vra/resource.rb', line 199

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



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/vra/resource.rb', line 236

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

#actionsObject



191
192
193
194
195
196
197
# File 'lib/vra/resource.rb', line 191

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_idObject



99
100
101
# File 'lib/vra/resource.rb', line 99

def catalog_id
  catalog_item["id"]
end

#catalog_itemObject



93
94
95
96
97
# File 'lib/vra/resource.rb', line 93

def catalog_item
  return {} if resource_data["catalogItem"].nil?

  resource_data["catalogItem"]
end

#catalog_nameObject



103
104
105
# File 'lib/vra/resource.rb', line 103

def catalog_name
  catalog_item["label"]
end

#descriptionObject



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

def description
  resource_data["description"]
end

#destroyObject



208
209
210
211
212
213
# File 'lib/vra/resource.rb', line 208

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_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("/catalog-service/api/consumer/resources/#{@id}")
rescue Vra::Exception::HTTPNotFound
  raise Vra::Exception::NotFound, "resource ID #{@id} does not exist"
end

#ip_addressesObject

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/vra/resource.rb', line 160

def ip_addresses # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  return if !vm? || network_interfaces.nil?

  addrs = []

  request_id = @resource_data["requestId"]

  resource_views = @client.http_get("/catalog-service/api/consumer/requests/#{request_id}/resourceViews")

  data_zero = JSON.parse(resource_views.body)["content"][0]["data"]["ip_address"]
  data_one = JSON.parse(resource_views.body)["content"][1]["data"]["ip_address"]

  print "Waiting For vRA to collect the IP"
  while (data_zero == "" || data_one == "") && (data_zero.nil? || data_one.nil?)
    resource_views = @client.http_get("/catalog-service/api/consumer/requests/#{request_id}/resourceViews")
    data_zero = JSON.parse(resource_views.body)["content"][0]["data"]["ip_address"]
    data_one = JSON.parse(resource_views.body)["content"][1]["data"]["ip_address"]
    sleep 10
    print "."
  end

  ip_address = if JSON.parse(resource_views.body)["content"][0]["data"]["ip_address"].nil?
                 JSON.parse(resource_views.body)["content"][1]["data"]["ip_address"]
               else
                 JSON.parse(resource_views.body)["content"][0]["data"]["ip_address"]
               end

  addrs << ip_address
  addrs
end

#machine_in_provisioned_state?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/vra/resource.rb', line 138

def machine_in_provisioned_state?
  machine_status == "MachineProvisioned"
end

#machine_off?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/vra/resource.rb', line 126

def machine_off?
  machine_status == "Off"
end

#machine_on?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/vra/resource.rb', line 122

def machine_on?
  machine_status == "On"
end

#machine_statusObject



115
116
117
118
119
120
# File 'lib/vra/resource.rb', line 115

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

Returns:

  • (Boolean)


134
135
136
# File 'lib/vra/resource.rb', line 134

def machine_turning_off?
  %w{TurningOff ShuttingDown}.include?(machine_status)
end

#machine_turning_on?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/vra/resource.rb', line 130

def machine_turning_on?
  machine_status == "TurningOn" || machine_status == "MachineActivated"
end

#nameObject



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

def name
  resource_data["name"]
end

#network_interfacesObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/vra/resource.rb', line 142

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

#organizationObject



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

def organization
  return {} if resource_data["organization"].nil?

  resource_data["organization"]
end

#owner_idsObject



107
108
109
# File 'lib/vra/resource.rb', line 107

def owner_ids
  resource_data["owners"].map { |x| x["ref"] }
end

#owner_namesObject



111
112
113
# File 'lib/vra/resource.rb', line 111

def owner_names
  resource_data["owners"].map { |x| x["value"] }
end

#poweroffObject



222
223
224
225
226
227
# File 'lib/vra/resource.rb', line 222

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

#poweronObject



229
230
231
232
233
234
# File 'lib/vra/resource.rb', line 229

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

#shutdownObject



215
216
217
218
219
220
# File 'lib/vra/resource.rb', line 215

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

#statusObject



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

def status
  resource_data["status"]
end

#submit_action_request(action_id) ⇒ Object



259
260
261
262
263
264
# File 'lib/vra/resource.rb', line 259

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_idObject



85
86
87
# File 'lib/vra/resource.rb', line 85

def subtenant_id
  organization["subtenantRef"]
end

#subtenant_nameObject



89
90
91
# File 'lib/vra/resource.rb', line 89

def subtenant_name
  organization["subtenantLabel"]
end

#tenant_idObject



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

def tenant_id
  organization["tenantRef"]
end

#tenant_nameObject



81
82
83
# File 'lib/vra/resource.rb', line 81

def tenant_name
  organization["tenantLabel"]
end

#vm?Boolean

Returns:

  • (Boolean)


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

def vm?
  %w{Infrastructure.Virtual Infrastructure.Cloud}.include?(resource_data["resourceTypeRef"]["id"])
end