def getIssuesByDevice(cred_params, token, hostname): apiServerId = cred_params['apiServerId'] servInfo = pythonutil.GetApiServerInfo(apiServerId) host_url = servInfo['endpoint'] + '/api' header = {'Authorization': token, 'Accept':'application/json'} full_url = host_url + "/Devices/" + hostname + "/Issues" querystring = {"isHostname":"true", "includeOccurences":"false", "includeLastAlert":"true", "includeRemediations":"false", "includeAppliedRemediations":"false", "includeExternalTextInSearch":"true", "page":"1", "pageSize":"100" } try: response = requests.get(full_url, headers=header, params=querystring, verify=False) if response.status_code == 200: result = response.json() # return result return response.text else: print(response.text) except Exception as e: print(str(e))
def extract_param(param): # The NetBrain initial parameters with customized fields. if isinstance(param, str): param = json.loads(param) #username, password, endpoint are build-in keywords in initial param. username = '' password = '' endpoint = '' #callParam is customized fields. api_param = {} apiServerId = '' servInfo = {} if 'apiServerId' in param: apiServerId = param['apiServerId'] servInfo = pythonutil.GetApiServerInfo(apiServerId) username = servInfo['username'] password = servInfo['password'] endpoint = servInfo['endpoint'] api_params = param['api_params'] else: username = param["username"] password = param["password"] endpoint = param["endpoint"] api_params = param['api_params'] return (endpoint, username, password, api_params)
def extract_param(param): if isinstance(param, str): param = json.loads(param) username = '' password = '' endpoint = '' callParam = {} apiServerId = '' servInfo = {} if 'apiServerId' in param: apiServerId = param['apiServerId'] servInfo = pythonutil.GetApiServerInfo(apiServerId) username = servInfo['username'] password = servInfo['password'] endpoint = servInfo['endpoint'] else: if 'username' in param: username = param['username'] if 'password' in param: password = param['password'] if 'endpoint' in param: endpoint = param['endpoint'] if 'query' in param: query = param['query'] if 'deviceName' in param: deviceName = param['deviceName'] return (username, password, endpoint, query, deviceName)
def GetWarrantyByID(token, sn, params): header = {'Authorization': token, 'Accept': 'application/json'} apiServerId = params['apiServerId'] servInfo = pythonutil.GetApiServerInfo(apiServerId) base_url = servInfo['endpoint'] full_url = base_url + sn status_code = 403 counter = 1 locks = Locks.getInstance() locks.update_lock(base_url) response = {} try: while status_code == 403 and counter < 4: locks.request_access(base_url) response = requests.get(full_url, headers=header, verify=False) status_code = response.status_code counter += 1 if status_code == 200: result = response.json() return result["EOXRecord"] else: print("Get Token Failed with API Response: " + response.text + " -- And API Header: " + response.headers) except Exception as e: print(str(e))
def GetTokenClient(params, full_url, header, payload_id, payload_secret): apiServerId = params['apiServerId'] servInfo = pythonutil.GetApiServerInfo(apiServerId) client_id = servInfo['username'] client_secret = servInfo['password'] payload = payload_id + client_id + payload_secret + client_secret #predefine the status code for lock function. status_code = 403 #define the initial value for re-call counter. counter = 1 #initial the lock function locks = Locks.getInstance() #apply which url should be locked in current call. locks.update_lock(full_url) response = {} try: #provide a while loop when we face conflict and with three times re-call. while status_code == 403 and counter < 4: # apply the lock to token API call. locks.request_access(full_url) response = requests.post(full_url, data=payload, headers=header, verify=False) status_code = response.status_code counter += 1 if status_code == 200: result = response.json() token = result["token_type"] + " " + result["access_token"] return token else: print("Get Token Failed with API Response: " + response.text + " -- And API Header: " + response.headers) except Exception as e: print(str(e))
def extract_param(param): # The NetBrain initial parameters with customized fields. if isinstance(param, str): param = json.loads(param) #username, password, endpoint are build-in keywords in initial param. username = '' password = '' endpoint = '' #callParam is customized fields. callParam = {} apiServerId = '' servInfo = {} if 'apiServerId' in param: apiServerId = param['apiServerId'] servInfo = pythonutil.GetApiServerInfo(apiServerId) username = servInfo['username'] password = servInfo['password'] endpoint = servInfo['endpoint'] else: if 'username' in param: username = param['username'] if 'password' in param: password = param['password'] if 'endpoint' in param: endpoint = param['endpoint'] if 'query' in param: query = param['query'] if 'deviceName' in param: deviceName = param['deviceName'] return (username, password, endpoint, query, deviceName)
def getToken(cred_params): apiServerId = cred_params['apiServerId'] servInfo = pythonutil.GetApiServerInfo(apiServerId) username = servInfo['username'] password = servInfo['password'] host_url = servInfo['endpoint'] + '/api' header = {"Content-Type": "application/x-www-form-urlencoded", 'Accept':'application/json'} full_url = host_url + "/token" payload = "username="******"&password="******"&grant_type=password" try: response = requests.post(full_url, data=payload, headers=header, verify=False) if response.status_code == 200: result = response.json() #{'access_token': 'Kqpa99145ZHXMjYlcmf6Ht38SAhe', 'token_type': 'Bearer', 'expires_in': 3599} token = result["token_type"] + " " + result["access_token"] return token else: print(response.text) except Exception as e: print(str(e))