Uploaded image for project: 'CFEngine Community'
  1. CFEngine Community
  2. CFE-3311

support column headers in readcsv()

    XMLWordPrintable

    Details

      Description

      It would be convenient if readcsv() supported column headers.

      That way the policy could be implemented identically for csv, json, and yaml.

      Location data in json /tmp/location.json
      {
          "10.101.188.0/24": {
              "class": "location_one"
          },
          "10.101.47.0/24": {
              "class": "location_two"
          },
          "10.177.0.0/18": {
              "class": "location_three"
          },
          "192.168.42.0/24": {
              "class": "bat_cave"
          }
      }
      
      Location data in csv /tmp/location.yaml
      ---
      10.101.188.0/24:
        class: location_one
      10.101.47.0/24:
        class: location_two
      10.177.0.0/18:
        class: location_three
      192.168.42.0/24:
        class: bat_cave
      
      Location data in csv /tmp/location.csv (don't forget unix2dos)
      10.101.188.0/24,location_one
      10.101.47.0/24,location_two
      10.177.0.0/18,location_three
      192.168.42.0/24,bat_cave
      
      Parse location from data
      bundle agent location_data_from_data(type)
      {
        vars:
          "d" data => readyaml( "/tmp/location.$(type)" );
          "_i" slist => getindices( d );
      
        classes:
          "$(d[$(_i)][class])"
            expression => iprange( "$(_i))" );
      
        reports:
          "My location class from $(type) data is '$(d[$(_i)][class])'" if => "$(d[$(_i)][class])";
      }
      bundle agent location_data_from_csv
      # Unfortunately, since readcsv() doens't support headers, we must implemnt differently than for json|yaml
      {
        vars:
          "d" data => readcsv( "/tmp/location.csv" );
          "_i" slist => getindices( d );
      
        classes:
          "$(d[$(_i)][1])"
            expression => iprange( "$(d[$(_i)][0])" );
      
        reports:
          "My location class from csv data is '$(d[$(_i)][1])'" if => "$(d[$(_i)][1])";
      }
      bundle agent __main__
      {
        methods:
          "parse location from json" usebundle => location_data_from_data(json);
          "parse location from json" usebundle => location_data_from_data(yaml);
          "parse location from csv" usebundle => location_data_from_csv;
      }
      
      R: My location class from json data is 'bat_cave'
      R: My location class from yaml data is 'bat_cave'
      R: My location class from csv data is 'bat_cave'

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              a10042 Nick Anderson
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:

                Summary Panel