コード例 #1
0
def is_file_present(apic, namespace, filename):
    filename = name_wrap(filename)
    file_list = apic.file.getFilesByNamespace(nameSpace=namespace)
    fileid_list = [
        file.id for file in file_list.response if file.name == filename
    ]
    return None if fileid_list == [] else fileid_list[0]
コード例 #2
0
def remove_project(apic, project_name):
    project_name = name_wrap(project_name)
    project = apic.pnpproject.getPnpSiteByRange(siteName=project_name)
    if project.response != []:
        project_id = project.response[0].id
        pnp_task_response = apic.pnpproject.deletePnpSiteByID(projectId=project_id, deleteRule=1, deleteDevice=1)
        task_response = apic.task_util.wait_for_task_complete(pnp_task_response, timeout=5)
        print (apic.serialize(task_response))
    else:
        print ("No project named %s" % project_name)
コード例 #3
0
def remove_project(apic, project_name):
    project_name = name_wrap(project_name)
    project = apic.pnpproject.getPnpSiteByRange(siteName=project_name)
    if project.response != []:
        project_id = project.response[0].id
        pnp_task_response = apic.pnpproject.deletePnpSiteByID(
            projectId=project_id, deleteRule=1, deleteDevice=1)
        task_response = apic.task_util.wait_for_task_complete(
            pnp_task_response, timeout=5)
        print(apic.serialize(task_response))
    else:
        print("No project named %s" % project_name)
コード例 #4
0
def create_rule(apic, param_dict, project_id, file_id):
    serial_number = name_wrap(param_dict['serialNumber'], fixed_len=True)
    rule_data = [{
        "serialNumber": serial_number,
        "platformId": param_dict['platformId'],
        "hostName": param_dict['hostName'],
        "configId": file_id,
        "pkiEnabled": True
    }]
    print(json.dumps(rule_data, indent=2))
    rule_task = apic.pnpproject.createPnpSiteDevice(projectId=project_id,
                                                    rule=rule_data)
    task_response = apic.task_util.wait_for_task_complete(rule_task, timeout=5)
    progress = task_response.progress
    print(progress)
コード例 #5
0
def clean_up_all(apic, devices):

    f = open(devices, 'rt')
    try:
        reader = csv.DictReader(f)
        for dict_row in reader:
            print (dict_row)

            config_filename = dict_row['hostName'] + '-config'
            remove_project(apic, dict_row['site'])
            remove_file(apic, config_filename)

            print("cleaned site: %s" % name_wrap(dict_row['site']))

    finally:
        f.close()
コード例 #6
0
def clean_up_all(apic, devices):

    f = open(devices, 'rt')
    try:
        reader = csv.DictReader(f)
        for dict_row in reader:
            print(dict_row)

            config_filename = dict_row['hostName'] + '-config'
            remove_project(apic, dict_row['site'])
            remove_file(apic, config_filename)

            print("cleaned site: %s" % name_wrap(dict_row['site']))

    finally:
        f.close()
コード例 #7
0
def create_rule(apic, param_dict, project_id, file_id):
    serial_number = name_wrap(param_dict["serialNumber"], fixed_len=True)
    rule_data = [
        {
            "serialNumber": serial_number,
            "platformId": param_dict["platformId"],
            "hostName": param_dict["hostName"],
            "configId": file_id,
            "pkiEnabled": True,
        }
    ]
    print(json.dumps(rule_data, indent=2))
    rule_task = apic.pnpproject.createPnpSiteDevice(projectId=project_id, rule=rule_data)
    task_response = apic.task_util.wait_for_task_complete(rule_task, timeout=5)
    progress = task_response.progress
    print(progress)
コード例 #8
0
def lookup_and_create(apic, project_name):
    project_name = name_wrap(project_name)
    project = apic.pnpproject.getPnpSiteByRange(siteName=project_name)

    if project.response != []:
        project_id = project.response[0].id
    else:
        # create it
        print("creating project:{project}".format(project=project_name))
        pnp_task_response = apic.pnpproject.createPnpSite(project=[{"siteName": project_name}])
        task_response = apic.task_util.wait_for_task_complete(pnp_task_response, timeout=5)

        # 'progress': '{"message":"Success creating new site","siteId":"6e059831-b399-4667-b96d-8b184b6bc8ae"}'
        progress = task_response.progress
        project_id = json.loads(progress)["siteId"]

    return project_id
コード例 #9
0
def build_templates(template_file, devices):

    templateLoader = jinja2.FileSystemLoader( searchpath="." )
    templateEnv = jinja2.Environment( loader=templateLoader )
    template = templateEnv.get_template(template_file)

    f = open(devices, 'rt')
    try:
        reader = csv.DictReader(f)
        for dict_row in reader:
            print (dict_row)
            outputText = template.render(dict_row)

            config_filename = name_wrap(CONFIGS_DIR + dict_row['hostName'] + '-config')
            with open(config_filename, 'w') as config_file:
                config_file.write(outputText)
            print("wrote file: %s" % config_filename)

    finally:
        f.close()
