Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Last backup api extension #993

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.ovirt.engine.core.common.AuditLogType;
import org.ovirt.engine.core.common.BackendService;
import org.ovirt.engine.core.common.businessentities.EngineBackupLog;
import org.ovirt.engine.core.common.businessentities.EngineBackupScope;
import org.ovirt.engine.core.common.config.Config;
import org.ovirt.engine.core.common.config.ConfigValues;
import org.ovirt.engine.core.dal.dbbroker.auditloghandling.AuditLogDirector;
Expand All @@ -32,21 +33,6 @@
@Singleton
public class EngineBackupAwarenessManager implements BackendService {

private enum BackupScope {
DB("db"),
FILES("files");

String name;

BackupScope(String name) {
this.name = name;
}

public String getName() {
return name;
}
}

private static final Logger log = LoggerFactory.getLogger(EngineBackupAwarenessManager.class);
private Lock lock = new ReentrantLock();
@Inject
Expand Down Expand Up @@ -96,8 +82,8 @@ private void doBackupCheck() {
AuditLogable alert = new AuditLogableImpl();

//try to get last backup record
EngineBackupLog lastDbBackup = getLastBackupByScope(BackupScope.DB);
EngineBackupLog lastFilesBackup = getLastBackupByScope(BackupScope.FILES);
EngineBackupLog lastDbBackup = getLastBackupByScope(EngineBackupScope.DB);
EngineBackupLog lastFilesBackup = getLastBackupByScope(EngineBackupScope.FILES);
if (lastDbBackup == null || lastFilesBackup == null) {
auditLogDirector.log(alert, AuditLogType.ENGINE_NO_FULL_BACKUP);
} else {
Expand All @@ -118,7 +104,7 @@ private void doBackupCheck() {

}

private EngineBackupLog getLastBackupByScope(BackupScope scope) {
private EngineBackupLog getLastBackupByScope(EngineBackupScope scope) {
return engineBackupLogDao.getLastSuccessfulEngineBackup(scope.getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.ovirt.engine.core.bll.aaa;

import javax.inject.Inject;

import org.ovirt.engine.core.bll.QueriesCommandBase;
import org.ovirt.engine.core.bll.context.EngineContext;
import org.ovirt.engine.core.common.businessentities.EngineBackupLog;
import org.ovirt.engine.core.common.queries.GetLastEngineBackupParameters;
import org.ovirt.engine.core.dao.EngineBackupLogDao;

public class GetLastEngineBackupQuery<P extends GetLastEngineBackupParameters> extends QueriesCommandBase<P> {

@Inject
private EngineBackupLogDao engineBackupLogDao;

public GetLastEngineBackupQuery(P parameters, EngineContext engineContext) {
super(parameters, engineContext);
}

@Override
protected void executeQueryCommand() {
EngineBackupLog log = (EngineBackupLog) engineBackupLogDao.getLastSuccessfulEngineBackup(getParameters().getEngineBackupScope());
getQueryReturnValue().setReturnValue(log);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.ovirt.engine.core.common.businessentities;

public enum EngineBackupScope {

DB("db"),
FILES("files"),
DWH("dwhdb"),
CINDER("cinderlib"),
KEYCLOAK("keycloak"),
GRAFANA("grafanadb");

String name;

EngineBackupScope(String name) {
this.name = name;
}

public String getName() {
return name;
}

public static EngineBackupScope fromString(String name) {
for (EngineBackupScope scope : EngineBackupScope.values()) {
if (scope.getName().equalsIgnoreCase(name)) {
return scope;
}
}
throw new IllegalArgumentException("No enum constant for name " + name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.ovirt.engine.core.common.queries;

import org.ovirt.engine.core.common.businessentities.EngineBackupScope;

public class GetLastEngineBackupParameters extends QueryParametersBase {

private EngineBackupScope scope;

public GetLastEngineBackupParameters() {
}

public GetLastEngineBackupParameters(EngineBackupScope scope) {
this.scope = scope;
}

public void setEngineBackupScope(EngineBackupScope scope) {
this.scope = scope;
}

public String getEngineBackupScope() {
return scope.getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,10 @@ public enum QueryType implements Serializable {
GetSystemOption(QueryAuthType.User),

// Default type instead of having to null check
Unknown(QueryAuthType.User);
Unknown(QueryAuthType.User),

// Last Engine Backup
GetLastEngineBackup;

/**
* What kind of authorization the query requires. Although this is essentially a <code>boolean</code>, it's
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.ovirt.engine.api.model.ApiSummaryItem;
import org.ovirt.engine.api.model.BaseResource;
import org.ovirt.engine.api.model.DetailedLink;
import org.ovirt.engine.api.model.EngineBackupInfo;
import org.ovirt.engine.api.model.ProductInfo;
import org.ovirt.engine.api.model.Rsdl;
import org.ovirt.engine.api.model.SpecialObjects;
Expand Down Expand Up @@ -95,10 +96,13 @@
import org.ovirt.engine.core.branding.BrandingManager;
import org.ovirt.engine.core.common.action.ActionParametersBase;
import org.ovirt.engine.core.common.action.ActionType;
import org.ovirt.engine.core.common.businessentities.EngineBackupLog;
import org.ovirt.engine.core.common.businessentities.EngineBackupScope;
import org.ovirt.engine.core.common.businessentities.aaa.DbUser;
import org.ovirt.engine.core.common.config.ConfigValues;
import org.ovirt.engine.core.common.constants.QueryConstants;
import org.ovirt.engine.core.common.mode.ApplicationMode;
import org.ovirt.engine.core.common.queries.GetLastEngineBackupParameters;
import org.ovirt.engine.core.common.queries.GetSystemStatisticsQueryParameters;
import org.ovirt.engine.core.common.queries.QueryParametersBase;
import org.ovirt.engine.core.common.queries.QueryReturnValue;
Expand Down Expand Up @@ -282,13 +286,50 @@ public Response get() {
//(https://bugzilla.redhat.com/1612124)
if (!isFiltered()) {
addSummary(api);
// Backup info also admin-only
EngineBackupInfo lastEngineBackupInfo = getLastEngineBackup();
api.setEngineBackup(lastEngineBackupInfo);
}
}
setAuthenticatedUser(api);
return getResponseBuilder(api).entity(api).build();
}
}

private EngineBackupInfo getLastEngineBackup() {
EngineBackupInfo backupInfo = new EngineBackupInfo();
for (EngineBackupScope backupScope : EngineBackupScope.values()) {
QueryReturnValue lastBackupQuery = runQuery(QueryType.GetLastEngineBackup,
new GetLastEngineBackupParameters(backupScope));
EngineBackupLog backupLog = lastBackupQuery.getReturnValue();
if (backupLog != null) {
switch (backupScope) {
case DB:
backupInfo.setLastDbBackup(DateMapper.map(backupLog.getDoneAt(), null));
break;
case DWH:
backupInfo.setLastDwhBackup(DateMapper.map(backupLog.getDoneAt(), null));
break;
case CINDER:
backupInfo.setLastCinderBackup(DateMapper.map(backupLog.getDoneAt(), null));
break;
case KEYCLOAK:
backupInfo.setLastKeycloakBackup(DateMapper.map(backupLog.getDoneAt(), null));
break;
case GRAFANA:
backupInfo.setLastGrafanaBackup(DateMapper.map(backupLog.getDoneAt(), null));
break;
case FILES:
backupInfo.setLastEngineBackup(DateMapper.map(backupLog.getDoneAt(), null));
break;
default:
break;
}
}
}
return backupInfo;
}

/**
* Set a link to the user of the current session
* (the 'authenticated user') in the API object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.ovirt.engine.api.restapi.test.util.TestHelper.eqParams;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand All @@ -22,18 +24,22 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatcher;
import org.ovirt.engine.api.model.Api;
import org.ovirt.engine.api.model.Link;
import org.ovirt.engine.api.model.SpecialObjects;
import org.ovirt.engine.api.restapi.invocation.Current;
import org.ovirt.engine.api.restapi.invocation.CurrentManager;
import org.ovirt.engine.api.restapi.invocation.VersionSource;
import org.ovirt.engine.api.restapi.logging.MessageBundle;
import org.ovirt.engine.core.common.businessentities.EngineBackupLog;
import org.ovirt.engine.core.common.businessentities.EngineBackupScope;
import org.ovirt.engine.core.common.businessentities.aaa.DbUser;
import org.ovirt.engine.core.common.config.ConfigValues;
import org.ovirt.engine.core.common.interfaces.BackendLocal;
import org.ovirt.engine.core.common.mode.ApplicationMode;
import org.ovirt.engine.core.common.queries.GetConfigurationValueParameters;
import org.ovirt.engine.core.common.queries.GetLastEngineBackupParameters;
import org.ovirt.engine.core.common.queries.GetSystemStatisticsQueryParameters;
import org.ovirt.engine.core.common.queries.QueryParametersBase;
import org.ovirt.engine.core.common.queries.QueryReturnValue;
Expand Down Expand Up @@ -306,6 +312,7 @@ private void setupExpectations(ApplicationMode appMode) {
setUpGetInstanceIdExpectations();
setUpGetUserBySessionExpectations();
setUpGetSystemStatisticsExpectations();
setUpGetLastDbBackendExpectations();
}

protected void doTestGlusterOnlyGet() {
Expand Down Expand Up @@ -465,6 +472,22 @@ private QueryParametersBase getProductVersionParams() {
return eqParams(QueryParametersBase.class, new String[0], new Object[0]);
}

protected void setUpGetLastDbBackendExpectations() {
QueryReturnValue queryResult = new QueryReturnValue();
queryResult.setSucceeded(true);
EngineBackupLog emptyReturn = new EngineBackupLog();
emptyReturn.setDoneAt(new Date());
emptyReturn.setScope(EngineBackupScope.DB.getName());
queryResult.setReturnValue(emptyReturn);
when(backend.runQuery(eq(QueryType.GetLastEngineBackup), argThat(new ArgumentMatcher<GetLastEngineBackupParameters>() {
@Override
public boolean matches(GetLastEngineBackupParameters argument) {
return argument != null && argument.getEngineBackupScope() != null; // Match any non-null EngineBackupScope
}
}
))).thenReturn(queryResult);
}

protected void setUpGetSystemStatisticsExpectations() {
QueryReturnValue queryResult = new QueryReturnValue();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,9 @@
<!-- Cpu topology -->
<include name="common/businessentities/VdsCpuUnit.java"/>

<!-- For GetLastEngineBackupParameters -->
<include name="common/businessentities/EngineBackupScope.java"/>

</source>

<super-source path="ui/uioverrides" />
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<!-- oVirt dependencies versions -->
<kubevirt-client.version>0.5.0</kubevirt-client.version>
<metamodel.version>1.3.10</metamodel.version>
<model.version>4.6.0</model.version>
<model.version>4.6.1-SNAPSHOT</model.version>
<ovirt-engine-extensions-api.version>1.0.1</ovirt-engine-extensions-api.version>
<vdsm-jsonrpc-java.version>1.7.2</vdsm-jsonrpc-java.version>

Expand Down
Loading