Search in sources :

Example 1 with ApiConfigList

use of com.cloudera.api.swagger.model.ApiConfigList in project knox by apache.

the class ClouderaManagerServiceDiscovery method discoverCluster.

private ClouderaManagerCluster discoverCluster(DiscoveryApiClient client, String clusterName, Collection<String> includedServices) throws ApiException {
    ServicesResourceApi servicesResourceApi = new ServicesResourceApi(client);
    RolesResourceApi rolesResourceApi = new RolesResourceApi(client);
    log.discoveringCluster(clusterName);
    repository.registerCluster(client.getConfig());
    Set<ServiceModel> serviceModels = new HashSet<>();
    List<ApiService> serviceList = getClusterServices(client.getConfig(), servicesResourceApi);
    if (serviceList != null) {
        /*
      Since Cloudera Manager does not have a service for itself, we will add a skeleton CM
      service so that we can add CM service to topology when auto-discovery is
      turned on and CM service is selected in the descriptor
      */
        final ApiService cmService = new ApiService();
        cmService.setName(CM_SERVICE_TYPE.toLowerCase(Locale.ROOT));
        cmService.setType(CM_SERVICE_TYPE);
        serviceList.add(cmService);
        for (ApiService service : serviceList) {
            final List<ServiceModelGenerator> modelGenerators = serviceModelGenerators.get(service.getType());
            if (shouldSkipServiceDiscovery(modelGenerators, includedServices)) {
                log.skipServiceDiscovery(service.getName(), service.getType());
                continue;
            }
            log.discoveringService(service.getName(), service.getType());
            ApiServiceConfig serviceConfig = null;
            /* no reason to check service config for CM service */
            if (!CM_SERVICE_TYPE.equals(service.getType())) {
                serviceConfig = getServiceConfig(client.getConfig(), servicesResourceApi, service);
            }
            ApiRoleList roleList = getRoles(client.getConfig(), rolesResourceApi, clusterName, service);
            if (roleList != null) {
                for (ApiRole role : roleList.getItems()) {
                    String roleName = role.getName();
                    log.discoveringServiceRole(roleName, role.getType());
                    ApiConfigList roleConfig = null;
                    /* no reason to check role config for CM service */
                    if (!CM_SERVICE_TYPE.equals(service.getType())) {
                        roleConfig = getRoleConfig(client.getConfig(), rolesResourceApi, service, role);
                    }
                    if (modelGenerators != null) {
                        for (ServiceModelGenerator serviceModelGenerator : modelGenerators) {
                            ServiceModelGeneratorHandleResponse response = serviceModelGenerator.handles(service, serviceConfig, role, roleConfig);
                            if (response.handled()) {
                                serviceModelGenerator.setApiClient(client);
                                ServiceModel serviceModel = serviceModelGenerator.generateService(service, serviceConfig, role, roleConfig);
                                serviceModels.add(serviceModel);
                            } else if (!response.getConfigurationIssues().isEmpty()) {
                                log.serviceRoleHasConfigurationIssues(roleName, String.join(";", response.getConfigurationIssues()));
                            }
                        }
                    }
                    log.discoveredServiceRole(roleName, role.getType());
                }
            }
            log.discoveredService(service.getName(), service.getType());
        }
        ClouderaManagerCluster cluster = new ClouderaManagerCluster(clusterName);
        cluster.addServiceModels(serviceModels);
        return cluster;
    }
    return null;
}
Also used : ServicesResourceApi(com.cloudera.api.swagger.ServicesResourceApi) RolesResourceApi(com.cloudera.api.swagger.RolesResourceApi) ApiRole(com.cloudera.api.swagger.model.ApiRole) ApiConfigList(com.cloudera.api.swagger.model.ApiConfigList) ApiService(com.cloudera.api.swagger.model.ApiService) ApiRoleList(com.cloudera.api.swagger.model.ApiRoleList) HashSet(java.util.HashSet) ApiServiceConfig(com.cloudera.api.swagger.model.ApiServiceConfig)

Example 2 with ApiConfigList

use of com.cloudera.api.swagger.model.ApiConfigList in project knox by apache.

the class PollingConfigurationAnalyzer method getCurrentServiceConfiguration.

/**
 * Get the current configuration for the specified service.
 *
 * @param address     The address of the ClouderaManager instance.
 * @param clusterName The name of the cluster.
 * @param service     The name of the service.
 *
 * @return A ServiceConfigurationModel object with the configuration properties associated with the specified
 * service.
 */
