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 (72 lines) | stat: -rw-r--r-- 2,280 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
"""Add a new subnet to your account."""
# :license: MIT, see LICENSE for more details.

import click

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


@click.command(cls=SoftLayer.CLI.command.SLCommand, short_help="Add a new subnet to your account")
@click.argument('network', type=click.Choice(['static', 'public', 'private']))
@click.argument('quantity', type=click.INT)
@click.argument('endpoint-id', type=click.INT)
@click.option('--ipv6', '--v6', is_flag=True, help="Order IPv6 Addresses")
@click.option('--test',
              is_flag=True,
              help="Do not order the subnet; just get a quote")
@environment.pass_env
def cli(env, network, quantity, endpoint_id, ipv6, test):
    """Add a new subnet to your account. Valid quantities vary by type.

    \b
    IPv4
    static  - 1, 2, 4, 8, 16, 32, 64, 128, 256
    public  - 4, 8, 16, 32, 64, 128, 256
    private - 4, 8, 16, 32, 64, 128, 256

    \b
    IPv6
    static  - 64
    public  - 64

    \b
    endpoint-id
    static  - Network_Subnet_IpAddress identifier.
    public  - Network_Vlan identifier
    private - Network_Vlan identifier
"""

    mgr = SoftLayer.NetworkManager(env.client)

    if not (test or env.skip_confirmations):
        if not formatting.confirm("This action will incur charges on your "
                                  "account. Continue?"):
            raise exceptions.CLIAbort('Cancelling order.')

    version = 4
    if ipv6:
        version = 6

    try:
        result = mgr.add_subnet(network, quantity=quantity, endpoint_id=endpoint_id, version=version, test_order=test)

    except SoftLayer.SoftLayerAPIError as error:
        raise exceptions.CLIAbort(f"Unable to order {quantity} {network} ipv{version} , error: {error.faultString}")

    table = formatting.Table(['Item', 'cost'])
    table.align['Item'] = 'r'
    table.align['cost'] = 'r'

    total = 0.0
    if 'prices' in result:
        for price in result['prices']:
            total += float(price.get('recurringFee', 0.0))
            rate = "%.2f" % float(price['recurringFee'])

            table.add_row([price['item']['description'], rate])

    table.add_row(['Total monthly cost', "%.2f" % total])
    env.fout(table)