6
6
# pylint: disable=docstring-keyword-should-match-keyword-only
7
7
8
8
import uuid
9
-
10
- from typing import Union , Optional , Any , TYPE_CHECKING
9
+ from typing import Any , cast , Optional , Union , TYPE_CHECKING
11
10
12
11
from azure .core .exceptions import HttpResponseError
13
12
from azure .core .tracing .decorator_async import distributed_trace_async
14
-
15
- from .._shared .response_handlers import return_response_headers , process_storage_error
16
13
from .._generated .aio .operations import FileOperations , ShareOperations
14
+ from .._shared .response_handlers import return_response_headers , process_storage_error
17
15
18
16
if TYPE_CHECKING :
19
17
from azure .storage .fileshare .aio import ShareClient , ShareFileClient
18
+ from datetime import datetime
20
19
21
20
22
21
class ShareLeaseClient : # pylint: disable=client-accepts-api-version-keyword
@@ -42,17 +41,27 @@ class ShareLeaseClient: # pylint: disable=client-accepts-api-version-keyword
42
41
A string representing the lease ID of an existing lease. This value does not
43
42
need to be specified in order to acquire a new lease, or break one.
44
43
"""
44
+
45
+ id : str
46
+ """The ID of the lease currently being maintained. This will be `None` if no
47
+ lease has yet been acquired."""
48
+ etag : Optional [str ]
49
+ """The ETag of the lease currently being maintained. This will be `None` if no
50
+ lease has yet been acquired or modified."""
51
+ last_modified : Optional ["datetime" ]
52
+ """The last modified timestamp of the lease currently being maintained.
53
+ This will be `None` if no lease has yet been acquired or modified."""
54
+
45
55
def __init__ ( # pylint: disable=missing-client-constructor-parameter-credential, missing-client-constructor-parameter-kwargs
46
- self , client : Union ["ShareFileClient" , "ShareClient" ],
47
- lease_id : Optional [str ] = None
56
+ self , client : Union ["ShareFileClient" , "ShareClient" ], lease_id : Optional [str ] = None
48
57
) -> None :
49
58
self .id = lease_id or str (uuid .uuid4 ())
50
59
self .last_modified = None
51
60
self .etag = None
52
- if hasattr (client , ' file_name' ):
61
+ if hasattr (client , " file_name" ):
53
62
self ._client = client ._client .file # type: ignore
54
63
self ._snapshot = None
55
- elif hasattr (client , ' share_name' ):
64
+ elif hasattr (client , " share_name" ):
56
65
self ._client = client ._client .share
57
66
self ._snapshot = client .snapshot
58
67
else :
@@ -90,20 +99,21 @@ async def acquire(self, **kwargs: Any) -> None:
90
99
:rtype: None
91
100
"""
92
101
try :
93
- lease_duration = kwargs .pop (' lease_duration' , - 1 )
102
+ lease_duration = kwargs .pop (" lease_duration" , - 1 )
94
103
if self ._snapshot :
95
- kwargs [' sharesnapshot' ] = self ._snapshot
104
+ kwargs [" sharesnapshot" ] = self ._snapshot
96
105
response = await self ._client .acquire_lease (
97
- timeout = kwargs .pop (' timeout' , None ),
106
+ timeout = kwargs .pop (" timeout" , None ),
98
107
duration = lease_duration ,
99
108
proposed_lease_id = self .id ,
100
109
cls = return_response_headers ,
101
- ** kwargs )
110
+ ** kwargs
111
+ )
102
112
except HttpResponseError as error :
103
113
process_storage_error (error )
104
- self .id = response .get (' lease_id' )
105
- self .last_modified = response .get (' last_modified' )
106
- self .etag = response .get (' etag' )
114
+ self .id = response .get (" lease_id" )
115
+ self .last_modified = response .get (" last_modified" )
116
+ self .etag = response .get (" etag" )
107
117
108
118
@distributed_trace_async
109
119
async def renew (self , ** kwargs : Any ) -> None :
@@ -130,15 +140,16 @@ async def renew(self, **kwargs: Any) -> None:
130
140
try :
131
141
response = await self ._client .renew_lease (
132
142
lease_id = self .id ,
133
- timeout = kwargs .pop (' timeout' , None ),
143
+ timeout = kwargs .pop (" timeout" , None ),
134
144
sharesnapshot = self ._snapshot ,
135
145
cls = return_response_headers ,
136
- ** kwargs )
146
+ ** kwargs
147
+ )
137
148
except HttpResponseError as error :
138
149
process_storage_error (error )
139
- self .etag = response .get (' etag' )
140
- self .id = response .get (' lease_id' )
141
- self .last_modified = response .get (' last_modified' )
150
+ self .etag = response .get (" etag" )
151
+ self .id = response .get (" lease_id" )
152
+ self .last_modified = response .get (" last_modified" )
142
153
143
154
@distributed_trace_async
144
155
async def release (self , ** kwargs : Any ) -> None :
@@ -156,21 +167,19 @@ async def release(self, **kwargs: Any) -> None:
156
167
"""
157
168
try :
158
169
if self ._snapshot :
159
- kwargs [' sharesnapshot' ] = self ._snapshot
170
+ kwargs [" sharesnapshot" ] = self ._snapshot
160
171
response = await self ._client .release_lease (
161
- lease_id = self .id ,
162
- timeout = kwargs .pop ('timeout' , None ),
163
- cls = return_response_headers ,
164
- ** kwargs )
172
+ lease_id = self .id , timeout = kwargs .pop ("timeout" , None ), cls = return_response_headers , ** kwargs
173
+ )
165
174
except HttpResponseError as error :
166
175
process_storage_error (error )
167
- self .etag = response .get (' etag' )
168
- self .id = response .get (' lease_id' )
169
- self .last_modified = response .get (' last_modified' )
176
+ self .etag = response .get (" etag" )
177
+ self .id = response .get (" lease_id" )
178
+ self .last_modified = response .get (" last_modified" )
170
179
171
180
@distributed_trace_async
172
181
async def change (self , proposed_lease_id : str , ** kwargs : Any ) -> None :
173
- """ Changes the lease ID of an active lease. A change must include the current lease ID in x-ms-lease-id and
182
+ """Changes the lease ID of an active lease. A change must include the current lease ID in x-ms-lease-id and
174
183
a new lease ID in x-ms-proposed-lease-id.
175
184
176
185
:param str proposed_lease_id:
@@ -186,18 +195,19 @@ async def change(self, proposed_lease_id: str, **kwargs: Any) -> None:
186
195
"""
187
196
try :
188
197
if self ._snapshot :
189
- kwargs [' sharesnapshot' ] = self ._snapshot
198
+ kwargs [" sharesnapshot" ] = self ._snapshot
190
199
response = await self ._client .change_lease (
191
200
lease_id = self .id ,
192
201
proposed_lease_id = proposed_lease_id ,
193
- timeout = kwargs .pop (' timeout' , None ),
202
+ timeout = kwargs .pop (" timeout" , None ),
194
203
cls = return_response_headers ,
195
- ** kwargs )
204
+ ** kwargs
205
+ )
196
206
except HttpResponseError as error :
197
207
process_storage_error (error )
198
- self .etag = response .get (' etag' )
199
- self .id = response .get (' lease_id' )
200
- self .last_modified = response .get (' last_modified' )
208
+ self .etag = response .get (" etag" )
209
+ self .id = response .get (" lease_id" )
210
+ self .last_modified = response .get (" last_modified" )
201
211
202
212
@distributed_trace_async
203
213
async def break_lease (self , ** kwargs : Any ) -> int :
@@ -232,18 +242,17 @@ async def break_lease(self, **kwargs: Any) -> int:
232
242
:rtype: int
233
243
"""
234
244
try :
235
- lease_break_period = kwargs .pop (' lease_break_period' , None )
245
+ lease_break_period = kwargs .pop (" lease_break_period" , None )
236
246
if self ._snapshot :
237
- kwargs [' sharesnapshot' ] = self ._snapshot
247
+ kwargs [" sharesnapshot" ] = self ._snapshot
238
248
if isinstance (self ._client , ShareOperations ):
239
- kwargs [' break_period' ] = lease_break_period
249
+ kwargs [" break_period" ] = lease_break_period
240
250
if isinstance (self ._client , FileOperations ) and lease_break_period :
241
251
raise TypeError ("Setting a lease break period is only applicable to Share leases." )
242
252
243
253
response = await self ._client .break_lease (
244
- timeout = kwargs .pop ('timeout' , None ),
245
- cls = return_response_headers ,
246
- ** kwargs )
254
+ timeout = kwargs .pop ("timeout" , None ), cls = return_response_headers , ** kwargs
255
+ )
247
256
except HttpResponseError as error :
248
257
process_storage_error (error )
249
- return response .get (' lease_time' ) # type: ignore
258
+ return cast ( int , response .get (" lease_time" ))
0 commit comments