summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/Server/Admin/Client.py
blob: 325b7ae6eda075173c9449e2de9546446f4a3d5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
""" Create, delete, or list client entries """

import sys
import Bcfg2.Server.Admin
from Bcfg2.Server.Plugin import MetadataConsistencyError


def get_attribs(args):
    """ Get a list of attributes to set on a client when adding/updating it """
    attr_d = {}
    for i in args[2:]:
        attr, val = i.split('=', 1)
        if attr not in ['profile', 'uuid', 'password', 'floating', 'secure',
                        'address', 'auth']:
            print("Attribute %s unknown" % attr)
            raise SystemExit(1)
        attr_d[attr] = val
    return attr_d


class Client(Bcfg2.Server.Admin.MetadataCore):
    """ Create, delete, or list client entries """
    __usage__ = "[options] [add|del|update|list] [attr=val]"
    __plugin_whitelist__ = ["Metadata"]

    def __call__(self, args):
        if len(args) == 0:
            self.errExit("No argument specified.\n"
                         "Usage: %s" % self.__usage__)
        if args[0] == 'add':
            try:
                self.metadata.add_client(args[1], get_attribs(args))
            except MetadataConsistencyError:
                self.errExit("Error adding client: %s" % sys.exc_info()[1])
        elif args[0] in ['update', 'up']:
            try:
                self.metadata.update_client(args[1], get_attribs(args))
            except MetadataConsistencyError:
                self.errExit("Error updating client: %s" % sys.exc_info()[1])
        elif args[0] in ['delete', 'remove', 'del', 'rm']:
            try:
                self.metadata.remove_client(args[1])
            except MetadataConsistencyError:
                self.errExit("Error deleting client: %s" %
                             sys.exc_info()[1])
        elif args[0] in ['list', 'ls']:
            for client in self.metadata.list_clients():
                print(client)
        else:
            self.errExit("No command specified")