11"""Creates a user """
22# :license: MIT, see LICENSE for more details.
33
4- import click
54import json
6- import secrets
75import string
6+ import sys
7+
8+ import click
89
910import SoftLayer
10- from SoftLayer .CLI import columns as column_helper
1111from SoftLayer .CLI import environment
1212from SoftLayer .CLI import exceptions
1313from SoftLayer .CLI import formatting
14+ from SoftLayer .CLI import helpers
1415
1516
16- from pprint import pprint as pp
17-
1817@click .command ()
1918@click .argument ('username' )
20- @click .option ('--email' , '-e' , required = True ,
19+ @click .option ('--email' , '-e' , required = True ,
2120 help = "Email address for this user. Required for creation." )
2221@click .option ('--password' , '-p' , default = None , show_default = True ,
2322 help = "Password to set for this user. If no password is provided, user will be sent an email "
24- "to generate one, which expires in 24 hours. '-p generate' will create a password for you. "
25- "Passwords require 8+ characters, upper and lowercase, a number and a symbol." )
26- @click .option ('--from-user' , '-u' , default = None ,
23+ "to generate one, which expires in 24 hours. '-p generate' will create a password for you "
24+ "(Requires Python 3.6+). Passwords require 8+ characters, upper and lowercase, a number "
25+ "and a symbol." )
26+ @click .option ('--from-user' , '-u' , default = None ,
2727 help = "Base user to use as a template for creating this user. "
2828 "Will default to the user running this command. Information provided in --template "
2929 "supersedes this template." )
3434def cli (env , username , email , password , from_user , template , api_key ):
3535 """Creates a user Users.
3636
37- :Example: slcli user create my@email.com -e my@email.com -p generate -a -t '{"firstName": "Test", "lastName": "Testerson"}'
37+ :Example: slcli user create my@email.com -e my@email.com -p generate -a
38+ -t '{"firstName": "Test", "lastName": "Testerson"}'
39+
3840 Remember to set the permissions and access for this new user.
3941 """
4042
4143 mgr = SoftLayer .UserManager (env .client )
4244 user_mask = ("mask[id, firstName, lastName, email, companyName, address1, city, country, postalCode, "
43- "state, userStatusId, timezoneId]" )
45+ "state, userStatusId, timezoneId]" )
4446 from_user_id = None
4547 if from_user is None :
4648 user_template = mgr .get_current_user (objectmask = user_mask )
@@ -56,8 +58,8 @@ def cli(env, username, email, password, from_user, template, api_key):
5658 template_object = json .loads (template )
5759 for key in template_object :
5860 user_template [key ] = template_object [key ]
59- except json . decoder . JSONDecodeError as ex :
60- raise exceptions .ArgumentError ("Unable to parse --template. %s" % ex . msg )
61+ except ValueError as ex :
62+ raise exceptions .ArgumentError ("Unable to parse --template. %s" % ex )
6163
6264 user_template ['username' ] = username
6365 if password == 'generate' :
@@ -79,15 +81,20 @@ def cli(env, username, email, password, from_user, template, api_key):
7981 new_api_key = None
8082 if api_key :
8183 click .secho ("Adding API key..." , fg = 'green' )
82- new_api_key = mgr .addApiAuthenticationKey (result ['id' ])
84+ new_api_key = mgr .add_api_authentication_key (result ['id' ])
8385
8486 table = formatting .Table (['Username' , 'Email' , 'Password' , 'API Key' ])
8587 table .add_row ([result ['username' ], result ['email' ], password , new_api_key ])
8688 env .fout (table )
8789
88- def generate_password ():
89- alphabet = string .ascii_letters + string .digits
90- password = '' .join (secrets .choice (alphabet ) for i in range (20 )) # for a 20-character password
91- special = '' .join (secrets .choice (string .punctuation ) for i in range (3 ))
92- return password + special
9390
91+ def generate_password ():
92+ """Returns a 23 character random string, with 3 special characters at the end"""
93+ if sys .version_info > (3 , 6 ):
94+ import secrets # pylint: disable=import-error
95+ alphabet = string .ascii_letters + string .digits
96+ password = '' .join (secrets .choice (alphabet ) for i in range (20 ))
97+ special = '' .join (secrets .choice (string .punctuation ) for i in range (3 ))
98+ return password + special
99+ else :
100+ raise ImportError ("Generating passwords require python 3.6 or higher" )
0 commit comments