1
|
|
|
""" |
2
|
|
|
Copyright George Sibble 2018 |
3
|
|
|
""" |
4
|
|
|
|
5
|
1 |
|
from ..b2_exceptions import B2Exception, B2InvalidBucketName, B2InvalidBucketConfiguration |
6
|
1 |
|
from .bucket import B2Bucket |
7
|
1 |
|
from ..api import API |
8
|
|
|
|
9
|
|
|
|
10
|
1 |
|
class B2Buckets(object): |
|
|
|
|
11
|
|
|
""" |
12
|
|
|
|
13
|
|
|
""" |
14
|
1 |
|
public = 'allPublic' |
15
|
1 |
|
private = 'allPrivate' |
16
|
|
|
|
17
|
1 |
|
def __init__(self, connector): |
18
|
|
|
""" |
19
|
|
|
|
20
|
|
|
:param connector: |
21
|
|
|
""" |
22
|
1 |
|
self.connector = connector |
23
|
1 |
|
self._buckets_by_name = {} |
24
|
1 |
|
self._buckets_by_id = {} |
25
|
|
|
|
26
|
1 |
|
def all(self): |
27
|
|
|
""" |
28
|
|
|
|
29
|
|
|
:return: |
30
|
|
|
""" |
31
|
1 |
|
return self._update_bucket_list(retrieve=True) |
32
|
|
|
|
33
|
1 |
|
def _update_bucket_list(self, retrieve=False): |
34
|
|
|
""" |
35
|
|
|
|
36
|
|
|
:param retrieve: |
37
|
|
|
:return: |
38
|
|
|
""" |
39
|
1 |
|
path = API.list_all_buckets |
40
|
1 |
|
response = self.connector.make_request(path=path, method='post', account_id_required=True) |
41
|
1 |
|
if response.status_code == 200: |
42
|
1 |
|
response_json = response.json() |
43
|
1 |
|
buckets = [] |
44
|
1 |
|
self._buckets_by_name = {} |
45
|
1 |
|
self._buckets_by_id = {} |
46
|
1 |
|
for bucket_json in response_json['buckets']: |
47
|
1 |
|
new_bucket = B2Bucket(connector=self.connector, parent_list=self, **bucket_json) |
48
|
1 |
|
buckets.append(new_bucket) |
49
|
1 |
|
self._buckets_by_name[bucket_json['bucketName']] = new_bucket |
50
|
1 |
|
self._buckets_by_id[bucket_json['bucketId']] = new_bucket |
51
|
1 |
|
if retrieve: |
52
|
1 |
|
return buckets |
53
|
|
|
else: |
54
|
|
|
raise B2Exception.parse(response) |
55
|
|
|
|
56
|
1 |
|
def get(self, bucket_name=None, bucket_id=None): |
57
|
|
|
""" |
58
|
|
|
|
59
|
|
|
:param bucket_name: |
60
|
|
|
:param bucket_id: |
61
|
|
|
:return: |
62
|
|
|
""" |
63
|
1 |
|
self._update_bucket_list() |
64
|
1 |
|
if bucket_name is not None: |
|
|
|
|
65
|
1 |
|
return self._buckets_by_name.get(bucket_name, None) |
66
|
|
|
else: |
67
|
|
|
return self._buckets_by_id.get(bucket_id, None) |
68
|
|
|
|
69
|
1 |
|
def create(self, bucket_name, security, configuration=None): |
70
|
|
|
""" |
71
|
|
|
|
72
|
|
|
:param bucket_name: |
73
|
|
|
:param configuration: |
74
|
|
|
:return: |
75
|
|
|
""" |
76
|
1 |
|
path = API.create_bucket |
77
|
1 |
|
if type(bucket_name) != str and type(bucket_name) != bytes: |
|
|
|
|
78
|
|
|
raise B2InvalidBucketName("Bucket name must be alphanumeric or '-") |
79
|
1 |
|
if type(configuration) != dict and configuration is not None: |
|
|
|
|
80
|
|
|
raise B2InvalidBucketConfiguration |
81
|
1 |
|
params = { |
82
|
|
|
'bucketName': bucket_name, |
83
|
|
|
'bucketType': security, |
84
|
|
|
#TODO: bucketInfo |
|
|
|
|
85
|
|
|
#TODO: corsRules |
|
|
|
|
86
|
|
|
#TODO: lifeCycleRules |
|
|
|
|
87
|
|
|
} |
88
|
1 |
|
response = self.connector.make_request(path=path, method='post', params=params, account_id_required=True) |
|
|
|
|
89
|
1 |
|
if response.status_code == 200: |
90
|
1 |
|
bucket_json = response.json() |
91
|
1 |
|
new_bucket = B2Bucket(connector=self.connector, parent_list=self, **bucket_json) |
92
|
1 |
|
self._buckets_by_name[bucket_json['bucketName']] = new_bucket |
93
|
1 |
|
self._buckets_by_id[bucket_json['bucketId']] = new_bucket |
94
|
1 |
|
return new_bucket |
95
|
|
|
else: |
96
|
|
|
raise B2Exception.parse(response) |
|
|
|
|