diff --git a/examples_v2/set_obj_id.py b/examples_v2/set_obj_id.py index 4d18dd2..5afaf8b 100644 --- a/examples_v2/set_obj_id.py +++ b/examples_v2/set_obj_id.py @@ -1,3 +1,10 @@ +# +# Set of scripts showing: +# (1) How to set obj_id on objects + export TML with obj_id instead of GUID +# (2) Scripts for generating automatic obj_ids from existing objects that do not have them +# to build out a mapping of GUID:obj_id to generate the update REST API commands +# + import json import os import requests.exceptions @@ -22,50 +29,56 @@ print(e) print(e.response.content) exit() -def create_obj_id_update_request(guid: str, obj_id: str): - update_req = { - "headers_update": - ( - {'identifier': guid, - 'attributes': ( - { - 'name': 'obj_id', - 'value': obj_id - } - ) - } - ) - } - return update_req +# Simple example of setting a single obj_id on a known GUID +def set_one_object(): + resp = ts.metadata_update_obj_id(new_obj_id='Conn.DB_Name.TableName', guid='43ab8a16-473a-44dc-9c78-4346eeb51f6c') + print(json.dumps(resp, indent=2)) -# { 'guid' : 'obj_id' } -def create_multi_obj_id_update_request(guid_obj_id_map: Dict): - update_req = { - "headers_update": [] +# Simple example of setting multiple at once using the full request format +def set_multiple_objects(): + req = { + "metadata": [ + { + "new_obj_id": "Table_A", + "metadata_identifier": "43ab8a16-473a-44dc-9c78-4346eeb51f6c" + }, + { + "new_obj_id": "Table_B", + "metadata_identifier": "4346eeb51f6c-9c78-473a-44dc-43ab8a16" + }, + ] } - for guid in guid_obj_id_map: - header_item = { - 'identifier': guid, - 'attributes': ( - { - 'name': 'obj_id', - 'value': guid_obj_id_map[guid] - } - ) - } - update_req["headers_update"].append(header_item) - - return update_req -def set_one_object(): - # Simple example of setting a Table object to have a Full Qualified Name as the obj_id - update_req = create_obj_id_update_request(guid='43ab8a16-473a-44dc-9c78-4346eeb51f6c', obj_id='Conn.DB_Name.TableName') - - resp = ts.metadata_headers_update(request=update_req) + resp = ts.metadata_update_obj_id(request_override=req) print(json.dumps(resp, indent=2)) - +# Build out full request from simple GUID:obj_id Dict +def build_multiple_objects_update_request_by_guid(guid_obj_id_map: Dict): + req = { + "metadata": [] + } + for g in guid_obj_id_map: + req["metadata"].append( + {"metadata_identifier": g, + "new_obj_id": guid_obj_id_map[g]} + ) + return req + +# Build out full request from simple current_obj_id:new_obj_id Dict +def build_multiple_objects_update_request_by_obj_id(cur_obj_id_new_obj_id_map: Dict): + req = { + "metadata": [] + } + for c in cur_obj_id_new_obj_id_map: + req["metadata"].append( + {"current_obj_id": c, + "new_obj_id": cur_obj_id_new_obj_id_map[c]} + ) + return req + +# Wrapper of Export TML of a single item, with lookup via GUID or obj_id, and saving to disk with +# standard naming pattern def export_tml_with_obj_id(guid:Optional[str] = None, obj_id: Optional[str] = None, save_to_disk=True): @@ -120,6 +133,12 @@ def export_tml_with_obj_id(guid:Optional[str] = None, return yaml_tml + +# +# Objects may have null obj_ids or will have the GUID attached on the auto-generated obj_id +# To standardize for using across the entire set of Orgs, you'll need to create your own naming patterns +# This shows retrieving all the objects and using either Table Attributes or Name to generate unique obj_id +# def retrieve_dev_org_objects_for_mapping(org_name: Optional[str] = None, org_id: Optional[int] = None): if org_id is None: @@ -242,6 +261,10 @@ def retrieve_dev_org_objects_for_mapping(org_name: Optional[str] = None, org_id: return final_guid_obj_id_map + +# +# Looks at a guid:obj_id mapping and discovers any duplicate obj_ids generated by the initial auto-generation +# def find_duplicate_obj_ids(initial_map: Dict) -> Dict: cnt = Counter(initial_map.values()) diff --git a/setup.cfg b/setup.cfg index 9a6a7f5..657dcb7 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = thoughtspot_rest_api_v1 -version = 1.8.5 +version = 1.8.6 description = Library implementing the ThoughtSpot V1 REST API long_description = file: README.md long_description_content_type = text/markdown diff --git a/src/thoughtspot_rest_api_v1/_version.py b/src/thoughtspot_rest_api_v1/_version.py index 7e3de06..871921a 100644 --- a/src/thoughtspot_rest_api_v1/_version.py +++ b/src/thoughtspot_rest_api_v1/_version.py @@ -1 +1 @@ -__version__ = '1.8.5' +__version__ = '1.8.6' diff --git a/src/thoughtspot_rest_api_v1/tsrestapiv2.py b/src/thoughtspot_rest_api_v1/tsrestapiv2.py index c9a61d0..aabbdbf 100644 --- a/src/thoughtspot_rest_api_v1/tsrestapiv2.py +++ b/src/thoughtspot_rest_api_v1/tsrestapiv2.py @@ -706,7 +706,8 @@ def metadata_headers_update(self, request: Dict): endpoint = 'metadata/headers/update' return self.post_request(endpoint=endpoint, request=request) - def metadata_update_obj_id(self, new_obj_id: str, guid: Optional[str], current_obj_id: Optional[str], + def metadata_update_obj_id(self, new_obj_id: Optional[str] = None, guid: Optional[str] = None, + current_obj_id: Optional[str] = None, request_override: Optional[Dict] = None): endpoint = 'metadata/update-obj-id' if request_override is not None: