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
|
|
|
from st2client.models import Pack |
17
|
|
|
from st2client.commands import resource |
18
|
|
|
from st2client.commands.noop import NoopCommand |
19
|
|
|
from st2client.formatters import table |
20
|
|
|
from st2client.exceptions.operations import OperationFailureException |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
class PackBranch(resource.ResourceBranch): |
24
|
|
|
def __init__(self, description, app, subparsers, parent_parser=None): |
25
|
|
|
super(PackBranch, self).__init__( |
26
|
|
|
Pack, description, app, subparsers, |
27
|
|
|
parent_parser=parent_parser, |
28
|
|
|
read_only=True, |
29
|
|
|
commands={ |
30
|
|
|
'list': PackListCommand, |
31
|
|
|
'get': NoopCommand |
32
|
|
|
}) |
33
|
|
|
|
34
|
|
|
self.commands['register'] = PackRegisterCommand(self.resource, self.app, self.subparsers) |
35
|
|
|
self.commands['create'] = PackCreateCommand(self.resource, self.app, self.subparsers) |
36
|
|
|
self.commands['install'] = PackInstallCommand(self.resource, self.app, self.subparsers) |
37
|
|
|
self.commands['remove'] = PackRemoveCommand(self.resource, self.app, self.subparsers) |
38
|
|
|
self.commands['search'] = PackSearchCommand(self.resource, self.app, self.subparsers) |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
class PackResourceCommand(resource.ResourceCommand): |
42
|
|
|
def run_and_print(self, args, **kwargs): |
43
|
|
|
try: |
44
|
|
|
instance = self.run(args, **kwargs) |
45
|
|
|
if not instance: |
46
|
|
|
raise Exception('Server did not create instance.') |
47
|
|
|
self.print_output(instance, table.PropertyValueTable, |
48
|
|
|
attributes=['all'], json=args.json, yaml=args.yaml) |
49
|
|
|
except Exception as e: |
50
|
|
|
message = e.message or str(e) |
51
|
|
|
print('ERROR: %s' % (message)) |
52
|
|
|
raise OperationFailureException(message) |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
class PackListCommand(resource.ResourceListCommand): |
56
|
|
|
display_attributes = ['name', 'description', 'version', 'author'] |
57
|
|
|
attribute_display_order = ['name', 'description', 'version', 'author'] |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
class PackInstallCommand(PackResourceCommand): |
61
|
|
|
def __init__(self, resource, *args, **kwargs): |
|
|
|
|
62
|
|
|
super(PackInstallCommand, self).__init__(resource, 'install', |
63
|
|
|
'Install a new %s.' % resource.get_display_name().lower(), |
64
|
|
|
*args, **kwargs) |
65
|
|
|
|
66
|
|
|
self.parser.add_argument('name', |
67
|
|
|
help='Name of the %s to install.' % |
68
|
|
|
resource.get_display_name().lower()) |
69
|
|
|
|
70
|
|
|
@resource.add_auth_token_to_kwargs_from_cli |
71
|
|
|
def run(self, args, **kwargs): |
72
|
|
|
return self.manager.install(args.name, **kwargs) |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
class PackRemoveCommand(PackResourceCommand): |
76
|
|
|
def __init__(self, resource, *args, **kwargs): |
|
|
|
|
77
|
|
|
super(PackRemoveCommand, self).__init__(resource, 'remove', |
78
|
|
|
'Remove a %s.' % resource.get_display_name().lower(), |
79
|
|
|
*args, **kwargs) |
80
|
|
|
|
81
|
|
|
self.parser.add_argument('name', |
82
|
|
|
help='Name of the %s to remove.' % |
83
|
|
|
resource.get_display_name().lower()) |
84
|
|
|
|
85
|
|
|
@resource.add_auth_token_to_kwargs_from_cli |
86
|
|
|
def run(self, args, **kwargs): |
87
|
|
|
return self.manager.remove(args.name, **kwargs) |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
class PackCreateCommand(PackResourceCommand): |
91
|
|
|
def __init__(self, resource, *args, **kwargs): |
|
|
|
|
92
|
|
|
super(PackCreateCommand, self).__init__(resource, 'create', |
93
|
|
|
'Create a template for a new %s.' % resource.get_display_name().lower(), |
94
|
|
|
*args, **kwargs) |
95
|
|
|
|
96
|
|
|
self.parser.add_argument('name', |
97
|
|
|
help='Name of the %s to create.' % |
98
|
|
|
resource.get_display_name().lower()) |
99
|
|
|
|
100
|
|
|
@resource.add_auth_token_to_kwargs_from_cli |
101
|
|
|
def run(self, args, **kwargs): |
102
|
|
|
return self.manager.create(args.name, **kwargs) |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
class PackRegisterCommand(PackResourceCommand): |
106
|
|
|
def __init__(self, resource, *args, **kwargs): |
|
|
|
|
107
|
|
|
super(PackRegisterCommand, self).__init__(resource, 'register', |
108
|
|
|
'Register a %s: sync all file changes with DB.' % resource.get_display_name().lower(), |
109
|
|
|
*args, **kwargs) |
110
|
|
|
|
111
|
|
|
self.parser.add_argument('name', |
112
|
|
|
help='Name of the %s to register.' % |
113
|
|
|
resource.get_display_name().lower()) |
114
|
|
|
|
115
|
|
|
@resource.add_auth_token_to_kwargs_from_cli |
116
|
|
|
def run(self, args, **kwargs): |
117
|
|
|
return self.manager.register(args.name, **kwargs) |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
class PackSearchCommand(PackResourceCommand): |
121
|
|
|
def __init__(self, resource, *args, **kwargs): |
|
|
|
|
122
|
|
|
super(PackSearchCommand, self).__init__(resource, 'search', |
123
|
|
|
'Search for a %s in the directory.' % resource.get_display_name().lower(), |
124
|
|
|
*args, **kwargs) |
125
|
|
|
|
126
|
|
|
self.parser.add_argument('query', |
127
|
|
|
help='Search query.') |
128
|
|
|
|
129
|
|
|
@resource.add_auth_token_to_kwargs_from_cli |
130
|
|
|
def run(self, args, **kwargs): |
131
|
|
|
return self.manager.search(args.query, **kwargs) |
132
|
|
|
|
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: