public void execute()

in src/main/java/com/awslabs/aws/greengrass/provisioner/implementations/helpers/BasicGroupQueryHelper.java [67:206]


    public void execute(QueryArguments queryArguments) {
        if (!queryArguments.getGroupCa &&
                !queryArguments.listSubscriptions &&
                !queryArguments.listFunctions &&
                !queryArguments.listDevices &&
                !queryArguments.downloadLogs &&
                !queryArguments.watchLogs &&
                !queryArguments.diagnose) {
            throw new RuntimeException("No query specified");
        }

        GreengrassGroupName greengrassGroupName = ImmutableGreengrassGroupName.builder().groupName(queryArguments.groupName).build();
        Optional<GroupInformation> optionalGroupInformation = v2GreengrassHelper.getGroupInformation(greengrassGroupName).findFirst();

        if (!optionalGroupInformation.isPresent()) {
            throw new RuntimeException(String.join("", "Group [", queryArguments.groupName, "] not found"));
        }

        GroupInformation groupInformation = optionalGroupInformation.get();

        if (queryArguments.getGroupCa) {
            Optional<GetGroupCertificateAuthorityResponse> optionalGetGroupCertificateAuthorityResponse = v2GreengrassHelper.getGroupCertificateAuthorityResponse(groupInformation);

            if (!optionalGetGroupCertificateAuthorityResponse.isPresent()) {
                throw new RuntimeException("Couldn't get the group CA");
            }

            String pem = optionalGetGroupCertificateAuthorityResponse.get().pemEncodedCertificate();
            log.info(String.join("", "Group CA for group [", queryArguments.groupName, "]\n", pem));

            String outputFilename = String.join("", BUILD_DIRECTORY, queryArguments.groupName, "_Core_CA.pem");

            writeToFile(queryArguments, pem, outputFilename);

            return;
        }

        if (queryArguments.listSubscriptions) {
            List<Subscription> subscriptions = v2GreengrassHelper.getSubscriptions(groupInformation)
                    .orElseThrow(() -> new RuntimeException("Group not found, can not continue"));

            log.info("Subscriptions:");
            String output = jsonHelper.toJson(subscriptions);
            log.info(output);

            String outputFilename = String.join("", BUILD_DIRECTORY, queryArguments.groupName, "_subscription_table.json");

            writeToFile(queryArguments, output, outputFilename);

            return;
        }

        if (queryArguments.listFunctions) {
            List<Function> functions = v2GreengrassHelper.getFunctions(groupInformation)
                    .orElseThrow(() -> new RuntimeException("Group not found, can not continue"));

            log.info("Functions:");
            String output = jsonHelper.toJson(functions);
            log.info(output);

            String outputFilename = String.join("", BUILD_DIRECTORY, queryArguments.groupName, "_function_table.json");

            writeToFile(queryArguments, output, outputFilename);

            return;
        }

        if (queryArguments.listDevices) {
            List<Device> devices = v2GreengrassHelper.getDevices(groupInformation)
                    .orElseThrow(() -> new RuntimeException("Group not found, can not continue"));

            log.info("Devices:");
            String output = jsonHelper.toJson(devices);
            log.info(output);

            String outputFilename = String.join("", BUILD_DIRECTORY, queryArguments.groupName, "_device_table.json");

            writeToFile(queryArguments, output, outputFilename);

            return;
        }

        if (queryArguments.downloadLogs) {
            Stream<Tuple3<LogGroup, LogStream, String>> logs = getLatestLogMessagesForGroup(greengrassGroupName, groupInformation);

            File directory = cleanAndCreateDirectory(queryArguments.groupName);

            logs.forEach(events -> saveLogEvents(directory, events));

            if (!ioHelper.isRunningInDocker()) {
                log.info(String.join("", "Logs written to [", directory.getPath(), "]"));
            } else {
                // Remove the leading slash
                log.info(String.join("", "Logs copied to host in [", directory.getPath().substring(1), "]"));
            }

            return;
        }

        if (queryArguments.watchLogs) {
            Stream<Tuple3<LogGroup, LogStream, GetLogEventsResponse>> logEvents = getLatestLogEventsForGroup(greengrassGroupName, groupInformation);

            Stream<Tuple3<LogGroup, LogStream, String>> logGroupStreamAndForwardTokens = logEvents
                    .map(this::getNextForwardTokenForEvents);

            do {
                // Get the new events using the forward tokens
                List<Tuple3<LogGroup, LogStream, GetLogEventsResponse>> newEvents = logGroupStreamAndForwardTokens
                        .map(this::getLogEvents)
                        .collect(Collectors.toList());

                // Print the new events
                newEvents.forEach(this::printLogEvents);

                // Get the next set of forward tokens
                logGroupStreamAndForwardTokens = newEvents.stream()
                        .map(this::getNextForwardTokenForEvents);

                // Sleep so we don't hit the CloudWatch Logs APIs too much
                ioHelper.sleep(1000);
            } while (true);
        }

        if (queryArguments.diagnose) {
            List<Tuple3<LogGroup, LogStream, String>> logs = getLatestLogMessagesForGroup(greengrassGroupName, groupInformation)
                    .collect(Collectors.toList());

            if (!topLevelLogsPresent(logs)) {
                log.error("Not all of the Greengrass logs are present in CloudWatch. Turn on CloudWatch logging in your Greengrass group, redeploy, and try again.");

                return;
            }

            diagnosticsHelper.runDiagnostics(logs);

            return;
        }

        throw new RuntimeException("This should never happen.  This is a bug.");
    }