protected ServiceConfigurationModel getCurrentServiceConfiguration(final String address, final String clusterName, final String service) {
    ServiceConfigurationModel currentConfig = null;
    log.gettingCurrentClusterConfiguration(service, clusterName, address);
    ApiClient apiClient = getApiClient(configCache.getDiscoveryConfig(address, clusterName));
    ServicesResourceApi api = new ServicesResourceApi(apiClient);
    try {
        ApiServiceConfig svcConfig = api.readServiceConfig(clusterName, service, "full");
        Map<ApiRole, ApiConfigList> roleConfigs = new HashMap<>();
        RolesResourceApi rolesApi = (new RolesResourceApi(apiClient));
        ApiRoleList roles = rolesApi.readRoles(clusterName, service, "", "full");
        for (ApiRole role : roles.getItems()) {
            ApiConfigList config = rolesApi.readRoleConfig(clusterName, role.getName(), service, "full");
            roleConfigs.put(role, config);
        }
        currentConfig = new ServiceConfigurationModel(svcConfig, roleConfigs);
    } catch (ApiException e) {
        log.clouderaManagerConfigurationAPIError(e);
    }
    return currentConfig;
}
Also used : ServicesResourceApi(com.cloudera.api.swagger.ServicesResourceApi) ApiRole(com.cloudera.api.swagger.model.ApiRole) ApiConfigList(com.cloudera.api.swagger.model.ApiConfigList) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) RolesResourceApi(com.cloudera.api.swagger.RolesResourceApi) ApiRoleList(com.cloudera.api.swagger.model.ApiRoleList) ApiClient(com.cloudera.api.swagger.client.ApiClient) DiscoveryApiClient(org.apache.knox.gateway.topology.discovery.cm.DiscoveryApiClient) ApiServiceConfig(com.cloudera.api.swagger.model.ApiServiceConfig) ApiException(com.cloudera.api.swagger.client.ApiException)

Example 3 with ApiConfigList

use of com.cloudera.api.swagger.model.ApiConfigList in project knox by apache.

the class ClouderaManagerServiceDiscoveryTest method doTestDiscovery.

private ServiceDiscovery.Cluster doTestDiscovery(final String hostName, final String serviceName, final String serviceType, final String roleName, final String roleType, final Map<String, String> serviceProperties, final Map<String, String> roleProperties) {
    final String clusterName = "cluster-1";
    GatewayConfig gwConf = EasyMock.createNiceMock(GatewayConfig.class);
    EasyMock.replay(gwConf);
    ServiceDiscoveryConfig sdConfig = createMockDiscoveryConfig(clusterName);
    // Create the test client for providing test response content
    TestDiscoveryApiClient mockClient = new TestDiscoveryApiClient(sdConfig, null, null);
    // Prepare the service list response for the cluster
    ApiServiceList serviceList = EasyMock.createNiceMock(ApiServiceList.class);
    final List<ApiService> apiServiceList = new ArrayList<>();
    apiServiceList.add(createMockApiService(serviceName, serviceType, clusterName));
    EasyMock.expect(serviceList.getItems()).andReturn(apiServiceList).anyTimes();
    EasyMock.replay(serviceList);
    mockClient.addResponse(ApiServiceList.class, new TestApiServiceListResponse(serviceList));
    // Prepare the service config response for the cluster
    ApiServiceConfig serviceConfig = createMockApiServiceConfig(serviceProperties);
    mockClient.addResponse(ApiServiceConfig.class, new TestApiServiceConfigResponse(serviceConfig));
    // Prepare the role
    ApiRole role = createMockApiRole(roleName, roleType, hostName);
    ApiRoleList roleList = EasyMock.createNiceMock(ApiRoleList.class);
    EasyMock.expect(roleList.getItems()).andReturn(Collections.singletonList(role)).anyTimes();
    EasyMock.replay(roleList);
    mockClient.addResponse(ApiRoleList.class, new TestApiRoleListResponse(roleList));
    // Configure the role
    ApiConfigList roleConfigList = createMockApiConfigList(roleProperties);
    mockClient.addResponse(ApiConfigList.class, new TestApiConfigListResponse(roleConfigList));
    // Invoke the service discovery
    ClouderaManagerServiceDiscovery cmsd = new ClouderaManagerServiceDiscovery(true, gwConf);
    // to clear the repo
    cmsd.onConfigurationChange(null, null);
    ServiceDiscovery.Cluster cluster = cmsd.discover(gwConf, sdConfig, clusterName, Collections.emptySet(), mockClient);
    assertNotNull(cluster);
    assertEquals(clusterName, cluster.getName());
    return cluster;
}
Also used : ArrayList(java.util.ArrayList) ApiServiceList(com.cloudera.api.swagger.model.ApiServiceList) ServiceDiscoveryConfig(org.apache.knox.gateway.topology.discovery.ServiceDiscoveryConfig) ApiRole(com.cloudera.api.swagger.model.ApiRole) ApiConfigList(com.cloudera.api.swagger.model.ApiConfigList) ApiService(com.cloudera.api.swagger.model.ApiService) ApiRoleList(com.cloudera.api.swagger.model.ApiRoleList) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) ApiServiceConfig(com.cloudera.api.swagger.model.ApiServiceConfig) ServiceDiscovery(org.apache.knox.gateway.topology.discovery.ServiceDiscovery)

