import csv
import time
from pyhpeimc.auth import *
from pyhpeimc.plat.groups import *
from pyhpeimc.version import *

2+34

"""
Explanation: Working with HPE IMC API for Custom Views
In this notebook, we will be covering the basics of using the pyhpimc python module to access the RESTFUL interface ( eAPI ) of the HPE IMC Network Management Server. 
The python library is currently available at HPE Github repository
Import Required Modules from pyhpeimc
Before we get started, we must import the required libraries into the ipython interpreter . For this notebook, you will require the Auth and Operator modules from the pyhpimc library, as well as the standard csv module.
End of explanation
"""


auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin")


"""
Explanation: Input IMC Credentials
In this section, we will first create a authentication object which will contain

the protocol
ip address
port number
username
password

If you are running this on your local network, please change the appropriate values in the following field to connect to your local IMC server.
End of explanation
"""


def print_views():
    views_list = get_custom_views(url=auth.url, auth=auth.creds)
    print ("There are a total of " + str(len(views_list)) + " views currently")
    for view in views_list:
        print (view['name'])
    print (json.dumps(views_list[0], indent = 4))

"""
Explanation: Create Print View List Helper Function
This function will gather the current list of custom views from the HPE IMC NMS and print them out to the screen.
End of explanation
"""


print_views()

"""
Explanation: Display the current views
This section will count the total number of existing views as well as display the first view returned.
Notice the 'upLevelSymbolId" is 3 for this custom view. The three designates that this is a level one custom view, meaning that it's at the root of the custom view tree.
End of explanation
"""


create_custom_views(auth=auth.creds, url=auth.url, name="Canada")
create_custom_views(auth=auth.creds, url=auth.url, name="Alberta",upperview='Canada')
create_custom_views(auth=auth.creds, url=auth.url, name="Calgary",upperview='Alberta')


print_views()

"""
Explanation: Create New Custom View
In this step, we will create two custom views Canada and Alberta using the create_custom_views() function.
For this example, we will create 
- the Canada view which is a L1 custom view
- the Alberta custom view which will be a child of the Canada Custom view.
- the Calgary custom view which will be a child of the Alberta Custom View
|Name| Upperview |
|:-----|-----|
|Canada|     |
|Alberta| Canada |
|Calgary | Alberta |
End of explanation
"""


get_custom_views(url=auth.url, auth=auth.creds, name="Canada")

"""
Explanation: Canada View
Notice that the upLevelSymbolId for this view is the same as the default My Network View shown above. We can see that this is a L1 custom view.
End of explanation
"""


get_custom_views(url=auth.url, auth=auth.creds, name="Alberta")

"""
Explanation: Alberta View
Notice that upLevelSymbolId for this view is equal to the symbolId of the Canada view shown in the previous example. we can see that this is a child of the Canada Custom View.
End of explanation
"""


get_custom_views(url=auth.url, auth=auth.creds, name="Calgary")

"""
Explanation: Calgary View
Notice that upLevelSymbolId for this view is equal to the symbolId of the Alberta view shown in the previous example. we can see that this is a child of the Canada Custom View.
End of explanation
"""


delete_custom_view(url=auth.url, auth=auth.creds, name='Canada')
delete_custom_view(url=auth.url, auth=auth.creds, name='Alberta')
delete_custom_view(url=auth.url, auth=auth.creds, name='Calgary')
print_views()

"""
Explanation: Deleting Custom Views
In this section we will delete individual custom views.
End of explanation
"""


with open('custom_views.csv') as f:
    s = f.read()
    print (s)

"""
Explanation: Display Contents of Custom Views.csv
We have prepared a CSV file which contains the new custom views that we wish to create. This can contain both parent and child custom views. 
| name | upperview |
| ------ | ----------- |
| Branches   |   |
| Branch1 | Branches |
| Branch2    | Branches |
| WAN      |            |
End of explanation
"""


def import_custom_views(filename):
    """
    Function which takes in a csv files as input to the create_custom_views function from the pyhpimc python module
    available at https://github.com/HPNetworking/HP-Intelligent-Management-Center
    :param filename: user-defined filename which contains two column named "name" and "upperview" as input into the
    create_custom_views function from the pyhpimc module.
    :return: returns output of the create_custom_vies function (str) for each item in the CSV file.
    """
    with open(filename) as csvfile:
        # decodes file as csv as a python dictionary
        reader = csv.DictReader(csvfile)
        for view in reader:
            # loads each row of the CSV as a JSON string
            name = view['name']
            upperview = view['upperview']
            if len(upperview) is 0:
                upperview = None
            create_custom_views(auth=auth.creds, url=auth.url,name=name,upperview=upperview)

"""
Explanation: Create Import Custom Views Function
Here we create a new function which will take a CSV file shown above as an input to the create_custom_views function
End of explanation
"""


start_time = time.time()
import_custom_views('custom_views.csv')
print("--- %s seconds ---" % (time.time() - start_time))

views_list = get_custom_views(url=auth.url, auth=auth.creds)
print_views()
print ("There are a total of " + str(len(views_list)) + " views currently")

"""
Explanation: Import Custom Views from CSV File
End of explanation
"""


def delete_custom_views_csv(filename):
    """
    Function which takes in a csv files as input to the delete_custom_view function from the pyhpeimc python module
    available at https://github.com/HPENetworking/HP-Intelligent-Management-Center
    :param filename: user-defined filename which contains two column named "name" and "upperview" as input into the
    create_custom_views function from the pyhpimc module.
    :return: returns output of the create_custom_vies function (str) for each item in the CSV file.
    """
    with open(filename) as csvfile:
        # decodes file as csv as a python dictionary
        reader = csv.DictReader(csvfile)
        for view in reader:
            # loads each row of the CSV as a JSON string
            name = view['name']
            delete_custom_view(url=auth.url, auth=auth.creds, name=name)

start_time = time.time()
delete_custom_views_csv('custom_views.csv')
print("--- %s seconds ---" % (time.time() - start_time))

"""
Explanation: Cleaning up After Ourselves
End of explanation
"""


