@@ -63,6 +63,7 @@ def __init__(self, client_connection, database_link, id, properties=None): # py
63
63
self .client_connection = client_connection
64
64
self .id = id
65
65
self ._properties = properties
66
+ self .database_link = database_link
66
67
self .container_link = u"{}/colls/{}" .format (database_link , self .id )
67
68
self ._is_system_key = None
68
69
self ._scripts = None # type: Optional[ScriptsProxy]
@@ -151,6 +152,79 @@ def read(
151
152
152
153
return cast ('Dict[str, Any]' , self ._properties )
153
154
155
+ @distributed_trace
156
+ def create_if_not_exists (
157
+ self ,
158
+ partition_key , # type: Any
159
+ indexing_policy = None , # type: Optional[Dict[str, Any]]
160
+ default_ttl = None , # type: Optional[int]
161
+ populate_query_metrics = None , # type: Optional[bool]
162
+ offer_throughput = None , # type: Optional[int]
163
+ unique_key_policy = None , # type: Optional[Dict[str, Any]]
164
+ conflict_resolution_policy = None , # type: Optional[Dict[str, Any]]
165
+ ** kwargs # type: Any
166
+ ):
167
+ # type: (...) -> ContainerProxy
168
+ """
169
+ Create the container if it does not exist already.
170
+
171
+ If the container already exists, it is returned.
172
+
173
+ :param partition_key: The partition key to use for the container.
174
+ :param indexing_policy: The indexing policy to apply to the container.
175
+ :param default_ttl: Default time to live (TTL) for items in the container. If unspecified, items do not expire.
176
+ :param session_token: Token for use with Session consistency.
177
+ :param initial_headers: Initial headers to be sent as part of the request.
178
+ :param access_condition: Conditions Associated with the request.
179
+ :param populate_query_metrics: Enable returning query metrics in response headers.
180
+ :param offer_throughput: The provisioned throughput for this offer.
181
+ :param unique_key_policy: The unique key policy to apply to the container.
182
+ :param conflict_resolution_policy: The conflict resolution policy to apply to the container.
183
+ :param request_options: Dictionary of additional properties to be used for the request.
184
+ :param response_hook: a callable invoked with the response metadata
185
+ :returns: A `ContainerProxy` instance representing the container.
186
+ :raise CosmosHttpResponseError: The container read or creation failed.
187
+ :rtype: ~azure.cosmos.container.ContainerProxy
188
+ """
189
+
190
+ response_hook = kwargs .pop ('response_hook' , None )
191
+ try :
192
+ collection_link = self .container_link
193
+ self ._properties = self .client_connection .ReadContainer (
194
+ collection_link , ** kwargs
195
+ )
196
+
197
+ if response_hook :
198
+ response_hook (self .client_connection .last_response_headers , self ._properties )
199
+ return self
200
+ except CosmosResourceNotFoundError :
201
+ definition = dict (id = self .id ) # type: Dict[str, Any]
202
+ if partition_key :
203
+ definition ["partitionKey" ] = partition_key
204
+ if indexing_policy :
205
+ definition ["indexingPolicy" ] = indexing_policy
206
+ if default_ttl :
207
+ definition ["defaultTtl" ] = default_ttl
208
+ if unique_key_policy :
209
+ definition ["uniqueKeyPolicy" ] = unique_key_policy
210
+ if conflict_resolution_policy :
211
+ definition ["conflictResolutionPolicy" ] = conflict_resolution_policy
212
+
213
+ request_options = build_options (kwargs )
214
+ if populate_query_metrics is not None :
215
+ request_options ["populateQueryMetrics" ] = populate_query_metrics
216
+ if offer_throughput is not None :
217
+ request_options ["offerThroughput" ] = offer_throughput
218
+
219
+ data = self .client_connection .CreateContainer (
220
+ database_link = self .database_link , collection = definition , options = request_options , ** kwargs
221
+ )
222
+
223
+ if response_hook :
224
+ response_hook (self .client_connection .last_response_headers , data )
225
+
226
+ return ContainerProxy (self .client_connection , self .database_link , data ["id" ], properties = data )
227
+
154
228
@distributed_trace
155
229
def read_item (
156
230
self ,
0 commit comments