AmbariContext { public void createAmbariHostResources(long clusterId, String hostName, Map<String, Collection<String>> components) { Host host; try { host = getController().getClusters().getHost(hostName); } catch (AmbariException e) { throw new RuntimeException(String.format( "Unable to obtain host instance '%s' when persisting host resources", hostName)); } Cluster cluster = null; try { cluster = getController().getClusters().getClusterById(clusterId); } catch (AmbariException e) { LOG.error("Cannot get cluster for clusterId = " + clusterId, e); throw new RuntimeException(e); } String clusterName = cluster.getClusterName(); Map<String, Object> properties = new HashMap<String, Object>(); properties.put(HostResourceProvider.HOST_CLUSTER_NAME_PROPERTY_ID, clusterName); properties.put(HostResourceProvider.HOST_NAME_PROPERTY_ID, hostName); properties.put(HostResourceProvider.HOST_RACK_INFO_PROPERTY_ID, host.getRackInfo()); try { getHostResourceProvider().createHosts(new RequestImpl(null, Collections.singleton(properties), null, null)); } catch (AmbariException e) { LOG.error("Unable to create host component resource for host {}", hostName, e); throw new RuntimeException(String.format("Unable to create host resource for host '%s': %s", hostName, e.toString()), e); } final Set<ServiceComponentHostRequest> requests = new HashSet<ServiceComponentHostRequest>(); for (Map.Entry<String, Collection<String>> entry : components.entrySet()) { String service = entry.getKey(); for (String component : entry.getValue()) { try { if (cluster.getService(service) != null && !component.equals("AMBARI_SERVER")) { requests.add(new ServiceComponentHostRequest(clusterName, service, component, hostName, null)); } } catch(AmbariException se) { LOG.warn("Service already deleted from cluster: {}", service); } } } try { RetryHelper.executeWithRetry(new Callable<Object>() { @Override public Object call() throws Exception { getController().createHostComponents(requests); return null; } }); } catch (AmbariException e) { LOG.error("Unable to create host component resource for host {}", hostName, e); throw new RuntimeException(String.format("Unable to create host component resource for host '%s': %s", hostName, e.toString()), e); } }  boolean isClusterKerberosEnabled(long clusterId); HostRoleCommand createAmbariTask(long requestId, long stageId, String component, String host,
                                          TaskType type, boolean skipFailure); HostRoleCommand createAmbariTask(long taskId, long requestId, long stageId,
                                          String component, String host, TaskType type, boolean skipFailure); HostRoleCommand getPhysicalTask(long id); Collection<HostRoleCommand> getPhysicalTasks(Collection<Long> ids); void createAmbariResources(ClusterTopology topology, String clusterName, SecurityType securityType, String repoVersion); void createAmbariClusterResource(String clusterName, String stackName, String stackVersion, SecurityType securityType, String repoVersion); void createAmbariServiceAndComponentResources(ClusterTopology topology, String clusterName); void createAmbariHostResources(long  clusterId, String hostName, Map<String, Collection<String>> components); Long getNextRequestId(); synchronized static AmbariManagementController getController(); synchronized static ClusterController getClusterController(); static void init(HostRoleCommandFactory factory); void registerHostWithConfigGroup(final String hostName, final ClusterTopology topology, final String groupName); RequestStatusResponse installHost(String hostName, String clusterName, Collection<String> skipInstallForComponents, Collection<String> dontSkipInstallForComponents, boolean skipFailure); RequestStatusResponse startHost(String hostName, String clusterName, Collection<String> installOnlyComponents, boolean skipFailure); void persistInstallStateForUI(String clusterName, String stackName, String stackVersion); List<ConfigurationRequest> createConfigurationRequests(Map<String, Object> clusterProperties); void setConfigurationOnCluster(final ClusterRequest clusterRequest); void waitForConfigurationResolution(String clusterName, Set<String> updatedConfigTypes); boolean isTopologyResolved(long clusterId); PersistedState getPersistedTopologyState(); boolean isHostRegisteredWithCluster(long clusterId, String host); long getClusterId(String clusterName); String getClusterName(long clusterId);  }
@Test public void testCreateAmbariHostResources() throws Exception { expect(cluster.getServices()).andReturn(clusterServices).anyTimes(); hostResourceProvider.createHosts(anyObject(Request.class)); expectLastCall().once(); expect(cluster.getService("service1")).andReturn(mockService1).times(2); expect(cluster.getService("service2")).andReturn(mockService1).once(); Capture<Set<ServiceComponentHostRequest>> requestsCapture = EasyMock.newCapture(); controller.createHostComponents(capture(requestsCapture)); expectLastCall().once(); replayAll(); Map<String, Collection<String>> componentsMap = new HashMap<>(); Collection<String> components = new ArrayList<>(); components.add("component1"); components.add("component2"); componentsMap.put("service1", components); components = new ArrayList<>(); components.add("component3"); componentsMap.put("service2", components); context.createAmbariHostResources(CLUSTER_ID, "host1", componentsMap); assertEquals(requestsCapture.getValue().size(), 3); }
@Test public void testCreateAmbariHostResourcesWithMissingService() throws Exception { expect(cluster.getServices()).andReturn(clusterServices).anyTimes(); hostResourceProvider.createHosts(anyObject(Request.class)); expectLastCall().once(); expect(cluster.getService("service1")).andReturn(mockService1).times(2); Capture<Set<ServiceComponentHostRequest>> requestsCapture = EasyMock.newCapture(); controller.createHostComponents(capture(requestsCapture)); expectLastCall().once(); replayAll(); Map<String, Collection<String>> componentsMap = new HashMap<>(); Collection<String> components = new ArrayList<>(); components.add("component1"); components.add("component2"); componentsMap.put("service1", components); components = new ArrayList<>(); components.add("component3"); componentsMap.put("service2", components); context.createAmbariHostResources(CLUSTER_ID, "host1", componentsMap); assertEquals(requestsCapture.getValue().size(), 2); }