3. Listing your services and containers
You can save a lot of time if
you learn how to automate your deployments instead of doing them by hand
through the portal. We’re going to start this section by showing some
code you can use to get a list of the service and storage accounts
you’ve created in Azure. This code is fairly primitive and uses the REST
API directly. We’ll eventually start using a tool that will abstract
away the raw REST so that you have something nicer to work with.
You’re going to use the WebRequest
class to work with the REST call you’ll be making. You need to pass in
the URI of the call you want to make. The following listing shows how to
use REST to query for a list of services.
Listing 1. Querying for the list of services with REST
In the previous listing, you pass in a string that includes the root of the call, https://management.core.windows.net/, and the subscription ID (which can be found on your Accounts tab in the portal) at . You pass this string because you want a list of the
services that you’ve created. Every request into the management service
needs this base address. Because for this example we want to include a
list of the hosted services you have in your account, add /services/hostedservices to the end of your URL.
You also need to attach a
version header and your certificate for authentication. The version
header tells Azure which version of the management service you intend to
call. Right now the latest version to call is the one that was
published in October of 2009, so let’s use that one. The certificate is
easily attached at line .
You’re attaching it from the file you generated above. You could have
used the certificate that’s in your secure certificate store.
When you run this code, the
result you get in raw XML format is shown in the following listing. All
three services that are running in your subscription (aiademo1, aiademo2, and aiademo3) are listed in HostedService elements.
Listing 2. The raw XML that comes back from our request
After you’ve received the XML,
you can use something like Language-Integrated Query (LINQ) to XML to
parse through the results. You’ll want the URL for each service for
later when you’re calling back to reference that particular service. You
can use the same code to get a list of storage accounts in your Azure
account by changing the address of the request from /hostedservices to /storageservices.
You’ll be able to use these
endpoints to see the storage and service accounts you have. This part
of the API is read-only. To create service or storage accounts, you’ll
have to use the portal.
Now that you can make simple
queries of the management service, let’s flip over to csmanage.exe and
use it to deploy a simple web application to Azure.