GabrielBrascher commented on a change in pull request #5774:
URL: https://github.com/apache/cloudstack/pull/5774#discussion_r775922142



##########
File path: 
plugins/backup/veeam/src/main/java/org/apache/cloudstack/backup/veeam/VeeamClient.java
##########
@@ -337,25 +337,47 @@ private boolean checkTaskStatus(final HttpResponse 
response) throws IOException
     //////////////// Public Veeam APIs /////////////////////
     ////////////////////////////////////////////////////////
 
-    public Ref listBackupRepository(final String backupServerId) {
-        LOG.debug("Trying to list backup repository for backup server id: " + 
backupServerId);
+    public Ref listBackupRepository(final String backupServerId, final String 
backupName) {
+        LOG.debug(String.format("Trying to list backup repository for backup 
job [name: %s] in server [id: %s].", backupName, backupServerId));
         try {
+            String repositoryName = getRepositoryNameFromJob(backupName);
+            if (StringUtils.isEmpty(repositoryName)) {
+                throw new CloudRuntimeException(String.format("Can't find any 
repository name for Job [name: %s].", backupName));
+            }
             final HttpResponse response = 
get(String.format("/backupServers/%s/repositories", backupServerId));
             checkResponseOK(response);
             final ObjectMapper objectMapper = new XmlMapper();
             final EntityReferences references = 
objectMapper.readValue(response.getEntity().getContent(), 
EntityReferences.class);
             for (final Ref ref : references.getRefs()) {
-                if (ref.getType().equals("RepositoryReference")) {
+                if (ref.getType().equals("RepositoryReference") && 
ref.getName().equals(repositoryName)) {
                     return ref;
                 }
             }
         } catch (final IOException e) {
-            LOG.error("Failed to list Veeam jobs due to:", e);
+            LOG.error(String.format("Failed to list Veeam backup repository 
used by backup job [name: %s] due to: [%s].", backupName, e.getMessage()), e);
             checkResponseTimeOut(e);
         }
         return null;
     }
 
+    private String getRepositoryNameFromJob(String backupName) {
+        final List<String> cmds = Arrays.asList(
+                String.format("$Job = Get-VBRJob -name \"%s\"", backupName),
+                "$Job.GetBackupTargetRepository() ^| select Name | Format-List"
+        );
+        Pair<Boolean, String> result = executePowerShellCommands(cmds);
+        if (result == null || !result.first()) {
+            throw new CloudRuntimeException(String.format("Failed to get 
Repository Name from Job [name: %s].", backupName));
+        }
+
+        for (String block : result.second().split("\n\n")) {
+           if (block.matches("Name(\\s)+:(.)*")) {
+               return block.split(":")[1].trim();
+           }

Review comment:
       Maybe a few unit test cases would be good, just to ensure this method 
sends the expected `cmds` (lines 364 - 367) and retrieves the correct String 
when processing the result (373 - 377).

##########
File path: 
plugins/backup/veeam/src/main/java/org/apache/cloudstack/backup/veeam/VeeamClient.java
##########
@@ -337,25 +337,47 @@ private boolean checkTaskStatus(final HttpResponse 
response) throws IOException
     //////////////// Public Veeam APIs /////////////////////
     ////////////////////////////////////////////////////////
 
-    public Ref listBackupRepository(final String backupServerId) {
-        LOG.debug("Trying to list backup repository for backup server id: " + 
backupServerId);
+    public Ref listBackupRepository(final String backupServerId, final String 
backupName) {
+        LOG.debug(String.format("Trying to list backup repository for backup 
job [name: %s] in server [id: %s].", backupName, backupServerId));
         try {
+            String repositoryName = getRepositoryNameFromJob(backupName);
+            if (StringUtils.isEmpty(repositoryName)) {
+                throw new CloudRuntimeException(String.format("Can't find any 
repository name for Job [name: %s].", backupName));
+            }
             final HttpResponse response = 
get(String.format("/backupServers/%s/repositories", backupServerId));
             checkResponseOK(response);
             final ObjectMapper objectMapper = new XmlMapper();
             final EntityReferences references = 
objectMapper.readValue(response.getEntity().getContent(), 
EntityReferences.class);
             for (final Ref ref : references.getRefs()) {
-                if (ref.getType().equals("RepositoryReference")) {
+                if (ref.getType().equals("RepositoryReference") && 
ref.getName().equals(repositoryName)) {
                     return ref;
                 }
             }
         } catch (final IOException e) {
-            LOG.error("Failed to list Veeam jobs due to:", e);
+            LOG.error(String.format("Failed to list Veeam backup repository 
used by backup job [name: %s] due to: [%s].", backupName, e.getMessage()), e);
             checkResponseTimeOut(e);
         }
         return null;
     }
 
+    private String getRepositoryNameFromJob(String backupName) {
+        final List<String> cmds = Arrays.asList(
+                String.format("$Job = Get-VBRJob -name \"%s\"", backupName),
+                "$Job.GetBackupTargetRepository() ^| select Name | Format-List"
+        );
+        Pair<Boolean, String> result = executePowerShellCommands(cmds);
+        if (result == null || !result.first()) {
+            throw new CloudRuntimeException(String.format("Failed to get 
Repository Name from Job [name: %s].", backupName));
+        }
+
+        for (String block : result.second().split("\n\n")) {
+           if (block.matches("Name(\\s)+:(.)*")) {
+               return block.split(":")[1].trim();
+           }
+        }
+        return null;

Review comment:
       You can already throw the exception that is thrown in case this method 
returns null or empty, instead of returning `null`.
   ```
   String repositoryName = getRepositoryNameFromJob(backupName);
   if (StringUtils.isEmpty(repositoryName)) {
      throw new CloudRuntimeException(String.format("Can't find any repository 
name for Job [name: %s].", backupName));
   }
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to