Frank Fu Frank Fu - 2 months ago 11
C# Question

Get status of slot swap on Azure websites?

I'm building a Web App named

fooapp
on Azure's App Service platform and is configured to have slot named
slot1


Currently I'm programatically swapping my slot named
slot1
into my actual web app named
fooapp
via Azure's ARM API consumed by a HTTP client written in C# . The possible endpoints can be explored using the Resource Explorer.

This works but swap itself takes around 1-2 minutes so I would like to notify myself via email when the swap is complete. Is there an endpoint to call for the status for the swap?

Here is the code that's making the HTTP Json Post to swap
slot1
with
fooapp
in C#.

var httpClient = new HttpClient();
var uri = "https://management.azure.com/subscriptions/{mysubscriptionid}/resourceGroups/{myresourcegroup}/providers/Microsoft.Web/sites/fooapp/slots/slot1/slotsswap?api-version=2015-08-01";
var response = httpClient.PostAsJsonAsync(uri, new { targetSlot = "production" });


Ideas anyone? I've tried scouting for documentation but with the ever expanding features provided by Azure it's desert dry

Answer

You can use the Activity Log API to keep track of the status of your slots swapping. Here is a simple sample call of the REST API.

HTTP Method:
GET

Absolute Uri:
https://management.azure.com/subscriptions/{mysubscriptionid}/providers/microsoft.insights/eventtypes/management/values?api-version=2015-04-01&$filter=eventTimestamp ge '2016-09-08T01:53:03.0422573Z' and eventTimestamp le '2016-09-08T02:53:03.0422573Z' and resourceUri eq '/subscriptions/{mysubscriptionid}/resourcegroups/{myresourcegroup}/providers/Microsoft.Web/sites/fooapp/slots/slot1'&select=Authorization,Caller,CorrelationId,Category,EventTimestamp,OperationName,ResourceGroupName,ResourceUri,Status,SubscriptionId,SubStatus

For this REST API, there are a few things you need to be careful with.

  1. Be aware that you might need URL encoding.
  2. You need to change eventTimestamp ge '2016-09-08T01:53:03.0422573Z', setting "eventTimestamp" to be greater than or equal to a timestamp that just before you call the slots swapping.
  3. Similar for eventTimestamp le '2016-09-08T02:53:03.0422573Z'. You should set "eventTimestamp" to be less than or equal to the current timestamp.

There are 3 possible logs you are going to get, that indicating you slots swapping succeeds.

First one, "Started".

Authorization     :
                    Scope     : /subscriptions/{mysubscriptionid}/resourcegroups/{myresourcegroup}/providers/Microsoft.Web/sites/fooapp/slots/slot1
                    Action    : Microsoft.Web/sites/slots/slotsswap/action
                    Role      :
                    Condition :
Caller            : <your Azure account>
CorrelationId     : xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Category          : Administrative
EventTimestamp    : 2016-09-08 2:48:39 AM
OperationName     : Microsoft.Web/sites/slots/slotsswap/action
ResourceGroupName : {myresourcegroup}
ResourceId        :
Status            : Started
SubscriptionId    : {mysubscriptionid}
SubStatus         :

Second one, "Accepted".

Authorization     :
                    Scope     : /subscriptions/{mysubscriptionid}/resourcegroups/{myresourcegroup}/providers/Microsoft.Web/sites/fooapp/slots/slot1
                    Action    : Microsoft.Web/sites/slots/slotsswap/action
                    Role      :
                    Condition :
Caller            : <your Azure account>
CorrelationId     : xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Category          : Administrative
EventTimestamp    : 2016-09-08 2:48:41 AM
OperationName     : Microsoft.Web/sites/slots/slotsswap/action
ResourceGroupName : {myresourcegroup}
ResourceId        :
Status            : Accepted
SubscriptionId    : {mysubscriptionid}
SubStatus         : Accepted

Third one, "Succeeded".

Authorization     :
                    Scope     : /subscriptions/{mysubscriptionid}/resourcegroups/{myresourcegroup}/providers/Microsoft.Web/sites/fooapp/slots/slot1
                    Action    : Microsoft.Web/sites/slots/write
                    Role      :
                    Condition :
Caller            : <your Azure account>
CorrelationId     : xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Category          : Administrative
EventTimestamp    : 2016-09-08 2:49:44 AM
OperationName     : Microsoft.Web/sites/slots/write
ResourceGroupName : {myresourcegroup}
ResourceId        :
Status            : Succeeded
SubscriptionId    : {mysubscriptionid}
SubStatus         :

If you are using Service Principal, the "caller" in the outputs may be different. If the slots swapping failed, you will get a "Failed" for the second or the third log.

Comments