|
| 1 | +# |
| 2 | +# Set of scripts showing: |
| 3 | +# (1) How to set obj_id on objects + export TML with obj_id instead of GUID |
| 4 | +# (2) Scripts for generating automatic obj_ids from existing objects that do not have them |
| 5 | +# to build out a mapping of GUID:obj_id to generate the update REST API commands |
| 6 | +# |
| 7 | + |
1 | 8 | import json |
2 | 9 | import os |
3 | 10 | import requests.exceptions |
|
22 | 29 | print(e) |
23 | 30 | print(e.response.content) |
24 | 31 | exit() |
25 | | -def create_obj_id_update_request(guid: str, obj_id: str): |
26 | | - update_req = { |
27 | | - "headers_update": |
28 | | - ( |
29 | | - {'identifier': guid, |
30 | | - 'attributes': ( |
31 | | - { |
32 | | - 'name': 'obj_id', |
33 | | - 'value': obj_id |
34 | | - } |
35 | | - ) |
36 | | - } |
37 | | - ) |
38 | | - } |
39 | | - return update_req |
40 | 32 |
|
| 33 | +# Simple example of setting a single obj_id on a known GUID |
| 34 | +def set_one_object(): |
| 35 | + resp = ts.metadata_update_obj_id(new_obj_id='Conn.DB_Name.TableName', guid='43ab8a16-473a-44dc-9c78-4346eeb51f6c') |
| 36 | + print(json.dumps(resp, indent=2)) |
41 | 37 |
|
42 | | -# { 'guid' : 'obj_id' } |
43 | | -def create_multi_obj_id_update_request(guid_obj_id_map: Dict): |
44 | | - update_req = { |
45 | | - "headers_update": [] |
| 38 | +# Simple example of setting multiple at once using the full request format |
| 39 | +def set_multiple_objects(): |
| 40 | + req = { |
| 41 | + "metadata": [ |
| 42 | + { |
| 43 | + "new_obj_id": "Table_A", |
| 44 | + "metadata_identifier": "43ab8a16-473a-44dc-9c78-4346eeb51f6c" |
| 45 | + }, |
| 46 | + { |
| 47 | + "new_obj_id": "Table_B", |
| 48 | + "metadata_identifier": "4346eeb51f6c-9c78-473a-44dc-43ab8a16" |
| 49 | + }, |
| 50 | + ] |
46 | 51 | } |
47 | | - for guid in guid_obj_id_map: |
48 | | - header_item = { |
49 | | - 'identifier': guid, |
50 | | - 'attributes': ( |
51 | | - { |
52 | | - 'name': 'obj_id', |
53 | | - 'value': guid_obj_id_map[guid] |
54 | | - } |
55 | | - ) |
56 | | - } |
57 | | - update_req["headers_update"].append(header_item) |
58 | | - |
59 | | - return update_req |
60 | 52 |
|
61 | | -def set_one_object(): |
62 | | - # Simple example of setting a Table object to have a Full Qualified Name as the obj_id |
63 | | - update_req = create_obj_id_update_request(guid='43ab8a16-473a-44dc-9c78-4346eeb51f6c', obj_id='Conn.DB_Name.TableName') |
64 | | - |
65 | | - resp = ts.metadata_headers_update(request=update_req) |
| 53 | + resp = ts.metadata_update_obj_id(request_override=req) |
66 | 54 | print(json.dumps(resp, indent=2)) |
67 | 55 |
|
68 | | - |
| 56 | +# Build out full request from simple GUID:obj_id Dict |
| 57 | +def build_multiple_objects_update_request_by_guid(guid_obj_id_map: Dict): |
| 58 | + req = { |
| 59 | + "metadata": [] |
| 60 | + } |
| 61 | + for g in guid_obj_id_map: |
| 62 | + req["metadata"].append( |
| 63 | + {"metadata_identifier": g, |
| 64 | + "new_obj_id": guid_obj_id_map[g]} |
| 65 | + ) |
| 66 | + return req |
| 67 | + |
| 68 | +# Build out full request from simple current_obj_id:new_obj_id Dict |
| 69 | +def build_multiple_objects_update_request_by_obj_id(cur_obj_id_new_obj_id_map: Dict): |
| 70 | + req = { |
| 71 | + "metadata": [] |
| 72 | + } |
| 73 | + for c in cur_obj_id_new_obj_id_map: |
| 74 | + req["metadata"].append( |
| 75 | + {"current_obj_id": c, |
| 76 | + "new_obj_id": cur_obj_id_new_obj_id_map[c]} |
| 77 | + ) |
| 78 | + return req |
| 79 | + |
| 80 | +# Wrapper of Export TML of a single item, with lookup via GUID or obj_id, and saving to disk with |
| 81 | +# standard naming pattern |
69 | 82 | def export_tml_with_obj_id(guid:Optional[str] = None, |
70 | 83 | obj_id: Optional[str] = None, |
71 | 84 | save_to_disk=True): |
@@ -120,6 +133,12 @@ def export_tml_with_obj_id(guid:Optional[str] = None, |
120 | 133 |
|
121 | 134 | return yaml_tml |
122 | 135 |
|
| 136 | + |
| 137 | +# |
| 138 | +# Objects may have null obj_ids or will have the GUID attached on the auto-generated obj_id |
| 139 | +# To standardize for using across the entire set of Orgs, you'll need to create your own naming patterns |
| 140 | +# This shows retrieving all the objects and using either Table Attributes or Name to generate unique obj_id |
| 141 | +# |
123 | 142 | def retrieve_dev_org_objects_for_mapping(org_name: Optional[str] = None, org_id: Optional[int] = None): |
124 | 143 |
|
125 | 144 | if org_id is None: |
@@ -242,6 +261,10 @@ def retrieve_dev_org_objects_for_mapping(org_name: Optional[str] = None, org_id: |
242 | 261 |
|
243 | 262 | return final_guid_obj_id_map |
244 | 263 |
|
| 264 | + |
| 265 | +# |
| 266 | +# Looks at a guid:obj_id mapping and discovers any duplicate obj_ids generated by the initial auto-generation |
| 267 | +# |
245 | 268 | def find_duplicate_obj_ids(initial_map: Dict) -> Dict: |
246 | 269 | cnt = Counter(initial_map.values()) |
247 | 270 |
|
|
0 commit comments