Repository: ambari Updated Branches: refs/heads/trunk 9c07b8b54 -> af8b1cdfe
AMBARI-19891 - Extend View URL's to auto instances This commit creates a default view URL for each view using the view name.(Ashwin Rajeev via gauravn7) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/af8b1cdf Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/af8b1cdf Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/af8b1cdf Branch: refs/heads/trunk Commit: af8b1cdfee14c11c2b37a0da6faa1aa3361ddf36 Parents: 9c07b8b Author: Gaurav Nagar <[email protected]> Authored: Thu Feb 9 15:59:09 2017 +0530 Committer: Gaurav Nagar <[email protected]> Committed: Thu Feb 9 15:59:35 2017 +0530 ---------------------------------------------------------------------- .../apache/ambari/server/orm/dao/ViewDAO.java | 8 ++- .../apache/ambari/server/view/ViewRegistry.java | 62 ++++++++++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/af8b1cdf/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ViewDAO.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ViewDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ViewDAO.java index ab6dcf4..192e426 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ViewDAO.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ViewDAO.java @@ -26,6 +26,7 @@ import javax.persistence.TypedQuery; import org.apache.ambari.server.orm.RequiresSession; import org.apache.ambari.server.orm.entities.ViewEntity; +import com.google.common.collect.Lists; import com.google.inject.Inject; import com.google.inject.Provider; import com.google.inject.Singleton; @@ -62,15 +63,16 @@ public class ViewDAO { * @return a matching view or null */ @RequiresSession - public ViewEntity findByCommonName(String viewCommonName) { + public List<ViewEntity> findByCommonName(String viewCommonName) { + List<ViewEntity> list = Lists.newArrayList(); if (viewCommonName != null) { for (ViewEntity viewEntity : findAll()) { if (viewCommonName.equals(viewEntity.getCommonName())) { - return viewEntity; + list.add(viewEntity); } } } - return null; + return list; } /** http://git-wip-us.apache.org/repos/asf/ambari/blob/af8b1cdf/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java index 6316b5b..81c4734 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java @@ -71,6 +71,7 @@ import org.apache.ambari.server.orm.dao.ResourceTypeDAO; import org.apache.ambari.server.orm.dao.UserDAO; import org.apache.ambari.server.orm.dao.ViewDAO; import org.apache.ambari.server.orm.dao.ViewInstanceDAO; +import org.apache.ambari.server.orm.dao.ViewURLDAO; import org.apache.ambari.server.orm.entities.GroupEntity; import org.apache.ambari.server.orm.entities.MemberEntity; import org.apache.ambari.server.orm.entities.PermissionEntity; @@ -86,6 +87,7 @@ import org.apache.ambari.server.orm.entities.ViewInstanceDataEntity; import org.apache.ambari.server.orm.entities.ViewInstanceEntity; import org.apache.ambari.server.orm.entities.ViewParameterEntity; import org.apache.ambari.server.orm.entities.ViewResourceEntity; +import org.apache.ambari.server.orm.entities.ViewURLEntity; import org.apache.ambari.server.security.SecurityHelper; import org.apache.ambari.server.security.authorization.AuthorizationHelper; import org.apache.ambari.server.security.authorization.ResourceType; @@ -125,6 +127,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.xml.sax.SAXException; +import com.google.common.base.Optional; +import com.google.common.base.Preconditions; import com.google.common.collect.Sets; import com.google.common.eventbus.AllowConcurrentEvents; import com.google.common.eventbus.Subscribe; @@ -133,6 +137,7 @@ import com.google.inject.Guice; import com.google.inject.Injector; import com.google.inject.persist.Transactional; + /** * Registry for view and view instance definitions. */ @@ -153,6 +158,7 @@ public class ViewRegistry { private static final String LOG4J = "log4j."; public static final String API_PREFIX = "/api/v1/clusters/"; + public static final String DEFAULT_AUTO_INSTANCE_URL = "auto_instance"; /** * Thread pool @@ -317,6 +323,9 @@ public class ViewRegistry { @Inject RemoteAmbariClusterDAO remoteAmbariClusterDAO; + @Inject + ViewURLDAO viewURLDAO; + // ----- Constructors ----------------------------------------------------- /** @@ -997,6 +1006,59 @@ public class ViewRegistry { viewInstanceEntity.setClusterHandle(clusterId); installViewInstance(viewInstanceEntity); setViewInstanceRoleAccess(viewInstanceEntity, roles); + try { + setViewUrl(viewInstanceEntity); + } catch (Exception urlCreateException) { + LOG.error("Error while creating an auto URL for the view instance {}, Url should be created in view instance settings", viewInstanceEntity.getViewName()); + LOG.error("View URL creation error ", urlCreateException); + } + + } + + private String getUrlName(ViewInstanceEntity viewInstanceEntity) { + return viewInstanceEntity.getViewEntity().getCommonName().toLowerCase() + "_" + viewInstanceEntity.getInstanceName().toLowerCase(); + } + + private void setViewUrl(ViewInstanceEntity instanceEntity) { + ViewInstanceEntity viewInstanceEntity = instanceDAO.findByName(instanceEntity.getViewName(), instanceEntity.getInstanceName()); + Preconditions.checkNotNull(viewInstanceEntity); + ViewURLEntity viewUrl = viewInstanceEntity.getViewUrl(); + // check if there is a URL attached + if (viewUrl != null) { + LOG.warn("Url exists for the auto instance {}, new url will not be created", viewInstanceEntity.getViewName()); + return; + } + + String urlName = getUrlName(viewInstanceEntity); + //Check if a URL exists with the same name + Optional<ViewURLEntity> existingUrl = viewURLDAO.findByName(urlName); + // remove any pre-existing URL's before creating new URL's + ViewURLEntity urlEntity = new ViewURLEntity(); + urlEntity.setUrlName(urlName); + + urlEntity.setUrlSuffix(viewInstanceEntity.getInstanceName().toLowerCase()); + + ViewURLEntity toSaveOrUpdate = existingUrl.or(urlEntity); + toSaveOrUpdate.setViewInstanceEntity(viewInstanceEntity); + + if (existingUrl.isPresent()) { + LOG.info("Url already present for {}", viewInstanceEntity.getViewName()); + viewURLDAO.update(toSaveOrUpdate); + } else { + LOG.info("Creating a new URL for auto instance {}", viewInstanceEntity.getViewName()); + viewURLDAO.save(urlEntity); + } + // Update the view with the URL + viewInstanceEntity.setViewUrl(urlEntity); + try { + updateViewInstance(viewInstanceEntity); + } catch (Exception ex) { + LOG.error("Could not update the view instance with new URL, removing URL", ex); + // Clean up + Optional<ViewURLEntity> viewURLDAOByName = viewURLDAO.findByName(urlName); + if (viewURLDAOByName.isPresent()) + viewURLDAO.delete(viewURLDAOByName.get()); + } } @Subscribe
