File: block.py

package info (click to toggle)
python-softlayer 6.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,100 kB
  • sloc: python: 53,771; makefile: 289; sh: 57
file content (217 lines) | stat: -rw-r--r-- 8,548 bytes parent folder | download | duplicates (2)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
"""
    SoftLayer.block
    ~~~~~~~~~~~~~~~
    Block Storage Manager

    :license: MIT, see LICENSE for more details.
"""
from SoftLayer.managers.storage import StorageManager
from SoftLayer.managers import storage_utils
from SoftLayer import utils


# pylint: disable=too-many-public-methods


class BlockStorageManager(StorageManager):
    """Manages SoftLayer Block Storage volumes.

    See product information here: https://www.ibm.com/cloud/block-storage
    """

    def list_block_volume_limit(self):
        """Returns a list of block volume count limit.

        :return: Returns a list of block volume count limit.
        """
        return self.get_volume_count_limits()

    def list_block_volumes(self, datacenter=None, username=None, storage_type=None, order=None, **kwargs):
        """Returns a list of block volumes.

        :param datacenter: Datacenter short name (e.g.: dal09)
        :param username: Name of volume.
        :param storage_type: Type of volume: Endurance or Performance
        :param order: Volume order id.
        :param kwargs:
        :return: Returns a list of block volumes.
        """
        if 'mask' not in kwargs:
            items = [
                'id',
                'username',
                'lunId',
                'capacityGb',
                'bytesUsed',
                'serviceResource.datacenter[name]',
                'serviceResourceBackendIpAddress',
                'activeTransactionCount',
                'replicationPartnerCount'
            ]
            kwargs['mask'] = ','.join(items)

        _filter = utils.NestedDict(kwargs.get('filter') or {})

        _filter['iscsiNetworkStorage']['serviceResource']['type']['type'] = utils.query_filter('!~ ISCSI')

        _filter['iscsiNetworkStorage']['storageType']['keyName'] = (
            utils.query_filter('*BLOCK_STORAGE*'))
        if storage_type:
            _filter['iscsiNetworkStorage']['storageType']['keyName'] = (
                utils.query_filter('%s_BLOCK_STORAGE*' % storage_type.upper()))

        if datacenter:
            _filter['iscsiNetworkStorage']['serviceResource']['datacenter'][
                'name'] = utils.query_filter(datacenter)

        if username:
            _filter['iscsiNetworkStorage']['username'] = utils.query_filter(username)

        if order:
            _filter['iscsiNetworkStorage']['billingItem']['orderItem'][
                'order']['id'] = utils.query_filter(order)

        kwargs['filter'] = _filter.to_dict()
        return self.client.call('Account', 'getIscsiNetworkStorage', iter=True, **kwargs)

    def get_block_volume_details(self, volume_id, **kwargs):
        """Returns details about the specified volume.

        :param volume_id: ID of volume.
        :param kwargs:
        :return: Returns details about the specified volume.
        """
        return self.get_volume_details(volume_id, **kwargs)

    def get_block_volume_access_list(self, volume_id, **kwargs):
        """Returns a list of authorized hosts for a specified volume.

        :param volume_id: ID of volume.
        :param kwargs:
        :return: Returns a list of authorized hosts for a specified volume.
        """
        return self.get_volume_access_list(volume_id, **kwargs)

    def get_block_volume_snapshot_list(self, volume_id, **kwargs):
        """Returns a list of snapshots for the specified volume.

        :param volume_id: ID of volume.
        :param kwargs:
        :return: Returns a list of snapshots for the specified volume.
        """
        return self.get_volume_snapshot_list(volume_id, **kwargs)

    def assign_subnets_to_acl(self, access_id, subnet_ids):
        """Assigns subnet records to ACL for the access host.

        access_id is the host_id obtained by: slcli block access-list <volume_id>

        :param integer access_id: id of the access host
        :param list subnet_ids: The ids of the subnets to be assigned
        :return: Returns int array of assigned subnet ids
        """
        return self.client.call('Network_Storage_Allowed_Host', 'assignSubnetsToAcl', subnet_ids, id=access_id)

    def remove_subnets_from_acl(self, access_id, subnet_ids):
        """Removes subnet records from ACL for the access host.

        access_id is the host_id obtained by: slcli block access-list <volume_id>

        :param integer access_id: id of the access host
        :param list subnet_ids: The ids of the subnets to be removed
        :return: Returns int array of removed subnet ids
        """
        return self.client.call('Network_Storage_Allowed_Host', 'removeSubnetsFromAcl', subnet_ids, id=access_id)

    def get_subnets_in_acl(self, access_id):
        """Returns a list of subnet records for the access host.

         access_id is the host_id obtained by: slcli block access-list <volume_id>

        :param integer access_id: id of the access host
        :return: Returns an array of SoftLayer_Network_Subnet objects
        """
        return self.client.call('Network_Storage_Allowed_Host', 'getSubnetsInAcl', id=access_id)

    def order_block_volume(self, storage_type, location, size, os_type,
                           iops=None, tier_level=None, snapshot_size=None,
                           service_offering='storage_as_a_service',
                           hourly_billing_flag=False):
        """Places an order for a block volume.

        :param storage_type: 'performance' or 'endurance'
        :param location: Datacenter in which to order iSCSI volume
        :param size: Size of the desired volume, in GB
        :param os_type: OS Type to use for volume alignment, see help for list
        :param iops: Number of IOPs for a "Performance" order
        :param tier_level: Tier level to use for an "Endurance" order
        :param snapshot_size: The size of optional snapshot space,
            if snapshot space should also be ordered (None if not ordered)
        :param service_offering: Requested offering package to use in the order
            ('storage_as_a_service', 'enterprise', or 'performance')
        :param hourly_billing_flag: Billing type, monthly (False)
            or hourly (True), default to monthly.
        """
        order = storage_utils.prepare_volume_order_object(
            self, storage_type, location, size, iops, tier_level,
            snapshot_size, service_offering, 'block', hourly_billing_flag
        )

        order['osFormatType'] = {'keyName': os_type}

        return self.client.call('Product_Order', 'placeOrder', order)

    def cancel_block_volume(self, volume_id, reason='No longer needed', immediate=False):
        """Cancels the given block storage volume.

        :param integer volume_id: The volume ID
        :param string reason: The reason for cancellation
        :param boolean immediate_flag: Cancel immediately or on anniversary date
        """
        return self.cancel_volume(volume_id, reason, immediate)

    def set_credential_password(self, access_id, password):
        """Sets the password for an access host

        :param integer access_id: id of the access host
        :param string password: password to  set
        """

        return self.client.call('Network_Storage_Allowed_Host', 'setCredentialPassword',
                                password, id=access_id)

    def create_or_update_lun_id(self, volume_id, lun_id):
        """Set the LUN ID on a volume.

        :param integer volume_id: The id of the volume
        :param integer lun_id: LUN ID to set on the volume
        :return: a SoftLayer_Network_Storage_Property object
        """
        return self.client.call('Network_Storage', 'createOrUpdateLunId', lun_id, id=volume_id)

    def _get_ids_from_username(self, username):
        object_mask = "mask[id]"
        results = self.list_block_volumes(username=username, mask=object_mask)
        if results:
            return [result['id'] for result in results]
        return []

    def get_cloud_list(self):
        """Returns a list cloud object storage.

        return: Returns a list cloud object storage.
        """

        mask = 'mask[id,username,billingItem,storageType, notes]'

        return self.client.call('Account', 'getHubNetworkStorage', mask=mask)

    def get_buckets(self, object_id):
        """Return  buckets data of the cloud storage.

        :param object_id cloud object storage identifier

        Returns: Get buckets

        """
        return self.client.call('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'getBuckets', id=object_id)