1"""Add a new subnet to your account."""
2# :license: MIT, see LICENSE for more details.
3
4import click
5
6import SoftLayer
7from SoftLayer.CLI import environment
8from SoftLayer.CLI import exceptions
9from SoftLayer.CLI import formatting
10
11
12@click.command(short_help="Add a new subnet to your account")
13@click.argument('network', type=click.Choice(['static', 'public', 'private']))
14@click.argument('quantity', type=click.INT)
15@click.argument('endpoint-id', type=click.INT)
16@click.option('--ipv6', '--v6', is_flag=True, help="Order IPv6 Addresses")
17@click.option('--test',
18              is_flag=True,
19              help="Do not order the subnet; just get a quote")
20@environment.pass_env
21def cli(env, network, quantity, endpoint_id, ipv6, test):
22    """Add a new subnet to your account. Valid quantities vary by type.
23
24    \b
25    IPv4
26    static  - 1, 2, 4, 8, 16, 32, 64, 128, 256
27    public  - 4, 8, 16, 32, 64, 128, 256
28    private - 4, 8, 16, 32, 64, 128, 256
29
30    \b
31    IPv6
32    static  - 64
33    public  - 64
34
35    \b
36    endpoint-id
37    static  - Network_Subnet_IpAddress identifier.
38    public  - Network_Vlan identifier
39    private - Network_Vlan identifier
40"""
41
42    mgr = SoftLayer.NetworkManager(env.client)
43
44    if not (test or env.skip_confirmations):
45        if not formatting.confirm("This action will incur charges on your "
46                                  "account. Continue?"):
47            raise exceptions.CLIAbort('Cancelling order.')
48
49    version = 4
50    if ipv6:
51        version = 6
52
53    try:
54        result = mgr.add_subnet(network, quantity=quantity, endpoint_id=endpoint_id, version=version, test_order=test)
55
56    except SoftLayer.SoftLayerAPIError as error:
57        raise exceptions.CLIAbort('Unable to order {} {} ipv{} , error: {}'.format(quantity,
58                                                                                   network,
59                                                                                   version,
60                                                                                   error.faultString))
61
62    table = formatting.Table(['Item', 'cost'])
63    table.align['Item'] = 'r'
64    table.align['cost'] = 'r'
65
66    total = 0.0
67    if 'prices' in result:
68        for price in result['prices']:
69            total += float(price.get('recurringFee', 0.0))
70            rate = "%.2f" % float(price['recurringFee'])
71
72            table.add_row([price['item']['description'], rate])
73
74    table.add_row(['Total monthly cost', "%.2f" % total])
75    env.fout(table)
76