@@ -84,6 +84,18 @@ def backup_repository_uid
8484 . dig ( :QueryResult , :Refs , :Ref , :UID )
8585 end
8686
87+ def backup_server_id
88+ backup_repository_ref =
89+ api_get ( "repositories/#{ uid_to_identifier ( :Repository , backup_repository_uid ) } " )
90+ . dig ( :EntityRef , :Links , :Link )
91+
92+ return unless backup_repository_ref
93+
94+ backup_repository_ref . detect { |hash | hash [ :Type ] == 'BackupServerReference' }
95+ . fetch ( :Href , "" )
96+ . rpartition ( '/' ) . last
97+ end
98+
8799 def vcenter_instance_uuid ( virtual_server )
88100 if virtual_server . vcloud?
89101 virtual_server . vcenter
@@ -122,19 +134,37 @@ def add_vm_params(vm_to_add_ref)
122134 end
123135
124136 def vm_ref ( virtual_server )
125- hierarchy_root_id =
126- uid_to_identifier (
127- :HierarchyRoot ,
128- api_get (
129- build_query ( :hierarchyroot , { uniqueid : "\" #{ vcenter_instance_uuid ( virtual_server ) } \" " } , entities : false )
130- ) . dig ( :QueryResult , :Refs , :Ref , :UID )
131- )
132-
133- return unless hierarchy_root_id
137+ hierarchy_roots =
138+ api_get (
139+ build_query ( :hierarchyroot , { uniqueid : "\" #{ vcenter_instance_uuid ( virtual_server ) } \" " } , entities : false )
140+ ) . dig ( :QueryResult , :Refs , :Ref )
141+
142+ return unless hierarchy_roots
143+
144+ # Iterate over hierarchyroots to find proper VMware vCenter
145+ ( hierarchy_roots . is_a? ( Hash ) ? [ hierarchy_roots ] : hierarchy_roots ) . each do |hierarchy_root |
146+ hierarchy_root_id = uid_to_identifier ( :HierarchyRoot , hierarchy_root [ :UID ] )
147+ next unless hierarchy_root_id
148+
149+ backup_server_href =
150+ api_get ( "hierarchyRoots/#{ hierarchy_root_id } " ) . dig ( :EntityRef , :Links , :Link )
151+ next unless backup_server_href
152+
153+ backup_server_url =
154+ backup_server_href . detect { |hash | hash [ :Type ] == 'BackupServerReference' } [ :Href ]
155+ next unless backup_server_url
156+
157+ # BackupServer ID of VM hierarchyroot is equal to BackupServer ID of BackupRepository
158+ if backup_server_url
159+ . rpartition ( '/' ) . last
160+ . eql? backup_server_id
161+ return {
162+ obj_ref : "urn:VMware:Vm:#{ hierarchy_root_id } .#{ virtual_server . vcenter_moref } " ,
163+ obj_name : virtual_server . label
164+ }
165+ end
166+ end
134167
135- {
136- obj_ref : "urn:VMware:Vm:#{ hierarchy_root_id } .#{ virtual_server . vcenter_moref } " ,
137- obj_name : virtual_server . label
138- }
168+ return
139169 end
140170end
0 commit comments