function clusterList = hDiscoverClusters()
% Discover all configured clusters

%   Copyright 2011-2020 The MathWorks, Inc.

    clusterInfos = parallel.internal.cluster.MJSComputeCloudFinder.getCompatibleClusters();
    if isempty(clusterInfos)
        clusterList = parallel.cluster.MJSComputeCloud.empty();
        return
    end
    
    numberOfClusterInfos = numel(clusterInfos);
    clusterList = cell(numberOfClusterInfos, 1);
    for ii = 1:numberOfClusterInfos
        try
            clusterList{ii} = parallel.cluster.MJSComputeCloud(clusterInfos(ii));
        catch E
            iShowWarnings(E, clusterInfos(ii));
        end
    end
    clusterList = [clusterList{:}];
end
%
function iShowWarnings(E, clusterInfo)
    clusterName = clusterInfo.ClusterName;
    if strcmp(E.identifier,'parallel:cluster:MJSComputeCloud:FailedToBuildSupport')
        if ~isempty(E.cause)
            msg = message('parallel:cluster:MJSComputeCloud:SupportBuildFailedDiscoveryWarning', ...
                          clusterName, E.cause{1}.message);
        else
            msg = message('parallel:cluster:MJSComputeCloud:DiscoveryWarning', clusterName);
        end
    else
        msg = message('parallel:cluster:MJSComputeCloud:DiscoveryWarning', clusterName);
    end
    parallel.internal.warningNoBackTrace(msg);
end

