File: order.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 (127 lines) | stat: -rw-r--r-- 5,549 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""Order a file storage volume."""
# :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


CONTEXT_SETTINGS = {'token_normalize_func': lambda x: x.upper()}


@click.command(cls=SoftLayer.CLI.command.SLCommand, context_settings=CONTEXT_SETTINGS)
@click.option('--storage-type', required=True, type=click.Choice(['performance', 'endurance']),
              help='Type of file storage volume')
@click.option('--size', type=int, required=True,
              help='Size of file storage volume in GB')
@click.option('--iops', type=int,
              help="""Performance Storage IOPs. Options vary based on storage size.
[required for storage-type performance]""")
@click.option('--tier', type=click.Choice(['0.25', '2', '4', '10']),
              help='Endurance Storage Tier (IOP per GB) [required for storage-type endurance]')
@click.option('-l', '--location', required=True,
              help='Datacenter short name (e.g.: dal09)')
@click.option('--snapshot-size', type=int,
              help='Optional parameter for ordering snapshot '
              'space along with endurance file storage; specifies '
              'the size (in GB) of snapshot space to order')
@click.option('--service-offering',
              help="""The service offering package to use for placing the order.
[optional, default is \'storage_as_a_service\']. enterprise and performance are depreciated""",
              default='storage_as_a_service',
              type=click.Choice([
                  'storage_as_a_service',
                  'enterprise',
                  'performance']))
@click.option('--billing', type=click.Choice(['hourly', 'monthly']), default='monthly',
              help="Optional parameter for Billing rate (default to monthly)")
@click.option('--force', default=False, is_flag=True, help="Force order file storage volume without confirmation")
@environment.pass_env
def cli(env, storage_type, size, iops, tier,
        location, snapshot_size, service_offering, billing, force):
    """Order a file storage volume.

    Example::
        slcli file volume-order --storage-type performance --size 1000 --iops 4000 -d dal09
        This command orders a performance volume with size is 1000GB, IOPS is 4000, located at dal09.

        slcli file volume-order --storage-type endurance --size 500 --tier 4 -d dal09 --snapshot-size 500
        This command orders an endurance volume with size is 500GB, tier level is 4 IOPS per GB,\
located at dal09 with an additional snapshot space size of 500GB

    Valid size and iops options can be found here:
    https://cloud.ibm.com/docs/FileStorage/index.html#provisioning-considerations
    """
    file_manager = SoftLayer.FileStorageManager(env.client)
    storage_type = storage_type.lower()

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

    hourly_billing_flag = False
    if billing.lower() == "hourly":
        hourly_billing_flag = True

    if service_offering != 'storage_as_a_service':
        click.secho(f'{service_offering} is a legacy storage offering', fg='red')
        if hourly_billing_flag:
            raise exceptions.CLIAbort(
                'Hourly billing is only available for the storage_as_a_service service offering'
            )

    order = {}
    if storage_type == 'performance':
        if iops is None:
            raise exceptions.CLIAbort('Option --iops required with Performance')

        if service_offering == 'performance' and snapshot_size is not None:
            raise exceptions.CLIAbort(
                '--snapshot-size is not available for performance service offerings. '
                'Use --service-offering storage_as_a_service'
            )

        try:
            order = file_manager.order_file_volume(
                storage_type=storage_type,
                location=location,
                size=size,
                iops=iops,
                snapshot_size=snapshot_size,
                service_offering=service_offering,
                hourly_billing_flag=hourly_billing_flag
            )
        except ValueError as ex:
            raise exceptions.ArgumentError(str(ex))

    if storage_type == 'endurance':
        if tier is None:
            raise exceptions.CLIAbort(
                'Option --tier required with Endurance in IOPS/GB [0.25,2,4,10]'
            )

        try:
            order = file_manager.order_file_volume(
                storage_type=storage_type,
                location=location,
                size=size,
                tier_level=float(tier),
                snapshot_size=snapshot_size,
                service_offering=service_offering,
                hourly_billing_flag=hourly_billing_flag
            )
        except ValueError as ex:
            raise exceptions.ArgumentError(str(ex))

    if 'placedOrder' in order.keys():
        click.echo(f"Order #{order['placedOrder']['id']} placed successfully!")
        for item in order['placedOrder']['items']:
            click.echo(" > %s" % item['description'])
        click.echo(
            f"\nYou may run \"slcli file volume-list --order {order['placedOrder']['id']}\" to find this file "
            "volume after it is ready.")
    else:
        click.echo("Order could not be placed! Please verify your options and try again.")