src/main/java/com/digitalglobe/util/maven/plugin/CloudFormationDeployMavenPlugin.java
/**
     * Use this method to evaluate parameters from the execution of a describe AWS command.
     *
     * @param credentials is a default set of credentials.
     * @param outputParameters is the output parameters array to populate with the results from the command.
     * @param parameterMappings are the mappings to execute.
     * @param ssmClient is the client to use for the parameter store.
     * @param region is the region to store parameters in.
     * @throws IOException when an exception occurs while reading/writing to process or file system.
     * @throws ClassCastException when the parameter isn't a string.  This should never happen (bug if it does).
     * @throws MojoExecutionException when it can't find a parameter.
     */
Use this method to evaluate parameters from the execution of a describe AWS command.
@param credentials is a default set of credentials.
@param outputParameters is the output parameters array to populate with the results from the command.
@param parameterMappings are the mappings to execute.
@param ssmClient is the client to use for the parameter store.
@param region is the region to store parameters in.
@throws IOException when an exception occurs while reading/writing to process or file system.
@throws ClassCastException when the parameter isn't a string.  This should never happen (bug if it does).
@throws MojoExecutionException when it can't find a parameter.
Use this method to evaluate parameters from the execution of a describe AWS command . @param credentials is a default set of credentials . @param outputParameters is the output parameters array to populate with the results from the command . @param parameterMappings are the mappings to execute . @param ssmClient is the client to use for the parameter store . @param region is the region to store parameters in . @throws IOException when an exception occurs while reading / writing to process or file system . @throws ClassCastException when the parameter isn ' t a string . This should never happen ( bug if it does ) . @throws MojoExecutionException when it can ' t find a parameter .
private void processCommandOutputParameters(AwsCredentialsProvider credentials,
                                                SsmClient ssmClient,
                                                Map<String, String> outputParameters,
                                                CliCommandOutputParameterMapping[] parameterMappings, String region)
            throws IOException, MojoExecutionException, ClassCastException {
        if(parameterMappings != null) {
            System.out.println("Command Output Parameter Mappings:");
            for (CliCommandOutputParameterMapping mapping : parameterMappings) {
                if (((mapping.condition == null) || conditions.get(mapping.condition)) &&
                        evaluateCheckCondition(mapping.checkCondition, outputParameters) &&
                        testRegionCondition(mapping.regionCondition, mapping.regionConditionExclude)) {
                    HashMap<String, String> environmentMap = new HashMap<>();
                    getCredentialMap(credentials, mapping.roleArn, environmentMap);
                    StringBuilder mappingCommand = new StringBuilder(mapping.command);
                    if (mapping.commandParameters != null) {
                        for (StackInputParameter commandParameter : mapping.commandParameters) {
                            String parameterValue = getInputParameterValue(outputParameters, ssmClient,
                                    commandParameter);
                            if(mapping.commandParameterSpacing) {
                                mappingCommand
                                        .append(" ")
                                        .append(commandParameter.parameterName)
                                        .append(" ")
                                        .append(parameterValue);
                            } else {
                                mappingCommand.append(commandParameter.parameterName).append(parameterValue);
                            }
                        }
                    }
                    String builtCommand = mappingCommand.toString().replace("{SPACE}", " ");
                    audit.write("Executing: " + mappingCommand.toString() + "\n");
                    ExecuteCommand command = new ExecuteCommand()
                            .withCommandInformation(builtCommand.split("\\s+(?=(?:[^\'\"`]*(?:(\')[^\']*\\1|(\")[^\"]*\\2|(`)[^`]*\\3))*[^\'\"`]*$)"))
                            .withEnvironmentMap(environmentMap);
                    command.executeCommand();
                    if(command.getExecutionErrors() != null) {
                        Arrays.stream(command.getExecutionErrors())
                                .forEach(error -> {
                                        try {
                                            audit.write(error.getMessage() + "\n");
                                        } catch (IOException ioex) { /* Ignore */ }
                                });
                        throw new RuntimeException("Unable to execute command.");
                    } else {
                        String errors = command.getStandardErrorFromCommand().toString();
                        if(errors.length() > 0) {
                            System.out.println("Errors: " + errors);
                            audit.write("Errors: " + errors + "\n");
                            throw new RuntimeException("Unable to execute command.");
                        }
                    }
                    if (mapping.parameters != null) {
                        String output = command.getStandardOutputFromCommand().toString();
                        ObjectMapper mapper = new ObjectMapper();
                        LinkedHashMap map = mapper.readValue(output, LinkedHashMap.class);
                        for (String key : mapping.parameters.keySet()) {
                            Pattern pattern = Pattern.compile("^(/[A-Za-z0-9_-]+(\\[[A-Za-z0-9_/= -]+\\])?)+$");
                            Matcher matcher = pattern.matcher(mapping.parameters.get(key).parameterName);
                            if(!matcher.matches()) throw new MojoExecutionException("Invalid parameter name syntax.");
                            String[] pathElements = mapping.parameters.get(key).parameterName.split("/(?!(?:[^\\[]+\\]))");
                            Object parameter = map;
                            String array = null;
                            for (String element : pathElements) {
                                if(array == null) {
                                    if (!parameter.getClass().isAssignableFrom(LinkedHashMap.class))
                                        throw new MojoExecutionException("Element is not a Dictionary");
                                } else {
                                    if (!parameter.getClass().isAssignableFrom(ArrayList.class))
                                        throw new MojoExecutionException("Element is not an Array.");
                                }
                                if (element.length() > 0) {
                                    pattern = Pattern.compile(
                                            "^([A-Za-z0-9_-]+)(\\[([0-9]+|[A-Za-z0-9_-]+=[A-Za-z0-9_ /-]+)\\])?$");
                                    matcher = pattern.matcher(element);
                                    if (!matcher.matches())
                                        throw new MojoExecutionException("Couldn't find parameter: " + element);
                                    String name = matcher.group(1);
                                    if (array != null) {
                                        parameter = processArray(parameter, array, false);
                                        if(parameter == null) break;
                                    }
                                    parameter = ((LinkedHashMap) parameter).get(name);
                                    array = matcher.group(3);
                                }
                            }
                            if ((parameter == null) || !parameter.getClass().isAssignableFrom(String.class)) {
                                if ((parameter != null) && parameter.getClass().isAssignableFrom(ArrayList.class) &&
                                        (array != null)) parameter = processArray(parameter, array, true);
                                if ((parameter == null) || !parameter.getClass().isAssignableFrom(String.class)) {
                                    if (mapping.parameters.get(key).defaultParameterValue != null) {
                                        parameter = mapping.parameters.get(key).defaultParameterValue;
                                    } else throw new MojoExecutionException("Couldn't find parameter: " + key);
                                }
                            }
                            ProcessMapping(outputParameters, ssmClient, key, (String) parameter,
                                    mapping.parameters.get(key), region);
                        }
                    }
                }
            }
            System.out.println();
        }
    }
