# == Class: vigie::validate_compute
#
# Configures validating compute nodes
#
# === Parameters
# [*glance_image*]
#
# [*flavor*]
#   Flavor to use to validate compute nodes.
#
# [*network*]
#   Network to use to valide compute nodes.
#
# [*ssh_username*]
#   (optional) SSH username to use when validating compute.
#   Default to: 'debian'.
#
# [*keep_compute_enabled*]
#   (optional) Should compute nodes stay enabled after validation.
#   Defaults to 'false'.
#
# [*ssh_jump_host_enable*]
#   (optional) Should we use a jump host for validation?
#   Defaults to 'false'.
#
# [*ssh_jump_host_hostname*]
#   (optional) Jump host hostname for validation.
#   Mandatory if ssh_jump_host_enable is set to true.
#   Defaults to $facts['os_service_default'].
#
# [*ssh_jump_host_port*]
#   (optional) Jump host port.
#   Defaults to $facts['os_service_default'].
#
# [*ssh_private_key*]
#   (optional) base64 SSH private key to use for validating compute nodes.
#   Mandatory paramaeter.
#
# [*ssh_public_key*]
#   (optional) base64 SSH public key to use for validating compute nodes.
#   Mandatory paramaeter.
#
class vigie::validate_compute (
  $glance_image           = undef,
  $flavor                 = undef,
  $network                = undef,
  $ssh_username           = 'debian',
  $keep_compute_enabled   = $facts['os_service_default'],
  $ssh_jump_host_enable   = $facts['os_service_default'],
  $ssh_jump_host_hostname = $facts['os_service_default'],
  $ssh_jump_host_port     = $facts['os_service_default'],
  $ssh_key_type           = 'ssh-rsa',
  $ssh_private_key        = undef,
  $ssh_public_key         = undef,
) {

  include vigie::deps
  include vigie::params

  if $ssh_jump_host_enable {
    $path  = '/etc/vigie'
    $user  = 'vigie'
    $group = 'vigie'

    $private_key_file_path = $ssh_key_type ? {
      'ssh-rsa'   => "${path}/.ssh/id_rsa",
      'ssh-dsa'   => "${path}/.ssh/id_dsa",
      'ssh-ecdsa' => "${path}/.ssh/id_ecdsa",
      default     => undef
    }

    $public_key_file_path = $ssh_key_type ? {
      'ssh-rsa'   => "${path}/.ssh/id_rsa.pub",
      'ssh-dsa'   => "${path}/.ssh/id_dsa.pub",
      'ssh-ecdsa' => "${path}/.ssh/id_ecdsa.pub",
      default     => undef
    }

    file { "${path}/.ssh":
      ensure  => directory,
      mode    => '0700',
      owner   => $user,
      group   => $group,
    }

    file { $private_key_file_path:
      content => $ssh_private_key,
      mode    => '0600',
      owner   => $user,
      group   => $group,
      require => File["${path}/.ssh"],
    }

    file { $public_key_file_path:
      content => $ssh_public_key,
      mode    => '0600',
      owner   => $user,
      group   => $group,
      require => File["${path}/.ssh"],
    }
  }

  vigie_config {
    'validate_compute/glance_image':           value => $glance_image;
    'validate_compute/flavor':                 value => $flavor;
    'validate_compute/network':                value => $network;
    'validate_compute/ssh_username':           value => $ssh_username;
    'validate_compute/keep_enabled':           value => $keep_compute_enabled;
    'validate_compute/ssh_use_jump_host':      value => $ssh_jump_host_enable;
    'validate_compute/ssh_jump_host_hostname': value => $ssh_jump_host_hostname;
    'validate_compute/ssh_jump_host_port':     value => $ssh_jump_host_port;
    'validate_compute/ssh_jump_host_key_file': value => '/etc/vigie/.ssh/id_rsa';
  }

}
