config/sysinv/cgts-client/cgts-client/cgtsclient/v1/label_shell.py
Teresa Ho 3c3a1f293a host-label validation via kubernetes-client
The host label key and value are changed to be stored in
separate fields in the database. A unique constraint
between the host and the label key is added.
The validation of the label key and value is performed in the
kubernetes client api instead of replicating the checking
in the host-label api. The error message from the kubernetes
client api is passed to the host-label api.

Story: 2002845
Task: 24936

Change-Id: I7e318d5e58602d33652d49452e35d99322565148
2018-10-04 13:02:59 -04:00

91 lines
2.8 KiB
Python

#!/usr/bin/env python
#
# Copyright (c) 2018 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# All Rights Reserved.
#
from cgtsclient.common import utils
from cgtsclient import exc
from cgtsclient.v1 import ihost as ihost_utils
def _print_label_show(obj):
fields = ['uuid', 'host_uuid', 'label_key', 'label_value']
data = [(f, getattr(obj, f, '')) for f in fields]
utils.print_tuple_list(data)
@utils.arg('hostnameorid',
metavar='<hostname or id>',
help="Name or ID of host [REQUIRED]")
def do_host_label_list(cc, args):
"""List kubernetes labels assigned to a host."""
ihost = ihost_utils._find_ihost(cc, args.hostnameorid)
host_label = cc.label.list(ihost.uuid)
for i in host_label[:]:
setattr(i, 'hostname', ihost.hostname)
field_labels = ['hostname', 'label key', 'label value']
fields = ['hostname', 'label_key', 'label_value']
utils.print_list(host_label, fields, field_labels, sortby=1)
@utils.arg('hostnameorid',
metavar='<hostname or id>',
help="Name or ID of host [REQUIRED]")
@utils.arg('attributes',
metavar='<name=value>',
nargs='+',
action='append',
default=[],
help="List of Kubernetes labels")
def do_host_label_assign(cc, args):
"""Update the Kubernetes labels on a host."""
attributes = utils.extract_keypairs(args)
ihost = ihost_utils._find_ihost(cc, args.hostnameorid)
new_labels = cc.label.assign(ihost.uuid, attributes)
for p in new_labels.labels:
uuid = p['uuid']
if uuid is not None:
try:
label_obj = cc.label.get(uuid)
except exc.HTTPNotFound:
raise exc.CommandError('Host label not found: %s' % uuid)
_print_label_show(label_obj)
@utils.arg('hostnameorid',
metavar='<hostname or id>',
help="Name or ID of host [REQUIRED]")
@utils.arg('attributes',
metavar='<name>',
nargs='+',
action='append',
default=[],
help="List of Kubernetes label keys")
def do_host_label_remove(cc, args):
"""Remove Kubernetes label(s) from a host"""
ihost = ihost_utils._find_ihost(cc, args.hostnameorid)
for i in args.attributes[0]:
lbl = _find_host_label(cc, ihost, i)
if lbl:
cc.label.remove(lbl.uuid)
print 'Deleted host label %s for host %s' % (i, ihost.hostname)
def _find_host_label(cc, host, label):
host_labels = cc.label.list(host.uuid)
for lbl in host_labels:
if lbl.host_uuid == host.uuid and lbl.label_key == label:
break
else:
lbl = None
print('Host label not found: host %s, label key %s ' %
(host.hostname, label))
return lbl