| 
									
										
										
										
											2015-11-05 12:30:06 -08:00
										 |  |  | /* | 
					
						
							|  |  |  |  * This Source Code Form is subject to the terms of the Mozilla Public | 
					
						
							|  |  |  |  * License, v. 2.0. If a copy of the MPL was not distributed with this | 
					
						
							|  |  |  |  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  |  * Copyright 2015 Joyent, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * `triton rbac policies ...` | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var tabula = require('tabula'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var common = require('../common'); | 
					
						
							|  |  |  | var errors = require('../errors'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // columns default without -o
 | 
					
						
							| 
									
										
										
										
											2015-11-21 12:41:16 -08:00
										 |  |  | var columnsDefault = 'name,description,nrules'; | 
					
						
							| 
									
										
										
										
											2015-11-05 12:30:06 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // columns default with -l
 | 
					
						
							| 
									
										
										
										
											2015-11-21 12:41:16 -08:00
										 |  |  | var columnsDefaultLong = 'id,name,description,rules'; | 
					
						
							| 
									
										
										
										
											2015-11-05 12:30:06 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // sort default with -s
 | 
					
						
							|  |  |  | var sortDefault = 'name'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function do_policies(subcmd, opts, args, cb) { | 
					
						
							|  |  |  |     if (opts.help) { | 
					
						
							|  |  |  |         this.do_help('help', {}, [subcmd], cb); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } else if (args.length !== 0) { | 
					
						
							|  |  |  |         cb(new errors.UsageError('invalid args: ' + args)); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     var columns = columnsDefault; | 
					
						
							|  |  |  |     if (opts.o) { | 
					
						
							|  |  |  |         columns = opts.o; | 
					
						
							|  |  |  |     } else if (opts.long) { | 
					
						
							|  |  |  |         columns = columnsDefaultLong; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     columns = columns.split(','); | 
					
						
							|  |  |  |     var sort = opts.s.split(','); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     this.top.tritonapi.cloudapi.listPolicies(function (err, policies) { | 
					
						
							|  |  |  |         if (err) { | 
					
						
							|  |  |  |             cb(err); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (opts.json) { | 
					
						
							|  |  |  |             common.jsonStream(policies); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             // Add some convenience fields
 | 
					
						
							| 
									
										
										
										
											2015-11-05 15:13:14 -08:00
										 |  |  |             for (var i = 0; i < policies.length; i++) { | 
					
						
							| 
									
										
										
										
											2015-11-05 12:30:06 -08:00
										 |  |  |                 var role = policies[i]; | 
					
						
							|  |  |  |                 role.nrules = role.rules.length; | 
					
						
							|  |  |  |                 role.rules = role.rules.sort().join('; '); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             tabula(policies, { | 
					
						
							|  |  |  |                 skipHeader: opts.H, | 
					
						
							|  |  |  |                 columns: columns, | 
					
						
							|  |  |  |                 sort: sort | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         cb(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | do_policies.options = [ | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         names: ['help', 'h'], | 
					
						
							|  |  |  |         type: 'bool', | 
					
						
							|  |  |  |         help: 'Show this help.' | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | ].concat(common.getCliTableOptions({ | 
					
						
							|  |  |  |     includeLong: true, | 
					
						
							|  |  |  |     sortDefault: sortDefault | 
					
						
							|  |  |  | })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | do_policies.help = ( | 
					
						
							|  |  |  |     /* BEGIN JSSTYLED */ | 
					
						
							|  |  |  |     'List RBAC policies.\n' + | 
					
						
							|  |  |  |     '\n' + | 
					
						
							|  |  |  |     'Usage:\n' + | 
					
						
							|  |  |  |     '    {{name}} policies [<options>]\n' + | 
					
						
							|  |  |  |     '\n' + | 
					
						
							|  |  |  |     '{{options}}' + | 
					
						
							|  |  |  |     '\n' + | 
					
						
							|  |  |  |     'Fields (most are self explanatory, the client adds some for convenience):\n' + | 
					
						
							|  |  |  |     '    nrules             The number of rules in this policy.\n' | 
					
						
							|  |  |  |     /* END JSSTYLED */ | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = do_policies; |