I need your help to add a server via REST API by using variables for the keys and values.
In general it works but I need some help to get the right syntax for the key and value definitions by using variables.
I need the right code to post key and value which are defined by a variable: 'tag_<VARIABLE1>': '<VARIABLE1>', # how is the right syntax to use the variable at the "json={" part?
And another question from my site: is there a more dynamical way to add attributes instead of such a rigid list, especially for the labels which are dynamic?
Has somebody a solution?
Thanks a lot!
Greetings JP
here is my code example:
session = requests.session()
session.headers['Authorization'] = f"Bearer {USERNAME} {PASSWORD}"
session.headers['Accept'] = 'application/json'
SITE = "XXX"
SERVER_ALIAS = "ALIAS"
SERVER_IP = "127.0.0.1"
ENVIRONMENT = "Test"
VARIABLE1 = "1XXXXX"
VARIABLE2 = "'tag_" + VARIABLE1 + "': '" + VARIABLE1 + "'"
resp = session.post(
f"{API_URL}/domain-types/host_config/collections/all",
headers={
"Content-Type": 'application/json',
},
json={
'folder':'/test/test',
'host_name': '%s' % HOSTNAME,
'attributes': {
"site": '%s' % SITE,
'alias': '%s' % SERVER_ALIAS,
'ipaddress': '%s' % SERVER_IP,
'tag_address_family': 'ip-v4-only',
# ---------> upper part by using the variables are ok if the variable is a value < ---------------------
#..... Problem.......
'tag_<VARIABLE1>': '<VARIABLE1>', # how is the right syntax to use the variable?
#f"{VARIABLE2}," # not working ==> how is the right syntax to use the variable?
#'%s' % VARIABLE2, # not working ==> how is the right syntax to use the variable?
#'' '%s' % VARIABLE2, # not working ==> how is the right syntax to use the variable?
#..........
"tag_environment": '%s' % ENVIRONMENT
},
},
)
if resp.status_code == 200:
pprint.pprint(resp.json())
elif resp.status_code == 204:
print("Done")
else:
raise RuntimeError(pprint.pformat(resp.json()))
import ast
# the variable ICTO_IMPORT_STRING_4 has such definition, defined dyn. at a previous step.
ICTO_IMPORT_STRING_4 = "'tag_cmdb_icto_number_icto_777': 'icto_777','tag_cmdb_icto_number_icto_99': 'icto_99','tag_cmdb_icto_number_icto_0': 'icto_0',"
# convert string into real dict:
import_string_as_dict = ast.literal_eval("{" + ICTO_IMPORT_STRING_4 + "}") # (1)
...
"labels": {
'tag_application_priority': '%s' % CMDB_APPLICATION_PRIORITY,
'tag_cluster_function': '%s' % CLUSTER_AND_CLUSTER_NODE,
'tag_department': '%s' % DEPARTMENT,
'tag_test': '%s' % XXXXX,
**import_string_as_dict # (2)
}
The line marked with (1) converts the stringICTO_IMPORT_STRING_4 into a real Python dictionary. To achieve this, it first surrounds the string with curlies {…} and then applies ast.literal_eval() to that string. For this to work you need to import the ast module.
The line marked with (2) flattens the dictionary into key/value pairs and adds them to the "labels" dictionary.
I find out what the mistake was in my environment, the variable IMPORT_STRING_DICT_1 was filled with tag-values which aren’t in my system. I replaced it and now it works like expected.
Here is my short example how my solution (with your help) works for me:
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed. Contact an admin if you think this should be re-opened.