require stdlib
require concat

class eid::telegraf(String $version = "stable", Array $volumes = []) {
   $influxdb2_token = hiera("influxdb2_token","NOT_SET_IN_HIERA");
   $influxdb2_org = hiera("influxdb2_org","NOT_SET_IN_HIERA");
   $influxdb2_bucket = hiera("influxdb2_bucket","NOT_SET_IN_HIERA");
   file {['/etc/telegraf','/etc/telegraf/telegraf.conf.d']: ensure => directory }
   concat {'/etc/telegraf/telegraf.conf':
      owner     => 'root',
      group     => 'root',
      mode      => '0644',
      notify    => Sunet::Docker_run['telegraf']
   }

   eid_glob(["/etc/telegraf/telegraf.conf.d/*.conf"]).each |String $file| {
      concat::fragment { $file:
         target => "/etc/telegraf/telegraf.conf",
         source => $file,
      }
   }

   $_volumes = flatten(["/dev/log:/dev/log","/var/log:/var/log","/etc/telegraf:/etc/telegraf","/var/run/utmp:/var/run/utmp:ro",$volumes])

   sunet::docker_run {"telegraf":
      hostname => "${::fqdn}",
      image     => "docker.sunet.se/eduid/telegraf",
      imagetag  => $version,
      volumes   => $_volumes,
      env       => ["INFLUXDB2_TOKEN=$influxdb2_token","INFLUXDB2_ORG=$influxdb2_org","INFLUXDB2_BUCKET=$influxdb2_bucket"]
   }
}
