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 """Job filter rule commands"""
31
32
33
34
35
36 from ganeti.cli import *
37 from ganeti import constants
38 from ganeti import utils
39
40
41
42 _LIST_DEF_FIELDS = ["uuid", "watermark", "priority",
43 "predicates", "action", "reason_trail"]
44
45
47 """Add a job filter rule.
48
49 @param opts: the command line options selected by the user
50 @type args: list
51 @param args: should be an empty list
52 @rtype: int
53 @return: the desired exit code
54
55 """
56 assert args == []
57
58 reason = []
59 if opts.reason:
60 reason = [(constants.OPCODE_REASON_SRC_USER,
61 opts.reason,
62 utils.EpochNano())]
63
64 cl = GetClient()
65 result = cl.ReplaceFilter(None, opts.priority, opts.predicates, opts.action,
66 reason)
67
68 print result
69
70
72 """List job filter rules and their properties.
73
74 @param opts: the command line options selected by the user
75 @type args: list
76 @param args: filters to list, or empty for all
77 @rtype: int
78 @return: the desired exit code
79
80 """
81 desired_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
82 cl = GetClient()
83 return GenericList(constants.QR_FILTER, desired_fields, args, None,
84 opts.separator, not opts.no_headers,
85 verbose=opts.verbose, cl=cl, namefield="uuid")
86
87
89 """List filter rule fields.
90
91 @param opts: the command line options selected by the user
92 @type args: list
93 @param args: fields to list, or empty for all
94 @rtype: int
95 @return: the desired exit code
96
97 """
98 cl = GetClient()
99 return GenericListFields(constants.QR_FILTER, args, opts.separator,
100 not opts.no_headers, cl=cl)
101
102
104 """Replaces a job filter rule with the given UUID, or creates it, if it
105 doesn't exist already.
106
107 @param opts: the command line options selected by the user
108 @type args: list
109 @param args: should contain only one element, the UUID of the filter
110
111 @rtype: int
112 @return: the desired exit code
113
114 """
115 (uuid,) = args
116
117 reason = []
118 if opts.reason:
119 reason = [(constants.OPCODE_REASON_SRC_USER,
120 opts.reason,
121 utils.EpochNano())]
122
123 cl = GetClient()
124 result = cl.ReplaceFilter(uuid,
125 priority=opts.priority,
126 predicates=opts.predicates,
127 action=opts.action,
128 reason=reason)
129
130 print result
131 return 0
132
133
135 """Show filter rule details.
136
137 @type args: list
138 @param args: should either be an empty list, in which case
139 we show information about all filters, or should contain
140 a list of filter UUIDs to be queried for information
141 @rtype: int
142 @return: the desired exit code
143
144 """
145 cl = GetClient()
146 result = cl.QueryFilters(fields=["uuid", "watermark", "priority",
147 "predicates", "action", "reason_trail"],
148 uuids=args)
149
150 for (uuid, watermark, priority, predicates, action, reason_trail) in result:
151 ToStdout("UUID: %s", uuid)
152 ToStdout(" Watermark: %s", watermark)
153 ToStdout(" Priority: %s", priority)
154 ToStdout(" Predicates: %s", predicates)
155 ToStdout(" Action: %s", action)
156 ToStdout(" Reason trail: %s", reason_trail)
157
158 return 0
159
160
162 """Remove a job filter rule.
163
164 @type args: list
165 @param args: a list of length 1 with the UUID of the filter to remove
166 @rtype: int
167 @return: the desired exit code
168
169 """
170 (uuid,) = args
171 cl = GetClient()
172 result = cl.DeleteFilter(uuid)
173 assert result is None
174 return 0
175
176
177 FILTER_PRIORITY_OPT = \
178 cli_option("--priority",
179 dest="priority", action="store", default=0, type="int",
180 help="Priority for filter processing")
181
182 FILTER_PREDICATES_OPT = \
183 cli_option("--predicates",
184 dest="predicates", action="store", default=[], type="json",
185 help="List of predicates in the Ganeti query language,"
186 " given as a JSON list.")
187
188 FILTER_ACTION_OPT = \
189 cli_option("--action",
190 dest="action", action="store", default="CONTINUE",
191 type="filteraction",
192 help="The effect of the filter. Can be one of 'ACCEPT',"
193 " 'PAUSE', 'REJECT', 'CONTINUE' and '[RATE_LIMIT, n]',"
194 " where n is a positive integer.")
195
196
197 commands = {
198 "add": (
199 AddFilter, ARGS_NONE,
200 [FILTER_PRIORITY_OPT, FILTER_PREDICATES_OPT, FILTER_ACTION_OPT],
201 "",
202 "Adds a new filter rule"),
203 "list": (
204 ListFilters, ARGS_MANY_FILTERS,
205 [NOHDR_OPT, SEP_OPT, FIELDS_OPT, VERBOSE_OPT],
206 "[<filter_uuid>...]",
207 "Lists the job filter rules. The available fields can be shown"
208 " using the \"list-fields\" command (see the man page for details)."
209 " The default list is (in order): %s." % utils.CommaJoin(_LIST_DEF_FIELDS)),
210 "list-fields": (
211 ListFilterFields, [ArgUnknown()],
212 [NOHDR_OPT, SEP_OPT],
213 "[fields...]",
214 "Lists all available fields for filters"),
215 "info": (
216 ShowFilter, ARGS_MANY_FILTERS,
217 [],
218 "[<filter_uuid>...]",
219 "Shows information about the filter(s)"),
220 "replace": (
221 ReplaceFilter, ARGS_ONE_FILTER,
222 [FILTER_PRIORITY_OPT, FILTER_PREDICATES_OPT, FILTER_ACTION_OPT],
223 "<filter_uuid>",
224 "Replaces a filter"),
225 "delete": (
226 DeleteFilter, ARGS_ONE_FILTER,
227 [],
228 "<filter_uuid>",
229 "Removes a filter"),
230 }
231
232
235