summaryrefslogtreecommitdiffstats
path: root/bin/ksl
blob: 3354288a6878798f86901187948c091309c44bd7 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python

import sys

import cli.app
import cli.log

from keystonelight import client


DEFAULT_PARAMS = (
    (('--config',), {'dest': 'configfile',
                     'action': 'store',
                     'default': './etc/default.conf'}),
    (('--url',), {'dest': 'url',
                  'action': 'store',
                  'default': 'http://localhost:5000'}),
    (('--token',), {'dest': 'token', 'action': 'store'}),
    )


class BaseApp(cli.log.LoggingApp):
  def __init__(self, *args, **kw):
    kw.setdefault('name', self.__class__.__name__.lower())
    super(BaseApp, self).__init__(*args, **kw)

  def add_default_params(self):
    for args, kw in DEFAULT_PARAMS:
      self.add_param(*args, **kw)

  def _parse_keyvalues(self, args):
    kv = {}
    for x in args:
      key, value = x.split('=', 1)
      # make lists if there are multiple values
      if key in kv:
        if type(kv) is type(tuple()):
          kv[key] = kv[key] + (value,)
        else:
          kv[key] = (kv[key], value)
      else:
        kv[key] = value
    return kv


class LoadData(BaseApp):
  def __init__(self, *args, **kw):
    super(LoadData, self).__init__(*args, **kw)
    self.add_default_params()
    self.add_param('fixture', nargs='+')

  def main(self):
    """Given some fixtures, create the appropriate data in Keystone Light."""
    pass


class CrudCommands(BaseApp):
  ACTION_MAP = {}

  def __init__(self, *args, **kw):
    super(CrudCommands, self).__init__(*args, **kw)
    self.add_default_params()
    self.add_param('action')
    self.add_param('keyvalues', nargs='+')

  def main(self):
    """Given some keyvalues create the appropriate data in Keystone Light."""
    c = client.HttpClient(self.params.url, token=self.params.token)
    action_name = self.ACTION_MAP[self.params.action]
    kv = self._parse_keyvalues(self.params.keyvalues)
    resp = getattr(c, action_name)(**kv)
    print resp


class UserCommands(CrudCommands):
  ACTION_MAP = {'add': 'create_user',
                'create': 'create_user',
                }


class TenantCommands(CrudCommands):
  ACTION_MAP = {'add': 'create_tenant',
                'create': 'create_tenant',
                }


class ExtrasCommands(CrudCommands):
  ACTION_MAP = {'add': 'create_extras',
                'create': 'create_extras',
                }


class Auth(BaseApp):
  def __init__(self, *args, **kw):
    super(Auth, self).__init__(*args, **kw)
    self.add_default_params()
    self.add_param('keyvalues', nargs='+')

  def main(self):
    """Attempt to authenticate against the Keystone Light API."""
    c = client.HttpClient(self.params.url, token=self.params.token)
    kv = self._parse_keyvalues(self.params.keyvalues)
    resp = c.authenticate(**kv)
    print resp


CMDS = {'loaddata': LoadData,
        'user': UserCommands,
        'tenant': TenantCommands,
        'extras': ExtrasCommands,
        'auth': Auth,
        }


if __name__ == '__main__':
  if not len(sys.argv) > 1:
    print 'try one of:', ' '.join(CMDS.keys())
    sys.exit(1)

  cmd = sys.argv[1]
  if cmd in CMDS:
    CMDS[cmd](argv=(sys.argv[:1] + sys.argv[2:])).run()