|
| 1 | +"""Create a password for a software component""" |
| 2 | +# :license: MIT, see LICENSE for more details. |
| 3 | + |
| 4 | +import click |
| 5 | + |
| 6 | +import SoftLayer |
| 7 | +from SoftLayer.CLI import environment |
| 8 | +from SoftLayer.CLI import exceptions |
| 9 | +from SoftLayer.CLI import formatting |
| 10 | + |
| 11 | + |
| 12 | +@click.command(cls=SoftLayer.CLI.command.SLCommand, ) |
| 13 | +@click.argument('identifier') |
| 14 | +@click.option('--username', '-U', required=True, help="The username part of the username/password pair") |
| 15 | +@click.option('--password', '-P', required=True, help="The password part of the username/password pair.") |
| 16 | +@click.option('--notes', '-n', help="A note string stored for this username/password pair.") |
| 17 | +@click.option('--system', required=True, help="The name of this specific piece of software.") |
| 18 | +@environment.pass_env |
| 19 | +def cli(env, identifier, username, password, notes, system): |
| 20 | + """Create a password for a software component.""" |
| 21 | + |
| 22 | + mgr = SoftLayer.HardwareManager(env.client) |
| 23 | + |
| 24 | + software = mgr.get_software_components(identifier) |
| 25 | + sw_id = '' |
| 26 | + try: |
| 27 | + for sw_instance in software: |
| 28 | + if (sw_instance['softwareLicense']['softwareDescription']['name']).lower() == system: |
| 29 | + sw_id = sw_instance['id'] |
| 30 | + except KeyError as ex: |
| 31 | + raise exceptions.CLIAbort('System id not found') from ex |
| 32 | + |
| 33 | + template = { |
| 34 | + "notes": notes, |
| 35 | + "password": password, |
| 36 | + "softwareId": sw_id, |
| 37 | + "username": username, |
| 38 | + "software": { |
| 39 | + "hardwareId": identifier, |
| 40 | + "softwareLicense": { |
| 41 | + "softwareDescription": { |
| 42 | + "name": system |
| 43 | + } |
| 44 | + } |
| 45 | + }} |
| 46 | + |
| 47 | + result = mgr.create_credential(template) |
| 48 | + |
| 49 | + table = formatting.KeyValueTable(['name', 'value']) |
| 50 | + table.align['name'] = 'r' |
| 51 | + table.align['value'] = 'l' |
| 52 | + table.add_row(['Software Id', result['id']]) |
| 53 | + table.add_row(['Created', result['createDate']]) |
| 54 | + table.add_row(['Username', result['username']]) |
| 55 | + table.add_row(['Password', result['password']]) |
| 56 | + table.add_row(['Notes', result['notes']]) |
| 57 | + env.fout(table) |
0 commit comments