The IP Fabric webinar covering Application Programming Interface (API) and its capabilities starting with authentication and inventory collection in part 1.
Transcript
Good morning or good afternoon, everyone. This is the recorded session that we created after the webinars actually happened. The webinars were held on 26th March. We had 2 sessions. The first one for the was for the, EU times, but the second one for for the Americas.
This webinar is dedicated to the API operations, with the IP fabric platform. We know that we can operate the IP fabric platform through the graphical user interface. But if we want to integrate a platform with other tools, such a monitoring suite or the Ansible scripts, we would like to interact with the back end's API to extract information or simulate the API or to start a minor snapshot with different settings. And this is exactly what the webinar is gonna be focused on. So let's go just through a couple of slides at the beginning.
And after we finish the slides, we will jump into the console, and I I will show you the practical examples of how to use the IP fabrics API. The first is gonna be about how to authenticate. The second part of the IP fabric webinar will be about how to modify settings and creating minor snapshot with a different settings, from the API. This third part can be the end to end path simulation over API. We know that IP fabric has a unique capability to simulate the end to end end to end of the application, from the source to destination IP and protocol.
It's practically the simulation of the routing and switching and the security portion through your network motor model in the IP fabric. So let's get to that. First, we will see how we can authenticate to the IP fabric's API. We will we support 2 standard basic, authentication model. The first is the basic with the username and the password.
Please note that any user that you create an IP fabric and you want to use for the API communication, you need to log in through the GUI first confirm end user license agreement unless you can't connect. This basic authentication model is usually just used for getting a token. And then you can switch to token based authentication. After you log in through the basic authentication, you can retrieve 2 types of token. The first is the access token.
The access token you can use for extract any kind of data from the IP fabric. It expires after 30 minutes of time, and when it expires, you can refresh the token with the refresh token, which expires after 24 hours if not blacklisted. The recommended use of the token is to authenticate through the basic authentication first, then to get the access token and use access token every time you're calling data from the IP fabric. We will use Python 3 as the language to connect to the IP fabric's API. Of course, you can use any type of language.
You can use Python 2. You can use JavaScript or or Bash. It's it's really up to you. It's a great and clean multipurpose programming language adopted by a lot of network engineers. And it's especially friendly with requests using an HTTP method.
And in the webinar, we are using the requests library. The description of the request library is very, very self explanatory. It abstracts the complexities of making requests. We had a beautiful simple API so that you can focus on interacting with services and consuming data in your application. Please follow the documentation.
It's strongly recommended. It's it's a great source of information about the request library. It's great. I'm not a developer myself. I'm formerly network engineer, and it does a great job.
The first use case we will do is the data collection. With the most cases, we use the post request for collecting the data from the API. There can be used other types of requests. We will discuss them later on. In any technology table in the IP fabric and now I can give you a quick example.
We can switch to the IP fabric platform so I can show you where you can get the information about the API endpoint and the payload that you need to send with the post request to get the right data. So let's switch to the IV fabric. And for example, I wanna go and extract the information from the inventory. So I go to inventory and devices. I can search for the vendor, let's say, Juniper.
I wanna know everything about the SRX. So this is the list of the SRXs I have. Right now, this is the data extraction from the GUI. If I want to get the API endpoint, I just go here and click the little table description on the right part. And here it is.
The API endpoint for the inventory and devices is v one tables inventory and devices. You need to place the API before before the URL and the IP fabric server, of course. The payload is very simple to use. Here are the columns that you would like to receive with the with the response, and here you can see it didn't show you the filters per your filter that you used in the in the query, which is extremely important. Another important part is to use the correct snapshot ID.
We can kind of get around that if you don't know the snapshot ID to use the dollar and last, which will pick up automatically the last available snapshot in the discovery. I created a couple of very, very simple functions for this demonstration. The first one of the first function will be to get data function. This is the simple example and please keep in mind, I'm not a developer myself. I just play with a tool and it's like a playing guitar.
You do it because you like it. So there's a simple flow control. If we have a look at our at the at the function, the first is a function definition. First, you need to enter the API endpoint, the headers, and the payload. So the API endpoint, we exactly know what it is.
It is the URL, plus the endpoint. The headers, we will get to that later on. The data payload is the payload that we observed in the inventory table. Then we will just send out the data and request post. And, here is a little flow control where we check if the response from the server for our API request is okay.
Because if it's okay, we know we collected data. If not, we know we are unable to get the data. It can be an authentication issue or error issue because maybe we selected the the wrong API endpoint. There can be more reasons. I created more functions than just getting to data.
I'll show you the function later on. The first is to get seed, which we just saw. The first function here is to get seed information. This is the function for changing the seed IP configuration. Then Then we have the update seat and the rewrite seat.
We will get to them later on. They have just different type of request, and it has quite a different function. Then we have the the function get settings, which will give us the current IP fabric settings. Then we have the get settings nets which provides us the current IP exclude and include settings from the IP fabric. And finally, the function with the help with which one we can update the settings on the IP fabric.
The before last is get snapshot information and the last one is to start a snapshot for with the help of the API in the IP fabric. The last phase, and this is one of the last slides, I promise, is gonna be the simulation of the end to end path through the API. It's a great tool to have because we know that in the IP fabric, we can simulate the end to the path, over over over the network with the the information that we need to define, which is source, destination IP, source, and destination port, protocol type, RP of option, and a snapshot ID. When you do the simulation over the API, you get much more data because you will get the the complete JSON response with all the hops. Not only the IP hops, also the hops on layer 2 network.
And, this is the last slide. Thank you a lot. And, let's move to the console and, see how it works. Okay. So now we switch to the to the console.
We are on the Ubuntu server. We're gonna be using, Python 3. Let's see where we at. We are right now in the webinar folder where we have all the files. The first and the most important file is the ipfapi dotpy, which is the file with all the functions that we will use in the demonstration.
Let's have a look quickly how the functions will look like. So if I I have the add tab that I use for creating code. So this is the file that I will be using for importing the functions. So we import requests. This is just a little little line that is used for suppressing the sis SSL certificate warnings.
I'm not gonna be using, the correct SSL settings. So I I I wanna suppress it. I don't wanna hear the information about a warning every time I I run a function. So the first function, it will be the token request, which is the most important function, which is the authentication itself. Right?
So with the token request, we just need to put a token server, which is the IP fabric server and the username and the password. It is a simple, post request as we can see in here. The API endpoint is combination of the server and the correct API endpoint, which is the v one out login. And we will place adjacent and the username and password. This is the verification of the certificate.
I disabled that. And here is a little flow control for checking out if the request is okay. If the request is okay, we we got an authentication and we got a token. If not, there's something wrong. We need to see the message why it happened.
So let's go to the to the console and let's request a token. For your token request, I have a prepared setup. So this is the script for collecting the data from the IP fabric and specifically inventory. So the server definition is in here. It's our demo server from the internal domain.
You cannot access it from the public domain. This is the user I created in IP fabric for the API. I logged in before with the user through the GUI to confirm the end user license agreement. The snapshot is this is what I told you before. This is the dollar and the loss, so we automatically select the last snapshot.
This is the definition of the payload. It's gonna be columns. I just wanna pick up the serial number, host name, and the vendor. The snapshot is gonna be the snapshot ID, which is last, and here is the endpoint. It's a combination of the server and inventory and the devices.
So let's let's try to auto ticket first. So we need to import the the functions. And for the functions, we will import just token request and to get data function, and then we will define the variables for the server and authentication data. So I'm gonna copy that. I'm gonna go back to the console, and I'm gonna go directly into the Python 3 console.
So Python 3 is my version of the Python. Let's get in. So right now I'm in the Python console. I'm gonna just paste the information I have from my prepared script. Then I'm gonna go back.
And, first, I'm gonna try to authenticate. Right? So I wanna get a token or tokens. So, again, I'm creating the the the tokens and its token is gonna be equal to token request, and I'm gonna try to get a data. And we get the right response.
It seems that we got a token. So let's have a look what we have. Exactly as expected, we have the access token, which is a very, very long string, and the refresh token that can be used for getting our token after the first token expires, which we have 30 minutes to go right now. So the access token actually is the tokens and, we will just pick up the access token from from the dictionary. So it's just a string.
Right? And we will define the headers. The the the header is gonna be defined always this way. The content type is application JSON and, this is the way how you can authenticate your IP fabric. Use the authorizer's there plus the the access token.
So this is gonna be the header, and let's see how the header looks like right now. Alright. Looks good. And right now, let's try to extract the data. I will define the devices at the simple get data function, which I again imported from the from the other file, and that the device's endpoint is going to be the server and the inventory devices.
The headers is gonna be the headers I just defined, and the payload is gonna be the device's payload that I define in the here. So it's gonna be, I want a serial number, I want a host name, I want vendor. So let's have a look how to get data function looks like. The API endpoint, the headers, and the payload. In the within a function, we simply call a simple post request using the endpoint, the headers, and the payload.
Here we again get a a little check where we are checking if the response from the server is okay. If it is, we will get the the the simple message that we successfully got our data. If not, we'll get a message that we are unable to get the data from the endpoint. And in return, we will get the response, which we can investigate later on if we need to. So we can see the exact information we received, we can see, the information we can go out from the headers, etcetera.
So let's try to get, or extract the information from the API. So now we are back in the inventory script, and I refreshed my Python 3 console in the meantime. So we're gonna do everything from the scratch. So you have to do the full flow of how to authenticate and get the data set down. So here we will just import it from the IPF API, which is the file with all the function.
We will functions. We will re re import import a token request function and the get data function. Then we will define a server, which is gonna be our demo server and the authentication data, which is our API user and the password. We will define the endpoint and the snapshot ID. The snapshot ID will be the last.
Endpoint is gonna be the combination of the server URL and the API endpoint. And the payload, we request only serial number, host name, and vendor. However, we will have in a response more than 600 devices, and we don't wanna maybe play with that amount of data. So let's filter that just for the Johnny Burners Rx's where we are we can expect to get maybe 10 devices. So, again, let's go back to the IP fabric server and to the inventory.
Now there are actually 3 ways how you can filter data. The first one is that you just start typing, and the filter will be created like Juniper. This means that the string in the column needs to at least contain the word, and it doesn't mean that it equals the words. Right? So if I do, like like, it will still get a juniper.
The other way how you can say or filter for data is to put an equal mark before. If you do an equal, and I'm not gonna search for the SRXs, I won't get anything. And the reason is that that filter is searching for a equal string. Right? And if you check the payload right now, it says equal.
Here, it says like. The like is like, is similar to contains and equal, it has to be the same string. And now we can, collide the payload, and we can say you come on there. Let me say devices payload is gonna be equal to the text we copied. We still don't want to have all the information.
We don't want a site key. We just want a serial number, host name, and the vendor. Maybe the family. We don't need config register, and maybe we'll keep the MAC. In the filters, we have the, like, Juniper, and the platform has to be equaled VSRX.
This is important. The pagination, we don't really need to play with that. The snapshot, we can either keep this snapshot ID or we can simply say it's gonna be the last one. Reports, we don't need to worry about them right now. They are they're they're a special use, and right now, they're not important.
When we are creating the the payload, we need to set the keys as strings. Otherwise, it will search for the variable. So we have to find a payload, and, right now, I'm gonna just data function over here. So we're gonna define server data, and we will also need to define the header before that. So now we have a complete function.
So we're gonna go back to the console and just paste the information. Okay? So we have server. We got a snapshot ID. We got a device's payload.
We get a authentication data, and now we will request the token again. And with the token, the access token, we will create a header. K. Let's check the tokens. We have the response message over here, so we know that the authentication was successful.
And now with token headers and all the other information, we can request the data through the API. So I just copy this. This is the console. And you see we immediately got a response that it was successful. And now let's see what we have in the response.
And here are the options. In our little flow control check where we are checking the response in the in the function, if you remember we are using devices okay and the response is either true or false. So if the response is okay, we it's it's true. We can check for the reason or we can check for the status code, which can be very important when you wanna check for different type of codes as responses. We also have the URL.
So you can even see the header and all the information that you got from the server. The text itself, which is in here, is string response. And remember, when you wanna play with the data, you usually expect to get a JSON. The requests library had a great feature because it has built in JSON decoder, and this is very important, and the decoder is in here. So we can do.
Because devices, JSON, and we can see what keys we have. Right? And in our response, we have 2 primary keys. The first is data that contains the actual data from the text, and the second is meta, which is the information about the response itself. So we can see what we have in the in the meta first.
And this is the basic information. So we know that we have as in a response, we have 9 devices. There's currently no limit, and the snapshot ID as a reference in the data. It's all the information. And we can, for example, have a look at the data.
And because the data is a list of devices, so we can show this first one, which is gonna be the l one f w 56. We get a Juniper platform family and the Mac. And that's exactly what we, expected in our response with the filter we created. If we go back to the code right now so here's the exact columns that we requested. The exact columns we had in response, and here are the filters.
And with these filters, we got 9 devices, which is expected. And the same, if we have a look quickly at the added, Juniper platform, this is actually what we have. The same type of response. So this is a basic function or basic API request, how to get information from the API, how to get information from the inventory, how to use filters, And, I think we can move on and start with the second part of the webinar, which will be, changing the settings and, starting the snapshot with the updated settings.
