Class YAMLHelper


  • public class YAMLHelper
    extends java.lang.Object
    • Constructor Summary

      Constructors 
      Constructor Description
      YAMLHelper()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static com.fasterxml.jackson.databind.JsonNode addLabelToResource​(java.lang.String yaml, java.lang.String labelKey, java.lang.String labelValue)
      Add a label to resource metadata
      static com.fasterxml.jackson.databind.JsonNode editValueInYAML​(java.lang.String yamlAsString, java.lang.String[] fieldnames, java.lang.String value)
      Edit value of a yaml field and return the update yaml as JsonNode
      static java.lang.String getStringValueFromYAML​(java.lang.String yamlAsString, java.lang.String[] path)
      Retrieve value as String from YAML text
      static com.fasterxml.jackson.databind.JsonNode getValueFromYAML​(java.lang.String yamlAsString, java.lang.String[] path)
      Retrieve value as JsonNode from YAML text
      static java.lang.String JSONToYAML​(com.fasterxml.jackson.databind.JsonNode json)  
      static java.lang.String JSONToYAML​(com.fasterxml.jackson.databind.JsonNode json, boolean minimizeQuotes)  
      static com.fasterxml.jackson.databind.JsonNode removeLabelFromResource​(java.lang.String yaml, java.lang.String labelKey)
      Remove a label from resource metadata
      static com.fasterxml.jackson.databind.JsonNode URLToJSON​(java.net.URL file)  
      static com.fasterxml.jackson.databind.JsonNode YAMLToJsonNode​(java.lang.String yaml)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • YAMLHelper

        public YAMLHelper()
    • Method Detail

      • getStringValueFromYAML

        public static java.lang.String getStringValueFromYAML​(java.lang.String yamlAsString,
                                                              java.lang.String[] path)
                                                       throws java.io.IOException
        Retrieve value as String from YAML text
        Parameters:
        yamlAsString - Full YAML where to search the value from
        path - Path to scan to search for the value (e.g to get the resource name `String[] { "metadata", "name" } `)
        Returns:
        last field value or null if the YAML doesn't contain any field
        Throws:
        java.io.IOException - if erroring during parsing
      • getValueFromYAML

        public static com.fasterxml.jackson.databind.JsonNode getValueFromYAML​(java.lang.String yamlAsString,
                                                                               java.lang.String[] path)
                                                                        throws java.io.IOException
        Retrieve value as JsonNode from YAML text
        Parameters:
        yamlAsString - Full YAML where to search the value from
        path - Path to scan to search for the value (e.g to get the resource name `String[] { "metadata", "name" } `)
        Returns:
        last field value or null if the YAML doesn't contain any field
        Throws:
        java.io.IOException - if erroring during parsing
      • JSONToYAML

        public static java.lang.String JSONToYAML​(com.fasterxml.jackson.databind.JsonNode json)
                                           throws java.io.IOException
        Throws:
        java.io.IOException
      • JSONToYAML

        public static java.lang.String JSONToYAML​(com.fasterxml.jackson.databind.JsonNode json,
                                                  boolean minimizeQuotes)
                                           throws java.io.IOException
        Throws:
        java.io.IOException
      • YAMLToJsonNode

        public static com.fasterxml.jackson.databind.JsonNode YAMLToJsonNode​(java.lang.String yaml)
                                                                      throws java.io.IOException
        Throws:
        java.io.IOException
      • URLToJSON

        public static com.fasterxml.jackson.databind.JsonNode URLToJSON​(java.net.URL file)
                                                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • editValueInYAML

        public static com.fasterxml.jackson.databind.JsonNode editValueInYAML​(java.lang.String yamlAsString,
                                                                              java.lang.String[] fieldnames,
                                                                              java.lang.String value)
                                                                       throws java.io.IOException
        Edit value of a yaml field and return the update yaml as JsonNode
        Parameters:
        yamlAsString - original yaml to edit
        fieldnames - array of string to define path of field to change (e.g "spec", "template", "containers[0]", "image")
        value - new value to add to the field
        Returns:
        edited yaml as JsonNode or null if unable to update yaml
        Throws:
        java.io.IOException - if errored during yaml parsing
      • addLabelToResource

        public static com.fasterxml.jackson.databind.JsonNode addLabelToResource​(java.lang.String yaml,
                                                                                 java.lang.String labelKey,
                                                                                 java.lang.String labelValue)
                                                                          throws java.io.IOException
        Add a label to resource metadata
        Parameters:
        yaml - the YAML resource configuration
        labelKey - label key to add
        labelValue - label value to add
        Returns:
        The updated resource with the new label in the metadata
        Throws:
        java.io.IOException - if errored during yaml parsing
      • removeLabelFromResource

        public static com.fasterxml.jackson.databind.JsonNode removeLabelFromResource​(java.lang.String yaml,
                                                                                      java.lang.String labelKey)
                                                                               throws java.io.IOException
        Remove a label from resource metadata
        Parameters:
        yaml - the YAML resource configuration
        labelKey - label key to remove
        Returns:
        the updated resource with the label removed
        Throws:
        java.io.IOException - if errored during yaml parsing