Skip to content

Commit

Permalink
Implement bulk write operations
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubFrejlach committed Jul 10, 2024
1 parent 259e3be commit f7a5ba7
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased

## [4.1.0] - 2024-07-01
### Added
- support for bulk create and bulk update for Affects (OSIDB-3124)

## [4.0.0] - 2024-06-17
### Added
Expand Down
67 changes: 67 additions & 0 deletions osidb_bindings/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ def __init__(self, base_url, auth=None, verify_ssl=True):
"list",
"create",
"destroy",
"bulk_create",
"bulk_update",
# TODO: currently blocked by OSIDB-2996
# "bulk_delete",
),
subresources={
"cvss_scores": {
Expand Down Expand Up @@ -419,6 +423,27 @@ def create(self, form_data: Dict[str, Any], *args, **kwargs):
else:
self.__raise_operation_unsupported("create")

def bulk_create(self, form_data: Dict[str, Any], *args, **kwargs):
if "bulk_create" in self.allowed_operations:
method_module = self.__get_method_module(
resource_name=self.resource_name, method="bulk_create"
)
model = getattr(method_module, "REQUEST_BODY_TYPE", None)
if model is None:
self.__raise_undefined_request_body("bulk_create")

serialized_data = serialize_data(form_data, model)
sync_fn = get_sync_function(method_module)
return sync_fn(
*args,
client=self.client(),
json_body=serialized_data,
multipart_data=UNSET,
**kwargs,
)
else:
self.__raise_operation_unsupported("bulk_create")

def update(self, id, form_data: Dict[str, Any], *args, **kwargs):
if "update" in self.allowed_operations:
method_module = self.__get_method_module(
Expand All @@ -442,6 +467,27 @@ def update(self, id, form_data: Dict[str, Any], *args, **kwargs):
else:
self.__raise_operation_unsupported("update")

def bulk_update(self, form_data: Dict[str, Any], *args, **kwargs):
if "bulk_update" in self.allowed_operations:
method_module = self.__get_method_module(
resource_name=self.resource_name, method="bulk_update"
)
model = getattr(method_module, "REQUEST_BODY_TYPE", None)
if model is None:
self.__raise_undefined_request_body("bulk_update")

serialized_data = serialize_data(form_data, model)
sync_fn = get_sync_function(method_module)
return sync_fn(
*args,
client=self.client(),
json_body=serialized_data,
multipart_data=UNSET,
**kwargs,
)
else:
self.__raise_operation_unsupported("bulk_update")

def delete(self, id, *args, **kwargs):
if "destroy" in self.allowed_operations:
method_module = self.__get_method_module(
Expand All @@ -457,6 +503,27 @@ def delete(self, id, *args, **kwargs):
else:
self.__raise_operation_unsupported("delete")

def bulk_delete(self, form_data: Dict[str, Any], *args, **kwargs):
if "bulk_delete" in self.allowed_operations:
method_module = self.__get_method_module(
resource_name=self.resource_name, method="bulk_destroy"
)
model = getattr(method_module, "REQUEST_BODY_TYPE", None)
if model is None:
self.__raise_undefined_request_body("bulk_delete")

serialized_data = serialize_data(form_data, model)
sync_fn = get_sync_function(method_module)
return sync_fn(
*args,
client=self.client(),
json_body=serialized_data,
multipart_data=UNSET,
**kwargs,
)
else:
self.__raise_operation_unsupported("bulk_delete")

# Extra operations

def count(self, *args, **kwargs):
Expand Down

0 comments on commit f7a5ba7

Please sign in to comment.