Général:FAQ API MAJ

De WikiFr_dbSQWare
Aller à : navigation, rechercher

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

  1 <?php
  2 
  3 $v_bearer_token = "";
  4 
  5 /**
  6 * f_api_request does API request
  7 * 
  8 * @param String $v_method : request method
  9 * @param String $v_url    : website url
 10 * @param Array $t_headers : JSON object headers 
 11 * @param Array $t_data    : JSON object data
 12 * @return $v_response     : API request return
 13 */
 14 function f_api_request($v_method, $v_url, $t_headers=[], $t_data='{}'){
 15  $v_curl = curl_init();
 16 
 17  curl_setopt_array($v_curl, array(
 18 	 CURLOPT_URL            => $v_url,
 19 	 CURLOPT_RETURNTRANSFER => true,
 20 	 CURLOPT_ENCODING       => '',
 21 	 CURLOPT_MAXREDIRS      => 10,
 22 	 CURLOPT_TIMEOUT        => 0,
 23 	 CURLOPT_FOLLOWLOCATION => true,
 24 	 CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
 25 	 CURLOPT_CUSTOMREQUEST  => $v_method,
 26 	 CURLOPT_POSTFIELDS     => $t_data,
 27 	 CURLOPT_HTTPHEADER     => $t_headers,
 28  ));
 29 
 30  $v_response = curl_exec($v_curl);
 31  return $v_response;
 32 }
 33 
 34 /**
 35 * f_get_bearer_token recovers bearer token
 36 * 
 37 * @param String $v_username : account username
 38 * @param String $v_password : account password
 39 * @return $v_bearer_token   : bearer token
 40 */
 41 function f_get_bearer_token($v_username, $v_password){
 42  $v_method  = "POST";
 43  $v_url     = "http://my_sqwareweb_url/lib/apiUpdate_login.php";
 44  $t_headers = array('Content-Type: application/json');
 45  $t_data    = json_encode(array("username" => $v_username, "password" => $v_password), JSON_FORCE_OBJECT); // Crendentials for connection
 46 
 47  $v_response = f_api_request($v_method, $v_url, $t_headers=$t_headers, $t_data=$t_data); // API request
 48  $v_json_response = json_decode($v_response, true);
 49  if(!array_key_exists("error_message", $v_json_response)){
 50 	 global $v_bearer_token;
 51 	 $v_bearer_token = $v_json_response['results'][0]['token']; // Get token response
 52 	 echo("Bearer token : $v_bearer_token");
 53 	 return 0;
 54  }
 55  else{
 56 	 $v_error_message = $v_json_response['error_message'];
 57 	 echo("Error message [$v_error_message]");
 58 	 return 1;
 59  }
 60 }
 61 
 62 /**
 63 * f_get_instance recovers instance object
 64 * 
 65 * @param String $v_dbalias      : dbalias name
 66 * @return $v_instance           : instance object
 67 */
 68 function f_get_instance($v_dbalias){
 69  global $v_bearer_token;
 70  $v_method  = "GET";
 71  $v_url     = "http://my_sqwareweb_url/lib/apiUpdate.php?dbalias=".$v_dbalias;
 72  $t_headers = array('Authorization: Bearer '.$v_bearer_token.'', 'Content-Type: application/json');
 73 
 74  $v_response = f_api_request($v_method, $v_url, $t_headers=$t_headers); // API request
 75  $v_json_response = json_decode($v_response, true);
 76  if(!array_key_exists("error_message", $v_json_response)){
 77 	 $v_instance = $v_json_response['results'][0]; // Get instance
 78 	 var_dump($v_instance);
 79 	 return 0;
 80  }
 81  else{
 82 	 $v_error_message = $v_json_response['error_message'];
 83 	 echo("Error message [$v_error_message]");
 84 	 return 1;
 85  }
 86 }
 87 
 88 /**
 89 * f_get_instances recovers instances objects
 90 * 
 91 * @return $v_instances          : instances objects
 92 */
 93 function f_get_instances(){
 94  global $v_bearer_token;
 95  $v_method  = "GET";
 96  $v_url     = "http://my_sqwareweb_url/lib/apiUpdate.php";
 97  $t_headers = array('Authorization: Bearer '.$v_bearer_token.'', 'Content-Type: application/json');
 98 
 99  $v_response = f_api_request($v_method, $v_url, $t_headers=$t_headers); # API request
100  $v_json_response = json_decode($v_response, true);
101  if(!array_key_exists("error_message", $v_json_response)){
102 	 $v_instance = $v_json_response['results']; // Get instances
103 	 var_dump($v_instance);
104 	 return 0;
105  }
106  else{
107 	 $v_error_message = $v_json_response['error_message'];
108 	 echo("Error message [$v_error_message]");
109 	 return 1;
110  }
111 }
112 
113 /**
114 * f_post_instance create new instance object
115 * 
116 * @param Array $t_data          : JSON object data
117 * @return Array mixed[]         : request response
118 */
119 function f_post_instance($t_data){
120  global $v_bearer_token;
121  $v_method  = "POST";
122  $v_url     = "http://my_sqwareweb_url/lib/apiUpdate.php";
123  $t_headers = array('Authorization: Bearer '.$v_bearer_token.'', 'Content-Type: application/json');
124  $t_data    = json_encode($t_data, JSON_FORCE_OBJECT);
125 
126  $v_response = f_api_request($v_method, $v_url, $t_headers=$t_headers, $t_data=$t_data); // API request
127  $v_json_response = json_decode($v_response, true);
128  if(!array_key_exists("error_message", $v_json_response)){
129 	 echo("Insert with success !");
130 	 return 0;
131  }
132  else{
133 	 $v_error_message = $v_json_response['error_message'];
134 	 echo("Error message [$v_error_message]");
135 	 return 1;
136  }
137 } 
138 
139 /**
140 * f_put_instance update instance object
141 * 
142 * @param String $v_dbalias      : dbalias name
143 * @param Array $t_data          : JSON object data
144 * @return Array mixed[]         : request response
145 */
146 function f_put_instance($v_dbalias, $t_data){
147  global $v_bearer_token;
148  $v_method  = "PUT";
149  $v_url      = "http://my_sqwareweb_url/lib/apiUpdate.php?dbalias=".$v_dbalias;
150  $t_headers = array('Authorization: Bearer '.$v_bearer_token.'', 'Content-Type: application/json');
151  $t_data    = json_encode($t_data, JSON_FORCE_OBJECT);
152 
153  $v_response = f_api_request($v_method, $v_url, $t_headers=$t_headers, $t_data=$t_data); // API request
154  $v_json_response = json_decode($v_response, true);
155  if(!array_key_exists("error_message", $v_json_response)){
156 	 echo("Update with success !");
157 	 return 0;
158  }
159  else{
160 	 $v_error_message = $v_json_response['error_message'];
161 	 echo("Error message [$v_error_message]");
162 	 return 1;
163  }
164 }
165 
166 function main(){
167  // Variables
168  $v_username = "toto";
169  $v_password = "toto";
170  $v_dbalias  = "toto";
171 
172  // Get bearer token
173  f_get_bearer_token($v_username, $v_password);
174 
175  // Post new instance
176  $t_data = array(
177 					 "dbalias"        => $v_dbalias,
178 					 "rdbmsname"      => "cassandra",
179 					 "virt_host_name" => "AA-TEST-API",
180 					 "host_name"      => "NPW",
181 					 "username"       => "system",
182 					 "port"           => "1430",
183 					 "comments"       => "desc IPS_A",
184 					 "contact"        => "OAY .L",
185 					 "status"         => "ON",
186 					 "client"         => "CUST2",
187 					 "env"            => "PRD3",
188 					 "globalhost"     => "A",
189 					 "custom1"        => "B",
190 					 "custom2"        => "C",
191 					 "dbaname"        => $v_username,
192 					 "comments_upd"   => "insert new instance by API"
193 				 );
194 
195  f_post_instance($t_data);
196 
197  // Get new instance
198  f_get_instance($v_dbalias);
199 
200  // Put new instance
201  $t_data = array(
202 					 "dbalias"        => $v_dbalias,
203 					 "rdbmsname"      => "mysql",
204 					 "virt_host_name" => "AA-TEST-API",
205 					 "host_name"      => "NPW",
206 					 "username"       => "system",
207 					 "port"           => "1430",
208 					 "comments"       => "desc IPS_A",
209 					 "contact"        => "OAY .L",
210 					 "status"         => "OFF",
211 					 "client"         => "CUST2",
212 					 "env"            => "PRD3",
213 					 "globalhost"     => "A",
214 					 "custom1"        => "B",
215 					 "custom2"        => "C",
216 					 "comments_upd"   => "update instance status by API"
217 				 );
218 
219  f_put_instance($v_dbalias, $t_data);
220 
221  // Get all instances
222  f_get_instances();
223 }
224 
225 main();
226 
227 ?>

