virt.virt_utils: Get rid of create_report function
[autotest-zwu.git] / cli / acl.py
blobd6f5baaa351625a57ef988f8a2b03ab1e594a7d3
2 # Copyright 2008 Google Inc. All Rights Reserved.
4 """
5 The acl module contains the objects and methods used to
6 manage ACLs in Autotest.
8 The valid actions are:
9 add: adds acl(s), or users or hosts to an ACL
10 remove: deletes acl(s), or users or hosts from an ACL
11 list: lists acl(s)
13 The common options are:
14 --alist / -A: file containing a list of ACLs
16 See topic_common.py for a High Level Design and Algorithm.
18 """
20 import os, sys
21 from autotest_lib.cli import topic_common, action_common
24 class acl(topic_common.atest):
25 """ACL class
26 atest acl [create|delete|list|add|remove] <options>"""
27 usage_action = '[create|delete|list|add|remove]'
28 topic = 'acl_group'
29 msg_topic = 'ACL'
30 msg_items = '<acls>'
32 def __init__(self):
33 """Add to the parser the options common to all the ACL actions"""
34 super(acl, self).__init__()
35 self.parser.add_option('-A', '--alist',
36 help='File listing the ACLs',
37 type='string',
38 default=None,
39 metavar='ACL_FLIST')
41 self.topic_parse_info = topic_common.item_parse_info(
42 attribute_name='acls',
43 filename_option='alist',
44 use_leftover=True)
47 def get_items(self):
48 return self.acls
51 class acl_help(acl):
52 """Just here to get the atest logic working.
53 Usage is set by its parent"""
54 pass
57 class acl_list(action_common.atest_list, acl):
58 """atest acl list [--verbose]
59 [--user <users>|--mach <machine>|--alist <file>] [<acls>]"""
60 def __init__(self):
61 super(acl_list, self).__init__()
63 self.parser.add_option('-u', '--user',
64 help='List ACLs containing USER',
65 type='string',
66 metavar='USER')
67 self.parser.add_option('-m', '--machine',
68 help='List ACLs containing MACHINE',
69 type='string',
70 metavar='MACHINE')
73 def parse(self):
74 user_info = topic_common.item_parse_info(attribute_name='users',
75 inline_option='user')
76 host_info = topic_common.item_parse_info(attribute_name='hosts',
77 inline_option='machine')
79 (options, leftover) = super(acl_list, self).parse([user_info,
80 host_info])
82 if ((self.users and (self.hosts or self.acls)) or
83 (self.hosts and self.acls)):
84 self.invalid_syntax('Only specify one of --user,'
85 '--machine or ACL')
87 if len(self.users) > 1:
88 self.invalid_syntax('Only specify one <user>')
89 if len(self.hosts) > 1:
90 self.invalid_syntax('Only specify one <machine>')
92 try:
93 self.users = self.users[0]
94 except IndexError:
95 pass
97 try:
98 self.hosts = self.hosts[0]
99 except IndexError:
100 pass
101 return (options, leftover)
104 def execute(self):
105 filters = {}
106 check_results = {}
107 if self.acls:
108 filters['name__in'] = self.acls
109 check_results['name__in'] = 'name'
111 if self.users:
112 filters['users__login'] = self.users
113 check_results['users__login'] = None
115 if self.hosts:
116 filters['hosts__hostname'] = self.hosts
117 check_results['hosts__hostname'] = None
119 return super(acl_list,
120 self).execute(op='get_acl_groups',
121 filters=filters,
122 check_results=check_results)
125 def output(self, results):
126 # If an ACL was specified, always print its details
127 if self.acls or self.verbose:
128 sublist_keys=('hosts', 'users')
129 else:
130 sublist_keys=()
132 super(acl_list, self).output(results,
133 keys=('name', 'description'),
134 sublist_keys=sublist_keys)
137 class acl_create(action_common.atest_create, acl):
138 """atest acl create <acl> --desc <description>"""
139 def __init__(self):
140 super(acl_create, self).__init__()
141 self.parser.add_option('-d', '--desc',
142 help='Creates the ACL with the DESCRIPTION',
143 type='string')
144 self.parser.remove_option('--alist')
147 def parse(self):
148 (options, leftover) = super(acl_create, self).parse(req_items='acls')
150 if not options.desc:
151 self.invalid_syntax('Must specify a description to create an ACL.')
153 self.data_item_key = 'name'
154 self.data['description'] = options.desc
156 if len(self.acls) > 1:
157 self.invalid_syntax('Can only create one ACL at a time')
159 return (options, leftover)
162 class acl_delete(action_common.atest_delete, acl):
163 """atest acl delete [<acls> | --alist <file>"""
164 pass
167 class acl_add_or_remove(acl):
168 def __init__(self):
169 super(acl_add_or_remove, self).__init__()
170 # Get the appropriate help for adding or removing.
171 words = self.usage_words
172 lower_words = tuple(word.lower() for word in words)
174 self.parser.add_option('-u', '--user',
175 help='%s USER(s) %s the ACL' % words,
176 type='string',
177 metavar='USER')
178 self.parser.add_option('-U', '--ulist',
179 help='File containing users to %s %s '
180 'the ACL' % lower_words,
181 type='string',
182 metavar='USER_FLIST')
183 self.parser.add_option('-m', '--machine',
184 help='%s MACHINE(s) %s the ACL' % words,
185 type='string',
186 metavar='MACHINE')
187 self.parser.add_option('-M', '--mlist',
188 help='File containing machines to %s %s '
189 'the ACL' % lower_words,
190 type='string',
191 metavar='MACHINE_FLIST')
194 def parse(self):
195 user_info = topic_common.item_parse_info(attribute_name='users',
196 inline_option='user',
197 filename_option='ulist')
198 host_info = topic_common.item_parse_info(attribute_name='hosts',
199 inline_option='machine',
200 filename_option='mlist')
201 (options, leftover) = super(acl_add_or_remove,
202 self).parse([user_info, host_info],
203 req_items='acls')
205 if (not getattr(self, 'users', None) and
206 not getattr(self, 'hosts', None)):
207 self.invalid_syntax('Specify at least one USER or MACHINE')
209 return (options, leftover)
212 class acl_add(action_common.atest_add, acl_add_or_remove):
213 """atest acl add <acl> --user <user>|
214 --machine <machine>|--mlist <FILE>]"""
215 pass
218 class acl_remove(action_common.atest_remove, acl_add_or_remove):
219 """atest acl remove [<acls> | --alist <file>
220 --user <user> | --machine <machine> | --mlist <FILE>]"""
221 pass