|
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 logging |
|
17
|
|
|
|
|
18
|
|
|
from st2client import models |
|
19
|
|
|
from st2client.commands import resource |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
LOG = logging.getLogger(__name__) |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
class PolicyTypeBranch(resource.ResourceBranch): |
|
26
|
|
|
|
|
27
|
|
|
def __init__(self, description, app, subparsers, parent_parser=None): |
|
28
|
|
|
super(PolicyTypeBranch, self).__init__( |
|
29
|
|
|
models.PolicyType, description, app, subparsers, |
|
30
|
|
|
parent_parser=parent_parser, |
|
31
|
|
|
read_only=True, |
|
32
|
|
|
commands={ |
|
33
|
|
|
'list': PolicyTypeListCommand, |
|
34
|
|
|
'get': PolicyTypeGetCommand |
|
35
|
|
|
}) |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
class PolicyTypeListCommand(resource.ResourceListCommand): |
|
39
|
|
|
display_attributes = ['id', 'resource_type', 'name', 'description'] |
|
40
|
|
|
|
|
41
|
|
|
def __init__(self, resource, *args, **kwargs): |
|
|
|
|
|
|
42
|
|
|
super(PolicyTypeListCommand, self).__init__(resource, *args, **kwargs) |
|
43
|
|
|
|
|
44
|
|
|
self.parser.add_argument('-r', '--resource-type', type=str, dest='resource_type', |
|
45
|
|
|
help='Return policy types for the resource type.') |
|
46
|
|
|
|
|
47
|
|
|
@resource.add_auth_token_to_kwargs_from_cli |
|
48
|
|
|
def run(self, args, **kwargs): |
|
49
|
|
|
if args.resource_type: |
|
50
|
|
|
filters = {'resource_type': args.resource_type} |
|
51
|
|
|
filters.update(**kwargs) |
|
52
|
|
|
return self.manager.query(**filters) |
|
53
|
|
|
else: |
|
54
|
|
|
return self.manager.get_all(**kwargs) |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
class PolicyTypeGetCommand(resource.ResourceGetCommand): |
|
58
|
|
|
pk_argument_name = 'ref_or_id' |
|
59
|
|
|
|
|
60
|
|
|
def get_resource(self, ref_or_id, **kwargs): |
|
61
|
|
|
return self.get_resource_by_ref_or_id(ref_or_id=ref_or_id, **kwargs) |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
class PolicyBranch(resource.ResourceBranch): |
|
65
|
|
|
|
|
66
|
|
|
def __init__(self, description, app, subparsers, parent_parser=None): |
|
67
|
|
|
super(PolicyBranch, self).__init__( |
|
68
|
|
|
models.Policy, description, app, subparsers, |
|
69
|
|
|
parent_parser=parent_parser, |
|
70
|
|
|
commands={ |
|
71
|
|
|
'list': PolicyListCommand, |
|
72
|
|
|
'get': PolicyGetCommand, |
|
73
|
|
|
'update': PolicyUpdateCommand, |
|
74
|
|
|
'delete': PolicyDeleteCommand |
|
75
|
|
|
}) |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
class PolicyListCommand(resource.ContentPackResourceListCommand): |
|
79
|
|
|
display_attributes = ['ref', 'resource_ref', 'policy_type'] |
|
80
|
|
|
|
|
81
|
|
|
def __init__(self, resource, *args, **kwargs): |
|
|
|
|
|
|
82
|
|
|
super(PolicyListCommand, self).__init__(resource, *args, **kwargs) |
|
83
|
|
|
|
|
84
|
|
|
self.parser.add_argument('-r', '--resource-ref', type=str, dest='resource_ref', |
|
85
|
|
|
help='Return policies for the resource ref.') |
|
86
|
|
|
self.parser.add_argument('-pt', '--policy-type', type=str, dest='policy_type', |
|
87
|
|
|
help='Return policies of the policy type.') |
|
88
|
|
|
|
|
89
|
|
|
@resource.add_auth_token_to_kwargs_from_cli |
|
90
|
|
|
def run(self, args, **kwargs): |
|
91
|
|
|
if args.resource_ref or args.policy_type: |
|
92
|
|
|
filters = {} |
|
93
|
|
|
|
|
94
|
|
|
if args.resource_ref: |
|
95
|
|
|
filters['resource_ref'] = args.resource_ref |
|
96
|
|
|
|
|
97
|
|
|
if args.policy_type: |
|
98
|
|
|
filters['policy_type'] = args.policy_type |
|
99
|
|
|
|
|
100
|
|
|
filters.update(**kwargs) |
|
101
|
|
|
|
|
102
|
|
|
return self.manager.query(**filters) |
|
103
|
|
|
else: |
|
104
|
|
|
return self.manager.get_all(**kwargs) |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
class PolicyGetCommand(resource.ContentPackResourceGetCommand): |
|
108
|
|
|
display_attributes = ['all'] |
|
109
|
|
|
attribute_display_order = ['id', 'ref', 'pack', 'name', 'description', |
|
110
|
|
|
'enabled', 'resource_ref', 'policy_type', |
|
111
|
|
|
'parameters'] |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
class PolicyUpdateCommand(resource.ContentPackResourceUpdateCommand): |
|
115
|
|
|
pass |
|
116
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
class PolicyDeleteCommand(resource.ContentPackResourceDeleteCommand): |
|
119
|
|
|
pass |
|
120
|
|
|
|
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: