Share via


VMNetworks Collection [SPFSDK][VMMREF]

 

Applies To: Windows Azure Pack

The VMNetworks collection gets, creates, updates, or deletes virtual networks for Virtual Machine Manager (VMM).

For more information about the placeholders used in the URI, see URL placeholders.

Namespace

OData Collection URI

VMM

https://{server-name}:{auth-port}/{subscription-id}/services/systemcenter/vmm/VMNetworks

Supported HTTP Verbs

Create (POST)

Read (GET)

Update (PUT)

Delete (DELETE)

True

True

True

True

Operations

Example: Create a VM network and assign it to a virtual network adapter

Each of the four operation topics linked above contain basic code examples.

The following example demonstrates creating a virtual network along with all required dependencies and then assigning it to a virtual machine.

  1. Create a VMNetwork [SPFSDK][VMMREF] object.

  2. Create a VMSubnet [SPFSDK][VMMREF] object and assign it to the VMNetwork.

  3. Create a StaticIPAddressPool [SPFSDK][VMMREF] object and assign it to the VMSubnet.

  4. Get the instance of a VirtualMachine object, and shut it down if it is running.

  5. Get and assign the first VirtualNetworkAdapter [SPFSDK][VMMREF] of the VirtualMachine to the VMNetwork created in step 1.

  6. Starts the VirtualMachine if it the code shut it down.


// Ensure that when we get an entity over and over, we always get the latest from the service.
vmmService.MergeOption = System.Data.Services.Client.MergeOption.OverwriteChanges;

// First, create our virtual machine network.
Guid stampId = new Guid("56ebc6dc-f63c-46e2-8438-2967e0ad83bc");
SpfVMM.VMNetwork network = new SpfVMM.VMNetwork();

network.Name = "My Network 1 - User1";
network.StampId = stampId;
network.LogicalNetworkId = new Guid("ba2af885-daf7-467e-b78d-af75a05fe5f0");

vmmService.AddToVMNetworks(network);
vmmService.SaveChanges();

// Second, we need a subnet for the VMNetwork before it can be assigned to an adapter.
SpfVMM.VMSubnet subnet = new SpfVMM.VMSubnet();
subnet.Name = "Default Subnet";
subnet.StampId = stampId;
subnet.Subnet = "192.168.10.0/24";
subnet.VMNetworkId = network.ID;

vmmService.AddToVMSubnets(subnet);
vmmService.SaveChanges();

// Third, we need an ip address pool to auto assign addresses for the adapter.
SpfVMM.StaticIPAddressPool pool = new SpfVMM.StaticIPAddressPool();
pool.Name = "Default Pool";
pool.IPAddressRangeStart = "192.168.10.100";
pool.IPAddressRangeEnd = "192.168.10.200";
pool.VMSubnetId = subnet.ID;
pool.Subnet = "192.168.10.0/24";
pool.StampId = stampId;

vmmService.AddToStaticIPAddressPools(pool);
vmmService.SaveChanges();

// We must get the virtual machine and assign its network adapter to the virtual network we created.
Guid targetVirtualMachineId = new Guid("ee9fb3ed-c2f1-4ed5-9597-0a30c69d17f8");

// We need to expand the VirtualNetworkAdapters link on our query for the .VirtualNetworkAdapters collection property 
// to be filled out, otherwise, it will be empty.
SpfVMM.VirtualMachine targetVM = vmmService.VirtualMachines.Expand("VirtualNetworkAdapters")
                                    .Where(v => v.StampId == stampId && v.ID == targetVirtualMachineId).FirstOrDefault();

// This variable lets us know that we shut down the VM, so at the end, we should start it.
bool virtualMachineWasPowered = false;

// The virtual machine is currently running, it must be powered off to change the network.
if (targetVM.VirtualMachineState != "PowerOff")
{
    Guid jobid = new Guid();
    virtualMachineWasPowered = true;
    targetVM.Operation = "Shutdown";

    // Listen to the response event to find the job ID associated with this request.
    EventHandler<System.Data.Services.Client.ReceivingResponseEventArgs> responseReceived = (o, e) =>
        {
            string jobHeaderValue = e.ResponseMessage.GetHeader("x-ms-request-id");

            if (!string.IsNullOrEmpty(jobHeaderValue))
                jobid = Guid.Parse(jobHeaderValue);
            else
                throw new Exception("Expected job GUID to be returned when shutting down the VM.");
        };

    vmmService.ReceivingResponse += responseReceived;

    vmmService.UpdateObject(targetVM);
    vmmService.SaveChanges();

    // Stop listening to the response event.
    vmmService.ReceivingResponse -= responseReceived;

    // Loop and check for the job status every 3 seconds.
    do
    {
        var job = vmmService.Jobs.Where(j => j.StampId == stampId && j.ID == jobid).FirstOrDefault();

        if (job.IsCompleted == true)
        {
            // Double check that the VM is indeed powered off.
            targetVM = vmmService.VirtualMachines.Expand("VirtualNetworkAdapters")
                        .Where(v => v.StampId == stampId && v.ID == targetVirtualMachineId).FirstOrDefault();

            if (targetVM.VirtualMachineState != "PowerOff")
                throw new Exception("Tried to shut down the VM, but it is still running.");

            break;
        }

        // Sleep for 3 seconds so we do not hammer the service.
        System.Threading.Thread.Sleep(new TimeSpan(0, 0, 3));

    } while (true);
}

// The virtual machine was either already stopped, or we stopped it. Now we can look at its 
// virtual network adapters and assign the first one to the new VM network.
if (targetVM.VirtualNetworkAdapters.Count > 0)
{
    targetVM.VirtualNetworkAdapters[0].VMNetworkId = network.ID;
    vmmService.UpdateObject(targetVM.VirtualNetworkAdapters[0]);

    vmmService.SaveChanges();
}

// Start the virtual machine if we shut it down.
if (virtualMachineWasPowered)
{
    targetVM.Operation = "Start";

    vmmService.UpdateObject(targetVM);
    vmmService.SaveChanges();
}

See Also

Resource Collections
VMNetworkGateways Collection [SPFSDK][VMMREF]
VirtualNetworkAdapters Collection [SPFSDK][VMMREF]
StaticIPAddressPools Collection [SPFSDK][VMMREF]
VMNetwork [SPFSDK][VMMREF]