#!/usr/bin/env python3
"""
Update a given tag for a given host using the OMD WATO API.
"""

#########################################################################
### Declarations ########################################################
#########################################################################

import omdclient, optparse, os, sys

#########################################################################
### Configuration #######################################################
#########################################################################

## Central configuration file.
config_file_base = '/etc/omdclient/config.yaml'

## Text for --help
text = "update a given host tag in OMD, using the WATO API"
usage_text = "usage: %prog HOSTNAME TAG VALUE"

#########################################################################
### main () #############################################################
#########################################################################

def main():
    config_file = os.environ.get('OMDCONFIG', config_file_base)
    try:
        config = omdclient.loadCfg(config_file)
    except Exception as e:
        print("failed to load config: %s" % (e))
        sys.exit(3)

    p = omdclient.generateParser(text, usage_text, config)
    group = optparse.OptionGroup(p, 'create/update options')
    p.add_option_group(group)
    opt, args = p.parse_args()

    argdict = omdclient.parserArgDict(opt)

    if len(args) != 3:
        p.print_help()
        sys.exit(1)

    host = args[0]
    tag = args[1]
    value = args[2]

    if value == 'UNDEF': argdict['unset'] = "tag_%s" % tag
    else:                argdict['extra'] = "tag_%s=%s" % (tag, value)

    value, result = omdclient.updateHost(host, argdict)
    if value is False:
        if result is None: print("%s - unknown error" % host)
        else: print("error on update: %s" % result)
        sys.exit(1)

    print("%s - host updated, will now inventory..." % host)

    value1, result1 = omdclient.discoverServicesHost(host, argdict)
    if value1 is False:
        if result is None: print("%s - unknown error" % host)
        else: print("error on inventory: %s" % result)
        sys.exit(1)
    else:
        print("%s - %s" % (host, result1))
        sys.exit(0)

if __name__ == "__main__":
    main()

#########################################################################
### POD Documentation ###################################################
#########################################################################

"""

=head1 NAME

omd-host-tag - update/remove a given host tag in OMD

=head1 SYNOPSIS

B<omd-host-tag> cms-foo my_tag new_value

B<omd-host-tag> cms-foo my_tag UNDEF

=head1 USAGE

omd-host-tag provides a way to update or clear a tag for a specific
host using hte OMD/WATO the OMD/WATO check_mk web interface.

=head1 ARGUMENTS

=head2 REQUIRED

=over 4

=item I<hostname>

Hostname for which to query.  No default, must be set.

=item I<tag>

Tag to update.  This tag must already exist.

=item I<value>

Value of this tag.  UNDEF means "unset the flag".

=back

=head2 DEFAULT

=over 4

=item B<--debug>

If set, print debugging information.

=item B<--help>

Print this information and exit.

=back

=head2 CONNECTION OPTIONS

=over 4

=item B<--apikey> I<key>

Password for the API User.  Default: comes from the configuration file.

=item B<--server> I<server>

Host name of the server.  Default: comes from the configuration file.

=item B<--site> I<site>

Site name within the server.  Default: comes from the configuration file.

=item B<--user> I<user>

API User name.  The user must exist on the server, and be an 'automation
user'.  Default: comes from the configuration file.

=back

=head1 FILES

=over 4

=item F</etc/omdclient/config.yaml>

=back

=head1 SEE ALSO

https://mathias-kettner.de/checkmk_wato_webapi.html

=head1 AUTHOR

Tim Skirvin <tskirvin@fnal.gov>

=head1 LICENSE + COPYRIGHT

Copyright 2017, Fermi National Accelerator Laboratory

This program is free software; you may redistribute it and/or modify it
under the same terms as Perl itself.

=cut

"""
