1

I have created Virtual machine on Azure portal via Fluent API class in c#. my c# code is:

public JsonResult createVM()
{
    try
    {
        IAzure azure = Authenticate(subscriptionId);
        azure.VirtualMachines.Define(vmName)
             .WithRegion(location)
             .WithExistingResourceGroup(ResourceGroupName)
             .WithExistingPrimaryNetworkInterface(networkInterface)
             .WithSpecializedOSDisk(managedDisk, operatingSystem)
             .WithSize(vmSize)
             .Create();

        //Here i want complete response from above code..if success.
        return Json(string.Empty);
    }
    catch (CloudException ex)
    {
        Response.Write(ex.Response.Content);
        return Json(string.Empty);
    }
}

i get response in catch block if execution failed. but we need response if execution succeed .

Presto
  • 888
  • 12
  • 30
Priti kumari
  • 79
  • 2
  • 12

3 Answers3

0

Assign the azure VM creation code piece to a var. Check if that var is not null. If yes then the VM got created successfully. In case of exception you obviously go to the catch block. And if you want you can validate any property of the newly created VM object like they do it in this unit test.

Aravind
  • 4,125
  • 1
  • 28
  • 39
  • yes we can do that.but i want complete httpresponse text .how we achieve this. – Priti kumari Jun 27 '18 at 07:14
  • 1
    it returns an object itself. if you are specific about http response then you can use the azure ARM rest api PUT https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{vmName}?api-version=2017-12-01 – Aravind Jun 27 '18 at 14:53
0

Since you are using Azure Management Libraries for creating your Azure VMs, the SDK has automatically converted all the successful response into IVirtualMachine, you could just access the IVirtualMachine instance to retrieve all properties you want instead of the directly accessing the original HTTP response.

You could follow PrintVirtualMachine(IVirtualMachine virtualMachine) under here to retrieve the properties you expected and construct a new anonymous class which would contain your VM properties, then return it to your client as follows:

return Json(new
{
    ComputerName = linuxVM.ComputerName,
    PowerState = linuxVM.PowerState,
    ProvisioningState = linuxVM.ProvisioningState
    .
    .
});

I can't understand why you want the original HTTP response. But if you still insist on just retrieving the pure HTTP response, you need to follow the suggestion commented by Aravind to explicitly send the REST API Virtual Machines - Create Or Update with the related authorization by yourself. For authentication, you could follow Authentication API to access subscriptions to register your AAD app for accessing https://management.azure.com/ to create your Azure VMs. At this point, you need to do everything by yourself and you could control this process.

Bruce Chen
  • 18,207
  • 2
  • 21
  • 35
0

I am able to trace Log for Microsoft.Azure.Management.Fluent class. Follow Follow this link- Log and Trace section

i have log response in database:

 /// <summary>
    /// Here we can handle response.insert response in database
    /// </summary>
    /// <param name="invocationId">The invocation identifier.</param>
    /// <param name="response">The response message instance.</param>
    public void ReceiveResponse(string invocationId, HttpResponseMessage response)
    {
             logapResponse(response);

    }
Priti kumari
  • 79
  • 2
  • 12