Package ganeti :: Package client :: Module gnt_filter
[hide private]
[frames] | no frames]

Source Code for Module ganeti.client.gnt_filter

  1  # 
  2  # 
  3   
  4  # Copyright (C) 2014 Google Inc. 
  5  # All rights reserved. 
  6  # 
  7  # Redistribution and use in source and binary forms, with or without 
  8  # modification, are permitted provided that the following conditions are 
  9  # met: 
 10  # 
 11  # 1. Redistributions of source code must retain the above copyright notice, 
 12  # this list of conditions and the following disclaimer. 
 13  # 
 14  # 2. Redistributions in binary form must reproduce the above copyright 
 15  # notice, this list of conditions and the following disclaimer in the 
 16  # documentation and/or other materials provided with the distribution. 
 17  # 
 18  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 
 19  # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
 20  # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 21  # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
 22  # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 23  # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 24  # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 25  # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 26  # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 27  # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 28  # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 29   
 30  """Job filter rule commands""" 
 31   
 32  # pylint: disable=W0401,W0614 
 33  # W0401: Wildcard import ganeti.cli 
 34  # W0614: Unused import %s from wildcard import (since we need cli) 
 35   
 36  from ganeti.cli import * 
 37  from ganeti import constants 
 38  from ganeti import utils 
 39   
 40   
 41  #: default list of fields for L{ListFilters} 
 42  _LIST_DEF_FIELDS = ["uuid", "watermark", "priority", 
 43                      "predicates", "action", "reason_trail"] 
 44   
 45   
46 -def AddFilter(opts, args):
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 # Prints the UUID of the replaced/created filter
69 70
71 -def ListFilters(opts, args):
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
88 -def ListFilterFields(opts, args):
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
103 -def ReplaceFilter(opts, args):
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 # Prints the UUID of the replaced/created filter 131 return 0
132 133
134 -def ShowFilter(_, args):
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
161 -def DeleteFilter(_, args):
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
233 -def Main():
234 return GenericMain(commands)
235