def create_project(project_name): token = get_auth_token() url = create_url(path="pnp-project") print("POST URL %s" % url) print("Creating project %s" % project_name) headers = { 'x-auth-token': token['token'], 'content-type': 'application/json' } body = [{"siteName": project_name}] try: response = requests.post(url, headers=headers, data=json.dumps(body), verify=False) except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) taskid = response.json()['response']['taskId'] print("Waiting for Task %s" % taskid) task_result = wait_on_task(taskid, token) return task_result
def post_and_wait(url, data): token = get_auth_token() url = create_url(path=url) headers = { 'x-auth-token': token['token'], 'content-type': 'application/json' } try: response = requests.post(url, headers=headers, data=json.dumps(data), verify=False) except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) logging.debug("POST Result{}".format(json.dumps(response.json()))) if 'message' in response.json()['response']: return response.json() taskid = response.json()['response']['taskId'] print("Waiting for Task %s" % taskid) task_result = wait_on_task(taskid, token) return task_result
def project_name_to_id(project_name): token = get_auth_token() url = create_url(path="pnp-project") headers= { 'x-auth-token': token['token'], 'content-type' : 'application/json'} # look for project by name. need to get the project id search_url = url + '?siteName=%s&offset=1&limit=10' %project_name print("GET: %s" % search_url) try: response = requests.get(search_url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) # check response.json() for values # no match # multi match # single match matches = response.json()['response'] if len(matches) <1: raise ValueError('No matches found for %s' % project_name) elif len(matches) > 1: raise ValueError("multiple matches found for %s" % project_name) else: project_id = matches[0]['id'] return project_id
def delete_project(project_name): token = get_auth_token() url = create_url(path="pnp-project") headers = { 'x-auth-token': token['token'], 'content-type': 'application/json' } try: project_id = project_name_to_id(project_name) except ValueError as v: print("NO project: %s" % project_name) return {} delete_url = url + '/%s?deleteRule=1&deleteDevice=1' % project_id print("DELETE: %s" % delete_url) try: response = requests.delete(delete_url, headers=headers, verify=False) response.raise_for_status() except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) taskid = response.json()['response']['taskId'] print("Waiting for Task %s" % taskid) task_result = wait_on_task(taskid, token) return task_result
def wait_on_tasks(tasklist): response_list = [] token = get_auth_token() for task in tasklist: task_result = wait_on_task(task, token, timeout=90, retry_interval=2) response_list.append(task_result) return response_list
def deploy_and_wait(url, data): token = get_auth_token() url = create_url(path=url) print(url) headers= { 'x-auth-token': token['token'], 'content-type' : 'application/json'} try: response = requests.post(url, headers=headers, data=json.dumps(data), verify=False) response.raise_for_status() except requests.exceptions.RequestException as cerror: print ("Error processing request", cerror) raise print ("Response", response.json()) # this has changed in 1.2 ## deploymentId = response.json()['deploymentId'].split(":")[-1].strip() # look for the already deployed issue if "already deployed" in deploymentId: print("Error:", deploymentId) raise ValueError(deploymentId) applicable = response.json()['deploymentId'].split(":")[1].strip() # look for device type issues if 'nonApp' in applicable: print("Error: {}".format(response.json()['deploymentId'])) raise ValueError("Error: {}".format(response.json()['deploymentId'])) print ("waiting for deploymentId", deploymentId) start_time = time.time() retry_interval = 2 timeout = 10 * retry_interval while True: # changed in 1.2 response = get_url('template-programmer/template/deploy/status/' + deploymentId) print (response) if response["endTime"] != '': break else: if timeout and (start_time + timeout < time.time()): raise DeploymentTimeoutError("Task %s did not complete within the specified timeout " "(%s seconds)" % (deploymentId, timeout)) print("Task=%s has not completed yet. Sleeping %s seconds..." % (deploymentId, retry_interval)) time.sleep(retry_interval) if response["status"] != "SUCCESS": raise DeploymentError("Task %s had status: %s" % (deploymentId, response['status'])) #print (response) return response
def list_files(namespace): token = get_auth_token() url = create_url(path="file/namespace/%s" % namespace) print("Getting %s" % url) headers = {'x-auth-token': token['token']} try: response = requests.get(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) return response.json()
def post_multipart_file(url, files): token = get_auth_token() url = create_iox_url(path=url) headers= { 'x-auth-token': token['token']} print("url - %s" % url) try: response = requests.post(url, files=files, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print ("Error processing request", cerror) sys.exit(1) resp_json = json.dumps(response.json()) return resp_json
def list_project_detail(projectname): token = get_auth_token() project_id = project_name_to_id(projectname) url = create_url(path="pnp-project/%s/device" % project_id) print("Getting %s" % url) headers= { 'x-auth-token': token['token']} try: response = requests.get(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) return response.json()
def get_url(url): url = create_url(path=url) print(url) token = get_auth_token() headers = {'X-auth-token': token['token']} try: response = requests.get(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) return response.json()
def delete_file(namespace, fileid): token = get_auth_token() url = create_url(path="file/%s" % fileid) print("DELETE %s" % url) headers= { 'x-auth-token': token['token']} try: response = requests.delete(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) return response.json()
def get_url(url, extraHeaders={}): url = create_url(path=url) print(url) token = get_auth_token() headers = copy.deepcopy(extraHeaders) headers.update({'X-auth-token': token['token'], "__runsync": "true"}) try: response = requests.get(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) return response.json()
def post_req_iox(url, data): if FAKE: return fake_post[url] token = get_auth_token() url = create_iox_url(path=url) headers= { 'x-auth-token': token['token'], 'content-type' : 'application/json'} try: response = requests.post(url, headers=headers, data=json.dumps(data), verify=False) except requests.exceptions.RequestException as cerror: print ("Error processing request", cerror) sys.exit(1) print (response.json())
def delete_sync(url): token = get_auth_token() url = create_url(path=url) headers = { 'x-auth-token': token['token'], 'content-type': 'application/json', '__runsync': 'true' } logging.debug(headers) try: response = requests.delete(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) return response.json()
def delete(url): token = get_auth_token() url = create_url(path=url) headers = { 'x-auth-token': token['token'], 'content-type': 'application/json' } try: response = requests.delete(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) taskid = response.json()['response']['taskId'] return (taskid)
def delete_and_wait(url): if FAKE: return fake_post[url] token = get_auth_token() url = create_url(path=url) headers= { 'x-auth-token': token['token'], 'content-type' : 'application/json'} try: response = requests.delete(url, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print ("Error processing request", cerror) sys.exit(1) taskid = response.json()['response']['taskId'] print ("Waiting for Task %s" % taskid) task_result = wait_on_task(taskid, token) return task_result
def post(url, data): url = create_url(path=url) print(url) token = get_auth_token() headers = { 'X-auth-token': token['token'], 'content-type': 'application/json' } try: response = requests.post(url, data=json.dumps(data), headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) return response.json()
def create_project_rule(project_name, serial, platform, host, config_file_id=None): token = get_auth_token() url = create_url(path="pnp-project") headers = { 'x-auth-token': token['token'], 'content-type': 'application/json' } project_id = project_name_to_id(project_name) post_url = url + '/%s/device' % project_id print("POST URL %s" % post_url) body = [{ "serialNumber": serial, "platformId": platform, "hostName": host, "pkiEnabled": True }] if config_file_id is not None: body[0]['configId'] = config_file_id print(json.dumps(body, indent=2)) try: response = requests.post(post_url, headers=headers, data=json.dumps(body), verify=False) response.raise_for_status() except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) if response.json(): print(json.dumps(response.json(), indent=2)) sys.exit(1) taskid = response.json()['response']['taskId'] print("Waiting for Task %s" % taskid) task_result = wait_on_task(taskid, token) return task_result
def put(url, data): token = get_auth_token() url = create_url(path=url) headers = { 'x-auth-token': token['token'], 'content-type': 'application/json' } try: response = requests.put(url, headers=headers, data=json.dumps(data), verify=False) except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) logging.debug("PUT Result{}".format(json.dumps(response.json()))) return response.json()
def put_and_wait(url, data): token = get_auth_token() url = create_url(path=url) headers = { 'x-auth-token': token['token'], 'content-type': 'application/json' } try: response = requests.put(url, headers=headers, data=json.dumps(data), verify=False) except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) taskid = response.json()['response']['taskId'] print("Waiting for Task %s" % taskid) task_result = wait_on_task(taskid, token, timeout=900, retry_interval=60) return task_result
def upload_file(namespace, filepath): token = get_auth_token() try: f = open(filepath, "r") files = {'fileUpload': f} except: print("Could not open file %s" % filepath) sys.exit(1) url = create_url(path="file/%s" % namespace) print("POST %s" % url) headers = {'x-auth-token': token['token']} try: response = requests.post(url, files=files, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) return response.json()
def post_and_wait(url, data): token = get_auth_token() url = create_url(path=url) headers = { 'x-auth-token': token['token'], 'content-type': 'application/json' } try: response = requests.post(url, headers=headers, data=json.dumps(data), verify=False) response.raise_for_status() except requests.exceptions.RequestException as cerror: print("Error processing request", cerror) sys.exit(1) print("Response", response.json()) # this has changed in 1.2 ## deploymentId = response.json()['deploymentId'] taskId = deploymentId #taskId=response.json()['response']['taskId'] print("waiting for taskId", taskId) start_time = time.time() retry_interval = 2 timeout = 10 * retry_interval while True: # changed in 1.2 response = get_url('template-programmer/template/deploy/status/' + deploymentId) #response = get_url('task/' + taskId) print(response) if response['response']["endTime"] != '': break else: if timeout and (start_time + timeout < time.time()): raise DeploymentTimeoutError( "Task %s did not complete within the specified timeout " "(%s seconds)" % (task_id, timeout)) print("Task=%s has not completed yet. Sleeping %s seconds..." % (deploymentId, retry_interval)) time.sleep(retry_interval) if response['response']["isError"]: raise DeploymentError("Task %s had error %s" % (deploymentId, response['status'])) print(response) # 1.2 need to get the deploymentId from the progress in the task try: progress = response['response']['progress'] deploymentId = re.match(r'.*: (.*)$', progress.split("\n")[1]).group(1) except KeyError: raise DeploymentError("Task %s had no deploymentId %s" % (taskId, response['status'])) response = get_url('template-programmer/template/deploy/status/' + deploymentId) print(response) return response