def main(apic, user, password, policyid): token = get_auth_token(controller_ip=apic, username=user, password=password) url = create_url(path="policy", controller_ip=apic) print "GET %s" % url headers= { 'x-auth-token': token['token']} try: response = requests.get(url+"/%s" % policyid, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print "Error processing request", cerror sys.exit(1) policy = response.json()['response'] print json.dumps(policy, indent=2) if policy['state'] == "Active": policy['state'] = 'Inactive' else: policy['state'] = "Active" print "Setting policy %s to %s" %(policy['policyName'], policy['state']) headers['Content-Type'] = 'application/json' policylist = [policy] try: response = requests.put(url, headers=headers, data=json.dumps(policylist), verify=False) print json.dumps(response.json()['response']) taskid = response.json()['response']['taskId'] wait_on_task(taskid, token) except requests.exceptions.RequestException as cerror: print "Error processing request", cerror sys.exit(1)
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 path_trace(): ''' :return: the result of path trace ''' url = create_url(path="flow-analysis") print url data = {"sourceIP" : "65.1.1.46", "destIP" : "212.1.10.20"} 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) taskid = response.json()['response']['taskId'] print "Waiting for Task %s" % taskid task_result = wait_on_task(taskid, token) flow_id = task_result['progress'] url = create_url(path="flow-analysis/%s" % flow_id) 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 main(apic, user, password, policyid): token = get_auth_token(controller_ip=apic, username=user, password=password) url = create_url(path="policy/%s" % policyid, controller_ip=apic) print "GET %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) policy = response.json()['response'] print "Deleting policy %s to %s" %(policy['policyName'], policy['state']) headers['Content-Type'] = 'application/json' try: response = requests.delete(url, headers=headers, verify=False) print json.dumps(response.json()['response']) taskid = response.json()['response']['taskId'] task = wait_on_task(taskid, token) print json.dumps(task, indent=2) except requests.exceptions.RequestException as cerror: print "Error processing request", cerror sys.exit(1)
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 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 path_trace(): ''' :return: the result of path trace ''' url = create_url(path="flow-analysis") print url data = {"sourceIP": "65.1.1.46", "destIP": "212.1.10.20"} 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) taskid = response.json()['response']['taskId'] print "Waiting for Task %s" % taskid task_result = wait_on_task(taskid, token) flow_id = task_result['progress'] url = create_url(path="flow-analysis/%s" % flow_id) 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 main(apic, user, password, policyid): token = get_auth_token(controller_ip=apic, username=user, password=password) url = create_url(path="policy/%s" % policyid, controller_ip=apic) print "GET %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) policy = response.json()['response'] print "Deleting policy %s to %s" % (policy['policyName'], policy['state']) headers['Content-Type'] = 'application/json' try: response = requests.delete(url, headers=headers, verify=False) print json.dumps(response.json()['response']) taskid = response.json()['response']['taskId'] task = wait_on_task(taskid, token) print json.dumps(task, indent=2) except requests.exceptions.RequestException as cerror: print "Error processing request", cerror sys.exit(1)
def main(apic, user, password, policyid): token = get_auth_token(controller_ip=apic, username=user, password=password) url = create_url(path="policy", controller_ip=apic) print "GET %s" % url headers = {'x-auth-token': token['token']} try: response = requests.get(url + "/%s" % policyid, headers=headers, verify=False) except requests.exceptions.RequestException as cerror: print "Error processing request", cerror sys.exit(1) policy = response.json()['response'] print json.dumps(policy, indent=2) if policy['state'] == "Active": policy['state'] = 'Inactive' else: policy['state'] = "Active" print "Setting policy %s to %s" % (policy['policyName'], policy['state']) headers['Content-Type'] = 'application/json' policylist = [policy] try: response = requests.put(url, headers=headers, data=json.dumps(policylist), verify=False) print json.dumps(response.json()['response']) taskid = response.json()['response']['taskId'] wait_on_task(taskid, token) except requests.exceptions.RequestException as cerror: print "Error processing request", cerror sys.exit(1)
def post_and_wait(url, data): 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.post(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) return task_result
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 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 create_policy(token, apic, policyscope, appname, appid, relevance): url = create_url(path="policy", controller_ip=apic) policy = [{ "policyName" : "adam-test", "policyScope" : policyscope, "policyPriority": 4095, "actionProperty" : { "pathControlFlag": False, "pathPreferenceFlag": False}, "resource" : {"applications" : [ {"appName": appname, "id" : appid} ]}, "actionProperty": { "relevanceLevel": relevance }, "actions" : ["SET_PROPERTY"] }] headers= { 'x-auth-token': token['token'], 'content-type' : 'application/json'} print "POST", url, json.dumps(policy, indent=2) response = requests.post(url, headers=headers, data=json.dumps(policy), verify=False) response.raise_for_status() task_id = response.json()['response']['taskId'] task = wait_on_task(task_id, token) print json.dumps(task, indent=2)
def create_policy(token, apic, policyscope, appname, appid, relevance): url = create_url(path="policy", controller_ip=apic) policy = [{ "policyName": "adam-test", "policyScope": policyscope, "policyPriority": 4095, "actionProperty": { "pathControlFlag": False, "pathPreferenceFlag": False }, "resource": { "applications": [{ "appName": appname, "id": appid }] }, "actionProperty": { "relevanceLevel": relevance }, "actions": ["SET_PROPERTY"] }] headers = { 'x-auth-token': token['token'], 'content-type': 'application/json' } print "POST", url, json.dumps(policy, indent=2) response = requests.post(url, headers=headers, data=json.dumps(policy), verify=False) response.raise_for_status() task_id = response.json()['response']['taskId'] task = wait_on_task(task_id, token) print json.dumps(task, indent=2)
def post_and_wait(url, data): 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.post(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) return task_result