Skip to content

Commit

Permalink
Added yaml data size parameter at model source
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Abarca committed Jul 15, 2024
1 parent d3c74f9 commit e72aefa
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ public static String[] getValues() {

public static final String ANSIBLE_ENCRYPT_EXTRA_VARS = "ansible-encrypt-extra-vars";

String ANSIBLE_YAML_DATA_SIZE = "ansible-yaml-data-size";

public static Property PLAYBOOK_PATH_PROP = PropertyUtil.string(
ANSIBLE_PLAYBOOK_PATH,
"Playbook",
Expand Down Expand Up @@ -527,4 +529,13 @@ public static String[] getValues() {
.title("Encrypt Extra Vars.")
.description("Encrypt the value of the extra vars keys.")
.build();

Property YAML_DATA_SIZE_PROP = PropertyBuilder.builder()
.string(ANSIBLE_YAML_DATA_SIZE)
.required(false)
.title("Inventory Yaml Data Size")
.description("Set the MB size (Default value is 10MB)"+
" that the plugin can process with the yaml data response from Ansible."+
" (This only applies when Gather Facts = No)")
.build();
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public class AnsibleResourceModelSource implements ResourceModelSource, ProxyRun

private String inventory;
private boolean gatherFacts;
private Integer yamlDataSize;
private boolean ignoreErrors = false;
private String limit;
private String ignoreTagPrefix;
Expand Down Expand Up @@ -167,6 +168,10 @@ public void configure(Properties configuration) throws ConfigurationException {
gatherFacts = "true".equals(resolveProperty(AnsibleDescribable.ANSIBLE_GATHER_FACTS,null,configuration,executionDataContext));
ignoreErrors = "true".equals(resolveProperty(AnsibleDescribable.ANSIBLE_IGNORE_ERRORS,null,configuration,executionDataContext));

yamlDataSize = getIntegerValue(
resolveProperty(AnsibleDescribable.ANSIBLE_YAML_DATA_SIZE,"10", configuration, executionDataContext),
AnsibleDescribable.ANSIBLE_YAML_DATA_SIZE);

limit = (String) resolveProperty(AnsibleDescribable.ANSIBLE_LIMIT,null,configuration,executionDataContext);
ignoreTagPrefix = (String) resolveProperty(AnsibleDescribable.ANSIBLE_IGNORE_TAGS,null,configuration,executionDataContext);

Expand Down Expand Up @@ -670,9 +675,11 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOEx
*/
public void ansibleInventoryList(NodeSetImpl nodes, AnsibleRunner.AnsibleRunnerBuilder runnerBuilder) throws ResourceModelSourceException {

int codePointLimit = yamlDataSize * 1024 * 1024;

LoaderOptions snakeOptions = new LoaderOptions();
// max inventory file size allowed to 10mb
snakeOptions.setCodePointLimit(10_485_760);
snakeOptions.setCodePointLimit(codePointLimit);
Yaml yaml = new Yaml(new SafeConstructor(snakeOptions));

String listResp = getNodesFromInventory(runnerBuilder);
Expand Down Expand Up @@ -812,4 +819,23 @@ public List<String> listSecretsPathResourceModel(Map<String, Object> configurati

}

/**
*
* @param value parameter from gui
* @param paramName parameter name
* @return an integer value
* @throws ConfigurationException
*/
private Integer getIntegerValue(String value, String paramName) throws ConfigurationException {
if (value == null) {
throw new ConfigurationException("Value cannot be null for parameter: " + paramName);
}

try {
return Integer.parseInt(value);
} catch (NumberFormatException e) {
throw new ConfigurationException("Error parsing " + paramName + " as integer: " + e.getMessage(), e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public AnsibleResourceModelSourceFactory(final Framework framework) {
builder.property(INVENTORY_PROP);
builder.property(CONFIG_FILE_PATH);
builder.property(GATHER_FACTS_PROP);
builder.property(YAML_DATA_SIZE_PROP);
builder.property(IGNORE_ERRORS_PROP);
builder.property(LIMIT_PROP);
builder.property(DISABLE_LIMIT_PROP);
Expand Down

0 comments on commit e72aefa

Please sign in to comment.