Skip to content

Commit

Permalink
Convert WaitForUpdatesEx and friends to rbvmomi
Browse files Browse the repository at this point in the history
  • Loading branch information
agrare committed Jul 14, 2021
1 parent 992e2ee commit 6d66f2d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 91 deletions.
1 change: 0 additions & 1 deletion lib/VMwareWebService/MiqVimInventory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ def initialize(server:, username:, password:, port: 443, cache_scope: nil)
@spec = spec
@updateSpec = updateSpec

@globalIndent = ""
@selectorHash = @@selectorHash
@selectorPropPathHash = {}

Expand Down
135 changes: 45 additions & 90 deletions lib/VMwareWebService/VimService.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,49 +44,28 @@ def acquireMksTicket(mor)
rbvmomi_to_vim_types(vim_to_rbvmomi_types(mor).AcquireMksTicket)
end

def acquireTicket(mor, ticketType)
rbvmomi_to_vim_types(vim_to_rbvmomi_types(mor).AcquireTicket(:ticketType => ticketType))
def acquireTicket(mor, ticket_type)
rbvmomi_to_vim_types(vim_to_rbvmomi_types(mor).AcquireTicket(:ticketType => ticket_type))
end

def addHost_Task(clustMor, spec, asConnected, resourcePool = nil, license = nil)
response = invoke("n1:AddHost_Task") do |message|
message.add "n1:_this", clustMor do |i|
i.set_attr "type", clustMor.vimType
end
message.add "n1:spec" do |i|
i.set_attr "xsi:type", spec.xsiType
marshalObj(i, spec)
end
message.add "n1:asConnected", asConnected
message.add "n1:resourcePool", resourcePool do |i|
i.set_attr "type", resourcePool.vimType
end unless resourcePool.nil?
message.add "n1:license", license unless license.nil?
end
(parse_response(response, 'AddHost_TaskResponse')['returnval'])
def addHost_Task(cluster_mor, spec, as_connected, resource_pool = nil, license = nil)
task_mor = vim_to_rbvmomi_types(cluster_mor).AddHost_Task(
:spec => vim_to_rbvmomi_types(spec),
:asConnected => as_connected,
:resourcePool => vim_to_rbvmomi_types(resource_pool),
:license => license
)

rbvmomi_to_vim_types(task_mor)
end

def addInternetScsiSendTargets(hssMor, iScsiHbaDevice, targets)
response = invoke("n1:AddInternetScsiSendTargets") do |message|
message.add "n1:_this", hssMor do |i|
i.set_attr "type", hssMor.vimType
end
message.add "n1:iScsiHbaDevice", iScsiHbaDevice
if targets.kind_of?(Array)
targets.each do |t|
message.add "n1:targets" do |i|
i.set_attr "xsi:type", t.xsiType
marshalObj(i, t)
end
end
else
message.add "n1:targets" do |i|
i.set_attr "xsi:type", targets.xsiType
marshalObj(i, targets)
end
end
end
(parse_response(response, 'AddInternetScsiSendTargetsResponse'))
response = vim_to_rbvmomi_types(hssMor).AddInternetScsiSendTargets(
:iScsiHbaDevice => vim_to_rbvmomi_types(iScsiHbaDevice),
:targets => vim_to_rbvmomi_types(targets)
)

rbvmomi_to_vim_types(response)
end

def addInternetScsiStaticTargets(hssMor, iScsiHbaDevice, targets)
Expand Down Expand Up @@ -151,13 +130,8 @@ def cancelTask(tmor)
(parse_response(response, 'CancelTaskResponse'))
end

def cancelWaitForUpdates(propCol)
response = invoke("n1:CancelWaitForUpdates") do |message|
message.add "n1:_this", propCol do |i|
i.set_attr "type", propCol.vimType
end
end
(parse_response(response, 'CancelWaitForUpdatesResponse'))
def cancelWaitForUpdates(prop_col)
rbvmomi_to_vim_types(vim_to_rbvmomi_types(prop_col).CancelWaitForUpdates)
end

def cloneVM_Task(vmMor, fmor, name, cspec)
Expand Down Expand Up @@ -229,18 +203,13 @@ def createCustomizationSpec(csmMor, item)
(parse_response(response, 'CreateCustomizationSpecResponse')['returnval'])
end