コード例 #10
0
def build_templates(template_file, devices):

    templateLoader = jinja2.FileSystemLoader(searchpath=".")
    templateEnv = jinja2.Environment(loader=templateLoader)
    template = templateEnv.get_template(template_file)

    f = open(devices, 'rt')
    try:
        reader = csv.DictReader(f)
        for dict_row in reader:
            print(dict_row)
            outputText = template.render(dict_row)

            config_filename = name_wrap(CONFIGS_DIR + dict_row['hostName'] +
                                        '-config')
            with open(config_filename, 'w') as config_file:
                config_file.write(outputText)
            print("wrote file: %s" % config_filename)

    finally:
        f.close()
コード例 #11
0
def lookup_and_create(apic, project_name):
    project_name = name_wrap(project_name)
    project = apic.pnpproject.getPnpSiteByRange(siteName=project_name)

    if project.response != []:
        project_id = project.response[0].id
    else:
        # create it
        print("creating project:{project}".format(project=project_name))
        pnp_task_response = apic.pnpproject.createPnpSite(
            project=[{
                'siteName': project_name
            }])
        task_response = apic.task_util.wait_for_task_complete(
            pnp_task_response, timeout=5)

        # 'progress': '{"message":"Success creating new site","siteId":"6e059831-b399-4667-b96d-8b184b6bc8ae"}'
        progress = task_response.progress
        project_id = json.loads(progress)['siteId']

    return project_id
コード例 #12
0
def create_and_upload(apic, devices, template_file):
    templateLoader = jinja2.FileSystemLoader(searchpath=".")
    templateEnv = jinja2.Environment(loader=templateLoader)
    template = templateEnv.get_template(template_file)

    f = open(devices, "rt")
    try:
        reader = csv.DictReader(f)
        for dict_row in reader:
            print(dict_row)
            outputText = template.render(dict_row)
            config_filename = name_wrap(CONFIGS_DIR + dict_row["hostName"] + "-config")
            with open(config_filename, "w") as config_file:
                config_file.write(outputText)
            project_id = lookup_and_create(apic, dict_row["site"])
            file_id = upload_file(apic, config_filename)
            create_rule(apic, dict_row, project_id, file_id)

            print("created file: %s" % config_filename)

    finally:
        f.close()
コード例 #13
0
def create_and_upload(apic, devices, template_file):
    templateLoader = jinja2.FileSystemLoader(searchpath=".")
    templateEnv = jinja2.Environment(loader=templateLoader)
    template = templateEnv.get_template(template_file)

    f = open(devices, 'rt')
    try:
        reader = csv.DictReader(f)
        for dict_row in reader:
            print(dict_row)
            outputText = template.render(dict_row)
            config_filename = name_wrap(CONFIGS_DIR + dict_row['hostName'] +
                                        '-config')
            with open(config_filename, 'w') as config_file:
                config_file.write(outputText)
            print("created file: %s" % config_filename)
            project_id = lookup_and_create(apic, dict_row['site'])
            file_id = upload_file(apic, config_filename)

            create_rule(apic, dict_row, project_id, file_id)

    finally:
        f.close()
コード例 #14
0
    print("Waiting for Task %s" % taskid)
    task_result = wait_on_task(taskid, token)

    return task_result

if __name__ == "__main__":
    if len(sys.argv) >1  and sys.argv[1] == "-a":
        filelist = list_files('config')['response']
        filetuple = [(file['name'], file['id']) for file in filelist]

        project_name_list = set()
        f = open(DEVICES, 'rt')
        try:
            reader = csv.DictReader(f)
            for dict_row in reader:
                filename = name_wrap(dict_row['hostName'] + "-config")

                fileid = [ id for fn, id in filetuple if fn == filename][0]
                response = create_project_rule(name_wrap(dict_row['site']),
                                               serial=name_wrap(dict_row['serialNumber'], fixed_len=True),
                                               platform=dict_row['platformId'],
                                               host=dict_row['hostName'],
                                               config_file_id=fileid)
                print(json.dumps(response, indent=2))
        finally:
            f.close()

    else:
        response = create_project_rule(name_wrap("Canberra"), serial=name_wrap("12345678910", fixed_len=True ),
                                       platform="2960x", host="device", )
        print(json.dumps(response, indent=2))
コード例 #15
0
#!/usr/bin/env python

import os, sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from  login import login
from name_wrapper import name_wrap

