Class: Vra::CatalogItem

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, opts) ⇒ CatalogItem

Returns a new instance of CatalogItem.



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

def initialize(client, opts)
  @client            = client
  @id                = opts[:id]
  @catalog_item_data = opts[:data]

  if @id.nil? && @catalog_item_data.nil?
    raise ArgumentError, "must supply an id or a catalog item data hash"
  end

  if !@id.nil? && !@catalog_item_data.nil?
    raise ArgumentError, "must supply an id OR a catalog item data hash, not both"
  end

  if @catalog_item_data.nil?
    fetch_catalog_item
  else
    @id = @catalog_item_data["id"]
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

Class Method Details

.dump_template(client, id) ⇒ String

Returns - returns a json string of the catalog template.

Parameters:

  • - (String)

    the id of the catalog item

  • - (Vra::Client)

    a vra client object

Returns:

  • (String)
    • returns a json string of the catalog template



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

def self.dump_template(client, id)
  response = client.http_get("/catalog-service/api/consumer/entitledCatalogItems/#{id}/requests/template")
  response.body
end

.dump_templates(client, dir_name = "vra_templates", use_id = false) ⇒ Array[String]

Returns - a array of all the files that were generated.

Parameters:

  • - (Vra::Client)

    a vra client object

  • - (String)

    the directory path to write the files to

  • - (Boolean)

    set to true if you wish the file name to be the id of the catalog item

Returns:

  • (Array[String])
    • a array of all the files that were generated



120
121
122
123
124
125
126
127
128
# File 'lib/vra/catalog_item.rb', line 120

def self.dump_templates(client, dir_name = "vra_templates", use_id = false)
  FileUtils.mkdir_p(dir_name) unless File.exist?(dir_name)
  client.catalog.entitled_items.map do |c|
    id = use_id ? c.id : c.name.tr(" ", "_")
    filename = File.join(dir_name, "#{id}.json").downcase
    write_template(client, c.id, filename)
    filename
  end
end

.write_template(client, id, filename = nil) ⇒ Object

Note:

outputs the catalog template to a file in serialized format

if left blank, will default to the id of the item

Parameters:

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

  • id (String)
    • the id of the catalog item

  • filename (String) (defaults to: nil)
    • the name of the file you want to output the template to



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/vra/catalog_item.rb', line 103

def self.write_template(client, id, filename = nil)
  filename ||= "#{id}.json"
  begin
    contents = dump_template(client, id)
    data = JSON.parse(contents)
    pretty_contents = JSON.pretty_generate(data)
    File.write(filename, pretty_contents)
    return filename
  rescue Vra::Exception::HTTPError => e
    raise e
  end
end

Instance Method Details

#blueprint_idObject



86
87
88
# File 'lib/vra/catalog_item.rb', line 86

def blueprint_id
  @catalog_item_data["providerBinding"]["bindingId"]
end

#descriptionObject



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

def description
  @catalog_item_data["description"]
end

#fetch_catalog_itemObject



46
47
48
49
50
# File 'lib/vra/catalog_item.rb', line 46

def fetch_catalog_item
  @catalog_item_data = client.get_parsed("/catalog-service/api/consumer/catalogItems/#{id}")
rescue Vra::Exception::HTTPNotFound
  raise Vra::Exception::NotFound, "catalog ID #{id} does not exist"
end

#nameObject



52
53
54
# File 'lib/vra/catalog_item.rb', line 52

def name
  @catalog_item_data["name"]
end

#organizationObject



64
65
66
67
68
# File 'lib/vra/catalog_item.rb', line 64

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

  @catalog_item_data["organization"]
end

#statusObject



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

def status
  @catalog_item_data["status"]
end

#subtenant_idObject



78
79
80
# File 'lib/vra/catalog_item.rb', line 78

def subtenant_id
  organization["subtenantRef"]
end

#subtenant_nameObject



82
83
84
# File 'lib/vra/catalog_item.rb', line 82

def subtenant_name
  organization["subtenantLabel"]
end

#tenant_idObject



70
71
72
# File 'lib/vra/catalog_item.rb', line 70

def tenant_id
  organization["tenantRef"]
end

#tenant_nameObject



74
75
76
# File 'lib/vra/catalog_item.rb', line 74

def tenant_name
  organization["tenantLabel"]
end