Vitor de Lima has uploaded a new change for review. Change subject: database, core: Add the UserProfile entity ......................................................................
database, core: Add the UserProfile entity Create an entity to store additional user data (currently just the ssh public key for serial console access). Includes the DAO, unit tests and the schema update. Change-Id: Idca5769f84d379fd20a184608667a7774cc465c8 Signed-off-by: Vitor de Lima <[email protected]> --- A backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/aaa/UserProfile.java M backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/DbFacade.java A backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/UserProfileDAO.java A backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/UserProfileDAODbFacadeImpl.java M backend/manager/modules/dal/src/main/jdbc-resources/engine-daos.properties M backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/DbFacadeDAOTest.java M backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/DbUserDAOTest.java A backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/UserProfileDAOTest.java M backend/manager/modules/dal/src/test/resources/fixtures.xml A packaging/dbscripts/upgrade/03_06_0870_add_user_profiles.sql A packaging/dbscripts/user_profiles_sp.sql 11 files changed, 498 insertions(+), 2 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/12/38012/1 diff --git a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/aaa/UserProfile.java b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/aaa/UserProfile.java new file mode 100644 index 0000000..e99036f --- /dev/null +++ b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/aaa/UserProfile.java @@ -0,0 +1,66 @@ +package org.ovirt.engine.core.common.businessentities.aaa; + +import org.ovirt.engine.core.common.businessentities.IVdcQueryable; +import org.ovirt.engine.core.common.utils.ObjectUtils; +import org.ovirt.engine.core.compat.Guid; + +public class UserProfile extends IVdcQueryable { + private static final long serialVersionUID = 7251907866347833460L; + + private Guid id; + + private Guid userId; + + private String sshPublicKey; + + public UserProfile() { + sshPublicKey = ""; + } + + public Guid getId() { + return id; + } + + public void setId(Guid id) { + this.id = id; + } + + public Guid getUserId() { + return userId; + } + + public void setUserId(Guid user_id) { + this.userId = user_id; + } + + public String getSshPublicKey() { + return sshPublicKey; + } + + public void setSshPublicKey(String sshPublicKey) { + this.sshPublicKey = sshPublicKey; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((sshPublicKey == null) ? 0 : sshPublicKey.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + UserProfile other = (UserProfile) obj; + return ObjectUtils.objectsEqual(sshPublicKey, other.sshPublicKey); + } +} diff --git a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/DbFacade.java b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/DbFacade.java index 3dc9685..8db35c4 100644 --- a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/DbFacade.java +++ b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/DbFacade.java @@ -47,6 +47,7 @@ import org.ovirt.engine.core.common.businessentities.VmStatic; import org.ovirt.engine.core.common.businessentities.VmStatistics; import org.ovirt.engine.core.common.businessentities.VmTemplate; +import org.ovirt.engine.core.common.businessentities.aaa.UserProfile; import org.ovirt.engine.core.common.businessentities.image_storage_domain_map; import org.ovirt.engine.core.common.businessentities.vds_spm_id_map; import org.ovirt.engine.core.common.businessentities.network.Network; @@ -105,6 +106,7 @@ import org.ovirt.engine.core.dao.StorageServerConnectionLunMapDAO; import org.ovirt.engine.core.dao.TagDAO; import org.ovirt.engine.core.dao.UnregisteredOVFDataDAO; +import org.ovirt.engine.core.dao.UserProfileDAO; import org.ovirt.engine.core.dao.VdcOptionDAO; import org.ovirt.engine.core.dao.VdsCpuStatisticsDAO; import org.ovirt.engine.core.dao.VdsDAO; @@ -216,6 +218,7 @@ put(FenceAgent.class, FenceAgentDAO.class); put(EngineSession.class, EngineSessionDAO.class); put(HostDevice.class, HostDeviceDao.class); + put(UserProfile.class, UserProfileDAO.class); } }; @@ -386,6 +389,15 @@ } /** + * Returns the singleton instance of {@link UserProfileDAO}. + * + * @return the dao + */ + public UserProfileDAO getUserProfileDao() { + return getDao(UserProfileDAO.class); + } + + /** * Returns the singleton instance of {@link VdsDAO}. * * @return the dao diff --git a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/UserProfileDAO.java b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/UserProfileDAO.java new file mode 100644 index 0000000..f8f43ad --- /dev/null +++ b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/UserProfileDAO.java @@ -0,0 +1,57 @@ +package org.ovirt.engine.core.dao; + +import org.ovirt.engine.core.common.businessentities.aaa.UserProfile; +import org.ovirt.engine.core.compat.Guid; + +import java.util.List; + +public interface UserProfileDAO extends DAO { + /** + * Retrieves the user profile with the specified id. + * + * @param id + * the id + * @return the user profile, or <code>null</code> if the id was invalid + */ + UserProfile get(Guid id); + + /** + * Retrieves the user profile associated with the specified user id. + * + * @param userId + * the user id + * @return the user profile, or <code>null</code> if the id was invalid + */ + UserProfile getByUserId(Guid userId); + + /** + * Retrieves all user profiles. + * + * @return the collection of all user profiles + */ + List<UserProfile> getAll(); + + /** + * Saves the user profile. + * + * @param profile + * the user profile + */ + void save(UserProfile profile); + + /** + * Updates the specified user profile in the database. + * + * @param profile + * the user profile + */ + void update(UserProfile profile); + + /** + * Removes the user profile with the specified id. + * + * @param id + * the user profile id + */ + void remove(Guid id); +} diff --git a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/UserProfileDAODbFacadeImpl.java b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/UserProfileDAODbFacadeImpl.java new file mode 100644 index 0000000..0624b7b --- /dev/null +++ b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/UserProfileDAODbFacadeImpl.java @@ -0,0 +1,79 @@ +package org.ovirt.engine.core.dao; + +import org.ovirt.engine.core.common.businessentities.aaa.UserProfile; +import org.ovirt.engine.core.compat.Guid; +import org.ovirt.engine.core.dal.dbbroker.CustomMapSqlParameterSource; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; + +public class UserProfileDAODbFacadeImpl extends BaseDAODbFacade implements UserProfileDAO { + + private static final class UserProfileRowMapper implements RowMapper<UserProfile> { + public static final UserProfileRowMapper instance = new UserProfileRowMapper(); + + @Override + public UserProfile mapRow(ResultSet rs, int rowNum) + throws SQLException { + UserProfile entity = new UserProfile(); + entity.setId(getGuidDefaultEmpty(rs, "profile_id")); + entity.setUserId(getGuidDefaultEmpty(rs, "user_id")); + entity.setSshPublicKey(rs.getString("ssh_public_key")); + return entity; + } + } + + private class UserProfileMapSqlParameterSource extends + CustomMapSqlParameterSource { + public UserProfileMapSqlParameterSource(UserProfile profile) { + super(dialect); + addValue("profile_id", profile.getId()); + addValue("user_id", profile.getUserId()); + addValue("ssh_public_key", profile.getSshPublicKey()); + } + } + + @Override + public UserProfile get(Guid id) { + MapSqlParameterSource parameterSource = getCustomMapSqlParameterSource() + .addValue("profile_id", id); + + return getCallsHandler().executeRead("GetUserProfileByProfileId", UserProfileRowMapper.instance, parameterSource); + } + + @Override + public UserProfile getByUserId(Guid id) { + MapSqlParameterSource parameterSource = getCustomMapSqlParameterSource() + .addValue("user_id", id); + + return getCallsHandler().executeRead("GetUserProfileByUserId", UserProfileRowMapper.instance, parameterSource); + } + + @Override + public List<UserProfile> getAll() { + MapSqlParameterSource parameterSource = getCustomMapSqlParameterSource(); + return getCallsHandler().executeReadList("GetAllFromUserProfiles", UserProfileRowMapper.instance, parameterSource); + } + + @Override + public void save(UserProfile profile) { + getCallsHandler().executeModification("InsertUserProfile", new UserProfileMapSqlParameterSource(profile)); + + } + + @Override + public void update(UserProfile profile) { + getCallsHandler().executeModification("UpdateUserProfile", new UserProfileMapSqlParameterSource(profile)); + } + + @Override + public void remove(Guid id) { + MapSqlParameterSource parameterSource = getCustomMapSqlParameterSource() + .addValue("profile_id", id); + + getCallsHandler().executeModification("DeleteUserProfile", parameterSource); + } +} diff --git a/backend/manager/modules/dal/src/main/jdbc-resources/engine-daos.properties b/backend/manager/modules/dal/src/main/jdbc-resources/engine-daos.properties index 1d03aa2..d94c82f 100644 --- a/backend/manager/modules/dal/src/main/jdbc-resources/engine-daos.properties +++ b/backend/manager/modules/dal/src/main/jdbc-resources/engine-daos.properties @@ -2,6 +2,7 @@ CommandEntityDao=org.ovirt.engine.core.dao.CommandEntityDaoDbFacadeImpl DbUserDAO=org.ovirt.engine.core.dao.DbUserDAODbFacadeImpl EngineSessionDAO=org.ovirt.engine.core.dao.EngineSessionDAODbFacadeImpl +UserProfileDAO=org.ovirt.engine.core.dao.UserProfileDAODbFacadeImpl VdsDAO=org.ovirt.engine.core.dao.VdsDAODbFacadeImpl VdsStaticDAO=org.ovirt.engine.core.dao.VdsStaticDAODbFacadeImpl VdsDynamicDAO=org.ovirt.engine.core.dao.VdsDynamicDAODbFacadeImpl diff --git a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/DbFacadeDAOTest.java b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/DbFacadeDAOTest.java index 3fc6d7c..958b817 100644 --- a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/DbFacadeDAOTest.java +++ b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/DbFacadeDAOTest.java @@ -66,7 +66,7 @@ private static final Guid ADMIN_ROLE_TYPE_FROM_FIXTURE_ID = new Guid("F5972BFA-7102-4D33-AD22-9DD421BFBA78"); private static final Guid SYSTEM_OBJECT_ID = new Guid("AAA00000-0000-0000-0000-123456789AAA"); private static final int NUM_OF_VM_IN_FIXTURES_WITH_STATUS_MIGRATING_FROM = 2; - private static final int NUM_OF_USERS_IN_FIXTURES = 2; + private static final int NUM_OF_USERS_IN_FIXTURES = 4; private static final Guid STORAGE_POOL_WITH_MASTER_UP = new Guid("386BFFD1-E7ED-4B08-BCE9-D7DF10F8C9A0"); private static final Guid STORAGE_POOL_WITH_MASTER_DOWN = new Guid("72B9E200-F48B-4687-83F2-62828F249A47"); private static final Guid VM_STATIC_GUID = new Guid("77296e00-0cad-4e5a-9299-008a7b6f4354"); diff --git a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/DbUserDAOTest.java b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/DbUserDAOTest.java index a106568..1e617c4 100644 --- a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/DbUserDAOTest.java +++ b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/DbUserDAOTest.java @@ -146,7 +146,7 @@ assertNotNull(result); assertFalse(result.isEmpty()); - assertEquals(2, result.size()); + assertEquals(4, result.size()); } @Test diff --git a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/UserProfileDAOTest.java b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/UserProfileDAOTest.java new file mode 100644 index 0000000..34d3e442 --- /dev/null +++ b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/UserProfileDAOTest.java @@ -0,0 +1,116 @@ +package org.ovirt.engine.core.dao; + +import org.junit.Before; +import org.junit.Test; +import org.ovirt.engine.core.common.businessentities.aaa.UserProfile; +import org.ovirt.engine.core.compat.Guid; + +import java.util.List; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +public class UserProfileDAOTest extends BaseDAOTestCase { + private UserProfileDAO dao; + private UserProfile existingProfile; + private UserProfile deletableProfile; + private UserProfile newProfile; + + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + + dao = dbFacade.getUserProfileDao(); + + existingProfile = dao.get(new Guid("38cb5663-96bc-485c-834a-cbbc03acc820")); + deletableProfile = dao.get(new Guid("38cb5663-96bc-485c-834a-cbbc03acc821")); + + newProfile = new UserProfile(); + + newProfile.setId(Guid.newGuid()); + newProfile.setUserId(new Guid("81940459-2ec4-4afa-bbaa-22549555293c")); + newProfile.setSshPublicKey("key3"); + } + + /** + * Ensures that trying to get a user profile using an invalid id fails. + */ + @Test + public void testGetWithInvalidId() { + UserProfile result = dao.get(Guid.newGuid()); + + assertNull(result); + } + + /** + * Ensures that retrieving an user profile by id works as expected. + */ + @Test + public void testGet() { + UserProfile result = dao.get(existingProfile.getId()); + + assertNotNull(result); + assertEquals(existingProfile, result); + } + + /** + * Ensures that retrieving an user profile by user id works as expected. + */ + @Test + public void testGetByUserId() { + UserProfile result = dao.getByUserId(existingProfile.getUserId()); + + assertNotNull(result); + assertEquals(existingProfile, result); + } + + @Test + public void testGetAll() { + List<UserProfile> result = dao.getAll(); + + assertNotNull(result); + assertFalse(result.isEmpty()); + assertEquals(2, result.size()); + } + + /** + * Ensures that saving a user profile works as expected. + */ + @Test + public void testSave() { + dao.save(newProfile); + + UserProfile result = dao.get(newProfile.getId()); + + assertEquals(newProfile, result); + } + + /** + * Ensures that updating a user profile works as expected. + */ + @Test + public void testUpdate() { + existingProfile.setSshPublicKey("key4"); + + dao.update(existingProfile); + + UserProfile result = dao.get(existingProfile.getId()); + + assertEquals(existingProfile, result); + } + + /** + * Ensures that removing user profiles works as expected. + */ + @Test + public void testRemove() { + dao.remove(deletableProfile.getId()); + + UserProfile result = dao.get(deletableProfile.getId()); + + assertNull(result); + } +} diff --git a/backend/manager/modules/dal/src/test/resources/fixtures.xml b/backend/manager/modules/dal/src/test/resources/fixtures.xml index 0201703..b3a2cd2 100644 --- a/backend/manager/modules/dal/src/test/resources/fixtures.xml +++ b/backend/manager/modules/dal/src/test/resources/fixtures.xml @@ -4959,6 +4959,48 @@ <value>0</value> <value>*</value> </row> + <row> + <value>81940459-2ec4-4afa-bbaa-22549555293b</value> + <value>c</value> + <value>userportal4</value> + <null /> + <value>testportal.redhat.com</value> + <value>[email protected]</value> + <null /> + <null /> + <null /> + <value>0</value> + <value>*</value> + </row> + <row> + <value>81940459-2ec4-4afa-bbaa-22549555293c</value> + <value>d</value> + <value>userportal4</value> + <null /> + <value>testportal.redhat.com</value> + <value>[email protected]</value> + <null /> + <null /> + <null /> + <value>0</value> + <value>*</value> + </row> + </table> + + <table name="user_profiles"> + <column>profile_id</column> + <column>user_id</column> + <column>ssh_public_key</column> + <row> + <value>38cb5663-96bc-485c-834a-cbbc03acc820</value> + <value>9bf7c640-b620-456f-a550-0348f366544a</value> + <value>key1</value> + </row> + <row> + <value>38cb5663-96bc-485c-834a-cbbc03acc821</value> + <value>81940459-2ec4-4afa-bbaa-22549555293b</value> + <value>key2</value> + </row> </table> <table name="tags_user_map"> diff --git a/packaging/dbscripts/upgrade/03_06_0870_add_user_profiles.sql b/packaging/dbscripts/upgrade/03_06_0870_add_user_profiles.sql new file mode 100644 index 0000000..c0a3806 --- /dev/null +++ b/packaging/dbscripts/upgrade/03_06_0870_add_user_profiles.sql @@ -0,0 +1,17 @@ +-- ---------------------------------------------------------------------- +-- table user_profiles +-- ---------------------------------------------------------------------- + +CREATE TABLE user_profiles +( + profile_id UUID NOT NULL, + user_id UUID NOT NULL, + ssh_public_key TEXT, + CONSTRAINT PK_profile_id PRIMARY KEY (profile_id) +) WITH OIDS; + +ALTER TABLE user_profiles ADD CONSTRAINT fk_user_id FOREIGN KEY (user_id) + REFERENCES users (user_id) + ON UPDATE NO ACTION ON DELETE CASCADE; + +CREATE INDEX IDX_user_profiles_user_id ON user_profiles(user_id); diff --git a/packaging/dbscripts/user_profiles_sp.sql b/packaging/dbscripts/user_profiles_sp.sql new file mode 100644 index 0000000..1aee4d0 --- /dev/null +++ b/packaging/dbscripts/user_profiles_sp.sql @@ -0,0 +1,106 @@ + + + + + + +---------------------------------------------------------------- +-- [user_profiles] Table +-- + + + + + +Create or replace FUNCTION InsertUserProfile( + v_profile_id UUID, + v_user_id UUID, + v_ssh_public_key TEXT) +RETURNS VOID +AS $procedure$ +BEGIN + INSERT INTO user_profiles ( + profile_id, + user_id, + ssh_public_key) + VALUES( + v_profile_id, + v_user_id, + v_ssh_public_key); +END; $procedure$ +LANGUAGE plpgsql; + + + + + +Create or replace FUNCTION UpdateUserProfile( + v_profile_id UUID, + v_user_id UUID, + v_ssh_public_key TEXT) +RETURNS VOID +AS $procedure$ +BEGIN + UPDATE user_profiles + SET profile_id = v_profile_id, + user_id = v_user_id, + ssh_public_key = v_ssh_public_key + WHERE profile_id = v_profile_id; +END; $procedure$ +LANGUAGE plpgsql; + + + + + +Create or replace FUNCTION DeleteUserProfile(v_profile_id UUID) +RETURNS VOID +AS $procedure$ +BEGIN + DELETE + FROM user_profiles + WHERE profile_id = v_profile_id; +END; $procedure$ +LANGUAGE plpgsql; + + + + + +Create or replace FUNCTION GetAllFromUserProfiles() RETURNS SETOF user_profiles STABLE + AS $procedure$ +BEGIN + RETURN QUERY SELECT user_profiles.* + FROM user_profiles; +END; $procedure$ +LANGUAGE plpgsql; + + + + + +Create or replace FUNCTION GetUserProfileByUserId(v_user_id UUID) +RETURNS SETOF user_profiles STABLE +AS $procedure$ +BEGIN + RETURN QUERY + SELECT * + FROM user_profiles + WHERE user_id = v_user_id; +END; $procedure$ +LANGUAGE plpgsql; + + + + + +Create or replace FUNCTION GetUserProfileByProfileId(v_profile_id UUID) +RETURNS SETOF user_profiles STABLE +AS $procedure$ +BEGIN + RETURN QUERY + SELECT * + FROM user_profiles + WHERE profile_id = v_profile_id; +END; $procedure$ +LANGUAGE plpgsql; -- To view, visit http://gerrit.ovirt.org/38012 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Idca5769f84d379fd20a184608667a7774cc465c8 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Vitor de Lima <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