private void processCommandOutputParameters ( AwsCredentialsProvider credentials , SsmClient ssmClient , Map < String , String > outputParameters , CliCommandOutputParameterMapping [ ] parameterMappings , String region ) throws IOException , MojoExecutionException , ClassCastException { if ( parameterMappings != null ) { System . out . println ( "Command Output Parameter Mappings:" ) ; for ( CliCommandOutputParameterMapping mapping : parameterMappings ) { if ( ( ( mapping . condition == null ) || conditions . get ( mapping . condition ) ) && evaluateCheckCondition ( mapping . checkCondition , outputParameters ) && testRegionCondition ( mapping . regionCondition , mapping . regionConditionExclude ) ) { HashMap < String , String > environmentMap = new HashMap < > ( ) ; getCredentialMap ( credentials , mapping . roleArn , environmentMap ) ; StringBuilder mappingCommand = new StringBuilder ( mapping . command ) ; if ( mapping . commandParameters != null ) { for ( StackInputParameter commandParameter : mapping . commandParameters ) { String parameterValue = getInputParameterValue ( outputParameters , ssmClient , commandParameter ) ; if ( mapping . commandParameterSpacing ) { mappingCommand . append ( " " ) . append ( commandParameter . parameterName ) . append ( " " ) . append ( parameterValue ) ; } else { mappingCommand . append ( commandParameter . parameterName ) . append ( parameterValue ) ; } } } String builtCommand = mappingCommand . toString ( ) . replace ( "{SPACE}" , " " ) ; audit . write ( "Executing: " + mappingCommand . toString ( ) + "\n" ) ; ExecuteCommand command = new ExecuteCommand ( ) . withCommandInformation ( builtCommand . split ( "\\s+(?=(?:[^\'\"`]*(?:(\')[^\']*\\1|(\")[^\"]*\\2|(`)[^`]*\\3))*[^\'\"`]*$)" ) ) . withEnvironmentMap ( environmentMap ) ; command . executeCommand ( ) ; if ( command . getExecutionErrors ( ) != null ) { Arrays . stream ( command . getExecutionErrors ( ) ) . forEach ( error -> { try { audit . write ( error . getMessage ( ) + "\n" ) ; } catch ( IOException ioex ) { /* Ignore */ } } ) ; throw new RuntimeException ( "Unable to execute command." ) ; } else { String errors = command . getStandardErrorFromCommand ( ) . toString ( ) ; if ( errors . length ( ) > 0 ) { System . out . println ( "Errors: " + errors ) ; audit . write ( "Errors: " + errors + "\n" ) ; throw new RuntimeException ( "Unable to execute command." ) ; } } if ( mapping . parameters != null ) { String output = command . getStandardOutputFromCommand ( ) . toString ( ) ; ObjectMapper mapper = new ObjectMapper ( ) ; LinkedHashMap map = mapper . readValue ( output , LinkedHashMap . class ) ; for ( String key : mapping . parameters . keySet ( ) ) { Pattern pattern = Pattern . compile ( "^(/[A-Za-z0-9_-]+(\\[[A-Za-z0-9_/= -]+\\])?)+$" ) ; Matcher matcher = pattern . matcher ( mapping . parameters . get ( key ) . parameterName ) ; if ( ! matcher . matches ( ) ) throw new MojoExecutionException ( "Invalid parameter name syntax." ) ; String [ ] pathElements = mapping . parameters . get ( key ) . parameterName . split ( "/(?!(?:[^\\[]+\\]))" ) ; Object parameter = map ; String array = null ; for ( String element : pathElements ) { if ( array == null ) { if ( ! parameter . getClass ( ) . isAssignableFrom ( LinkedHashMap . class ) ) throw new MojoExecutionException ( "Element is not a Dictionary" ) ; } else { if ( ! parameter . getClass ( ) . isAssignableFrom ( ArrayList . class ) ) throw new MojoExecutionException ( "Element is not an Array." ) ; } if ( element . length ( ) > 0 ) { pattern = Pattern . compile ( "^([A-Za-z0-9_-]+)(\\[([0-9]+|[A-Za-z0-9_-]+=[A-Za-z0-9_ /-]+)\\])?$" ) ; matcher = pattern . matcher ( element ) ; if ( ! matcher . matches ( ) ) throw new MojoExecutionException ( "Couldn't find parameter: " + element ) ; String name = matcher . group ( 1 ) ; if ( array != null ) { parameter = processArray ( parameter , array , false ) ; if ( parameter == null ) break ; } parameter = ( ( LinkedHashMap ) parameter ) . get ( name ) ; array = matcher . group ( 3 ) ; } } if ( ( parameter == null ) || ! parameter . getClass ( ) . isAssignableFrom ( String . class ) ) { if ( ( parameter != null ) && parameter . getClass ( ) . isAssignableFrom ( ArrayList . class ) && ( array != null ) ) parameter = processArray ( parameter , array , true ) ; if ( ( parameter == null ) || ! parameter . getClass ( ) . isAssignableFrom ( String . class ) ) { if ( mapping . parameters . get ( key ) . defaultParameterValue != null ) { parameter = mapping . parameters . get ( key ) . defaultParameterValue ; } else throw new MojoExecutionException ( "Couldn't find parameter: " + key ) ; } } ProcessMapping ( outputParameters , ssmClient , key , ( String ) parameter , mapping . parameters . get ( key ) , region ) ; } } } } System . out . println ( ) ; } }
Use this method to evaluate parameters from the execution of a describe AWS command.
Use this method to evaluate parameters from the execution of a describe AWS command .
// Retrieve the credential values and set them for passing to the environment variables. //noinspection ConstantConditions