2 # Copyright 2008 Google Inc. All Rights Reserved.
5 The acl module contains the objects and methods used to
6 manage ACLs in Autotest.
9 add: adds acl(s), or users or hosts to an ACL
10 remove: deletes acl(s), or users or hosts from an ACL
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.
21 from autotest_lib
.cli
import topic_common
, action_common
24 class acl(topic_common
.atest
):
26 atest acl [create|delete|list|add|remove] <options>"""
27 usage_action
= '[create|delete|list|add|remove]'
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',
41 self
.topic_parse_info
= topic_common
.item_parse_info(
42 attribute_name
='acls',
43 filename_option
='alist',
52 """Just here to get the atest logic working.
53 Usage is set by its parent"""
57 class acl_list(action_common
.atest_list
, acl
):
58 """atest acl list [--verbose]
59 [--user <users>|--mach <machine>|--alist <file>] [<acls>]"""
61 super(acl_list
, self
).__init
__()
63 self
.parser
.add_option('-u', '--user',
64 help='List ACLs containing USER',
67 self
.parser
.add_option('-m', '--machine',
68 help='List ACLs containing MACHINE',
74 user_info
= topic_common
.item_parse_info(attribute_name
='users',
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
,
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,'
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>')
93 self
.users
= self
.users
[0]
98 self
.hosts
= self
.hosts
[0]
101 return (options
, leftover
)
108 filters
['name__in'] = self
.acls
109 check_results
['name__in'] = 'name'
112 filters
['users__login'] = self
.users
113 check_results
['users__login'] = None
116 filters
['hosts__hostname'] = self
.hosts
117 check_results
['hosts__hostname'] = None
119 return super(acl_list
,
120 self
).execute(op
='get_acl_groups',
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')
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>"""
140 super(acl_create
, self
).__init
__()
141 self
.parser
.add_option('-d', '--desc',
142 help='Creates the ACL with the DESCRIPTION',
144 self
.parser
.remove_option('--alist')
148 (options
, leftover
) = super(acl_create
, self
).parse(req_items
='acls')
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>"""
167 class acl_add_or_remove(acl
):
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
,
178 self
.parser
.add_option('-U', '--ulist',
179 help='File containing users to %s %s '
180 'the ACL' % lower_words
,
182 metavar
='USER_FLIST')
183 self
.parser
.add_option('-m', '--machine',
184 help='%s MACHINE(s) %s the ACL' % words
,
187 self
.parser
.add_option('-M', '--mlist',
188 help='File containing machines to %s %s '
189 'the ACL' % lower_words
,
191 metavar
='MACHINE_FLIST')
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
],
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>]"""
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>]"""