Général:FAQ API MAJ : Différence entre versions
(→Limites de cette section) |
(→Python) |
||
Ligne 15 : | Ligne 15 : | ||
===Python=== | ===Python=== | ||
− | + | <syntaxhighlight lang="python" line> | |
− | + | import requests | |
− | + | import json | |
− | + | ||
− | + | v_bearer_token = "" | |
− | + | ||
− | + | """ | |
− | + | f_api_request does API request | |
− | + | ||
− | + | :param v_method : request method | |
− | + | :param v_url : website url | |
− | + | :param t_headers : JSON object headers | |
− | + | :param t_data : JSON object data | |
− | + | :return : API request return | |
− | + | """ | |
− | + | def f_api_request(v_method, v_url, t_headers={}, t_data={}): | |
− | + | v_response = requests.request(v_method, v_url, headers=t_headers, data=t_data) # API request | |
− | + | return v_response | |
− | + | ||
− | + | """ | |
− | + | f_get_bearer_token recovers bearer token | |
− | + | ||
− | + | :param v_username : account username | |
− | + | :param v_password : account password | |
− | + | :return : bearer token | |
− | + | """ | |
− | + | def f_get_bearer_token(v_username, v_password): | |
− | + | v_method = "POST" | |
− | + | v_url = "http://my_sqwareweb_url/lib/apiUpdate_login.php" | |
− | + | t_headers = {'Content-Type': 'application/json'} | |
− | + | t_data = json.dumps({"username":v_username,"password":v_password}) # Crendentials for connection | |
− | + | ||
− | + | v_response = f_api_request(v_method, v_url, t_headers=t_headers, t_data=t_data) # API request | |
− | + | v_json_response = json.loads(v_response.text) | |
− | + | if "error_message" not in v_json_response: | |
− | + | global v_bearer_token | |
− | + | v_bearer_token = v_json_response['results'][0]['token'] # Get token response | |
− | + | print("Bearer token : "+v_bearer_token) | |
− | + | return 1 | |
− | + | else: | |
− | + | v_error_message = v_json_response['error_message'] | |
− | + | print("Error message ["+v_error_message+"]") | |
− | + | return 0 | |
− | + | ||
− | + | """ | |
− | + | f_get_instance recovers instance object | |
− | + | ||
− | + | :param v_dbalias : dbalias name | |
− | + | :return : instance object | |
− | + | """ | |
− | + | def f_get_instance(v_dbalias): | |
− | + | v_method = "GET" | |
− | + | v_url = "http://my_sqwareweb_url/lib/apiUpdate.php?dbalias="+v_dbalias | |
− | + | t_headers = {'Authorization':'Bearer '+v_bearer_token, 'Content-Type': 'application/json'} | |
− | + | ||
− | + | v_response = f_api_request(v_method, v_url, t_headers=t_headers) # API request | |
− | + | v_json_response = json.loads(v_response.text) | |
− | + | if "error_message" not in v_json_response: | |
− | + | v_instance = v_json_response['results'][0] # Get instance | |
− | + | print(v_instance) | |
− | + | return 1 | |
− | + | else: | |
− | + | v_error_message = v_json_response['error_message'] | |
− | + | print("Error message ["+v_error_message+"]") | |
− | + | return 0 | |
− | + | ||
− | + | """ | |
− | + | f_get_instances recovers instances objects | |
− | + | ||
− | + | :return : instances objects | |
− | + | """ | |
− | + | def f_get_instances(): | |
− | + | v_method = "GET" | |
− | + | v_url = "http://my_sqwareweb_url/lib/apiUpdate.php" | |
− | + | t_headers = {'Authorization':'Bearer '+v_bearer_token, 'Content-Type': 'application/json'} | |
− | + | ||
− | + | v_response = f_api_request(v_method, v_url, t_headers=t_headers) # API request | |
− | + | v_json_response = json.loads(v_response.text) | |
− | + | if "error_message" not in v_json_response: | |
− | + | v_instances = v_json_response['results'] # Get instances | |
− | + | print(v_instances) | |
− | + | return 1 | |
− | + | else: | |
− | + | v_error_message = v_json_response['error_message'] | |
− | + | print("Error message ["+v_error_message+"]") | |
− | + | return 0 | |
− | + | ||
− | + | """ | |
− | + | f_post_instance create new instance object | |
− | + | ||
− | + | :param t_data : JSON object data | |
− | + | :return : request response | |
− | + | """ | |
− | + | def f_post_instance(t_data): | |
− | + | v_method = "POST" | |
− | + | v_url = "http://my_sqwareweb_url/lib/apiUpdate.php" | |
− | + | t_headers = {'Authorization':'Bearer '+v_bearer_token, 'Content-Type': 'application/json'} | |
− | + | t_data = json.dumps(t_data) | |
− | + | ||
− | + | v_response = f_api_request(v_method, v_url, t_headers=t_headers, t_data=t_data) # API request | |
− | + | v_json_response = json.loads(v_response.text) | |
− | + | if "error_message" not in v_json_response: | |
− | + | print("Insert with success !") | |
− | + | return 1 | |
− | + | else: | |
− | + | v_error_message = v_json_response['error_message'] | |
− | + | print("Error message ["+v_error_message+"]") | |
− | + | return 0 | |
− | + | ||
− | + | """ | |
− | + | f_put_instance update instance object | |
− | + | ||
− | + | :param v_dbalias : dbalias name | |
− | + | :param t_data : JSON object data | |
− | + | :return : request response | |
− | + | """ | |
− | + | def f_put_instance(v_dbalias, t_data): | |
− | + | v_method = "PUT" | |
− | + | v_url = "http://my_sqwareweb_url/lib/apiUpdate.php?dbalias="+v_dbalias | |
− | + | t_headers = {'Authorization':'Bearer '+v_bearer_token, 'Content-Type': 'application/json'} | |
− | + | t_data = json.dumps(t_data) | |
− | + | ||
− | + | v_response = f_api_request(v_method, v_url, t_headers=t_headers, t_data=t_data) # API request | |
− | + | v_json_response = json.loads(v_response.text) | |
− | + | if "error_message" not in v_json_response: | |
− | + | print("Update with success !") | |
− | + | return 1 | |
− | + | else: | |
− | + | v_error_message = v_json_response['error_message'] | |
− | + | print("Error message ["+v_error_message+"]") | |
− | + | return 0 | |
− | + | ||
− | + | def main(): | |
− | + | # Variables | |
− | + | v_username = "toto" | |
− | + | v_password = "toto" | |
− | + | v_dbalias = "toto" | |
− | + | ||
− | + | # Get bearer token | |
− | + | f_get_bearer_token(v_username, v_password) | |
− | + | ||
− | + | # Post new instance | |
− | + | t_data = { | |
− | + | "dbalias" :v_dbalias, | |
− | + | "rdbmsname" :"cassandra", | |
− | + | "virt_host_name":"AA-TEST-API", | |
− | + | "host_name" :"NPW", | |
− | + | "username" :"system", | |
− | + | "port" :"1430", | |
− | + | "comments" :"desc IPS_A", | |
− | + | "contact" :"OAY .L", | |
− | + | "status" :"ON", | |
− | + | "client" :"CUST2", | |
− | + | "env" :"PRD3", | |
− | + | "globalhost" :"A", | |
− | + | "custom1" :"B", | |
− | + | "custom2" :"C", | |
− | + | "dbaname" :v_username, | |
− | + | "comments_upd" :"insert new instance by API" | |
− | + | } | |
− | + | ||
− | + | f_post_instance(t_data) | |
− | + | ||
− | + | # Get new instance | |
− | + | f_get_instance(v_dbalias) | |
− | + | ||
− | + | # Put new instance | |
− | + | t_data = { | |
− | + | "rdbmsname" :"mysql", | |
− | + | "virt_host_name":"AA-TEST-API", | |
− | + | "host_name" :"NPW", | |
− | + | "username" :"system", | |
− | + | "port" :"1430", | |
− | + | "comments" :"desc IPS_A", | |
− | + | "contact" :"OAY .L", | |
− | + | "status" :"OFF", | |
− | + | "client" :"CUST2", | |
− | + | "env" :"PRD3", | |
− | + | "globalhost" :"A", | |
− | + | "custom1" :"B", | |
− | + | "custom2" :"C", | |
− | + | "comments_upd" : "update instance status by API" | |
− | + | } | |
− | + | ||
− | + | f_put_instance(v_dbalias, t_data) | |
− | + | ||
− | + | # Get all instances | |
− | + | f_get_instances() | |
− | + | ||
− | + | if __name__ == '__main__': | |
+ | main() | ||
+ | </syntaxhighlight> | ||
===PHP=== | ===PHP=== |
Version du 3 octobre 2023 à 22:29
Sommaire
Généralités
Limites de cette section
Cette section n’a pas la prétention de traiter tous les cas possibles d'utilisation de l'API de mise à jour du référentiel.
Vous devez être à l'aise avec le concept des API et la gestion de token, si ce n'est pas le cas, faites appel au support.
Attention, vous allez mettre à jour la table du référentiel général, insert et/ou update, vous devez savoir ce que vous faites !
Paramétrage préalable
Afin de pouvoir utiliser l'API "update", vous devez au préalable avoir paramétré un login avec le type d'authentification "internal_api_update".
Exemples d'utilisation
Vous devez adapter les exemples suivants avec vos propres données (URL, user, password, ...)
Ceci représente une base pour du Python, PHP et shell.
Vous pouvez bien évidemment utiliser le langage de votre choix.
Python
1 import requests
2 import json
3
4 v_bearer_token = ""
5
6 """
7 f_api_request does API request
8
9 :param v_method : request method
10 :param v_url : website url
11 :param t_headers : JSON object headers
12 :param t_data : JSON object data
13 :return : API request return
14 """
15 def f_api_request(v_method, v_url, t_headers={}, t_data={}):
16 v_response = requests.request(v_method, v_url, headers=t_headers, data=t_data) # API request
17 return v_response
18
19 """
20 f_get_bearer_token recovers bearer token
21
22 :param v_username : account username
23 :param v_password : account password
24 :return : bearer token
25 """
26 def f_get_bearer_token(v_username, v_password):
27 v_method = "POST"
28 v_url = "http://my_sqwareweb_url/lib/apiUpdate_login.php"
29 t_headers = {'Content-Type': 'application/json'}
30 t_data = json.dumps({"username":v_username,"password":v_password}) # Crendentials for connection
31
32 v_response = f_api_request(v_method, v_url, t_headers=t_headers, t_data=t_data) # API request
33 v_json_response = json.loads(v_response.text)
34 if "error_message" not in v_json_response:
35 global v_bearer_token
36 v_bearer_token = v_json_response['results'][0]['token'] # Get token response
37 print("Bearer token : "+v_bearer_token)
38 return 1
39 else:
40 v_error_message = v_json_response['error_message']
41 print("Error message ["+v_error_message+"]")
42 return 0
43
44 """
45 f_get_instance recovers instance object
46
47 :param v_dbalias : dbalias name
48 :return : instance object
49 """
50 def f_get_instance(v_dbalias):
51 v_method = "GET"
52 v_url = "http://my_sqwareweb_url/lib/apiUpdate.php?dbalias="+v_dbalias
53 t_headers = {'Authorization':'Bearer '+v_bearer_token, 'Content-Type': 'application/json'}
54
55 v_response = f_api_request(v_method, v_url, t_headers=t_headers) # API request
56 v_json_response = json.loads(v_response.text)
57 if "error_message" not in v_json_response:
58 v_instance = v_json_response['results'][0] # Get instance
59 print(v_instance)
60 return 1
61 else:
62 v_error_message = v_json_response['error_message']
63 print("Error message ["+v_error_message+"]")
64 return 0
65
66 """
67 f_get_instances recovers instances objects
68
69 :return : instances objects
70 """
71 def f_get_instances():
72 v_method = "GET"
73 v_url = "http://my_sqwareweb_url/lib/apiUpdate.php"
74 t_headers = {'Authorization':'Bearer '+v_bearer_token, 'Content-Type': 'application/json'}
75
76 v_response = f_api_request(v_method, v_url, t_headers=t_headers) # API request
77 v_json_response = json.loads(v_response.text)
78 if "error_message" not in v_json_response:
79 v_instances = v_json_response['results'] # Get instances
80 print(v_instances)
81 return 1
82 else:
83 v_error_message = v_json_response['error_message']
84 print("Error message ["+v_error_message+"]")
85 return 0
86
87 """
88 f_post_instance create new instance object
89
90 :param t_data : JSON object data
91 :return : request response
92 """
93 def f_post_instance(t_data):
94 v_method = "POST"
95 v_url = "http://my_sqwareweb_url/lib/apiUpdate.php"
96 t_headers = {'Authorization':'Bearer '+v_bearer_token, 'Content-Type': 'application/json'}
97 t_data = json.dumps(t_data)
98
99 v_response = f_api_request(v_method, v_url, t_headers=t_headers, t_data=t_data) # API request
100 v_json_response = json.loads(v_response.text)
101 if "error_message" not in v_json_response:
102 print("Insert with success !")
103 return 1
104 else:
105 v_error_message = v_json_response['error_message']
106 print("Error message ["+v_error_message+"]")
107 return 0
108
109 """
110 f_put_instance update instance object
111
112 :param v_dbalias : dbalias name
113 :param t_data : JSON object data
114 :return : request response
115 """
116 def f_put_instance(v_dbalias, t_data):
117 v_method = "PUT"
118 v_url = "http://my_sqwareweb_url/lib/apiUpdate.php?dbalias="+v_dbalias
119 t_headers = {'Authorization':'Bearer '+v_bearer_token, 'Content-Type': 'application/json'}
120 t_data = json.dumps(t_data)
121
122 v_response = f_api_request(v_method, v_url, t_headers=t_headers, t_data=t_data) # API request
123 v_json_response = json.loads(v_response.text)
124 if "error_message" not in v_json_response:
125 print("Update with success !")
126 return 1
127 else:
128 v_error_message = v_json_response['error_message']
129 print("Error message ["+v_error_message+"]")
130 return 0
131
132 def main():
133 # Variables
134 v_username = "toto"
135 v_password = "toto"
136 v_dbalias = "toto"
137
138 # Get bearer token
139 f_get_bearer_token(v_username, v_password)
140
141 # Post new instance
142 t_data = {
143 "dbalias" :v_dbalias,
144 "rdbmsname" :"cassandra",
145 "virt_host_name":"AA-TEST-API",
146 "host_name" :"NPW",
147 "username" :"system",
148 "port" :"1430",
149 "comments" :"desc IPS_A",
150 "contact" :"OAY .L",
151 "status" :"ON",
152 "client" :"CUST2",
153 "env" :"PRD3",
154 "globalhost" :"A",
155 "custom1" :"B",
156 "custom2" :"C",
157 "dbaname" :v_username,
158 "comments_upd" :"insert new instance by API"
159 }
160
161 f_post_instance(t_data)
162
163 # Get new instance
164 f_get_instance(v_dbalias)
165
166 # Put new instance
167 t_data = {
168 "rdbmsname" :"mysql",
169 "virt_host_name":"AA-TEST-API",
170 "host_name" :"NPW",
171 "username" :"system",
172 "port" :"1430",
173 "comments" :"desc IPS_A",
174 "contact" :"OAY .L",
175 "status" :"OFF",
176 "client" :"CUST2",
177 "env" :"PRD3",
178 "globalhost" :"A",
179 "custom1" :"B",
180 "custom2" :"C",
181 "comments_upd" : "update instance status by API"
182 }
183
184 f_put_instance(v_dbalias, t_data)
185
186 # Get all instances
187 f_get_instances()
188
189 if __name__ == '__main__':
190 main()
PHP
<?php $v_bearer_token = ""; /** * f_api_request does API request * * @param String $v_method : request method * @param String $v_url : website url * @param Array $t_headers : JSON object headers * @param Array $t_data : JSON object data * @return $v_response : API request return */ function f_api_request($v_method, $v_url, $t_headers=[], $t_data='{}'){ $v_curl = curl_init(); curl_setopt_array($v_curl, array( CURLOPT_URL => $v_url, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => , CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => $v_method, CURLOPT_POSTFIELDS => $t_data, CURLOPT_HTTPHEADER => $t_headers, )); $v_response = curl_exec($v_curl); return $v_response; } /** * f_get_bearer_token recovers bearer token * * @param String $v_username : account username * @param String $v_password : account password * @return $v_bearer_token : bearer token */ function f_get_bearer_token($v_username, $v_password){ $v_method = "POST"; $v_url = "http://my_sqwareweb_url/lib/apiUpdate_login.php"; $t_headers = array('Content-Type: application/json'); $t_data = json_encode(array("username" => $v_username, "password" => $v_password), JSON_FORCE_OBJECT); // Crendentials for connection $v_response = f_api_request($v_method, $v_url, $t_headers=$t_headers, $t_data=$t_data); // API request $v_json_response = json_decode($v_response, true); if(!array_key_exists("error_message", $v_json_response)){ global $v_bearer_token; $v_bearer_token = $v_json_response['results'][0]['token']; // Get token response echo("Bearer token : $v_bearer_token"); return 0; } else{ $v_error_message = $v_json_response['error_message']; echo("Error message [$v_error_message]"); return 1; } } /** * f_get_instance recovers instance object * * @param String $v_dbalias : dbalias name * @return $v_instance : instance object */ function f_get_instance($v_dbalias){ global $v_bearer_token; $v_method = "GET"; $v_url = "http://my_sqwareweb_url/lib/apiUpdate.php?dbalias=".$v_dbalias; $t_headers = array('Authorization: Bearer '.$v_bearer_token., 'Content-Type: application/json'); $v_response = f_api_request($v_method, $v_url, $t_headers=$t_headers); // API request $v_json_response = json_decode($v_response, true); if(!array_key_exists("error_message", $v_json_response)){ $v_instance = $v_json_response['results'][0]; // Get instance var_dump($v_instance); return 0; } else{ $v_error_message = $v_json_response['error_message']; echo("Error message [$v_error_message]"); return 1; } } /** * f_get_instances recovers instances objects * * @return $v_instances : instances objects */ function f_get_instances(){ global $v_bearer_token; $v_method = "GET"; $v_url = "http://my_sqwareweb_url/lib/apiUpdate.php"; $t_headers = array('Authorization: Bearer '.$v_bearer_token., 'Content-Type: application/json'); $v_response = f_api_request($v_method, $v_url, $t_headers=$t_headers); # API request $v_json_response = json_decode($v_response, true); if(!array_key_exists("error_message", $v_json_response)){ $v_instance = $v_json_response['results']; // Get instances var_dump($v_instance); return 0; } else{ $v_error_message = $v_json_response['error_message']; echo("Error message [$v_error_message]"); return 1; } } /** * f_post_instance create new instance object * * @param Array $t_data : JSON object data * @return Array mixed[] : request response */ function f_post_instance($t_data){ global $v_bearer_token; $v_method = "POST"; $v_url = "http://my_sqwareweb_url/lib/apiUpdate.php"; $t_headers = array('Authorization: Bearer '.$v_bearer_token., 'Content-Type: application/json'); $t_data = json_encode($t_data, JSON_FORCE_OBJECT); $v_response = f_api_request($v_method, $v_url, $t_headers=$t_headers, $t_data=$t_data); // API request $v_json_response = json_decode($v_response, true); if(!array_key_exists("error_message", $v_json_response)){ echo("Insert with success !"); return 0; } else{ $v_error_message = $v_json_response['error_message']; echo("Error message [$v_error_message]"); return 1; } } /** * f_put_instance update instance object * * @param String $v_dbalias : dbalias name * @param Array $t_data : JSON object data * @return Array mixed[] : request response */ function f_put_instance($v_dbalias, $t_data){ global $v_bearer_token; $v_method = "PUT"; $v_url = "http://my_sqwareweb_url/lib/apiUpdate.php?dbalias=".$v_dbalias; $t_headers = array('Authorization: Bearer '.$v_bearer_token., 'Content-Type: application/json'); $t_data = json_encode($t_data, JSON_FORCE_OBJECT); $v_response = f_api_request($v_method, $v_url, $t_headers=$t_headers, $t_data=$t_data); // API request $v_json_response = json_decode($v_response, true); if(!array_key_exists("error_message", $v_json_response)){ echo("Update with success !"); return 0; } else{ $v_error_message = $v_json_response['error_message']; echo("Error message [$v_error_message]"); return 1; } } function main(){ // Variables $v_username = "toto"; $v_password = "toto"; $v_dbalias = "toto"; // Get bearer token f_get_bearer_token($v_username, $v_password); // Post new instance $t_data = array( "dbalias" => $v_dbalias, "rdbmsname" => "cassandra", "virt_host_name" => "AA-TEST-API", "host_name" => "NPW", "username" => "system", "port" => "1430", "comments" => "desc IPS_A", "contact" => "OAY .L", "status" => "ON", "client" => "CUST2", "env" => "PRD3", "globalhost" => "A", "custom1" => "B", "custom2" => "C", "dbaname" => $v_username, "comments_upd" => "insert new instance by API" ); f_post_instance($t_data); // Get new instance f_get_instance($v_dbalias); // Put new instance $t_data = array( "dbalias" => $v_dbalias, "rdbmsname" => "mysql", "virt_host_name" => "AA-TEST-API", "host_name" => "NPW", "username" => "system", "port" => "1430", "comments" => "desc IPS_A", "contact" => "OAY .L", "status" => "OFF", "client" => "CUST2", "env" => "PRD3", "globalhost" => "A", "custom1" => "B", "custom2" => "C", "comments_upd" => "update instance status by API" ); f_put_instance($v_dbalias, $t_data); // Get all instances f_get_instances(); } main(); ?>
Shell
#!/bin/sh v_bearer_token="" v_tempfile=api_dbSQWare.tmp <<COMMENTS f_get_bearer_token recovers bearer token :param v_username : account username :param v_password : account password :param v_tempfile : temporary file :return : bearer token COMMENTS f_get_bearer_token(){ v_username="$1" v_password="$2" v_tempfile="$3" curl --location --request POST "http://my_sqwareweb_url/lib/apiUpdate_login.php" -H "Content-Type:application/json" -d '{"username":"'$v_username'","password":"'$v_password'"}' > $v_tempfile 2>$v_tempfile.err if [ $(grep -c '"error_message"' $v_tempfile) -eq 0 ] then # Get bearer token v_bearer_token=`cat $v_tempfile|grep '\"token\":\"' | cut -d'"' -f4` echo "Bearer token : $v_bearer_token" return 0 else v_error_message=`cat $v_tempfile|grep '\"error_message\": \"' | cut -d'"' -f4` echo "Error message [$v_error_message]" return 1 fi } <<COMMENTS f_get_instance recovers instance object :param v_dbalias : dbalias name :return : instance object COMMENTS f_get_instance(){ v_dbalias="$1" curl --location --request GET "http://my_sqwareweb_url/lib/apiUpdate.php?dbalias=$v_dbalias" -H "Authorization: Bearer $v_bearer_token" -H "Content-Type:application/json" > $v_tempfile 2>$v_tempfile.err if [ $(grep -c '"error_message"' $v_tempfile) -eq 0 ] then # Get instance v_instance=`cat $v_tempfile|grep -A 1 '\"results\":'` echo "Instance : $v_instance" return 0 else v_error_message=`cat $v_tempfile|grep '\"error_message\": \"' | cut -d'"' -f4` echo "Error message [$v_error_message]" return 1 fi } <<COMMENTS f_get_instances recovers instances objects :return : instances objects COMMENTS f_get_instances(){ curl --location --request GET "http://my_sqwareweb_url/lib/apiUpdate.php" -H "Authorization: Bearer $v_bearer_token" -H "Content-Type:application/json" > $v_tempfile 2>$v_tempfile.err if [ $(grep -c '"error_message"' $v_tempfile) -eq 0 ] then # Get instances v_instances=`cat $v_tempfile|grep -A 1 '\"results\":'` echo "Instances : $v_instances" return 0 else v_error_message=`cat $v_tempfile|grep '\"error_message\": \"' | cut -d'"' -f4` echo "Error message [$v_error_message]" return 1 fi } <<COMMENTS f_post_instance create new instance object :param t_data : JSON object data :return : request response COMMENTS f_post_instance(){ t_data="$1" curl --location --request POST "http://my_sqwareweb_url/lib/apiUpdate.php" -H "Authorization: Bearer $v_bearer_token" -H "Content-Type:application/json" -d "$t_data" > $v_tempfile 2>$v_tempfile.err if [ $(grep -c '"error_message"' $v_tempfile) -eq 0 ] then echo "Insert with success !" return 0 else v_error_message=`cat $v_tempfile|grep '\"error_message\": \"' | cut -d'"' -f4` echo "Error message [$v_error_message]" return 1 fi } <<COMMENTS f_put_instance update instance object :param v_dbalias : dbalias name :param t_data : JSON object data :return : request response COMMENTS f_put_instance(){ v_dbalias="$1" t_data="$2" echo $t_data > test.tmp curl --location --request PUT "http://my_sqwareweb_url/lib/apiUpdate.php?dbalias=$v_dbalias" -H "Authorization: Bearer $v_bearer_token" -H "Content-Type:application/json" -d "$t_data" > $v_tempfile 2>$v_tempfile.err if [ $(grep -c '"error_message"' $v_tempfile) -eq 0 ] then echo "Update with success !" return 0 else v_error_message=`cat $v_tempfile|grep '\"error_message\": \"' | cut -d'"' -f4` echo "Error message [$v_error_message]" return 1 fi } main(){ # Variables v_username="toto"; v_password="toto"; v_dbalias="toto"; # Get bearer token f_get_bearer_token $v_username $v_password $v_tempfile # Post new instance t_data='{"dbalias":"'$v_dbalias'","rdbmsname":"cassandra","virt_host_name":"AA-TEST-API","host_name":"NPW","username":"system","port":"1430","comments":"desc IPS_A","contact":"OAY .L","status":"ON","client":"CUST2","env":"PRD3","globalhost":"A","custom1":"B","custom2":"C","dbaname":"'$v_username'","comments_upd":"insert new instance by API"}' f_post_instance "$t_data" # Get new instance f_get_instance $v_dbalias # Put new instance t_data='{"rdbmsname":"mysql","virt_host_name":"AA-TEST-API","host_name":"NPW","username":"system","port":"1430","comments":"desc IPS_A","contact":"OAY .L","status":"OFF","client":"CUST2","env":"PRD3","globalhost":"A","custom1":"B","custom2":"C","comments_upd":"update instance status by API"}' f_put_instance $v_dbalias "$t_data" # Get all instances f_get_instances } main