Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: Add support for bulk updates without UUID requirement #73

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions fireREST/fmc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
import logging
from http.client import responses as http_responses
from typing import Dict, Union
from typing import Dict, List, Union
from urllib.parse import urlencode

import requests
Expand Down Expand Up @@ -339,7 +339,8 @@ def url(self, path, namespace=None):
'tid': f'{self.conn.protocol}://{self.conn.hostname}{defaults.API_TID_URL}{path}',
'refresh': f'{self.conn.protocol}://{self.conn.hostname}{defaults.API_REFRESH_URL}',
'troubleshoot': f'{self.conn.protocol}://{self.conn.hostname}{defaults.API_TROUBLESHOOT_URL}/domain/'
f'{self.conn.domain["id"]}{path}'
f'{self.conn.domain["id"]}{path}',
'bulk': f'{self.conn.protocol}://{self.conn.hostname}{path}',
}
if namespace not in options.keys():
raise exc.InvalidNamespaceError(f'Invalid namespace "{namespace}" provided. Options: {options.keys()}')
Expand Down Expand Up @@ -377,7 +378,7 @@ def get(self, uuid=None, name=None, params=None):
return self.conn.get(url, params)

@utils.minimum_version_required
def update(self, data: Dict, params=None):
def update(self, data: Union[Dict, List[Dict]], container_uuid=None, params=None):
"""Update existing api resource. Existing data will be overridden with
the provided payload. The request will be routed to the correct resource
by extracting the `id` within the payload
Expand All @@ -389,7 +390,12 @@ def update(self, data: Dict, params=None):
:return: api response
:rtype: requests.Response
"""
url = self.url(self.PATH.format(uuid=data['id']))
if isinstance(data, list):
url = self.url(self.PATH.format(container_uuid=container_uuid, uuid='bulk'))
else:
if 'id' not in data:
raise ValueError("The UUID of the resource to update must be included in the data parameter")
url = self.url(self.PATH.format(uuid=data['id']))
return self.conn.put(url, data, params, self.IGNORE_FOR_UPDATE)

@utils.resolve_by_name
Expand Down Expand Up @@ -465,7 +471,7 @@ def get(self, container_uuid=None, container_name=None, uuid=None, name=None, pa

@utils.resolve_by_name
@utils.minimum_version_required
def update(self, data: Dict, container_uuid=None, container_name=None, params=None):
def update(self, data: Union[Dict, List[Dict]], container_uuid=None, container_name=None, params=None):
"""Update existing api resource. Either name or uuid of container resource must be provided
Existing data will be overridden with the provided payload. The request will be routed
to the correct resource by extracting the `id` within the payload
Expand All @@ -481,7 +487,12 @@ def update(self, data: Dict, container_uuid=None, container_name=None, params=No
:return: api response
:rtype: requests.Response
"""
url = self.url(self.PATH.format(container_uuid=container_uuid, uuid=data['id']))
if isinstance(data, list):
url = self.url(self.PATH.format(container_uuid=container_uuid, uuid='bulk'))
else:
if 'id' not in data:
raise ValueError("The UUID of the resource to update must be included in the data parameter")
url = self.url(self.PATH.format(container_uuid=container_uuid, uuid=data['id']))
return self.conn.put(url, data, params, self.IGNORE_FOR_UPDATE)

@utils.resolve_by_name
Expand Down Expand Up @@ -571,7 +582,7 @@ def get(self, container_uuid=None, container_name=None, child_container_name=Non

@utils.resolve_by_name
@utils.minimum_version_required
def update(self, data: Dict, container_uuid=None, container_name=None,
def update(self, data: Union[Dict, List[Dict]], container_uuid=None, container_name=None,
child_container_name=None, child_container_uuid=None, params=None):
"""Update existing api resource. Either name or uuid of container resource must be provided
Existing data will be overridden with the provided payload. The request will be routed
Expand All @@ -592,7 +603,13 @@ def update(self, data: Dict, container_uuid=None, container_name=None,
:return: api response
:rtype: requests.Response
"""
url = self.url(self.PATH.format(container_uuid=container_uuid, child_container_uuid=child_container_uuid,
if isinstance(data, list):
url = self.url(self.PATH.format(container_uuid=container_uuid, child_container_uuid=child_container_uuid,
uuid='bulk'))
else:
if 'id' not in data:
raise ValueError("The UUID of the resource to update must be included in the data parameter")
url = self.url(self.PATH.format(container_uuid=container_uuid, child_container_uuid=child_container_uuid,
uuid=data['id']))
return self.conn.put(url, data, self.IGNORE_FOR_UPDATE)

Expand Down