Repository: ambari Updated Branches: refs/heads/trunk 2f2e37267 -> fe702ab2a
AMBARI-10445. Populate cluster widgets from stack-defined widgets on cluster/service create. (swagle) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/c71624a6 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/c71624a6 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/c71624a6 Branch: refs/heads/trunk Commit: c71624a60654b1ed2ef04068fe986a0409fdd648 Parents: 2f2e372 Author: Siddharth Wagle <swa...@hortonworks.com> Authored: Fri Apr 10 15:09:08 2015 -0700 Committer: Siddharth Wagle <swa...@hortonworks.com> Committed: Fri Apr 10 15:09:08 2015 -0700 ---------------------------------------------------------------------- .../api/services/WidgetLayoutService.java | 17 ++- .../server/api/services/WidgetService.java | 17 ++- .../controller/AmbariManagementController.java | 9 ++ .../AmbariManagementControllerImpl.java | 139 ++++++++++++++++- .../internal/ClusterResourceProvider.java | 33 ++-- .../internal/ServiceResourceProvider.java | 12 +- .../internal/StackArtifactResourceProvider.java | 4 +- .../internal/WidgetLayoutResourceProvider.java | 19 ++- .../internal/WidgetResourceProvider.java | 19 ++- .../apache/ambari/server/orm/dao/WidgetDAO.java | 11 ++ .../ambari/server/orm/dao/WidgetLayoutDAO.java | 11 ++ .../server/orm/entities/WidgetEntity.java | 7 +- .../server/orm/entities/WidgetLayoutEntity.java | 3 +- .../ambari/server/state/stack/WidgetLayout.java | 7 + .../server/state/stack/WidgetLayoutInfo.java | 2 +- .../main/resources/Ambari-DDL-MySQL-CREATE.sql | 4 +- .../main/resources/Ambari-DDL-Oracle-CREATE.sql | 4 +- .../resources/Ambari-DDL-Postgres-CREATE.sql | 4 +- .../Ambari-DDL-Postgres-EMBEDDED-CREATE.sql | 4 +- .../AmbariManagementControllerTest.java | 150 +++++++++++++------ .../OTHER/2.0/services/HBASE/widgets.json | 2 +- 21 files changed, 374 insertions(+), 104 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/c71624a6/ambari-server/src/main/java/org/apache/ambari/server/api/services/WidgetLayoutService.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/WidgetLayoutService.java b/ambari-server/src/main/java/org/apache/ambari/server/api/services/WidgetLayoutService.java index 234b773..fad83cb 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/WidgetLayoutService.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/WidgetLayoutService.java @@ -28,6 +28,7 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; import java.nio.charset.Charset; import java.util.HashMap; +import java.util.List; import java.util.Map; /** @@ -119,13 +120,15 @@ public class WidgetLayoutService extends BaseService { } private String getUserName(HttpHeaders headers) { - String authorizationString = headers.getRequestHeaders().get("Authorization").get(0); - if (authorizationString != null && authorizationString.startsWith("Basic")) { - String base64Credentials = authorizationString.substring("Basic".length()).trim(); - String clearCredentials = new String(Base64.decode(base64Credentials),Charset.forName("UTF-8")); - return clearCredentials.split(":", 2)[0]; - } else { - return null; + List<String> authorizationHeaders = headers.getRequestHeaders().get("Authorization"); + if (authorizationHeaders != null && !authorizationHeaders.isEmpty()){ + String authorizationString = authorizationHeaders.get(0); + if (authorizationString != null && authorizationString.startsWith("Basic")) { + String base64Credentials = authorizationString.substring("Basic".length()).trim(); + String clearCredentials = new String(Base64.decode(base64Credentials),Charset.forName("UTF-8")); + return clearCredentials.split(":", 2)[0]; + } } + return null; } } http://git-wip-us.apache.org/repos/asf/ambari/blob/c71624a6/ambari-server/src/main/java/org/apache/ambari/server/api/services/WidgetService.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/WidgetService.java b/ambari-server/src/main/java/org/apache/ambari/server/api/services/WidgetService.java index ead0985..07dae40 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/WidgetService.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/WidgetService.java @@ -28,6 +28,7 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; import java.nio.charset.Charset; import java.util.HashMap; +import java.util.List; import java.util.Map; /** @@ -119,13 +120,15 @@ public class WidgetService extends BaseService { } private String getUserName(HttpHeaders headers) { - String authorizationString = headers.getRequestHeaders().get("Authorization").get(0); - if (authorizationString != null && authorizationString.startsWith("Basic")) { - String base64Credentials = authorizationString.substring("Basic".length()).trim(); - String clearCredentials = new String(Base64.decode(base64Credentials), Charset.forName("UTF-8")); - return clearCredentials.split(":", 2)[0]; - } else { - return null; + List<String> authorizationHeaders = headers.getRequestHeaders().get("Authorization"); + if (authorizationHeaders != null && !authorizationHeaders.isEmpty()) { + String authorizationString = authorizationHeaders.get(0); + if (authorizationString != null && authorizationString.startsWith("Basic")) { + String base64Credentials = authorizationString.substring("Basic".length()).trim(); + String clearCredentials = new String(Base64.decode(base64Credentials), Charset.forName("UTF-8")); + return clearCredentials.split(":", 2)[0]; + } } + return null; } } http://git-wip-us.apache.org/repos/asf/ambari/blob/c71624a6/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementController.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementController.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementController.java index 613d5f5..e83e820 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementController.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementController.java @@ -748,5 +748,14 @@ public interface AmbariManagementController { * @throws AmbariException if an error occurs during the rack change registration */ public void registerRackChange(String clusterName) throws AmbariException; + + /** + * Initialize cluster scoped widgets and widgetLayouts for different stack + * components. + * + * @param cluster @Cluster object + * @param service @Service object + */ + public void initializeWidgetsAndLayouts(Cluster cluster, Service service) throws AmbariException; } http://git-wip-us.apache.org/repos/asf/ambari/blob/c71624a6/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java index 5959327..5368eab 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java @@ -51,15 +51,24 @@ import org.apache.ambari.server.controller.internal.RequestOperationLevel; import org.apache.ambari.server.controller.internal.RequestResourceFilter; import org.apache.ambari.server.controller.internal.RequestStageContainer; import org.apache.ambari.server.controller.internal.URLStreamProvider; +import org.apache.ambari.server.controller.internal.WidgetLayoutResourceProvider; +import org.apache.ambari.server.controller.internal.WidgetResourceProvider; import org.apache.ambari.server.controller.spi.Resource; import org.apache.ambari.server.customactions.ActionDefinition; import org.apache.ambari.server.metadata.ActionMetadata; import org.apache.ambari.server.metadata.RoleCommandOrder; +import org.apache.ambari.server.orm.dao.ClusterDAO; import org.apache.ambari.server.orm.dao.RepositoryVersionDAO; +import org.apache.ambari.server.orm.dao.WidgetDAO; +import org.apache.ambari.server.orm.dao.WidgetLayoutDAO; +import org.apache.ambari.server.orm.entities.ClusterEntity; import org.apache.ambari.server.orm.entities.ClusterVersionEntity; import org.apache.ambari.server.orm.entities.OperatingSystemEntity; import org.apache.ambari.server.orm.entities.RepositoryEntity; import org.apache.ambari.server.orm.entities.RepositoryVersionEntity; +import org.apache.ambari.server.orm.entities.WidgetEntity; +import org.apache.ambari.server.orm.entities.WidgetLayoutEntity; +import org.apache.ambari.server.orm.entities.WidgetLayoutUserWidgetEntity; import org.apache.ambari.server.scheduler.ExecutionScheduleManager; import org.apache.ambari.server.security.authorization.AuthorizationHelper; import org.apache.ambari.server.security.authorization.Group; @@ -101,6 +110,8 @@ import org.apache.ambari.server.state.StackInfo; import org.apache.ambari.server.state.State; import org.apache.ambari.server.state.configgroup.ConfigGroupFactory; import org.apache.ambari.server.state.scheduler.RequestExecutionFactory; +import org.apache.ambari.server.state.stack.WidgetLayout; +import org.apache.ambari.server.state.stack.WidgetLayoutInfo; import org.apache.ambari.server.state.svccomphost.ServiceComponentHostInstallEvent; import org.apache.ambari.server.state.svccomphost.ServiceComponentHostStartEvent; import org.apache.ambari.server.state.svccomphost.ServiceComponentHostStopEvent; @@ -113,9 +124,10 @@ import org.apache.commons.lang.math.NumberUtils; import org.apache.http.client.utils.URIBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import java.io.File; +import java.io.FileReader; import java.io.IOException; +import java.lang.reflect.Type; import java.net.InetAddress; import java.text.MessageFormat; import java.util.ArrayList; @@ -132,7 +144,6 @@ import java.util.Map.Entry; import java.util.Set; import java.util.TreeMap; import java.util.concurrent.TimeUnit; - import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.AMBARI_DB_RCA_DRIVER; import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.AMBARI_DB_RCA_PASSWORD; import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.AMBARI_DB_RCA_URL; @@ -208,6 +219,12 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle private AmbariLdapDataPopulator ldapDataPopulator; @Inject private RepositoryVersionDAO repositoryVersionDAO; + @Inject + private WidgetDAO widgetDAO; + @Inject + private WidgetLayoutDAO widgetLayoutDAO; + @Inject + private ClusterDAO clusterDAO; private MaintenanceStateHelper maintenanceStateHelper; @@ -3811,4 +3828,122 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle ldapSyncInProgress = false; } } + + @SuppressWarnings("unchecked") + @Override + public void initializeWidgetsAndLayouts(Cluster cluster, Service service) throws AmbariException { + StackId stackId = cluster.getCurrentStackVersion(); + Type widgetLayoutType = new TypeToken<Map<String, List<WidgetLayout>>>(){}.getType(); + + try { + Map<String, Object> widgetDescriptor = null; + StackInfo stackInfo = ambariMetaInfo.getStack(stackId.getStackName(), stackId.getStackVersion()); + if (service != null) { + // Service widgets + ServiceInfo serviceInfo = stackInfo.getService(service.getName()); + File widgetDescriptorFile = serviceInfo.getWidgetsDescriptorFile(); + if (widgetDescriptorFile != null && widgetDescriptorFile.exists()) { + try { + widgetDescriptor = gson.fromJson(new FileReader(widgetDescriptorFile), widgetLayoutType); + } catch (Exception ex) { + String msg = "Error loading widgets from file: " + widgetDescriptorFile; + LOG.error(msg, ex); + throw new AmbariException(msg); + } + } + } else { + // Cluster level widgets + String widgetDescriptorFileLocation = stackInfo.getWidgetsDescriptorFileLocation(); + if (widgetDescriptorFileLocation != null) { + File widgetDescriptorFile = new File(widgetDescriptorFileLocation); + if (widgetDescriptorFile.exists()) { + try { + widgetDescriptor = gson.fromJson(new FileReader(widgetDescriptorFile), widgetLayoutType); + } catch (Exception ex) { + String msg = "Error loading widgets from file: " + widgetDescriptorFile; + LOG.error(msg, ex); + throw new AmbariException(msg); + } + } + } + } + if (widgetDescriptor != null) { + LOG.debug("Loaded widgest descriptor: " + widgetDescriptor); + for (Object artifact : widgetDescriptor.values()) { + List<WidgetLayout> widgetLayouts = (List<WidgetLayout>) artifact; + createWidgetsAndLayouts(cluster, widgetLayouts); + } + } + } catch (Exception e) { + throw new AmbariException("Error creating stack widget artifacts. " + + (service != null ? "Service: " + service.getName() + ", " : "") + + "Cluster: " + cluster.getClusterName(), e); + } + } + + @Transactional + private void createWidgetsAndLayouts(Cluster cluster, List<WidgetLayout> widgetLayouts) { + String user = "ambari"; + ClusterEntity clusterEntity = clusterDAO.findById(cluster.getClusterId()); + Long now = System.currentTimeMillis(); + Long clusterId = cluster.getClusterId(); + + if (widgetLayouts != null) { + for (WidgetLayout widgetLayout : widgetLayouts) { + List<WidgetLayoutEntity> existingEntities = + widgetLayoutDAO.findByName(clusterId, widgetLayout.getLayoutName(), user); + // Avoid creating widgets / layouts if the layout exists + if (existingEntities == null || existingEntities.isEmpty()) { + WidgetLayoutEntity layoutEntity = new WidgetLayoutEntity(); + layoutEntity.setClusterEntity(clusterEntity); + layoutEntity.setClusterId(clusterId); + layoutEntity.setLayoutName(widgetLayout.getLayoutName()); + layoutEntity.setDisplayName(widgetLayout.getDisplayName()); + layoutEntity.setSectionName(widgetLayout.getSectionName()); + layoutEntity.setScope(WidgetLayoutResourceProvider.SCOPE.CLUSTER.name()); + layoutEntity.setUserName(user); + + List<WidgetLayoutUserWidgetEntity> widgetLayoutUserWidgetEntityList = new LinkedList<WidgetLayoutUserWidgetEntity>(); + int order = 0; + for (WidgetLayoutInfo layoutInfo : widgetLayout.getWidgetLayoutInfoList()) { + WidgetEntity widgetEntity = new WidgetEntity(); + widgetEntity.setClusterId(clusterId); + widgetEntity.setClusterEntity(clusterEntity); + widgetEntity.setScope(WidgetResourceProvider.SCOPE.CLUSTER.name()); + widgetEntity.setWidgetName(layoutInfo.getWidgetName()); + widgetEntity.setDisplayName(layoutInfo.getDisplayName()); + widgetEntity.setAuthor(user); + widgetEntity.setDescription(layoutInfo.getDescription()); + widgetEntity.setTimeCreated(now); + widgetEntity.setWidgetType(layoutInfo.getType()); + widgetEntity.setMetrics(gson.toJson(layoutInfo.getMetricsInfo())); + widgetEntity.setProperties(gson.toJson(layoutInfo.getProperties())); + widgetEntity.setWidgetValues(gson.toJson(layoutInfo.getValues())); + widgetDAO.create(widgetEntity); + // Add to layout if visibility is true + if (layoutInfo.isVisible()) { + List<WidgetEntity> createdEntities = + widgetDAO.findByName(clusterId, layoutInfo.getWidgetName(), user); + + if (createdEntities != null && !createdEntities.isEmpty()) { + if (createdEntities.size() > 1) { + LOG.error("Skipping layout entry for widget. Multiple widgets " + + "found with name = " + layoutInfo.getWidgetName() + + ", cluster = " + cluster.getClusterName() + ", user = " + user); + } else { + WidgetLayoutUserWidgetEntity widgetLayoutUserWidgetEntity = new WidgetLayoutUserWidgetEntity(); + widgetLayoutUserWidgetEntity.setWidget(widgetEntity); + widgetLayoutUserWidgetEntity.setWidgetOrder(order++); + widgetLayoutUserWidgetEntity.setWidgetLayout(layoutEntity); + widgetLayoutUserWidgetEntityList.add(widgetLayoutUserWidgetEntity); + } + } + } + } + layoutEntity.setListWidgetLayoutUserWidgetEntity(widgetLayoutUserWidgetEntityList); + widgetLayoutDAO.create(layoutEntity); + } + } + } + } } http://git-wip-us.apache.org/repos/asf/ambari/blob/c71624a6/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterResourceProvider.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterResourceProvider.java index c226823..0b5bb8e 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterResourceProvider.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterResourceProvider.java @@ -17,21 +17,16 @@ */ package org.apache.ambari.server.controller.internal; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Set; - import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.api.services.AmbariMetaInfo; -import org.apache.ambari.server.controller.*; +import org.apache.ambari.server.controller.AmbariManagementController; +import org.apache.ambari.server.controller.ClusterRequest; +import org.apache.ambari.server.controller.ClusterResponse; +import org.apache.ambari.server.controller.ConfigGroupRequest; +import org.apache.ambari.server.controller.ConfigurationRequest; +import org.apache.ambari.server.controller.RequestStatusResponse; +import org.apache.ambari.server.controller.ServiceConfigVersionRequest; +import org.apache.ambari.server.controller.ServiceConfigVersionResponse; import org.apache.ambari.server.controller.spi.NoSuchParentResourceException; import org.apache.ambari.server.controller.spi.NoSuchResourceException; import org.apache.ambari.server.controller.spi.Predicate; @@ -53,6 +48,17 @@ import org.apache.ambari.server.state.ConfigImpl; import org.apache.ambari.server.state.SecurityType; import org.apache.ambari.server.state.StackId; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Set; + /** * Resource provider for cluster resources. */ @@ -126,7 +132,6 @@ public class ClusterResourceProvider extends BaseBlueprintProcessor { private Map<String, Map<String, Map<String, String>>> mapClusterAttributes = new HashMap<String, Map<String, Map<String, String>>>(); - // ----- Constructors ---------------------------------------------------- /** http://git-wip-us.apache.org/repos/asf/ambari/blob/c71624a6/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ServiceResourceProvider.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ServiceResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ServiceResourceProvider.java index 2724a97..e715d42 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ServiceResourceProvider.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ServiceResourceProvider.java @@ -48,14 +48,12 @@ import org.apache.ambari.server.controller.spi.Resource; import org.apache.ambari.server.controller.spi.ResourceAlreadyExistsException; import org.apache.ambari.server.controller.spi.SystemException; import org.apache.ambari.server.controller.spi.UnsupportedPropertyException; +import org.apache.ambari.server.controller.utilities.PredicateBuilder; import org.apache.ambari.server.controller.utilities.PropertyHelper; import org.apache.ambari.server.metadata.RoleCommandOrder; import org.apache.ambari.server.serveraction.kerberos.KerberosAdminAuthenticationException; import org.apache.ambari.server.serveraction.kerberos.KerberosInvalidConfigurationException; -import org.apache.ambari.server.serveraction.kerberos.KerberosKDCConnectionException; -import org.apache.ambari.server.serveraction.kerberos.KerberosLDAPContainerException; import org.apache.ambari.server.serveraction.kerberos.KerberosMissingAdminCredentialsException; -import org.apache.ambari.server.serveraction.kerberos.KerberosRealmException; import org.apache.ambari.server.state.Cluster; import org.apache.ambari.server.state.Clusters; import org.apache.ambari.server.state.ComponentInfo; @@ -65,9 +63,11 @@ import org.apache.ambari.server.state.ServiceComponent; import org.apache.ambari.server.state.ServiceComponentHost; import org.apache.ambari.server.state.ServiceFactory; import org.apache.ambari.server.state.StackId; +import org.apache.ambari.server.state.StackInfo; import org.apache.ambari.server.state.State; +import org.apache.ambari.server.state.stack.WidgetLayout; +import org.apache.ambari.server.state.stack.WidgetLayoutInfo; import org.apache.commons.lang.StringUtils; - import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -77,6 +77,7 @@ import java.util.EnumSet; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -510,6 +511,9 @@ public class ServiceResourceProvider extends AbstractControllerResourceProvider s.setDesiredState(state); s.setDesiredStackVersion(cluster.getDesiredStackVersion()); cluster.addService(s); + // Initialize service widgets + getManagementController().initializeWidgetsAndLayouts(cluster, s); + s.persist(); } } http://git-wip-us.apache.org/repos/asf/ambari/blob/c71624a6/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/StackArtifactResourceProvider.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/StackArtifactResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/StackArtifactResourceProvider.java index 53734aa..62209e2 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/StackArtifactResourceProvider.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/StackArtifactResourceProvider.java @@ -431,7 +431,7 @@ public class StackArtifactResourceProvider extends AbstractControllerResourcePro } } - private Map<String, Object> getWidgetsDescriptorForService(StackInfo stackInfo, String serviceName) + public Map<String, Object> getWidgetsDescriptorForService(StackInfo stackInfo, String serviceName) throws NoSuchParentResourceException, IOException { Map<String, Object> widgetDescriptor = null; @@ -449,7 +449,7 @@ public class StackArtifactResourceProvider extends AbstractControllerResourcePro return widgetDescriptor; } - private Map<String, Object> getWidgetsDescriptorForCluster(StackInfo stackInfo) + public Map<String, Object> getWidgetsDescriptorForCluster(StackInfo stackInfo) throws NoSuchParentResourceException, IOException { Map<String, Object> widgetDescriptor = null; http://git-wip-us.apache.org/repos/asf/ambari/blob/c71624a6/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/WidgetLayoutResourceProvider.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/WidgetLayoutResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/WidgetLayoutResourceProvider.java index eedde92..ba3bce9 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/WidgetLayoutResourceProvider.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/WidgetLayoutResourceProvider.java @@ -67,6 +67,10 @@ public class WidgetLayoutResourceProvider extends AbstractControllerResourceProv public static final String WIDGETLAYOUT_INFO_PROPERTY_ID = PropertyHelper.getPropertyId("WidgetLayouts", "WidgetInfo"); public static final String WIDGETLAYOUT_USERNAME_PROPERTY_ID = PropertyHelper.getPropertyId("WidgetLayouts", "user_name"); public static final String WIDGETLAYOUT_DISPLAY_NAME_PROPERTY_ID = PropertyHelper.getPropertyId("WidgetLayouts", "display_name"); + public static enum SCOPE { + CLUSTER, + USER + } @SuppressWarnings("serial") private static Set<String> pkPropertyIds = new HashSet<String>() { @@ -108,8 +112,7 @@ public class WidgetLayoutResourceProvider extends AbstractControllerResourceProv * Create a new resource provider. * */ - public WidgetLayoutResourceProvider( - AmbariManagementController managementController) { + public WidgetLayoutResourceProvider(AmbariManagementController managementController) { super(propertyIds, keyPropertyIds, managementController); } @@ -140,16 +143,16 @@ public class WidgetLayoutResourceProvider extends AbstractControllerResourceProv } } final WidgetLayoutEntity entity = new WidgetLayoutEntity(); + String userName = getUserName(properties); Set widgetsSet = (LinkedHashSet) properties.get(WIDGETLAYOUT_INFO_PROPERTY_ID); String clusterName = properties.get(WIDGETLAYOUT_CLUSTER_NAME_PROPERTY_ID).toString(); entity.setLayoutName(properties.get(WIDGETLAYOUT_LAYOUT_NAME_PROPERTY_ID).toString()); - entity.setClusterId(getManagementController().getClusters().getCluster(clusterName).getClusterId()); entity.setSectionName(properties.get(WIDGETLAYOUT_SECTION_NAME_PROPERTY_ID).toString()); entity.setScope(properties.get(WIDGETLAYOUT_SCOPE_PROPERTY_ID).toString()); - entity.setUserName(properties.get(WIDGETLAYOUT_USERNAME_PROPERTY_ID).toString()); + entity.setUserName(userName); entity.setDisplayName(properties.get(WIDGETLAYOUT_DISPLAY_NAME_PROPERTY_ID).toString()); List<WidgetLayoutUserWidgetEntity> widgetLayoutUserWidgetEntityList = new LinkedList<WidgetLayoutUserWidgetEntity>(); @@ -193,7 +196,7 @@ public class WidgetLayoutResourceProvider extends AbstractControllerResourceProv List<WidgetLayoutEntity> layoutEntities = new ArrayList<WidgetLayoutEntity>(); for (Map<String, Object> propertyMap: propertyMaps) { - final String userName = propertyMap.get(WIDGETLAYOUT_USERNAME_PROPERTY_ID).toString(); + String userName = getUserName(propertyMap); if (propertyMap.get(WIDGETLAYOUT_ID_PROPERTY_ID) != null) { final Long id; try { @@ -340,4 +343,10 @@ public class WidgetLayoutResourceProvider extends AbstractControllerResourceProv return pkPropertyIds; } + private String getUserName(Map<String, Object> properties) { + if (properties.get(WIDGETLAYOUT_USERNAME_PROPERTY_ID) != null) { + return properties.get(WIDGETLAYOUT_USERNAME_PROPERTY_ID).toString(); + } + return getManagementController().getAuthName(); + } } http://git-wip-us.apache.org/repos/asf/ambari/blob/c71624a6/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/WidgetResourceProvider.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/WidgetResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/WidgetResourceProvider.java index 87ae723..1bd1058 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/WidgetResourceProvider.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/WidgetResourceProvider.java @@ -40,7 +40,6 @@ import org.apache.ambari.server.orm.entities.WidgetEntity; import org.apache.ambari.server.security.authorization.AmbariGrantedAuthority; import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang.StringUtils; -import org.springframework.dao.PermissionDeniedDataAccessException; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.context.SecurityContext; import org.springframework.security.core.context.SecurityContextHolder; @@ -72,6 +71,10 @@ public class WidgetResourceProvider extends AbstractControllerResourceProvider { public static final String WIDGET_METRICS_PROPERTY_ID = PropertyHelper.getPropertyId("Widgets", "metrics"); public static final String WIDGET_VALUES_PROPERTY_ID = PropertyHelper.getPropertyId("Widgets", "values"); public static final String WIDGET_PROPERTIES_PROPERTY_ID = PropertyHelper.getPropertyId("Widgets", "properties"); + public static enum SCOPE { + CLUSTER, + USER + } @SuppressWarnings("serial") private static Set<String> pkPropertyIds = new HashSet<String>() { @@ -147,7 +150,6 @@ public class WidgetResourceProvider extends AbstractControllerResourceProvider { } final WidgetEntity entity = new WidgetEntity(); String clusterName = properties.get(WIDGET_CLUSTER_NAME_PROPERTY_ID).toString(); - String userName = properties.get(WIDGET_AUTHOR_PROPERTY_ID).toString(); String scope = properties.get(WIDGET_SCOPE_PROPERTY_ID).toString(); if (!isScopeAllowedForUser(scope)) { @@ -165,7 +167,7 @@ public class WidgetResourceProvider extends AbstractControllerResourceProvider { gson.toJson(properties.get(WIDGET_METRICS_PROPERTY_ID)) : null; entity.setMetrics(metrics); - entity.setAuthor(userName); + entity.setAuthor(getAuthorName(properties)); String description = (properties.containsKey(WIDGET_DESCRIPTION_PROPERTY_ID)) ? properties.get(WIDGET_DESCRIPTION_PROPERTY_ID).toString() : null; @@ -285,9 +287,7 @@ public class WidgetResourceProvider extends AbstractControllerResourceProvider { entity.setMetrics(gson.toJson(propertyMap.get(WIDGET_METRICS_PROPERTY_ID))); } - if (StringUtils.isNotBlank(ObjectUtils.toString(propertyMap.get(WIDGET_AUTHOR_PROPERTY_ID)))) { - entity.setAuthor(propertyMap.get(WIDGET_AUTHOR_PROPERTY_ID).toString()); - } + entity.setAuthor(getAuthorName(propertyMap)); if (StringUtils.isNotBlank(ObjectUtils.toString(propertyMap.get(WIDGET_DESCRIPTION_PROPERTY_ID)))) { entity.setDescription(propertyMap.get(WIDGET_DESCRIPTION_PROPERTY_ID).toString()); @@ -380,4 +380,11 @@ public class WidgetResourceProvider extends AbstractControllerResourceProvider { return false; } } + + private String getAuthorName(Map<String, Object> properties) { + if (StringUtils.isNotBlank(ObjectUtils.toString(properties.get(WIDGET_AUTHOR_PROPERTY_ID)))) { + return properties.get(WIDGET_AUTHOR_PROPERTY_ID).toString(); + } + return getManagementController().getAuthName(); + } } http://git-wip-us.apache.org/repos/asf/ambari/blob/c71624a6/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/WidgetDAO.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/WidgetDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/WidgetDAO.java index c85e255..dd41bd5 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/WidgetDAO.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/WidgetDAO.java @@ -63,6 +63,17 @@ public class WidgetDAO { } @RequiresSession + public List<WidgetEntity> findByName(Long clusterId, String widgetName, String author) { + TypedQuery<WidgetEntity> query = entityManagerProvider.get() + .createNamedQuery("WidgetEntity.findByName", WidgetEntity.class); + query.setParameter("clusterId", clusterId); + query.setParameter("widgetName", widgetName); + query.setParameter("author", author); + + return daoUtils.selectList(query); + } + + @RequiresSession public List<WidgetEntity> findAll() { TypedQuery<WidgetEntity> query = entityManagerProvider.get() .createNamedQuery("WidgetEntity.findAll", WidgetEntity.class); http://git-wip-us.apache.org/repos/asf/ambari/blob/c71624a6/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/WidgetLayoutDAO.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/WidgetLayoutDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/WidgetLayoutDAO.java index 1d05acc..9915fa8 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/WidgetLayoutDAO.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/WidgetLayoutDAO.java @@ -63,6 +63,17 @@ public class WidgetLayoutDAO { } @RequiresSession + public List<WidgetLayoutEntity> findByName(Long clusterId, String layoutName, String userName) { + TypedQuery<WidgetLayoutEntity> query = entityManagerProvider.get() + .createNamedQuery("WidgetLayoutEntity.findByName", WidgetLayoutEntity.class); + query.setParameter("clusterId", clusterId); + query.setParameter("layoutName", layoutName); + query.setParameter("userName", userName); + + return daoUtils.selectList(query); + } + + @RequiresSession public List<WidgetLayoutEntity> findAll() { TypedQuery<WidgetLayoutEntity> query = entityManagerProvider.get() .createNamedQuery("WidgetLayoutEntity.findAll", WidgetLayoutEntity.class); http://git-wip-us.apache.org/repos/asf/ambari/blob/c71624a6/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/WidgetEntity.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/WidgetEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/WidgetEntity.java index d383a62..82d9a15 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/WidgetEntity.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/WidgetEntity.java @@ -45,6 +45,7 @@ import java.util.List; @NamedQueries({ @NamedQuery(name = "WidgetEntity.findAll", query = "SELECT widget FROM WidgetEntity widget"), @NamedQuery(name = "WidgetEntity.findByCluster", query = "SELECT widget FROM WidgetEntity widget WHERE widget.clusterId = :clusterId"), + @NamedQuery(name = "WidgetEntity.findByName", query = "SELECT widget FROM WidgetEntity widget WHERE widget.clusterId = :clusterId AND widget.widgetName = :widgetName AND widget.author = :author"), @NamedQuery(name = "WidgetEntity.findBySectionName", query = "SELECT widget FROM WidgetEntity widget " + "INNER JOIN widget.listWidgetLayoutUserWidgetEntity widgetLayoutUserWidget " + @@ -79,16 +80,16 @@ public class WidgetEntity { @Column(name = "description", length = 255) private String description; - @Column(name = "display_name", nullable = false, length = 255) + @Column(name = "display_name", length = 255) private String displayName; @Column(name = "scope", length = 255) private String scope; - @Column(name = "widget_values", length = 255) + @Column(name = "widget_values", length = 4000) private String widgetValues; - @Column(name = "properties", length = 255) + @Column(name = "properties", length = 4000) private String properties; @Column(name = "cluster_id", nullable = false) http://git-wip-us.apache.org/repos/asf/ambari/blob/c71624a6/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/WidgetLayoutEntity.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/WidgetLayoutEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/WidgetLayoutEntity.java index 820e3e0..6ba14e4 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/WidgetLayoutEntity.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/WidgetLayoutEntity.java @@ -47,6 +47,7 @@ import java.util.List; @NamedQuery(name = "WidgetLayoutEntity.findAll", query = "SELECT widgetLayout FROM WidgetLayoutEntity widgetLayout"), @NamedQuery(name = "WidgetLayoutEntity.findByCluster", query = "SELECT widgetLayout FROM WidgetLayoutEntity widgetLayout WHERE widgetLayout.clusterId = :clusterId"), @NamedQuery(name = "WidgetLayoutEntity.findBySectionName", query = "SELECT widgetLayout FROM WidgetLayoutEntity widgetLayout WHERE widgetLayout.sectionName = :sectionName"), + @NamedQuery(name = "WidgetLayoutEntity.findByName", query = "SELECT widgetLayout FROM WidgetLayoutEntity widgetLayout WHERE widgetLayout.clusterId = :clusterId AND widgetLayout.layoutName = :layoutName AND widgetLayout.userName = :userName") }) public class WidgetLayoutEntity { @@ -70,7 +71,7 @@ public class WidgetLayoutEntity { @Column(name = "scope", nullable = false) private String scope; - @Column(name = "display_name", nullable = false) + @Column(name = "display_name") private String displayName; @ManyToOne http://git-wip-us.apache.org/repos/asf/ambari/blob/c71624a6/ambari-server/src/main/java/org/apache/ambari/server/state/stack/WidgetLayout.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/WidgetLayout.java b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/WidgetLayout.java index d730adf..cb6b7fb 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/WidgetLayout.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/WidgetLayout.java @@ -28,6 +28,8 @@ public class WidgetLayout { private String layoutName; @SerializedName("section_name") private String sectionName; + @SerializedName("display_name") + private String displayName; @SerializedName("widgetLayoutInfo") private List<WidgetLayoutInfo> widgetLayoutInfoList; @@ -49,6 +51,11 @@ public class WidgetLayout { this.sectionName = sectionName; } + @JsonProperty("display_name") + public String getDisplayName() { + return displayName; + } + @JsonProperty("widgetLayoutInfo") public List<WidgetLayoutInfo> getWidgetLayoutInfoList() { return widgetLayoutInfoList; http://git-wip-us.apache.org/repos/asf/ambari/blob/c71624a6/ambari-server/src/main/java/org/apache/ambari/server/state/stack/WidgetLayoutInfo.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/WidgetLayoutInfo.java b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/WidgetLayoutInfo.java index d3d317d..49b8fe9 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/state/stack/WidgetLayoutInfo.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/state/stack/WidgetLayoutInfo.java @@ -78,7 +78,7 @@ public class WidgetLayoutInfo { } @JsonProperty("is_visible") - public boolean getVisibility() { + public boolean isVisible() { return visibility; } http://git-wip-us.apache.org/repos/asf/ambari/blob/c71624a6/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql index 2ae970c..2edb93e 100644 --- a/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql +++ b/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql @@ -544,8 +544,8 @@ CREATE TABLE widget ( description VARCHAR(255), display_name VARCHAR(255) NOT NULL, scope VARCHAR(255), - widget_values VARCHAR(255), - properties VARCHAR(255), + widget_values VARCHAR(4000), + properties VARCHAR(4000), cluster_id BIGINT NOT NULL, PRIMARY KEY(id) ); http://git-wip-us.apache.org/repos/asf/ambari/blob/c71624a6/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql index 8db0e32..9834933 100644 --- a/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql +++ b/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql @@ -532,8 +532,8 @@ CREATE TABLE widget ( description VARCHAR2(255), display_name VARCHAR2(255) NOT NULL, scope VARCHAR2(255), - widget_values VARCHAR2(255), - properties VARCHAR2(255), + widget_values VARCHAR2(4000), + properties VARCHAR2(4000), cluster_id NUMBER(19) NOT NULL, PRIMARY KEY(id) ); http://git-wip-us.apache.org/repos/asf/ambari/blob/c71624a6/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql index ca27989..1b97c2a 100644 --- a/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql +++ b/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql @@ -537,8 +537,8 @@ CREATE TABLE widget ( description VARCHAR(255), display_name VARCHAR(255) NOT NULL, scope VARCHAR(255), - widget_values VARCHAR(255), - properties VARCHAR(255), + widget_values VARCHAR(4000), + properties VARCHAR(4000), cluster_id BIGINT NOT NULL, PRIMARY KEY(id) ); http://git-wip-us.apache.org/repos/asf/ambari/blob/c71624a6/ambari-server/src/main/resources/Ambari-DDL-Postgres-EMBEDDED-CREATE.sql ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/resources/Ambari-DDL-Postgres-EMBEDDED-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-Postgres-EMBEDDED-CREATE.sql index e1f9d6c..32852e7 100644 --- a/ambari-server/src/main/resources/Ambari-DDL-Postgres-EMBEDDED-CREATE.sql +++ b/ambari-server/src/main/resources/Ambari-DDL-Postgres-EMBEDDED-CREATE.sql @@ -612,8 +612,8 @@ CREATE TABLE ambari.widget ( description VARCHAR(255), display_name VARCHAR(255) NOT NULL, scope VARCHAR(255), - widget_values VARCHAR(255), - properties VARCHAR(255), + widget_values VARCHAR(4000), + properties VARCHAR(4000), cluster_id BIGINT NOT NULL, PRIMARY KEY(id) ); http://git-wip-us.apache.org/repos/asf/ambari/blob/c71624a6/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java index 87820ca..7c2cdcd 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java @@ -18,44 +18,13 @@ package org.apache.ambari.server.controller; -import static org.easymock.EasyMock.capture; -import static org.easymock.EasyMock.createNiceMock; -import static org.easymock.EasyMock.createStrictMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; -import static org.easymock.EasyMock.verify; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.io.IOException; -import java.io.StringReader; -import java.lang.reflect.Type; -import java.net.ConnectException; -import java.net.MalformedURLException; -import java.net.UnknownHostException; -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.Set; - -import javax.persistence.EntityManager; - +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import com.google.inject.AbstractModule; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.persist.PersistService; import junit.framework.Assert; - import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.ClusterNotFoundException; import org.apache.ambari.server.DuplicateResourceException; @@ -92,7 +61,12 @@ import org.apache.ambari.server.orm.InMemoryDefaultTestModule; import org.apache.ambari.server.orm.OrmTestHelper; import org.apache.ambari.server.orm.dao.ExecutionCommandDAO; import org.apache.ambari.server.orm.dao.HostDAO; +import org.apache.ambari.server.orm.dao.WidgetDAO; +import org.apache.ambari.server.orm.dao.WidgetLayoutDAO; import org.apache.ambari.server.orm.entities.ExecutionCommandEntity; +import org.apache.ambari.server.orm.entities.WidgetEntity; +import org.apache.ambari.server.orm.entities.WidgetLayoutEntity; +import org.apache.ambari.server.orm.entities.WidgetLayoutUserWidgetEntity; import org.apache.ambari.server.security.authorization.Users; import org.apache.ambari.server.serveraction.ServerAction; import org.apache.ambari.server.state.Cluster; @@ -140,12 +114,41 @@ import org.junit.rules.ExpectedException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; -import com.google.inject.AbstractModule; -import com.google.inject.Guice; -import com.google.inject.Injector; -import com.google.inject.persist.PersistService; +import javax.persistence.EntityManager; +import java.io.IOException; +import java.io.StringReader; +import java.lang.reflect.Type; +import java.net.ConnectException; +import java.net.MalformedURLException; +import java.net.UnknownHostException; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.Set; + +import static org.easymock.EasyMock.capture; +import static org.easymock.EasyMock.createNiceMock; +import static org.easymock.EasyMock.createStrictMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; public class AmbariManagementControllerTest { @@ -10295,6 +10298,67 @@ public class AmbariManagementControllerTest { assertThat(sles11Packages, is(expectedSles11)); } + @Test + public void testClusterWidgetCreateOnClusterCreate() throws Exception { + // TODO: Add once cluster widgets.json is available + } + + @Test + public void testServiceWidgetCreationOnServiceCreate() throws Exception { + String clusterName = "foo1"; + ClusterRequest r = new ClusterRequest(null, clusterName, + State.INSTALLED.name(), SecurityType.NONE, "OTHER-2.0", null); + controller.createCluster(r); + String serviceName = "HBASE"; + clusters.getCluster("foo1").setDesiredStackVersion(new StackId("OTHER-2.0")); + createService(clusterName, serviceName, State.INIT); + + Service s = clusters.getCluster(clusterName).getService(serviceName); + Assert.assertNotNull(s); + Assert.assertEquals(serviceName, s.getName()); + Assert.assertEquals(clusterName, s.getCluster().getClusterName()); + + WidgetDAO widgetDAO = injector.getInstance(WidgetDAO.class); + WidgetLayoutDAO widgetLayoutDAO = injector.getInstance(WidgetLayoutDAO.class); + List<WidgetEntity> widgetEntities = widgetDAO.findAll(); + List<WidgetLayoutEntity> layoutEntities = widgetLayoutDAO.findAll(); + + Assert.assertNotNull(widgetEntities); + Assert.assertFalse(widgetEntities.isEmpty()); + Assert.assertNotNull(layoutEntities); + Assert.assertFalse(layoutEntities.isEmpty()); + + WidgetEntity candidateVisibleEntity = null; + for (WidgetEntity entity : widgetEntities) { + if (entity.getWidgetName().equals("OPEN_CONNECTIONS")) { + candidateVisibleEntity = entity; + } + } + Assert.assertNotNull(candidateVisibleEntity); + Assert.assertEquals("GRAPH", candidateVisibleEntity.getWidgetType()); + Assert.assertEquals("ambari", candidateVisibleEntity.getAuthor()); + Assert.assertEquals("CLUSTER", candidateVisibleEntity.getScope()); + Assert.assertNotNull(candidateVisibleEntity.getMetrics()); + Assert.assertNotNull(candidateVisibleEntity.getProperties()); + Assert.assertNotNull(candidateVisibleEntity.getWidgetValues()); + + WidgetLayoutEntity candidateLayoutEntity = null; + for (WidgetLayoutEntity entity : layoutEntities) { + if (entity.getLayoutName().equals("default_hbase_layout")) { + candidateLayoutEntity = entity; + } + } + Assert.assertNotNull(candidateLayoutEntity); + List<WidgetLayoutUserWidgetEntity> layoutUserWidgetEntities = + candidateLayoutEntity.getListWidgetLayoutUserWidgetEntity(); + Assert.assertNotNull(layoutUserWidgetEntities); + Assert.assertEquals(4, layoutUserWidgetEntities.size()); + Assert.assertEquals("RS_READS_WRITES", layoutUserWidgetEntities.get(0).getWidget().getWidgetName()); + Assert.assertEquals("OPEN_CONNECTIONS", layoutUserWidgetEntities.get(1).getWidget().getWidgetName()); + Assert.assertEquals("FILES_LOCAL", layoutUserWidgetEntities.get(2).getWidget().getWidgetName()); + Assert.assertEquals("UPDATED_BLOCKED_TIME", layoutUserWidgetEntities.get(3).getWidget().getWidgetName()); + } + // this is a temporary measure as a result of moving updateHostComponents from AmbariManagementController // to HostComponentResourceProvider. Eventually the tests should be moved out of this class. private RequestStatusResponse updateHostComponents(Set<ServiceComponentHostRequest> requests, http://git-wip-us.apache.org/repos/asf/ambari/blob/c71624a6/ambari-server/src/test/resources/stacks/OTHER/2.0/services/HBASE/widgets.json ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/resources/stacks/OTHER/2.0/services/HBASE/widgets.json b/ambari-server/src/test/resources/stacks/OTHER/2.0/services/HBASE/widgets.json index 705c3eb..3d4e7bb 100644 --- a/ambari-server/src/test/resources/stacks/OTHER/2.0/services/HBASE/widgets.json +++ b/ambari-server/src/test/resources/stacks/OTHER/2.0/services/HBASE/widgets.json @@ -102,7 +102,7 @@ "widget_name": "ACTIVE_HANDLER", "display_name": "Active Handlers vs Calls in General Queue", "widget_type": "GRAPH", - "is_visible": true, + "is_visible": false, "metrics": [ { "name": "ipc.IPC.numOpenConnections",