#compdef isi events

# Copyright C 2010 by Isilon Systems, Inc. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
# 
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# Inc.
# 
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems. 
#  
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc.  Legion is a trademark of Isilon Systems, Inc.  All other
# trademarks and registered trademarks are the property of their
# respective holders.

_isi_events() {
  if (( CURRENT == 2 )); then
    _tags values && { compadd $@ list show quiet unquiet cancel notifications sendtest }
    _arguments \
        '--help[Print usage help and exit]'
  else
    local cmd=${words[2]}
    (( CURRENT-- ))
    shift words
    if [[ $cmd == quiet ]] ; then
      curcontext="${curcontext%:*:*}:isi-events-quiet:"
      _isi_events_specify_many_or_all
    elif [[ $cmd == cancel ]]; then
      curcontext="${curcontext%:*:*}:isi-events-cancel:"
      _isi_events_specify_many_or_all
    elif [[ $cmd == list ]]; then
      curcontext="${curcontext%:*:*}:isi-events-list:"
      _isi_events_list
    elif [[ $cmd == notifications ]]; then
      curcontext="${curcontext%:*:*}:isi-events-notifications:"
      _isi_events_notifications
    elif [[ $cmd == show ]]; then
      curcontext="${curcontext%:*:*}:isi-events-show:"
      _isi_events_specify_one
    elif [[ $cmd == unquiet ]]; then
      curcontext="${curcontext%:*:*}:isi-events-unquiet:"
      _isi_events_specify_many
    fi
  fi
}

# command takes one or more instance ids or 'all', positional or option forms
(( $+functions[_isi_events_specify_one] )) ||
_isi_events_specify_one() {
  _values 'id' \
    $(isi events list --all --columns id --csv)
  _arguments \
    {-i,--instanceids}'=[id of event to display]:instance_id:_isi_events_id' \
    {-h,--help}'[print help and exit]'
}

# command takes one or more instance ids or 'all', positional or option forms
(( $+functions[_isi_events_specify_many] )) ||
_isi_events_specify_many() {
  _values -s , 'id' \
    $(isi events list --all --columns id --csv)
  _arguments \
    {-i,--instanceids}'=[ids of event to display]:instance_id:_isi_events_ids' \
    {-h,--help}'[print help and exit]'
}

# command takes one or more instance ids, positional or option forms
(( $+functions[_isi_events_specify_many_or_all] )) ||
_isi_events_specify_many_or_all() {
  _values -s , 'id' \
    'all' \
    $(isi events list --all --columns id --csv)
  _arguments \
    {-i,--instanceids}'=[ids of event to display]:instance_id:_isi_events_ids_or_all' \
    {-h,--help}'[print help and exit]'
}

(( $+functions[_isi_events_list] )) ||
_isi_events_list() {
  _arguments \
    {-o,--oldest}'=[oldest events to retrieve, date or count]' \
    {-n,--newest}'=[newest events to retrieve, date or count]' \
    {-s,--history}'[only historical events]' \
    {-C,--coalesced}'[include coalesced events]' \
    {-c,--count}'[show only the total event count]' \
    {-v,--severity}'=[only specified severity (csv)]:severity_list:_isi_events_severity' \
    {-l,--localdb}'[use local DB rather than master]' \
    {-L,--limit}'=[Limit query to N events]' \
    {-N,--nodes}'=[filter by node (csv of LNN)]:node_list:_isi_events_nodes' \
    {-i,--types}'=[filter by event types]:event_types:_isi_events_types' \
    {-c,--columns}'=[output only columns]:event_columns:_isi_events_columns_all' \
    {-b,--sort-by}'=[order by columns]:event_columns:_isi_events_columns' \
    '--csv[output csv and suppress headers]' \
    {-w,--wide}'[display in wide mode]' \
    {-d,--debug}'[display debugging information]' \
    {-A,--all}'[display all events in database]' \
    {-h,--help}'[print help and exit]'
}

(( $+functions[_isi_events_notifications] )) ||
_isi_events_notifications() {
  if (( CURRENT == 2 ))
  then
    _tags values && { compadd $@ list create modify delete }
    _arguments '--help[Print usage help and exit]'
  else
    local cmd=${words[2]}
    (( CURRENT-- ))
    shift words
    if [[ $cmd == list ]] ; then
      curcontext="${curcontext%:*:*}:isi-events-notifications-list:"
      _isi_events_notifications_list
    fi
  fi
}

(( $+functions[_isi_events_notifications_list] )) ||
_isi_events_notifications_list() {
  _arguments \
    {-n,--name}'=[list specific notification]' \
    {-h,--help}'[print help and exit]'
}

(( $+functions[_isi_events_columns] )) ||
_isi_events_columns() {
  _values -s , 'columns' \
    'quiet' \
    'lnn' \
    'type' \
    'id' \
    'severity' \
    'message' \
    'start_time' \
    'end_time' \
    'value' \
    'coalesce_id'
}

(( $+functions[_isi_events_columns_all] )) ||
_isi_events_columns_all() {
  _values -s , 'columns' \
    'all' \
    'quiet' \
    'lnn' \
    'type' \
    'id' \
    'severity' \
    'message' \
    'start_time' \
    'end_time' \
    'value' \
    'coalesce_id'
}

# option value consisting of csv of instance iids
(( $+functions[_isi_events_id] )) ||
_isi_events_id() {
  _values 'id' \
  $(isi events list --all --columns id --csv)
}

# option value consisting of csv of instance iids
(( $+functions[_isi_events_ids] )) ||
_isi_events_ids() {
  _values -s , 'id' \
  $(isi events list --all --columns id --csv)
}

# option value consisting of csv of instance iids or 'all'
(( $+functions[_isi_events_ids_or_all] )) ||
_isi_events_ids_or_all() {
  _values -s , 'id' \
  'all' \
  $(isi events list --all --columns id --csv)
}

(( $+functions[_isi_event_types] )) ||
_isi_events_types() {
  _values -s , 'ids' $(isi events list --all --columns type --csv | sort -u)
}

(( $+functions[_isi_events_severity] )) ||
_isi_events_severity() {
  _values -s , 'severity filter' \
    'info' \
    'warn' \
    'critical' \
    'emergency'
}

(( $+functions[_isi_events_nodes] )) ||
_isi_events_nodes() {
  _values -s , 'node filter' \
    $(isi_nodes %{lnn}) \
    'local' \
    'all'
}
