File: create.py

package info (click to toggle)
python-softlayer 6.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,508 kB
  • sloc: python: 57,195; makefile: 133; xml: 97; sh: 59
file content (98 lines) | stat: -rw-r--r-- 4,003 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""Creates a user """
# :license: MIT, see LICENSE for more details.

import json
import string
import sys

import click

import SoftLayer
from SoftLayer.CLI import environment
from SoftLayer.CLI import exceptions
from SoftLayer.CLI import formatting
from SoftLayer.CLI import helpers


@click.command(cls=SoftLayer.CLI.command.SLCommand, )
@click.argument('username')
@click.option('--email', '-e', required=True,
              help="Email address for this user. Required for creation.")
@click.option('--password', '-p', default=None, show_default=True,
              help="Password to set for this user. If no password is provided, user will be sent an email "
                   "to generate one, which expires in 24 hours.  '-p generate' will create a password for you "
                   "(Requires Python 3.6+). Passwords require 8+ characters, upper and lowercase, a number "
                   "and a symbol.")
@click.option('--from-user', '-u', default=None,
              help="Base user to use as a template for creating this user. "
                   "Will default to the user running this command. Information provided in --template "
                   "supersedes this template.")
@click.option('--template', '-t', default=None,
              help="A json string describing https://softlayer.github.io/reference/datatypes/SoftLayer_User_Customer/")
@environment.pass_env
def cli(env, username, email, password, from_user, template):
    """Creates a user Users.

    Remember to set the permissions and access for this new user.

    Example::

        slcli user create my@email.com -e my@email.com -p generate -a
        -t '{"firstName": "Test", "lastName": "Testerson"}'

    """

    mgr = SoftLayer.UserManager(env.client)
    user_mask = ("mask[id, firstName, lastName, email, companyName, address1, city, country, postalCode, "
                 "state, userStatusId, timezoneId]")
    from_user_id = None
    if from_user is None:
        user_template = mgr.get_current_user(objectmask=user_mask)
        from_user_id = user_template['id']
    else:
        from_user_id = helpers.resolve_id(mgr.resolve_ids, from_user, 'username')
        user_template = mgr.get_user(from_user_id, objectmask=user_mask)
    # If we send the ID back to the API, an exception will be thrown
    del user_template['id']

    if template is not None:
        try:
            template_object = json.loads(template)
            for key in template_object:
                user_template[key] = template_object[key]
        except ValueError as ex:
            raise exceptions.ArgumentError(f"Unable to parse --template. {ex}")

    user_template['username'] = username
    if password == 'generate':
        password = generate_password()

    user_template['email'] = email

    if not env.skip_confirmations:
        table = formatting.KeyValueTable(['name', 'value'])
        for key in user_template:
            table.add_row([key, user_template[key]])
        table.add_row(['password', password])
        click.secho("You are about to create the following user...", fg='green')
        env.fout(table)
        if not formatting.confirm("Do you wish to continue?"):
            raise exceptions.CLIAbort("Canceling creation!")

    result = mgr.create_user(user_template, password)

    table = formatting.Table(['Username', 'Email', 'Password'])
    table.add_row([result['username'], result['email'], password])
    env.fout(table)


def generate_password():
    """Returns a 23 character random string, with 3 special characters at the end"""
    if sys.version_info > (3, 6):
        import secrets  # pylint: disable=import-error,import-outside-toplevel
        alphabet = string.ascii_letters + string.digits
        password = ''.join(secrets.choice(alphabet) for i in range(20))
        special = ''.join(secrets.choice(string.punctuation) for i in range(3))
        return password + special
    else:
        raise ImportError("Generating passwords require python 3.6 or higher")