def map_branch(branch_id, node_id, depth):
#recursively walks through a branch and outputs all elements
#1: output the current node
sep = '\t' #separator
node = get_node_by_id(branch_id, node_id)
description = ''.join(get_node_attribute_values(node, 'DESCRIPTION') + get_node_attribute_values(node, '_DESCRIPTION'))
name = get_node_name_by_id(branch_id, node_id)
print('\t' * depth + node['obj_type'], sep, name)
#2: do the same for all children
for child_id in get_children_ids(branch_id, node_id):
map_branch(branch_id, child_id, depth + 1)
import json
import requests
import pprint
host = 'SOLMAN.HOSTNAME' #SolMan Hostname or IP
port = '50000' #Port (most likely 5000)
sap_client = '800' #Client
user = 'USERNAME' #Username
passwd = 'PASSWORD' #Password
solution_name = 'DELETE_ME' #Solution Name
branch_name = 'DEVELOPMENT' #Branch Name
def get_solution_by_name (solution_name):
#returns a list of solutions by name
try:
response = requests.get('http://'+ host + ':' + port + '/sap/opu/odata/sap/ProcessManagement/SolutionSet?sap-client=' + sap_client, auth=(user, passwd), headers=headers)
response.raise_for_status()
except requests.exceptions.HTTPError as errh:
print("Http Error:", errh)
except requests.exceptions.ConnectionError as errc:
print("Error Connecting:", errc)
except requests.exceptions.Timeout as errt:
print("Timeout Error:", errt)
except requests.exceptions.RequestException as err:
print("Undefined Request Exception:", err)
return list(filter(lambda s: s['SolutionName'] == solution_name, response.json()['d']['results']))
def get_branch_by_name (solution_id, branch_name):
#returns a list of branches by name and solution_id
response = requests.get('http://'+ host + ':' + port + '/sap/opu/odata/sap/ProcessManagement/BranchSet?sap-client=' + sap_client, auth=(user, passwd), headers=headers)
return list(filter(lambda b: b['BranchName'] == branch_name and b['SolutionId'] == solution_id, response.json()['d']['results']))
def get_node_attribute_values(my_node, attribute_name):
#returns the value of an attribute of a node_id
res = []
for my_attribute in my_node['attributes']:
if my_attribute['attr_type'] == attribute_name: res = my_attribute['values']
return res
def get_node_by_type(branch_id, node_type):
#returns a list of nodes in a branch, having the specified node type
response = client.get('http://'+ host + ':' + port + '/sap/opu/odata/sap/ProcessManagement/BranchContentSet(BranchId=\'' + branch_id + '\',ScopeId=\'SAP_DEFAULT_SCOPE\',SiteId=\'\',SystemRole=\'D\')/$value?sap-client=' + sap_client ,auth=(user, passwd),headers=headers)
nodes_list = json.loads(list(filter(lambda s: s['section-id'] == 'NODES', response.json()['sections']))[0]['section-content'])
return list(filter(lambda n: n['obj_type'] == node_type, nodes_list))
def get_children_ids(branch_id, node_id):
#returns the list of children of a given node in a given branch
response = client.get('http://'+ host + ':' + port + '/sap/opu/odata/sap/ProcessManagement/BranchContentSet(BranchId=\'' + branch_id + '\',ScopeId=\'SAP_DEFAULT_SCOPE\',SiteId=\'\',SystemRole=\'D\')/$value?sap-client=' + sap_client ,auth=(user, passwd),headers=headers)
nodes_structure_list = json.loads(list(filter(lambda s: s['section-id'] == 'NODES-STRUCTURE', response.json()['sections']))[0]['section-content'])
nodes_structure_list_filtered = list(filter(lambda x: x['parent_occ'] == node_id, nodes_structure_list))
if len(nodes_structure_list_filtered) == 0:
return []
else:
return list(filter(lambda x: x['parent_occ'] == node_id, nodes_structure_list_filtered))[0]['children']
def get_node_by_id(branch_id, node_id):
#returns the specified node (by node_id and branch_id)
response = client.get('http://'+ host + ':' + port + '/sap/opu/odata/sap/ProcessManagement/BranchContentSet(BranchId=\'' + branch_id + '\',ScopeId=\'SAP_DEFAULT_SCOPE\',SiteId=\'\',SystemRole=\'D\')/$value?sap-client=' + sap_client ,auth=(user, passwd),headers=headers)
nodes_list = json.loads(list(filter(lambda s: s['section-id'] == 'NODES', response.json()['sections']))[0]['section-content'])
return list(filter(lambda x: x['occ_id'] == node_id, nodes_list))[0]
def get_node_name_by_id(branch_id, node_id):
#returns a node's name (based on node_id and branch_id)
response = client.get('http://'+ host + ':' + port + '/sap/opu/odata/sap/ProcessManagement/BranchContentSet(BranchId=\'' + branch_id + '\',ScopeId=\'SAP_DEFAULT_SCOPE\',SiteId=\'\',SystemRole=\'D\')/$value?sap-client=' + sap_client ,auth=(user, passwd),headers=headers)
names_list = json.loads(list(filter(lambda s: s['section-id'] == 'ELEMENT-NAMES', response.json()['sections']))[0]['section-content'])
names_list_filtered = list(filter(lambda n: n['occ_id'] == node_id, names_list['D']))
if len(names_list_filtered) == 0: return ''
else: return names_list_filtered[0]['name']
def map_branch(branch_id, node_id, depth):
#recursively walks through a branch and outputs all elements
#1: output the current node
sep = '\t'
node = get_node_by_id(branch_id, node_id)
description = ''.join(get_node_attribute_values(node, 'DESCRIPTION') + get_node_attribute_values(node, '_DESCRIPTION'))
name = get_node_name_by_id(branch_id, node_id)
print('\t' * depth + node['obj_type'], sep, name)
#2: do the same for all children
for child_id in get_children_ids(branch_id, node_id):
map_branch(branch_id, child_id, depth + 1)
client = requests.session()
headers = {'X-CSRF-Token': 'Fetch', 'Accept': 'application/json', 'Content-Type': 'application/json'}
#get solution id (by name), branch id (by name) and root node (by type)
solution_id = get_solution_by_name(solution_name)[0]['SolutionId']
branch_id = get_branch_by_name(solution_id, branch_name)[0]['BranchId']
root_node = get_node_by_type(branch_id, 'ROOT')
#map branch starting at root node
map_branch(branch_id, root_node[0]['occ_id'], 0)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
12 | |
11 | |
10 | |
10 | |
8 | |
6 | |
6 | |
6 | |
6 | |
5 |