Now that we've set up the basics of our Python script,
it's time to start doing things.
And the first thing that a Python script
does with the REST API is typically, get logged in.
Just like we said when we were talking
about APIs in a previous skill, we log in to a website.
Well, guess what?
Applications have to log in as well when
it interacts with an API.
So what we have to do first, the first block of code
that we're really going to start interpreting,
is how do we get our application, our Python script,
logged into FirePOWER Management Center WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
logged into FirePOWER Management Center
so that way any subsequent requests
or subsequent blocks of code can use that already
authenticated session.
That's what this section is all about.
Let's get going.
So we did a great job setting up what
we're supposed to be doing.
When we're building this script, we imported some info,
we set up some variables so that we
can reuse that stuff later down the road
without having to type it all.
Now, it's time to perform the first big action,
and that's getting logged in to FirePOWER Management Center. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and that's getting logged in to FirePOWER Management Center.
Here's the big thing.
When you log into FirePOWER Management Center,
you click that little Submit button, what
really happens under the hood?
FirePOWER Management Center send back a token,
and your web browser holds that token.
That way anything you do after the fact just re
sends that token it says, hey, I'm already authenticated.
Well, our API works the exact same way.
It's going to be sending off your username and password
to FMC, and FMC returns a token. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
to FMC, and FMC returns a token.
So our code has to be able to send off
that information first of all, but then receive and hold
that token.
That way any requests we do after the fact, we can
say, and look, here's my token.
I'm already authenticated.
That's what this batch of code does, but beyond that,
this batch of code also handles, what do we
do if it doesn't work right?
What if there is an error?
So there's a lot going on during this login request
but you're going to learn a lot about Python.
Let's jump into it.
So now, if I scroll down, we're starting with lines 13, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So now, if I scroll down, we're starting with lines 13,
and you see immediately, we've got
this thing that's called try.
That's literally what it's doing.
It says, try to run all of this code that's
indented right here.
And if there's any problem, if there's an exception or error,
meaning maybe it's going through this line of code
or this line of code or this line of code
or this line of code, anything that happens
in this indented that doesn't work and it breaks,
jump down to the exception. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
jump down to the exception.
This, in Python, is called a try-except block.
And in a lot of other languages, it's called a try-catch block.
In fact, Python is the only one that I've ever
seen that calls this an except block.
Pretty much every other language that I've ever worked with
calls it a try-catch block.
But the idea with Python is that it's very specific about what
stuff gets indented.
It knows that when you say try, followed by a little colon,
do everything under the indentation. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
do everything under the indentation.
And the except block says that if there is a problem,
grab that exception, and then here I've said,
print it out to the terminal.
Basically, when you run the code, if there is a problem,
let us know what that problem is.
Print out your little error right there.
So now that we know that what we're doing is we're first
going to try to run a block of code, and if there's a problem,
print that error out-- they also call this throwing an error
or raising an error--
we're now going to focus on what is it that we're trying to do. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
we're now going to focus on what is it that we're trying to do.
Actually, let me clear the screen like this,
and I'm going to mark this out right here,
and we're going to focus on just this line of code.
I'm even going to clean off the screen a little bit like this.
This is where your familiarity with the HTTP protocol
really needs to shine.
You see, HTTP performs actions that they call methods.
For instance, if you're just trying to browse to a website
and retrieve data-- maybe you're just trying to browse
a shopping website-- WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
a shopping website--
you'll be doing what's called a get request.
When you go to google.com or cbtnuggets.com,
for the first time, you're performing
a get request because you're getting the HTML documents,
you're getting the CSS documents,
you're getting the JavaScript documents.
However, you may see a little Search field right there.
There's a little text box, and you can type whatever text
you want into that Search box.
Then you see a big Submit button.
When you type into these Search fields, you're creating data. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
When you type into these Search fields, you're creating data.
You're creating new information, and you're
telling it to perform some action with that information.
When you provide inputs into a website
and you tell it to perform some action,
this is no longer a get request.
This is called a post request.
So if you think about it, when you browse to a website
and you see a login page that's got username
and a little field for you to type in your username
and password and a little field for you
to type in your password and then a Go button, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
to type in your password and then a Go button,
guess what you're doing?
When you type in your username, when you type in your password
and you click that Go button, you
are posting your log in information into the website,
and that website is supposed to do something
with this post request.
Typically, it validates that you're the correct user
and returns to you some sort of token or cookie
so that you can stay logged in.
That's exactly what we're doing here.
And if you recall, scrolling up just to here,
we imported the request library so that we
could work with HTTP endpoints. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
could work with HTTP endpoints.
So now when I scroll back down, you
can kind of see exactly what we're doing right here.
With the request library, we're going
to be performing a post request.
We're going to be sending in our data to the URL
and the login your URL endpoint that we specified above.
We're specifying authentication.
This is our username and password
that we're storing in a variable.
Now, this little thing just verify equals false.
This is if you're using a self-signed certificate. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
This is if you're using a self-signed certificate.
This is saying, don't verify the certificate on the website.
That FirePOWER Management Center website sometimes
doesn't have a valid certificate,
so by saying verify equals false,
it's just going to ignore the correct certificate.
But do you see what's happening here?
We're using the post HTTP method to send in data,
our username and password.
And we specify the URL that we're
sending it into right here.
So we say request.post then open parentheses.
The first thing that comes is the URL. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
The first thing that comes is the URL.
The second thing that comes is authentication
that's specified by username and password,
and then that's really it if you don't
have a self-signed certificate.
This line of code, this post does actually
perform the post request.
It actually sends in that information
that we stored in the variables above.
So pause right now in this time and try and piece
all of these together.
See that this URL gets squeezed in right here and the login
your URL gets squeezed in right here WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
your URL gets squeezed in right here
and the username and password gets squeezed in right here.
And that's how it knows where to send a post request
and what data it needs to send into that post request too.
Now, that we've uncommented this out,
take a look at what we've got here.
We fire off our post request to FMC,
and FMC is going to send back a response.
So this block of code performs the post action,
and it gets a response back.
That response is what we're storing in this variable, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
That response is what we're storing in this variable,
and that response contains all sorts of data.
It includes a payload of information,
but it also includes headers, and in those headers,
it contains our token.
So in the next line of code right here, what we're doing is
we're parsing out just the headers that came back
from our response, and we're holding just the headers
in this variable here.
That we can easily work with just the info and the headers.
I don't have to worry about any other information that WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
I don't have to worry about any other information that
came back in the response, like any text or the payload
or status codes or anything like that.
So we've posted in the data.
We've now grabbed just the headers
that came back in the response.
And from those headers, I can grab the specific header that
we need, the x-auth-access-token.
This is our token that we need in subsequent requests.
Once we've gotten just that token,
we store that token in a variable called token.
And the last thing we need to do is WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And the last thing we need to do is
we need to make sure that we actually did get a token.
So we'll say, if the token is empty--
that's what none means--
print out a little message that says
we failed to get the token, try again,
and then exit the script.
And then the very last thing that we do
is we add our token to our own headers
that we specified above when we specified the content
type of JSON.
Now look, I know that's a lot, but this is
what kind of goes in the APIs.
Moving into network automation is WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Moving into network automation is
kind of like taking two steps back so that you
can make a huge leap forward.
Not only do you have to learn about how Python itself works,
but now you also have to learn the nitty-gritty details
about the HTTP protocol.
This is why I say, again, feel free to pause.
Go back, watch this content over and over again
if that's what it takes because when I was learning
this stuff not too long ago, this
was stuff that took me a week or more to finally grasp
and get the hang of.
This is OK to repeat over and over again. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
This is OK to repeat over and over again.
But let's repeat what we've done so far in this particular code.
We're looking at a try and except block.
That way we're making sure that we're
trying to perform these login requests.
We're trying to perform the post request to grab the header.
And if anything goes wrong, we can just back out of it
and say, this error occurred when
we were trying to post it in.
Here, on this particular block of code,
we post in to this URL what our username and password are.
And then we store the tire a response in a variable WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And then we store the tire a response in a variable
called login response.
That response contains lots of stuff.
It contains a payload of data.
It contains a status code.
It contains headers, and those headers
are what we really care about.
So in the next line, we say, from the login response,
everything that came back, grab just the headers.
I only care about the headers.
So we store those headers in a variable called
just-the-response-headers.
Once we have just-the-response-headers, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Once we have just-the-response-headers,
I can say, get me this particular header.
I only want this one header.
That's the x-auth-access-token.
That's the token that was returned
upon a successful login request.
And I stored that token in a variable by itself.
You see how we kind of whittling it down a little bit?
We've got this huge response that we store in a variable.
Then we grab just the headers, and we
store that in a variable.
Then we grab just one header, the x-auth-access-token that we WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Then we grab just one header, the x-auth-access-token that we
store in a variable called token.
We validate that that token isn't empty.
It can't be none.
It has to add some info.
And lastly, we add that token to the headers
that we created above.
That way in our next request, we can just say,
hey, use those headers that specify what my token is.
We don't have to worry about copying and pasting tokens
over and over again.
We just stored the token in a variable
and then tacked it onto our headers
that we're going to use in the next Nugget.
So that's getting logged into a REST API. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So that's getting logged into a REST API.
Honestly, this is one of the hardest parts
about working with an API in any platform
is just getting authenticated.
But that's how you get authenticated to FirePOWER
Management Center Security Appliance.
I hope this has been informative for you,
and I'd like to thank you for viewing.