# create a unique name.  Only required in a lab environment
siteName = name_wrap("adam")
apic = login()

# get the project
pnp_project = apic.pnpproject.getPnpSiteByRange(siteName=siteName)
print (apic.serialize(pnp_project))

try:
    pnp_project_id = pnp_project.response[0].id

    # delete it
    pnp_delete_task_response= apic.pnpproject.deletePnpSiteByID(projectId=pnp_project_id)
    pnp_delete_response = apic.task_util.wait_for_task_complete(pnp_delete_task_response, timeout=5)
    print ("delete", apic.serialize(pnp_delete_task_response))

    print("status", apic.task_util.is_task_success(pnp_delete_task_response))
except ValueError :
    print(e)
コード例 #16
0
    task_result = wait_on_task(taskid, token)

    return task_result


if __name__ == "__main__":
    if len(sys.argv) > 1 and sys.argv[1] == "-a":
        filelist = list_files('config')['response']
        filetuple = [(file['name'], file['id']) for file in filelist]

        project_name_list = set()
        f = open(DEVICES, 'rt')
        try:
            reader = csv.DictReader(f)
            for dict_row in reader:
                filename = name_wrap(dict_row['hostName'] + "-config")

                fileid = [id for fn, id in filetuple if fn == filename][0]
                response = create_project_rule(name_wrap(dict_row['site']),
                                               serial=name_wrap(
                                                   dict_row['serialNumber'],
                                                   fixed_len=True),
                                               platform=dict_row['platformId'],
                                               host=dict_row['hostName'],
                                               config_file_id=fileid)
                print(json.dumps(response, indent=2))
        finally:
            f.close()

    else:
        response = create_project_rule(
コード例 #17
0
#!/usr/bin/env python

import os, sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from login import login
from name_wrapper import name_wrap

# create a unique name.  Only required in a lab environment
siteName = name_wrap("adam")
apic = login()

# get the project
pnp_project = apic.pnpproject.getPnpSiteByRange(siteName=siteName)
print(apic.serialize(pnp_project))

try:
    pnp_project_id = pnp_project.response[0].id

    # delete it
    pnp_delete_task_response = apic.pnpproject.deletePnpSiteByID(
        projectId=pnp_project_id)
    pnp_delete_response = apic.task_util.wait_for_task_complete(
        pnp_delete_task_response, timeout=5)
    print("delete", apic.serialize(pnp_delete_task_response))

    print("status", apic.task_util.is_task_success(pnp_delete_task_response))
except ValueError:
    print(e)
コード例 #18
0
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

if __name__ == "__main__":
    if len(sys.argv) >1  and sys.argv[1] == "-a":
        project_names = get_project_names()

        for project in project_names:
            response = create_project(name_wrap(project))
            print (json.dumps(response, indent=2))
    else:
        response = create_project(name_wrap("Canberra"))
        print (json.dumps(response, indent=2))
コード例 #19
0
    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

if __name__ == "__main__":
    if len(sys.argv) >1  and sys.argv[1] == "-a":
        project_list = get_project_names()

        for project in project_list:
            response = delete_project(name_wrap(project))
            print(json.dumps(response, indent=2))
    else:
        response = delete_project(name_wrap("Canberra"))
        print(json.dumps(response, indent=2))
コード例 #20
0
def is_file_present(apic, namespace, filename):
    filename = name_wrap(filename)
    file_list = apic.file.getFilesByNamespace(nameSpace=namespace)
    fileid_list = [file.id for file in file_list.response if file.name == filename]
    return None if fileid_list == [] else fileid_list[0]
コード例 #21
0
        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


if __name__ == "__main__":
    if len(sys.argv) > 1 and sys.argv[1] == "-a":
        project_list = get_project_names()

        for project in project_list:
            response = delete_project(name_wrap(project))
            print(json.dumps(response, indent=2))
    else:
        response = delete_project(name_wrap("Canberra"))
        print(json.dumps(response, indent=2))
コード例 #22
0
    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

if __name__ == "__main__":
    if len(sys.argv) >1  and sys.argv[1] == "-a":
        project_list = get_project_names()

        for project in project_list:
            response = delete_project(name_wrap(project))
            print(json.dumps(response, indent=2))
    else:
        response = delete_project(name_wrap("Sydney"))
        print(json.dumps(response, indent=2))
コード例 #23
0
#!/usr/bin/env python
import os, sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from  login import login
from name_wrapper import name_wrap

apic = login()

siteName = name_wrap('adam')
pnp_task_response= apic.pnpproject.createPnpSite(project=[{'siteName' :siteName}])
task_response = apic.task_util.wait_for_task_complete(pnp_task_response, timeout=5)
print (apic.serialize(task_response))

print(apic.task_util.is_task_success(pnp_task_response))