forked from softlayer/softlayer-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_storage.py
More file actions
122 lines (91 loc) · 4.18 KB
/
object_storage.py
File metadata and controls
122 lines (91 loc) · 4.18 KB
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
"""
SoftLayer.object_storage
~~~~~~~~~~~~~~~~~~~~~~~~
Object Storage Manager/helpers
:license: MIT, see LICENSE for more details.
"""
from SoftLayer.exceptions import SoftLayerError
from SoftLayer import utils
LIST_ACCOUNTS_MASK = '''mask[
id,username,notes,vendorName,serviceResource
]'''
ENDPOINT_MASK = '''mask(SoftLayer_Network_Storage_Hub_Swift)[
id,storageNodes[id,backendIpAddress,frontendIpAddress,datacenter]
]'''
class ObjectStorageManager(utils.IdentifierMixin, object):
"""Manager for SoftLayer Object Storage accounts.
See product information here: https://www.ibm.com/cloud/object-storage
:param SoftLayer.API.BaseClient client: the client instance
"""
def __init__(self, client):
self.client = client
self.resolvers = [self._get_id_from_username]
def list_accounts(self, object_mask=None, object_filter=None, limit=10):
"""Lists your object storage accounts."""
object_mask = object_mask if object_mask else LIST_ACCOUNTS_MASK
return self.client.call('Account',
'getHubNetworkStorage',
mask=object_mask,
filter=object_filter,
limit=limit)
def list_endpoints(self):
"""Lists the known object storage endpoints."""
_filter = {
'hubNetworkStorage': {'vendorName': {'operation': 'Swift'}},
}
endpoints = []
network_storage = self.client.call('Account',
'getHubNetworkStorage',
mask=ENDPOINT_MASK,
limit=1,
filter=_filter)
if network_storage:
for node in network_storage['storageNodes']:
endpoints.append({
'datacenter': node['datacenter'],
'public': node['frontendIpAddress'],
'private': node['backendIpAddress'],
})
return endpoints
def create_credential(self, identifier):
"""Create object storage credential.
:param int identifier: The object storage account identifier.
"""
return self.client.call('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'credentialCreate',
id=identifier)
def delete_credential(self, identifier, credential_id=None):
"""Delete the object storage credential.
:param int id: The object storage account identifier.
:param int credential_id: The credential id to be deleted.
"""
credential = {
'id': credential_id
}
return self.client.call('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'credentialDelete',
credential, id=identifier)
def limit_credential(self, identifier):
"""Limit object storage credentials.
:param int identifier: The object storage account identifier.
"""
return self.client.call('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'getCredentialLimit',
id=identifier)
def list_credential(self, identifier):
"""List the object storage credentials.
:param int identifier: The object storage account identifier.
"""
return self.client.call('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'getCredentials',
id=identifier)
def _get_id_from_username(self, username):
"""Looks up a username's id
:param string username: Username to lookup
:returns: The id that matches username.
"""
_mask = "mask[id,username]"
_filter = {'hubNetworkStorage': {'username': utils.query_filter(username)}}
account = self.list_accounts(_mask, _filter)
if len(account) == 1:
return [account[0]['id']]
elif len(account) > 1:
raise SoftLayerError("Multiple object storage accounts found with the name: {}".format(username))
else:
raise SoftLayerError("Unable to find object storage account id for: {}".format(username))