Debian Codegraph @

adjutantclient/osc/v1/tokens.py

raw
1 # Copyright (c) 2016 Catalyst IT Ltd.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 import json
16 import logging
17
18 from osc_lib.command import command
19 from osc_lib.i18n import _
20
21 from adjutantclient import client as adjutant_client
22
23 LOG = logging.getLogger(__name__)
24
25
26 def _list_tokens(client, filters={}):
27 tokens = client.tokens.list(filters=filters)
28
29 headers = ['Token', 'Task', 'Expires on', 'Created on']
30 rows = [[token.token, token.task, token.expires,
31 token.created_on] for token in tokens]
32
33 return headers, rows
34
35
36 class TokenList(command.Lister):
37 """Lists adjutant tokens. """
38
39 def get_parser(self, prog_name):
40 parser = super(TokenList, self).get_parser(prog_name)
41
42 parser.add_argument(
43 '--filters', metavar='<filters>',
44 required=False,
45 help=_('JSON containing filters for tokens.'),
46 default={})
47
48 return parser
49
50 def take_action(self, parsed_args):
51 client = self.app.client_manager.admin_logic
52 return _list_tokens(client, parsed_args.filters)
53
54
55 class TokenShow(command.ShowOne):
56 """Show details of one token."""
57
58 def get_parser(self, prog_name):
59 parser = super(TokenShow, self).get_parser(prog_name)
60
61 parser.add_argument(
62 'token', metavar='<token_id>',
63 help=_("The token."))
64 parser.add_argument(
65 '--bypass-url', metavar='<bypass-url>', default=None,
66 help=_('Bypasss URL for unauthenticated access to the endpoint.'))
67 return parser
68
69 def take_action(self, parsed_args):
70 if not parsed_args.bypass_url:
71 self.app.client_manager._auth_required = True
72 self.app.client_manager.setup_auth()
73 client = self.app.client_manager.admin_logic
74 else:
75 client = adjutant_client.Client("1", parsed_args.bypass_url)
76 token = client.tokens.get(parsed_args.token)
77 return zip(*(token.to_dict()).items())
78
79
80 class TokenSubmit(command.Command):
81 """Submit token data."""
82 def get_parser(self, prog_name):
83 parser = super(TokenSubmit, self).get_parser(prog_name)
84
85 parser.add_argument(
86 'token', metavar='<token_id>', help=_('The token.'))
87 parser.add_argument(
88 'data', metavar='<token_data>',
89 help=_('Submission data for the token. Must be valid json.'))
90 return parser
91
92 def take_action(self, parsed_args):
93 client = self.app.client_manager.admin_logic
94 resp = client.tokens.submit(
95 parsed_args.token, json.loads(parsed_args.data))
96 print('Success', ' '.join(resp.notes))
97
98
99 class TokenClear(command.Lister):
100 """Clear Expired tokens, admin only."""
101
102 def take_action(self, parsed_args):
103 client = self.app.client_manager.admin_logic
104 resp = client.tokens.clear_expired()
105 print('Success. ' + ' '.join(resp.json()['notes']))
106 return _list_tokens(client)