Class: Vra::CatalogRequest

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, catalog_id, opts = {}) ⇒ CatalogRequest

Returns a new instance of CatalogRequest.



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vra/catalog_request.rb', line 27

def initialize(client, catalog_id, opts = {})
  @client            = client
  @catalog_id        = catalog_id
  @cpus              = opts[:cpus]
  @memory            = opts[:memory]
  @requested_for     = opts[:requested_for]
  @lease_days        = opts[:lease_days]
  @notes             = opts[:notes]
  @subtenant_id      = opts[:subtenant_id]
  @additional_params = opts[:additional_params] || Vra::RequestParameters.new
  @catalog_item = Vra::CatalogItem.new(client, id: catalog_id)
end

Instance Attribute Details

#catalog_idObject (readonly)

Returns the value of attribute catalog_id.



23
24
25
# File 'lib/vra/catalog_request.rb', line 23

def catalog_id
  @catalog_id
end

#catalog_itemObject (readonly)

Returns the value of attribute catalog_item.



23
24
25
# File 'lib/vra/catalog_request.rb', line 23

def catalog_item
  @catalog_item
end

#clientObject (readonly)

Returns the value of attribute client.



23
24
25
# File 'lib/vra/catalog_request.rb', line 23

def client
  @client
end

#cpusObject

Returns the value of attribute cpus.



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

def cpus
  @cpus
end

#custom_fieldsObject (readonly)

Returns the value of attribute custom_fields.



23
24
25
# File 'lib/vra/catalog_request.rb', line 23

def custom_fields
  @custom_fields
end

#lease_daysObject

Returns the value of attribute lease_days.



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

def lease_days
  @lease_days
end

#memoryObject

Returns the value of attribute memory.



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

def memory
  @memory
end

#notesObject

Returns the value of attribute notes.



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

def notes
  @notes
end

#requested_forObject

Returns the value of attribute requested_for.



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

def requested_for
  @requested_for
end

#subtenant_idObject



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

def subtenant_id
  @subtenant_id || catalog_item.subtenant_id
end

#template_payloadString

Returns - the current catalog template payload from VRA or custom payload set in JSON format.

Returns:

  • (String)
    • the current catalog template payload from VRA or custom payload set in JSON format



108
109
110
# File 'lib/vra/catalog_request.rb', line 108

def template_payload
  @template_payload
end

Class Method Details

.request_from_payload(client, payload_file) ⇒ Vra::CatalogRequest

Returns - a request with the given payload merged.

Parameters:

  • payload_file (String)
    • A json payload that represents the catalog template you want to merge with this request

  • client (Vra::Client)
    • a vra client object

Returns:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/vra/catalog_request.rb', line 43

def self.request_from_payload(client, payload_file)
  hash_payload = JSON.parse(File.read(payload_file))
  catalog_id = hash_payload["catalogItemId"]
  blueprint_name = hash_payload["data"].select { |_k, v| v.is_a?(Hash) }.keys.first
  blueprint_data = hash_payload["data"][blueprint_name]
  opts = {}
  opts[:cpus] = blueprint_data["data"]["cpu"]
  opts[:memory] = blueprint_data["data"]["memory"]
  opts[:requested_for] = hash_payload["requestedFor"]
  opts[:lease_days] = blueprint_data.fetch("leaseDays", nil) || hash_payload["data"].fetch("_lease_days", 1)
  opts[:description] = hash_payload["description"]
  opts[:subtenant_id] = hash_payload["businessGroupId"]
  cr = Vra::CatalogRequest.new(client, catalog_id, opts)
  cr.template_payload = File.read(payload_file)
  cr
end

Instance Method Details

#delete_parameter(key) ⇒ Object



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

def delete_parameter(key)
  @additional_params.delete(key)
end

#merge_payload(payload) ⇒ String

Returns - the current catalog template payload merged with the settings applied from this request.

Parameters:

  • - (String)

    A json payload that represents the catalog template you want to merge with this request

Returns:

  • (String)
    • the current catalog template payload merged with the settings applied from this request



91
92
93
94
95
96
97
98
99
100
# File 'lib/vra/catalog_request.rb', line 91

def merge_payload(payload)
  hash_payload = JSON.parse(payload)
  blueprint_name = hash_payload["data"].select { |_k, v| v.is_a?(Hash) }.keys.first
  hash_payload["data"][blueprint_name]["data"]["cpu"] = @cpus
  hash_payload["data"][blueprint_name]["data"]["memory"] = @memory
  hash_payload["requestedFor"] = @requested_for
  hash_payload["data"]["_leaseDays"] = @lease_days
  hash_payload["description"] = @notes
  JSON.pretty_generate(deep_merge(hash_payload, parameters))
end

#merged_payloadString

Returns - the current catalog template payload merged with the settings applied from this request.

Returns:

  • (String)
    • the current catalog template payload merged with the settings applied from this request



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

def merged_payload
  merge_payload(template_payload)
end

#parametersObject



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

def parameters
  @additional_params.to_vra
end

#set_parameter(key, type, value) ⇒ Object



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

def set_parameter(key, type, value)
  @additional_params.set(key, type, value)
end

#set_parameters(key, value_data) ⇒ Object



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

def set_parameters(key, value_data)
  @additional_params.set_parameters(key, value_data)
end

#submitVra::Request

Returns - submits and returns the request, validating before hand.

Returns:

  • (Vra::Request)
    • submits and returns the request, validating before hand



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/vra/catalog_request.rb', line 113

def submit
  validate_params!

  begin
    post_response = client.http_post("/catalog-service/api/consumer/entitledCatalogItems/#{@catalog_id}/requests", merged_payload)
  rescue Vra::Exception::HTTPError => e
    raise Vra::Exception::RequestError, "Unable to submit request: #{e.errors.join(', ')}"
  rescue
    raise
  end
  request_id = JSON.parse(post_response.body)["id"]
  Vra::Request.new(client, request_id)
end

#validate_params!Object

Raises:

  • (ArgumentError)


80
81
82
83
84
85
86
87
# File 'lib/vra/catalog_request.rb', line 80

def validate_params!
  missing_params = []
  [ :catalog_id, :cpus, :memory, :requested_for, :subtenant_id ].each do |param|
    missing_params << param.to_s if send(param).nil?
  end

  raise ArgumentError, "Unable to submit request, required param(s) missing => #{missing_params.join(', ')}" unless missing_params.empty?
end