Skip to content

Commit

Permalink
Find VMware vCenter connected to BackupServer associated with need Ba…
Browse files Browse the repository at this point in the history
…ckupRersource

* Find VMware vCenter connected to BackupServer associated with need BackupRersource, while Backup Job is added for Virtual Server.
  • Loading branch information
ykohut authored Mar 12, 2020
1 parent c809957 commit 5020e21
Showing 1 changed file with 43 additions and 13 deletions.
56 changes: 43 additions & 13 deletions schedule_create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ def backup_repository_uid
.dig(:QueryResult, :Refs, :Ref, :UID)
end

def backup_server_id
backup_repository_ref =
api_get("repositories/#{uid_to_identifier(:Repository, backup_repository_uid)}")
.dig(:EntityRef, :Links, :Link)

return unless backup_repository_ref

backup_repository_ref.detect { |hash| hash[:Type] == 'BackupServerReference' }
.fetch(:Href, "")
.rpartition('/').last
end

def vcenter_instance_uuid(virtual_server)
if virtual_server.vcloud?
virtual_server.vcenter
Expand Down Expand Up @@ -122,19 +134,37 @@ def add_vm_params(vm_to_add_ref)
end

def vm_ref(virtual_server)
hierarchy_root_id =
uid_to_identifier(
:HierarchyRoot,
api_get(
build_query(:hierarchyroot, { uniqueid: "\"#{vcenter_instance_uuid(virtual_server)}\"" }, entities: false)
).dig(:QueryResult, :Refs, :Ref, :UID)
)

return unless hierarchy_root_id
hierarchy_roots =
api_get(
build_query(:hierarchyroot, { uniqueid: "\"#{vcenter_instance_uuid(virtual_server)}\"" }, entities: false)
).dig(:QueryResult, :Refs, :Ref)

return unless hierarchy_roots

# Iterate over hierarchyroots to find proper VMware vCenter
(hierarchy_roots.is_a?(Hash) ? [hierarchy_roots] : hierarchy_roots).each do |hierarchy_root|
hierarchy_root_id = uid_to_identifier(:HierarchyRoot, hierarchy_root[:UID])
next unless hierarchy_root_id

backup_server_href =
api_get("hierarchyRoots/#{hierarchy_root_id}").dig(:EntityRef, :Links, :Link)
next unless backup_server_href

backup_server_url =
backup_server_href.detect { |hash| hash[:Type] == 'BackupServerReference' }[:Href]
next unless backup_server_url

# BackupServer ID of VM hierarchyroot is equal to BackupServer ID of BackupRepository
if backup_server_url
.rpartition('/').last
.eql? backup_server_id
return {
obj_ref: "urn:VMware:Vm:#{hierarchy_root_id}.#{virtual_server.vcenter_moref}",
obj_name: virtual_server.label
}
end
end

{
obj_ref: "urn:VMware:Vm:#{hierarchy_root_id}.#{virtual_server.vcenter_moref}",
obj_name: virtual_server.label
}
return
end
end

0 comments on commit 5020e21

Please sign in to comment.