Shell

  1 #!/bin/sh
  2 
  3 v_bearer_token=""
  4 v_tempfile=api_dbSQWare.tmp
  5 
  6 <<COMMENTS
  7  f_get_bearer_token recovers bearer token
  8 
  9  :param v_username : account username
 10  :param v_password : account password
 11  :param v_tempfile : temporary file
 12  :return           : bearer token
 13 COMMENTS
 14 f_get_bearer_token(){
 15  v_username="$1"
 16  v_password="$2"
 17  v_tempfile="$3"
 18 
 19  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
 20 if [ $(grep -c '"error_message"' $v_tempfile) -eq 0 ]
 21 then
 22 	 # Get bearer token
 23 	v_bearer_token=`cat $v_tempfile|grep '\"token\":\"' | cut -d'"' -f4`
 24 	 echo "Bearer token : $v_bearer_token"
 25 	return 0
 26 else
 27 	v_error_message=`cat $v_tempfile|grep '\"error_message\": \"' | cut -d'"' -f4`
 28 	 echo "Error message [$v_error_message]"
 29 	return 1
 30 fi
 31 }
 32 
 33 <<COMMENTS
 34  f_get_instance recovers instance object
 35 
 36  :param v_dbalias : dbalias name
 37  :return          : instance object
 38 COMMENTS
 39 f_get_instance(){
 40  v_dbalias="$1"
 41 
 42  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
 43 if [ $(grep -c '"error_message"' $v_tempfile) -eq 0 ]
 44 then
 45 	 # Get instance
 46 	 v_instance=`cat $v_tempfile|grep -A 1 '\"results\":'`
 47 	 echo "Instance : $v_instance"
 48 	return 0
 49 else
 50 	v_error_message=`cat $v_tempfile|grep '\"error_message\": \"' | cut -d'"' -f4`
 51 	 echo "Error message [$v_error_message]"
 52 	return 1
 53 fi
 54 }
 55 
 56 <<COMMENTS
 57  f_get_instances recovers instances objects
 58 
 59  :return : instances objects
 60 COMMENTS
 61 f_get_instances(){
 62  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
 63 if [ $(grep -c '"error_message"' $v_tempfile) -eq 0 ]
 64 then
 65 	# Get instances
 66 	v_instances=`cat $v_tempfile|grep -A 1 '\"results\":'`
 67 	 echo "Instances : $v_instances"
 68 	return 0
 69 else
 70 	 v_error_message=`cat $v_tempfile|grep '\"error_message\": \"' | cut -d'"' -f4`
 71 	 echo "Error message [$v_error_message]"
 72 	return 1
 73 fi
 74 }
 75 
 76 <<COMMENTS
 77  f_post_instance create new instance object
 78 
 79  :param t_data : JSON object data
 80  :return       : request response
 81 COMMENTS
 82 f_post_instance(){
 83  t_data="$1"
 84 
 85  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
 86 if [ $(grep -c '"error_message"' $v_tempfile) -eq 0 ]
 87 then
 88 	echo "Insert with success !"
 89 	return 0
 90 else
 91 	v_error_message=`cat $v_tempfile|grep '\"error_message\": \"' | cut -d'"' -f4`
 92 	 echo "Error message [$v_error_message]"
 93 	return 1
 94 fi
 95 }
 96 
 97 <<COMMENTS
 98  f_put_instance update instance object
 99 
100  :param v_dbalias : dbalias name
101  :param t_data    : JSON object data
102  :return          : request response
103 COMMENTS
104 f_put_instance(){
105  v_dbalias="$1"
106  t_data="$2"
107 
108  echo $t_data > test.tmp
109  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
110 if [ $(grep -c '"error_message"' $v_tempfile) -eq 0 ]
111 then
112 	echo "Update with success !"
113 	return 0
114 else
115 	v_error_message=`cat $v_tempfile|grep '\"error_message\": \"' | cut -d'"' -f4`
116 	 echo "Error message [$v_error_message]"
117 	return 1
118 fi
119 }
120 
121 main(){
122  # Variables
123  v_username="toto";
124  v_password="toto";
125  v_dbalias="toto";
126 
127  # Get bearer token
128  f_get_bearer_token $v_username $v_password $v_tempfile
129 
130  # Post new instance
131  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"}'
132  f_post_instance "$t_data"
133 
134  # Get new instance
135  f_get_instance $v_dbalias
136 
137  # Put new instance
138  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"}'
139  f_put_instance $v_dbalias "$t_data"
140 
141  # Get all instances
142  f_get_instances
143 }
144 
145 main