>> The Azure SDK for Go
provides the packages you need to use
the Azure REST APIs for
managing and using Azure services.
There's a collection of these samples on
Github that'll show you how to use the SDK.
Joel Hendrix is here to demo and explain how it works,
today, on Azure Friday.
Welcome to Azure Friday.
I'm Erik St Martin and today with me,
I have Joel Hendrix who is going to teach
us all about the Go SDK for Azure.
>> Hi, Erik. It's great to be here.
>> So, let's talk a bit about
the Azure STK and what we can use it for.
>> Yeah. So, all of
our Azure services are documented
in open API specs also known as Swagger.
So, we generate a variety of SDKs from the Swagger specs.
What I work on is in the Go programming language.
So, any services that Azure exposes through OpenAPI,
we're able to generate Go code and
then consumers can use that in their applications.
>> What might you want to use these things for?
>> Well, it really depends.
Managing Azure services like creating container's, VM,
websites, to data plan operations,
like Blob storage, Key Vault,
interacting with the Redis cache.
It's just up, almost every service that
we've got available is exposed through our SDK.
>> That's great. I think you said you had a demo.
>> I do. So, let's bring our VS code here.
So, this is a demo
creating a Container Instant Service through Azure.
So, there's a couple of things here to notice.
So, the first thing is we're using
the Container Instance package from
the 2018-06-01 API version.
So, I'd like to talk just a little bit more about that.
If you look at our Swagger specs,
so here these are our OpenAPI specs,
I was talking about earlier.
As you can see, we've got different API versions.
From these, we generate Go packages.
So, you might be wondering,
well, there's a lot of API versions here,
how don't I know which one I want to pick.
Well, if it's new code,
you probably want to pick the latest one.
If you're migrating existing services
that use an older API version,
you can completely stick with that API version
for as long as it's available.
We partition them this way,
so that as new API versions are introduced,
which might contain breaking changes,
it keeps your code isolated from that.
So, if I'm consuming 2018-06-01,
Container Instant service creates,
they want to add
some new functionality that might include
breaking changes from this version.
They might release like a 2018-09-01,
completely side by side with your code
and you're not going to be impacted by that at all.
>> Now. These other versions,
are these set or
will updates like for
bug fixes the things that don't have braking changes?
Will those occur within this?
>> So, yes, they will.
Sadly, sometimes we will see
breaking changes in here from time to time.
It's rare but it does happen for a handful of reasons,
but mostly you will see
bug fixes and no breaking changes at all.
So, let's continue here.
So, going back to the demo code.
Here, we are importing the Container Instance package and
the resources package that
we can use to create a Resource Group.
So, the first thing that we need to
do is we need to create what we call an Authorizer.
This is what you'll use to authenticate with Azure,
to get you an access token,
so that you can actually consume the services.
So, we've tried to make this
simpler over the last couple of months.
There was a lot of boilerplate code that you ended up
copying pasting between projects and we said,
"Well, let's just see if we can put something in
the box that you don't have to do that."
So, we added this new function
called "NewAuthorizerFromEnvironment".
If we go look at this guy.
So, he'll look in a couple of different
places to try and find
credentials through environment variables.
So, client credentials like your Client ID,
your Client Secret Certificate,
if you are using Certificate Authentication,
username password for
some Azure services if you don't have 2FA
enabled and then finally MSI authentication
if your app is actually running in a VM,
inside of Azure and is using Managed Services Identity.
So, with that, really
all you need to do is call this function and it will
look to your environment and it
will find what you've
got set and it will create an authorizer from that.
So, next, what we do is from our resources package,
we create a "NewGroupsClient" and this is what
we can actually use to create a new resource group.
So far our demo here we've created,
we'll be creating a resource group called demoresgroup1.
Let's see. So, we call "CreateOrUpdate",
which starts the asynchronous operation
to create the.Resource.Group.
Then we use, goes
the first statement to
defer deleting the resource group until the very end.
So, this will run once
the main function exits
and we'll clean up all the resources that we created.
>> Essentially, that program
is just creating a research group
at the beginning and then everything
it does and at the very end,
it's going to just to delete the resource
group and just clean up
anything that it created inside of whatever it'll do.
>> Exactly. So, one
of the things I want to call out here,
so we have these asynchronous operations
are sometimes we call them Long-Running
Operations, LLOs for short.
There's a couple of different patterns
that you can use for that depending,
so here, so if we look at delete,
you'll see here, it returns as 'GroupsDeleteFuture'.
Since, we don't really
care to wait for the Resource Group
to finish deleting for the purposes of this demo,
we just call delete and then we exit.
So, it's fire and forget.
So, next, what we do is we are going
to create a Container Group's Client.
So, here's where we use
this Authorizer that we created above,
and so here's our create Long-Running operations.
So, here, we capture the future that's returned from us,
because we want to wait for this thing when it's done,
so that we can do some stuff afterwards.
So, originally,
we used to return these things back through channels,
it was clunky, it didn't work too well.
People weren't too happy with it. So, we
have this whole future concept
to capture asynchronous operations.
So, the future that's returned,
you can do a couple different things with it.
So, the first one that we
saw was like this just fire and forget,
like if you're deleting a Resource Group and you
don't need to block and waiting for that to be completed,
you just call the operation
and you move on with your code.
So here, since we
actually want to wait for the creation to complete,
we call this "WaitForCompletionRef"
on the future that was returned to us.
By default, it will wait
for 15 minutes as configured on the default client.
Now, there's been a couple of
times where I've seen some things that
take a really long time to deploy.
So, you can change this polling duration, if you need to.
So, you can see I've got
an example of how you might set it to 20 minutes.
So, here we wait for this to
complete and then we
have another pattern as well with a feature.
So, maybe you don't want to block,
you want to maybe do some
other work while it's going but you
need to come back and check the stats from time to time,
because eventually you do care
when the operation is completed.
So, what you can do is you
can call the future and say, "Hey, are you done?"
The future is going to say it's going to check with
the asynchronous operation on Azure and
you're going to get back a response, Yes/No.
So, if you're not done,
then maybe we can do some other stuff
while we're waiting,
maybe we want to print the status or send it to a log
or what have you
and then we're going to loop around and try again.
Then finally, once we've created our Container.
Group we can list them.
So, one of the other things that we
added for page of our operations,
so some data sets might give you
back information and pages,
so maybe like 10 at a time, a 100 at a time.
>> You have 10,000 users and you're
trying to iterate over all of them.
>> Exactly. So, those APIs will return
a page and then you can just iterate over the page.
You can ask it if it's does it have more information?
If it does, then you just move to the next page
and then that page will give you the range of values.
Now, this kind of chunks it up into,
you'll notice this value here is
a slice of Container Groups.
So, maybe you might
care to keep everything segregated page by page.
We also have this.
We also generate this method called,
it'll have a suffix of Complete
and that returns an iterator.
That will just seamlessly move
across page boundaries as required.
So, if you have 10,000 users and
you just want to iterate over and you don't
care about the page boundaries,
iterators a great option to do that. So, if we-
>> So, you don't really know that behind the scenes,
it's going out and fetching a new page,
you're just iterating over each individual-
>> That's correct.
>> If you had. The have entire collection at once.
>> Right. Exactly. So, if we can run our example here.
It we'll take a little while because creating
this container group takes
about a couple of minutes or so.
But it will just run in the background while
we and I've already.
So, in this command window,
I've already set up all of
my environment variables with all of my credentials.
So, the authorizer knows to look for
a Client ID and a Client Secret Tenant ID.
>> Just looking for it and like its default locations.
>> Correct.
>> I know the application itself
is just going to go out and create just a.
Container Group inside of ACI. Okay.
>> Yeah. We can go pick on this while it's running.
So, if we go look at our Resource Group
is this demoresourcegroup1 that the app created
and here is the Container Group that
is, status is running.
That means that it has finished deploying.
It seems like it's still waiting for it to
complete and then once this done.
>> We're done.
>> So, at least, we grabbed the page,
it was only one thing that it found,
of course because we only created the one.
If we go back,
we should see that our Resource Group,
that's probably in progress of
being deleting, as you can see there.
>> So, it's cleaning up after itself.
We're going to start with the first statement.
>> Today we were learning about the Azure SDK for Go,
here on Azure Friday.
Không có nhận xét nào:
Đăng nhận xét