def createFilter(propCol, pfSpec, partialUpdates)
response = invoke("n1:CreateFilter") do |message|
message.add "n1:_this", propCol do |i|
i.set_attr "type", propCol.vimType
end
message.add "n1:spec" do |i|
i.set_attr "xsi:type", pfSpec.xsiType
marshalObj(i, pfSpec)
end
message.add "n1:partialUpdates", partialUpdates
end
(parse_response(response, 'CreateFilterResponse')['returnval'])
def createFilter(property_collector, spec, partial_updates)
result = vim_to_rbvmomi_types(property_collector).CreateFilter(
:spec => vim_to_rbvmomi_types(spec),
:partialUpdates => partial_updates
)

rbvmomi_to_vim_types(result)
end

def createFolder(pfMor, fname)
Expand Down Expand Up @@ -302,7 +271,7 @@ def createVM_Task(fMor, vmcs, pool, hMor)
end

def currentTime
serviceInstanceMor.CurrentTime
vim_to_rbvmomi_types(serviceInstanceMor).CurrentTime
end

def customizationSpecItemToXml(csmMor, item)
Expand Down Expand Up @@ -1156,37 +1125,14 @@ def updateSoftwareInternetScsiEnabled(hssMor, enabled)
(parse_response(response, 'UpdateSoftwareInternetScsiEnabledResponse'))
end

def waitForUpdates(propCol, version = nil)
response = invoke("n1:WaitForUpdates") do |message|
message.add "n1:_this", propCol do |i|
i.set_attr "type", propCol.vimType
end
message.add "n1:version", version if version
end
(parse_response(response, 'WaitForUpdatesResponse')['returnval'])
def waitForUpdates(property_collector, version = nil)
rbvmomi_to_vim_types(vim_to_rbvmomi_types(property_collector).WaitForUpdates(:version => version))
end

def waitForUpdatesEx(propCol, version = nil, options = {})
max_wait = options[:max_wait]
max_objects = options[:max_objects]

options = VimHash.new("WaitOptions") do |opts|
opts.maxObjectUpdates = max_objects.to_s if max_objects
opts.maxWaitSeconds = max_wait.to_s if max_wait
end

response = invoke("n1:WaitForUpdatesEx") do |message|
message.add "n1:_this", propCol do |i|
i.set_attr "type", propCol.vimType
end

message.add "n1:version", version if version
message.add "n1:options" do |i|
i.set_attr "type", options.vimType
marshalObj(i, options)
end
end
(parse_response(response, 'WaitForUpdatesExResponse')['returnval'])
def waitForUpdatesEx(property_collector, version = nil, options = {})
options = RbVmomi::VIM.WaitOptions(:maxObjectUpdates => options[:max_objects], :maxWaitSeconds => options[:max_wait])
response = vim_to_rbvmomi_types(property_collector).WaitForUpdatesEx(:version => version, :options => options)
rbvmomi_to_vim_types(response)
end

def xmlToCustomizationSpecItem(csmMor, specItemXml)
Expand All @@ -1203,8 +1149,6 @@ def xmlToCustomizationSpecItem(csmMor, specItemXml)

def rbvmomi_to_vim_types(obj)
case obj
when String
obj
when Array
obj.map { |i| rbvmomi_to_vim_types(i) }
when RbVmomi::BasicTypes::ManagedObject
Expand All @@ -1215,6 +1159,8 @@ def rbvmomi_to_vim_types(obj)
vim.send("#{key}=", rbvmomi_to_vim_types(val))
end
end
when String, Symbol, Integer, Time, TrueClass, FalseClass, NillClass
obj
else
raise ArgumentError, "Invalid type #{obj.class}"
end
Expand All @@ -1224,15 +1170,24 @@ def vim_to_rbvmomi_types(obj)
case obj
when Array
obj.map { |i| vim_to_rbvmomi_types(i) }
when VimHash
klass = "RbVmomi::VIM::#{obj.xsiType}".constantize
klass.new.tap do |new_obj|
obj.each do |key, val|
new_obj.send("#{key}=", vim_to_rbvmomi_types(val))
end
end
when VimString
if obj.xsiType == "ManagedObjectReference"
klass = "RbVmomi::VIM::#{obj.vimType}".constantize
klass.new(vim, obj.to_s)
else
obj.to_s
end
when String
when String, Symbol, Integer, Time, TrueClass, FalseClass, NillClass
obj
else
raise ArgumentError, "Invalid type #{obj.class}"
end
end
end

0 comments on commit 6d66f2d

Please sign in to comment.