That last Nugget was jam packed full of stuff.
We talked about HTTP methods.
We talked about try-catch blocks.
We talked about if-then statements.
These are things called control flow,
and that's really, really important
to understand so that you can write clean code
when it comes to doing Python scripts.
Now that we've logged in though and we're holding that token
in a variable, we can create our next request, which
is a get request to retrieve the list of applications,
leveraging that token that we just
got in the previous Nugget. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
got in the previous Nugget.
Let's jump back into the code and take a look
at what's going on here.
Let's go.
So in that last Nugget, we did a lot of stuff.
We got logged in.
We handled a try and accept block.
We even had an if statement to check to make sure
that the token was filled.
That was honestly, the hardest part to this entire script,
and many scripts that you'll work
with in a network automation journey
is just getting logged in.
So if you understand that, if you understood it,
you're off to a great start.
If you don't, don't worry.
You're not alone.
This is just part of learning network automation. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
This is just part of learning network automation.
Don't be afraid to watch it again or go check
out some DevNet associate content
to see how it works with other platforms
too because that's a really, really important thing
is understanding just getting authenticated.
But now that we're authenticated and we
see what we've gone through here with a try accept and we're
storing our token finally in the correct header,
let's scroll down a little bit more
and use this new token to get the list of applications
that we're trying to monitor.
So just like before where we had a base URL, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So just like before where we had a base URL,
we're changing the endpoint now though.
We're specifying a huge endpoint right here
that goes all the way down into objects
and then the applications.
I actually need to scroll over so that you can see that.
There we go, applications.
So we're going into the API endpoint, the Firepower
Management Center Configuration section.
This is where we configure what applications
that we need to monitor.
Version 1, then the domain, then it's WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Version 1, then the domain, then it's
got this big ol' GUID right here.
The trick to finding this GUID is
to actually access Firepower Management Center's
on-box documentation.
Firepower Management Center makes
it easy to explore the documentation, how
you can make an API call to it.
This is no different from what we saw when we were exploring
the API and DNA Center.
Remember that?
In DNA Center, the API documentation
was on DNA Center, and it told you what kind of requests
you can make and use.
Firepower Management Center does the exact same thing, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Firepower Management Center does the exact same thing,
and we actually explore that on-box documentation
as part of the DevNet associate course.
One extra little pro tip that I'll throw in there,
Cisco ASA products do the exact same thing.
They have the REST API documentation
built into the ASA.
And then we also cover how to do just ASA automation
in the DevNet associate course too.
So I grabbed this GUID that you see here--
that's a big thing called a GUID;
it's basically a unique ID-- WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
it's basically a unique ID--
from the Firepower Management Center box.
Then we can explore objects like applications from there.
So I've got my base URL, and I'm going to tack this
onto the end of it.
So let me clear the screen a little bit.
We'll scroll back over so we can see the rest of the code,
and you can see immediately now we have a try
and accept block again.
Just like before, it's going to try
to perform all of the code that's indented here
unless there is an error, then it
will do what's in the indented exception section. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
will do what's in the indented exception section.
But now, see we've also thrown in a finally.
The finally says, run this code no matter
what, whether the try is successful
or the accept is successful, do whatever
the finally says to do.
And recall again our HTTP methods.
The post request was all about creating moo data.
It's all about sending in our login information
and making the server perform some action.
But now we're not doing that.
We're just trying to get a list of applications,
and that's what the get request does. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and that's what the get request does.
We're using the request library one more time.
We're performing a get request.
We have our base URL, but now the apps URL
is targeting what's right in here, our applications.
Now this is where it gets a little different.
When we were logging in, if I scroll up for a second,
you see we specified our URL and then authentication,
we were using a username and password.
We're not doing that anymore down here.
Here, we've already gotten logged in, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Here, we've already gotten logged in,
and we added our token to our headers.
So all I have to do here is specify my headers.
That way when it goes to this URL, it'll send in the token
that we've already received, which basically says,
look, I'm already authenticated.
Don't worry about me that much.
The verify equals false is here again
because we're still working with the same endpoint,
and it's still using a self-signed certificate.
But this is where this little thing gets
kind of interesting and neat.
Remember at the top of the code, we had import JSON. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Remember at the top of the code, we had import JSON.
When we performed the get request to REST API,
the default behavior is to send back
a payload, all of these applications
that are going to be written in JSON.
From Python's perspective, JSON is
nothing more than just a giant block of text.
So when we perform this little method right here
on the end of it, this is saying,
convert all of that JSON text into what's
called a Python dictionary.
That way later down the road, we can WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
That way later down the road, we can
perform programmatic actions in Python
against all of the data that comes back.
So once all of this is done, we've
done the get request to this URL, specifying our token
and converted it from JSON into Python,
it stores all of that Python data in a variable called
apps_response.
So any time after that, we can now
work with the apps response variable and all
of the data inside of it to look at what
application info came back.
And from there, the world is our oyster. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And from there, the world is our oyster.
We could send text message alerts off.
We could print it out to a text file.
We could validate that the data is correct
and it's what we expected.
And if it's not, we could send requests
back in to change the data if we wanted to.
But all I've done here online 39 is
I've simply taken that apps response variable
and printed it out in a nice, pretty format.
That's what all this code is really doing here
is it just prints it out to the terminal
and in nice, pretty format.
Now, of course, we have our exception clause here. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now, of course, we have our exception clause here.
In case anything goes wrong, it prints the error out.
And finally, as a last act, if the apps response
does contain data, that's what we're saying here,
just go ahead and close out the code.
So this is quite a journey that we've been on, don't you think?
If we recap the whole thing right now,
what we can see is at the very beginning,
we imported the three libraries that we wanted to work with.
We've got JSON data, we've got the operating system
in case we need to exit out of this code,
in case there's an error, and then WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
in case there's an error, and then
we have the request library for performing those HTTP posts
and get requests.
The post is what's going to be creating data, or in our case,
sending in our user name and password,
and the get request is what retrieves data.
Then we set up some variables that we
store our username and password as well
as the URLs in the headers that we want to work with.
We move into the login block of code where we try to log in.
We send in a post request containing our user name
and password.
We store the data that comes back in a giant variable, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
We store the data that comes back in a giant variable,
and then we whittle it down one step at a time.
We get just the headers.
Then from the headers, we get just the token,
and then we add the token to our own header
that we can use in the later request.
Then, as we scroll down more, we build our next variable,
which is going to be the endpoint that holds
all of the application data.
We send in a get request to get data from those application
endpoints, and we specified our token in the headers
that we provide there. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
that we provide there.
We parse the data that comes back out from JSON into Python,
and then we can do whatever we want to with it,
and then we cleanly exit the code.
That's how we interpret a basic Python script.
Go back through this as many times as you need,
and try to read it to yourself in as plain as English
as possible.
Things like try to get data from this URL using this token.
If there is an exception, throw this error or print this error.
Finally, exit the code cleanly. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Finally, exit the code cleanly.
That's really the purpose and the intent of Python.
So I hope this has been informative for you,
and I'd like to thank you for viewing.