|
1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
|
2
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
|
3
|
|
|
# this work for additional information regarding copyright ownership. |
|
4
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
|
5
|
|
|
# (the "License"); you may not use this file except in compliance with |
|
6
|
|
|
# the License. You may obtain a copy of the License at |
|
7
|
|
|
# |
|
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
|
9
|
|
|
# |
|
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
|
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
|
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13
|
|
|
# See the License for the specific language governing permissions and |
|
14
|
|
|
# limitations under the License. |
|
15
|
|
|
|
|
16
|
|
|
import json |
|
17
|
|
|
import logging |
|
18
|
|
|
|
|
19
|
|
|
from st2client.commands import resource |
|
20
|
|
|
from st2client.formatters import table |
|
21
|
|
|
from st2client.models.inquiry import Inquiry |
|
22
|
|
|
from st2client.utils.interactive import InteractiveForm |
|
23
|
|
|
|
|
24
|
|
|
LOG = logging.getLogger(__name__) |
|
25
|
|
|
|
|
26
|
|
|
DEFAULT_SCOPE = 'system' |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
class InquiryBranch(resource.ResourceBranch): |
|
30
|
|
|
|
|
31
|
|
|
def __init__(self, description, app, subparsers, parent_parser=None): |
|
32
|
|
|
|
|
33
|
|
|
super(InquiryBranch, self).__init__( |
|
34
|
|
|
Inquiry, description, app, subparsers, |
|
35
|
|
|
parent_parser=parent_parser, read_only=True, |
|
36
|
|
|
commands={'list': InquiryListCommand, |
|
37
|
|
|
'get': InquiryGetCommand}) |
|
38
|
|
|
|
|
39
|
|
|
# Register extended commands |
|
40
|
|
|
self.commands['respond'] = InquiryRespondCommand( |
|
41
|
|
|
self.resource, self.app, self.subparsers) |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
class InquiryListCommand(resource.ResourceCommand): |
|
45
|
|
|
|
|
46
|
|
|
# Omitting "schema" and "response", as it doesn't really show up in a table well. |
|
47
|
|
|
# The user can drill into a specific Inquiry to get this |
|
48
|
|
|
display_attributes = [ |
|
49
|
|
|
'id', |
|
50
|
|
|
'roles', |
|
51
|
|
|
'users', |
|
52
|
|
|
'route', |
|
53
|
|
|
'ttl' |
|
54
|
|
|
] |
|
55
|
|
|
|
|
56
|
|
|
def __init__(self, resource, *args, **kwargs): |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
self.default_limit = 20 |
|
59
|
|
|
|
|
60
|
|
|
super(InquiryListCommand, self).__init__( |
|
61
|
|
|
resource, 'list', 'Get the list of the %s most recent %s.' % |
|
62
|
|
|
(self.default_limit, resource.get_plural_display_name().lower()), |
|
63
|
|
|
*args, **kwargs) |
|
64
|
|
|
|
|
65
|
|
|
self.resource_name = resource.get_plural_display_name().lower() |
|
66
|
|
|
self.parser.add_argument('-n', '--last', type=int, dest='last', |
|
67
|
|
|
default=self.default_limit, |
|
68
|
|
|
help=('List N most recent %s.' % self.resource_name)) |
|
69
|
|
|
|
|
70
|
|
|
# Display options |
|
71
|
|
|
self.parser.add_argument('-a', '--attr', nargs='+', |
|
72
|
|
|
default=self.display_attributes, |
|
73
|
|
|
help=('List of attributes to include in the ' |
|
74
|
|
|
'output. "all" will return all ' |
|
75
|
|
|
'attributes.')) |
|
76
|
|
|
self.parser.add_argument('-w', '--width', nargs='+', type=int, |
|
77
|
|
|
default=None, |
|
78
|
|
|
help=('Set the width of columns in output.')) |
|
79
|
|
|
|
|
80
|
|
|
@resource.add_auth_token_to_kwargs_from_cli |
|
81
|
|
|
def run(self, args, **kwargs): |
|
82
|
|
|
return self.manager.query_with_count(limit=args.last, **kwargs) |
|
83
|
|
|
|
|
84
|
|
|
def run_and_print(self, args, **kwargs): |
|
85
|
|
|
instances, count = self.run(args, **kwargs) |
|
86
|
|
|
|
|
87
|
|
|
self.print_output(reversed(instances), table.MultiColumnTable, |
|
88
|
|
|
attributes=args.attr, widths=args.width, |
|
89
|
|
|
json=args.json, |
|
90
|
|
|
yaml=args.yaml) |
|
91
|
|
|
if args.last >= self.default_limit and count and int(count) > args.last: |
|
92
|
|
|
table.SingleRowTable.note_box(self.resource_name, args.last) |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
class InquiryGetCommand(resource.ResourceGetCommand): |
|
96
|
|
|
pk_argument_name = 'id' |
|
97
|
|
|
display_attributes = ['id', 'roles', 'users', 'route', 'ttl', 'schema'] |
|
98
|
|
|
|
|
99
|
|
|
def __init__(self, kv_resource, *args, **kwargs): |
|
100
|
|
|
super(InquiryGetCommand, self).__init__(kv_resource, *args, **kwargs) |
|
101
|
|
|
|
|
102
|
|
|
@resource.add_auth_token_to_kwargs_from_cli |
|
103
|
|
|
def run(self, args, **kwargs): |
|
104
|
|
|
resource_name = getattr(args, self.pk_argument_name, None) |
|
105
|
|
|
return self.get_resource_by_id(id=resource_name, **kwargs) |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
class InquiryRespondCommand(resource.ResourceCommand): |
|
109
|
|
|
display_attributes = ['id', 'response'] |
|
110
|
|
|
|
|
111
|
|
|
def __init__(self, resource, *args, **kwargs): |
|
|
|
|
|
|
112
|
|
|
super(InquiryRespondCommand, self).__init__( |
|
113
|
|
|
resource, 'respond', |
|
114
|
|
|
'Respond to an %s.' % resource.get_display_name().lower(), |
|
115
|
|
|
*args, **kwargs |
|
116
|
|
|
) |
|
117
|
|
|
|
|
118
|
|
|
self.parser.add_argument('id', |
|
119
|
|
|
metavar='id', |
|
120
|
|
|
help='Inquiry ID') |
|
121
|
|
|
self.parser.add_argument('-r', '--response', type=str, dest='response', |
|
122
|
|
|
default=None, |
|
123
|
|
|
help=('Entire response payload as JSON string ' |
|
124
|
|
|
'(bypass interactive mode)')) |
|
125
|
|
|
|
|
126
|
|
|
@resource.add_auth_token_to_kwargs_from_cli |
|
127
|
|
|
def run(self, args, **kwargs): |
|
128
|
|
|
instance = Inquiry() |
|
129
|
|
|
instance.id = args.id |
|
130
|
|
|
if args.response: |
|
131
|
|
|
instance.response = json.loads(args.response) |
|
132
|
|
|
else: |
|
133
|
|
|
inquiry = self.get_resource_by_id(id=args.id, **kwargs) |
|
134
|
|
|
response = InteractiveForm( |
|
135
|
|
|
inquiry.schema.get('properties')).initiate_dialog() |
|
136
|
|
|
instance.response = response |
|
137
|
|
|
|
|
138
|
|
|
return self.manager.update(instance, **kwargs) |
|
139
|
|
|
|
|
140
|
|
|
def run_and_print(self, args, **kwargs): |
|
141
|
|
|
instance = self.run(args, **kwargs) |
|
142
|
|
|
print("\n Response accepted. Successful response data to follow...") |
|
143
|
|
|
self.print_output(instance, table.PropertyValueTable, |
|
144
|
|
|
attributes=self.display_attributes, json=args.json, |
|
145
|
|
|
yaml=args.yaml) |
|
146
|
|
|
|
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: