Skip to content

Commit c204e8f

Browse files
code cleanup
1 parent 9b92ece commit c204e8f

File tree

6 files changed

+43
-32
lines changed

6 files changed

+43
-32
lines changed

SoftLayer/CLI/routes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@
287287
('user:detail', 'SoftLayer.CLI.user.detail:cli'),
288288
('user:permissions', 'SoftLayer.CLI.user.permissions:cli'),
289289
('user:edit-permissions', 'SoftLayer.CLI.user.edit_permissions:cli'),
290-
('user:edit-details', 'SoftLayer.CLI.user.edit-details:cli'),
290+
('user:edit-details', 'SoftLayer.CLI.user.edit_details:cli'),
291291
('user:create', 'SoftLayer.CLI.user.create:cli'),
292292

293293
('vlan', 'SoftLayer.CLI.vlan'),

SoftLayer/CLI/user/create.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
"""Creates a user """
22
# :license: MIT, see LICENSE for more details.
33

4-
import click
54
import json
6-
import secrets
75
import string
6+
import sys
7+
8+
import click
89

910
import SoftLayer
10-
from SoftLayer.CLI import columns as column_helper
1111
from SoftLayer.CLI import environment
1212
from SoftLayer.CLI import exceptions
1313
from 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.")
@@ -34,13 +34,15 @@
3434
def 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")
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
"""List Users."""
22
# :license: MIT, see LICENSE for more details.
33

4-
import click
4+
55
import json
66

7+
import click
8+
79
import SoftLayer
810
from SoftLayer.CLI import environment
11+
from SoftLayer.CLI import exceptions
912
from SoftLayer.CLI import helpers
1013

1114

@@ -20,7 +23,7 @@ def cli(env, user, template):
2023
JSON strings should be enclosed in '' and each item should be enclosed in ""
2124
2225
:Example: slcli user edit-details testUser -t '{"firstName": "Test", "lastName": "Testerson"}'
23-
."""
26+
"""
2427
mgr = SoftLayer.UserManager(env.client)
2528
user_id = helpers.resolve_id(mgr.resolve_ids, user, 'username')
2629

@@ -30,11 +33,11 @@ def cli(env, user, template):
3033
template_object = json.loads(template)
3134
for key in template_object:
3235
user_template[key] = template_object[key]
33-
except json.decoder.JSONDecodeError as ex:
34-
raise exceptions.ArgumentError("Unable to parse --template. %s" % ex.msg)
36+
except ValueError as ex:
37+
raise exceptions.ArgumentError("Unable to parse --template. %s" % ex)
3538

3639
result = mgr.edit_user(user_id, user_template)
3740
if result:
3841
click.secho("%s updated successfully" % (user), fg='green')
3942
else:
40-
click.secho("Failed to update %s" % (user), fg='red')
43+
click.secho("Failed to update %s" % (user), fg='red')

SoftLayer/managers/user.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,14 @@ def edit_user(self, user_id, user_object):
253253
"""
254254
return self.user_service.editObject(user_object, id=user_id)
255255

256-
def addApiAuthenticationKey(self, user_id):
256+
def add_api_authentication_key(self, user_id):
257+
"""Calls SoftLayer_User_Customer::addApiAuthenticationKey
258+
259+
:param int user_id: User to add API key to
260+
"""
257261
return self.user_service.addApiAuthenticationKey(id=user_id)
258262

263+
259264
def _keyname_search(haystack, needle):
260265
for item in haystack:
261266
if item.get('keyName') == needle:

tests/CLI/modules/user_tests.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
import json
1010

1111

12-
#SoftLayer/CLI/user/detail.py 114-119, 128-134
13-
#SoftLayer/CLI/user/edit_permissions.py 28-29, 38
14-
#SoftLayer/CLI/user/permissions.py 25, 56
15-
1612
class UserCLITests(testing.TestCase):
1713

1814
"""User list tests"""

tests/managers/user_tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ def test_permissions_from_user(self, user_permissions):
111111
]
112112
self.manager.permissions_from_user(1234, 5678)
113113
self.assert_called_with('SoftLayer_User_Customer', 'addBulkPortalPermission',
114-
args=(user_permissions.return_value,))
114+
args=(user_permissions.return_value,))
115115
self.assert_called_with('SoftLayer_User_Customer', 'removeBulkPortalPermission',
116-
args=(removed_permissions,))
116+
args=(removed_permissions,))
117117

118118
def test_get_id_from_username_one_match(self):
119119
account_mock = self.set_mock('SoftLayer_Account', 'getUsers')

0 commit comments

Comments
 (0)