import csv
import time
from pyhpeimc.auth import *
from pyhpeimc.plat.operator import *

"""
Explanation: Working with HPE IMC API for Operators
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
"""


ops_list = get_plat_operator(url=auth.url, auth=auth.creds)
print ("There are currently " + str(len(ops_list)) + " operators configured.")

"""
Explanation: List Operators
In this step we list the current operators that exist in the system by using the get_plat_operators function and capturing the output into a varible called ops_list.
We will then run the len which measures the length of the ops_list object, or more simply, measures the number of operators currently configured in the system.
End of explanation
"""


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

"""
Explanation: Shown here is a screen capture of the current operators configured in the HPE IMC system. You can see that there are the same amoutn of operators as shown in the statement above. 
note: The screen capture is used for the intial demonstration only. If you are running this notebook against your own IMC server, this screen capture may not match what you have configured on your system.

Importing Operators
In the following section, we are going to import a group of operators from a prepared CSV file. 
For more information on the contents of the CSV file, please see the documentation available at http://ip_address:8080/imcrs  on your local IMC server. 
The CSV file provided is for example only. Please feel free to use it as a template, but you are encouraged to look at the documentation for more information on the specific attributes of the CSV file. 
This file contains the following operators
| Operator | Fullname |
| ------ | ----------- |
| cyoung   | Christoper Young |
| lstaurt | Les Stuart |
| kgott    | Ken Gott |
| rkauffman | Rick Kauffman |
| lknapp | Leonard Knapp |
| clinzell | Chris Linzell |
| dwenger | Dwayne Wenger |
| jmacdonald | James Macdonald |
| tkubica | Tomas Kubica |
| mbreen | Michael Breen|
| mventer | Marius Venter |
End of explanation
"""


def import_operator(filename='imc_operator_list.csv'):
    with open(filename) as csvfile:
        # decodes file as csv as a python dictionary
        reader = csv.DictReader(csvfile)
        try:
            for operator in reader:
                # loads each row of the CSV as a JSON string
                create_operator(operator, url=auth.url, auth=auth.creds)
        except:
            pass

"""
Explanation: Create import_operator function
In this section, we have created a simple python function to perform the following tasks

Use the standard library CSV module to open the file imc_operator_list.csv file. 
Use a standard For loop to take each line from the CSV file and run the create_operator function from the pyhpimc library over it.

This function will allow us to run the create_operator function once for each operator in the csv file.
End of explanation
"""


start_time = time.time()
import_operator()
print("--- %s seconds ---" % (time.time() - start_time))

"""
Explanation: Run Import Operators Function
We will now run the import_operator function we created above. In this example, we do not have to input the name of the csv file as we are using the default values as configured in the function (filename='imc_operator_list.csv')
End of explanation
"""


ops_list = get_plat_operator(url=auth.url, auth=auth.creds)
print ("There are currently " + str(len(ops_list)) + " operators configured.")

"""
Explanation: List Operators
We will now list the number of operators in the system.
End of explanation
"""


set_operator_password('cyoung', password='newpass',auth=auth.creds,url=auth.url,)
set_operator_password('')

"""
Explanation: Shown here is a screen capture of the current operators configured in the HPE IMC system. You can see that there are the same amoutn of operators as shown in the statement above. 
note: The screen capture is used for the intial demonstration only. If you are running this notebook against your own IMC server, this screen capture may not match what you have configured on your system.

Resetting an Operator Password
We will now use the set_operator_password function to set the password of the newly created cyoung account to something a little more secure. 
Feel free to try using this function to reset the password of another account by replacing the cyoung variable in the function below.
End of explanation
"""


ops_list = ['cyoung', 'lstuart', 'clinzell', 'kgott', 'rkauffman', 'lknapp', 'dwenger', 'jmacdonald', 'tkubica', 'mbreen', 'mventer']

for operator in ops_list:
    delete_plat_operator(operator,url=auth.url, auth=auth.creds, headers=HEADERS)

"""
Explanation: Clean up the system when we're done
In the following section, we will continue to use the API to clean up the system, using the delete_plat_operator function to delete all the new operators.
We will
- create a new python list called ops_list with the name of all the operators we configured as items in the list. 
- Use a standard For loop to take each line from the ops_list object and run the delete_plat_operator function from the pyhpimc library over it.
End of explanation
"""


