Class: Chef::Knife::Cloud::VraService

Inherits:
Service
  • Object
show all
Includes:
VraServiceHelpers
Defined in:
lib/chef/knife/cloud/vra_service.rb

Instance Method Summary collapse

Methods included from VraServiceHelpers

#check_for_missing_config_values!, #create_service_instance, #validate!, #verify_ssl?, #wait_for_request

Constructor Details

#initialize(options = {}) ⇒ VraService

Returns a new instance of VraService.


32
33
34
35
36
37
38
39
40
41
# File 'lib/chef/knife/cloud/vra_service.rb', line 32

def initialize(options = {})
  super(config: options)

  @username   = options[:username]
  @password   = options[:password]
  @base_url   = options[:base_url]
  @tenant     = options[:tenant]
  @verify_ssl = options[:verify_ssl]
  @page_size  = options[:page_size]
end

Instance Method Details

#catalog_request(options) ⇒ Object


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/chef/knife/cloud/vra_service.rb', line 123

def catalog_request(options)
  catalog_request = connection.catalog.request(options[:catalog_id])

  catalog_request.cpus          = options[:cpus]
  catalog_request.memory        = options[:memory]
  catalog_request.requested_for = options[:requested_for]
  catalog_request.lease_days    = options[:lease_days]    unless options[:lease_days].nil?
  catalog_request.notes         = options[:notes]         unless options[:notes].nil?
  catalog_request.subtenant_id  = options[:subtenant_id]  unless options[:subtenant_id].nil?
  options[:extra_params]&.each do |param|
    catalog_request.set_parameter(param[:key], param[:type], param[:value])
  end
  # rubocop:enable all

  catalog_request
end

#connectionObject


43
44
45
46
47
48
49
50
51
52
# File 'lib/chef/knife/cloud/vra_service.rb', line 43

def connection
  @client ||= Vra::Client.new(
    base_url:   @base_url,
    username:   @username,
    password:   @password,
    tenant:     @tenant,
    page_size:  @page_size,
    verify_ssl: @verify_ssl
  )
end

#create_server(options = {}) ⇒ Object

Raises:

  • (CloudExceptions::ServerCreateError)

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/chef/knife/cloud/vra_service.rb', line 54

def create_server(options = {})
   = catalog_request(options).submit
  ui.msg("Catalog request #{.id} submitted.")
  wait_for_request(, options[:wait_time].to_i, options[:refresh_rate])
  ui.msg("Catalog request complete.\n")
  request_summary()

  raise CloudExceptions::ServerCreateError, .completion_details if .failed?

  servers = .resources.select(&:vm?)
  raise CloudExceptions::ServerCreateError, "The vRA request created more than one server, but we were only expecting 1" if servers.length > 1
  raise CloudExceptions::ServerCreateError, "The vRA request did not create any servers" if servers.length == 0

  servers.first
end

#delete_server(instance_id) ⇒ Object


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/chef/knife/cloud/vra_service.rb', line 70

def delete_server(instance_id)
  server = get_server(instance_id)
  server_summary(server)
  ui.msg("")

  if server.status == "DELETED"
    ui.warn("Server is already deleted.\n")
    return
  end

  ui.confirm("Do you really want to delete this server")

  destroy_request = server.destroy
  ui.msg("Destroy request #{destroy_request.id} submitted.")
  wait_for_request(destroy_request)
  ui.msg("Destroy request complete.")
  request_summary(destroy_request)
end

#get_server(instance_id) ⇒ Object


101
102
103
# File 'lib/chef/knife/cloud/vra_service.rb', line 101

def get_server(instance_id)
  connection.resources.by_id(instance_id)
end

#list_catalog_items(entitled) ⇒ Object


93
94
95
96
97
98
99
# File 'lib/chef/knife/cloud/vra_service.rb', line 93

def list_catalog_items(entitled)
  if entitled
    connection.catalog.entitled_items
  else
    connection.catalog.all_items
  end
end

#list_serversObject


89
90
91
# File 'lib/chef/knife/cloud/vra_service.rb', line 89

def list_servers
  connection.resources.all_resources.select(&:vm?)
end

#request_summary(request) ⇒ Object


115
116
117
118
119
120
121
# File 'lib/chef/knife/cloud/vra_service.rb', line 115

def request_summary(request)
  ui.msg("")
  msg_pair("Request Status", request.status)
  msg_pair("Completion State", request.completion_state)
  msg_pair("Completion Details", request.completion_details)
  ui.msg("")
end

#server_summary(server, _columns_with_info = nil) ⇒ Object


105
106
107
108
109
110
111
112
113
# File 'lib/chef/knife/cloud/vra_service.rb', line 105

def server_summary(server, _columns_with_info = nil)
  msg_pair("Server ID", server.id)
  msg_pair("Server Name", server.name)
  msg_pair("IP Addresses", server.ip_addresses.nil? ? "none" : server.ip_addresses.join(", "))
  msg_pair("Status", server.status)
  msg_pair("Catalog Name", server.catalog_name)
  msg_pair("Owner IDs", server.owner_ids.empty? ? "none" : server.owner_ids.join(", "))
  msg_pair("Owner Names", server.owner_names.empty? ? "none" : server.owner_names.join(", "))
end