Example 4 with ApiConfigList

use of com.cloudera.api.swagger.model.ApiConfigList in project knox by apache.

the class ClouderaManagerServiceDiscovery method getRoleConfig.

private ApiConfigList getRoleConfig(ServiceDiscoveryConfig serviceDiscoveryConfig, RolesResourceApi rolesResourceApi, ApiService service, ApiRole role) {
    log.lookupRoleConfigsFromRepository();
    // first, try in the service discovery repository
    ApiConfigList configList = repository.getRoleConfigs(serviceDiscoveryConfig, service, role);
    if (configList == null || configList.getItems() == null) {
        // no role configs in the repository -> query CM
        try {
            log.lookupRoleConfigsFromCM();
            configList = rolesResourceApi.readRoleConfig(serviceDiscoveryConfig.getCluster(), role.getName(), service.getName(), VIEW_FULL);
            // make sure that role config is populated in the service discovery repository to avoid subsequent CM calls
            repository.addRoleConfigs(serviceDiscoveryConfig, service, role, configList);
        } catch (Exception e) {
            log.failedToAccessServiceRoleConfigs(service.getName(), role.getName(), serviceDiscoveryConfig.getCluster(), e);
        }
    }
    return configList;
}
Also used : ApiConfigList(com.cloudera.api.swagger.model.ApiConfigList) ApiException(com.cloudera.api.swagger.client.ApiException)

Example 5 with ApiConfigList

use of com.cloudera.api.swagger.model.ApiConfigList in project knox by apache.

the class ClouderaManagerServiceDiscoveryTest method createMockApiConfigList.

private static ApiConfigList createMockApiConfigList(Map<String, String> properties) {
    ApiConfigList configList = EasyMock.createNiceMock(ApiConfigList.class);
    List<ApiConfig> roleConfigs = new ArrayList<>();
    int i = 0;
    for (Map.Entry<String, String> property : properties.entrySet()) {
        ApiConfig config = EasyMock.createNiceMock(ApiConfig.class);
        if (i++ % 2 == 0) {
            EasyMock.expect(config.getName()).andReturn(property.getKey()).anyTimes();
        } else {
            EasyMock.expect(config.getRelatedName()).andReturn(property.getKey()).anyTimes();
        }
        EasyMock.expect(config.getValue()).andReturn(property.getValue()).anyTimes();
        EasyMock.replay(config);
        roleConfigs.add(config);
    }
    EasyMock.expect(configList.getItems()).andReturn(roleConfigs).anyTimes();
    EasyMock.replay(configList);
    return configList;
}
Also used : ApiConfigList(com.cloudera.api.swagger.model.ApiConfigList) ArrayList(java.util.ArrayList) ApiConfig(com.cloudera.api.swagger.model.ApiConfig) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

ApiConfigList (com.cloudera.api.swagger.model.ApiConfigList)7 ApiRole (com.cloudera.api.swagger.model.ApiRole)4 ApiRoleList (com.cloudera.api.swagger.model.ApiRoleList)4 ApiService (com.cloudera.api.swagger.model.ApiService)3 ApiServiceConfig (com.cloudera.api.swagger.model.ApiServiceConfig)3 RolesResourceApi (com.cloudera.api.swagger.RolesResourceApi)2 ServicesResourceApi (com.cloudera.api.swagger.ServicesResourceApi)2 ApiException (com.cloudera.api.swagger.client.ApiException)2 ApiConfig (com.cloudera.api.swagger.model.ApiConfig)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ApiClient (com.cloudera.api.swagger.client.ApiClient)1 ApiServiceList (com.cloudera.api.swagger.model.ApiServiceList)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 GatewayConfig (org.apache.knox.gateway.config.GatewayConfig)1 ServiceDiscovery (org.apache.knox.gateway.topology.discovery.ServiceDiscovery)1 ServiceDiscoveryConfig (org.apache.knox.gateway.topology.discovery.ServiceDiscoveryConfig)1 DiscoveryApiClient (org.apache.knox.gateway.topology.discovery.cm.DiscoveryApiClient)1